How to do dynamic translation in QML
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:
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.
QML implementation
A convenient way to switch between languages is by using buttons, so we create a Button.qml component as follows:
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.
The project file
In the .pro file we need to list the translation files under the TRANSLATIONS [doc.qt.nokia.com] variable.
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:
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:
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].