• May 01, 2024, 09:44:21 pm

Author Topic: How to: Translate QtQuick apps  (Read 2808 times)

0 Members and 1 Guest are viewing this topic.

Offline huellif

  • Developer
  • Christmas Santa
  • ****
  • Posts: 402
  • Reputation: 212
How to: Translate QtQuick apps
« 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
« Last Edit: January 26, 2014, 08:05:29 pm by huellif »

Offline symster

  • Developer
  • Happy Member
  • ****
  • Posts: 214
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : Nokia N8 Soon Nokia 808
Re: How to: Translate QtQuick apps
« Reply #1 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.

Offline Allstar12345

  • Allstar Software Founder
  • Administrator
  • Forum Genius
  • ******
  • Posts: 5,235
  • Reputation: 812
    • Allstar Software
  • Current Phone: : OnePlus 8 Pro, Xperia 10, Nexus 6p, Jolla Phone, Nokia N8, Nokia 808 PureView, BlackBerry Z30
Re: How to: Translate QtQuick apps
« Reply #2 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.

Offline symster

  • Developer
  • Happy Member
  • ****
  • Posts: 214
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : Nokia N8 Soon Nokia 808
Re: How to: Translate QtQuick apps
« Reply #3 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.