How to do dynamic translation in QML: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:HowTo]]<br />[[Category:Developing_with_Qt::General]]<br />[[Category:Tutorial]] | [[Category:HowTo]]<br />[[Category:Developing_with_Qt::General]]<br />[[Category:Tutorial]] | ||
[toc align_right= | [toc align_right="yes" depth="2"] | ||
= Introduction = | = Introduction = | ||
Line 13: | Line 13: | ||
<code>Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)<code> | <code>Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)<code> | ||
that simply sets an empty string. This empty string is appended to the text in the QML code. This is a trick to get the whole expression to be reevaluated whenever the property emptyString changes. When not using this trick, QDeclarativeEngine will not know that it should reevaluate the text and do the translation. There is a suggestion in | that simply sets an empty string. This empty string is appended to the text in the QML code. This is a trick to get the whole expression to be reevaluated whenever the property emptyString changes. When not using this trick, QDeclarativeEngine will not know that it should reevaluate the text and do the translation. There is a suggestion in "Jira ":https://bugreports.qt.nokia.com//browse/QTBUG-15602 for for adding a more convenient way to know when the strings should be retranslated. | ||
The selectLanguage() function checks which language is currently being set and loads and installs the corresponding translation file accordingly. 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. This trick is based on the one suggested in the Jira report above and in | The selectLanguage() function checks which language is currently being set and loads and installs the corresponding translation file accordingly. 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. This trick is based on the one suggested in the Jira report above and in "this thread":http://developer.qt.nokia.com/forums/viewthread/3307/P15 | ||
TranslationTest::selectLanguage() is callable from QML as it uses Q_INVOKABLE. To be able to call Qt class methods from QML code, the methods must be declared as public slots or conventional methods with the Q_INVOKABLE macro. In both cases, Qt methods are available to the Qt Meta-Object system and the methods are callable from QML. | TranslationTest::selectLanguage() is callable from QML as it uses Q_INVOKABLE. To be able to call Qt class methods from QML code, the methods must be declared as public slots or conventional methods with the Q_INVOKABLE macro. In both cases, Qt methods are available to the Qt Meta-Object system and the methods are callable from QML. | ||
</code><br />#include | </code><br />#include <QtGui><br />#include <QtDeclarative> | ||
class TranslationTest : public QObject<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)<br />public:<br /> TranslationTest() {<br /> translator1 = new QTranslator(this);<br /> translator2 = new QTranslator(this);<br /> } | class TranslationTest : public QObject<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)<br />public:<br /> TranslationTest() {<br /> translator1 = new QTranslator(this);<br /> translator2 = new QTranslator(this);<br /> } | ||
QString getEmptyString() {<br /> return | QString getEmptyString() {<br /> return "";<br /> } | ||
Q_INVOKABLE void selectLanguage(QString language) {<br /> if(language QString( | Q_INVOKABLE void selectLanguage(QString language) {<br /> if(language QString("fr")) { | ||
translator1- | translator1->load("t1_fr", "."); | ||
qApp- | qApp->installTranslator(translator1); | ||
} | |||
if(language QString( | if(language QString("sp")) {<br /> translator2->load("t1_sp", ".");<br /> qApp->installTranslator(translator2);<br /> }<br /> if(language == QString("en")){<br /> qApp->removeTranslator(translator1);<br /> qApp->removeTranslator(translator2);<br /> }<br /> emit languageChanged();<br /> }<br />signals:<br /> void languageChanged();<br />private:<br /> QTranslator *translator1;<br /> QTranslator *translator2;<br />}; | ||
int main(int argc, char '''argv[])<br />{<br /> QApplication app(argc, argv);<br /> TranslationTest myObj;<br /> QDeclarativeView view;<br /> view.rootContext()- | int main(int argc, char '''argv[])<br />{<br /> QApplication app(argc, argv);<br /> TranslationTest myObj;<br /> QDeclarativeView view;<br /> view.rootContext()->setContextProperty("rootItem", (QObject''')&myObj);<br /> view.setSource(QUrl::fromLocalFile("main.qml"));<br /> view.show();<br /> return app.exec();<br />} | ||
#include | #include "main.moc" | ||
<code> | <code> | ||
Line 47: | Line 47: | ||
signal clicked() | signal clicked() | ||
width: 120<br /> height: 30<br /> color: mouse.pressed ? | width: 120<br /> height: 30<br /> color: mouse.pressed ? "lightgray" : "white" | ||
radius: 4<br /> border.width: 1<br /> border.color: | radius: 4<br /> border.width: 1<br /> border.color: "gray" | ||
MouseArea {<br /> id: mouse<br /> anchors.fill: parent<br /> onClicked: root.clicked();<br /> } | MouseArea {<br /> id: mouse<br /> anchors.fill: parent<br /> onClicked: root.clicked();<br /> } | ||
Line 63: | Line 63: | ||
Column {<br /> anchors.fill: parent; spacing: 20 | Column {<br /> anchors.fill: parent; spacing: 20 | ||
Text {<br /> text: qsTr( | Text {<br /> text: qsTr("Hello") + rootItem.emptyString<br /> font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter<br /> }<br /> } | ||
Row {<br /> anchors.verticalCenter: parent.verticalCenter<br /> Button { label: | Row {<br /> anchors.verticalCenter: parent.verticalCenter<br /> Button { label: "English"; onClicked: rootItem.selectLanguage("en"); }<br /> Button { label: "Spanish"; onClicked: rootItem.selectLanguage("sp"); }<br /> Button { label: "French"; onClicked: rootItem.selectLanguage("fr"); }<br /> }<br />} | ||
<code> | <code> | ||
Line 71: | Line 71: | ||
== The project file == | == The project file == | ||
In the .pro file we need to list the translation files under the | In the .pro file we need to list the translation files under the "TRANSLATIONS":http://doc.qt.nokia.com/latest/qmake-variable-reference.html#translations variable. | ||
</code><br />TEMPLATE = app<br />TARGET =<br />INCLUDEPATH ''= . | </code><br />TEMPLATE = app<br />TARGET =<br />INCLUDEPATH ''= . | ||
Line 82: | Line 82: | ||
== lupdate and lrelease == | == lupdate and lrelease == | ||
We can now create the translation files by running | We can now create the translation files by running "lupdate":http://doc.qt.nokia.com/latest/linguist-manager.html#lupdate on the .qml file. lupdate will generate the first set of TS translation source files with all the user-visible text but no translations. In our example, we do: | ||
</code>lupdate main.qml -ts t1_fr.ts t1_sp.ts</code> | </code>lupdate main.qml -ts t1_fr.ts t1_sp.ts</code> | ||
to create the translation files. These files can now be opened in | to create the translation files. These files can now be opened in "Linguist":http://doc.qt.nokia.com/latest/linguist-manual.html and translated there. Finally, when the application is finished, you need to run "lrelease":http://doc.qt.nokia.com/latest/linguist-manager.html#lrelease on the .ts files to read the .ts files and produce the .qm files used by the application at runtime. For example: | ||
<code>lrelease t1_fr.ts<br />lrelease t1_sp.ts</code> | <code>lrelease t1_fr.ts<br />lrelease t1_sp.ts</code> |
Revision as of 14:38, 24 February 2015
[toc align_right="yes" depth="2"]
Introduction
QML relies on the core internationalization capabilities provided by Qt. In the sections below we show how you can load and install the translators on the C++ side and set the needed translation on the QML side.
What to do on the C++ side
The example below illustrates the necessary steps to get the dynamic translation to work. In the TranslationTest class we create a property:
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)<code>
that simply sets an empty string. This empty string is appended to the text in the QML code. This is a trick to get the whole expression to be reevaluated whenever the property emptyString changes. When not using this trick, QDeclarativeEngine will not know that it should reevaluate the text and do the translation. There is a suggestion in "Jira ":https://bugreports.qt.nokia.com//browse/QTBUG-15602 for for adding a more convenient way to know when the strings should be retranslated.
The selectLanguage() function checks which language is currently being set and loads and installs the corresponding translation file accordingly. 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. This trick is based on the one suggested in the Jira report above and in "this thread":http://developer.qt.nokia.com/forums/viewthread/3307/P15
TranslationTest::selectLanguage() is callable from QML as it uses Q_INVOKABLE. To be able to call Qt class methods from QML code, the methods must be declared as public slots or conventional methods with the Q_INVOKABLE macro. In both cases, Qt methods are available to the Qt Meta-Object system and the methods are callable from QML.
#include <QtGui>
#include <QtDeclarative>
class TranslationTest : public QObject
{
Q_OBJECT
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
public:
TranslationTest() {
translator1 = new QTranslator(this);
translator2 = new QTranslator(this);
}
QString getEmptyString() {
return "";
}
Q_INVOKABLE void selectLanguage(QString language) {
if(language QString("fr")) {
translator1->load("t1_fr", "."); qApp->installTranslator(translator1); } if(language QString("sp")) {
translator2->load("t1_sp", ".");
qApp->installTranslator(translator2);
}
if(language == QString("en")){
qApp->removeTranslator(translator1);
qApp->removeTranslator(translator2);
}
emit languageChanged();
}
signals:
void languageChanged();
private:
QTranslator *translator1;
QTranslator *translator2;
};
int main(int argc, char argv[])
{
QApplication app(argc, argv);
TranslationTest myObj;
QDeclarativeView view;
view.rootContext()->setContextProperty("rootItem", (QObject)&myObj);
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}
- include "main.moc"
== QML implementation ==
A convenient way to switch between languages is by using buttons, so we create a Button.qml component as follows:
import QtQuick 1.0
Rectangle {
id: root;
property string label;
signal clicked()
width: 120
height: 30
color: mouse.pressed ? "lightgray" : "white"
radius: 4
border.width: 1
border.color: "gray"
MouseArea {
id: mouse
anchors.fill: parent
onClicked: root.clicked();
}
Text {
anchors.centerIn: parent
text: root.label
}
}
Button.qml is then used in the main.qml file below to create 3 buttons that allow us to switch between English, French and Spanish. Each time a button is pressed, it causes the selectLanguage() of the C++ class to be called with the appropriate language string. This will then trigger the text to be reevaluated and hence translated.
import QtQuick 1.0
Rectangle {
width: 340; height: 150
Column {
anchors.fill: parent; spacing: 20
Text {
text: qsTr("Hello") + rootItem.emptyString
font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
}
}
Row {
anchors.verticalCenter: parent.verticalCenter
Button { label: "English"; onClicked: rootItem.selectLanguage("en"); }
Button { label: "Spanish"; onClicked: rootItem.selectLanguage("sp"); }
Button { label: "French"; onClicked: rootItem.selectLanguage("fr"); }
}
}
== The project file ==
In the .pro file we need to list the translation files under the "TRANSLATIONS":http://doc.qt.nokia.com/latest/qmake-variable-reference.html#translations variable.
TEMPLATE = app
TARGET =
INCLUDEPATH = .
# Input
SOURCES= main.cpp
QT+= declarative
TRANSLATIONS = t1_fr.ts t1_sp.ts
OTHER_FILES += Button.qml
== lupdate and lrelease ==
We can now create the translation files by running "lupdate":http://doc.qt.nokia.com/latest/linguist-manager.html#lupdate on the .qml file. lupdate will generate the first set of TS translation source files with all the user-visible text but no translations. In our example, we do:
lupdate main.qml -ts t1_fr.ts t1_sp.ts
to create the translation files. These files can now be opened in "Linguist":http://doc.qt.nokia.com/latest/linguist-manual.html and translated there. Finally, when the application is finished, you need to run "lrelease":http://doc.qt.nokia.com/latest/linguist-manager.html#lrelease on the .ts files to read the .ts files and produce the .qm files used by the application at runtime. For example:
lrelease t1_fr.ts<br />lrelease t1_sp.ts
When running the example at this point, it should now switch between English, French and Spanish when clicking the respective buttons.