Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Question around internationalization in QML

    QML and Qt Quick
    7
    21
    14327
    Loading More Posts
    • 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.
    • K
      kyleplattner last edited by

      So I produced a .qm file and am able to load it when I pass it as a command on qmlviewer launch. Now how can I have a qml button that loads that translation when the user selects a language?

      1 Reply Last reply Reply Quote 0
      • A
        andre last edited by

        I don't think you can do that entirely from QML. I think you need some C++ for that, unless I overlooked the QML functionality that would do this.

        1 Reply Last reply Reply Quote 0
        • K
          kyleplattner last edited by

          How would I do this with C++ then?

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            Well, you could create a Object subclass that you register for use in QML (see qmlRegisterType()), so you can access the slots in that class from QML.

            Then, from that class, you can use QCoreApplication::installTranslator. I am just not sure if Quick plays nice with dynamic translation. You'd have to try that.

            1 Reply Last reply Reply Quote 0
            • K
              kyleplattner last edited by

              If we just forget the qml part of it for now, how can I load it up in C++? Will I need to restart the application? Does it just pick those files up on runtime?

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

                Perhaps you should read up on using internationalization in Qt applications. There is an extensive page in the documentation on this. It comes down to you, as a programmer, having to load in the the translation files you want into a QTranslator, and installing them in your app using the above mentioned QCoreApplication::installTranslator. Normally, you would use QLocale to figure out which one to use, but if you want to be able to change that on runtime, you need to adapt your logic a bit to allow for that.

                1 Reply Last reply Reply Quote 0
                • K
                  kyleplattner last edited by

                  I read the documentation but it is still not clear to me how to deploy an application on a device with several languages and allow the user to navigate between them.

                  1 Reply Last reply Reply Quote 0
                  • A
                    andre last edited by

                    Do you get how to install a translation in a single language?
                    You can use the same technique, except instead of loading the translation based on the locale, as is done in the example in the documentation, you load a translation file in response to a request from your user. The loading is the same, just the timing and the way you select the correct file to load will differ. If you are asking for ready-to-use code: I am sorry, I am not going to write it for you.

                    1 Reply Last reply Reply Quote 0
                    • K
                      kyleplattner last edited by

                      Should each of my qml files have qm file? Do I have to load each of those individually?

                      1 Reply Last reply Reply Quote 0
                      • A
                        andre last edited by

                        No, you make one per language for your whole project.

                        1 Reply Last reply Reply Quote 0
                        • M
                          mbarclaygmail.com last edited by

                          I am struggling with dynamic translation in QML as well. I can install new QM files, but I only get a translated UI on new Items that get loaded after the QM is installed. All existing items remain untranslated. Theoretically, the QDeclarativeView receives a changeEvent(LanguageChange) when the translation is installed, but changeEvent() hasn't been reimplemented in QDeclarativeView yet. Hopefully that feature will be added soon. For now, after you install the new translation file, you'll have to reload all of your text manually, i.e. using a Loader or some other mechanism.

                          1 Reply Last reply Reply Quote 0
                          • K
                            kyleplattner last edited by

                            Good to know I am not the only one struggling through it. Let me know if you find a good solution.

                            Kyle

                            1 Reply Last reply Reply Quote 0
                            • A
                              andre last edited by

                              May I suggest filing a bugreport in "Jira":http://bugreports.qt.nokia.com? I considder it a bug that dynamic translation doesn't work with QDeclarativeView.

                              1 Reply Last reply Reply Quote 0
                              • M
                                mbarclaygmail.com last edited by

                                Great idea!

                                "QTBUG-16827":http://bugreports.qt.nokia.com/browse/QTBUG-16827

                                It kind of makes sense to me that it wouldn't be translated. e.g.

                                @
                                Text{
                                id: myTxt
                                text: qsTr("Translate Me")
                                }
                                @

                                When the above Text is created, qsTr() is script code that gets executed. The result is assigned to myTxt.text. So when a new translation is installed, there's no new script to be executed. However, there needs to be a mechanism to retranslate when a QEvent::LanguageChange is posted.

                                1 Reply Last reply Reply Quote 0
                                • M
                                  mbarclaygmail.com last edited by

                                  Turns out there was already a bug filed for this:

                                  "http://bugreports.qt.nokia.com/browse/QTBUG-15602":http://bugreports.qt.nokia.com/browse/QTBUG-15602

                                  And someone just posted a great suggestion for a workaround. I have verified it in my application:

                                  QML:
                                  @
                                  Text {
                                  text: qsTr("Translate Me") + rootItem.emptyString
                                  }
                                  @

                                  By binding "text:" to another property, the whole expression will be reevaluated whenever the property emptyString changes.

                                  In my case rootItem is a QObject that is installed with setContextProperty()

                                  My rootItem has:

                                  Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)

                                  In my method that installs a new QM translation file, I end with:

                                  emit languageChanged();

                                  The method getNullString() just returns "" so it doesn't effect the display of the text.

                                  A better solution still needs to be resolved by the Trolls, but this will work until that time.

                                  Regards,
                                  Matt

                                  1 Reply Last reply Reply Quote 0
                                  • G
                                    goli last edited by

                                    i have this problem now...
                                    i didn't understand your solution.
                                    can you please explain this or give an example, i tried to do the above but it's also didn't work.

                                    1 Reply Last reply Reply Quote 0
                                    • M
                                      mbarclaygmail.com last edited by

                                      goli:

                                      Basically, your code should look similar to this. Post your C++ code if this doesn't help.

                                      @
                                      class MyClass : public QObject
                                      Q_OBJECT
                                      Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)

                                      QString getEmptyString() { return ""; }

                                      Q_INVOKABLE void selectLanguage(QString language) {

                                      // Install QTranslator. See Docs for details

                                      emit languageChanged();
                                      }
                                      @

                                      @
                                      int main(argc, argv) {

                                      QDeclarativeView view;
                                      MyClass myObj;
                                      ...
                                      ...

                                      view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);

                                      ...
                                      ...

                                      }
                                      @

                                      The selectLanguage() method installs a new QTranslator in the application. 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.

                                      Please post your code if you have more questions.

                                      Regards,
                                      Matt

                                      1 Reply Last reply Reply Quote 0
                                      • G
                                        goli last edited by

                                        thank you very much. this was helpful :)

                                        1 Reply Last reply Reply Quote 0
                                        • B
                                          BlackWiCKED last edited by

                                          This code works, thank you. I had the same problem, but this method solved it.

                                          Waiting for more elegant solution - without adding any "empty strings"... ;)

                                          1 Reply Last reply Reply Quote 0
                                          • D
                                            digital_aspirin last edited by

                                            Another approach would be to clear the engine's internal cache and set the main qml file again. clearComponentCache() forces all components to be reloaded, so there is no need for the emptyString property.

                                            side note: clearComponentCache() is also nice for dynamic qml loading without restarting your application. It really speeds qml development up.

                                            [code]
                                            QDeclarativeView view;
                                            ...
                                            ...
                                            void languageChange ( QString qsLanguage )
                                            {
                                            // install the translator here. See Qt docs about it

                                            // Now do:
                                            view.engine()->clearComponentCache ( );
                                            view.setSource(QUrl::fromLocalFile ("apps_main.qml"));
                                            

                                            }
                                            [/code]

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post