Sophie

Sophie

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

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" />
<!-- i18n.qdoc -->
  <title>Qt 4.8: Internationalization with Qt</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>Internationalization with Qt</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">
  <link rel="next" href="i18n-source-translation.html" />
<p class="naviNextPrevious headerNavi">
<a class="nextPage" href="i18n-source-translation.html">Writing Source Code for Translation</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#relevant-qt-classes-and-apis">Relevant Qt Classes and APIs</a></li>
<li class="level1"><a href="#languages-and-writing-systems">Languages and Writing Systems</a></li>
<li class="level1"><a href="#step-by-step">Step by Step</a></li>
<li class="level2"><a href="#use-qstring-for-all-user-visible-text">Use QString for All User-Visible Text</a></li>
<li class="level2"><a href="#use-tr-for-all-literal-text">Use tr() for All Literal Text</a></li>
<li class="level2"><a href="#use-qkeysequence-for-accelerator-values">Use QKeySequence() for Accelerator Values</a></li>
<li class="level2"><a href="#use-qstring-arg-for-dynamic-text">Use QString::arg() for Dynamic Text</a></li>
<li class="level2"><a href="#produce-translations">Produce Translations</a></li>
<li class="level2"><a href="#support-for-encodings">Support for Encodings</a></li>
<li class="level2"><a href="#localize">Localize</a></li>
<li class="level1"><a href="#dynamic-translation">Dynamic Translation</a></li>
<li class="level1"><a href="#translating-non-qt-classes">Translating Non-Qt Classes</a></li>
<li class="level1"><a href="#system-support">System Support</a></li>
<li class="level2"><a href="#unix-x11">Unix/X11</a></li>
<li class="level2"><a href="#windows">Windows</a></li>
<li class="level2"><a href="#mac-os-x">Mac OS X</a></li>
</ul>
</div>
<h1 class="title">Internationalization with Qt</h1>
<span class="subtitle"></span>
<!-- $$$internationalization-description -->
<div class="descr"> <a name="details"></a>
<a name="internationalization"></a><a name="i18n"></a><p>The internationalization of an application is the process of making the application usable by people in countries other than one's own.</p>
<a name="relevant-qt-classes-and-apis"></a>
<h2>Relevant Qt Classes and APIs</h2>
<p>These classes support internationalizing of Qt applications.</p>
<table class="annotated">
<tr class="odd topAlign"><td class="tblName"><p><a href="qinputcontext.html">QInputContext</a></p></td><td class="tblDescr"><p>Abstracts the input method dependent data and composing state</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qlocale.html">QLocale</a></p></td><td class="tblDescr"><p>Converts between numbers and their string representations in various languages</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qsystemlocale.html">QSystemLocale</a></p></td><td class="tblDescr"><p>Can be used to finetune the system locale of the user</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qtextcodec.html">QTextCodec</a></p></td><td class="tblDescr"><p>Conversions between text encodings</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qtextdecoder.html">QTextDecoder</a></p></td><td class="tblDescr"><p>State-based decoder</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qtextencoder.html">QTextEncoder</a></p></td><td class="tblDescr"><p>State-based encoder</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qtranslator.html">QTranslator</a></p></td><td class="tblDescr"><p>Internationalization support for text output</p></td></tr>
</table>
<a name="languages-and-writing-systems"></a>
<h2>Languages and Writing Systems</h2>
<p>In some cases internationalization is simple, for example, making a US application accessible to Australian or British users may require little more than a few spelling corrections. But to make a US application usable by Japanese users, or a Korean application usable by German users, will require that the software operate not only in different languages, but use different input techniques, character encodings and presentation conventions.</p>
<p>Qt tries to make internationalization as painless as possible for developers. All input widgets and text drawing methods in Qt offer built-in support for all supported languages. The built-in font engine is capable of correctly and attractively rendering text that contains characters from a variety of different writing systems at the same time.</p>
<p>Qt supports most languages in use today, in particular:</p>
<ul>
<li>All East Asian languages (Chinese, Japanese and Korean)</li>
<li>All Western languages (using Latin script)</li>
<li>Arabic</li>
<li>Cyrillic languages (Russian, Ukrainian, etc.)</li>
<li>Greek</li>
<li>Hebrew</li>
<li>Thai and Lao</li>
<li>All scripts in Unicode 5.1 that do not require special processing</li>
</ul>
<p>On Windows, Unix/X11 with FontConfig (client side font support) and Qt for Embedded Linux the following languages are also supported:</p>
<ul>
<li>Bengali</li>
<li>Devanagari</li>
<li>Dhivehi (Thaana)</li>
<li>Gujarati</li>
<li>Gurmukhi</li>
<li>Kannada</li>
<li>Khmer</li>
<li>Malayalam</li>
<li>Myanmar</li>
<li>Syriac</li>
<li>Tamil</li>
<li>Telugu</li>
<li>Tibetan</li>
<li>N'Ko</li>
</ul>
<p>Many of these writing systems exhibit special features:</p>
<ul>
<li><b>Special line breaking behavior.</b> Some of the Asian languages are written without spaces between words. Line breaking can occur either after every character (with exceptions) as in Chinese, Japanese and Korean, or after logical word boundaries as in Thai.</li>
<li><b>Bidirectional writing.</b> Arabic and Hebrew are written from right to left, except for numbers and embedded English text which is written left to right. The exact behavior is defined in the <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Annex #9</a>.</li>
<li><b>Non-spacing or diacritical marks (accents or umlauts in European languages).</b> Some languages such as Vietnamese make extensive use of these marks and some characters can have more than one mark at the same time to clarify pronunciation.</li>
<li><b>Ligatures.</b> In special contexts, some pairs of characters get replaced by a combined glyph forming a ligature. Common examples are the fl and fi ligatures used in typesetting US and European books.</li>
</ul>
<p>Qt tries to take care of all the special features listed above. You usually don't have to worry about these features so long as you use Qt's input widgets (e.g&#x2e; <a href="qlineedit.html">QLineEdit</a>, <a href="qtextedit.html">QTextEdit</a>, and derived classes) and Qt's display widgets (e.g&#x2e; <a href="qlabel.html">QLabel</a>).</p>
<p>Support for these writing systems is transparent to the programmer and completely encapsulated in <a href="richtext.html">Qt's text engine</a>. This means that you don't need to have any knowledge about the writing system used in a particular language, except for the following small points:</p>
<ul>
<li>QPainter::drawText(int x, int y, const <a href="qstring.html">QString</a> &amp;str) will always draw the string with its left edge at the position specified with the x, y parameters. This will usually give you left aligned strings. Arabic and Hebrew application strings are usually right aligned, so for these languages use the version of drawText() that takes a <a href="qrect.html">QRect</a> since this will align in accordance with the language.</li>
<li>When you write your own text input controls, use <a href="qtextlayout.html">QTextLayout</a>. In some languages (e.g&#x2e; Arabic or languages from the Indian subcontinent), the width and shape of a glyph changes depending on the surrounding characters, which <a href="qtextlayout.html">QTextLayout</a> takes into account. Writing input controls usually requires a certain knowledge of the scripts it is going to be used in. Usually the easiest way is to subclass <a href="qlineedit.html">QLineEdit</a> or <a href="qtextedit.html">QTextEdit</a>.</li>
</ul>
<p>The following sections give some information on the status of the internationalization (i18n) support in Qt. See also the <a href="linguist-manual.html">Qt Linguist manual</a>.</p>
<a name="step-by-step"></a>
<h2>Step by Step</h2>
<p>Writing cross-platform international software with Qt is a gentle, incremental process. Your software can become internationalized in the following stages:</p>
<a name="use-qstring-for-all-user-visible-text"></a>
<h3>Use QString for All User-Visible Text</h3>
<p>Since <a href="qstring.html">QString</a> uses the Unicode 5.1 encoding internally, every language in the world can be processed transparently using familiar text processing operations. Also, since all Qt functions that present text to the user take a <a href="qstring.html">QString</a> as a parameter, there is no <tt>char *</tt> to <a href="qstring.html">QString</a> conversion overhead.</p>
<p>Strings that are in &quot;programmer space&quot; (such as <a href="qobject.html">QObject</a> names and file format texts) need not use <a href="qstring.html">QString</a>; the traditional <tt>char *</tt> or the <a href="qbytearray.html">QByteArray</a> class will suffice.</p>
<p>You're unlikely to notice that you are using Unicode; <a href="qstring.html">QString</a>, and <a href="qchar.html">QChar</a> are just like easier versions of the crude <tt>const char *</tt> and char from traditional C.</p>
<a name="use-tr-for-all-literal-text"></a>
<h3>Use tr() for All Literal Text</h3>
<p>Wherever your program uses &quot;quoted text&quot; for text that will be presented to the user, ensure that it is processed by the <a href="qcoreapplication.html#translate">QCoreApplication::translate</a>() function. Essentially all that is necessary to achieve this is to use <a href="qobject.html#tr">QObject::tr</a>(). For example, assuming the <tt>LoginWidget</tt> is a subclass of <a href="qwidget.html">QWidget</a>:</p>
<pre class="cpp"> LoginWidget<span class="operator">::</span>LoginWidget()
 {
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Password:&quot;</span>));
     <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
 }</pre>
