[Solved] How to translate text in a listmodel? (qsTr, QT_TR_NOOP)
-
Hallo again,
When I tried to create translations for my QML UI, an error occurred when I started it: "ListElement: Cannot use script as property value". I have already searched the web for the problem and it turns out that qsTr() cannot be used within a ListModel. As I really need those strings translated, I searched for other options and some bug report answers suggest that QT_TR_NOOP() will do the job. When I replace all the qsTr() with QT_TR_NOOP(), the application is able to start again. However, nothing is translated. Why?
The translation works well for the C++ part of the application. What do I have to do to get it working for the QML part?
I have created the language files via the usual queue lupdate qml.qml -ts qml.ts -> Linguist -> lrelease qml.ts. The translators are loaded right after app start:
@
QTranslator translatorApp;
translatorApp.load("language/de");
app.installTranslator(&translatorApp);
QTranslator translatorUI;
translatorUI.load("language/ui_de");
app.installTranslator(&translatorUI);
@
The lines should load both app and UI translations into German. The "app" one works well, the other one doesn't. The files exist, load() returns true.I assume there is an easy way to do it (because it seems like a very standard task for an internationalization-capable framework), but I have not been able to find it. Can anyone point me to a viable solution please?
-
Hi.
Have a look at "this tutorial":http://developer.qt.nokia.com/wiki/Qt_Quick_Carousel#70b4903abcb62ace84264ad0443ae759
-
Sweet! Works like a charm. I just made one javascript function for each of my list models and put model.append() there for every element I want to create. Then at application start I make a call via invokeMethod to the javascript function and - voila - everything is translated without bugging me for the qsTr() calls.
I would have never thought of searching for "carousel" nor did that article pop up when I searched, so a very big thanks for that pointer!
-
You can search by tags. They describe pages better ;)
-
You can use plain list of JS object instead of ListModel.
Example:
@
id: root// define plain JS object list
property var model: [
{ title: qsTr("Airplane"), descr: qsTr("Some descr 1") },
{ title: qsTr("Car"), descr: qsTr("Some descr 2") },
{ title: qsTr("Credit Card"), descr: qsTr("Some descr 3") }
]ListView {
model: root.model
delegate: Rectangle {
height: 20// cross operability with ListModel and plain JS object list property var item: model.modelData ? model.modelData : model Text { text: item.title + " " + item.descr } }
}
@