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. Translated string in multiple languages
QtWS25 Last Chance

Translated string in multiple languages

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtranslatormultilingual
8 Posts 5 Posters 3.2k Views
  • 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.
  • P Offline
    P Offline
    Phill
    wrote on last edited by Phill
    #1

    Is there any way to get a translated string in multiple languages?

    Specifically, I want to look up a bible book name like 'Genesis' and get a list of 'Genesis' translated in to each language that the app I work on supports?

    Will I have to manually parse the language files?


    Edit:
    To make myself clearer, the application has / is already being translated. I just want to access all the languages it has been translated in to at run time so I can get, for example the french, german and spanish translations of one particular string,

    P 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      http://doc.qt.io/qt-5/qtlinguist-index.html

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • goldenhawkingG Offline
        goldenhawkingG Offline
        goldenhawking
        wrote on last edited by
        #3

        The "ts" files seems just to be a XML file, may be you have to manually parse the language files using linguist.

        I have a similar problem like this -- how to translate dynamic data at run-time instead of re-compile ts files?

        Qt is the best C++ framework I've ever met.

        1 Reply Last reply
        1
        • P Phill

          Is there any way to get a translated string in multiple languages?

          Specifically, I want to look up a bible book name like 'Genesis' and get a list of 'Genesis' translated in to each language that the app I work on supports?

          Will I have to manually parse the language files?


          Edit:
          To make myself clearer, the application has / is already being translated. I just want to access all the languages it has been translated in to at run time so I can get, for example the french, german and spanish translations of one particular string,

          P Offline
          P Offline
          Phill
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by Pablo J. Rogina
            #5

            @Phil please be aware that your requirements are "slightly" away from what was/is the original intent of the Qt translation approach, where the translatable strings were translated into several languages, but only one was used at any time, even if the language in use can be switched at runtime.

            Given that said, using the Qt translation features in a "slightly" different way, you can have all the translated values for a particular string at once if you create one translator per language, and then ask every translator for a particular string. One drawback from this approach is that you need to provide the context, in this example "QObject"

            #include <QCoreApplication>
            #include <QDebug>
            #include <QObject>
            #include <QTranslator>
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                QTranslator translator_es;
                QTranslator translator_it;
                QTranslator translator_fr;
            
                translator_es.load("lang/translate_es.qm");
                translator_it.load("lang/translate_it.qm");
                translator_fr.load("lang/translate_fr.qm");
            
                qDebug() << "ES: " << translator_es.translate("QObject", "Hello");
                qDebug() << "IT: " << translator_it.translate("QObject", "Hello");
                qDebug() << "FR: " << translator_fr.translate("QObject", "Hello");
            
                return a.exec();
            }
            

            with output like this:

            ES:  "Hola"
            IT:  "Ciao"
            FR:  "Bonjour"
            

            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

            P 1 Reply Last reply
            4
            • Pablo J. RoginaP Pablo J. Rogina

              @Phil please be aware that your requirements are "slightly" away from what was/is the original intent of the Qt translation approach, where the translatable strings were translated into several languages, but only one was used at any time, even if the language in use can be switched at runtime.

              Given that said, using the Qt translation features in a "slightly" different way, you can have all the translated values for a particular string at once if you create one translator per language, and then ask every translator for a particular string. One drawback from this approach is that you need to provide the context, in this example "QObject"

              #include <QCoreApplication>
              #include <QDebug>
              #include <QObject>
              #include <QTranslator>
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  QTranslator translator_es;
                  QTranslator translator_it;
                  QTranslator translator_fr;
              
                  translator_es.load("lang/translate_es.qm");
                  translator_it.load("lang/translate_it.qm");
                  translator_fr.load("lang/translate_fr.qm");
              
                  qDebug() << "ES: " << translator_es.translate("QObject", "Hello");
                  qDebug() << "IT: " << translator_it.translate("QObject", "Hello");
                  qDebug() << "FR: " << translator_fr.translate("QObject", "Hello");
              
                  return a.exec();
              }
              

              with output like this:

              ES:  "Hola"
              IT:  "Ciao"
              FR:  "Bonjour"
              
              P Offline
              P Offline
              Phill
              wrote on last edited by
              #6

              @Pablo-J.-Rogina I like your approach, but I think if I cant use the QTranslaor that is being used to translate the ui, I think parsing the language files and then storing the limited subset I need in a dictionary (I'm using Python) may be the best option.

              mrjjM 1 Reply Last reply
              0
              • P Phill

                @Pablo-J.-Rogina I like your approach, but I think if I cant use the QTranslaor that is being used to translate the ui, I think parsing the language files and then storing the limited subset I need in a dictionary (I'm using Python) may be the best option.

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

                @Phill

                Hi
                Just as a note.
                QTranslator do not translate UI as such.
                Its a dictionary based text loop up solution that will return the translated text based on the input.
                Its not tied to (G)UI at all.
                However, using python, it wont be able to automatically extract the
                texts i guess ? so if you dont need Qt Linguist tool , i guess its not
                that useful overall.

                There is also
                https://inventwithpython.com/blog/2014/12/20/translate-your-python-3-program-with-the-gettext-module/
                I like gettext for c++ programs.

                I cant tell for python as i never used it.

                1 Reply Last reply
                0
                • Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @Phill

                  parsing the language files and then storing the limited subset I need in a dictionary

                  I don't want to convince you about my idea, but based on my experience it looks like you are heading to re-invent the wheel. A parser for .ts files? A map for "translatable string" KEY -> "translated string" VALUE? Let's see below.

                  (I'm using Python) may be the best option

                  I assume you're using PyQt , if so you have most of the same tools for Qt translations. See here.

                  @mrjj There are tr()/translate() functions (with some caveats) but the idea is the same: all the strings wrapped within tr()/translate() will be copied into .ts files upon using pylupdate5 (lupdate equivalent)

                  if I cant use the QTranslaor that is being used to translate the ui

                  The main thing is that only one translator is used at a time by Qt to translate the UI. You can install tons of translators for the UI with

                   qApp->installTranslator(& aTranslator)
                  

                  but (from Qt documentation):

                  Multiple translation files can be installed in an application. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched for translations first and the earliest translation file is searched last. The search stops as soon as a translation containing a matching string is found.

                  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

                  • Login

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