[Interest] Dynamic translations for mobile apps at runtime?

Gianluca gmaxera at gmail.com
Tue Mar 8 21:44:52 CET 2016


qApp->installTranslator add a new translation into the stack. Does not remove the old ones.
So, if the user click 10 times: Italian - English - Italian - English … etc…
you got ten translator into the memory.
That’s because the translation is searched into the order on which the translator are installed into the stack.

That’s why I remove everything so there is only one translators at time into the memory.

Il giorno 08/mar/2016, alle ore 18:46, Jason H <jhihn at gmx.com> ha scritto:

> I'm wondering why you load all those languages and then remove all but one of them? Being a mobile app, I have to be somewhat conscience of memory foot print. Do you see anything wrong with:
> 
> void Backend::selectLanguage( QString language ) {
>    translator = new QTranslator(this);
>    translator->load( language, commonPath()+"/translations" );
>    qApp->installTranslator(translator);
> }
> 
> ?
> 
> 
>> 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.
>> 




More information about the Interest mailing list