Translate a QApplication after I create the .exe
-
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?
-
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?
@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 ises_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.
-
@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 ises_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.
@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?
-
@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?
@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.
-
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.