<p>This accounts for 99% of the user-visible strings you're likely to write.</p>
<p>If the quoted text is not in a member function of a <a href="qobject.html">QObject</a> subclass, use either the tr() function of an appropriate class, or the <a href="qcoreapplication.html#translate">QCoreApplication::translate</a>() function directly:</p>
<pre class="cpp"> <span class="type">void</span> some_global_function(LoginWidget <span class="operator">*</span>logwid)
 {
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(
                 LoginWidget<span class="operator">::</span>tr(<span class="string">&quot;Password:&quot;</span>)<span class="operator">,</span> logwid);
 }

 <span class="type">void</span> same_global_function(LoginWidget <span class="operator">*</span>logwid)
 {
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(
                 qApp<span class="operator">-</span><span class="operator">&gt;</span>translate(<span class="string">&quot;LoginWidget&quot;</span><span class="operator">,</span> <span class="string">&quot;Password:&quot;</span>)<span class="operator">,</span> logwid);
 }</pre>
<p>If you need to have translatable text completely outside a function, there are two macros to help: <a href="qtglobal.html#QT_TR_NOOP">QT_TR_NOOP</a>() and <a href="qtglobal.html#QT_TRANSLATE_NOOP">QT_TRANSLATE_NOOP</a>(). They merely mark the text for extraction by the <tt>lupdate</tt> utility described below. The macros expand to just the text (without the context).</p>
<p>Example of <a href="qtglobal.html#QT_TR_NOOP">QT_TR_NOOP</a>():</p>
<pre class="cpp"> <span class="type"><a href="qstring.html">QString</a></span> FriendlyConversation<span class="operator">::</span>greeting(<span class="type">int</span> type)
 {
     <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>greeting_strings<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
         QT_TR_NOOP(<span class="string">&quot;Hello&quot;</span>)<span class="operator">,</span>
         QT_TR_NOOP(<span class="string">&quot;Goodbye&quot;</span>)
     };
     <span class="keyword">return</span> tr(greeting_strings<span class="operator">[</span>type<span class="operator">]</span>);
 }</pre>
