This is my QML code:
Text {
id: helloText
//: Hello World text
//% "Hello World"
//~ Context No specific context
text: qsTrId("hello-world-id");
anchors.top: parent.top
}
Excerpt from the Qt documentation here:
The "Engineering English" text that you see in the user interface for development builds is indicated with a //% comment. If you do not include this, the text ID will be shown in the user interface.
When I run the application, I'm seeing hello-world-id
rather than Hello World
, as described in the documentation. Am I missing something here?
Thanks in advance.
First of all you have to create .ts
file with translated strings. For example, if I want to create translation for Russian locale, I do:
lupdate main.qml -ts your_project_ru_RU.ts
If you have several QML files you can specify it in .pro
file:
lupdate_only {
SOURCES = main.qml \
foo.qml
}
and so write:
lupdate your_project.pro -ts your_project_ru_RU.ts
this command will create untranslated file for Russian locale that you have to open with QT Linguist to translate.
You can specify .ts
files in .pro file:
TRANSLATIONS += your_project_ru_RU.ts
After all you have to compile the file (File/Compile in Qt Linguist). That will create your_project_ru_RU.qm
.
Finally, you must load .qm
file, for example in main.cpp
:
QTranslator translator;
translator.load("your_project_" + QLocale::system().name());
app.installTranslator(&translator);
UPDATED:
If you need some semblance of Qt i18n engine, without providing qm
files:
foo.qml
import QtQuick 2.4
import QtQuick.Window 2.2
import "i18n.js" as I18n
Window {
Text {
text: I18n.myTr("text-id");
anchors.centerIn: parent
}
}
i18n.js
var texts = {
"text-id": "Hello, World!",
"another-text-id": "Goodbye, World!"
}
function myTr(textid) {
return texts[textid] ? texts[textid] : "";
}