diff options
author | Joerg Bornemann <joerg.bornemann@qt.io> | 2024-04-26 12:49:33 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-04-30 04:19:22 +0000 |
commit | dff9c66033a82f91f6294a60bcf90d9d2fed8dc5 (patch) | |
tree | f85c764d5af1c6f58bacdfd1f49ca38f8d4b72ad | |
parent | 5b81351aba23d9e3d6fb334cfc98bb2e84627841 (diff) |
CMake: Fix path of embedded .qm files in sub-directories
Consider a project with sub-directories and a qt_add_translations call
in a sub-directory. Before Qt 6.7.0, the path to the embedded .qm files
was ":/i18n/foo_de.qm". With Qt 6.7.0, the path is
":/i18n/subdir/foo_de.qm".
The reason is that we're called qt6_add_resources with the BASE argument
"${CMAKE_CURRENT_BINARY_DIR}", and that is different if targets are
automatically collected at the end of the project's directory scope.
Fix this by passing the target's BINARY_DIR as BASE instead.
Fixes: QTBUG-124188
Change-Id: I6afd39c072e7dccc2ff5937c5cd312a119b68587
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
(cherry picked from commit 9a6c9cc926f3b87f44f8e1db1272e1773af62cd4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit a5b2239a8759d0f526412a1d14468f2cbbd53544)
6 files changed, 68 insertions, 1 deletions
diff --git a/src/linguist/Qt6LinguistToolsMacros.cmake b/src/linguist/Qt6LinguistToolsMacros.cmake index 0cae240c9..6f0fc3a0b 100644 --- a/src/linguist/Qt6LinguistToolsMacros.cmake +++ b/src/linguist/Qt6LinguistToolsMacros.cmake @@ -735,9 +735,10 @@ function(qt6_add_translations) if(NOT "${arg_RESOURCE_PREFIX}" STREQUAL "") set(accumulated_out_targets "") foreach(target IN LISTS targets) + get_target_property(target_binary_dir ${target} BINARY_DIR) qt6_add_resources(${target} "${target}_translations" PREFIX "${arg_RESOURCE_PREFIX}" - BASE "${CMAKE_CURRENT_BINARY_DIR}" + BASE "${target_binary_dir}" OUTPUT_TARGETS out_targets FILES ${qm_files}) list(APPEND accumulated_out_targets ${out_targets}) diff --git a/tests/auto/cmake/linguist/CMakeLists.txt b/tests/auto/cmake/linguist/CMakeLists.txt index 90be96016..54a538128 100644 --- a/tests/auto/cmake/linguist/CMakeLists.txt +++ b/tests/auto/cmake/linguist/CMakeLists.txt @@ -64,3 +64,6 @@ _qt_internal_test_expect_pass(test_translation_api) _qt_internal_test_expect_pass(test_i18n_auto_ts_file_names) _qt_internal_test_expect_pass(test_i18n_exclusion) _qt_internal_test_expect_pass(test_i18n_source_language) +if(NOT CMAKE_CROSSCOMPILING) + _qt_internal_test_expect_pass(test_i18n_subdir BINARY app1) +endif() diff --git a/tests/auto/cmake/linguist/test_i18n_subdir/CMakeLists.txt b/tests/auto/cmake/linguist/test_i18n_subdir/CMakeLists.txt new file mode 100644 index 000000000..4bc38b822 --- /dev/null +++ b/tests/auto/cmake/linguist/test_i18n_subdir/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(test_i18n_subdir) +find_package(Qt6 COMPONENTS Gui LinguistTools) +qt_standard_project_setup() +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") +add_subdirectory(subdir) diff --git a/tests/auto/cmake/linguist/test_i18n_subdir/subdir/CMakeLists.txt b/tests/auto/cmake/linguist/test_i18n_subdir/subdir/CMakeLists.txt new file mode 100644 index 000000000..c799968d8 --- /dev/null +++ b/tests/auto/cmake/linguist/test_i18n_subdir/subdir/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +qt_add_executable(app1 + main.cpp) +target_link_libraries(app1 PRIVATE Qt::Gui) +qt_add_translations(app1 + TS_FILES app1_de.ts + NO_GENERATE_PLURALS_TS_FILE +) diff --git a/tests/auto/cmake/linguist/test_i18n_subdir/subdir/app1_de.ts b/tests/auto/cmake/linguist/test_i18n_subdir/subdir/app1_de.ts new file mode 100644 index 000000000..924515f31 --- /dev/null +++ b/tests/auto/cmake/linguist/test_i18n_subdir/subdir/app1_de.ts @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE TS> +<TS version="2.1" language="de_DE"> +<context> + <name>main</name> + <message> + <location filename="main.cpp" line="17"/> + <source>Hello from main!</source> + <translation>Hallo aus der Hauptfunktion!</translation> + </message> +</context> +</TS> diff --git a/tests/auto/cmake/linguist/test_i18n_subdir/subdir/main.cpp b/tests/auto/cmake/linguist/test_i18n_subdir/subdir/main.cpp new file mode 100644 index 000000000..ed3035d18 --- /dev/null +++ b/tests/auto/cmake/linguist/test_i18n_subdir/subdir/main.cpp @@ -0,0 +1,32 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QtCore/QCoreApplication> +#include <QtCore/QTranslator> + +#include <iostream> + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + const QString qmFile = ":/i18n/app1_de.qm"; + std::cout << "Loading translation '" << qPrintable(qmFile) << "'..." << std::endl; + QTranslator *translator = new QTranslator(&app); + if (!translator->load(qmFile)) { + std::cerr << "Error: failed to load the translation" << std::endl; + return 1; + } + app.installTranslator(translator); + + std::cout << "Checking translated text..." << std::endl; + const QString translated = QCoreApplication::translate("main", "Hello from main!"); + if (!translated.startsWith("Hallo aus")) { + std::cerr << "Error: translation doesn't seem to work. " + << "The translated text is '" << qPrintable(translated) << "'" << std::endl; + return 2; + } + + std::cout << "All good." << std::endl; + return 0; +} |