[Interest] Dynamic translations for mobile apps at runtime?

Jérôme Godbout jerome at bodycad.com
Thu Mar 3 18:50:50 CET 2016


We did the same thing into Qml, we have a C++ singleton equivalent to your
backend, that select the current language files and emit the QString
property changed.
qsTr("String to convert") + I18n.revaluate

I wish they made underlying hook to revaluate the qsTr() with a signal
connected like if the qsTr() have changed. This pollute the code all over,
only to be able to swap language on the fly.

We also maintain a .ts and .qm per Qml module. We made a Visual Studio Task
target to rebuild them automatically per application that take the project
Language.txt where each wanted i18n wanted is specified 1/line (en, fr,
fr-CA, es, etc...).

On Thu, Mar 3, 2016 at 11:04 AM, Gian Maxera <gmaxera at gmail.com> wrote:

> 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
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160303/f6e3b674/attachment.html>


More information about the Interest mailing list