Proper way for adding 2nd language
-
wrote on 10 Mar 2023, 08:34 last edited by
I added all my text label in english in my project. But now i want to add 2nd language to project. I created a combobox for language selection.
I watched a tutorial, created a
.ts
file, translated some texts with qlinguist, created.qm
file from it. Added it to my project as resource and i tried to load it. Loading returnstrue
but texts are not changing.QTranslator t; qDebug() << t.load(":/i18n/lang_2nd.qm");
Any clean resource about this? Are what can be problem here? And also, i need to create a qm file for original language(english) and load it when selected in combobox, rgiht?
-
I added all my text label in english in my project. But now i want to add 2nd language to project. I created a combobox for language selection.
I watched a tutorial, created a
.ts
file, translated some texts with qlinguist, created.qm
file from it. Added it to my project as resource and i tried to load it. Loading returnstrue
but texts are not changing.QTranslator t; qDebug() << t.load(":/i18n/lang_2nd.qm");
Any clean resource about this? Are what can be problem here? And also, i need to create a qm file for original language(english) and load it when selected in combobox, rgiht?
As described in the documentation you also have to install the translator (and make sure it's alive during the program lifetime).
-
As described in the documentation you also have to install the translator (and make sure it's alive during the program lifetime).
wrote on 10 Mar 2023, 08:53 last edited by@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. -
@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
wrote on 10 Mar 2023, 09:08 last edited by@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.wrote on 10 Mar 2023, 09:14 last edited by JonB 3 Oct 2023, 09:15@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.... -
wrote on 10 Mar 2023, 09:17 last edited by ollarch 3 Oct 2023, 09:19
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". -
@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....wrote on 10 Mar 2023, 09:25 last edited by@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".wrote on 10 Mar 2023, 09:30 last edited by masa4 3 Oct 2023, 09:39@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?wrote on 10 Mar 2023, 09:38 last edited by@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?wrote on 10 Mar 2023, 09:46 last edited by@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?wrote on 10 Mar 2023, 10:07 last edited by@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. -
6/14