<p>Example of <a href="qtglobal.html#QT_TRANSLATE_NOOP">QT_TRANSLATE_NOOP</a>():</p>
<pre class="cpp"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>greeting_strings<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
     QT_TRANSLATE_NOOP(<span class="string">&quot;FriendlyConversation&quot;</span><span class="operator">,</span> <span class="string">&quot;Hello&quot;</span>)<span class="operator">,</span>
     QT_TRANSLATE_NOOP(<span class="string">&quot;FriendlyConversation&quot;</span><span class="operator">,</span> <span class="string">&quot;Goodbye&quot;</span>)
 };

 <span class="type"><a href="qstring.html">QString</a></span> FriendlyConversation<span class="operator">::</span>greeting(<span class="type">int</span> type)
 {
     <span class="keyword">return</span> tr(greeting_strings<span class="operator">[</span>type<span class="operator">]</span>);
 }

 <span class="type"><a href="qstring.html">QString</a></span> global_greeting(<span class="type">int</span> type)
 {
     <span class="keyword">return</span> qApp<span class="operator">-</span><span class="operator">&gt;</span>translate(<span class="string">&quot;FriendlyConversation&quot;</span><span class="operator">,</span>
                            greeting_strings<span class="operator">[</span>type<span class="operator">]</span>);
 }</pre>
