Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 17:47, 22 October 2013 by kiran10182 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Quick guide to add multilanguage support to your QML application

From Wiki
Jump to: navigation, search
Featured Article
13 May

This article explains how to add multilingual support in QML application.

Article Metadata
Keywords: QTranslator, multilanguage, qsTr,
Created: galazzo (20 Apr 2012)
Last edited: kiran10182 (22 Oct 2013)



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!"));


Menu {
id: colorMenu
visualParent: window
MenuLayout {
MenuItem {
text: qsTr("Restore purchased products");
onClicked: {
MenuItem {
text: qsTr("Credits");
onClicked: {

On your directory project create the directory languages where we will put all languages files.



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.



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

The qrc-language-resource.png


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, ":/") ){
qDebug() << "Translation file loaded" << filename;
} else
qDebug() << "Translation file not loaded:" << filename;

Now you can compile, run your app and enjoy with the magic :-)

This page was last modified on 22 October 2013, at 17:47.
244 page views in the last 30 days.