PDA

View Full Version : Change dynamically language just for some forms (not the app)



holst
7th October 2011, 14:21
Hi all,
I have a question about change language dynamically, but not the application language just for same forms.
The scenario is a qt application that has 2 forms:
* One simulates a HMI that can change its language
* Anothe one simulates other HMI that can change its language
So if I change the language in one HMI (form) like this:


if (translator.load(QLatin1String("idiomas_") + locale, resourceDir))
qApp->installTranslator(&translator);

ui.retranslateUi(this);


Everything seems ok, but when I start using the other HMI (form) texts with the language in the othe form are showing. That's because I calling tr() while using the second form and in the first form I've done
qApp->installTranslator(&translator);

Things that I tried without success:
1) Not call qApp->installTranslator(), but nothing changes.
2) Not call qApp->installTranslator() and not use tr() function, use
translator.traslate() instead. This works only with the text in the source but not in ui. Why? Because in funtion "ui.retranslateUi(this)" use the following for the texts:


FrmERTMSClass->setWindowTitle(QApplication::translate("FrmERTMSClass", "FrmERTMS", 0, QApplication::UnicodeUTF8));

So you have to install the translator if you want to use the ui.retranslateUi() function because it call QApplication:translate().

3) Install the new language, call ui.retranslateUi and the install the language that was before. But my problems are in the tr() functions of the first form.

So, anyone can help me?
Does anyone know how to change the language dynamically but only to a part of the application not to all the app?

Thanks in advance.

wysota
7th October 2011, 19:33
Have separate translators for each dialog. Then by exchanging one translator for another one, you only translate the dialog handled by a particular instance of QTranslator.

holst
10th October 2011, 08:40
Thank you for your answer.
So you say to have 1 .ts file for each form, so when I change the language in 1 form remove the translator form the qApp and install the new one.
But if I have the same literal in both forms, example:

tr("Name");
If I install the translator in the first form in the second one the strings in with tr("Name") will be translate with the last installed translator (that is not the second form language).

So the solution is: To have separate translator for each dialog, as your said, and NOT using the tr() function. Instead of it use translator.translate() function?

wysota
10th October 2011, 08:44
But if I have the same literal in both forms, example:

tr("Name");
If I install the translator in the first form in the second one the strings in with tr("Name") will be translate with the last installed translator (that is not the second form language).
No. Have a look at arguments of QObject::tr(). However even that is not required since tr() understands the concept of "translation context", each form will have a different context so no such problems you describe should occur (of course provided the ts file has the context information embedded).