<p>If you disable the <tt>const char *</tt> to <a href="qstring.html">QString</a> automatic conversion by compiling your software with the macro <tt>QT_NO_CAST_FROM_ASCII</tt> defined, you'll be very likely to catch any strings you are missing. See <a href="qstring.html#fromLatin1">QString::fromLatin1</a>() for more information. Disabling the conversion can make programming a bit cumbersome.</p>
<p>If your source language uses characters outside Latin1, you might find <a href="qobject.html#trUtf8">QObject::trUtf8</a>() more convenient than <a href="qobject.html#tr">QObject::tr</a>(), as tr() depends on the <a href="qtextcodec.html#codecForTr">QTextCodec::codecForTr</a>(), which makes it more fragile than <a href="qobject.html#trUtf8">QObject::trUtf8</a>().</p>
<a name="use-qkeysequence-for-accelerator-values"></a>
<h3>Use QKeySequence() for Accelerator Values</h3>
<p>Accelerator values such as Ctrl+Q or Alt+F need to be translated too. If you hardcode <a href="qt.html#Modifier-enum">Qt::CTRL</a> + <a href="qt.html#Key-enum">Qt::Key_Q</a> for &quot;quit&quot; in your application, translators won't be able to override it. The correct idiom is</p>
<pre class="cpp">     exitAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
     exitAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Quit);</pre>
<a name="use-qstring-arg-for-dynamic-text"></a>
<h3>Use QString::arg() for Dynamic Text</h3>
<p>The <a href="qstring.html#arg">QString::arg</a>() functions offer a simple means for substituting arguments:</p>
<pre class="cpp"> <span class="type">void</span> FileCopier<span class="operator">::</span>showProgress(<span class="type">int</span> done<span class="operator">,</span> <span class="type">int</span> total<span class="operator">,</span>
                               <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>currentFile)
 {
     label<span class="operator">.</span>setText(tr(<span class="string">&quot;%1 of %2 files copied.\nCopying: %3&quot;</span>)
                   <span class="operator">.</span>arg(done)
                   <span class="operator">.</span>arg(total)
                   <span class="operator">.</span>arg(currentFile));
 }</pre>
<p>In some languages the order of arguments may need to change, and this can easily be achieved by changing the order of the % arguments. For example:</p>
<pre class="cpp"> <span class="type"><a href="qstring.html">QString</a></span> s1 <span class="operator">=</span> <span class="string">&quot;%1 of %2 files copied. Copying: %3&quot;</span>;
 <span class="type"><a href="qstring.html">QString</a></span> s2 <span class="operator">=</span> <span class="string">&quot;Kopierer nu %3. Av totalt %2 filer er %1 kopiert.&quot;</span>;

 <a href="qtglobal.html#qDebug">qDebug</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> s1<span class="operator">.</span>arg(<span class="number">5</span>)<span class="operator">.</span>arg(<span class="number">10</span>)<span class="operator">.</span>arg(<span class="string">&quot;somefile.txt&quot;</span>);
 <a href="qtglobal.html#qDebug">qDebug</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> s2<span class="operator">.</span>arg(<span class="number">5</span>)<span class="operator">.</span>arg(<span class="number">10</span>)<span class="operator">.</span>arg(<span class="string">&quot;somefile.txt&quot;</span>);</pre>
