Sunday, October 10, 2010

Localization in QT

Some instructions need to be followed in performing localization in Qt


1)Add tr macro for all the QString assignment.
2)Assigning the string to Non Qt objects pointer use QT_TRANSLATE_NOOP macro.
3)Assigning the string to Non Qt objects in some function use QT_NOOP macro.

Here are some samples,which shows how to add strings for Localization compatible.

Syntax to be used for global char*.

const char* TestStr = QT_TRANSLATE_NOOP("StringList","Welcome To Qt");
QPushButton *pushBtn = new QPushButton(qApp->translate("StringList",TestStr));

For Qstring Objects.

QString UIstr = QObject::tr("Qt is a Framework");
QPushButton *pushBtn = new QPushButton(tr(UIstr));

For local char*.

const char * text = QT_TR_NOOP("Qt framework rocks");
QPushButton *pushBtn = new QPushButton(tr(text));

Steps needs to be followed in generation and usage of localized file in application.
1)Add the TRANSLATION Macro in the .pro file and save.

TRANSLATIONS += superapp_fr.ts\
TRANSLATIONS += superapp_jp.ts

like this, add all possible language translation file you need.
2)Use lupdate command in Qt Command prompt to generate the .ts file and .ts file will be in your project folder.

3)  Open the .ts file in Qt linguist tool and add the mapping strings.   
     Launch Qt Linguist From Start Menu

 Adding mapping strings to the English equivalent strings.

4)Similar to step2 use lrelease to generate the .qm file and .qm file will be in your project folder.

5)Copy .qm file and place it in desired location and load it on launching the application.

Here how the loading part of code looks.

QTranslator translator;

No comments:

Post a Comment