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)

© 2025 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.