<p>produces the correct output in English and Norwegian:</p>
<pre class="cpp"> 5 of 10 files copied. Copying: somefile.txt
 Kopierer nu somefile.txt. Av totalt 10 filer er 5 kopiert.</pre>
<a name="produce-translations"></a>
<h3>Produce Translations</h3>
<p>Once you are using tr() throughout an application, you can start producing translations of the user-visible text in your program.</p>
<p>The <a href="linguist-manual.html">Qt Linguist manual</a> provides further information about Qt's translation tools, <i>Qt Linguist</i>, <tt>lupdate</tt> and <tt>lrelease</tt>.</p>
<p>Translation of a Qt application is a three-step process:</p>
<ol class="1">
<li>Run <tt>lupdate</tt> to extract translatable text from the C++ source code of the Qt application, resulting in a message file for translators (a TS file). The utility recognizes the tr() construct and the <tt>QT_TR*_NOOP()</tt> macros described above and produces TS files (usually one per language).</li>
<li>Provide translations for the source texts in the TS file, using <i>Qt Linguist</i>. Since TS files are in XML format, you can also edit them by hand.</li>
<li>Run <tt>lrelease</tt> to obtain a light-weight message file (a QM file) from the TS file, suitable only for end use. Think of the TS files as &quot;source files&quot;, and QM files as &quot;object files&quot;. The translator edits the TS files, but the users of your application only need the QM files. Both kinds of files are platform and locale independent.</li>
</ol>
<p>Typically, you will repeat these steps for every release of your application. The <tt>lupdate</tt> utility does its best to reuse the translations from previous releases.</p>
<p>Before you run <tt>lupdate</tt>, you should prepare a project file. Here's an example project file (<tt>.pro</tt> file):</p>
<pre class="cpp"> HEADERS         = funnydialog.h \
                   wackywidget.h
 SOURCES         = funnydialog.cpp \
                   main.cpp \
                   wackywidget.cpp
 FORMS           = fancybox.ui
 TRANSLATIONS    = superapp_dk.ts \
                   superapp_fi.ts \
                   superapp_no.ts \
                   superapp_se.ts</pre>
