qt_collect_translation_source_targets
收集符合翻译条件的翻译目标。
该命令定义在Qt6 包的LinguistTools 组件中。使用以下命令加载该包:
find_package(Qt6 REQUIRED COMPONENTS LinguistTools)该命令于 Qt 6.7 版本中引入。
语法
qt_collect_translation_source_targets(out_var [DIRECTORY path])如果禁用了无版本号的命令,请改用qt6_collect_translation_source_targets() 。它支持与本命令相同的参数集。
描述
收集给定DIRECTORY 及其所有子目录中符合翻译条件的目标。如果未指定DIRECTORY ,则从CMAKE_CURRENT_SOURCE_DIR 开始收集目标。
该命令将目标列表存储在作为第一个参数指定的变量中。
将目标从翻译中排除
默认情况下,所有未被导入的可执行文件和库目标都符合翻译条件。
可以通过设置目标属性 QT_EXCLUDE_FROM_TRANSLATION 来排除单个目标。
通过设置目录属性 QT_EXCLUDE_FROM_TRANSLATION,可以排除某个目录下的目标。
何时调用此命令
qt_collect_translation_source_targets 命令会读取BUILDSYSTEM_TARGETS目录属性。因此,它只会收集已经创建的目标。在调用qt_collect_translation_source_targets 之后创建的目标不会被收集。
若要收集构建系统中的所有目标,请在顶级CMakeLists.txt 的末尾调用qt_collect_translation_source_targets ,或在顶级目录作用域的末尾使用cmake_language(DEFER CALL) 来配置国际化(i18n)。
示例
将qt_collect_translation_source_targets 的结果作为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.在 CMake 3.19 及更高版本中,您可以在目录作用域结束时收集源代码目标。这样,qt_collect_translation_source_targets 就可以在所有目标定义完成之前被调用。但是,您需要通过将目录属性QT_EXCLUDE_FROM_TRANSLATION设置为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)© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.