Introduction

QML relies on the core internationalization capabilities provided by Qt. In the sections below we show how you can load and install the translators on the C++ side and set the needed translation on the QML side.

What to do on the C++ side

The example below illustrates the necessary steps to get the dynamic translation to work. In the TranslationTest class we create a property:

  1. Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)

that simply sets an empty string. This empty string is appended to the text in the QML code. This is a trick to get the whole expression to be reevaluated whenever the property emptyString changes. When not using this trick, QDeclarativeEngine will not know that it should reevaluate the text and do the translation. There is a suggestion in Jira [bugreports.qt.nokia.com] for for adding a more convenient way to know when the strings should be retranslated.

The selectLanguage() function checks which language is currently being set and loads and installs the corresponding translation file accordingly. It then emits languageChanged(). All QML Text { } elements that use rootItem.emptyString in their text binding will receive a “NOTIFY” signal causing the qsTr(”….”) to be reevaluated using the new QTranslator. Because rootItem.emptyString is just “” (an empty string), it will alter the text in your GUI. This trick is based on the one suggested in the Jira report above and in this thread [developer.qt.nokia.com]

TranslationTest::selectLanguage() is callable from QML as it uses Q_INVOKABLE. To be able to call Qt class methods from QML code, the methods must be declared as public slots or conventional methods with the Q_INVOKABLE macro. In both cases, Qt methods are available to the Qt Meta-Object system and the methods are callable from QML.

  1. #include <QtGui>
  2. #include <QtDeclarative>
  3.  
  4. class TranslationTest : public QObject
  5. {
  6.  Q_OBJECT
  7.  Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
  8. public:
  9.   TranslationTest() {
  10.    translator1 = new QTranslator(this);
  11.    translator2 = new QTranslator(this);
  12.   }
  13.  
  14.   QString getEmptyString() {
  15.    return "";
  16.   }
  17.  
  18.   Q_INVOKABLE void selectLanguage(QString language) {
  19.    if(language == QString("fr")) {
  20.     translator1->load("t1_fr", ".");
  21.     qApp->installTranslator(translator1);
  22.    }
  23.    if(language == QString("sp")) {
  24.     translator2->load("t1_sp", ".");
  25.     qApp->installTranslator(translator2);
  26.    }
  27.    if(language == QString("en")){
  28.     qApp->removeTranslator(translator1);
  29.     qApp->removeTranslator(translator2);
  30.    }
  31.    emit languageChanged();
  32.  }
  33. signals:
  34.  void languageChanged();
  35. private:
  36.  QTranslator *translator1;
  37.  QTranslator *translator2;
  38. };
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42.  QApplication app(argc, argv);
  43.  TranslationTest myObj;
  44.  QDeclarativeView view;
  45.  view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);
  46.  view.setSource(QUrl::fromLocalFile("main.qml"));
  47.  view.show();
  48.  return app.exec();
  49. }
  50.  
  51. #include "main.moc"

QML implementation

A convenient way to switch between languages is by using buttons, so we create a Button.qml component as follows:

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     id: root;
  5.     property string label;
  6.    
  7.     signal clicked()
  8.    
  9.     width: 120
  10.     height: 30
  11.     color: mouse.pressed ? "lightgray" : "white"
  12.    
  13.     radius: 4
  14.     border.width: 1
  15.     border.color: "gray"
  16.    
  17.     MouseArea {
  18.       id: mouse
  19.       anchors.fill: parent
  20.       onClicked: root.clicked();
  21.     }
  22.    
  23.     Text {
  24.       anchors.centerIn: parent
  25.       text: root.label
  26.     }
  27. }

Button.qml is then used in the main.qml file below to create 3 buttons that allow us to switch between English, French and Spanish. Each time a button is pressed, it causes the selectLanguage() of the C++ class to be called with the appropriate language string. This will then trigger the text to be reevaluated and hence translated.

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     width: 340; height: 150
  5.  
  6.     Column {
  7.         anchors.fill: parent; spacing: 20
  8.  
  9.         Text {
  10.             text: qsTr("Hello") + rootItem.emptyString
  11.             font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
  12.         }
  13.     }
  14.    
  15.     Row {
  16.       anchors.verticalCenter: parent.verticalCenter
  17.       Button { label: "English"; onClicked: rootItem.selectLanguage("en"); }
  18.       Button { label: "Spanish"; onClicked: rootItem.selectLanguage("sp"); }
  19.       Button { label: "French"; onClicked: rootItem.selectLanguage("fr"); }
  20.     }
  21. }

The project file

In the .pro file we need to list the translation files under the TRANSLATIONS [doc.qt.nokia.com] variable.

  1. TEMPLATE = app
  2. TARGET =
  3. INCLUDEPATH += .
  4.  
  5. # Input
  6. SOURCES += main.cpp
  7. QT+= declarative
  8. TRANSLATIONS = t1_fr.ts t1_sp.ts
  9.  
  10. OTHER_FILES += \
  11.     Button.qml

lupdate and lrelease

We can now create the translation files by running lupdate [doc.qt.nokia.com] on the .qml file. lupdate will generate the first set of TS translation source files with all the user-visible text but no translations. In our example, we do:

  1. lupdate main.qml -ts t1_fr.ts t1_sp.ts

to create the translation files. These files can now be opened in Linguist [doc.qt.nokia.com] and translated there. Finally, when the application is finished, you need to run lrelease [doc.qt.nokia.com] on the .ts files to read the .ts files and produce the .qm files used by the application at runtime. For example:

  1. lrelease t1_fr.ts
  2. lrelease t1_sp.ts

When running the example at this point, it should now switch between English, French and Spanish when clicking the respective buttons.

Further reading

For further reading, see the QML internationalization [doc.qt.nokia.com] documentation, the QML internationalization example [doc.qt.nokia.com], the Qt internationalization documentation [doc.qt.nokia.com] and the Qt Internationalization wiki [developer.qt.nokia.com].

Categories: