(Difference between revisions)

Quick guide to add multilanguage support to your QML application

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Text replace - "[[Category:Internationalization" to "[[Category:Localization")
kiran10182 (Talk | contribs)
m (Kiran10182 - Project migration task)
Line 4: Line 4:
* [http://qt-project.org/doc/qt-4.8/qdeclarativei18n.html QML Internationalization] (Library)
* [http://qt-project.org/doc/qt-4.8/qdeclarativei18n.html QML Internationalization] (Library)
* [http://projects.developer.nokia.com/localizationqml Localisation QML example] (Nokia Projects)
* [[Media:Localizationqml.zip|Localisation QML example]]
* [[Localizing Qt application on Symbian]]}}
* [[Localizing Qt application on Symbian]]}}
{{ArticleMetaData <!-- v1.2 -->
{{ArticleMetaData <!-- v1.2 -->

Latest revision as of 20:47, 22 October 2013

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)


[edit] 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.

[edit] Prepare your app for Internationalization

In order to allow Qt Linguist to translate your phrases you have to use tr and qsTr functions.

[edit] Qt C++

QMessageBox message;
message.setText(tr("No products available. Please try again later!"));

[edit] QML

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.


[edit] 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.


[edit] 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.


[edit] lrelease

Although the common way is to use the command line lrelease the fastest way is to use Qt Linguist clicking on : File -> Release


[edit] 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

[edit] 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, ":/") ){
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 20:47.
285 page views in the last 30 days.