Change language on runtime in QML application
-
I want to change text on runtime. User can select languages on runtime. How to do it?
I searched on google and see some examples.- I created separate class MyClass inherited from QObject and created new slot: languageCreated(QString lang)
@#include <QObject>
#include <QDebug>
#include <QGuiApplication>
#include <QTranslator>
class MyClass : public QObject
{
Q_OBJECT
public:
QString lang;
explicit MyClass(QObject *parent = 0);
void setApp(QGuiApplication *app) {
this->app = app;
}private:
QGuiApplication *app;
signals:public slots:
void languageChanged(QString lang) {
QTranslator translator;
QString filename = QString("languages/lang_") + lang;
if( translator.load(filename, ":/") ){
app->installTranslator(&translator);
qDebug() << "Translation file loaded" << filename;
qDebug() << "Language changed to " << lang;
}
else
qDebug() << "Translation file not loaded:" << filename;}
};@
in main.cpp I created object of this class and connect its slot to qml item signal.
@#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QTranslator>
#include <QTextCodec>
#include <QLocale>
#include <QDebug>
#include "myclass.h"int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);qDebug() << "local:" << QLocale::system().name(); QString locale = "ru_RU"; QString filename = QString("languages/lang_") + locale; static QTranslator translator; if( translator.load(filename, ":/") ){ app.installTranslator(&translator); QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8")); qDebug() << "Translation file loaded" << filename; } else qDebug() << "Translation file not loaded:" << filename; QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/MultiLingualQML/main.qml")); viewer.showExpanded(); MyClass myclass; QObject *item = (QObject *)viewer.rootObject(); QObject::connect(item, SIGNAL(languageChanging(QString)), &myclass, SLOT(languageChanged(QString))); return app.exec();
}@
and on mouse click in qml I emit signal.
@import QtQuick 2.0
import QtQuick.Layouts 1.0Item {
id: item
signal languageChanging(string lang)
GridLayout {
Rectangle {
Layout.fillWidth: true;
width: 80
height: 80
anchors.verticalCenter: parent.verticalCenter
Text {
id: hello
anchors.fill: parent
text: qsTr("Hello World") //+ Retranslate.onLanguageChanged
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
Rectangle {
width: 80
height: 80
Layout.fillWidth: true;
Text {
id: lang_en
text: "EN"
}
MouseArea {
anchors.fill: lang_en
onClicked: {
item.languageChanging("en_US");
hello.text = qsTr("Hello World")
}
}
}
Rectangle {
Layout.fillWidth: true;
width: 80
height: 80
Text {
id: lang_ru
text: "Русский"
}
MouseArea {
anchors.fill: lang_ru
onClicked: {
item.languageChanging("ru_RU");
hello.text = qsTr("Hello World")
}
}
}
Rectangle {
Layout.fillWidth: true;
width: 80
height: 80
Text {
id: name
text: qsTr("Firstname")
}
}
}
}
@debug logs show that slot of myclass object is ran. But language of text not changed.
- I created separate class MyClass inherited from QObject and created new slot: languageCreated(QString lang)
-
This "How-To":http://qt-project.org/wiki/How_to_do_dynamic_translation_in_QML could be useful..
-
[quote author="Rahul Das" date="1385372342"]This "How-To":http://qt-project.org/wiki/How_to_do_dynamic_translation_in_QML could be useful..[/quote]
Thank you. I will look at it. -
Unfortunatly I did not succeed. My project is Qt Quick project. On example above I thought is Qt GUI project.
May be there is not big difference between this two type of projects to implement this functionality, but I could not manage it.
Any help is appreciated. -
Hi,
The example given is exactly what you need except it's a QtQuick1 project but that shouldn't be a problem.
-
ok, I will keep trying.
-
thank you Rahul Das and SGaist.
I have succeed. But I make a little change on example you gave. I used translation file with qm extension and included it to resource file. Otherwise it did not work on my example. I dont know why.