Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.
-
Hello Guys,
I am building an app that should dynamically change the language of my app to any used .qm file. I am successful in doing so as well. But I am unable to make it Dynamic.What I have done is->
used== QTranslator t== object and loaded particular qm file.
then used QGuiApplication object i.e-> app and called installTranslator(&t).Point is I cant find the way to make dynamic changes to the app. So I have to first destroy the ===QQmlApplicationEngine engine ===
again recreate new object of the same and again load the app like this->==== QQmlApplicationEngine engex;
=========engex.load(QUrl(QLatin1String("qrc:/main.qml")));What it does is-> it kills the ongoing app and again opens a new app with changed language.
How successful I am-> I created a another class and used pointers to destroy engine, change app.installTranslator() and again load engex from another class.
How it is working-> It is only working when I am calling the class function from main() method. Means doing all these changes before app.exec();
What problem is actually am facing-> I want to do same thing (or may be i can do different if you guys suggest something else) ON click of a button. I tried to do this but what happens is-> that application closes and opens the changed application but it then crashes just in 1 sec after opening.
NOTE: calling of the function will take place on click of the button of qml, and it always calls it after app.exec().
Is there any possible way to do this with another trick in QT Control 2 application, or can be done in same way with changes?
NOTE: I cant use QWidget class at all.
-
There is a way to force each string to be updated when you change the language.
Define "empty" property and attach it to all your strings.
// C++ side. Expose this property to QML via root context, or QML singleton - whatever Q_PROPERTY(int empty READ empty NOTIFY emptyChanged) // QML side text: qsTr("Some translatable text") + empty
Now, when you change the translation (.qm file), just emit EmptyChanged() signal. You don't need to change it's value, just emit the signal. All strings will be reloaded.
-
There is a way to force each string to be updated when you change the language.
Define "empty" property and attach it to all your strings.
// C++ side. Expose this property to QML via root context, or QML singleton - whatever Q_PROPERTY(int empty READ empty NOTIFY emptyChanged) // QML side text: qsTr("Some translatable text") + empty
Now, when you change the translation (.qm file), just emit EmptyChanged() signal. You don't need to change it's value, just emit the signal. All strings will be reloaded.
@sierdzio wow, Thank you so much...This is one of the best and simplest way to the same.........
I will apply the changes and reply you back.
Thanks. -
Well, I'm not saying it is the best solution, but definitely a simple one and it works (I've seen it in one project, but I am not the author). It would be cool if Qt included some official solution to this.
-
Well, I'm not saying it is the best solution, but definitely a simple one and it works (I've seen it in one project, but I am not the author). It would be cool if Qt included some official solution to this.
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.Its lengthy but works. I applied and its working fine. Thanks mate. It helped a lot.
Its just same as including a .ts and .qm file in project. There it automatically pics the lines you have to change and here You have to go through your project for getting the lines to be changed.
-
Cool, thanks for feedback. Happy coding! (and if the issue is indeed solved, please mark this thread as solved, too. Maybe it will help somebody else, too)
-
Cool, thanks for feedback. Happy coding! (and if the issue is indeed solved, please mark this thread as solved, too. Maybe it will help somebody else, too)
Sure :)
-
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.Its lengthy but works. I applied and its working fine. Thanks mate. It helped a lot.
Its just same as including a .ts and .qm file in project. There it automatically pics the lines you have to change and here You have to go through your project for getting the lines to be changed.
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
-
There is a way to force each string to be updated when you change the language.
Define "empty" property and attach it to all your strings.
// C++ side. Expose this property to QML via root context, or QML singleton - whatever Q_PROPERTY(int empty READ empty NOTIFY emptyChanged) // QML side text: qsTr("Some translatable text") + empty
Now, when you change the translation (.qm file), just emit EmptyChanged() signal. You don't need to change it's value, just emit the signal. All strings will be reloaded.
@sierdzio curios:
BlackBerry10 Cascades did something similar ;-)
https://developer.blackberry.com/native/reference/cascades/bb__cascades__qmlretranslate.html -
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
This post is deleted! -
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
@Eeli-K said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
Hey thanks for pointing out the mistake. Now everyone will find me fool....but anyway
here is what I was doing->I was using QProperty as a String and using signal to change the text. Giving that string property a already translated text. And changing on a click of a button.Thanks for pointing out the mistake sir. Now only empty property is to be added for every qsTr() string. I used the changes and project is working fine.
But on thing is remaining there. 0 number is added automatically at the end of the text,this is because I am using int property. Well I will do my own experiments and update when It will be working properly.
Am sorry for my mistake.Thanks again
-
@Eeli-K said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
Hey thanks for pointing out the mistake. Now everyone will find me fool....but anyway
here is what I was doing->I was using QProperty as a String and using signal to change the text. Giving that string property a already translated text. And changing on a click of a button.Thanks for pointing out the mistake sir. Now only empty property is to be added for every qsTr() string. I used the changes and project is working fine.
But on thing is remaining there. 0 number is added automatically at the end of the text,this is because I am using int property. Well I will do my own experiments and update when It will be working properly.
Am sorry for my mistake.Thanks again
Using QString in place of int for Q_PROPERTY, it worked. Thanks to all.
-
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
@Eeli-K said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
Why it needs distinct property for each text?
It does not. You can use the same "empty" property for all strings.
-
@Eeli-K said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
@sierdzio Yeah.....But this simple method is very easy to implement. You just have to note that..every text in qml should change according to the corresponding property. Every distinct text should have distinct property.
Why it needs distinct property for each text?
Hey thanks for pointing out the mistake. Now everyone will find me fool....but anyway
here is what I was doing->I was using QProperty as a String and using signal to change the text. Giving that string property a already translated text. And changing on a click of a button.Thanks for pointing out the mistake sir. Now only empty property is to be added for every qsTr() string. I used the changes and project is working fine.
But on thing is remaining there. 0 number is added automatically at the end of the text,this is because I am using int property. Well I will do my own experiments and update when It will be working properly.
Am sorry for my mistake.Thanks again
@Akshay-Singh said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
0 number is added automatically at the end of the text,this is because I am using int property.
Yeah, I used int to save me some typing ;-) Using an empty string is much better, indeed, it won't give you an "0" appended to all texts.
-
@Eeli-K said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
Why it needs distinct property for each text?
It does not. You can use the same "empty" property for all strings.
@sierdzio said in Is it possible to Dynamically change the language of Qt Control 2 app using Linguistic, lupdate, .qm,.ts files.:
It does not. You can use the same "empty" property for all strings.
I knew that already, I just wondered why Akshay said so, and he then noticed it, too :)
-
Ah, ok, sorry :-)
-
Hi,
For a simple tutorial on this issue, have a look at this: https://v-play.net/doc/howto-multi-language/
The tutorial uses the V-Play SDK, but it works the same way for standard Qt apps.
Cheers,
Lorenz -
Thanks Everyone for helping me out. Now am having a running project. This is a topic whose solution can not be found easily. So here I uploaded my project in Github. So the beginners can have a look at it. It is the most simplest project that I can make.
Sorry for the naming of the project and files. :p
https://github.com/AkshaySingh0294/QT-Language-Translation-with-QObject-class.git
Refer my .md file for understanding it more correctly