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

    @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.

    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      • Christian EhrlicherC Christian Ehrlicher

        @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 last edited by QtTester
        #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
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          0
          • Christian EhrlicherC Christian Ehrlicher

            @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 last edited by QtTester
            #17

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

            Christian EhrlicherC J.HilkJ 2 Replies Last reply
            0
            • Q QtTester

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

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              0
              • Q QtTester

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

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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
                • Christian EhrlicherC Christian Ehrlicher

                  @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 last edited by QtTester
                  #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
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    It's because the context is wrong - it's not QObject but your class name ('Dll' in your example).
                    Still don't understand the reason behind all this. If you want some kind of translation library you should use a proper class with a get() function which takes an enum which represents your string

                    class Translator : public QObject
                    {
                      Q_OBJECT
                    public:
                      enum class Id
                      {
                       Hello1,
                       Hello2,
                      }
                      QString translateMe(Id id) const;
                    }
                    
                    ...
                    
                    QString Translator::translateMe(Id id) const
                    {
                      switch (id) {
                        case Id::Hello1: return tr("Hello1");
                        case Id::Hello2: return tr("Hello2");
                       }
                    }
                    

                    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
                    3
                    • Christian EhrlicherC Christian Ehrlicher

                      It's because the context is wrong - it's not QObject but your class name ('Dll' in your example).
                      Still don't understand the reason behind all this. If you want some kind of translation library you should use a proper class with a get() function which takes an enum which represents your string

                      class Translator : public QObject
                      {
                        Q_OBJECT
                      public:
                        enum class Id
                        {
                         Hello1,
                         Hello2,
                        }
                        QString translateMe(Id id) const;
                      }
                      
                      ...
                      
                      QString Translator::translateMe(Id id) const
                      {
                        switch (id) {
                          case Id::Hello1: return tr("Hello1");
                          case Id::Hello2: return tr("Hello2");
                         }
                      }
                      
                      Q Offline
                      Q Offline
                      QtTester
                      wrote on last edited by QtTester
                      #22

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

                      class Translator : public QObject
                      {
                      Q_OBJECT
                      public:
                      enum class Id
                      {
                      Hello1,
                      Hello2,
                      }
                      QString translateMe(Id id) const;
                      }

                      QString Translator::translateMe(Id id) const
                      {
                      switch (id) {
                      case Id::Hello1: return tr("Hello1");
                      case Id::Hello2: return tr("Hello2");
                      }
                      }

                      That is what I first said: write a common、 stand alone project and call it's API.

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • Q QtTester

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

                        class Translator : public QObject
                        {
                        Q_OBJECT
                        public:
                        enum class Id
                        {
                        Hello1,
                        Hello2,
                        }
                        QString translateMe(Id id) const;
                        }

                        QString Translator::translateMe(Id id) const
                        {
                        switch (id) {
                        case Id::Hello1: return tr("Hello1");
                        case Id::Hello2: return tr("Hello2");
                        }
                        }

                        That is what I first said: write a common、 stand alone project and call it's API.

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

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

                        That is what I first said: write a common、 stand alone project and call it's API.

                        So what's the actual problem then?

                        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
                        0
                        • Christian EhrlicherC Christian Ehrlicher

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

                          That is what I first said: write a common、 stand alone project and call it's API.

                          So what's the actual problem then?

                          Q Offline
                          Q Offline
                          QtTester
                          wrote on last edited by
                          #24

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

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

                          That is what I first said: write a common、 stand alone project and call it's API.

                          So what's the actual problem then?

                          guys said it is overcomplicate or no need to do this , so i try to find another native way .after a long circle way , you give me the same solution. ha-ha-ha

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

                            I would not use this solution but translate it more than once - it's easier esp. since linguist auto-translates your strings when it already find the exact same match.

                            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
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              I would not use this solution but translate it more than once - it's easier esp. since linguist auto-translates your strings when it already find the exact same match.

                              Q Offline
                              Q Offline
                              QtTester
                              wrote on last edited by
                              #26

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

                              e

                              How do you solve this situation:

                              Project1
                                 dll1
                                 dll2
                                 app
                              
                              Project2
                                 dll3
                                 dll4
                                 app
                              

                              Project1 and Project2 or other projects need to use the same string. many many string(like ok, yes, no ...)
                              if you translate each BIg Project, you donot need to do the repetitive work?
                              and you cannot put dll1.ts to Project2, you mean linguist know it's already translated?

                              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