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

« back to all changes in this revision

Viewing changes to doc/html/linguist-hellotr.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/hellotr.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Hello tr() 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">Hello tr() Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="linguist-hellotr-main-cpp.html">linguist/hellotr/main.cpp</a></li>
 
24
</ul>
 
25
<p>This example is a reworking of the <a href="tutorial-t1.html">Hello World!</a> example from the <a href="tutorial.html">Qt Tutorial</a>, with a Latin translation. The screenshot below shows the English version.</p>
 
26
<center><img src="images/linguist-hellotr_en.png" /></center><p>See the <a href="linguist-manual.html">Qt Linguist manual</a> for more information about translating Qt application.</p>
 
27
<a name="line-by-line-walkthrough"></a>
 
28
<h2>Line by Line Walkthrough</h2>
 
29
<pre>&nbsp;   #include &lt;QTranslator&gt;</pre>
 
30
<p>This line includes the definition of the <a href="qtranslator.html">QTranslator</a> class. Objects of this class provide translations for user-visible text.</p>
 
31
<pre>&nbsp;       QTranslator translator;</pre>
 
32
<p>Creates a <a href="qtranslator.html">QTranslator</a> object without a parent.</p>
 
33
<pre>&nbsp;       translator.load(&quot;hellotr_la&quot;);</pre>
 
34
<p>Tries to load a file called <tt>hellotr_la.qm</tt> (the <tt>.qm</tt> file extension is implicit) that contains Latin translations for the source texts used in the program. No error will occur if the file is not found.</p>
 
35
<pre>&nbsp;       app.installTranslator(&amp;translator);</pre>
 
36
<p>Adds the translations from <tt>hellotr_la.qm</tt> to the pool of translations used by the program.</p>
 
37
<pre>&nbsp;       QPushButton hello(QPushButton::tr(&quot;Hello world!&quot;));</pre>
 
38
<p>Creates a push button that displays &quot;Hello world!&quot;. If <tt>hellotr_la.qm</tt> was found and contains a translation for &quot;Hello world!&quot;, the translation appears; if not, the source text appears.</p>
 
39
<p>All classes that inherit <a href="qobject.html">QObject</a> have a <tt>tr()</tt> function. Inside a member function of a <a href="qobject.html">QObject</a> class, we simply write <tt>tr(&quot;Hello world!&quot;)</tt> instead of <tt>QPushButton::tr(&quot;Hello world!&quot;)</tt> or <tt>QObject::tr(&quot;Hello world!&quot;)</tt>.</p>
 
40
<a name="running-the-application-in-english"></a>
 
41
<h2>Running the Application in English</h2>
 
42
<p>Since we haven't made the translation file <tt>hellotr_la.qm</tt>, the source text is shown when we run the application:</p>
 
43
<center><img src="images/linguist-hellotr_en.png" /></center><a name="creating-a-latin-message-file"></a>
 
44
<h2>Creating a Latin Message File</h2>
 
45
<p>The first step is to create a project file, <tt>hellotr.pro</tt>, that lists all the source files for the project. The project file can be a qmake project file, or even an ordinary makefile. Any file that contains</p>
 
46
<pre>&nbsp;   SOURCES      = main.cpp
 
47
    TRANSLATIONS = hellotr_la.ts</pre>
 
48
<p>will work. <tt>TRANSLATIONS</tt> specifies the message files we want to maintain. In this example, we just maintain one set of translations, namely Latin.</p>
 
49
<p>Note that the file extension is <tt>.ts</tt>, not <tt>.qm</tt>. The <tt>.ts</tt> translation source format is designed for use during the application's development. Programmers or release managers run the <tt>lupdate</tt> program to generate and update <tt>.ts</tt> files with the source text that is extracted from the source code. Translators read and update the <tt>.ts</tt> files using <i>Qt Linguist</i> adding and editing their translations.</p>
 
50
<p>The <tt>.ts</tt> format is human-readable XML that can be emailed directly and is easy to put under version control. If you edit this file manually, be aware that the default encoding for XML is UTF-8, not Latin1 (ISO 8859-1). One way to type in a Latin1 character such as '�' (Norwegian o with slash) is to use an XML entity: &quot;&amp;#xf8;&quot;. This will work for any Unicode 4.0 character.</p>
 
