QML Internationalization Example
Hi,
Just pick up Qt i18n, and I follow the 4.7 i18n example [developer.qt.nokia.com], 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
- }
- }
- }
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?
3 replies
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:
- {
- if (!translator) {
- }
- translator->load(QLatin1String("qml_" )+QLocale::system().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. [developer.qt.nokia.com]
Thank you, mlong !
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:
{ if (!translator) { } translator->load(QLatin1String("qml_" )+QLocale::system().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. [developer.qt.nokia.com]
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 [developer.qt.nokia.com] .
You must log in to post a reply. Not a member yet? Register here!




