This command was introduced in Qt 6.2.
qt_add_translations(target TS_FILES file1.ts [file2.ts ...] [RESOURCE_PREFIX [OUTPUT_TARGETS variable-name]] [QM_FILES_OUTPUT_VARIABLE variable-name] [SOURCES] [INCLUDE_DIRECTORIES] [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.
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
.
You can set additional options for lupdate and lrelease with LUPDATE_OPTIONS
and LRELEASE_OPTIONS
.
You can find possible options in the translations documentation.
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")
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.
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")
As part of the free Business evaluation, we offer a free welcome call for companies, to talk about your requirements, and how the Felgo SDK & Services can help you. Just sign up and schedule your call.
Sign up now to start your free Business evaluation: