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. [Solved] Get translated string without switching the language
Forum Updated to NodeBB v4.3 + New Features

[Solved] Get translated string without switching the language

Scheduled Pinned Locked Moved General and Desktop
qt 4.8translationlanguagelanguagechange
13 Posts 3 Posters 7.1k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 28 Jul 2015, 22:14 last edited by
    #4

    Wouldn't QTranslator and it's translate function do the thing ?

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

    K 1 Reply Last reply 29 Jul 2015, 12:33
    0
    • S SGaist
      28 Jul 2015, 22:14

      Wouldn't QTranslator and it's translate function do the thing ?

      K Offline
      K Offline
      kumararajas
      wrote on 29 Jul 2015, 12:33 last edited by kumararajas
      #5

      @SGaist Sam, not really. Because,

      QString QTranslator::translate(const char * context, const char * sourceText, const char * disambiguation = 0, int n = -1) const
      

      Takes sourceText, but it does not take the language to which the string to be translated.

      Meaning, QTranslator translates the sourceText to the current language, not to the preferred language.

      Am I right?

      --Kumar

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 Jul 2015, 13:15 last edited by
        #6

        I haven't tested it but AFAIK you can load the translator with the translation file you want then call this method. It should only return the translation for the file you loaded.

        You can use several translator in your application, but you always have one per translation file

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

        K 1 Reply Last reply 29 Jul 2015, 14:46
        0
        • S SGaist
          29 Jul 2015, 13:15

          I haven't tested it but AFAIK you can load the translator with the translation file you want then call this method. It should only return the translation for the file you loaded.

          You can use several translator in your application, but you always have one per translation file

          K Offline
          K Offline
          kumararajas
          wrote on 29 Jul 2015, 14:46 last edited by
          #7

          @SGaist That's interesting.

          If I relate correctly, if I have this kind of snippet, should do the job, right?

          
          QTranslator m_localTranslator;
          
          QString translateThis(const char * context, const char * sourceText, const char * languageName)
          {
              if (languageName == "French")
              {
                  m_localTranslator.load("french.ts");
              }
              else if (languageName == "German")
              {
                  m_localTranslator.load("german.ts");
              }
              else if (languageName == "Tamil")
              {
                  m_localTranslator.load("tamil.ts");
              }
              else
              {
              }
              
              QString desText = m_localTranslator.translate(context, sourceText);
              
              return desText;
          }
          

          Without calling installTranslator will be able to get the translations right?

          Thanks,
          Kumara

          --Kumar

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on 29 Jul 2015, 16:18 last edited by mcosta
            #8

            Hi,

            for performance reason I suggest to use different translators (one per language) and select the one to use in you function.

            QMap<QString, QTranslator> m_traslatorMap;
            // For each language insert a translator in the map
            ....
            m_traslatorMap[languageName].translate(context, sourceText);
            

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            K 1 Reply Last reply 29 Jul 2015, 17:51
            0
            • M mcosta
              29 Jul 2015, 16:18

              Hi,

              for performance reason I suggest to use different translators (one per language) and select the one to use in you function.

              QMap<QString, QTranslator> m_traslatorMap;
              // For each language insert a translator in the map
              ....
              m_traslatorMap[languageName].translate(context, sourceText);
              
              K Offline
              K Offline
              kumararajas
              wrote on 29 Jul 2015, 17:51 last edited by kumararajas
              #9

              @mcosta Not sure where the problem is:

              <message>
              <location filename="formdata.cpp" line="19"/>
              <location filename="formdata.cpp" line="92"/>
              <location filename="formdata.cpp" line="106"/>
              <location filename="formdata.cpp" line="120"/>
              <location filename="formdata.cpp" line="137"/>
              <location filename="formdata.cpp" line="151"/>
              <location filename="formdata.cpp" line="168"/>
              <location filename="formdata.cpp" line="185"/>
              <source>Language</source>
              <translation>langue</translation>
              </message>

              m_localTranslator = new QTranslator(this);
              m_localTranslator->load("../FormData/french");
              QString test = m_localTranslator->translate("", "Language");
              qDebug() << test;
              

              Application Output shows nothing ""

              Should I need to call installTranslator or it should be translating even without that?

              --Kumar

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 31 Jul 2015, 20:05 last edited by
                #10

                You need to pass the class name where the corresponding tr can be found, no need to install the translator.

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

                K 1 Reply Last reply 4 Aug 2015, 20:29
                0
                • S SGaist
                  31 Jul 2015, 20:05

                  You need to pass the class name where the corresponding tr can be found, no need to install the translator.

                  K Offline
                  K Offline
                  kumararajas
                  wrote on 4 Aug 2015, 20:29 last edited by kumararajas 8 Apr 2015, 20:32
                  #11

                  @SGaist Oh ya! That's the mistake in my code.

                  <context>
                     <name>MyBooh</name>
                      <message>
                          <source>Formula</source>
                          <translation>Formule</translation>
                      </message>
                  </context>
                  

                  In the above sample .ts file, I had context name. So the translations where closely bounded with the name "MyBooh".

                  So in the code, I should be doing something like

                  QString test = m_localTranslator->translate("MyBooh", "Formula");
                  

                  Having said that, I do not want to bind the translations particular to the context.

                  So I have removed the <name> tag.

                  <context>
                      <message>
                          <source>Formula</source>
                          <translation>Formule</translation>
                      </message>
                  </context>
                  

                  With this, I was able to translate in this way:

                  QString test = m_localTranslator->translate("", "Formula");
                  

                  You guys help me a lot. Thanks for that. Come to India, I am going to treat you ;) Or I will come to Lausanne (Especially for Sam)

                  Added to this,
                  Since I do not have the name tag, Can I also remove the <context> tag, which is of no use?

                  Thanks,
                  Kumara

                  --Kumar

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 4 Aug 2015, 20:48 last edited by
                    #12

                    That I don't know, you need to try. But I'd say a context with no name is still a context as in "global"

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

                    K 1 Reply Last reply 4 Aug 2015, 21:22
                    0
                    • S SGaist
                      4 Aug 2015, 20:48

                      That I don't know, you need to try. But I'd say a context with no name is still a context as in "global"

                      K Offline
                      K Offline
                      kumararajas
                      wrote on 4 Aug 2015, 21:22 last edited by
                      #13

                      @SGaist OK. Thanks for the answer. I'll give a try.

                      --Kumar

                      1 Reply Last reply
                      0

                      13/13

                      4 Aug 2015, 21:22

                      • Login

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