Quick guide to add multilanguage support to your QML application
This article explains how to add multilingual support in QML application.
See Also
- QML Internationalization (Library)
- Localisation QML example (Nokia Projects)
- Localizing Qt application on Symbian
Article Metadata
Article
Contents |
Introduction
Add multilingual support to your application is important as adds more quality, but can be a key factor for its spread in the Store.
Prepare your app for Internationalization
In order to allow Qt Linguist to translate your phrases you have to use tr and qsTr functions.
Qt C++
QMessageBox message;
message.setText(tr("No products available. Please try again later!"));
message.exec();
QML
Menu {
id: colorMenu
visualParent: window
MenuLayout {
MenuItem {
text: qsTr("Restore purchased products");
onClicked: {
iap_manager.restoreProducts();
}
}
MenuItem {
text: qsTr("Credits");
onClicked: {
creditsDialog.open()
}
}
}
}
On your directory project create the directory languages where we will put all languages files.
lupdate
lupdate is a tool that create the .ts file, an XML file that map all the prhases enclosed by tr and qsTr function.
Open the Qt shell, move to your directory project and run the following command for each language you are interested to:
lupdate . -ts languages/lang_it_IT.ts
lupdate . -ts languages/lang_es_ES.ts
lupdate . -ts languages/lang_pt_PT.ts
.....
Although you can use any name, for ease of use and order, my suggestion is to keep the syntax described.
Qt Linguist
Move to the languages directory and open the .ts file you are interested to with Qt Linguist, for example lang_it_IT.ts and for each sentence add the translation.
lrelease
Although the common way is to use the command line lrelease the fastest way is to use Qt Linguist clicking on : File -> Release
the .qm file
Now if all steps went well you should find the lang_it_IT.qm into the languages directory. This is the binary file you have to import into your application.
Add a new resource file to your project called languages and add all the .qm fille without using prefix
main.cpp
Adding the following code lines your app will be able to load the correct language file based on the phone settings on witch your app will be installed.
You can test by changing language settings on your phone.
If a language file is not available for the current phone settings the original text will be used.
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QTranslator>
#include <QTextCodec>
#include <QLocale>
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
qDebug() << "local:" << QLocale::system().name();
QString locale = QLocale::system().name();
QString filename = QString("languages/lang_") + locale;
static QTranslator translator;
if( translator.load(filename, ":/") ){
app->installTranslator(&translator);
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
qDebug() << "Translation file loaded" << filename;
} else
qDebug() << "Translation file not loaded:" << filename;
.....
Now you can compile, run your app and enjoy with the magic :-)






