Just a reminder, this is for compiled sofware using C++ and QDeclarativeView; for QML files loaded with QMLViewer there is no need since it's already set (and you just need to put qm files in a subdirectory).
Any translatable string in qml must be wrapped inside qsTr(), like this:
Than, assuming you have put all qml files in the same directory, you have to run lupdate to generate the ts file:
Now you have a ts file to open with QtLinguist. After you are done release it and put it in the translations subdir. To make the application locale-aware, we need to load the translation automatically at runtime. If we're using last QtSdk than we can implement a public function in the QmlApplicationViewer class, otherwise we can simply implement it in the main.cpp. This function must be called before the QDeclarativeView::showFullScreen() and QDeclarativeView::setSource() are called.
void QmlApplicationViewer::loadTranslator()
{
QString locale = QLocale::system().name();
QTranslator* translator = new QTranslator;
qDebug()<<"Translating: "<<translator->load(QString("/opt/myproject/translations/myproject_") + locale + QString(".qm"));
qApp->installTranslator(translator);
}
In this way the application will look for an available translation for the current set locale, and will load it if founf. Otherwise application will keep staying in source language. Coded in this way, to add a new translation is really simple: just add another qm file in the translations subdir.This Subdir does not have to stay in the file system, it can be put in the resource file of the application.