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. Qt Linguist translation error
Forum Updated to NodeBB v4.3 + New Features

Qt Linguist translation error

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 3.1k Views 1 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello!

    I am using Qt Linguist to translate my app to chinese. I want to do it inside the application, in a settings window.

    I have a QComboBox to switch between the languages, and when I select the option, this is my code:

    if (arg1.compare("Chinese", Qt::CaseInsensitive) == 0) {
            T.load(":/Translations/resources/translations/myApp_ch.qm");
            QApplication::installTranslator(&T);
            QCoreApplication::installTranslator(&T);
            ui->retranslateUi(this);
    }
    

    The problem is that there are some windows that do not translate.

    However, if I do it from main.cpp, it works fine:

    QApplication a(argc, argv);
    QTranslator T;
    T.load(":/Translations/resources/translations/myApp_ch.qm");
    a.installTranslator(&T);
    

    What am I doing wrong?

    Thank you very guys!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ivanicy said in Qt Linguist translation error:

      What am I doing wrong?

      Only newly created windows are using the translator automatically. For the rest you have to watch for the LanguageChange event as explained here.

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

      ivanicyI 1 Reply Last reply
      5
      • Christian EhrlicherC Christian Ehrlicher

        @ivanicy said in Qt Linguist translation error:

        What am I doing wrong?

        Only newly created windows are using the translator automatically. For the rest you have to watch for the LanguageChange event as explained here.

        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        @Christian-Ehrlicher And, how should I do that? I have to reimplement the changeEvent in every windows in my app?

        Christian EhrlicherC J.HilkJ artwawA 3 Replies Last reply
        0
        • ivanicyI ivanicy

          @Christian-Ehrlicher And, how should I do that? I have to reimplement the changeEvent in every windows in my app?

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

          @ivanicy said in Qt Linguist translation error:

          I have to reimplement the changeEvent in every windows in my app?

          if you want online translation, yes. I would simply restart the app / tell the user to do so.

          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
          0
          • ivanicyI ivanicy

            @Christian-Ehrlicher And, how should I do that? I have to reimplement the changeEvent in every windows in my app?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @ivanicy if all your translatable texts are inside your ui form classes, than it's a simple matter of calling

            ui->retranslateUi(this);

            on each form, after the translator is installed.

            However, if you yourself set a - for translation marked -string inside your application (tr("")), you will have to set that string again after the translator is loaded and installed


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • ivanicyI ivanicy

              @Christian-Ehrlicher And, how should I do that? I have to reimplement the changeEvent in every windows in my app?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @ivanicy It's not that complicated. Basically you need to write custom slot for every class that has a form. Slot is rather simple:

              void MainWindow::changeEvent(QEvent *e)
              {
                  QMainWindow::changeEvent(e);
                  switch (e->type()) {
                  case QEvent::LanguageChange:
                      ui->retranslateUi(this);
                      break;
                  default:
                      break;
                  }
              }
              

              This is the example for a main program window (hence MainWindow and QMainWindow), but for classes that origin in, let's say, QDialog you'd obviously need to change code a bit.

              For more information please re-read.

              Kind Regards,
              Artur

              ivanicyI 2 Replies Last reply
              0
              • artwawA artwaw

                @ivanicy It's not that complicated. Basically you need to write custom slot for every class that has a form. Slot is rather simple:

                void MainWindow::changeEvent(QEvent *e)
                {
                    QMainWindow::changeEvent(e);
                    switch (e->type()) {
                    case QEvent::LanguageChange:
                        ui->retranslateUi(this);
                        break;
                    default:
                        break;
                    }
                }
                

                This is the example for a main program window (hence MainWindow and QMainWindow), but for classes that origin in, let's say, QDialog you'd obviously need to change code a bit.

                ivanicyI Offline
                ivanicyI Offline
                ivanicy
                wrote on last edited by
                #7

                @artwaw Yes, I know how to do it, but I have a lot of windows in my app hehe. Thank you very much guys. @J-Hilk @Christian-Ehrlicher.

                I have another question about it. I have lines like this in my code:

                QString sText;
                if (sText.compare(tr("something"), Qt::CaseInsensitive) == 0) { ... }
                

                In these cases, the compare function doesn't work. But I need to compare with the translated word. How can I fix it?

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ivanicy said in Qt Linguist translation error:

                  In these cases, the compare function doesn't work.

                  What does this mean?

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

                  ivanicyI 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @ivanicy said in Qt Linguist translation error:

                    In these cases, the compare function doesn't work.

                    What does this mean?

                    ivanicyI Offline
                    ivanicyI Offline
                    ivanicy
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher The compare function doesn't work when I have a tr() inside it. It doesn't enter in this if().

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then you should look what tr("seomthing") returns matches what you're expecting.

                      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
                      0
                      • artwawA artwaw

                        @ivanicy It's not that complicated. Basically you need to write custom slot for every class that has a form. Slot is rather simple:

                        void MainWindow::changeEvent(QEvent *e)
                        {
                            QMainWindow::changeEvent(e);
                            switch (e->type()) {
                            case QEvent::LanguageChange:
                                ui->retranslateUi(this);
                                break;
                            default:
                                break;
                            }
                        }
                        

                        This is the example for a main program window (hence MainWindow and QMainWindow), but for classes that origin in, let's say, QDialog you'd obviously need to change code a bit.

                        ivanicyI Offline
                        ivanicyI Offline
                        ivanicy
                        wrote on last edited by
                        #11

                        @artwaw I have a stack overflow error in this function:

                        837ec2b1-0ce0-4b08-96a1-4e77c4fb6330-image.png

                        Do you know what is happening here?

                        Thank you very much!

                        J.HilkJ artwawA 2 Replies Last reply
                        0
                        • ivanicyI ivanicy

                          @artwaw I have a stack overflow error in this function:

                          837ec2b1-0ce0-4b08-96a1-4e77c4fb6330-image.png

                          Do you know what is happening here?

                          Thank you very much!

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by J.Hilk
                          #12

                          @ivanicy I would change your last line from
                          MainWindow::changeEvent(...)
                          to
                          QMainWindow::changeEvent(...)

                          therefore, making it nor a recursive function but rather a call to the base class.


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          ivanicyI 1 Reply Last reply
                          2
                          • J.HilkJ J.Hilk

                            @ivanicy I would change your last line from
                            MainWindow::changeEvent(...)
                            to
                            QMainWindow::changeEvent(...)

                            therefore, making it nor a recursive function but rather a call to the base class.

                            ivanicyI Offline
                            ivanicyI Offline
                            ivanicy
                            wrote on last edited by
                            #13

                            @J-Hilk I use this function in more windows. I have to use QMainWindow in this windows too?

                            artwawA 1 Reply Last reply
                            0
                            • ivanicyI ivanicy

                              @artwaw I have a stack overflow error in this function:

                              837ec2b1-0ce0-4b08-96a1-4e77c4fb6330-image.png

                              Do you know what is happening here?

                              Thank you very much!

                              artwawA Offline
                              artwawA Offline
                              artwaw
                              wrote on last edited by
                              #14

                              @ivanicy Please look again at my example - let the QMainWindow try to handle the event first.
                              In your code you call QMainWindow at the last line of the method.

                              For more information please re-read.

                              Kind Regards,
                              Artur

                              ivanicyI 1 Reply Last reply
                              0
                              • ivanicyI ivanicy

                                @J-Hilk I use this function in more windows. I have to use QMainWindow in this windows too?

                                artwawA Offline
                                artwawA Offline
                                artwaw
                                wrote on last edited by artwaw
                                #15

                                @ivanicy said in Qt Linguist translation error:

                                I use this function in more windows. I have to use QMainWindow in this windows too?

                                First thing in the changeEvent is to call respective ancestor's changeEvent.

                                EDIT: I use QMainWindow in the example since I stated that it is for QMainWindow descendant. If your window inherits from QDialog you should call QDialog, etc...

                                For more information please re-read.

                                Kind Regards,
                                Artur

                                ivanicyI 1 Reply Last reply
                                1
                                • artwawA artwaw

                                  @ivanicy Please look again at my example - let the QMainWindow try to handle the event first.
                                  In your code you call QMainWindow at the last line of the method.

                                  ivanicyI Offline
                                  ivanicyI Offline
                                  ivanicy
                                  wrote on last edited by
                                  #16
                                  This post is deleted!
                                  1 Reply Last reply
                                  0
                                  • artwawA artwaw

                                    @ivanicy said in Qt Linguist translation error:

                                    I use this function in more windows. I have to use QMainWindow in this windows too?

                                    First thing in the changeEvent is to call respective ancestor's changeEvent.

                                    EDIT: I use QMainWindow in the example since I stated that it is for QMainWindow descendant. If your window inherits from QDialog you should call QDialog, etc...

                                    ivanicyI Offline
                                    ivanicyI Offline
                                    ivanicy
                                    wrote on last edited by
                                    #17

                                    @artwaw Thank you very much!

                                    1 Reply Last reply
                                    0
                                    • ivanicyI Offline
                                      ivanicyI Offline
                                      ivanicy
                                      wrote on last edited by
                                      #18

                                      Sorry for bothering so much. I have detected that this method don't translate texts that I have put by coding, for example context menus or treeviews.

                                      I use

                                      ui->retranslateUi(this);
                                      

                                      Is there something missing to translate the code part as well?

                                      Thank you and sorry guys :(

                                      J.HilkJ artwawA 2 Replies Last reply
                                      0
                                      • Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @ivanicy said in Qt Linguist translation error:

                                        ui->retranslateUi(this);

                                        As you can see, only the stuff in the ui struct can be translated with this call - how should this translate e.g. text from a model or somewhere else? The rest has to be done manually.

                                        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
                                        0
                                        • ivanicyI ivanicy

                                          Sorry for bothering so much. I have detected that this method don't translate texts that I have put by coding, for example context menus or treeviews.

                                          I use

                                          ui->retranslateUi(this);
                                          

                                          Is there something missing to translate the code part as well?

                                          Thank you and sorry guys :(

                                          J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by
                                          #20

                                          @ivanicy said in Qt Linguist translation error:

                                          Sorry for bothering so much. I have detected that this method don't translate texts that I have put by coding, for example context menus or treeviews.

                                          I use

                                          ui->retranslateUi(this);
                                          

                                          Is there something missing to translate the code part as well?

                                          Thank you and sorry guys :(

                                          @J-Hilk said in Qt Linguist translation error:

                                          However, if you yourself set a - for translation marked -string inside your application (tr("")), you will have to set that string again after the translator is loaded and installed


                                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                          Q: What's that?
                                          A: It's blue light.
                                          Q: What does it do?
                                          A: It turns blue.

                                          1 Reply Last reply
                                          1

                                          • Login

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