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. Proper way for adding 2nd language
Forum Updated to NodeBB v4.3 + New Features

Proper way for adding 2nd language

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 869 Views 2 Watching
  • 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.
  • M masa4

    @Christian-Ehrlicher said in Proper way for adding 2nd language:

    As described in the documentation you also have to install the translator (and make sure it's alive during the program lifetime).

    I moved QTranslator t to header. and added qDebug() << QCoreApplication::installTranslator(&t); after load method. It returns true but still language is not changing. Note that, i didnt changed all translatable words in ts file, just some of them to test it.I dont know if that make any difference.

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @masa4 said in Proper way for adding 2nd language:

    It returns true but still language is not changing.

    Please read my comment:

    and make sure it's alive during the program lifetime

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    M 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @masa4 said in Proper way for adding 2nd language:

      It returns true but still language is not changing.

      Please read my comment:

      and make sure it's alive during the program lifetime

      M Offline
      M Offline
      masa4
      wrote on last edited by
      #5

      @Christian-Ehrlicher Yes object t is alive for program lifetime. I created it in Settings.h and I am just creating one instance from Settings class at program startup in mainwidget. And not deleting it anywhere.

      JonBJ 1 Reply Last reply
      0
      • M masa4

        @Christian-Ehrlicher Yes object t is alive for program lifetime. I created it in Settings.h and I am just creating one instance from Settings class at program startup in mainwidget. And not deleting it anywhere.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #6

        @masa4
        You are wanting to change language at runtime, is that right? Because https://doc.qt.io/qt-6/qtranslator.html#details tells you

        Note that the translator must be created before the application's widgets.

        If you want to change after that I think you have to cause all your tr()s to re-translate, e.g. Change language at runtime? But I have never used it so I stand to be corrected....

        M 1 Reply Last reply
        1
        • O Offline
          O Offline
          ollarch
          wrote on last edited by ollarch
          #7

          If your UI is designed on QDesigner you can call "retranslateUi" on your ui. Look at "ui_yourGUIClass.h" generated by UIC compiler.
          If you add GUI elements dynamically you need to set its text again using the "tr".

          M 1 Reply Last reply
          2
          • JonBJ JonB

            @masa4
            You are wanting to change language at runtime, is that right? Because https://doc.qt.io/qt-6/qtranslator.html#details tells you

            Note that the translator must be created before the application's widgets.

            If you want to change after that I think you have to cause all your tr()s to re-translate, e.g. Change language at runtime? But I have never used it so I stand to be corrected....

            M Offline
            M Offline
            masa4
            wrote on last edited by
            #8

            @JonB Yes in runtime, when user change language setting in combobox, i want to change language. I have multiple ui, so i need call changeEvent for all of my ui class?
            And I didnt understand ReUiSetText part. For example I have:

            QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");
            

            in one of slot. How will I change its text in ReUiSetText across 2 languages?

            JonBJ 1 Reply Last reply
            0
            • O ollarch

              If your UI is designed on QDesigner you can call "retranslateUi" on your ui. Look at "ui_yourGUIClass.h" generated by UIC compiler.
              If you add GUI elements dynamically you need to set its text again using the "tr".

              M Offline
              M Offline
              masa4
              wrote on last edited by masa4
              #9

              @ollarch I created ui's with design ui and all widgets are checked as translatable and they appeared in my ts file when i used lupdate. And yes some part is created dynamically lie QMessageBox etc.
              But its not working right now, since runtime language changing is need additional steps what i understand from above post

              edit: I read your post again, i will check it

              1 Reply Last reply
              0
              • M masa4

                @JonB Yes in runtime, when user change language setting in combobox, i want to change language. I have multiple ui, so i need call changeEvent for all of my ui class?
                And I didnt understand ReUiSetText part. For example I have:

                QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");
                

                in one of slot. How will I change its text in ReUiSetText across 2 languages?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #10

                @masa4
                I think you have to do as @ollarch says. Call retranslateUi() on each instance of classes designed in Designer, plus re-execute any dynamically created widgets' tr() statements.

                QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");

                This won't translate as there is no translation marked on it. Why aren't you using a tr() here?

                M 1 Reply Last reply
                0
                • JonBJ JonB

                  @masa4
                  I think you have to do as @ollarch says. Call retranslateUi() on each instance of classes designed in Designer, plus re-execute any dynamically created widgets' tr() statements.

                  QMessageBox msgBox(QMessageBox::Information, "PROCESS", "Process done.");

                  This won't translate as there is no translation marked on it. Why aren't you using a tr() here?

                  M Offline
                  M Offline
                  masa4
                  wrote on last edited by
                  #11

                  @JonB Thnik this as :

                  QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
                  

                  And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call retranslateUi(), i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something in ReUiSetText method?

                  JonBJ O Christian EhrlicherC 3 Replies Last reply
                  0
                  • M masa4

                    @JonB Thnik this as :

                    QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
                    

                    And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call retranslateUi(), i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something in ReUiSetText method?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #12

                    @masa4
                    Because you recreate the message box whenever it is to be shown its tr() will take into account the current language each time.

                    1 Reply Last reply
                    1
                    • M masa4

                      @JonB Thnik this as :

                      QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
                      

                      And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call retranslateUi(), i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something in ReUiSetText method?

                      O Offline
                      O Offline
                      ollarch
                      wrote on last edited by
                      #13

                      @masa4 As you have translator loaded, every call to "tr" will automatically translate the text. So, in the case of your QMessageBox you only have to add "tr" as @JonB suggested.
                      For your GUI widgets thar are created dynamically you have a pointer to it, so you have to set all the texts again with "tr". If you have own designed widgets it's a good idea to have a method like "retranslateUi" like UIC does.

                      1 Reply Last reply
                      1
                      • M masa4

                        @JonB Thnik this as :

                        QMessageBox msgBox(QMessageBox::Information, tr("PROCESS"), tr("Process done."));
                        

                        And I have 2 .qm files as resources, lang_en.qm, lang_de.qm. After i change langauge setting in combobox english to german, I will call retranslateUi(), i understand that part. But how will i change dynamically created widget's texts like this messagebox? Will it change automatically or will i do something in ReUiSetText method?

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @masa4 said in Proper way for adding 2nd language:

                        But how will i change dynamically created widget's texts like this messagebox?

                        Since you can't show a MessageBox and change the language simulatneously this shouldn't be a problem since the next time you call the message box you text is properly translated due to the tr() call.
                        If you changed a text in your ui after setupUi() you have to do the same manually after the language change event. See the documentation.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • M masa4 referenced this topic on

                        • Login

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