diff options
author | Joerg Bornemann <joerg.bornemann@qt.io> | 2024-02-22 09:46:29 +0100 |
---|---|---|
committer | Joerg Bornemann <joerg.bornemann@qt.io> | 2024-02-26 17:28:30 +0100 |
commit | 64dca647b3d5fced2fc51d8d77c0e896fe70aaf6 (patch) | |
tree | c600202900284b63cab41267f427fb5dfed9b06f | |
parent | 53923928f338f0e271c55021cc7f3be503f13ed7 (diff) |
CMake: Change behavior of automatic determination of .ts files
...according to the Qt 6.7 CMake i18n API review.
Add the NO_GENERATE_PLURALS_TS_FILE option to qt_add_translations and
don't generate a plurals .ts file if QT_I18N_SOURCE_LANGUAGE is in
QT_I18N_TRANSLATED_LANGUAGES.
Task-number: QTBUG-122396
Change-Id: I82e1828b8ce7367ba7f15b9cb1f6341441d806f3
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
(cherry picked from commit 908404d9f97880dd89905d37a3964c3d4a3337be)
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
-rw-r--r-- | src/linguist/Qt6LinguistToolsMacros.cmake | 26 | ||||
-rw-r--r-- | src/linguist/linguist/doc/cmake-macros.qdoc | 50 | ||||
-rw-r--r-- | tests/auto/cmake/linguist/test_i18n_auto_ts_file_names/CMakeLists.txt | 16 |
3 files changed, 71 insertions, 21 deletions
diff --git a/src/linguist/Qt6LinguistToolsMacros.cmake b/src/linguist/Qt6LinguistToolsMacros.cmake index 555b18d21..7cf6d89a9 100644 --- a/src/linguist/Qt6LinguistToolsMacros.cmake +++ b/src/linguist/Qt6LinguistToolsMacros.cmake @@ -535,7 +535,8 @@ endfunction() function(qt6_add_translations) set(options - IMMEDIATE_CALL) + IMMEDIATE_CALL + NO_GENERATE_PLURALS_TS_FILE) set(oneValueArgs LUPDATE_TARGET LRELEASE_TARGET @@ -565,14 +566,6 @@ function(qt6_add_translations) if(targets STREQUAL "") message(FATAL_ERROR "No targets provided.") endif() - if(NOT DEFINED arg_TS_FILES - AND NOT DEFINED arg_PLURALS_TS_FILE - AND "${QT_I18N_TRANSLATED_LANGUAGES}" STREQUAL "" - AND "${QT_I18N_SOURCE_LANGUAGE}" STREQUAL "") - message(FATAL_ERROR - "One of QT_I18N_TRANSLATED_LANGUAGES, QT_I18N_SOURCE_LANGUAGE, TS_FILES, " - "or PLURALS_TS_FILE must be provided.") - endif() if(DEFINED arg_RESOURCE_PREFIX AND DEFINED arg_QM_FILES_OUTPUT_VARIABLE) message(FATAL_ERROR "QM_FILES_OUTPUT_VARIABLE cannot be specified " "together with RESOURCE_PREFIX.") @@ -599,10 +592,19 @@ function(qt6_add_translations) list(APPEND arg_TS_FILES "${arg_TS_FILE_DIR}/${arg_TS_FILE_BASE}_${lang}.ts") endforeach() - # Determine the path to the native .ts file if necessary. - if(NOT DEFINED arg_PLURALS_TS_FILE AND NOT "${QT_I18N_SOURCE_LANGUAGE}" STREQUAL "") + # Default the source language to "en" in case the user doesn't use + # qt_standard_project_setup. + set(source_lang en) + if(NOT "${QT_I18N_SOURCE_LANGUAGE}" STREQUAL "") + set(source_lang ${QT_I18N_SOURCE_LANGUAGE}) + endif() + + # Determine the path to the plurals-only .ts file if necessary. + if(NOT arg_NO_GENERATE_PLURALS_TS_FILE + AND NOT DEFINED arg_PLURALS_TS_FILE + AND NOT "${source_lang}" IN_LIST QT_I18N_TRANSLATED_LANGUAGES) set(arg_PLURALS_TS_FILE - "${arg_TS_FILE_DIR}/${arg_TS_FILE_BASE}_${QT_I18N_SOURCE_LANGUAGE}.ts") + "${arg_TS_FILE_DIR}/${arg_TS_FILE_BASE}_${source_lang}.ts") endif() endif() diff --git a/src/linguist/linguist/doc/cmake-macros.qdoc b/src/linguist/linguist/doc/cmake-macros.qdoc index cef8dde2c..070860fa1 100644 --- a/src/linguist/linguist/doc/cmake-macros.qdoc +++ b/src/linguist/linguist/doc/cmake-macros.qdoc @@ -212,17 +212,17 @@ Pass \c{NO_GLOBAL_TARGET} to \c{qt_add_lupdate} to prevent this behavior. The name of this target can be overridden by setting the variable \c{QT_GLOBAL_LUPDATE_TARGET} before calling \c{qt_add_lupdate}. -//! [ts-native-language] -\section1 Native Language +//! [plurals-ts-file] +\section1 Plural Forms -The native language of a project is the language in which the source code +\l QT_I18N_SOURCE_LANGUAGE specifies the language in which the source code strings are written. For handling plural forms correctly, create an additional \c{.ts} file for that language that only contains translatable strings for plural forms. See \l{Handle Plural Forms} for details. -With \c PLURALS_TS_FILE you can specify the \c{.ts} file for the native language. -This file will only contain plural forms. -//! [ts-native-language] +With \c PLURALS_TS_FILE you can specify the \c{.ts} file for the source +language. This file will only contain plural forms. +//! [plurals-ts-file] \section1 Deprecated Command Signature @@ -361,6 +361,7 @@ qt_add_translations([target] [TS_FILE_DIR directory] [TS_FILES file1.ts [file2.ts ...]] [PLURALS_TS_FILE file.ts] + [NO_GENERATE_PLURALS_TS_FILE] [RESOURCE_PREFIX prefix] [OUTPUT_TARGETS variable-name] [QM_FILES_OUTPUT_VARIABLE variable-name] @@ -431,10 +432,41 @@ argument. By default, the \c{.ts} file names are constructed from \c{PROJECT_NAME}. You can specify a different base name with the \c{TS_FILE_BASE} argument. -\include cmake-macros.qdoc ts-native-language +\include cmake-macros.qdoc plurals-ts-file -If \l{QT_I18N_SOURCE_LANGUAGE} is set then the path to the native \c{.ts} file -will also be automatically determined. +A plurals-only \c{.ts} is automatically generated unless the option \c +NO_GENERATE_PLURALS_TS_FILE is specified. + +For example, +\badcode +project(myapp) +qt_standard_project_setup( + I18N_SOURCE_LANGUAGE en # optional - this is the default + I18N_TRANSLATED_LANGUAGES de +) +qt_add_executable(myapp ...) +... +qt_add_translations(myapp) +\endcode +creates the full translation file \c{myapp_de.ts} and the plurals-only file +\c{myapp_en.ts}. + +If you need a full translation of the source language, add it to +\l{QT_I18N_TRANSLATED_LANGUAGES} + +For example, +\badcode +project(myapp) +qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES en de) +qt_add_executable(myapp ...) +... +qt_add_translations(myapp) +\endcode +creates the full translation files +\list +\li \c{myapp_en.ts} +\li \c{myapp_de.ts} +\endlist \section1 Options diff --git a/tests/auto/cmake/linguist/test_i18n_auto_ts_file_names/CMakeLists.txt b/tests/auto/cmake/linguist/test_i18n_auto_ts_file_names/CMakeLists.txt index 08507388f..04e84edd4 100644 --- a/tests/auto/cmake/linguist/test_i18n_auto_ts_file_names/CMakeLists.txt +++ b/tests/auto/cmake/linguist/test_i18n_auto_ts_file_names/CMakeLists.txt @@ -21,6 +21,7 @@ foreach(f IN LISTS old_ts_files) endforeach() set(expected_files "") +set(unexpected_files "") # Check defaults for the deferred call. my_add_library(lib1 STATIC lib.cpp) @@ -84,12 +85,27 @@ qt_add_translations(TARGETS lib8) list(APPEND expected_files "${CMAKE_CURRENT_SOURCE_DIR}/test_i18n_auto_ts_file_names_nl.ts") +# Check whether we can turn off the generation of the plurals-only file. +my_add_library(lib10 STATIC lib.cpp) +set(QT_I18N_TRANSLATED_LANGUAGES cy) +set(QT_I18N_SOURCE_LANGUAGE an) +qt_add_translations(TARGETS lib10 NO_GENERATE_PLURALS_TS_FILE) +list(APPEND expected_files + "${CMAKE_CURRENT_SOURCE_DIR}/test_i18n_auto_ts_file_names_cy.ts") +list(APPEND unexpected_files + "${CMAKE_CURRENT_SOURCE_DIR}/test_i18n_auto_ts_file_names_an.ts") + function(check_ts_file_paths) foreach(filepath IN LISTS expected_files) if(NOT EXISTS "${filepath}") message(FATAL_ERROR "Expected file '${filepath}' does not exist.") endif() endforeach() + foreach(filepath IN LISTS unexpected_files) + if(EXISTS "${filepath}") + message(FATAL_ERROR "File '${filepath}' unexpectedly exists.") + endif() + endforeach() endfunction() if(CMAKE_VERSION VERSION_LESS "3.19") |