Qt World Summit: Register Today!

[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()

    app = QApplication(sys.argv)

    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):


  • 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!

Log in to reply