Proper way for adding 2nd language
-
@Christian-Ehrlicher said in Proper way for adding 2nd language:
As described in the documentation you also have to install the translator (and make sure it's alive during the program lifetime).
I moved
QTranslator t
to header. and addedqDebug() << QCoreApplication::installTranslator(&t);
afterload
method. It returns true but still language is not changing. Note that, i didnt changed all translatable words in ts file, just some of them to test it.I dont know if that make any difference.@masa4 said in Proper way for adding 2nd language:
It returns true but still language is not changing.
Please read my comment:
and make sure it's alive during the program lifetime
-
@masa4 said in Proper way for adding 2nd language:
It returns true but still language is not changing.
Please read my comment:
and make sure it's alive during the program lifetime
@Christian-Ehrlicher Yes object
t
is alive for program lifetime. I created it inSettings.h
and I am just creating one instance fromSettings
class at program startup in mainwidget. And not deleting it anywhere. -
@Christian-Ehrlicher Yes object
t
is alive for program lifetime. I created it inSettings.h
and I am just creating one instance fromSettings
class at program startup in mainwidget. And not deleting it anywhere.@masa4
You are wanting to change language at runtime, is that right? Because https://doc.qt.io/qt-6/qtranslator.html#details tells youNote that the translator must be created before the application's widgets.
If you want to change after that I think you have to cause all your
tr()
s to re-translate, e.g. Change language at runtime? But I have never used it so I stand to be corrected.... -
@masa4
You are wanting to change language at runtime, is that right? Because https://doc.qt.io/qt-6/qtranslator.html#details tells youNote that the translator must be created before the application's widgets.
If you want to change after that I think you have to cause all your
tr()
s to re-translate, e.g. Change language at runtime? But I have never used it so I stand to be corrected....@JonB Yes in runtime, when user change language setting in combobox, i want to change language. I have multiple ui, so i need call
changeEvent
for all of my ui class?
And I didnt understandReUiSetText
part. For example I have:QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");
in one of slot. How will I change its text in
ReUiSetText
across 2 languages? -
If your UI is designed on QDesigner you can call "retranslateUi" on your ui. Look at "ui_yourGUIClass.h" generated by UIC compiler.
If you add GUI elements dynamically you need to set its text again using the "tr".@ollarch I created ui's with design ui and all widgets are checked as translatable and they appeared in my ts file when i used
lupdate
. And yes some part is created dynamically lie QMessageBox etc.
But its not working right now, since runtime language changing is need additional steps what i understand from above postedit: I read your post again, i will check it
-
@JonB Yes in runtime, when user change language setting in combobox, i want to change language. I have multiple ui, so i need call
changeEvent
for all of my ui class?
And I didnt understandReUiSetText
part. For example I have:QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");
in one of slot. How will I change its text in
ReUiSetText
across 2 languages?@masa4
I think you have to do as @ollarch says. CallretranslateUi()
on each instance of classes designed in Designer, plus re-execute any dynamically created widgets'tr()
statements.QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");
This won't translate as there is no translation marked on it. Why aren't you using a
tr()
here? -
@masa4
I think you have to do as @ollarch says. CallretranslateUi()
on each instance of classes designed in Designer, plus re-execute any dynamically created widgets'tr()
statements.QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");
This won't translate as there is no translation marked on it. Why aren't you using a
tr()
here?@JonB Thnik this as :
QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call
retranslateUi()
, i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something inReUiSetText
method? -
@JonB Thnik this as :
QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call
retranslateUi()
, i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something inReUiSetText
method? -
@JonB Thnik this as :
QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call
retranslateUi()
, i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something inReUiSetText
method?@masa4 As you have translator loaded, every call to "tr" will automatically translate the text. So, in the case of your QMessageBox you only have to add "tr" as @JonB suggested.
For your GUI widgets thar are created dynamically you have a pointer to it, so you have to set all the texts again with "tr". If you have own designed widgets it's a good idea to have a method like "retranslateUi" like UIC does. -
@JonB Thnik this as :
QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call
retranslateUi()
, i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something inReUiSetText
method?@masa4 said in Proper way for adding 2nd language:
But how will i change dynamically created widget's texts like this messagebox?
Since you can't show a MessageBox and change the language simulatneously this shouldn't be a problem since the next time you call the message box you text is properly translated due to the tr() call.
If you changed a text in your ui after setupUi() you have to do the same manually after the language change event. See the documentation. -