QML Internationalization Example



  • Hi,

    Just pick up Qt i18n, and I follow the 4.7 i18n "example":http://developer.qt.nokia.com/doc/qt-4.7/declarative-i18n.html#id-1f296103-c860-4eb8-b188-57e4fb754359, with following code:

    @import QtQuick 1.0

    //
    // The QML runtime automatically loads a translation from the i18n subdirectory of the root
    // QML file, based on the system language.
    //
    // The files are created/updated by running:
    //
    // lupdate i18n.qml -ts i18n/base.ts
    //
    // Translations for new languages are created by copying i18n/base.ts to i18n/qml_<lang>.ts
    // The .ts files can then be edited with Linguist:
    //
    // linguist i18n/qml_fr.ts
    //
    // The run-time translation files are then generaeted by running:
    //
    // lrelease i18n/*.ts
    //

    Rectangle {
    width: 640; height: 480

    Column {
        anchors.fill: parent; spacing: 20
    
        Text {
            text: "If a translation is available for the system language (eg. French) then the
    

    string below will translated (eg. 'Bonjour'). Otherwise it will show 'Hello'."
    width: parent.width; wrapMode: Text.WordWrap
    }

        Text {
            text: qsTr("Hello")
            font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
        }
    }
    

    }
    @

    but did not get the translated "Bonjour" string on the running application. Any hints?

    Further question is that if I have two qm files ,say qml_fr.qm and qml_ge.qm. How will the application know the string will be translated into French or German?


  • Moderators

    If you're using the qmlviewer app to view the code (which I think the example is assuming you're doing) the source for the viewer contains:

    @
    void QDeclarativeViewer::loadTranslationFile(const QString& directory)
    {
    if (!translator) {
    translator = new QTranslator(this);
    QApplication::installTranslator(translator);
    }

    translator->load(QLatin1String("qml_" )+QLocale::system&#40;&#41;.name(), directory + QLatin1String("/i18n"));
    

    }
    @

    So the magic happens in line 8 where it queries QLocale::system().name() and loads the translation file.

    If you're using a hybrid C++/QML app, or using the QmlApplicationViewer class that's generated by the Qt Creator wizard, then you'd have to load the translation files manually, which is documented "here.":http://developer.qt.nokia.com/doc/qt-4.7/internationalization.html



  • Thank you, mlong !
    [quote author="mlong" date="1310750407"]If you're using the qmlviewer app to view the code (which I think the example is assuming you're doing) the source for the viewer contains:

    @
    void QDeclarativeViewer::loadTranslationFile(const QString& directory)
    {
    if (!translator) {
    translator = new QTranslator(this);
    QApplication::installTranslator(translator);
    }

    translator->load(QLatin1String("qml_" )+QLocale::system&#40;&#41;.name(&#41;, directory + QLatin1String("/i18n"));
    

    }
    @

    So the magic happens in line 8 where it queries QLocale::system().name() and loads the translation file.

    If you're using a hybrid C++/QML app, or using the QmlApplicationViewer class that's generated by the Qt Creator wizard, then you'd have to load the translation files manually, which is documented "here.":http://developer.qt.nokia.com/doc/qt-4.7/internationalization.html[/quote]



  • Don't forget that dynamic translation isn't well (at all) supported in QML. So if you're going to be changing language at runtime you need to take a look at "this thread":http://developer.qt.nokia.com/forums/viewthread/3307/ .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.