Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Change language dynamically?
QtWS25 Last Chance

[SOLVED] Change language dynamically?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ThomasDalla
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      renato.filho
      wrote on last edited by
      #2

      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"))
      ....

      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThomasDalla
        wrote on last edited by
        #3

        Oh, I see!
        Thanks ;)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

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

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThomasDalla
            wrote on last edited by
            #5

            Oh yeah, this is a very clean way to do it, thanks a lot!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved