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. Translation does not work when the text of QLineEdit is modified
Forum Updated to NodeBB v4.3 + New Features

Translation does not work when the text of QLineEdit is modified

Scheduled Pinned Locked Moved Solved General and Desktop
translation
13 Posts 4 Posters 3.8k 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.
  • Z Offline
    Z Offline
    Zoltan
    wrote on 3 Dec 2016, 11:55 last edited by
    #1

    I have a desktop application I want to translate. All translations work well except in the following case. I have a QLineEdit object called resultEdit whose text is updated when a pushbutton is clicked on. resultEdit can display the following strings:

    const QString days[7] = {QCoreApplication::tr("Monday"), \
                             QCoreApplication::tr("Tuesday"), \
                             QCoreApplication::tr("Wednesday"), \
                             QCoreApplication::tr("Thursday"), \
                             QCoreApplication::tr("Friday"), \
                             QCoreApplication::tr("Saturday"), \
                             QCoreApplication::tr("Sunday")};
    

    So days is a global array. One callback functions which calculates which day to print is

    void ZellerGUI::on_yearEdit_editingFinished()
    {
        // Calculate the day of the week
        int calculatedDay = some_integer_from_1_to_7;
        ui->resultEdit->setText(days[calculatedDay - 1]);
    }
    

    However, when I select the translation, the names of the days remain in English. It is weird as all other stings are translated well. When I opened the Linguist, it recognised all seven days. What happens in the background that the translations do not work for this global array?

    K 1 Reply Last reply 3 Dec 2016, 18:19
    0
    • K kshegunov
      3 Dec 2016, 19:44

      Well, did you translate it?

      QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
      
      Z Offline
      Z Offline
      Zoltan
      wrote on 3 Dec 2016, 19:52 last edited by
      #11

      @kshegunov I just used your previous solution:

      QString day = QCoreApplication::tr(days[calculatedDay - 1]);
      

      Using your last command

      QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
      

      perfectly works. Now I just have to think it over why it works.

      Thank you very much for your precious help!

      K ? 2 Replies Last reply 3 Dec 2016, 19:55
      0
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 3 Dec 2016, 16:48 last edited by
        #2

        Could it be that static string is built into the binary so it is not being initialized after you load translation QM file?

        (Z(:^

        Z 1 Reply Last reply 3 Dec 2016, 16:58
        2
        • S sierdzio
          3 Dec 2016, 16:48

          Could it be that static string is built into the binary so it is not being initialized after you load translation QM file?

          Z Offline
          Z Offline
          Zoltan
          wrote on 3 Dec 2016, 16:58 last edited by
          #3

          @sierdzio I don't know the inner working of Qt. If it is so, then what would you recommend me to modify?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 3 Dec 2016, 17:02 last edited by
            #4

            This is rather inner working of C++ itself. But I am only guessing to be honest.

            What you can try is to define this string array (btw. you can also use QStringList) in a singleton class that is initialized after translation is loaded, so that it gets correct data.

            (Z(:^

            Z 1 Reply Last reply 3 Dec 2016, 17:49
            0
            • S sierdzio
              3 Dec 2016, 17:02

              This is rather inner working of C++ itself. But I am only guessing to be honest.

              What you can try is to define this string array (btw. you can also use QStringList) in a singleton class that is initialized after translation is loaded, so that it gets correct data.

              Z Offline
              Z Offline
              Zoltan
              wrote on 3 Dec 2016, 17:49 last edited by
              #5

              @sierdzio My constant variable is called from the ui. But the ui class is instantiated after the translation is loaded. Shouldn't it be OK (it isn't)?

              1 Reply Last reply
              0
              • Z Zoltan
                3 Dec 2016, 11:55

                I have a desktop application I want to translate. All translations work well except in the following case. I have a QLineEdit object called resultEdit whose text is updated when a pushbutton is clicked on. resultEdit can display the following strings:

                const QString days[7] = {QCoreApplication::tr("Monday"), \
                                         QCoreApplication::tr("Tuesday"), \
                                         QCoreApplication::tr("Wednesday"), \
                                         QCoreApplication::tr("Thursday"), \
                                         QCoreApplication::tr("Friday"), \
                                         QCoreApplication::tr("Saturday"), \
                                         QCoreApplication::tr("Sunday")};
                

                So days is a global array. One callback functions which calculates which day to print is

                void ZellerGUI::on_yearEdit_editingFinished()
                {
                    // Calculate the day of the week
                    int calculatedDay = some_integer_from_1_to_7;
                    ui->resultEdit->setText(days[calculatedDay - 1]);
                }
                

                However, when I select the translation, the names of the days remain in English. It is weird as all other stings are translated well. When I opened the Linguist, it recognised all seven days. What happens in the background that the translations do not work for this global array?

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 3 Dec 2016, 18:19 last edited by
                #6

                Try something like this:

                const char * const days[7] = {
                    QT_TR_NOOP("Monday"),
                    QT_TR_NOOP("Tuesday"),
                    // ... and so on
                }
                
                void ZellerGUI::on_yearEdit_editingFinished()
                {
                    // ... calculate day
                    QString day = QCoreApplication::tr(days[calculatedDay - 1]);
                    ui->resultEdit->setText(day);
                }
                

                Read and abide by the Qt Code of Conduct

                Z 1 Reply Last reply 3 Dec 2016, 18:29
                0
                • K kshegunov
                  3 Dec 2016, 18:19

                  Try something like this:

                  const char * const days[7] = {
                      QT_TR_NOOP("Monday"),
                      QT_TR_NOOP("Tuesday"),
                      // ... and so on
                  }
                  
                  void ZellerGUI::on_yearEdit_editingFinished()
                  {
                      // ... calculate day
                      QString day = QCoreApplication::tr(days[calculatedDay - 1]);
                      ui->resultEdit->setText(day);
                  }
                  
                  Z Offline
                  Z Offline
                  Zoltan
                  wrote on 3 Dec 2016, 18:29 last edited by
                  #7

                  @kshegunov When I call lupdate on the project file, it throws these warnings:

                  tr() cannot be called without context
                  

                  and these strings do not appear in Linguist.

                  K 1 Reply Last reply 3 Dec 2016, 19:05
                  0
                  • Z Zoltan
                    3 Dec 2016, 18:29

                    @kshegunov When I call lupdate on the project file, it throws these warnings:

                    tr() cannot be called without context
                    

                    and these strings do not appear in Linguist.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 3 Dec 2016, 19:05 last edited by
                    #8

                    Yes I think my example is rotten. Try with QT_TRANSLATE_NOOP and give it a context:

                    const char * const days[7] = {
                        QT_TRANSLATE_NOOP("Weekdays", "Monday"),
                        QT_TRANSLATE_NOOP("Weekdays", "Tuesday"),
                        // ... and so on
                    }
                    

                    Read and abide by the Qt Code of Conduct

                    Z 1 Reply Last reply 3 Dec 2016, 19:18
                    2
                    • K kshegunov
                      3 Dec 2016, 19:05

                      Yes I think my example is rotten. Try with QT_TRANSLATE_NOOP and give it a context:

                      const char * const days[7] = {
                          QT_TRANSLATE_NOOP("Weekdays", "Monday"),
                          QT_TRANSLATE_NOOP("Weekdays", "Tuesday"),
                          // ... and so on
                      }
                      
                      Z Offline
                      Z Offline
                      Zoltan
                      wrote on 3 Dec 2016, 19:18 last edited by Zoltan 12 Mar 2016, 19:29
                      #9

                      @kshegunov Thank you. Now Linguist recognizes it but my application doesn't appreciate it yet. In the debugger, I also see the English name.

                      K 1 Reply Last reply 3 Dec 2016, 19:44
                      0
                      • Z Zoltan
                        3 Dec 2016, 19:18

                        @kshegunov Thank you. Now Linguist recognizes it but my application doesn't appreciate it yet. In the debugger, I also see the English name.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 3 Dec 2016, 19:44 last edited by
                        #10

                        Well, did you translate it?

                        QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
                        

                        Read and abide by the Qt Code of Conduct

                        Z 1 Reply Last reply 3 Dec 2016, 19:52
                        2
                        • K kshegunov
                          3 Dec 2016, 19:44

                          Well, did you translate it?

                          QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
                          
                          Z Offline
                          Z Offline
                          Zoltan
                          wrote on 3 Dec 2016, 19:52 last edited by
                          #11

                          @kshegunov I just used your previous solution:

                          QString day = QCoreApplication::tr(days[calculatedDay - 1]);
                          

                          Using your last command

                          QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
                          

                          perfectly works. Now I just have to think it over why it works.

                          Thank you very much for your precious help!

                          K ? 2 Replies Last reply 3 Dec 2016, 19:55
                          0
                          • Z Zoltan
                            3 Dec 2016, 19:52

                            @kshegunov I just used your previous solution:

                            QString day = QCoreApplication::tr(days[calculatedDay - 1]);
                            

                            Using your last command

                            QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
                            

                            perfectly works. Now I just have to think it over why it works.

                            Thank you very much for your precious help!

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 3 Dec 2016, 19:55 last edited by kshegunov 12 Mar 2016, 20:01
                            #12

                            You're welcome.
                            Happy coding!

                            Edit:
                            Btw, moving the keys to the function should also work:

                            void ZellerGUI::on_yearEdit_editingFinished()
                            {
                                // If this is initialized here it should work okay
                                static const char * const days[7] = {
                                    QT_TR_NOOP("Monday"),
                                    QT_TR_NOOP("Tuesday"),
                                    // ... and so on
                                };
                                // ... calculate day
                                QString day = QCoreApplication::tr(days[calculatedDay - 1]);
                                ui->resultEdit->setText(day);
                            }
                            

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            3
                            • Z Zoltan
                              3 Dec 2016, 19:52

                              @kshegunov I just used your previous solution:

                              QString day = QCoreApplication::tr(days[calculatedDay - 1]);
                              

                              Using your last command

                              QString day = QCoreApplication::translate("Weekdays", days[calculatedDay - 1]);
                              

                              perfectly works. Now I just have to think it over why it works.

                              Thank you very much for your precious help!

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on 11 Apr 2019, 14:30 last edited by
                              #13

                              @Zoltan I have the same problem, must I do?

                              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