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. Qt dynamic translation on a serialized enum key in a comboBox
Forum Updated to NodeBB v4.3 + New Features

Qt dynamic translation on a serialized enum key in a comboBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 867 Views 3 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.
  • H Offline
    H Offline
    Habibie
    wrote on last edited by
    #1

    I am trying to lean on how to internationalize an existing code with a dynamic translation and am stuck at dynamically translating enum keys. So, I started doing some searches through Google and narrowed down to this answer (which is a rather 10 years old post). To show what I have done, I took a working demo code from chapter 11 of Cross-Platform Development with Qt 6 and Modern C++ book. My code can be downloaded from here. As you can see, in my CustomWidget::init() method, I added some routines (see below) to update the comboBox with a QStringList object filled with translated enum keys. The program compiles just fine, but the comboBox still shows the original (not translated) text when a language is changed. Did I do something wrong? I don't know if this makes any difference: The original post uses QObject::staticQtMetaObject while my code uses staticMetaObject.

    void CustomWidget::init()
    {
        int counter = metaEnumColor.keyCount();
        qDebug() << "[" << __func__ << "]: counter =" << counter;
    
        for(int i=0; i<counter; i++)
        {
            QString colorKey = tr(metaEnumColor.key(i));
            enumColorKeys << colorKey;
            qDebug() << "[" << __func__ << "]:" << colorKey << "color added to enum";
        }
    
        ui->comboBoxColor->addItems(enumColorKeys);
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • H Habibie

      I am trying to lean on how to internationalize an existing code with a dynamic translation and am stuck at dynamically translating enum keys. So, I started doing some searches through Google and narrowed down to this answer (which is a rather 10 years old post). To show what I have done, I took a working demo code from chapter 11 of Cross-Platform Development with Qt 6 and Modern C++ book. My code can be downloaded from here. As you can see, in my CustomWidget::init() method, I added some routines (see below) to update the comboBox with a QStringList object filled with translated enum keys. The program compiles just fine, but the comboBox still shows the original (not translated) text when a language is changed. Did I do something wrong? I don't know if this makes any difference: The original post uses QObject::staticQtMetaObject while my code uses staticMetaObject.

      void CustomWidget::init()
      {
          int counter = metaEnumColor.keyCount();
          qDebug() << "[" << __func__ << "]: counter =" << counter;
      
          for(int i=0; i<counter; i++)
          {
              QString colorKey = tr(metaEnumColor.key(i));
              enumColorKeys << colorKey;
              qDebug() << "[" << __func__ << "]:" << colorKey << "color added to enum";
          }
      
          ui->comboBoxColor->addItems(enumColorKeys);
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

      Did I do something wrong?

      You do not translate anything as far as I can see. You simply convert enum value names into strings.
      enum {
      First; // Will be translated into "First"
      }

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

        Did I do something wrong?

        You do not translate anything as far as I can see. You simply convert enum value names into strings.
        enum {
        First; // Will be translated into "First"
        }

        H Offline
        H Offline
        Habibie
        wrote on last edited by
        #3

        @jsulm said in Qt dynamic translation on a serialized enum key in a comboBox:

        @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

        Did I do something wrong?

        You do not translate anything as far as I can see. You simply convert enum value names into strings.
        enum {
        First; // Will be translated into "First"
        }

        I merely followed exactly what the original poster claimed the problem solved about 10 years ago by using the tr() function to build the list of values for the enumeration and then translated that value in the file. <. ts> file. Isn't that's exactly what I did? If so, what exactly did I do wrong? The following is the original code:

        QMetaEnum metaEnum = QObject::staticQtMetaObject.enumerator(index);
        QStringList enumNames;
        
        for (int i = 0; i < metaEnum.keyCount(); i++)
        {
             enumNames << tr(metaEnum.key(i));
        }
        

        and below is my code:

        void CustomWidget::init()
        {
            int counter = metaEnumColor.keyCount();
            qDebug() << "[" << __func__ << "]: counter =" << counter;
        
            for(int i=0; i<counter; i++)
            {
                QString colorKey = tr(metaEnumColor.key(i));
                enumColorKeys << colorKey;
                qDebug() << "[" << __func__ << "]:" << colorKey << "color added to enum";
            }
        
            ui->comboBoxColor->addItems(enumColorKeys);
        }
        
        
        jsulmJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi

          From your code we don't know if you installed a translator, when you did it, if you created the corresponding translation files nor if loaded them properly.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          H 1 Reply Last reply
          0
          • H Habibie

            @jsulm said in Qt dynamic translation on a serialized enum key in a comboBox:

            @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

            Did I do something wrong?

            You do not translate anything as far as I can see. You simply convert enum value names into strings.
            enum {
            First; // Will be translated into "First"
            }

            I merely followed exactly what the original poster claimed the problem solved about 10 years ago by using the tr() function to build the list of values for the enumeration and then translated that value in the file. <. ts> file. Isn't that's exactly what I did? If so, what exactly did I do wrong? The following is the original code:

            QMetaEnum metaEnum = QObject::staticQtMetaObject.enumerator(index);
            QStringList enumNames;
            
            for (int i = 0; i < metaEnum.keyCount(); i++)
            {
                 enumNames << tr(metaEnum.key(i));
            }
            

            and below is my code:

            void CustomWidget::init()
            {
                int counter = metaEnumColor.keyCount();
                qDebug() << "[" << __func__ << "]: counter =" << counter;
            
                for(int i=0; i<counter; i++)
                {
                    QString colorKey = tr(metaEnumColor.key(i));
                    enumColorKeys << colorKey;
                    qDebug() << "[" << __func__ << "]:" << colorKey << "color added to enum";
                }
            
                ui->comboBoxColor->addItems(enumColorKeys);
            }
            
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @Habibie You're right, somehow I oversaw the tr().

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi

              From your code we don't know if you installed a translator, when you did it, if you created the corresponding translation files nor if loaded them properly.

              H Offline
              H Offline
              Habibie
              wrote on last edited by
              #6

              @SGaist said in Qt dynamic translation on a serialized enum key in a comboBox:

              Hi

              From your code we don't know if you installed a translator, when you did it, if you created the corresponding translation files nor if loaded them properly.

              Perhaps, I wasn't clear enough; however, if you carefully read my OP above, I clearly stated that I took a working demo code from chapter 11 of Cross-Platform Development with Qt 6 and Modern C++ book and all I did was to add a CustomWidget::init() method to update the comboBox with a QStringList object filled with translated enum keys. The original working code still works fine in its translation (must have the translator properly installed) while the translation ONLY fails in the comboBox.

              Pablo J. RoginaP 1 Reply Last reply
              0
              • H Habibie

                @SGaist said in Qt dynamic translation on a serialized enum key in a comboBox:

                Hi

                From your code we don't know if you installed a translator, when you did it, if you created the corresponding translation files nor if loaded them properly.

                Perhaps, I wasn't clear enough; however, if you carefully read my OP above, I clearly stated that I took a working demo code from chapter 11 of Cross-Platform Development with Qt 6 and Modern C++ book and all I did was to add a CustomWidget::init() method to update the comboBox with a QStringList object filled with translated enum keys. The original working code still works fine in its translation (must have the translator properly installed) while the translation ONLY fails in the comboBox.

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

                @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                the translation ONLY fails in the comboBox

                Obviously, because you haven't provided any translation for the enum keys values as strings!

                From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
                Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and Rojo

                The problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:

                tr(metaEnumColor.key(i))
                

                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

                H 1 Reply Last reply
                2
                • Pablo J. RoginaP Pablo J. Rogina

                  @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                  the translation ONLY fails in the comboBox

                  Obviously, because you haven't provided any translation for the enum keys values as strings!

                  From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
                  Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and Rojo

                  The problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:

                  tr(metaEnumColor.key(i))
                  
                  H Offline
                  H Offline
                  Habibie
                  wrote on last edited by
                  #8

                  @Pablo-J-Rogina said in Qt dynamic translation on a serialized enum key in a comboBox:

                  @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                  the translation ONLY fails in the comboBox

                  Obviously, because you haven't provided any translation for the enum keys values as strings!

                  From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
                  Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and Rojo

                  The problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:

                  tr(metaEnumColor.key(i))
                  

                  When you said the translation files (.ts), did you mean the file generated by lupdate utility? If so, I noticed, the lupdate utility does not pickup all the enum keys and I reckon that's the problem. For instance, after I made the necessary changes to my code and save it, I executed the lupdate utility from within the qtcreator and I noticed lupdate utility reported with a 0 new. However, according this rather 10 years old post, it works and that's what I did in my code. This means there is something wrong with my code and I just couldn't find it.

                  Pablo J. RoginaP Christian EhrlicherC 2 Replies Last reply
                  0
                  • H Habibie

                    @Pablo-J-Rogina said in Qt dynamic translation on a serialized enum key in a comboBox:

                    @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                    the translation ONLY fails in the comboBox

                    Obviously, because you haven't provided any translation for the enum keys values as strings!

                    From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
                    Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and Rojo

                    The problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:

                    tr(metaEnumColor.key(i))
                    

                    When you said the translation files (.ts), did you mean the file generated by lupdate utility? If so, I noticed, the lupdate utility does not pickup all the enum keys and I reckon that's the problem. For instance, after I made the necessary changes to my code and save it, I executed the lupdate utility from within the qtcreator and I noticed lupdate utility reported with a 0 new. However, according this rather 10 years old post, it works and that's what I did in my code. This means there is something wrong with my code and I just couldn't find it.

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

                    @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                    according this rather 10 years old post

                    From that post, I'll extract this phrase from OP:

                    And I tranlate that value in my file. <. ts>

                    given that it was never stated that the lupdate tool was responsible for doing that, my bet is that the enum keys were manually added to the .ts file(s) and then translated as usual.

                    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
                    • H Habibie

                      @Pablo-J-Rogina said in Qt dynamic translation on a serialized enum key in a comboBox:

                      @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                      the translation ONLY fails in the comboBox

                      Obviously, because you haven't provided any translation for the enum keys values as strings!

                      From your code, and taking the Spanish translation file WidgetTranslationDemo_es_ES.ts for instance.
                      Where are there any entries for Blue, Green and Red? much less their translations as Azul, Verde and Rojo

                      The problem with your use case is that the lupate tool (used to extract literal strings from tr() calls into the translation files (.ts) won't work because of not having literals at all to extract!:

                      tr(metaEnumColor.key(i))
                      

                      When you said the translation files (.ts), did you mean the file generated by lupdate utility? If so, I noticed, the lupdate utility does not pickup all the enum keys and I reckon that's the problem. For instance, after I made the necessary changes to my code and save it, I executed the lupdate utility from within the qtcreator and I noticed lupdate utility reported with a 0 new. However, according this rather 10 years old post, it works and that's what I did in my code. This means there is something wrong with my code and I just couldn't find it.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Habibie said in Qt dynamic translation on a serialized enum key in a comboBox:

                      If so, I noticed, the lupdate utility does not pickup all the enum keys and I reckon that's the problem.

                      Because you do not properly define them - see QT_TRANSLATE_NOOP documentation.

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

                      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