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. Translate a QApplication after I create the .exe
Forum Updated to NodeBB v4.3 + New Features

Translate a QApplication after I create the .exe

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 402 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.
  • A Offline
    A Offline
    aim0d
    wrote on last edited by
    #1

    I was wondering
    I have my application with English, French and German translation installed (create with QLinguistic and then release) and I generate the executable to have my application fully released (via CMD with command "windeployqt --quick --no-translations --qmldir ...") and translated.

    Is it possible for me to add a new language (let's say: Spanish) adding the translation file in the executable folder, without touching the souce code/open QTCreator of my application?

    Paul ColbyP 1 Reply Last reply
    0
    • A aim0d

      I was wondering
      I have my application with English, French and German translation installed (create with QLinguistic and then release) and I generate the executable to have my application fully released (via CMD with command "windeployqt --quick --no-translations --qmldir ...") and translated.

      Is it possible for me to add a new language (let's say: Spanish) adding the translation file in the executable folder, without touching the souce code/open QTCreator of my application?

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      @aim0d said in Translate a QApplication after I create the .exe:

      Is it possible for me to add a new language (let's say: Spanish) adding the translation file in the executable folder, without touching the souce code/open QTCreator of my application?

      Yes, but only if your code is already looking for translation files. A typical pattern looks like:

      // Install a localised translator, if we have translations for the current locale.
      QTranslator translator;
      if (translator.load(QLocale(), QStringLiteral("app"), QStringLiteral("."))) {
          QCoreApplication::installTranslator(&translator);
      }
      

      In this example, if you drop a translations file called app.es_ES.qm file into the current directory, then (assuming your current locale is es_ES) the app would attempt to load it, regardless of if it was present at compile time.

      Of course, you could use multiple fallbacks, such as trying to load it from compiled-in resource files, before (or after) trying to load from the local filesystem, etc.

      Cheers.

      A 1 Reply Last reply
      4
      • Paul ColbyP Paul Colby

        @aim0d said in Translate a QApplication after I create the .exe:

        Is it possible for me to add a new language (let's say: Spanish) adding the translation file in the executable folder, without touching the souce code/open QTCreator of my application?

        Yes, but only if your code is already looking for translation files. A typical pattern looks like:

        // Install a localised translator, if we have translations for the current locale.
        QTranslator translator;
        if (translator.load(QLocale(), QStringLiteral("app"), QStringLiteral("."))) {
            QCoreApplication::installTranslator(&translator);
        }
        

        In this example, if you drop a translations file called app.es_ES.qm file into the current directory, then (assuming your current locale is es_ES) the app would attempt to load it, regardless of if it was present at compile time.

        Of course, you could use multiple fallbacks, such as trying to load it from compiled-in resource files, before (or after) trying to load from the local filesystem, etc.

        Cheers.

        A Offline
        A Offline
        aim0d
        wrote on last edited by
        #3

        @Paul-Colby okay, so it's possible

        But still I had to create the qm file with QLinguistic and before create the ts file with lupdate, right?

        W 1 Reply Last reply
        0
        • A aim0d

          @Paul-Colby okay, so it's possible

          But still I had to create the qm file with QLinguistic and before create the ts file with lupdate, right?

          W Offline
          W Offline
          wrosecrans
          wrote on last edited by
          #4

          @aim0d The translation file is XML. If you really wanted to, you could just open an existing translation in a text editor and start replacing all the French words with Spanish words with no special tooling. It's just more convenient to use the tooling.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            Yes, it is possible. How to approach this depends on what you want to achieve. The translator can load any .qm file you give him. It does not matter if it is located in your resources (i.e. embedded into the .exe) or if it is an individual file on disk.

            If you want the possibility to add many more languages or give the user the ability to do their own translations, you can generate a dummy .ts file. Everyone can just change the language right at the top of this .ts file before opening it with Qt Linguist. So, you don't really need to generate multiple .ts files from source code, but can just copy an existing .ts file (most useful if there are no translations in it) and change the language. You could provide this .ts file without any translations to your users.

            Then, you can have two different methods how to apply additional translations. One suggestion was already to do this through the locale. However, you can also provide an option to load a specific translation file by filename. Then, it does not actually matter which language is set in the translation file. If users should be able to do their own translations, I'd prefer to select additional languages by filename. This is easier to explain.

            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