<p>When you run <tt>lupdate</tt> or <tt>lrelease</tt>, you must give the name of the project file as a command-line argument.</p>
<p>In this example, four exotic languages are supported: Danish, Finnish, Norwegian and Swedish. If you use <a href="qmake-manual.html#qmake">qmake</a>, you usually don't need an extra project file for <tt>lupdate</tt>; your <tt>qmake</tt> project file will work fine once you add the <tt>TRANSLATIONS</tt> entry.</p>
<p>In your application, you must <a href="qtranslator.html#load">QTranslator::load</a>() the translation files appropriate for the user's language, and install them using <a href="qcoreapplication.html#installTranslator">QCoreApplication::installTranslator</a>().</p>
<p><tt>linguist</tt>, <tt>lupdate</tt> and <tt>lrelease</tt> are installed in the <tt>bin</tt> subdirectory of the base directory Qt is installed into. Click Help|Manual in <i>Qt Linguist</i> to access the user's manual; it contains a tutorial to get you started.</p>
<a name="qt-itself"></a><p>Qt itself contains over 400 strings that will also need to be translated into the languages that you are targeting. You will find translation files for French, German and Simplified Chinese in <tt>$QTDIR/translations</tt>, as well as a template for translating to other languages. (This directory also contains some additional unsupported translations which may be useful.)</p>
<p>Typically, your application's <tt>main()</tt> function will look like this:</p>
<pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

     <span class="type"><a href="qtranslator.html">QTranslator</a></span> qtTranslator;
     qtTranslator<span class="operator">.</span>load(<span class="string">&quot;qt_&quot;</span> <span class="operator">+</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>system()<span class="operator">.</span>name()<span class="operator">,</span>
             <span class="type"><a href="qlibraryinfo.html">QLibraryInfo</a></span><span class="operator">::</span>location(<span class="type"><a href="qlibraryinfo.html">QLibraryInfo</a></span><span class="operator">::</span>TranslationsPath));
     app<span class="operator">.</span>installTranslator(<span class="operator">&amp;</span>qtTranslator);

     <span class="type"><a href="qtranslator.html">QTranslator</a></span> myappTranslator;
     myappTranslator<span class="operator">.</span>load(<span class="string">&quot;myapp_&quot;</span> <span class="operator">+</span> <span class="type"><a href="qlocale.html">QLocale</a></span><span class="operator">::</span>system()<span class="operator">.</span>name());
     app<span class="operator">.</span>installTranslator(<span class="operator">&amp;</span>myappTranslator);

     <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>Note the use of <a href="qlibraryinfo.html#location">QLibraryInfo::location</a>() to locate the Qt translations. Developers should request the path to the translations at run-time by passing <a href="qlibraryinfo.html#LibraryLocation-enum">QLibraryInfo::TranslationsPath</a> to this function instead of using the <tt>QTDIR</tt> environment variable in their applications.</p>
<a name="support-for-encodings"></a>
<h3>Support for Encodings</h3>
<p>The <a href="qtextcodec.html">QTextCodec</a> class and the facilities in <a href="qtextstream.html">QTextStream</a> make it easy to support many input and output encodings for your users' data. When an application starts, the locale of the machine will determine the 8-bit encoding used when dealing with 8-bit data: such as for font selection, text display, 8-bit text I/O, and character input.</p>
<p>The application may occasionally require encodings other than the default local 8-bit encoding. For example, an application in a Cyrillic KOI8-R locale (the de-facto standard locale in Russia) might need to output Cyrillic in the ISO 8859-5 encoding. Code for this would be:</p>
<pre class="cpp"> <span class="type"><a href="qstring.html">QString</a></span> string <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="comment">// some Unicode text</span>

 <span class="type"><a href="qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec <span class="operator">=</span> <span class="type"><a href="qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>codecForName(<span class="string">&quot;ISO 8859-5&quot;</span>);
 <span class="type"><a href="qbytearray.html">QByteArray</a></span> encodedString <span class="operator">=</span> codec<span class="operator">-</span><span class="operator">&gt;</span>fromUnicode(string);</pre>
<p>For converting Unicode to local 8-bit encodings, a shortcut is available: the <a href="qstring.html#toLocal8Bit">QString::toLocal8Bit</a>() function returns such 8-bit data. Another useful shortcut is <a href="qstring.html#toUtf8">QString::toUtf8</a>(), which returns text in the 8-bit UTF-8 encoding: this perfectly preserves Unicode information while looking like plain ASCII if the text is wholly ASCII.</p>
<p>For converting the other way, there are the <a href="qstring.html#fromUtf8">QString::fromUtf8</a>() and <a href="qstring.html#fromLocal8Bit">QString::fromLocal8Bit</a>() convenience functions, or the general code, demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode conversion:</p>
<pre class="cpp"> <span class="type"><a href="qbytearray.html">QByteArray</a></span> encodedString <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="comment">// some ISO 8859-5 encoded text</span>

 <span class="type"><a href="qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec <span class="operator">=</span> <span class="type"><a href="qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>codecForName(<span class="string">&quot;ISO 8859-5&quot;</span>);
 <span class="type"><a href="qstring.html">QString</a></span> string <span class="operator">=</span> codec<span class="operator">-</span><span class="operator">&gt;</span>toUnicode(encodedString);</pre>
