qt translations
DESCRIPTION
TRANSCRIPT
![Page 1: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/1.jpg)
Qt Translations
Jussi PohjolainenTampere University of Applied Sciences
![Page 2: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/2.jpg)
How to Translate?
1. Every string (visible to user) surrounded by tr()– QLabel mytext(tr("Hello World!"));
2. 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!
![Page 3: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/3.jpg)
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")
![Page 4: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/4.jpg)
Example#include <QtGui>
int main(int argc, char *argv[]){ QApplication a(argc, argv);
QLabel mytext(QObject::tr("Hello World!")); mytext.show();
return a.exec();}
![Page 5: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/5.jpg)
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();}
![Page 6: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/6.jpg)
Translating Applications
1. Run lupdate to extract all tr strings from the application's source code.
2. Translate the application using Qt Linguist3. Run lrelease to generate binary .qm files that
the app can load using QTranslator (done automatically)
![Page 7: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/7.jpg)
lupdate i18n-example.pro
lrelease i18n-example.pro
appTranslator.load(...);
![Page 8: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/8.jpg)
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
![Page 9: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/9.jpg)
tb308pohjus-l:i18n-example pohjus$ cat i18n-example.proSOURCES += main.cppTRANSLATIONS = i18n-example_fi.ts i18n-example_en_US.tstb308pohjus-l:i18n-example pohjus$ lupdate -verbose i18n-example.proUpdating '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 -altotal 1624drwxr-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 Makefiledrwxr-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.otb308pohjus-l:i18n-example pohjus$
![Page 10: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/10.jpg)
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>
![Page 11: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/11.jpg)
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>
![Page 12: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/12.jpg)
2. Translate using Qt Linguist
![Page 13: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/13.jpg)
3. Run lrelease
• Generate .ts files to binary (.qm) either• 1) Using Qt Linguist– File/Release
• 2) lrelease– lrelease -verbose i18n-example.pro
![Page 14: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/14.jpg)
tb308pohjus-l:i18n-example pohjus$ lrelease -verbose i18n-example.proUpdating '/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 *.qmtb308pohjus-l:i18n-example pohjus$ lrelease -verbose i18n-example.proUpdating '/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 -altotal 1640drwxr-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 Makefiledrwxr-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
![Page 15: Qt Translations](https://reader031.vdocuments.mx/reader031/viewer/2022020723/540749c78d7f7288088b4a84/html5/thumbnails/15.jpg)
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();}