[SOLVED] Change language dynamically?
-
wrote on 8 May 2011, 07:34 last edited by
Dear community,
Is it possible to change the language while the program is running?
So far what I'm doing is to store the user language preference in the settings and load it when the application is loaded:@settings = QSettings()
lang = settings.value("lang","en_EN")
translator = QTranslator()
translator.load(os.path.join(currentDir,lang+".qm"))app = QApplication(sys.argv)
app.installTranslator(translator)@I tried calling the global "translator" and loading a new language, but it didn't change anything.
It works only if I close and restart the application.Is it possible to change the language while the program is running?
If yes, how? -
wrote on 8 May 2011, 14:24 last edited by
To update the texts of your objects you need call the "setText" function again, the translate functions only return the translated text and not keep this updated if you change the language.
To support language change dynamically I recommend to you create a function like used on generated ui files, to call after the language change, something like that:
@def retranslateUi(self, Widget):
Widget.button1.setText(QtObject.tr("label1"))
Widget.button2.setText(QtObject.tr("label2"))
....@
-
wrote on 9 May 2011, 03:09 last edited by
Oh, I see!
Thanks ;) -
wrote on 9 May 2011, 05:59 last edited by
Hi,
if a translator is changed or a new translator is installed, the "changeEvent":http://doc.qt.nokia.com/4.7/qwidget.html#changeEvent with QEvent::LanguageChange is called. If you get this event, you have to get all language dependent texts new. One idea to achieve this is to have a function retranslateUI() (or something similar) that you call from this event and your constructor :-)
-
wrote on 9 May 2011, 06:02 last edited by
Oh yeah, this is a very clean way to do it, thanks a lot!
4/5