qsTr vs QT_TR_NOOP with dynamic translations
-
Hi all,
I need to have translations in my QML app. A few years ago I was doing C++ apps with translations so I'm familiar with lupdate, lrelease and installTranslator in main.cpp
I also remember that I needed to restart app after changing language to have everything translated.
The question is: If I have some models in C++ with texts in it and whole user interface in QML will app translate everything when I change language and execute installTranslator ?
I mean what will be the difference in C++ files and QML ?
What is difference between qsTr and QT_TR_NOOP in terms of dynamic translations. I have read that QT_TR_NOOP only marks the text for the lupdate to see this. In old C++ apps I was using QT_TR_NOOP when I could not use trUtf8 (like in Singleton files, for variable initialization)Best.
Marek -
@Marek said in qsTr vs QT_TR_NOOP with dynamic translations:
Hi all,
hi
I need to have translations in my QML app. A few years ago I was doing C++ apps with translations so I'm familiar with lupdate, lrelease and installTranslator in main.cpp
I also remember that I needed to restart app after changing language to have everything translated.I'm unsure if that was a requirement back than(how many years?), but as far as I remember back, runtimes language changes have been possible
The question is: If I have some models in C++ with texts in it and whole user interface in QML will app translate everything when I change language and execute installTranslator ?
Installing a QTranslator to you application will trigger a changeEvent (QEvent::LanguageChange) on QML-side this will also trigger a reevaluation of all strings marked with qsTr - at least since 5.9 - and you'll have to do nothing on that side.
On c++ side, if you're using a ui-file you'll have to call
ui->retranslateUi(this);
if you assigned text by hand, e.g myLabel->setText(tr("Hello World")); you'll have to call that function again because retranslateUi will not effect this code, even if myLabel should live in side a ui file.If you feed your QML files QStrings from a c++ backend you'll have to trigger a reload as well.
What is difference between qsTr and QT_TR_NOOP in terms of dynamic translations. I have read that QT_TR_NOOP only marks the text for the lupdate to see this. In old C++ apps I was using QT_TR_NOOP when I could not use trUtf8 (like in Singleton files, for variable initialization)
QT_TR_NOOP is only needed if you have a QString that is not created inside a function, for example as a member of your class, and you still want to translate it.
Greetings
-
@J.Hilk
Thanks for clarifications.
I started coding with Qt 4.6 but my experience with translations was probably with some early versions of Qt 5. Now when You mentioned it I remember ui->retranslateUi(this) if I wanted for changes to be visible.
Now I don't have any QWidget and ui files, all UI are in QML, however I have models in C++ where I have some variables, these I assume I will have to assign (or reload) egmyStructPointer->variable=tr("some text)
QML should take car of itself providing that I have qsTr() for every string.
Best,
Marek