Qt Translations
Upcoming SlideShare
Loading in...5
×
 

Qt Translations

on

  • 858 views

 

Statistics

Views

Total Views
858
Views on SlideShare
843
Embed Views
15

Actions

Likes
0
Downloads
16
Comments
0

2 Embeds 15

http://a-ot042.ning.com 14
http://php.tpu.fi 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Qt Translations Qt Translations Presentation Transcript

  • Qt Translations
    Jussi Pohjolainen
    Tampere University of Applied Sciences
  • How to Translate?
    Every string (visible to user) surrounded by tr()
    QLabel mytext(tr("Hello World!"));
    Load a translation (.qm) file at startup
    Using tr() is good practice, even though you don't have translation files yet.. they can be added later!
  • tr()?
    tr() function is a static function defined in QObject
    QObject::tr(..);
    tr() returns the translation, if not found, it will return original text
    // if translation is not found,
    // "hello" is returned
    tr("hello")
  • Example
    #include <QtGui>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QLabel mytext(QObject::tr("Hello World!"));
    mytext.show();
    return a.exec();
    }
  • Loading Translation Files
    #include <QtGui>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTranslator appTranslator;
    // QLocale::system().name() => for example, en_US
    appTranslator.load("myapp_" + QLocale::system().name(), ".");
    a.installTranslator(&appTranslator);
    QLabel mytext(QObject::tr("Hello World!"));
    mytext.show();
    return a.exec();
    }
  • Translating Applications
    Run lupdate to extract all tr strings from the application's source code.
    Translate the application using Qt Linguist
    Run lrelease to generate binary .qm files that the app can load using QTranslator (done automatically)
  • lupdate i18n-example.pro
    lrelease i18n-example.pro
    appTranslator.load(...);
  • 1. lupdate
    Modify .pro file to specify language support
    TRANSLATIONS = myapp_fi.ts
    myapp_fr.ts
    lupdate will generate these xml-based files
    lupdate –verbose myapp.pro
  • tb308pohjus-l:i18n-example pohjus$ cat i18n-example.pro
    SOURCES += main.cpp
    TRANSLATIONS = i18n-example_fi.ts i18n-example_en_US.ts
    tb308pohjus-l:i18n-example pohjus$ lupdate -verbose i18n-example.pro
    Updating 'i18n-example_en_US.ts'...
    Found 1 source text(s) (1 new and 0 already existing)
    Updating 'i18n-example_fi.ts'...
    Found 1 source text(s) (1 new and 0 already existing)
    tb308pohjus-l:i18n-example pohjus$ ls -al
    total 1624
    drwxr-xr-x 9 pohjus staff 306 8 Hel 09:38 .
    drwxr-xr-x 9 pohjus staff 306 8 Hel 09:16 ..
    -rw-r--r-- 1 pohjus staff 9484 8 Hel 09:35 Makefile
    drwxr-xr-x 3 pohjus staff 102 8 Hel 09:18 i18n-example.app
    -rw-r--r-- 1 pohjus staff 76 8 Hel 09:38 i18n-example.pro
    -rw-r--r-- 1 pohjus staff 312 8 Hel 09:38 i18n-example_en_US.ts
    -rw-r--r-- 1 pohjus staff 312 8 Hel 09:38 i18n-example_fi.ts
    -rw-r--r-- 1 pohjus staff 371 8 Hel 09:25 main.cpp
    -rw-r--r-- 1 pohjus staff 799096 8 Hel 09:25 main.o
    tb308pohjus-l:i18n-example pohjus$
  • i18n-example_fi.ts
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS>
    <TS version="2.0" language="fi_FI">
    <context>
    <name>QObject</name>
    <message>
    <location filename="main.cpp" line="12"/>
    <source>Hello World!</source>
    <translation type="unfinished"></translation>
    </message>
    </context>
    </TS>
  • 2. Translate: i18n-example_fi.ts
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS>
    <TS version="2.0" language="fi_FI">
    <context>
    <name>QObject</name>
    <message>
    <location filename="main.cpp" line="12"/>
    <source>Hello World!</source>
    <translation type="unfinished">Terve maailma!</translation>
    </message>
    </context>
    </TS>
  • 2. Translate using Qt Linguist
  • 3. Run lrelease
    Generate .ts files to binary (.qm) either
    1) Using Qt Linguist
    File/Release
    2) lrelease
    lrelease -verbose i18n-example.pro
  • tb308pohjus-l:i18n-example pohjus$ lrelease -verbose i18n-example.pro
    Updating '/Users/pohjus/Documents/Kurssit/TAMK Mobiiliohjelmointi 2 (Symbian)/qt/examples/i18n-example/i18n-example_en_US.qm'...
    Generated 1 translation(s) (0 finished and 1 unfinished)
    Updating '/Users/pohjus/Documents/Kurssit/TAMK Mobiiliohjelmointi 2 (Symbian)/qt/examples/i18n-example/i18n-example_fi.qm'...
    Generated 1 translation(s) (0 finished and 1 unfinished)
    Generated 1 translation(s) (0 finished and 1 unfinished)
    tb308pohjus-l:i18n-example pohjus$ rm *.qm
    tb308pohjus-l:i18n-example pohjus$ lrelease -verbose i18n-example.pro
    Updating '/Users/pohjus/Documents/Kurssit/TAMK Mobiiliohjelmointi 2 (Symbian)/qt/examples/i18n-example/i18n-example_en_US.qm'...
    Generated 1 translation(s) (0 finished and 1 unfinished)
    Updating '/Users/pohjus/Documents/Kurssit/TAMK Mobiiliohjelmointi 2 (Symbian)/qt/examples/i18n-example/i18n-example_fi.qm'...
    Generated 1 translation(s) (0 finished and 1 unfinished)
    Generated 1 translation(s) (0 finished and 1 unfinished)
    tb308pohjus-l:i18n-example pohjus$ ls -al
    total 1640
    drwxr-xr-x 11 pohjus staff 374 8 Hel 09:59 .
    drwxr-xr-x 9 pohjus staff 306 8 Hel 09:16 ..
    -rw-r--r-- 1 pohjus staff 9484 8 Hel 09:35 Makefile
    drwxr-xr-x 3 pohjus staff 102 8 Hel 09:18 i18n-example.app
    -rw-r--r-- 1 pohjus staff 76 8 Hel 09:38 i18n-example.pro
    -rw-r--r-- 1 pohjus staff 127 8 Hel 09:59 i18n-example_en_US.qm
    -rw-r--r-- 1 pohjus staff 335 8 Hel 09:49 i18n-example_en_US.ts
    -rw-r--r-- 1 pohjus staff 109 8 Hel 09:59 i18n-example_fi.qm
    -rw-r--r-- 1 pohjus staff 326 8 Hel 09:49 i18n-example_fi.ts
    -rw-r--r-- 1 pohjus staff 371 8 Hel 09:25 main.cpp
    -rw-r--r-- 1 pohjus staff 799096 8 Hel 09:25 main.o
  • Running
    #include <QtGui>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTranslator appTranslator;
    // Path is now ../../../ because of OS X app bundle!
    appTranslator.load("i18n-example_fi, "../../../");
    a.installTranslator(&appTranslator);
    QLabel mytext(QObject::tr("Hello World!"));
    mytext.show();
    return a.exec();
    }