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 Offline
    M Offline
    masa4
    wrote on 10 Mar 2023, 08:34 last edited by
    #1

    I added all my text label in english in my project. But now i want to add 2nd language to project. I created a combobox for language selection.

    I watched a tutorial, created a .ts file, translated some texts with qlinguist, created .qm file from it. Added it to my project as resource and i tried to load it. Loading returns true but texts are not changing.

    QTranslator t;
    qDebug() << t.load(":/i18n/lang_2nd.qm");
    

    Any clean resource about this? Are what can be problem here? And also, i need to create a qm file for original language(english) and load it when selected in combobox, rgiht?

    C 1 Reply Last reply 10 Mar 2023, 08:40
    0
    • M masa4
      10 Mar 2023, 08:34

      I added all my text label in english in my project. But now i want to add 2nd language to project. I created a combobox for language selection.

      I watched a tutorial, created a .ts file, translated some texts with qlinguist, created .qm file from it. Added it to my project as resource and i tried to load it. Loading returns true but texts are not changing.

      QTranslator t;
      qDebug() << t.load(":/i18n/lang_2nd.qm");
      

      Any clean resource about this? Are what can be problem here? And also, i need to create a qm file for original language(english) and load it when selected in combobox, rgiht?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Mar 2023, 08:40 last edited by
      #2

      As described in the documentation you also have to install the translator (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 10 Mar 2023, 08:53
      1
      • C Christian Ehrlicher
        10 Mar 2023, 08:40

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

        M Offline
        M Offline
        masa4
        wrote on 10 Mar 2023, 08:53 last edited by
        #3

        @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.

        C 1 Reply Last reply 10 Mar 2023, 09:04
        0
        • M masa4
          10 Mar 2023, 08:53

          @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.

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 10 Mar 2023, 09:04 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 10 Mar 2023, 09:08
          0
          • C Christian Ehrlicher
            10 Mar 2023, 09:04

            @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 10 Mar 2023, 09:08 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.

            J 1 Reply Last reply 10 Mar 2023, 09:14
            0
            • M masa4
              10 Mar 2023, 09:08

              @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.

              J Offline
              J Offline
              JonB
              wrote on 10 Mar 2023, 09:14 last edited by JonB 3 Oct 2023, 09:15
              #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 10 Mar 2023, 09:25
              1
              • O Offline
                O Offline
                ollarch
                wrote on 10 Mar 2023, 09:17 last edited by ollarch 3 Oct 2023, 09:19
                #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 10 Mar 2023, 09:30
                2
                • J JonB
                  10 Mar 2023, 09:14

                  @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 10 Mar 2023, 09:25 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?

                  J 1 Reply Last reply 10 Mar 2023, 09:38
                  0
                  • O ollarch
                    10 Mar 2023, 09:17

                    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 10 Mar 2023, 09:30 last edited by masa4 3 Oct 2023, 09:39
                    #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
                      10 Mar 2023, 09:25

                      @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?

                      J Offline
                      J Offline
                      JonB
                      wrote on 10 Mar 2023, 09:38 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 10 Mar 2023, 09:46
                      0
                      • J JonB
                        10 Mar 2023, 09:38

                        @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 10 Mar 2023, 09:46 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?

                        J O C 3 Replies Last reply 10 Mar 2023, 10:06
                        0
                        • M masa4
                          10 Mar 2023, 09:46

                          @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?

                          J Offline
                          J Offline
                          JonB
                          wrote on 10 Mar 2023, 10:06 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
                            10 Mar 2023, 09:46

                            @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 10 Mar 2023, 10:07 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
                              10 Mar 2023, 09:46

                              @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?

                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 10 Mar 2023, 10:07 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 24 Mar 2023, 08:13

                              6/14

                              10 Mar 2023, 09:14

                              • Login

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