qt_add_translations

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

The command is defined in the LinguistTools component of the Qt6 package. Load the package with:

find_package(Qt6 REQUIRED COMPONENTS LinguistTools)

This command was introduced in Qt 6.2.

Synopsis

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 ...])

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

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

Warning: Calling qt_add_translations in a directory scope different than the target directory scope requires at least CMake version 3.18.

Description

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.

The parameter 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

By default, qt_add_lupdate extracts the source files and include directories from the given target and passes them to lupdate.

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

You can use INCLUDE_DIRECTORIES to explicitly specify include directories. This turns off the automatic extraction of include directories from the target.

Options

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

By default, the .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 file.

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

Examples

Add a German translation to a target super_calc using 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")

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