IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Qt Quick I18N

Generating translation files (TS and QM) for a Qt Quick application.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Qt Quick I18N

Image non disponible

When you run the example application, the QML runtime automatically loads a translation from the i18n subdirectory of the directory that contains the main .qml file, based on the system language.

Marking Text as Translatable

In the qml-i18n.qml file, use the qsTr command to mark UI text as translatable:

 
Sélectionnez
        Text {
            text: qsTr("Hello")

Enabling Translation

In the main.cpp file, use QQmlApplicationEngine to automatically load translation files from a subdirectory called i18n in the directory that contains the main QML file:

 
Sélectionnez
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

The translation file names in the i18n subdirectory must have the prefix qml_. For example, qml_en_AU.ts.

Creating Translation Files

Add the LinguistTools module as a value of the find_package command in the CMakeLists.txt file:

 
Sélectionnez
find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick Qml LinguistTools)

Define the translation files to generate in the qt_add_translations command:

 
Sélectionnez
qt_add_translations(qmli18n
    TS_FILES
    i18n/base.ts
    i18n/qml_en.ts
    i18n/qml_en_AU.ts
    i18n/qml_fr.ts
)

Build the application to generate TS and QM files.

On Android, make sure to include the QM files as resources.

Adding Translations

To create translations for new languages, copy i18n/base.ts to i18n/qml_<lang>.ts. You can use Qt Linguist to translate the strings.

Example project

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+