Translated string in multiple languages
-
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, -
-
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?
-
@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"
-
@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.
-
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.
-
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.