/usr/share/qt5/doc/qtlinguist/qtlinguist-hellotr-example.html is in qttools5-doc-html 5.2.1-8build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- hellotr.qdoc -->
<title>Hello tr() Example | QtLinguist </title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.2</li>
<li><a href="qtlinguist-index.html">Qt Linguist Manual</a></li>
<li>Hello tr() Example</li>
<li id="buildversion">
Qt 5.2.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#line-by-line-walkthrough">Line by Line Walkthrough</a></li>
<li class="level1"><a href="#running-the-application-in-english">Running the Application in English</a></li>
<li class="level1"><a href="#creating-a-latin-message-file">Creating a Latin Message File</a></li>
<li class="level1"><a href="#translating-to-latin-with-qt-linguist">Translating to Latin with Qt Linguist</a></li>
<li class="level1"><a href="#running-the-application-in-latin">Running the Application in Latin</a></li>
</ul>
</div>
<h1 class="title">Hello tr() Example</h1>
<span class="subtitle"></span>
<!-- $$$hellotr-description -->
<div class="descr"> <a name="details"></a>
<p>Translating a small Hello World program to Latin.<p>The screenshot shows the English version.</p>
<p class="centerAlign"><img src="images/linguist-hellotr_en.png" alt="" /></p><p>See the <a href="qtlinguist-index.html">Qt Linguist Manual</a> for more information about translating Qt application.</p>
<a name="line-by-line-walkthrough"></a>
<h2>Line by Line Walkthrough</h2>
<pre class="cpp"><span class="preprocessor">#include <QTranslator></span></pre>
<p>This line includes the definition of the <a href="../qtcore/qtranslator.html">QTranslator</a> class. Objects of this class provide translations for user-visible text.</p>
<pre class="cpp"> <span class="type"><a href="../qtcore/qtranslator.html">QTranslator</a></span> translator;</pre>
<p>Creates a <a href="../qtcore/qtranslator.html">QTranslator</a> object without a parent.</p>
<pre class="cpp"> translator<span class="operator">.</span>load(<span class="string">"hellotr_la"</span>);</pre>
<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>
<pre class="cpp"> app<span class="operator">.</span>installTranslator(<span class="operator">&</span>translator);</pre>
<p>Adds the translations from <tt>hellotr_la.qm</tt> to the pool of translations used by the program.</p>
<pre class="cpp"> <span class="type">QPushButton</span> hello(<span class="type">QPushButton</span><span class="operator">::</span>tr(<span class="string">"Hello world!"</span>));</pre>
<p>Creates a push button that displays "Hello world!". If <tt>hellotr_la.qm</tt> was found and contains a translation for "Hello world!", the translation appears; if not, the source text appears.</p>
<p>All classes that inherit <a href="../qtcore/qobject.html">QObject</a> have a <tt>tr()</tt> function. Inside a member function of a <a href="../qtcore/qobject.html">QObject</a> class, we simply write <tt>tr("Hello world!")</tt> instead of <tt>QPushButton::tr("Hello world!")</tt> or <tt>QObject::tr("Hello world!")</tt>.</p>
<a name="running-the-application-in-english"></a>
<h2>Running the Application in English</h2>
<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>
<p class="centerAlign"><img src="images/linguist-hellotr_en.png" alt="" /></p><a name="creating-a-latin-message-file"></a>
<h2>Creating a Latin Message File</h2>
<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>
<pre class="cpp">SOURCES = main.cpp
TRANSLATIONS = hellotr_la.ts</pre>
<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>
<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 TS files with the source text that is extracted from the source code. Translators read and update the TS files using <i>Qt Linguist</i> adding and editing their translations.</p>
<p>The TS 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: "&#xf8;". This will work for any Unicode 4.0 character.</p>
<p>Once the translations are complete the <tt>lrelease</tt> program is used to convert the TS files into the QM Qt message file format. The QM 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>
<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 TS files are updated whenever the source changes.</p>
<p>Try running <tt>lupdate</tt> right now, like this:</p>
<pre class="cpp">lupdate <span class="operator">-</span>verbose hellotr<span class="operator">.</span>pro</pre>
<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>
<pre class="cpp"><span class="operator"><</span><span class="operator">!</span>DOCTYPE TS<span class="operator">></span><span class="operator"><</span>TS<span class="operator">></span>
<span class="operator"><</span>context<span class="operator">></span>
<span class="operator"><</span>name<span class="operator">></span><span class="type">QPushButton</span><span class="operator"><</span><span class="operator">/</span>name<span class="operator">></span>
<span class="operator"><</span>message<span class="operator">></span>
<span class="operator"><</span>source<span class="operator">></span>Hello world<span class="operator">!</span><span class="operator"><</span><span class="operator">/</span>source<span class="operator">></span>
<span class="operator"><</span>translation type<span class="operator">=</span><span class="string">"unfinished"</span><span class="operator">></span><span class="operator"><</span><span class="operator">/</span>translation<span class="operator">></span>
<span class="operator"><</span><span class="operator">/</span>message<span class="operator">></span>
<span class="operator"><</span><span class="operator">/</span>context<span class="operator">></span>
<span class="operator"><</span><span class="operator">/</span>TS<span class="operator">></span></pre>
<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>
<a name="translating-to-latin-with-qt-linguist"></a>
<h2>Translating to Latin with Qt Linguist</h2>
<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 TS file.</p>
<p>To start <i>Qt Linguist</i>, type</p>
<pre class="cpp">linguist hellotr_la<span class="operator">.</span>ts</pre>
<p>You should now see the text "QPushButton" in the top left pane. Double-click it, then click on "Hello world!" and enter "Orbis, te saluto!" in the <b>Translation</b> pane (the middle right of the window). Don't forget the exclamation mark!</p>
<p>Click the <b>Done</b> checkbox and choose <b>File|Save</b> from the menu bar. The TS file will no longer contain</p>
<pre class="cpp"><span class="operator"><</span>translation type<span class="operator">=</span><span class="char">'unfinished'</span><span class="operator">></span><span class="operator"><</span><span class="operator">/</span>translation<span class="operator">></span></pre>
<p>but instead will have</p>
<pre class="cpp"><span class="operator"><</span>translation<span class="operator">></span>Orbis<span class="operator">,</span> te saluto<span class="operator">!</span><span class="operator"><</span><span class="operator">/</span>translation<span class="operator">></span></pre>
<a name="running-the-application-in-latin"></a>
<h2>Running the Application in Latin</h2>
<p>To see the application running in Latin, we have to generate a QM file from the TS file. Generating a QM file can be achieved either from within <i>Qt Linguist</i> (for a single TS file), or by using the command line program <tt>lrelease</tt> which will produce one QM file for each of the TS 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 "Orbis, te saluto!".</p>
<p class="centerAlign"><img src="images/linguist-hellotr_la.png" alt="" /></p><p>Files:</p>
<ul>
<li><a href="qtlinguist-hellotr-main-cpp.html">hellotr/main.cpp</a></li>
<li><a href="qtlinguist-hellotr-hellotr-pro.html">hellotr/hellotr.pro</a></li>
</ul>
</div>
<!-- @@@hellotr -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|