~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/linguist-arrowpad.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/arrowpad.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Arrow Pad Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Arrow Pad Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="linguist-arrowpad-arrowpad-cpp.html">linguist/arrowpad/arrowpad.cpp</a></li>
 
24
<li><a href="linguist-arrowpad-arrowpad-h.html">linguist/arrowpad/arrowpad.h</a></li>
 
25
<li><a href="linguist-arrowpad-mainwindow-cpp.html">linguist/arrowpad/mainwindow.cpp</a></li>
 
26
<li><a href="linguist-arrowpad-mainwindow-h.html">linguist/arrowpad/mainwindow.h</a></li>
 
27
<li><a href="linguist-arrowpad-main-cpp.html">linguist/arrowpad/main.cpp</a></li>
 
28
</ul>
 
29
<p>This example is a slightly more involved and introduces a key <i>Qt Linguist</i> concept: &quot;contexts&quot;. It also shows how to use two or more languages.</p>
 
30
<center><img src="images/linguist-arrowpad_en.png" /></center><p>We will use two translations, French and Dutch, although there is no effective limit on the number of possible translations that can be used with an application. The relevant lines of <tt>arrowpad.pro</tt> are</p>
 
31
<pre>&nbsp;   HEADERS      = arrowpad.h \
 
32
                   mainwindow.h
 
33
    SOURCES      = arrowpad.cpp \
 
34
                   main.cpp \
 
35
                   mainwindow.cpp
 
36
    TRANSLATIONS = arrowpad_fr.ts \
 
37
                   arrowpad_nl.ts</pre>
 
38
<p>Run <tt>lupdate</tt>; it should produce two identical message files <tt>arrowpad_fr.ts</tt> and <tt>arrowpad_nl.ts</tt>. These files will contain all the source texts marked for translation with <tt>tr()</tt> calls and their contexts.</p>
 
39
<p>See the <a href="linguist-manual.html">Qt Linguist manual</a> for more information about translating Qt application.</p>
 
40
<a name="line-by-line-walkthrough"></a>
 
41
<h2>Line by Line Walkthrough</h2>
 
42
<p>In <tt>arrowpad.h</tt> we define the <tt>ArrowPad</tt> subclass which is a subclass of <a href="qwidget.html">QWidget</a>. In the screenshot above, the central widget with the four buttons is an <tt>ArrowPad</tt>.</p>
 
43
<pre>&nbsp;   class ArrowPad : public QWidget
 
44
    {
 
45
        Q_OBJECT</pre>
 
46
<p>When <tt>lupdate</tt> is run it not only extracts the source texts but it also groups them into contexts. A context is the name of the class in which the source text appears. Thus, in this example, &quot;ArrowPad&quot; is a context: it is the context of the texts in the <tt>ArrowPad</tt> class. The <tt>Q_OBJECT</tt> macro defines <tt>tr(x)</tt> in <tt>ArrowPad</tt> like this:</p>
 
47
<pre>&nbsp;   qApp-&gt;translate(&quot;ArrowPad&quot;, x)</pre>
 
48
<p>Knowing which class each source text appears in enables <i>Qt Linguist</i> to group texts that are logically related together, e.g. all the text in a dialog will have the context of the dialog's class name and will be shown together. This provides useful information for the translator since the context in which text appears may influence how it should be translated. For some translations keyboard accelerators may need to be changed and having all the source texts in a particular context (class) grouped together makes it easier for the translator to perform any accelerator changes without introducing conflicts.</p>
 
49
<p>In <tt>arrowpad.cpp</tt> we implement the <tt>ArrowPad</tt> class.</p>
 
50
<pre>&nbsp;       upButton = new QPushButton(tr(&quot;&amp;Up&quot;));
 
51
        downButton = new QPushButton(tr(&quot;&amp;Down&quot;));
 
52
        leftButton = new QPushButton(tr(&quot;&amp;Left&quot;));
 
53
        rightButton = new QPushButton(tr(&quot;&amp;Right&quot;));</pre>
 
54
<p>We call <tt>ArrowPad::tr()</tt> for each button's label since the labels are user-visible text.</p>
 
55
<center><img src="images/linguist-arrowpad_en.png" /></center><pre>&nbsp;   class MainWindow : public QMainWindow
 
56
    {
 
57
        Q_OBJECT</pre>
 
58
<p>In the screenshot above, the whole window is a <tt>MainWindow</tt>. This is defined in the <tt>mainwindow.h</tt> header file. Here too, we use <tt>Q_OBJECT</tt>, so that <tt>MainWindow</tt> will become a context in <i>Qt Linguist</i>.</p>
 
59
<pre>&nbsp;       arrowPad = new ArrowPad;</pre>
 
60
<p>In the implementation of <tt>MainWindow</tt>, <tt>mainwindow.cpp</tt>, we create an instance of our <tt>ArrowPad</tt> class.</p>
 
61
<pre>&nbsp;       exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
62
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
63
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));</pre>
 
64
<p>We also call <tt>MainWindow::tr()</tt> twice, once for the action and once for the shortcut.</p>
 
65
<p>Note the use of <tt>tr()</tt> to support different keys in other languages. &quot;Ctrl+Q&quot; is a good choice for Quit in English, but a Dutch translator might want to use &quot;Ctrl+A&quot; (for Afsluiten) and a German translator &quot;Strg+E&quot; (for Beenden). When using <tt>tr()</tt> for <b>Ctrl</b> key accelerators, the two argument form should be used with the second argument describing the function that the accelerator performs.</p>
 
