January 19, 2011

kyleplattner kyleplattner
Lab Rat
242 posts

Question around internationalization in QML

Page  
1

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?

20 replies

January 20, 2011

Andre Andre
Robot Herder
6393 posts

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.

January 20, 2011

kyleplattner kyleplattner
Lab Rat
242 posts

How would I do this with C++ then?

January 20, 2011

Andre Andre
Robot Herder
6393 posts

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.

January 20, 2011

kyleplattner kyleplattner
Lab Rat
242 posts

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?

January 20, 2011

Andre Andre
Robot Herder
6393 posts

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.

January 20, 2011

kyleplattner kyleplattner
Lab Rat
242 posts

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.

January 20, 2011

Andre Andre
Robot Herder
6393 posts

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.

January 20, 2011

kyleplattner kyleplattner
Lab Rat
242 posts

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

January 20, 2011

Andre Andre
Robot Herder
6393 posts

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

January 20, 2011

MattB MattB
Lab Rat
35 posts

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.

January 20, 2011

kyleplattner kyleplattner
Lab Rat
242 posts

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

Kyle

January 21, 2011

Andre Andre
Robot Herder
6393 posts

May I suggest filing a bugreport in Jira [bugreports.qt.nokia.com]? I considder it a bug that dynamic translation doesn’t work with QDeclarativeView.

January 21, 2011

MattB MattB
Lab Rat
35 posts

Great idea!

QTBUG-16827 [bugreports.qt.nokia.com]

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

  1. Text{
  2.   id: myTxt
  3.   text: qsTr("Translate Me")
  4. }

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.

January 24, 2011

MattB MattB
Lab Rat
35 posts

Turns out there was already a bug filed for this:

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

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

QML:

  1. Text {
  2.   text: qsTr("Translate Me") + rootItem.emptyString
  3. }

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

June 14, 2011

goli goli
Lab Rat
111 posts

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.

Page  
1

  ‹‹ [Solved] return QModelIndex from c++ to qml      ListView Highlight Question ››

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