Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 3940

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>Qt 4.8: Hello tr() Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Hello tr() Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <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>
<!-- $$$linguist/hellotr-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="linguist-hellotr-main-cpp.html">linguist/hellotr/main.cpp</a></li>
<li><a href="linguist-hellotr-hellotr-pro.html">linguist/hellotr/hellotr.pro</a></li>
</ul>
<p>The Hello tr() example is a small Hello World program with a Latin translation.<p>The screenshot below shows the English version.</p>
<p class="centerAlign"><img src="images/linguist-hellotr_en.png" alt="" /></p><p>See the <a href="linguist-manual.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 &lt;QTranslator&gt;</span></pre>
<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>
<pre class="cpp">     <span class="type"><a href="qtranslator.html">QTranslator</a></span> translator;</pre>
<p>Creates a <a href="qtranslator.html">QTranslator</a> object without a parent.</p>
<pre class="cpp">     translator<span class="operator">.</span>load(<span class="string">&quot;hellotr_la&quot;</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">&amp;</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"><a href="qpushbutton.html">QPushButton</a></span> hello(<span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">::</span>tr(<span class="string">&quot;Hello world!&quot;</span>));</pre>
<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>
<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>
<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 '&oslash;' (Norwegian o with slash) is to use an XML entity: &quot;&amp;#xf8;&quot;. 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 -verbose hellotr.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"> &lt;!DOCTYPE TS&gt;&lt;TS&gt;
 &lt;context&gt;
     &lt;name&gt;QPushButton&lt;/name&gt;
     &lt;message&gt;
         &lt;source&gt;Hello world!&lt;/source&gt;
         &lt;translation type=&quot;unfinished&quot;&gt;&lt;/translation&gt;
     &lt;/message&gt;
 &lt;/context&gt;
 &lt;/TS&gt;</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.ts</pre>
<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>
<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"> &lt;translation type='unfinished'&gt;&lt;/translation&gt;</pre>
<p>but instead will have</p>
<pre class="cpp"> &lt;translation&gt;Orbis, te saluto!&lt;/translation&gt;</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 &quot;Orbis, te saluto!&quot;.</p>
<p class="centerAlign"><img src="images/linguist-hellotr_la.png" alt="" /></p></div>
<!-- @@@linguist/hellotr -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      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.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      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. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>