Weitere ähnliche Inhalte Ähnlich wie Qt Translations (20) Mehr von Jussi Pohjolainen (20) Kürzlich hochgeladen (20) Qt Translations2. 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! 3. 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") 4. Example #include <QtGui> int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel mytext(QObject::tr("Hello World!")); mytext.show(); return a.exec(); } 5. 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(); } 6. 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) 8. 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 9. 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$ 10. 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> 11. 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> 13. 3. Run lrelease Generate .ts files to binary (.qm) either 1) Using Qt Linguist File/Release 2) lrelease lrelease -verbose i18n-example.pro 14. 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 15. 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(); }