Internationalization and Localization with Qt Quick Ultralite
Use either the
qsTrId() function in QML files to mark string literals for translation. All such marked string literals are found by the Qt Linguist tool, which generates the translation source file (
.ts) containing those strings.
Important: Use one of these two alternatives in an application. Using both of them in an application may lead to unexpected behavior.
Once translations are made, there will a
.ts file for each language. These files are compiled to translation release files (
.qm) and passed to the QML compiler, which replaces the
qsTrId() calls with a lookup of the correct string based on the
Qt.uiLanguage property can also be used in other bindings to make decisions based on the active language.
The translation example shows how to use
qsTr() and the
Listing the translation source files for the target languages in the TranslationFiles.files QmlProject property and using the containing QmlProject file in
qul_add_target with QML_PROJECT specified will have two effects:
- It creates a
update_translationsCMake target which is not included in the default build. Trigger a build of this target to create or update the
.tsfiles with new and changed translatable strings from the source code.
- It sets up the build system to embed the translations from the
.tsfiles in the program binary.
.ts file introduces a translated language that can be switched to at runtime by setting the
Qt.uiLanguage property. The name of the language is derived from the name of the
.ts file. For example, the "app_nb_NO.ts" file defines the translations for
nb_NO, which is the ISO language code for Norwegian Bokmål.
Qt.uiLanguage is empty, no translation is applied and the strings used in the source code are shown.
If an application does not support multiple languages, set the
TranslationFiles.MCU.omitSourceLanguage to false.
When developing using a build system other than CMake,
qmlprojectexporter can be run solely to update the translations, refer to qmlprojectexporter's update translation argument for more details.
The following functionality is not currently supported:
- Translation of strings in C++ files
- Translation functions other than
- The plural disambiguation argument in
Available under certain Qt licenses.
Find out more.