66
<p>Our <tt>main()</tt> function is defined in <tt>main.cpp</tt> as usual.</p>
 
67
<pre>&nbsp;       QTranslator translator;
 
68
        translator.load(QString(&quot;arrowpad_&quot;) + locale);
 
69
        app.installTranslator(&amp;translator);</pre>
 
70
<p>We choose which translation to use according to the current locale. <a href="qlocale.html#system">QLocale::system</a>() can be influenced by setting the <tt>LANG</tt> environment variable, for example. Notice that the use of a naming convention that incorporates the locale for <tt>.qm</tt> message files, (and <tt>.ts</tt> files), makes it easy to implement choosing the translation file according to locale.</p>
 
71
<p>If there is no <tt>.qm</tt> message file for the locale chosen the original source text will be used and no error raised.</p>
 
72
<a name="translating-to-french-and-dutch"></a>
 
73
<h2>Translating to French and Dutch</h2>
 
74
<p>We'll begin by translating the example application into French. Start <i>Qt Linguist</i> with <tt>arrowpad_fr.ts</tt>. You should get the seven source texts (&quot;&amp;Up&quot;, &quot;&amp;Left&quot;, etc.) grouped in two contexts (&quot;ArrowPad&quot; and &quot;<a href="mainwindow.html">MainWindow</a>&quot;).</p>
 
75
<p>Now, enter the following translations:</p>
 
76
<ul>
 
77
<li><tt>ArrowPad</tt><ul>
 
78
<li>&amp;Up - &amp;Haut</li>
 
79
<li>&amp;Left - &amp;Gauche</li>
 
80
<li>&amp;Right - &amp;Droite</li>
 
81
<li>&amp;Down - &amp;Bas</li>
 
82
</ul>
 
83
</li>
 
84
<li><tt>MainWindow</tt><ul>
 
85
<li>E&amp;xit - &amp;Quitter</li>
 
86
<li>Ctrl+Q - Ctrl+Q</li>
 
87
<li>&amp;File - &amp;Fichier</li>
 
88
</ul>
 
89
</li>
 
90
</ul>
 
91
<p>It's quickest to press <b>Alt+D</b> (which clicks the <b>Done &amp; Next</b> button) after typing each translation, since this marks the translation as done and moves on to the next source text.</p>
 
92
<p>Save the file and do the same for Dutch working with <tt>arrowpad_nl.ts</tt>:</p>
 
93
<ul>
 
94
<li><tt>ArrowPad</tt><ul>
 
95
<li>&amp;Up - &amp;Boven</li>
 
96
<li>&amp;Left - &amp;Links</li>
 
97
<li>&amp;Right - &amp;Rechts</li>
 
98
<li>&amp;Down - &amp;Onder</li>
 
99
</ul>
 
100
</li>
 
101
<li><tt>MainWindow</tt><ul>
 
102
<li>E&amp;xit - &amp;Afsluiten</li>
 
103
<li>Ctrl+Q - Ctrl+A</li>
 
104
<li>File - &amp;Bestand</li>
 
105
</ul>
 
106
</li>
 
107
</ul>
 
108
<p>We have to convert the <tt>tt1_fr.ts</tt> and <tt>tt1_nl.ts</tt> translation source files into <tt>.qm</tt> files. We could use <i>Qt Linguist</i> as we've done before; however using the command line tool <tt>lrelease</tt> ensures that <i>all</i> the <tt>.qm</tt> files for the application are created without us having to remember to load and <b>File|Release</b> each one individually from <i>Qt Linguist</i>.</p>
 
109
<p>Type</p>
 
110
<pre>&nbsp;   lrelease arrowpad.pro</pre>
 
111
<p>This should create both <tt>arrowpad_fr.qm</tt> and <tt>arrowpad_nl.qm</tt>. Set the <tt>LANG</tt> environment variable to <tt>fr</tt>. In Unix, one of the two following commands should work</p>
 
112
<pre>&nbsp;   export LANG=fr
 
113
    setenv LANG fr</pre>
 
114
<p>In Windows, either modify <tt>autoexec.bat</tt> or run</p>
 
115
<pre>&nbsp;   set LANG=fr</pre>
 
116
<p>When you run the program, you should now see the French version:</p>
 
117
<center><img src="images/linguist-arrowpad_fr.png" /></center><p>Try the same with Dutch, by setting <tt>LANG=nl</tt>. Now the Dutch version should appear:</p>
 
118
<center><img src="images/linguist-arrowpad_nl.png" /></center><a name="exercises"></a>
 
119
<h2>Exercises</h2>
 
120
<p>Mark one of the translations in <i>Qt Linguist</i> as not done, i.e. by unchecking the &quot;done&quot; checkbox; run <tt>lupdate</tt>, then <tt>lrelease</tt>, then the example. What effect did this change have?</p>
 
121
<p>Set <tt>LANG=fr_CA</tt> (French Canada) and run the example program again. Explain why the result is the same as with <tt>LANG=fr</tt>.</p>
 
122
<p>Change one of the accelerators in the Dutch translation to eliminate the conflict between <i>&amp;Bestand</i> and <i>&amp;Boven</i>.</p>
 
123
<p /><address><hr /><div align="center">
 
124
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
125
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
126
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
127
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
128
</tr></table></div></address></body>
 
129
</html>