Collects targets that are eligible for translation.
命令的定义在
LinguistTools
组件对于
Qt6
包。加载包采用:
find_package(Qt6 REQUIRED COMPONENTS LinguistTools)
This command was introduced in Qt 6.7.
qt_collect_translation_source_targets(out_var [DIRECTORY path])
若
无版本命令
被禁用,使用
qt6_collect_translation_source_targets()
代替。它支持如此命令的一组相同自变量。
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.
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 .
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.
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)