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. qtranslator with dynamic labels
Forum Updated to NodeBB v4.3 + New Features

qtranslator with dynamic labels

Scheduled Pinned Locked Moved Solved General and Desktop
45 Posts 4 Posters 8.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have a dictionary italian-english and english-italian, but with this dictionary I can translate only the labels fixed in ui, the labels that I write during the program aren't translated
    I wrote:

    void MainWindow::on_Traduzione_Inglese_clicked()
    {
        qApp->installTranslator(&Lingua_Italiana);
        ui->Traduzione_Italiano->setDisabled(false);
        // if(ui->info->text=="CONF. IN CARICAMENTO")
        if(ui->Info->text()=="CONF. IN CARICAMENTO")
    
            ui->Info->setText("cONF..lOADING");
    }
    
    

    if is for dynamic label but it doesn't work, is there a way fast and not manual?

    J.HilkJ 1 Reply Last reply
    0
    • ? A Former User

      I have a dictionary italian-english and english-italian, but with this dictionary I can translate only the labels fixed in ui, the labels that I write during the program aren't translated
      I wrote:

      void MainWindow::on_Traduzione_Inglese_clicked()
      {
          qApp->installTranslator(&Lingua_Italiana);
          ui->Traduzione_Italiano->setDisabled(false);
          // if(ui->info->text=="CONF. IN CARICAMENTO")
          if(ui->Info->text()=="CONF. IN CARICAMENTO")
      
              ui->Info->setText("cONF..lOADING");
      }
      
      

      if is for dynamic label but it doesn't work, is there a way fast and not manual?

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

      @vale88
      hi, when you load a qtranslator a general ChangeEvent is send through the application.

      one usually listens to that and reapplies/sets the texts.
      for your ui form class this is automated via a ui->retranslateUi(this); call

      If you set any texts inside your source code, you'll have to mark those for translation your self viatr("Text to Translate") and simply recall the setText function.

      example:

      void myClass::changeEvent(QEvent *e)
      {
          QWidget::changeEvent(e);
          switch (e->type()) {
          case QEvent::LanguageChange:
              ui->retranslateUi(this);
              setTexts();
              break;
          default:
              break;
          }
      }
      
      void myClass::setTexts()
      {
          backButton->setText(tr("Back"));
          closeButton->setText(tr("Close"));
          saveButton->setText(tr( "Save"));
          ....
      }
      

      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
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @vale88 said in qtranslator with dynamic labels:

        if is for dynamic label but it doesn't work, is there a way fast and not manual?

        Nope. Any text must be marked with TR or it won't work.

        you should read this to get an overview of how it works
        https://doc.qt.io/qt-5/i18n-source-translation.html

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @vale88
          hi, when you load a qtranslator a general ChangeEvent is send through the application.

          one usually listens to that and reapplies/sets the texts.
          for your ui form class this is automated via a ui->retranslateUi(this); call

          If you set any texts inside your source code, you'll have to mark those for translation your self viatr("Text to Translate") and simply recall the setText function.

          example:

          void myClass::changeEvent(QEvent *e)
          {
              QWidget::changeEvent(e);
              switch (e->type()) {
              case QEvent::LanguageChange:
                  ui->retranslateUi(this);
                  setTexts();
                  break;
              default:
                  break;
              }
          }
          
          void myClass::setTexts()
          {
              backButton->setText(tr("Back"));
              closeButton->setText(tr("Close"));
              saveButton->setText(tr( "Save"));
              ....
          }
          
          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @J.Hilk yes, I have:

          void MainWindow::changeEvent(QEvent* event)
          {
              if(event->type() == QEvent::LanguageChange)
              {
                  ui->retranslateUi(this);
              }
          
              QWidget::changeEvent(event);
          }
          
          
          

          and then:

          void MainWindow::on_Traduzione_Inglese_clicked()
          {
              qApp->installTranslator(&Lingua_Italiana);
              ui->Traduzione_Italiano->setDisabled(false);
              // if(ui->info->text=="CONF. IN CARICAMENTO")
              if(ui->Info->text()=="CONF. IN CARICAMENTO")
          
                  ui->Info->setText("cONF..lOADING");
          }
          
          

          if I have a function like this:

          void MainWindow::Caricamento()
          {
              ui->Configurazione_Attuale->setText(selected_file_name);
              ui->Info->setText("CONF. CARICATA");
          }
          
          

          i can write ui->Info->setText(tr(....
          but how I can call the function in change event, because I have other functions in Caricamento

          ? 1 Reply Last reply
          0
          • ? A Former User

            @J.Hilk yes, I have:

            void MainWindow::changeEvent(QEvent* event)
            {
                if(event->type() == QEvent::LanguageChange)
                {
                    ui->retranslateUi(this);
                }
            
                QWidget::changeEvent(event);
            }
            
            
            

            and then:

            void MainWindow::on_Traduzione_Inglese_clicked()
            {
                qApp->installTranslator(&Lingua_Italiana);
                ui->Traduzione_Italiano->setDisabled(false);
                // if(ui->info->text=="CONF. IN CARICAMENTO")
                if(ui->Info->text()=="CONF. IN CARICAMENTO")
            
                    ui->Info->setText("cONF..lOADING");
            }
            
            

            if I have a function like this:

            void MainWindow::Caricamento()
            {
                ui->Configurazione_Attuale->setText(selected_file_name);
                ui->Info->setText("CONF. CARICATA");
            }
            
            

            i can write ui->Info->setText(tr(....
            but how I can call the function in change event, because I have other functions in Caricamento

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @vale88 must I add something in qt linguist? I don't understand

            mrjjM 1 Reply Last reply
            0
            • ? A Former User

              @vale88 must I add something in qt linguist? I don't understand

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @vale88
              all text must be flagged with TR
              so
              ui->Info->setText( tr("cONF..lOADING") );

              Then you extract the texts using the tool
              use qt linguist to do the actual translation. (adding the translated text)
              for the languages you support.
              Then produced the compiled text qm and use that with
              installTranslator

              The rules for what text it will extract is talked about here
              https://doc.qt.io/qt-5/i18n-source-translation.html

              Also for any dynamically created QLabel, you must also call its setText again.

              ? 1 Reply Last reply
              2
              • mrjjM mrjj

                @vale88
                all text must be flagged with TR
                so
                ui->Info->setText( tr("cONF..lOADING") );

                Then you extract the texts using the tool
                use qt linguist to do the actual translation. (adding the translated text)
                for the languages you support.
                Then produced the compiled text qm and use that with
                installTranslator

                The rules for what text it will extract is talked about here
                https://doc.qt.io/qt-5/i18n-source-translation.html

                Also for any dynamically created QLabel, you must also call its setText again.

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @mrjj I added tr but it doesn't work, I use qt linguist but I can see only words fixed and not label dynamic, how can I do?

                mrjjM 1 Reply Last reply
                0
                • ? A Former User

                  @mrjj I added tr but it doesn't work, I use qt linguist but I can see only words fixed and not label dynamic, how can I do?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @vale88
                  well how do you set the dynamic labels ?

                  it should be something like

                  QLabel *lab= new QLabel(this)
                  lab->setText( tr("my text") );

                  ? 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @vale88
                    well how do you set the dynamic labels ?

                    it should be something like

                    QLabel *lab= new QLabel(this)
                    lab->setText( tr("my text") );

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9
                    This post is deleted!
                    mrjjM J.HilkJ 2 Replies Last reply
                    0
                    • ? A Former User

                      This post is deleted!

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @vale88
                      Hi
                      You have to find out why it does not see the texts and extracts them.

                      1 Reply Last reply
                      0
                      • ? A Former User

                        This post is deleted!

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

                        @vale88 have you run lupdate after adding the tr tags around your dynamic labels ?


                        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
                        • J.HilkJ J.Hilk

                          @vale88 have you run lupdate after adding the tr tags around your dynamic labels ?

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          @J.Hilk MY PROBLEM is: if I have some words that I write during the code:
                          example:ui->info->setText("ciao");
                          to translate ciao I have write:
                          ui->infpo->setText(tr("ciao"));
                          but IT DOESN'T work, I think because "ciao" isn't in my dictionary, how cAN i DO TO ADD?

                          mrjjM 1 Reply Last reply
                          0
                          • ? A Former User

                            @J.Hilk MY PROBLEM is: if I have some words that I write during the code:
                            example:ui->info->setText("ciao");
                            to translate ciao I have write:
                            ui->infpo->setText(tr("ciao"));
                            but IT DOESN'T work, I think because "ciao" isn't in my dictionary, how cAN i DO TO ADD?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @vale88
                            try run lupdate a few times.
                            It does work that way.

                            ? 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @vale88
                              try run lupdate a few times.
                              It does work that way.

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              @mrjj I don't know lupdate, I searched but I don't know I can use it

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #15

                                alt text
                                and you need to have in your .pro file
                                TRANSLATIONS = myApp_it.ts
                                where the names are correct.

                                ? 1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  alt text
                                  and you need to have in your .pro file
                                  TRANSLATIONS = myApp_it.ts
                                  where the names are correct.

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  @mrjj i did this,maybe must I add some word in disctionary?

                                  mrjjM 1 Reply Last reply
                                  0
                                  • ? A Former User

                                    @mrjj i did this,maybe must I add some word in disctionary?

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @vale88
                                    If it does not see the texts, im not sure later the translation will work.
                                    Also, as far as i know, you can't add manual text in qt linguist. at least i didn't see any menu for that.

                                    Pablo J. RoginaP 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @vale88
                                      If it does not see the texts, im not sure later the translation will work.
                                      Also, as far as i know, you can't add manual text in qt linguist. at least i didn't see any menu for that.

                                      Pablo J. RoginaP Offline
                                      Pablo J. RoginaP Offline
                                      Pablo J. Rogina
                                      wrote on last edited by Pablo J. Rogina
                                      #18

                                      @mrjj said in qtranslator with dynamic labels:

                                      Also, as far as i know, you can't add manual text in qt linguist. at least i didn't see any menu for that.

                                      you could edit the XML file manually, but that's not a good idea.

                                      @vale88 please review the documentation suggested previously. As a summary you should:

                                      1. Enclose all the strings you want translated with tr() method in your code
                                      2. Set property translatable checked for all the texts you want translated in the UI (when using Qt Designer)
                                      3. Have a languagueChange() method in every class you want translated on the fly (if you want the user to switch languages with the application running). The method will be called automatically in response to a new translator being installed. Pseudo-code:
                                      void YourClass::languageChange()
                                      {
                                      // all strings in UI form for this class will be retranslated here 
                                          ui->retranslateUi(this);
                                      // Use following approach for setting a translated string other than the one you set in Designer
                                          ui->label->setText(tr("set label value to a different one from what you used in UI Designer")); 
                                      }
                                      
                                      1. Add the desired language(s) to your project (.pro) file:
                                      TRANSLATIONS = yourApp_it.ts /
                                          yourApp_es.ts /
                                          yourApp_frts
                                      
                                      1. run lupdate in your project's root folder. In the example above, you'll end up with 3 files to translate (Italian, Spanish, French)
                                      2. Run linguist and open the *.ts file and provide the proper translations for the desired language(s)
                                      3. Run lrelease to convert the translated .ts file(s) into .qm files (binary format)
                                      4. In your application, install the proper translator (.qm file) for the selected language
                                      5. Enjoy!

                                      Just in case from your previous snippet:

                                      void MainWindow::on_Traduzione_Inglese_clicked()
                                      {
                                          qApp->installTranslator(&Lingua_Italiana);
                                          ui->Traduzione_Italiano->setDisabled(false);
                                          // if(ui->info->text=="CONF. IN CARICAMENTO")
                                          if(ui->Info->text()=="CONF. IN CARICAMENTO")
                                      
                                              ui->Info->setText("cONF..lOADING");
                                      });
                                      }
                                      

                                      it looks like the method is called when a button for "English Transalation" is clicked but you seem to load the Italian translator. In addition, it's a good approach to check that the translator did actually was installed properly, i.e. (pseudo-code again):

                                      bool loaded = m_translator->load(pagePrefix +'_'+ locale);
                                      if (!loaded)
                                          qWarning() << QString("can't load %1 translation").arg(locale);
                                      

                                      Upvote the answer(s) that helped you solve the issue
                                      Use "Topic Tools" button to mark your post as Solved
                                      Add screenshots via postimage.org
                                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                      ? 1 Reply Last reply
                                      4
                                      • Pablo J. RoginaP Pablo J. Rogina

                                        @mrjj said in qtranslator with dynamic labels:

                                        Also, as far as i know, you can't add manual text in qt linguist. at least i didn't see any menu for that.

                                        you could edit the XML file manually, but that's not a good idea.

                                        @vale88 please review the documentation suggested previously. As a summary you should:

                                        1. Enclose all the strings you want translated with tr() method in your code
                                        2. Set property translatable checked for all the texts you want translated in the UI (when using Qt Designer)
                                        3. Have a languagueChange() method in every class you want translated on the fly (if you want the user to switch languages with the application running). The method will be called automatically in response to a new translator being installed. Pseudo-code:
                                        void YourClass::languageChange()
                                        {
                                        // all strings in UI form for this class will be retranslated here 
                                            ui->retranslateUi(this);
                                        // Use following approach for setting a translated string other than the one you set in Designer
                                            ui->label->setText(tr("set label value to a different one from what you used in UI Designer")); 
                                        }
                                        
                                        1. Add the desired language(s) to your project (.pro) file:
                                        TRANSLATIONS = yourApp_it.ts /
                                            yourApp_es.ts /
                                            yourApp_frts
                                        
                                        1. run lupdate in your project's root folder. In the example above, you'll end up with 3 files to translate (Italian, Spanish, French)
                                        2. Run linguist and open the *.ts file and provide the proper translations for the desired language(s)
                                        3. Run lrelease to convert the translated .ts file(s) into .qm files (binary format)
                                        4. In your application, install the proper translator (.qm file) for the selected language
                                        5. Enjoy!

                                        Just in case from your previous snippet:

                                        void MainWindow::on_Traduzione_Inglese_clicked()
                                        {
                                            qApp->installTranslator(&Lingua_Italiana);
                                            ui->Traduzione_Italiano->setDisabled(false);
                                            // if(ui->info->text=="CONF. IN CARICAMENTO")
                                            if(ui->Info->text()=="CONF. IN CARICAMENTO")
                                        
                                                ui->Info->setText("cONF..lOADING");
                                        });
                                        }
                                        

                                        it looks like the method is called when a button for "English Transalation" is clicked but you seem to load the Italian translator. In addition, it's a good approach to check that the translator did actually was installed properly, i.e. (pseudo-code again):

                                        bool loaded = m_translator->load(pagePrefix +'_'+ locale);
                                        if (!loaded)
                                            qWarning() << QString("can't load %1 translation").arg(locale);
                                        
                                        ? Offline
                                        ? Offline
                                        A Former User
                                        wrote on last edited by
                                        #19

                                        @Pablo-J.-Rogina I want to translate from italian to english, so how must i write in .pro?
                                        and an other question, if I write manually,like this..

                                        void MainWindow::on_Traduzione_Inglese_clicked()
                                        {
                                            qApp->installTranslator(&Lingua_Italiana);
                                            ui->Traduzione_Italiano->setDisabled(false);
                                            // if(ui->info->text=="CONF. IN CARICAMENTO")
                                            if(ui->Info->text()=="CONF. IN CARICAMENTO")
                                        
                                                ui->Info->setText("cONF..lOADING");
                                        }
                                        
                                        

                                        in Info doesn't wtite conf loading..because installTranslaton block this I think

                                        Pablo J. RoginaP 1 Reply Last reply
                                        0
                                        • ? A Former User

                                          @Pablo-J.-Rogina I want to translate from italian to english, so how must i write in .pro?
                                          and an other question, if I write manually,like this..

                                          void MainWindow::on_Traduzione_Inglese_clicked()
                                          {
                                              qApp->installTranslator(&Lingua_Italiana);
                                              ui->Traduzione_Italiano->setDisabled(false);
                                              // if(ui->info->text=="CONF. IN CARICAMENTO")
                                              if(ui->Info->text()=="CONF. IN CARICAMENTO")
                                          
                                                  ui->Info->setText("cONF..lOADING");
                                          }
                                          
                                          

                                          in Info doesn't wtite conf loading..because installTranslaton block this I think

                                          Pablo J. RoginaP Offline
                                          Pablo J. RoginaP Offline
                                          Pablo J. Rogina
                                          wrote on last edited by
                                          #20

                                          @vale88 it looks like you're not following the suggestions, like reading the documentation...

                                          Upvote the answer(s) that helped you solve the issue
                                          Use "Topic Tools" button to mark your post as Solved
                                          Add screenshots via postimage.org
                                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                          ? 1 Reply Last reply
                                          2

                                          • Login

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