[SOLVED] Change language dynamically?
-
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? -
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"))
....@
-
Oh, I see!
Thanks ;) -
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 :-)
-
Oh yeah, this is a very clean way to do it, thanks a lot!