qt_add_translation

Qt Linguist.ts 文件编译成 .qm 文件。

该命令在Qt6 软件包的LinguistTools 组件中定义。用以下命令加载软件包:

find_package(Qt6 REQUIRED COMPONENTS LinguistTools)

警告: 此函数已过时。请考虑使用基于目标的函数qt6_add_lreleaseqt6_add_translations

说明

qt_add_translation(<VAR> file1.ts [file2.ts ...]
                    [OPTIONS ...])

如果禁用了无版本命令,请使用qt6_add_translation 代替。它支持与本命令相同的参数集。

说明

在作为参数传递的每个.ts 文件上调用lrelease ,生成.qm 文件。生成文件的路径会添加到<VAR> 中。

选项

您可以设置调用lrelease 时应传递的其他OPTIONS 。您可以在lrelease 文档中找到可能的选项。

默认情况下,qm 文件将放置在当前构建目录 (CMAKE_CURRENT_BINARY_DIR) 中。要改变这种情况,可以在调用qt_add_translation 之前将OUTPUT_LOCATION 设置为源文件.ts 的属性。

例如,使用以下代码,.qm 文件将在当前构建目录下的translations 目录中生成。

set_source_files_properties(app_en.ts app_de.ts
    PROPERTIES OUTPUT_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/translations")
qt_add_translation(qmFiles app_en.ts app_de.ts)

示例

在构建目录中生成helloworld_en.qm,helloworld_de.qm

qt_add_translation(qmFiles helloworld_en.ts helloworld_de.ts)

l10n 子目录下生成helloworld_en.qm,helloworld_de.qm

set(TS_FILES helloworld_en.ts helloworld_de.ts)
set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION "l10n")
qt_add_translation(qmFiles ${TS_FILES})

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