Qt Translations<br />Jussi Pohjolainen<br />Tampere University of Applied Sciences<br />
How to Translate?<br />Every string (visible to user) surrounded by tr()<br />QLabel mytext(tr(&quot;Hello World!&quot;));...
tr()?<br />tr() function is a static function defined in QObject<br />QObject::tr(..);<br />tr() returns the translation, ...
Example<br />#include &lt;QtGui&gt;<br />int main(int argc, char *argv[])<br />{<br />    QApplication a(argc, argv);<br /...
Loading Translation Files<br />#include &lt;QtGui&gt;<br />int main(int argc, char *argv[])<br />{<br />    QApplication a...
Translating Applications<br />Run lupdate to extract all tr strings from the application&apos;s source code.<br />Translat...
lupdate i18n-example.pro<br />lrelease i18n-example.pro<br /> appTranslator.load(...);<br />
1. lupdate<br />Modify .pro file to specify language support<br />TRANSLATIONS = myapp_fi.ts<br />               myapp_fr....
tb308pohjus-l:i18n-example pohjus$ cat i18n-example.pro<br />SOURCES += main.cpp<br />TRANSLATIONS = i18n-example_fi.ts i1...
i18n-example_fi.ts<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />&lt;!DOCTYPE TS&gt;<br />&lt...
2. Translate: i18n-example_fi.ts<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />&lt;!DOCTYPE T...
2. Translate using Qt Linguist<br />
3. Run lrelease<br />Generate .ts files to binary (.qm) either<br />1) Using Qt Linguist<br />File/Release<br />2) lreleas...
tb308pohjus-l:i18n-example pohjus$ lrelease -verbose i18n-example.pro<br />Updating &apos;/Users/pohjus/Documents/Kurssit/...
Running<br />#include &lt;QtGui&gt;<br />int main(int argc, char *argv[])<br />{<br />    QApplication a(argc, argv);<br /...
Upcoming SlideShare
Loading in...5
×

Qt Translations

588

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
588
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Qt Translations

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

    Clipping is a handy way to collect important slides you want to go back to later.

×