[Interest] qsTr in components

Shantanu Tushar shaan7in at gmail.com
Sat Jun 11 20:25:36 CEST 2016


Hi,

Instead of using ID-based translations, yo can just use qsTranslate[1] and
specify a good context yourself. Here are some examples on how I use it-

text: qsTranslate("Tournaments", "Prize: %1 %2")
text: qsTranslate("Actions", "Logout")
text: qsTranslate("Screens", "Settings")

I find it much more readable than the ID-based option.

[1] http://doc.qt.io/qt-5/qml-qtqml-qt.html#qsTranslate-method

On Thu, May 26, 2016 at 3:00 PM, Kristoffersen, Even (NO14) <
Even.Kristoffersen at honeywell.com> wrote:

> Alright, so I think I've found out why this doesn't work.
>
> When doing the translation inside the TextTr component it loses the
> context.
> The QT_TR_NOOP is in main.qml so "main" is the context in the translation
> files.
> That's why it works when I do qsTr() in main.qml, but no translation is
> found when doing it from inside the component itself (context then becomes
> "TextTr" instead of "main")
>
> This blog entry explains it pretty well
> http://imaginativethinking.ca/how-the-heck-can-i-use-qstr-with-variables/.
>
> Seems like I'll have to switch to the ID based translations
> http://doc.qt.io/qt-5/linguist-id-based-i18n.html to make this approach
> work.
>
> -Even
>
> -----Original Message-----
> From: Kristoffersen, Even (NO14)
> Sent: 24. mai 2016 11:58
> To: Kristoffersen, Even (NO14) <Even.Kristoffersen at Honeywell.com>
> Cc: interest at qt-project.org
> Subject: RE: [Interest] qsTr in components
>
> Minimal example attached.
>
> If I use the TextTr component "txt" property it does not show any
> translation at all even though txt goes through qsTr and the translation is
> loaded at creation time.
> If I use " text: qsTr(x) + translator.refresh" directly from main.qml then
> it works.
>
> You can switch between the modes by changing to the commented out lines 36
> and 73 in main.qml instead of line 35 and 72.
>
> -Even
>
>
> -----Original Message-----
> From: Interest [mailto:interest-bounces+even.kristoffersen=
> honeywell.com at qt-project.org] On Behalf Of Kristoffersen, Even (NO14)
> Sent: 23. mai 2016 15:24
> To: Jason H <jhihn at gmx.com>
> Cc: interest at qt-project.org
> Subject: Re: [Interest] qsTr in components
>
> Yup,
>
> translator is my C++ object exposed to QML, and refresh is the signal.
> Not sure why it's not working, I'll try to create a minimal example and
> see if it's still an issue there.
>
> This is with Qt 5.6 btw.
>
> -Even
>
> -----Original Message-----
> From: Jason H [mailto:jhihn at gmx.com]
> Sent: 23. mai 2016 15:17
> To: Jason H <jhihn at gmx.com>
> Cc: Kristoffersen, Even (NO14) <Even.Kristoffersen at Honeywell.com>;
> interest at qt-project.org
> Subject: Re: [Interest] qsTr in components
>
>
>
> > Sent: Monday, May 23, 2016 at 9:14 AM
> > From: "Jason H" <jhihn at gmx.com>
> > To: "Kristoffersen, Even (NO14)" <Even.Kristoffersen at Honeywell.com>
> > Cc: "interest at qt-project.org" <interest at qt-project.org>
> > Subject: Re: [Interest] qsTr in components
> >
> >
> >
> > > Sent: Monday, May 23, 2016 at 8:31 AM
> > > From: "Kristoffersen, Even (NO14)" <Even.Kristoffersen at Honeywell.com>
> > > To: "interest at qt-project.org" <interest at qt-project.org>
> > > Subject: [Interest] qsTr in components
> > >
> > > Hello all,
> > >
> > > I've played around a bit with an approach inspired by
> http://wiki.qt.io/How_to_do_dynamic_translation_in_QML .
> > >
> >
> > If you are following along, the example a few months ago, it works. I'm
> using it, and in components too.
> >
> > You need to add a C++ object, and expose it to QML.
> >
> > it has a proeprty: Q_PROPERTY(QString null READ getNull NOTIFY
> languageChanged )
> > signals:
> >       void languageChanged(QString language);
> >
> > When you change the language, you emit that signal, that causes the
> re-evaluation of all string with the langMgr.null added.
>
> EDIT: Not sure why I used C++, it should be possible with QML. I think I
> used C++ so I could start the download of languages before the QML engine
> started. (My langmgr manages the languages, including downloads updates)
>
> _______________________________________________
> 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
>



-- 
Shantanu Tushar    (UTC +0530)
shantanu.io
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160611/dff0c3f2/attachment.html>


More information about the Interest mailing list