Sophie

Sophie

distrib > Mageia > 6 > i586 > by-pkgid > f93881942bd3805980c2fe63aa853d78 > files > 58

qtdoc5-5.9.4-1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- i18n.qdoc -->
  <title>Writing Source Code for Translation | Qt 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.9</a></td><td ><a href="internationalization.html">Internationalization with Qt</a></td><td >Writing Source Code for Translation</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="internationalization.html" />
  <link rel="next" href="i18n-plural-rules.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="internationalization.html">Internationalization with Qt</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="i18n-plural-rules.html">Translation Rules for Plurals</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#using-qstring-for-all-user-visible-text">Using QString for All User-Visible Text</a></li>
<li class="level1"><a href="#using-tr-for-all-literal-text">Using tr() for All Literal Text</a></li>
<li class="level1"><a href="#defining-a-translation-context">Defining a Translation Context</a></li>
<li class="level1"><a href="#using-tr-to-obtain-a-translation">Using tr() to Obtain a Translation</a></li>
<li class="level1"><a href="#using-tr-to-localize-numbers">Using tr() to Localize Numbers</a></li>
<li class="level1"><a href="#translating-non-qt-classes">Translating Non-Qt Classes</a></li>
<li class="level1"><a href="#translator-comments">Translator Comments</a></li>
<li class="level1"><a href="#adding-meta-data-to-strings">Adding Meta-Data to Strings</a></li>
<li class="level1"><a href="#disambiguation">Disambiguation</a></li>
<li class="level1"><a href="#handling-plurals">Handling Plurals</a></li>
<li class="level1"><a href="#translating-text-that-is-outside-of-a-qobject-subclass">Translating Text That is Outside of a QObject Subclass</a></li>
<li class="level2"><a href="#using-qcoreapplication-translate">Using QCoreApplication::translate()</a></li>
<li class="level2"><a href="#using-qt-tr-noop-and-qt-translate-noop-in-c">Using QT_TR_NOOP() and QT_TRANSLATE_NOOP() in C++</a></li>
<li class="level1"><a href="#using-qkeysequence-for-accelerator-values">Using QKeySequence() for Accelerator Values</a></li>
<li class="level1"><a href="#using-numbered-arguments">Using Numbered Arguments</a></li>
<li class="level1"><a href="#further-reading">Further Reading</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Writing Source Code for Translation</h1>
<span class="subtitle"></span>
<!-- $$$i18n-source-translation.html-description -->
<div class="descr"> <a name="details"></a>
<p>Writing cross-platform international software with Qt is a gentle, incremental process. Your software can become internationalized in the stages described in the following sections. For more information about internalizing Qt Quick application, see <a href="qtquick-internationalization.html">Internationalization and Localization with Qt Quick</a>.</p>
<a name="using-qstring-for-all-user-visible-text"></a>
<h2 id="using-qstring-for-all-user-visible-text">Using QString for All User-Visible Text</h2>
<p>Since QString uses the Unicode 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 QString as a parameter, there is no <code>char *</code> to QString conversion overhead.</p>
<p>Strings that are in &quot;programmer space&quot; (such as QObject names and file format texts) need not use QString; the traditional <code>char *</code> or the QByteArray class will suffice.</p>
<p>You're unlikely to notice that you are using Unicode; QString, and QChar are just easier versions of the crude <code>const char *</code> and <code>char</code> from traditional C.</p>
<p><code>char *</code> strings in source code are assumed to be <a href="http://www.ietf.org/rfc/rfc2279.txt">UTF-8</a> encoded when being implicitly converted to a QString. If your C string literal uses a different encoding, use QString::fromLatin1() or QTextCodec to convert the literal to a Unicode encoded QString.</p>
<a name="using-tr-for-all-literal-text"></a>
<h2 id="using-tr-for-all-literal-text">Using tr() for All Literal Text</h2>
<p>Wherever your program uses a string literal (quoted text) that will be presented to the user, ensure that it is processed by the QCoreApplication::translate() function. Essentially all that is necessary to achieve this is to use the tr() function to obtain translated text for your classes, typically for display purposes. This function is also used to indicate which text strings in an application are translatable.</p>
<p>For example, assuming the <code>LoginWidget</code> is a subclass of QWidget:</p>
<pre class="cpp">

  LoginWidget<span class="operator">::</span>LoginWidget()
  {
      <span class="type">QLabel</span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</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 QObject subclass, use either the tr() function of an appropriate class, or the QCoreApplication::translate() function directly:</p>
<pre class="cpp">

  <span class="type">void</span> some_global_function(LoginWidget <span class="operator">*</span>logwid)
  {
      <span class="type">QLabel</span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</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">QLabel</span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</span>(
                  <span class="type">QCoreApplication</span><span class="operator">::</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>Qt indexes each translatable string by the <i>translation context</i> it is associated with; this is generally the name of the QObject subclass it is used in.</p>
<p>Translation contexts are defined for new QObject-based classes by the use of the Q_OBJECT macro in each new class definition.</p>
<p>When tr() is called, it looks up the translatable string using a QTranslator object. For translation to work, one or more of these must have been installed on the application object in the way described in <a href="internationalization.html#enabling-translation">Enabling Translation</a>.</p>
<p>Translating strings in QML works exactly the same way as in C++, with the only difference being that you need to call qsTr() instead of tr(). See also the page on <a href="qtquick-internationalization.html">Internationalization and Localization with Qt Quick</a>.</p>
<a name="defining-a-translation-context"></a>
<h2 id="defining-a-translation-context">Defining a Translation Context</h2>
<p>The translation context for QObject and each QObject subclass is the class name itself. Developers subclassing QObject must use the Q_OBJECT macro in their class definition to override the translation context. This macro sets the context to the name of the subclass.</p>
<p>For example, the following class definition includes the Q_OBJECT macro, implementing a new tr() that uses the <code>MainWindow</code> context:</p>
<pre class="cpp">

  <span class="keyword">class</span> MainWindow : <span class="keyword">public</span> <span class="type">QMainWindow</span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      MainWindow();
      ...

</pre>
<p>If Q_OBJECT is not used in a class definition, the context will be inherited from the base class. For example, since all QObject-based classes in Qt provide a context, a new QWidget subclass defined without a Q_OBJECT macro will use the <code>QWidget</code> context if its tr() function is invoked.</p>
<a name="using-tr-to-obtain-a-translation"></a>
<h2 id="using-tr-to-obtain-a-translation">Using tr() to Obtain a Translation</h2>
<p>The following example shows how a translation is obtained for the class shown in the previous section:</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>createMenus()
  {
      fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));
      ...

</pre>
<p>Here, the translation context is <code>MainWindow</code> because it is the <code>MainWindow::tr()</code> function that is invoked. The text returned by the tr() function is a translation of &quot;&amp;File&quot; obtained from the <code>MainWindow</code> context.</p>
<p>When Qt's translation tool, lupdate, is used to process a set of source files, the text wrapped in tr() calls is stored in a section of the translation file that corresponds to its translation context.</p>
<p>In some situations, it is useful to give a translation context explicitly by fully qualifying the call to tr(); for example:</p>
<pre class="cpp">

  <span class="type">QString</span> text <span class="operator">=</span> <span class="type">QScrollBar</span><span class="operator">::</span>tr(<span class="string">&quot;Page up&quot;</span>);

</pre>
<p>This call obtains the translated text for &quot;Page up&quot; from the <code>QScrollBar</code> context. Developers can also use the QCoreApplication::translate() function to obtain a translation for a particular translation context.</p>
<a name="using-tr-to-localize-numbers"></a>
<h2 id="using-tr-to-localize-numbers">Using tr() to Localize Numbers</h2>
<p>You can localize numbers by 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">QTime</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 to make the code use the 24-hour clock branch.</p>
<a name="translating-non-qt-classes"></a>
<h2 id="translating-non-qt-classes">Translating Non-Qt Classes</h2>
<p>It is sometimes necessary to provide internationalization support for strings used in classes that do not inherit QObject or use the Q_OBJECT macro to enable translation features. Since Qt translates strings at run-time based on the class they are associated with and <code>lupdate</code> 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 Q_DECLARE_TR_FUNCTIONS() 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 tr() functions that can be used to translate strings associated with the class, and makes it possible for <code>lupdate</code> to find translatable strings in the source code.</p>
<p>Alternatively, the QCoreApplication::translate() function can be called with a specific context, and this will be recognized by <code>lupdate</code> and Qt Linguist.</p>
<a name="translator-comments"></a>
<h2 id="translator-comments">Translator Comments</h2>
<p>Developers can include information about each translatable string to help translators with the translation process. These are extracted when <code>lupdate</code> is used to process the source files. The recommended way to add comments is to annotate the tr() calls in your code with comments of the form:</p>
<p><code>//: ..&#x2e;</code></p>
<p>or</p>
<p><code><code>/*</code>: ..&#x2e; <code>*/</code></code></p>
<p>Examples:</p>
<pre class="cpp">

  <span class="comment">//: This name refers to a host name.</span>
  hostNameLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Name:&quot;</span>));

  <span class="comment">/*: This text refers to a C++ code example. */</span>
  <span class="type">QString</span> example <span class="operator">=</span> tr(<span class="string">&quot;Example&quot;</span>);