<p>Ideally Unicode I/O should be used as this maximizes the portability of documents between users around the world, but in reality it is useful to support all the appropriate encodings that your users will need to process existing documents. In general, Unicode (UTF-16 or UTF-8) is best for information transferred between arbitrary people, while within a language or national group, a local standard is often more appropriate. The most important encoding to support is the one returned by <a href="qtextcodec.html#codecForLocale">QTextCodec::codecForLocale</a>(), as this is the one the user is most likely to need for communicating with other people and applications (this is the codec used by local8Bit()).</p>
<p>Qt supports most of the more frequently used encodings natively. For a complete list of supported encodings see the <a href="qtextcodec.html">QTextCodec</a> documentation.</p>
<p>In some cases and for less frequently used encodings it may be necessary to write your own <a href="qtextcodec.html">QTextCodec</a> subclass. Depending on the urgency, it may be useful to contact Qt's technical support team or ask on the <tt>qt-interest</tt> mailing list to see if someone else is already working on supporting the encoding.</p>
<a name="localization"></a><a name="localize"></a>
<h3>Localize</h3>
<p>Localization is the process of adapting to local conventions, for example presenting dates and times using the locally preferred formats. Such localizations can be accomplished using appropriate tr() strings.</p>
<pre class="cpp"> <span class="type">void</span> Clock<span class="operator">::</span>setTime(<span class="keyword">const</span> <span class="type"><a href="qtime.html">QTime</a></span> <span class="operator">&amp;</span>time)
 {
     <span class="keyword">if</span> (tr(<span class="string">&quot;AMPM&quot;</span>) <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;AMPM&quot;</span>) {
         <span class="comment">// 12-hour clock</span>
     } <span class="keyword">else</span> {
         <span class="comment">// 24-hour clock</span>
     }
 }</pre>
<p>In the example, for the US we would leave the translation of &quot;AMPM&quot; as it is and thereby use the 12-hour clock branch; but in Europe we would translate it as something else and this will make the code use the 24-hour clock branch.</p>
<p>For localized numbers use the <a href="qlocale.html">QLocale</a> class.</p>
<p>Localizing images is not recommended. Choose clear icons that are appropriate for all localities, rather than relying on local puns or stretched metaphors. The exception is for images of left and right pointing arrows which may need to be reversed for Arabic and Hebrew locales.</p>
<a name="dynamic-translation"></a>
<h2>Dynamic Translation</h2>
<p>Some applications, such as Qt Linguist, must be able to support changes to the user's language settings while they are still running. To make widgets aware of changes to the installed QTranslators, reimplement the widget's <a href="qwidget.html#changeEvent">changeEvent()</a> function to check whether the event is a <a href="qevent.html#Type-enum">LanguageChange</a> event, and update the text displayed by widgets using the <a href="qobject.html#tr">tr()</a> function in the usual way. For example:</p>
<pre class="cpp"> <span class="type">void</span> MyWidget<span class="operator">::</span>changeEvent(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>event)
 {
     <span class="keyword">if</span> (e<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>LanguageChange) {
         titleLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Document Title&quot;</span>));
         <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
         okPushButton<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;&amp;OK&quot;</span>));
     } <span class="keyword">else</span>
         <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>changeEvent(event);
 }</pre>
