David Sánchez
David Sánchez

Reputation: 606

Qt i18n: QT_TR_NOOP in c++ qstr() in qml

I already consider myself with enough knowleadge in the i18n for Qt, but I came across a very interesting case, that im not even sure it can be done.

This is situation:

I have a class Station.cpp, that translates a string (before I was using tr(), but I switch to QT_TR_NOOP). This string is afterwards exposed to qml, and display as a normal text.

forecast_titles.append(QT_TR_NOOP("Tonight"));

The problem comes when after exposing that variable to qml, it was sucessfully translated when the text was generated (when the Station.cpp code was run), but when I change the language, the variable does not reevaluate in qml, and remains displaying in the original language.

Why I use QT_TR_NOOP:

QT_TR_NOOP ( sourceText ): Marks the string literal sourceText for dynamic translation in the current context (class), i.e the stored sourceText will not be altered.

So, i thought that, when it arrives to qml, and display it as

qsTr(mainboard.forecast_titles[0].title) + langProperty.emptyString

it should make the translation.

I guess the problem comes from the definition of QT_TR_NOOP, where it specifies in the current context class

Is it possible to do it some other way? (I'm talking about QT_TRANSLATE_NOOP(), or any other command) It could be that is really not possible, and I can find a solution, but still would like to know if is feasible.

Remarks: Just assume that the variable in cpp is suscessfully exposed to QML,there is not the problem.

Upvotes: 2

Views: 1848

Answers (2)

Сыч
Сыч

Reputation: 929

You are correct about translation context. Use qsTranslate('Station', mainboard.forecast_titles[0].title) + langProperty.emptyString on the QML side.

Upvotes: 0

skypjack
skypjack

Reputation: 50540

As I said in the comments, I suggest to use QT_TR_NOOP directly in your QML code, thus letting the upper layer translate the string.

Note that Qt Quick applications use the same underlying localization system as Qt C++ applications. So, maybe it's worth to use it in the right place as mentioned by the documentation.

Upvotes: 1

Related Questions