Namespaces

Variants
Actions

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 over the next few weeks. Thanks for all your past and future contributions.

Quick guide to add multilanguage support to your QML application

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

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

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

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.

Directory-languages.png

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.

Shell-languages.png

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.

Qt-linguist.png

lrelease

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

Qt-linguist-release.png

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

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 :-)

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

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×