July 15, 2011

changsheng230 changsheng230
Lab Rat
128 posts

QML Internationalization Example

 

Hi,

Just pick up Qt i18n, and I follow the 4.7 i18n example [developer.qt.nokia.com], with following code:

  1. import QtQuick 1.0
  2.  
  3. //
  4. // The QML runtime automatically loads a translation from the i18n subdirectory of the root
  5. // QML file, based on the system language.
  6. //
  7. // The files are created/updated by running:
  8. //
  9. //   lupdate i18n.qml -ts i18n/base.ts
  10. //
  11. // Translations for new languages are created by copying i18n/base.ts to i18n/qml_<lang>.ts
  12. // The .ts files can then be edited with Linguist:
  13. //
  14. //   linguist i18n/qml_fr.ts
  15. //
  16. // The run-time translation files are then generaeted by running:
  17. //
  18. //   lrelease i18n/*.ts
  19. //
  20.  
  21. Rectangle {
  22.     width: 640; height: 480
  23.  
  24.     Column {
  25.         anchors.fill: parent; spacing: 20
  26.  
  27.         Text {
  28.             text: "If a translation is available for the system language (eg. French) then the
  29.   string below will translated (eg. 'Bonjour'). Otherwise it will show 'Hello'."
  30.             width: parent.width; wrapMode: Text.WordWrap
  31.         }
  32.  
  33.         Text {
  34.             text: qsTr("Hello")
  35.             font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
  36.         }
  37.     }
  38. }

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?

 Signature 

Chang Sheng
常升

3 replies

July 15, 2011

mlong mlong
Robot Herder
1551 posts

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:

  1. void QDeclarativeViewer::loadTranslationFile(const QString& directory)
  2. {
  3.     if (!translator) {
  4.         translator = new QTranslator(this);
  5.         QApplication::installTranslator(translator);
  6.     }
  7.  
  8.     translator->load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
  9. }

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]

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

July 18, 2011

changsheng230 changsheng230
Lab Rat
128 posts

Thank you, mlong !

mlong wrote:
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:

  1. void QDeclarativeViewer::loadTranslationFile(const QString& directory)
  2. {
  3.     if (!translator) {
  4.         translator = new QTranslator(this);
  5.         QApplication::installTranslator(translator);
  6.     }
  7.  
  8.     translator->load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
  9. }

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]

 Signature 

Chang Sheng
常升

July 18, 2011

SteveKing SteveKing
Lab Rat
105 posts

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] .

 
  ‹‹ How to do customize skinning to the qt quick components      [solved] invoke a function defined on ListElement ››

You must log in to post a reply. Not a member yet? Register here!