qt_add_translation
将Qt Linguist 中的.ts文件编译为.qm文件。
该命令定义在Qt6 包的LinguistTools 组件中。使用以下命令加载该包:
find_package(Qt6 REQUIRED COMPONENTS LinguistTools)警告:此 函数已弃用。请考虑改用基于目标的函数qt_add_lrelease()或qt_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})© 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.