Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Internationalization Example
Forum Updated to NodeBB v4.3 + New Features

QML Internationalization Example

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 7.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    changsheng230
    wrote on last edited by
    #1

    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?

    Chang Sheng
    常升

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        changsheng230
        wrote on last edited by
        #3

        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]

        Chang Sheng
        常升

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SteveKing
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved