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 11.7k 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.
  • 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
              • Pablo J. RoginaP Pablo J. Rogina

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

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

                @Pablo-J.-Rogina yes I read but if I must translate from italian to english,I must write.
                TRANSLATIONS = yourApp_en.ts ?

                mrjjM 1 Reply Last reply
                0
                • ? A Former User

                  @Pablo-J.-Rogina yes I read but if I must translate from italian to english,I must write.
                  TRANSLATIONS = yourApp_en.ts ?

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

                  @vale88
                  Hi
                  The ts files can be named anything.
                  it is just a normal convention to name them
                  appname_languagecode.ts
                  as then the filename can be constructed based on locale
                  But yes, your English language could be
                  TRANSLATIONS = yourApp_en.ts
                  and you simply used that name to load it.

                  ? 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @vale88
                    Hi
                    The ts files can be named anything.
                    it is just a normal convention to name them
                    appname_languagecode.ts
                    as then the filename can be constructed based on locale
                    But yes, your English language could be
                    TRANSLATIONS = yourApp_en.ts
                    and you simply used that name to load it.

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

                    @mrjj ok, so with this I call a file ts that I must build? it's not automatic..sorry but i read documentation but i don't find a example clear

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

                      @mrjj ok, so with this I call a file ts that I must build? it's not automatic..sorry but i read documentation but i don't find a example clear

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

                      @vale88 said in qtranslator with dynamic labels:

                      @mrjj ok, so with this I call a file ts that I must build? it's not automatic..sorry but i read documentation but i don't find a example clear

                      than you didn‘t read carefully enough the docs even have an example
                      https://doc.qt.io/qt-5/qtlinguist-hellotr-example.html

                      a compileable one.


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

                        @vale88 said in qtranslator with dynamic labels:

                        @mrjj ok, so with this I call a file ts that I must build? it's not automatic..sorry but i read documentation but i don't find a example clear

                        than you didn‘t read carefully enough the docs even have an example
                        https://doc.qt.io/qt-5/qtlinguist-hellotr-example.html

                        a compileable one.

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

                        @J.Hilk yes, I read..
                        Tries to load a file called hellotr_la.qm (the .qm file extension is implicit) that contains Latin translations for the source texts used in the program. No error will occur if the file is not found.

                        but I would like see an exaple of file ts to see how I must write it

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

                          Hi
                          No, the translation is not automatic.
                          You use lupdate to extract the texts to the TS
                          Then qt linguist to open and help translate
                          then qt linguist can compile the ts to qm
                          and you load the qm version to translate the app.

                          But try to do the sample @J-Hilk links.
                          Its very basic and shows the procedure very well.

                          ? 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            Hi
                            No, the translation is not automatic.
                            You use lupdate to extract the texts to the TS
                            Then qt linguist to open and help translate
                            then qt linguist can compile the ts to qm
                            and you load the qm version to translate the app.

                            But try to do the sample @J-Hilk links.
                            Its very basic and shows the procedure very well.

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

                            @mrjj ok, I found a tutorial, tomorrow I try, thanks anyway

                            mrjjM 1 Reply Last reply
                            0
                            • ? A Former User

                              @mrjj ok, I found a tutorial, tomorrow I try, thanks anyway

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

                              @vale88
                              Hi
                              Just a note.

                              • but I would like see an exaple of file ts to see how I must write it

                              You don't write the ts file your self. that part is from extracting the texts with the tool

                              ? 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                @vale88
                                Hi
                                Just a note.

                                • but I would like see an exaple of file ts to see how I must write it

                                You don't write the ts file your self. that part is from extracting the texts with the tool

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

                                @mrjj I found an example in youTube, without see I didn't understand, I solved, I used prompt di comandi, thanks anyway

                                mrjjM Pablo J. RoginaP 2 Replies Last reply
                                0
                                • ? A Former User

                                  @mrjj I found an example in youTube, without see I didn't understand, I solved, I used prompt di comandi, thanks anyway

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

                                  @vale88
                                  Ok. super :)

                                  ? 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @vale88
                                    Ok. super :)

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

                                    @mrjj only a thing..if I change something in my project, must I recreated my file ts? or I can update it

                                    mrjjM 1 Reply Last reply
                                    0
                                    • ? A Former User

                                      @mrjj only a thing..if I change something in my project, must I recreated my file ts? or I can update it

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

                                      @vale88
                                      You can update it.
                                      Just like how you created it first time.
                                      It will then add new texts to the file.

                                      1 Reply Last reply
                                      1
                                      • ? A Former User

                                        @mrjj I found an example in youTube, without see I didn't understand, I solved, I used prompt di comandi, thanks anyway

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

                                        @vale88 said in qtranslator with dynamic labels:

                                        I found an example in youTube

                                        It'd be great you can share the link so other users in your same condition could benefit.

                                        In addition, since your issue seems solved, please don't forget to update your post. Thanks.

                                        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
                                        1
                                        • Pablo J. RoginaP Pablo J. Rogina

                                          @vale88 said in qtranslator with dynamic labels:

                                          I found an example in youTube

                                          It'd be great you can share the link so other users in your same condition could benefit.

                                          In addition, since your issue seems solved, please don't forget to update your post. Thanks.

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

                                          @Pablo-J.-Rogina https://www.youtube.com/watch?v=In5QH_ZrDJE
                                          but I noticed that if I push in button english then labels are written in english because I put tr, but the labels just written in Italian aren't changed in english if I click button after...is it normal?

                                          mrjjM Pablo J. RoginaP 2 Replies 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