Symbian-Developers

The Official Developers Section => Symbian Software Development Discussions => The Developers Section => Qt Application Development => Topic started by: huellif on August 21, 2013, 03:57:53 pm

Title: How to: Translate QtQuick apps
Post by: huellif on August 21, 2013, 03:57:53 pm
before your start:
you have to change all stings which should be translated like this:
text: "Hello World"; to qsTr("Hello World");

1. edit your main.cpp
inlucde
Code: [Select]
#include <QtCore/QTranslator>
#include <QtCore/QLocale>
#include <QtCore/QFile>
and add the following code BEFORE you load your QDeclarativeView/QmlApplicationViewer

Code: [Select]
QString lang = QLocale::system().name().truncate(2);

        QTranslator translator;
        if (QFile::exists(":/i18n/App_" + lang + ".qm")) {
            translator.load("App_" + lang, ":/i18n");
        }
        else {
            translator.load("App_en", ":/i18n");
        }
        app->installTranslator(&translator);

2. modify your .pro file:
add (change it to the path of your QML files)
Code: [Select]
#QRC+lupdate trick:
{ SOURCES += qml\App\*.qml }
and
Code: [Select]
TRANSLATIONS = i18n\App_en.ts \
i18n\App_de.ts
here you can add more languages.
en is for 01 (english), de is for 03 (german) etc.

3. open your .pro in QtCreator,
go to Tools, External, Linguist, Update Translation (lupdate)
now it will generate the .ts files for your qsTr() strings in a single file in \i18n\ folder
in this case for english and german.

4. now translate the .ts file, open them via Qt Qt Linguist: http://qt-project.org/doc/qt-4.8/linguist-translators.html

5. go back to QtCreator and do this:
go to Tools, External, Linguist, Release Translation (lrelease)
now you have compiled App_de.qm and App_en.qm files

6. add them to QRC

7. compile and try :)

with this code the QTranslator will automaticlly load english if your language isn't translated.
And if a single string isn't translated it uses the content of qsTr("")

if you want to try a specific language which isn't in your firmware you have to modify the code, e.g. you can replace
Code: [Select]
QTranslator translator;
        if (QFile::exists(":/i18n/App_" + lang + ".qm")) {
            translator.load("App_" + lang, ":/i18n");
        }
        else {
            translator.load("App_en", ":/i18n");
        }

with

Code: [Select]
QTranslator translator;
translator.load("App_PASTYOURLANGUAGEHERE", ":/i18n");


have fun ;)

sources:
http://harmattan-dev.nokia.com/docs/library/html/qt4/qtranslator.html
https://github.com/dicksonleong/Tweetian
Title: Re: How to: Translate QtQuick apps
Post by: symster on January 24, 2014, 07:23:51 pm
In translating am i suppose to do the translation myself or the phone will handle everything itself?.Example if want to translate "high" to russian,i would need to know the "high" in russian.
Title: Re: How to: Translate QtQuick apps
Post by: Allstar12345 on January 24, 2014, 08:21:39 pm
In translating am i suppose to do the translation myself or the phone will handle everything itself?.Example if want to translate "high" to russian,i would need to know the "high" in russian.


You add the translated words yourself.
Title: Re: How to: Translate QtQuick apps
Post by: symster on January 24, 2014, 08:46:53 pm
Then translating will be bottom priority as i don't know russia.Why nokia don't make it easy like the phone system languagr.Thanks anyway for your answer.