[Interest] Dynamic translation using Qt Quick 2.x in Qt5.1.1

Bo Thorsen bthorsen at ics.com
Tue Oct 1 09:49:08 CEST 2013


Den 01-10-2013 07:36, Ramakanthreddy Kesireddy skrev:
> Hi,
>
> I would like to know if it is possible to update the text dynamically
>
> for any locale change in QtQuick(QML) using Qt5.1.1.

No, not directly. But there are a couple of things you can do instead.

The key in both my solutions is to have a single C++ object with a 
property registered to QML:

   class LocaleUpdater : public QObject {
     Q_OBJECT
     Q_PROPERTY(QString localeChange READ localeChange
       NOTIFY localeChanged)
   ...
     QString localeChange() const { return QString(); }
   };

Export this to the QML:

   LocaleUpdater updater;
   qml.rootContext()->setContextProperty("localeUpdater", &updater);

When the language changes, have the LocaleUpdater emit the 
localeChanged() signal. You can now do one of two things:

1) Use the text updater string directly:

   someObject.text: qsTr("Foo") + localeUpdater.localeChange

2) Catch the emitted signal and set all strings

   Connections {
     target: localeUpdater
     onLocaleChange: {
       someObject.text = qsTr("Foo")
     }
   }

I hope this helps.

Bo.

-- 
Bo Thorsen, European Qt Manager, ICS
Integrated Computer Solutions. Delivering World-Class Applications
http://ics.com/services



More information about the Interest mailing list