51
<p>Once the translations are complete the <tt>lrelease</tt> program is used to convert the <tt>.ts</tt> files into the <tt>.qm</tt> Qt message file format. The <tt>.qm</tt> format is a compact binary format designed to deliver very fast lookup performance. Both <tt>lupdate</tt> and <tt>lrelease</tt> read all the project's source and header files (as specified in the HEADERS and SOURCES lines of the project file) and extract the strings that appear in <tt>tr()</tt> function calls.</p>
 
52
<p><tt>lupdate</tt> is used to create and update the message files (<tt>hellotr_la.ts</tt> in this case) to keep them in sync with the source code. It is safe to run <tt>lupdate</tt> at any time, as <tt>lupdate</tt> does not remove any information. For example, you can put it in the makefile, so the <tt>.ts</tt> files are updated whenever the source changes.</p>
 
53
<p>Try running <tt>lupdate</tt> right now, like this:</p>
 
54
<pre>&nbsp;   lupdate -verbose hellotr.pro</pre>
 
55
<p>(The <tt>-verbose</tt> option instructs <tt>lupdate</tt> to display messages that explain what it is doing.) You should now have a file <tt>hellotr_la.ts</tt> in the current directory, containing this:</p>
 
56
<pre>&nbsp;   &lt;!DOCTYPE TS&gt;&lt;TS&gt;
 
57
    &lt;context&gt;
 
58
        &lt;name&gt;QPushButton&lt;/name&gt;
 
59
        &lt;message&gt;
 
60
            &lt;source&gt;Hello world!&lt;/source&gt;
 
61
            &lt;translation type=&quot;unfinished&quot;&gt;&lt;/translation&gt;
 
62
        &lt;/message&gt;
 
63
    &lt;/context&gt;
 
64
    &lt;/TS&gt;</pre>
 
65
<p>You don't need to understand the file format since it is read and updated using tools (<tt>lupdate</tt>, <i>Qt Linguist</i>, <tt>lrelease</tt>).</p>
 
66
<a name="translating-to-latin-with-qt-linguist"></a>
 
67
<h2>Translating to Latin with Qt Linguist</h2>
 
68
<p>We will use <i>Qt Linguist</i> to provide the translation, although you can use any XML or plain text editor to enter a translation into a <tt>.ts</tt> file.</p>
 
69
<p>To start <i>Qt Linguist</i>, type</p>
 
70
<pre>&nbsp;   linguist hellotr_la.ts</pre>
 
71
<p>You should now see the text &quot;<a href="qpushbutton.html">QPushButton</a>&quot; in the top left pane. Double-click it, then click on &quot;Hello world!&quot; and enter &quot;Orbis, te saluto!&quot; in the <b>Translation</b> pane (the middle right of the window). Don't forget the exclamation mark!</p>
 
72
<p>Click the <b>Done</b> checkbox and choose <b>File|Save</b> from the menu bar. The <tt>.ts</tt> file will no longer contain</p>
 
73
<pre>&nbsp;   &lt;translation type='unfinished'&gt;&lt;/translation&gt;</pre>
 
74
<p>but instead will have</p>
 
75
<pre>&nbsp;   &lt;translation&gt;Orbis, te saluto!&lt;/translation&gt;</pre>
 
76
<a name="running-the-application-in-latin"></a>
 
77
<h2>Running the Application in Latin</h2>
 
78
<p>To see the application running in Latin, we have to generate a <tt>.qm</tt> file from the <tt>.ts</tt> file. Generating a <tt>.qm</tt> file can be achieved either from within <i>Qt Linguist</i> (for a single <tt>.ts</tt> file), or by using the command line program <tt>lrelease</tt> which will produce one <tt>.qm</tt> file for each of the <tt>.ts</tt> files listed in the project file. Generate <tt>hellotr_la.qm</tt> from <tt>hellotr_la.ts</tt> by choosing <b>File|Release</b> from <i>Qt Linguist</i>'s menu bar and pressing <b>Save</b> in the file save dialog that pops up. Now run the <tt>hellotr</tt> program again. This time the button will be labelled &quot;Orbis, te saluto!&quot;.</p>
 
79
<center><img src="images/linguist-hellotr_la.png" /></center><p /><address><hr /><div align="center">
 
80
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
81
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
82
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
83
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
84
</tr></table></div></address></body>
 
85
</html>