summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@qt.io>2024-04-26 12:49:33 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2024-04-29 18:04:14 +0000
commita5b2239a8759d0f526412a1d14468f2cbbd53544 (patch)
treeb1324578fbf4726dfcc2492cd6b91872e76a58d4
parentdc7b5f2377810c53f32f6ffd02ceb12884c1ea7b (diff)
CMake: Fix path of embedded .qm files in sub-directories6.7
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. Pick-to: 6.7.1 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>
-rw-r--r--src/linguist/Qt6LinguistToolsMacros.cmake3
-rw-r--r--tests/auto/cmake/linguist/CMakeLists.txt3
-rw-r--r--tests/auto/cmake/linguist/test_i18n_subdir/CMakeLists.txt9
-rw-r--r--tests/auto/cmake/linguist/test_i18n_subdir/subdir/CMakeLists.txt10
-rw-r--r--tests/auto/cmake/linguist/test_i18n_subdir/subdir/app1_de.ts12
-rw-r--r--tests/auto/cmake/linguist/test_i18n_subdir/subdir/main.cpp32
6 files changed, 68 insertions, 1 deletions
diff --git a/src/linguist/Qt6LinguistToolsMacros.cmake b/src/linguist/Qt6LinguistToolsMacros.cmake
index 485e49123..8bb5f9faf 100644
--- a/src/linguist/Qt6LinguistToolsMacros.cmake
+++ b/src/linguist/Qt6LinguistToolsMacros.cmake
@@ -739,9 +739,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 e08d12f5e..f557d134d 100644
--- a/tests/auto/cmake/linguist/CMakeLists.txt
+++ b/tests/auto/cmake/linguist/CMakeLists.txt
@@ -65,3 +65,6 @@ _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_filter_autogen_files)
_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;
+}