qt_collect_translation_source_targets

Collects targets that are eligible for translation.

The command is defined in the LinguistTools component of the Qt6 package. Load the package with:

 find_package(Qt6 REQUIRED COMPONENTS LinguistTools)

This command was introduced in Qt 6.7.

Synopsis

 qt_collect_translation_source_targets(out_var [DIRECTORY path])

If versionless commands are disabled, use qt6_collect_translation_source_targets() instead. It supports the same set of arguments as this command.

Description

Collect targets that are eligible for translation in the given DIRECTORY and all subdirectories. If DIRECTORY is not specified, start the target collection at CMAKE_CURRENT_SOURCE_DIR.

The command stores the list of targets in the variable that is specified as first argument.

Excluding targets from translation

By default, all non-imported executable and library targets are eligible for translation.

Single targets can be excluded by setting the target property QT_EXCLUDE_FROM_TRANSLATION.

Targets below a certain directory can be excluded by setting the directory property QT_EXCLUDE_FROM_TRANSLATION.

When to call this command

The qt_collect_translation_source_targets command reads the BUILDSYSTEM_TARGETS directory properties. As a consequence, it only collects targets that already have been created. Targets that are created after qt_collect_translation_source_targets has been called are not collected.

To collect all targets of the build system, call qt_collect_translation_source_targets at the end of the top-level CMakeLists.txt or use cmake_language(DEFER CALL) to set up i18n at the end of the top-level directory scope.

Examples

Use the result of qt_collect_translation_source_targets as input for qt_add_lupdate.

 add_subdirectory(src)         # The actual application is defined here.

 # All targets that have been defined up to this point will be in i18n_targets.
 qt_collect_translation_source_targets(i18n_targets)
 qt_add_lupdate(SOURCE_TARGETS ${i18n_targets})

 # No targets from this directory are in i18n_targets.
 add_subdirectory(tests)       # Unit tests - we don't want to translate those.

With CMake 3.19 and above, you can collect the source targets at the end of the directory scope. This way, qt_collect_translation_source_targets can be called before all targets have been defined. However, you need to exclude the tests by setting the directory property QT_EXCLUDE_FROM_TRANSLATION to ON.

 function(set_up_translations)
     qt_collect_translation_source_targets(i18n_targets)
     qt_add_lupdate(SOURCE_TARGETS ${i18n_targets})
 endfunction()

 cmake_language(DEFER CALL set_up_translations)

 add_subdirectory(src)         # The actual application is defined here.
 add_subdirectory(tests)       # Unit tests - we don't want to translate those.
 set_property(DIRECTORY tests PROPERTY QT_EXCLUDE_FROM_TRANSLATION ON)