[Interest] Dynamic translations for mobile apps at runtime?
Gian Maxera
gmaxera at gmail.com
Thu Mar 3 17:04:25 CET 2016
Hello Jason,
I got the same issue some times ago … and I found that it’s possible to use the translation feature of Qt … that seems static, but it’s not.
And localize.biz it’s a wonderful site that allow you to modify Qt translation files directly on web and download the updated one.
The trick to achieve (summarized) is the following:
Somewhere in your code maintain and update from remote an array of Translators:
translators["en"] = new QTranslator(this);
translators["en"]->load( "tr_en", commonPath()+"/translations" );
translators["de"] = new QTranslator(this);
translators["de"]->load( "tr_de", commonPath()+"/translations" );
translators["fr"] = new QTranslator(this);
translators["fr"]->load( "tr_fr", commonPath()+"/translations" );
translators["ru"] = new QTranslator(this);
translators["ru"]->load( "tr_ru", commonPath()+"/translations" );
You can change these entry with new files downloaded at runtime.
Then you implement a method that you call at runtime for changing the translator, something like that:
void Backend::selectLanguage( QString language ) {
foreach( QString lang, translators.keys() ) {
if ( lang == language ) {
qApp->installTranslator( translators[lang] );
} else {
qApp->removeTranslator( translators[lang] );
}
}
this->language = language;
emit languageChanged();
}
And then there is the final trick:
You create a “fake” property that is always an empty string but it’s binded to languageChanged signal:
Q_PROPERTY( QString es READ getES NOTIFY languageChanged )
And (the most annoying part), append this empty string to all string you want to change at runtime like that:
qsTr("NEWS<br/>HUB")+backend.es
And close the loop.
What will happen is the following: the translator change at runtime and you trigger a languageChanged that trigger an update of all string that got backend.es appended that trigger the call of qsTr that take the new translation from the new translator.
Ciao,
Gianluca.
> On 3 Mar 2016, at 15:51, Jason H <jhihn at gmx.com> wrote:
>
> First, I'm not talking about the standard translation features of Qt. Those are static - the translation file is generated and deployed with the app. I now need to support an ever-changing list of translations. I want my mobile app to download the current set of translations and use that. In addition, it would be nice if it was some kind of open format. We have a web app where these will be maintained, because they need to be shared with the web. The web is backed by a database, and exporting these to JSON would be ideal, along with Javascript versions for the Web UI, so we don't have to create and maintain two translation systems.
>
>
> What support is there for this?
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
More information about the Interest
mailing list