</pre>
<p>In these examples, the comments will be associated with the strings passed to tr() in the context of each call.</p>
<a name="adding-meta-data-to-strings"></a>
<h2 id="adding-meta-data-to-strings">Adding Meta-Data to Strings</h2>
<p>Additional data can be attached to each translatable message. These are extracted when <code>lupdate</code> is used to process the source files. The recommended way to add meta-data is to annotate the tr() calls in your code with comments of the form:</p>
<p><code>//= &lt;id&gt;</code></p>
<p>This can be used to give the message a unique identifier to support tools which need it.</p>
<p>An alternative way to attach meta-data is to use the following syntax:</p>
<p><code>//~ &lt;field name&gt; &lt;field contents&gt;</code></p>
<p>This can be used to attach meta-data to the message. The field name should consist of a domain prefix (possibly the conventional file extension of the file format the field is inspired by), a hyphen and the actual field name in underscore-delimited notation. For storage in TS files, the field name together with the prefix &quot;extra-&quot; will form an XML element name. The field contents will be XML-escaped, but otherwise appear verbatim as the element's contents. Any number of unique fields can be added to each message.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="comment">//: This is a comment for the translator.</span>
  <span class="comment">//= qtn_foo_bar</span>
  <span class="comment">//~ loc-layout_id foo_dialog</span>
  <span class="comment">//~ loc-blank False</span>
  <span class="comment">//~ magic-stuff This might mean something magic.</span>
  <span class="type">QString</span> text <span class="operator">=</span> MyMagicClass<span class="operator">::</span>tr(<span class="string">&quot;Sim sala bim.&quot;</span>);