<p>All other change events should be passed on by calling the default implementation of the function.</p>
<p>The list of installed translators might change in reaction to a <a href="qevent.html#Type-enum">LocaleChange</a> event, or the application might provide a user interface that allows the user to change the current application language.</p>
<p>The default event handler for <a href="qwidget.html">QWidget</a> subclasses responds to the <a href="qevent.html#Type-enum">QEvent::LanguageChange</a> event, and will call this function when necessary.</p>
<p><a href="qevent.html#Type-enum">LanguageChange</a> events are posted when a new translation is installed using the <a href="qcoreapplication.html#installTranslator">QCoreApplication::installTranslator</a>() function. Additionally, other application components can also force widgets to update themselves by posting LanguageChange events to them.</p>
<a name="translating-non-qt-classes"></a>
<h2>Translating Non-Qt Classes</h2>
<p>It is sometimes necessary to provide internationalization support for strings used in classes that do not inherit <a href="qobject.html">QObject</a> or use the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro to enable translation features. Since Qt translates strings at run-time based on the class they are associated with and <tt>lupdate</tt> looks for translatable strings in the source code, non-Qt classes must use mechanisms that also provide this information.</p>
<p>One way to do this is to add translation support to a non-Qt class using the <a href="qcoreapplication.html#Q_DECLARE_TR_FUNCTIONS">Q_DECLARE_TR_FUNCTIONS</a>() macro; for example:</p>
<pre class="cpp"> <span class="keyword">class</span> MyClass
 {
     Q_DECLARE_TR_FUNCTIONS(MyClass)

 <span class="keyword">public</span>:
     MyClass();
     ...
 };</pre>
<p>This provides the class with <a href="qobject.html#tr">tr()</a> functions that can be used to translate strings associated with the class, and makes it possible for <tt>lupdate</tt> to find translatable strings in the source code.</p>
<p>Alternatively, the <a href="qcoreapplication.html#translate">QCoreApplication::translate</a>() function can be called with a specific context, and this will be recognized by <tt>lupdate</tt> and Qt Linguist.</p>
<a name="system-support"></a>
<h2>System Support</h2>
<p>Some of the operating systems and windowing systems that Qt runs on only have limited support for Unicode. The level of support available in the underlying system has some influence on the support that Qt can provide on those platforms, although in general Qt applications need not be too concerned with platform-specific limitations.</p>
<a name="unix-x11"></a>
<h3>Unix/X11</h3>
<ul>
<li>Locale-oriented fonts and input methods. Qt hides these and provides Unicode input and output.</li>
<li>Filesystem conventions such as <a href="http://www.ietf.org/rfc/rfc2279.txt">UTF-8</a> are under development in some Unix variants. All Qt file functions allow Unicode, but convert filenames to the local 8-bit encoding, as this is the Unix convention (see <a href="qfile.html#setEncodingFunction">QFile::setEncodingFunction</a>() to explore alternative encodings).</li>
<li>File I/O defaults to the local 8-bit encoding, with Unicode options in <a href="qtextstream.html">QTextStream</a>.</li>
<li>Many Unix distributions contain only partial support for some locales. For example, if you have a <tt>/usr/share/locale/ja_JP.EUC</tt> directory, this does not necessarily mean you can display Japanese text; you also need JIS encoded fonts (or Unicode fonts), and the <tt>/usr/share/locale/ja_JP.EUC</tt> directory needs to be complete. For best results, use complete locales from your system vendor.</li>
</ul>
<a name="windows"></a>
<h3>Windows</h3>
<ul>
<li>Qt provides full Unicode support, including input methods, fonts, clipboard, drag-and-drop and file names.</li>
<li>File I/O defaults to Latin1, with Unicode options in <a href="qtextstream.html">QTextStream</a>. Note that some Windows programs do not understand big-endian Unicode text files even though that is the order prescribed by the Unicode Standard in the absence of higher-level protocols.</li>
</ul>
<a name="mac-os-x"></a>
<h3>Mac OS X</h3>
<p>For details on Mac-specific translation, refer to the Qt/Mac Specific Issues document <a href="mac-differences.html#translating-the-application-menu-and-native-dialogs">here</a>.</p>
</div>
<!-- @@@internationalization -->
<table class="annotated">
</table>
<p class="naviNextPrevious footerNavi">
<a class="nextPage" href="i18n-source-translation.html">Writing Source Code for Translation</a>
</p>
      </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>