Skip to content

Commit 302b7b0

Browse files
committed
CMake: Fix deferred qt_add_translations calls...
...if find_package(Qt6Core) is missing at the project directory level. Consider a project with a subdir. In the subdir, we have find_package(Qt6 COMPONENTS Widgets LinguistTools ...) ... qt_add_translations(mytarget TS_FILES mytarget_de.ts ...) such that the actual qt_add_translations call is deferred. In the top-level CMakeLists.txt we have no find_package call for Qt6. Then the deferred call will fail, because we defer to the project root, and there we're missing variables that have been pulled in by the find_package(Qt6 ...) call in the subdir. Fix this by adding a find_package(Qt6 ...) call when we're deferring qt_add_translations. The find_package call will do (almost) nothing if the packages have already been loaded. Pick-to: 6.7 6.8 Fixes: QTBUG-126167 Change-Id: I2083ebecfd64e67649906526d2f99bb202b03cb8 Reviewed-by: Alexandru Croitor <[email protected]>
1 parent 4be1823 commit 302b7b0

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

src/linguist/Qt6LinguistToolsMacros.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,14 @@ function(qt6_add_translations)
648648
endif()
649649
endforeach()
650650

651+
# The project-level CMakeLists.txt might miss a call to find_package(Qt6 COMPONENTS Core
652+
# LinguistTools). Then, subsequent function calls like qt_add_resources will fail. To
653+
# remedy this, pull the packages in.
654+
cmake_language(EVAL CODE
655+
"cmake_language(DEFER
656+
DIRECTORY \"${PROJECT_SOURCE_DIR}\"
657+
CALL find_package Qt6 COMPONENTS Core LinguistTools)")
658+
651659
# Schedule this command to be called at the end of the project's source dir.
652660
cmake_language(EVAL CODE
653661
"cmake_language(DEFER

tests/auto/cmake/linguist/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ _qt_internal_test_expect_pass(test_i18n_exclusion)
6666
_qt_internal_test_expect_pass(test_i18n_filter_autogen_files)
6767
_qt_internal_test_expect_pass(test_i18n_source_language)
6868
if(NOT CMAKE_CROSSCOMPILING)
69+
_qt_internal_test_expect_pass(test_i18n_find_package_in_subdir BINARY app1)
6970
_qt_internal_test_expect_pass(test_i18n_subdir BINARY app1)
7071
endif()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (C) 2024 The Qt Company Ltd.
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
cmake_minimum_required(VERSION 3.16)
5+
project(test_i18n_subdir)
6+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
7+
add_subdirectory(subdir)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (C) 2024 The Qt Company Ltd.
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
find_package(Qt6 COMPONENTS Gui LinguistTools)
4+
qt_add_executable(app1
5+
main.cpp)
6+
target_link_libraries(app1 PRIVATE Qt::Gui)
7+
qt_add_translations(app1
8+
TS_FILES app1_de.ts
9+
NO_GENERATE_PLURALS_TS_FILE
10+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE TS>
3+
<TS version="2.1" language="de_DE">
4+
<context>
5+
<name>main</name>
6+
<message>
7+
<location filename="main.cpp" line="17"/>
8+
<source>Hello from main!</source>
9+
<translation>Hallo aus der Hauptfunktion!</translation>
10+
</message>
11+
</context>
12+
</TS>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2024 The Qt Company Ltd.
2+
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3+
4+
#include <QtCore/QCoreApplication>
5+
#include <QtCore/QTranslator>
6+
7+
#include <iostream>
8+
9+
int main(int argc, char *argv[])
10+
{
11+
QCoreApplication app(argc, argv);
12+
13+
const QString qmFile = ":/i18n/app1_de.qm";
14+
std::cout << "Loading translation '" << qPrintable(qmFile) << "'..." << std::endl;
15+
QTranslator *translator = new QTranslator(&app);
16+
if (!translator->load(qmFile)) {
17+
std::cerr << "Error: failed to load the translation" << std::endl;
18+
return 1;
19+
}
20+
app.installTranslator(translator);
21+
22+
std::cout << "Checking translated text..." << std::endl;
23+
const QString translated = QCoreApplication::translate("main", "Hello from main!");
24+
if (!translated.startsWith("Hallo aus")) {
25+
std::cerr << "Error: translation doesn't seem to work. "
26+
<< "The translated text is '" << qPrintable(translated) << "'" << std::endl;
27+
return 2;
28+
}
29+
30+
std::cout << "All good." << std::endl;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)