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] : "";
}