qt_add_translations

Add targets to update and transform Qt Linguist .ts files into .qm files.

The command is defined in the LinguistTools 组件对于 Qt6 package. Load the package with:

find_package(Qt6 REQUIRED COMPONENTS LinguistTools)
					

This command was introduced in Qt 6.2.

概要

qt_add_translations(target TS_FILES file1.ts [file2.ts ...]
                    [RESOURCE_PREFIX prefix]
                    [OUTPUT_TARGETS variable-name]
                    [QM_FILES_OUTPUT_VARIABLE variable-name]
                    [SOURCES source1.cpp [sources2.cpp ...]]
                    [INCLUDE_DIRECTORIES directory1 [directory2 ...]]
                    [LUPDATE_OPTIONS ...]
                    [LRELEASE_OPTIONS ...])
					

versionless commands are disabled, use qt6_add_translations() instead. It supports the same set of arguments as this command.

注意: This command is in technology preview and may change in future releases.

警告: 调用 qt_add_translations in a directory scope different than the target directory scope requires at least CMake version 3.18.

描述

Creates targets for updating Qt Linguist .ts files and for transforming them into .qm files. This function is a convenience wrapper around qt_add_lupdate and qt_add_lrelease and aims to offer the most common usage of both functions with one call.

参数 target is an existing executable or library target that contains sources with translatable strings. This function will create the targets ${target}_lupdate , ${target}_lrelease , update_translations and release_translations . The latter targets are umbrella targets that build all ${target}_lupdate and ${target} _lrelease targets.

${target}_lrelease is built automatically whenever ${target} needs to be built.

The .ts files must be specified with the argument TS_FILES .

Sources and Include Directories

默认情况下, qt_add_lupdate extracts the source files and include directories from the given target and passes them to lupdate .

采用 SOURCES you can explicitly specify source files that contain translatable strings. This turns off the automatic extraction of source files from the target.

可以使用 INCLUDE_DIRECTORIES to explicitly specify include directories. This turns off the automatic extraction of include directories from the target.

选项

You can set additional options for lupdate and lrelease with LUPDATE_OPTIONS and LRELEASE_OPTIONS . You can find possible options in the lupdate options and lrelease 选项 .

默认情况下, .qm files will be placed in the current build directory ( CMAKE_CURRENT_BINARY_DIR ). To change this, you can set OUTPUT_LOCATION as a property of the source .ts 文件。

For example, with the following code, the .qm files are generated in a translations directory below the current build directory.

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

Embedding Generated .qm Files in Resources

By default, the generated .qm files are embedded in a Qt resource that will be linked into ${target} . The files in the resource are accessible under the resource prefix "/i18n" .

You can set the resource prefix with RESOURCE_PREFIX .

In a static Qt build, when a resource target is created, additional targets can be created. You can instruct qt_add_translations to store these targets in a variable, by passing OUTPUT_TARGETS <variable-name> .

The automatic resource embedding can be turned off by giving the QM_FILES_OUTPUT_VARIABLE option, followed by the name of a variable in which the command should store the list of generated .qm 文件。

范例

Add a German translation to a target super_calc 使用 qt_add_translations :

qt_add_translations(super_calc TS_FILES super_calc_de.ts)
					

This is roughly equivalent to the following:

qt_add_lupdate(super_calc TS_FILES super_calc_de.ts)
qt_add_lrelease(super_calc
    TS_FILES super_calc_de.ts
    QM_FILES_OUTPUT_VARIABLE qm_files)
qt_add_resources(super_calc "translations"
    PREFIX "/i18n"
    BASE "${CMAKE_CURRENT_BINARY_DIR}"
    FILES "${qm_files}")
					

Add a Norwegian translation to frogger_game with a custom resource prefix:

qt_add_translations(frogger_game
    TS_FILES frogger_game_no.ts
    RESOURCE_PREFIX "/translations")
					

Add a Finnish translation to business_logic , and install the generated .qm files:

qt_add_translations(business_logic
    TS_FILES myapp_fi.ts
    QM_FILES_OUTPUT_VARIABLE qm_files)
install(FILES ${qm_files} DESTINATION "translations")