Localizing Applications

The steps to localize applications include creating translatable applications, preparing them for translation, translating the strings, and creating runtime translation files for the released application.

Qt Quick and Qt C++ applications use the same underlying localization system: lupdate, lrelease, and the translation source (TS) files and QM files that they generate. You use the same tools for both QML and C++ code, as described in the Qt Linguist Manual.

You can even have user interface strings in QML and C++ source files in the same application. The system will create a single combined translation file and the strings are accessible from QML and C++ code.

To localize internationalized applications:

Specify Translation Sources in Qt Project Files

To enable lupdate and lrelease to generate TS and QM files, update the application project file to specify the source files that contain text to be translated.

Use ISO language and country codes in the TS file name to determine the language to load at runtime. For more information, see Enable Translation.

The lupdate tool extracts user interface strings from your application. It expects all source code to be encoded in UTF-8 by default. For more information, see Encoding.

Using CMake

Add the qt_add_translations command to the CMakeLists.txt file to create targets for updating TS files and transforming them into QM files.

Using qmake

When using qmake, set a conditional statement to hide the QML source from the compiler that you list in the SOURCES or HEADERS entry in the .pro file.

The SOURCES variable is intended for C++ source files. If you list QML or JavaScript source files there, the compiler tries to build them as though they are C++ files. As a workaround, you can use an lupdate_only{...} conditional statement so the lupdate tool sees the .qml files but the C++ compiler ignores them.

For example, the following .pro file snippet specifies two .qml files in the application.

 lupdate_only{
 SOURCES = main.qml \
           MainPage.qml
 }

You can also specify the .qml source files with a wildcard match. The search is not recursive, so you need to specify each directory that contains source files that contain UI strings:

 lupdate_only{
 SOURCES = *.qml \
           *.js \
           content/*.qml \
           content/*.js
 }
 

Deploy Translations

Place the .qm files required for the application in a location where the loader code using QTranslator can find them. Typically, you specify a path relative to QCoreApplication::applicationDirPath().

In addition to the application's QM files, you need to deploy the QM files for the Qt modules that you use in the application, unless they are installed on the system.

The QM files are split up by module and there is a so-called meta catalog file which includes the QM files of all modules. However, you only need to deploy the QM files for the modules that you use in the application.

You can use the lconvert tool in the deploy step to concatenate the required QM files into one file that matches the meta catalog file. For example, to create a German translation file for an application that uses the Qt Core, Qt GUI, and Qt Quick modules, run:

 lconvert -o installation_folder/qt_de.qm qtbase_de.qm qtdeclarative_de.qm

Use Qt Module Translations

Qt modules contain several thousands of strings that also need to be translated into the languages that you are targeting. You can find a number of TS files in the qttranslations repository. Before you start translating Qt, read the wiki page Translating Qt Into Other Languages.

Locating Qt Translations

You can use QLibraryInfo::path() to locate the translations for the Qt modules that your application uses. You can request the path to the translations at run-time by passing QLibraryInfo::TranslationsPath to this function.

Available Catalogs

The Qt translation catalogs are located in the qttranslations repository.

Warning: Qt translations are contributed by the Qt community, and provided without any guarantees. Translations might be missing, outdated, or entirely incorrect, up to the point of being malicious. It is recommended that you audit any translations you ship.

In Qt 4, there is one big, monolithic .qm file per locale. For example, the file qt_de.qm contains the German translation of all libraries.

The qt_ meta catalog contains the still-existing Qt translations that were included in the qt_ catalog in Qt 4. It was created to make porting applications from Qt 4 to Qt 5 easier. The meta catalog depends on translations that might be absent, because they belong to unnecessary or deprecated modules, which might cause the loading of the translations to fail. If you use modules that are new in Qt 5 or later in your application, you must specify the names of the catalogs for those modules even if you use the meta catalog.

The following table lists the translation catalogs available for the Qt modules and tools in Qt.

Qt Module or ToolCatalog
Qt Bluetoothqtconnectivity
Qt Concurrentqtbase
Qt Coreqtbase
Qt D-Busqtbase
Qt Designerdesigner
Qt GUIqtbase
Qt Helpqt_help
Qt Linguistlinguist
Qt Locationqtlocation
Qt Multimediaqtmultimedia
Qt Networkqtbase
Qt NFCqtconnectivity
Qt Print Supportqtbase
Qt Qmlqtdeclarative
Qt Quickqtdeclarative
Qt Quick Controlsqtdeclarative
Qt Quick Widgetsqtdeclarative
Qt Serial Portqtserialport
Qt SQLqtbase
Qt Widgetsqtbase
Qt WebSocketsqtsockets
Qt WebEngineqtwebengine

Example: Essential Qt Modules

For example, to locate translations for essential Qt modules, such as Qt Core, Qt GUI, Qt Network, and Qt Widgets, add the following code to the main() function:

     QTranslator qtTranslator;
     if (qtTranslator.load(QLocale::system(), u"qtbase"_s, u"_"_s,
                           QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
         app.installTranslator(&qtTranslator);
     }