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. how to define repeated string with qtranslator?
Forum Updated to NodeBB v4.3 + New Features

how to define repeated string with qtranslator?

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 5 Posters 3.4k 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.
  • Q Offline
    Q Offline
    QtTester
    wrote on 29 Nov 2021, 12:09 last edited by QtTester 12 Jan 2021, 00:35
    #1

    HI,all.
    you may use a same string in many cpp. how to translate it for just one time?
    here is my example:

    /res
        en-US-app.qm
        en-US-dll.qm
    /app
        app.pro
    /dll
        dll.pro
    strings.h
    

    where strings.h is:

    #include <QObject>
    class Strings:public QObject
    {
        Q_OBJECT
    public:
    #define STR_HELLO Strings::tr("你好")
    #define STR_HELLO2 Strings::tr("好不好")
    };
    

    in app:

    #include "../strings.h"
    void App::pbClick()
    {
        ui->myLbl->setText(STR_HELLO);
        ui->myLbl2->setText(STR_HELLO2);
    }
    

    in dll:

    #include "../strings.h"
    void Dll::pbClick()
    {
        ui->myPb->setText(STR_HELLO);
        ui->myPb2->setText(STR_HELLO2);
    }
    

    So that dll and app can use the same string for display or comparing.
    Unfortunately , I have to translate STR_HELLO and STR_HELLO2 for two times! each projcet needed to be translated!
    Is there a better way to translate for once?
    here is the code, it can be translated correctly, but need two times to translate the same string....

    labisart.com/public/uploads/multilang.zip

    A 1 Reply Last reply 29 Nov 2021, 12:23
    0
    • Q QtTester
      29 Nov 2021, 12:09

      HI,all.
      you may use a same string in many cpp. how to translate it for just one time?
      here is my example:

      /res
          en-US-app.qm
          en-US-dll.qm
      /app
          app.pro
      /dll
          dll.pro
      strings.h
      

      where strings.h is:

      #include <QObject>
      class Strings:public QObject
      {
          Q_OBJECT
      public:
      #define STR_HELLO Strings::tr("你好")
      #define STR_HELLO2 Strings::tr("好不好")
      };
      

      in app:

      #include "../strings.h"
      void App::pbClick()
      {
          ui->myLbl->setText(STR_HELLO);
          ui->myLbl2->setText(STR_HELLO2);
      }
      

      in dll:

      #include "../strings.h"
      void Dll::pbClick()
      {
          ui->myPb->setText(STR_HELLO);
          ui->myPb2->setText(STR_HELLO2);
      }
      

      So that dll and app can use the same string for display or comparing.
      Unfortunately , I have to translate STR_HELLO and STR_HELLO2 for two times! each projcet needed to be translated!
      Is there a better way to translate for once?
      here is the code, it can be translated correctly, but need two times to translate the same string....

      labisart.com/public/uploads/multilang.zip

      A Offline
      A Offline
      artwaw
      wrote on 29 Nov 2021, 12:23 last edited by artwaw
      #2

      @QtTester Build a static QHash or QMap with the strings, a method that sets the strings where you need them, call that method in changeEvent() right after retranslateUI call. In other words you are overcomplicating this.

      Please also note that this only makes sense in the case of avoiding multiple identical strings in the UI or views constructed dynamically (my use case is usually when I need to translate headers of a model), if we are talking unique strings in the UI then that is already covered by built in routines.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 29 Nov 2021, 15:16 last edited by
        #3

        Please read the documentation on how to translate strings: https://doc.qt.io/qt-5/i18n-source-translation.html#translating-text-that-is-outside-of-a-qobject-subclass

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

        Q 1 Reply Last reply 30 Nov 2021, 03:53
        3
        • C Christian Ehrlicher
          29 Nov 2021, 15:16

          Please read the documentation on how to translate strings: https://doc.qt.io/qt-5/i18n-source-translation.html#translating-text-that-is-outside-of-a-qobject-subclass

          Q Offline
          Q Offline
          QtTester
          wrote on 30 Nov 2021, 03:53 last edited by
          #4

          @Christian-Ehrlicher
          it does not mention how to use a repeated string in multi project.
          still , we should make a stand alone project to export the string. and use them in dll1 /dll2 /dll3 /app.
          so that people donot need to translate the same strings time and time again.

          J 1 Reply Last reply 30 Nov 2021, 05:37
          0
          • Q QtTester
            30 Nov 2021, 03:53

            @Christian-Ehrlicher
            it does not mention how to use a repeated string in multi project.
            still , we should make a stand alone project to export the string. and use them in dll1 /dll2 /dll3 /app.
            so that people donot need to translate the same strings time and time again.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 30 Nov 2021, 05:37 last edited by
            #5

            @QtTester
            I don't think thats a good idea, text is very often context relevant and can in many language result in a different translation.

            And in cases where it doesn't matter, the lupdate tool provides same/similar heuristic translation, that you only have to confirm via the QLinguist (cmd + enter)


            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.

            Q 1 Reply Last reply 30 Nov 2021, 06:47
            0
            • J J.Hilk
              30 Nov 2021, 05:37

              @QtTester
              I don't think thats a good idea, text is very often context relevant and can in many language result in a different translation.

              And in cases where it doesn't matter, the lupdate tool provides same/similar heuristic translation, that you only have to confirm via the QLinguist (cmd + enter)

              Q Offline
              Q Offline
              QtTester
              wrote on 30 Nov 2021, 06:47 last edited by QtTester 12 Jan 2021, 00:32
              #6

              @J-Hilk
              Ok,here is my example:

              /res
                  en-US-app.qm
                  en-US-dll.qm
              /app
                  app.pro
              /dll
                  dll.pro
              strings.h
              

              where strings.h is:

              #include <QObject>
              class Strings:public QObject
              {
                  Q_OBJECT
              public:
              #define STR_HELLO Strings::tr("你好")
              #define STR_HELLO2 Strings::tr("好不好")
              };
              

              in app:

              #include "../strings.h"
              void App::pbClick()
              {
                  ui->myLbl->setText(STR_HELLO);
                  ui->myLbl2->setText(STR_HELLO2);
              }
              

              in dll:

              #include "../strings.h"
              void Dll::pbClick()
              {
                  ui->myPb->setText(STR_HELLO);
                  ui->myPb2->setText(STR_HELLO2);
              }
              

              So that dll and app can use the same string for display or comparing.
              Unfortunately , I have to translate STR_HELLO and STR_HELLO2 for two times! each projcet needed to be translated!
              Is there a better way to translate for once?
              here is the code, it can be translated correctly, but need two times to translate the same string....

              labisart.com/public/uploads/multilang.zip

              J 1 Reply Last reply 30 Nov 2021, 06:54
              0
              • Q QtTester
                30 Nov 2021, 06:47

                @J-Hilk
                Ok,here is my example:

                /res
                    en-US-app.qm
                    en-US-dll.qm
                /app
                    app.pro
                /dll
                    dll.pro
                strings.h
                

                where strings.h is:

                #include <QObject>
                class Strings:public QObject
                {
                    Q_OBJECT
                public:
                #define STR_HELLO Strings::tr("你好")
                #define STR_HELLO2 Strings::tr("好不好")
                };
                

                in app:

                #include "../strings.h"
                void App::pbClick()
                {
                    ui->myLbl->setText(STR_HELLO);
                    ui->myLbl2->setText(STR_HELLO2);
                }
                

                in dll:

                #include "../strings.h"
                void Dll::pbClick()
                {
                    ui->myPb->setText(STR_HELLO);
                    ui->myPb2->setText(STR_HELLO2);
                }
                

                So that dll and app can use the same string for display or comparing.
                Unfortunately , I have to translate STR_HELLO and STR_HELLO2 for two times! each projcet needed to be translated!
                Is there a better way to translate for once?
                here is the code, it can be translated correctly, but need two times to translate the same string....

                labisart.com/public/uploads/multilang.zip

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 30 Nov 2021, 06:54 last edited by
                #7

                @QtTester you can define your own context - typically this is the class name - and as long as you specify the context, Qt will use the correct translation.

                https://doc.qt.io/qt-5/qtglobal.html#QT_TRANSLATE_NOOP


                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.

                Q 1 Reply Last reply 30 Nov 2021, 07:28
                0
                • J J.Hilk
                  30 Nov 2021, 06:54

                  @QtTester you can define your own context - typically this is the class name - and as long as you specify the context, Qt will use the correct translation.

                  https://doc.qt.io/qt-5/qtglobal.html#QT_TRANSLATE_NOOP

                  Q Offline
                  Q Offline
                  QtTester
                  wrote on 30 Nov 2021, 07:28 last edited by
                  #8

                  @J-Hilk
                  Strings already is the context. the point is : I have to translate it for two each project , do the repetitive work。

                  J 1 Reply Last reply 30 Nov 2021, 07:32
                  0
                  • Q QtTester
                    30 Nov 2021, 07:28

                    @J-Hilk
                    Strings already is the context. the point is : I have to translate it for two each project , do the repetitive work。

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 30 Nov 2021, 07:32 last edited by
                    #9

                    @QtTester said in how to define repeated string with qtranslator?:

                    Strings already is the context. the point is : I have to translate it for two each project , do the repetitive work。

                    No.
                    Strings are the source text. Context is for the translation engine. And you don't have to do the repetitive work, you can shove everything with custom context into their own *qm files and load those additionally to your normal app specific qm file


                    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.

                    Q 1 Reply Last reply 30 Nov 2021, 08:10
                    1
                    • J J.Hilk
                      30 Nov 2021, 07:32

                      @QtTester said in how to define repeated string with qtranslator?:

                      Strings already is the context. the point is : I have to translate it for two each project , do the repetitive work。

                      No.
                      Strings are the source text. Context is for the translation engine. And you don't have to do the repetitive work, you can shove everything with custom context into their own *qm files and load those additionally to your normal app specific qm file

                      Q Offline
                      Q Offline
                      QtTester
                      wrote on 30 Nov 2021, 08:10 last edited by QtTester
                      #10

                      @J-Hilk
                      I have try

                      #define PRODUCT_NAME QT_TRANSLATE_NOOP("QObject", "我的产品")
                      

                      in string.h ,but still need to do repetitive work.
                      Could you show me the code? still don't get it :-(

                      J 1 Reply Last reply 30 Nov 2021, 09:38
                      0
                      • Q QtTester
                        30 Nov 2021, 08:10

                        @J-Hilk
                        I have try

                        #define PRODUCT_NAME QT_TRANSLATE_NOOP("QObject", "我的产品")
                        

                        in string.h ,but still need to do repetitive work.
                        Could you show me the code? still don't get it :-(

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 30 Nov 2021, 09:38 last edited by
                        #11

                        @QtTester
                        Sorry I haven't the time right now, for an explicit example.

                        Are you sure this works at all? Because I would say that any call of lupdate does not expand /replace your defines before parsing the files.

                        I think you would need an actual "normal " entry of QT_TRANSLATE_NOOP("QObject", "我的产品") somewhere too. Btw. QObject is a horrible choice for context.


                        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.

                        Q C 2 Replies Last reply 30 Nov 2021, 10:13
                        0
                        • J J.Hilk
                          30 Nov 2021, 09:38

                          @QtTester
                          Sorry I haven't the time right now, for an explicit example.

                          Are you sure this works at all? Because I would say that any call of lupdate does not expand /replace your defines before parsing the files.

                          I think you would need an actual "normal " entry of QT_TRANSLATE_NOOP("QObject", "我的产品") somewhere too. Btw. QObject is a horrible choice for context.

                          Q Offline
                          Q Offline
                          QtTester
                          wrote on 30 Nov 2021, 10:13 last edited by
                          #12

                          @J-Hilk
                          Could you download my example and make a few change ? :-)

                          labisart.com/public/uploads/multilang.zip

                          1 Reply Last reply
                          0
                          • J J.Hilk
                            30 Nov 2021, 09:38

                            @QtTester
                            Sorry I haven't the time right now, for an explicit example.

                            Are you sure this works at all? Because I would say that any call of lupdate does not expand /replace your defines before parsing the files.

                            I think you would need an actual "normal " entry of QT_TRANSLATE_NOOP("QObject", "我的产品") somewhere too. Btw. QObject is a horrible choice for context.

                            C Offline
                            C Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 30 Nov 2021, 16:48 last edited by
                            #13

                            @J-Hilk said in how to define repeated string with qtranslator?:

                            Because I would say that any call of lupdate does not expand /replace your defines before parsing the files.

                            That's correct. lupdate will not grab such mess :)

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

                            Q 1 Reply Last reply 1 Dec 2021, 00:55
                            0
                            • JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on 30 Nov 2021, 19:17 last edited by JoeCFD
                              #14

                              @QtTester said in how to define repeated string with qtranslator?:

                              #define STR_HELLO QObject::tr("hello")

                              #define STR_HELLO QObject::tr("hello") is c style code <===avoid using it in C++ code because it is not needed.

                              Try this:

                              const QString STR_HELLO( QObject::tr("hello") );
                              if ( nullptr != ui && nullptr !=  ui->label ) {
                                   ui->label->setText(STR_HELLO);  <===this should work.
                              }
                              

                              Others are right you need proper translation code without hardcoding this.

                              1 Reply Last reply
                              0
                              • C Christian Ehrlicher
                                30 Nov 2021, 16:48

                                @J-Hilk said in how to define repeated string with qtranslator?:

                                Because I would say that any call of lupdate does not expand /replace your defines before parsing the files.

                                That's correct. lupdate will not grab such mess :)

                                Q Offline
                                Q Offline
                                QtTester
                                wrote on 1 Dec 2021, 00:55 last edited by QtTester 12 Jan 2021, 01:21
                                #15

                                @Christian-Ehrlicher said in how to define repeated string with qtranslator?:

                                That's correct. lupdate will not grab such mess :)

                                In this case ,lupdate is stupid , it grab STR_HELLO and STR_HELLO2 all to 'en-us-dll.ts' and 'en-us-app.ts'! and we need the repetitive work! can you show me the right code?

                                @JoeCFD said in how to define repeated string with qtranslator?:

                                @QtTester said in how to define repeated string with qtranslator?:
                                Try this:

                                const QString STR_HELLO( QObject::tr("hello") );
                                if ( nullptr != ui && nullptr !=  ui->label ) {
                                     ui->label->setText(STR_HELLO);  <===this should work.
                                }
                                

                                still need to input two times in QLinguist, and not work for language change ,because it 's const.

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 1 Dec 2021, 05:26 last edited by
                                  #16

                                  @QtTester said in how to define repeated string with qtranslator?:

                                  and not work for language change ,because it 's const.

                                  This is wrong completely wrong...

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

                                  Q 1 Reply Last reply 1 Dec 2021, 05:45
                                  0
                                  • C Christian Ehrlicher
                                    1 Dec 2021, 05:26

                                    @QtTester said in how to define repeated string with qtranslator?:

                                    and not work for language change ,because it 's const.

                                    This is wrong completely wrong...

                                    Q Offline
                                    Q Offline
                                    QtTester
                                    wrote on 1 Dec 2021, 05:45 last edited by QtTester 12 Jan 2021, 05:45
                                    #17

                                    @Christian-Ehrlicher
                                    Donot konwn why ,but I try , it cannot be translated, not just the repetitive issue. const is the reason?

                                    C J 2 Replies Last reply 1 Dec 2021, 06:09
                                    0
                                    • Q QtTester
                                      1 Dec 2021, 05:45

                                      @Christian-Ehrlicher
                                      Donot konwn why ,but I try , it cannot be translated, not just the repetitive issue. const is the reason?

                                      C Offline
                                      C Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 1 Dec 2021, 06:09 last edited by
                                      #18

                                      @QtTester said in how to define repeated string with qtranslator?:

                                      const is the reason?

                                      no, please learn c++

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

                                      Q 1 Reply Last reply 1 Dec 2021, 07:20
                                      0
                                      • Q QtTester
                                        1 Dec 2021, 05:45

                                        @Christian-Ehrlicher
                                        Donot konwn why ,but I try , it cannot be translated, not just the repetitive issue. const is the reason?

                                        J Offline
                                        J Offline
                                        J.Hilk
                                        Moderators
                                        wrote on 1 Dec 2021, 06:27 last edited by
                                        #19

                                        @QtTester said in how to define repeated string with qtranslator?:

                                        not just the repetitive issue. const is the reason?

                                        no, every string you set by hand (not via QDesigner) has to be set AGAIN after loading the translation file!


                                        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
                                        • C Christian Ehrlicher
                                          1 Dec 2021, 06:09

                                          @QtTester said in how to define repeated string with qtranslator?:

                                          const is the reason?

                                          no, please learn c++

                                          Q Offline
                                          Q Offline
                                          QtTester
                                          wrote on 1 Dec 2021, 07:20 last edited by QtTester 12 Jan 2021, 07:32
                                          #20

                                          @Christian-Ehrlicher
                                          @J-Hilk
                                          Since you donot have time to check the code, I paste below:

                                          #include <QObject>
                                          #include <QString>
                                          
                                          // cn encode
                                          #ifdef _MSC_VER
                                          #pragma execution_character_set("utf-8")
                                          #endif
                                          
                                          class Strings:public QObject
                                          {
                                              Q_OBJECT
                                          public:
                                          #define STR_HELLO1 Strings::tr("你好")
                                          #define STR_HELLO2 Strings::tr("好不好")
                                          };
                                          #define MY_CONTEXT "Strings"
                                          #define PRODUCT_NAME QT_TRANSLATE_NOOP(MY_CONTEXT, "我的产品")
                                          
                                          const QString STR_HELLO3( QObject::tr("你好3") );
                                          
                                          #include "mainwindow.h"
                                          #include "ui_mainwindow.h"
                                          
                                          #include "../strings.h"
                                          #include "../dll/dll.h"
                                          
                                          #ifdef _MSC_VER
                                          #pragma execution_character_set("utf-8")
                                          #endif
                                          MainWindow::~MainWindow()
                                          {
                                              delete ui;
                                          }
                                          MainWindow::MainWindow(QWidget *parent)
                                              : QMainWindow(parent)
                                              , ui(new Ui::MainWindow)
                                          {
                                              ui->setupUi(this);
                                              initUi();
                                          }
                                          void MainWindow::initUi()
                                          {
                                              setWindowTitle(qApp->translate(MY_CONTEXT,PRODUCT_NAME));
                                              ui->label->setText(STR_HELLO1);
                                              ui->label_2->setText(STR_HELLO3);
                                          }
                                          void MainWindow::changeEvent(QEvent *e)
                                          {
                                              if(e->type() == QEvent::LanguageChange){
                                                  ui->retranslateUi(this);
                                                  initUi();
                                                  qDebug("main:lang change event");
                                              }
                                          }
                                          void MainWindow::on_pbCn_clicked()
                                          {
                                              qApp->removeTranslator(&m_transApp);
                                              qApp->removeTranslator(&m_transDll);
                                          }
                                          void MainWindow::on_pbEn_clicked()
                                          {
                                              qApp->removeTranslator(&m_transApp);
                                              if(m_transApp.load(":/en-US-app.qm")){
                                                  qApp->installTranslator(&m_transApp);
                                              }
                                              qApp->removeTranslator(&m_transDll);
                                              if(m_transDll.load(":/en-US-dll.qm")){
                                                  qApp->installTranslator(&m_transDll);
                                              }
                                          }
                                          void MainWindow::on_pbPop_clicked()
                                          {
                                              static Dll *d = new Dll();
                                              d->show();
                                          }
                                          
                                          
                                          #include "dll.h"
                                          #include "../strings.h"
                                          
                                          #include <QApplication>
                                          #include <QMessageBox>
                                          #include <QPushButton>
                                          
                                          // 
                                          #ifdef _MSC_VER
                                          #pragma execution_character_set("utf-8")
                                          #endif
                                          Dll::Dll(QWidget *p):QWidget(p)
                                          {
                                              QMessageBox::warning(nullptr,"",STR_HELLO2);
                                          
                                              m_pb = new QPushButton(this);
                                              m_pb->move(0,0);
                                              initUi();
                                          
                                              resize(QSize(200,200));
                                          }
                                          void Dll::initUi()
                                          {
                                              setWindowTitle(qApp->translate(MY_CONTEXT,PRODUCT_NAME));
                                              m_pb->setText(STR_HELLO3);
                                          }
                                          void Dll::changeEvent(QEvent *e)
                                          {
                                              if(e->type() == QEvent::LanguageChange){
                                                  initUi();
                                                  qDebug("dll:lang change event");
                                              }
                                          }
                                          
                                          

                                          here is the result,default language:
                                          微信截图_20211201152452.png

                                          after select english, STR_HELLO3 not change in app ,and dll change nothing at all, although the event is triggered.:
                                          微信截图_20211201152507.png
                                          微信截图_20211201152324.png

                                          1 Reply Last reply
                                          0

                                          1/26

                                          29 Nov 2021, 12:09

                                          • Login

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