</pre>
<p>You can use the keyword <i>TRANSLATOR</i> for translator comments. Meta-data appearing right in front of the TRANSLATOR keyword applies to the whole TS file.</p>
<a name="disambiguation"></a>
<h2 id="disambiguation">Disambiguation</h2>
<p>If the same translatable string is used in different roles within the same translation context, an additional identifying string may be passed in the call to tr(). This optional disambiguation argument is used to distinguish between otherwise identical strings.</p>
<p>Example:</p>
<pre class="cpp">

  MyWindow<span class="operator">::</span>MyWindow()
  {
      <span class="type">QLabel</span> <span class="operator">*</span>senderLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</span>(tr(<span class="string">&quot;Name:&quot;</span>));
      <span class="type">QLabel</span> <span class="operator">*</span>recipientLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</span>(tr(<span class="string">&quot;Name:&quot;</span><span class="operator">,</span> <span class="string">&quot;recipient&quot;</span>));
      ...

</pre>
<p>In Qt 4.4 and earlier, this disambiguation parameter was the preferred way to specify comments to translators.</p>
<a name="handling-plurals"></a>
<h2 id="handling-plurals">Handling Plurals</h2>
<p>Some translatable strings contain placeholders for integer values and need to be translated differently depending on the values in use.</p>
<p>To help with this problem, developers pass an additional integer argument to the tr() function, and typically use a special notation for plurals in each translatable string.</p>
<p>If this argument is equal or greater than zero, all occurrences of <code>%n</code> in the resulting string are replaced with a decimal representation of the value supplied. In addition, the translation used will adapt to the value according to the rules for each language.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="type">int</span> n <span class="operator">=</span> messages<span class="operator">.</span>count();
  showMessage(tr(<span class="string">&quot;%n message(s) saved&quot;</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="operator">,</span> n));

</pre>
<p>The table below shows what string is returned depending on the active translation:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th ></th><th  colspan="3">Active Translation</th></tr>
<tr class="qt-style"><th ><i>n</i></th><th >No Translation</th><th >French</th><th >English</th></tr></thead>
<tr valign="top" class="odd"><td >0</td><td >&quot;0 message(s) saved&quot;</td><td >&quot;0 message sauvegardé&quot;</td><td >&quot;0 message<b>s</b> saved&quot;</td></tr>
<tr valign="top" class="even"><td >1</td><td >&quot;1 message(s) saved&quot;</td><td >&quot;1 message sauvegardé&quot;</td><td >&quot;1 message saved&quot;</td></tr>
<tr valign="top" class="odd"><td >2</td><td >&quot;2 message(s) saved&quot;</td><td >&quot;2 message<b>s</b> sauvegardé<b>s</b>&quot;</td><td >&quot;2 message<b>s</b> saved&quot;</td></tr>
<tr valign="top" class="even"><td >37</td><td >&quot;37 message(s) saved&quot;</td><td >&quot;37 message<b>s</b> sauvegardé<b>s</b>&quot;</td><td >&quot;37 message<b>s</b> saved&quot;</td></tr>
</table></div>
<p>This idiom is more flexible than the traditional approach; e.g&#x2e;,</p>
<pre class="cpp">

  n <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span> <span class="operator">?</span> tr(<span class="string">&quot;%n message saved&quot;</span>) : tr(<span class="string">&quot;%n messages saved&quot;</span>)

</pre>
<p>because it also works with target languages that have several plural forms (e.g&#x2e;, Irish has a special &quot;dual&quot; form that should be used when <code>n</code> is 2), and it handles the <i>n</i> == 0 case correctly for languages such as French that require the singular.</p>
<p>To handle plural forms in the native language, you need to load a translation file for this language, too. The lupdate tool has the <code>-pluralonly</code> command line option, which allows the creation of TS files containing only entries with plural forms.</p>
<p>See the <a href="http://doc.qt.io/archives/qq/">Qt Quarterly</a> Article <a href="http://doc.qt.io/archives/qq/qq19-plurals.html">Plural Forms in Translations</a> for further details on this issue.</p>
<p>Instead of <code>%n</code>, you can use <code>%Ln</code> to produce a localized representation of <i>n</i>. The conversion uses the default locale, set using QLocale::setDefault(). (If no default locale was specified, the system wide locale is used.)</p>
<p>A summary of the rules used to translate strings containing plurals can be found in the <a href="i18n-plural-rules.html">Translation Rules for Plurals</a> document.</p>
<a name="translating-text-that-is-outside-of-a-qobject-subclass"></a>
<h2 id="translating-text-that-is-outside-of-a-qobject-subclass">Translating Text That is Outside of a QObject Subclass</h2>
<a name="using-qcoreapplication-translate"></a>
<h3 >Using QCoreApplication::translate()</h3>
<p>If the quoted text is not in a member function of a QObject subclass, use either the tr() function of an appropriate class, or the QCoreApplication::translate() function directly:</p>
<pre class="cpp">

  <span class="type">void</span> some_global_function(LoginWidget <span class="operator">*</span>logwid)
  {
      <span class="type">QLabel</span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</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">QLabel</span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLabel</span>(
              <span class="type">QCoreApplication</span><span class="operator">::</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>
<a name="using-qt-tr-noop-and-qt-translate-noop-in-c"></a>
<h3 >Using QT_TR_NOOP() and QT_TRANSLATE_NOOP() in C++</h3>
<p>If you need to have translatable text completely outside a function, there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP(). They merely mark the text for extraction by the <code>lupdate</code> tool. The macros expand to just the text (without the context).</p>
<p>Example of QT_TR_NOOP():</p>
<pre class="cpp">

  <span class="type">QString</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 QT_TRANSLATE_NOOP():</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">QString</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">QString</span> global_greeting(<span class="type">int</span> type)
  {
      <span class="keyword">return</span> <span class="type">QCoreApplication</span><span class="operator">::</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 <code>const char *</code> to QString automatic conversion by compiling your software with the macro <code>QT_NO_CAST_FROM_ASCII</code> defined, you'll be very likely to catch any strings you are missing. See QString::fromUtf8() and QString::fromLatin1() for more information.</p>
<a name="using-qkeysequence-for-accelerator-values"></a>
<h2 id="using-qkeysequence-for-accelerator-values">Using QKeySequence() for Accelerator Values</h2>
<p>Accelerator values such as Ctrl+Q or Alt+F need to be translated too. If you hardcode <code>Qt::CTRL + Qt::Key_Q</code> 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">QAction</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">QKeySequence</span><span class="operator">::</span>Quit);

</pre>
<a name="using-numbered-arguments"></a>
<h2 id="using-numbered-arguments">Using Numbered Arguments</h2>
<p>The QString::arg() 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">QString</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">QString</span> s1 <span class="operator">=</span> <span class="string">&quot;%1 of %2 files copied. Copying: %3&quot;</span>;
  <span class="type">QString</span> s2 <span class="operator">=</span> <span class="string">&quot;Kopierer nu %3. Av totalt %2 filer er %1 kopiert.&quot;</span>;

  qDebug() <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>);
  qDebug() <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">

  <span class="number">5</span> of <span class="number">10</span> files copied<span class="operator">.</span> Copying: somefile<span class="operator">.</span>txt
  Kopierer nu somefile<span class="operator">.</span>txt<span class="operator">.</span> Av totalt <span class="number">10</span> filer er <span class="number">5</span> kopiert<span class="operator">.</span>

</pre>
<a name="further-reading"></a>
<h2 id="further-reading">Further Reading</h2>
<p>Qt Linguist Manual, Hello tr() Example, <a href="i18n-plural-rules.html">Translation Rules for Plurals</a></p>
</div>
<!-- @@@i18n-source-translation.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="internationalization.html">Internationalization with Qt</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="i18n-plural-rules.html">Translation Rules for Plurals</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   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>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>