/usr/share/qt5/doc/qtdoc/i18n-source-translation.html is in qt5-doc-html 5.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- i18n.qdoc -->
<title>Writing Source Code for Translation | QtDoc 5.2</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li><a href="index.html">Qt 5.2</a></li>
<li>Writing Source Code for Translation</li>
<li id="buildversion">
Qt 5.2.1 Reference Documentation</li>
</ul>
</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>
<a class="nextPage" href="i18n-plural-rules.html">Translation Rules for Plurals</a>
</p><p/>
<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>
<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>Using QString for All User-Visible Text</h2>
<p>Since <a href="../qtcore/qstring.html">QString</a> 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 <a href="../qtcore/qstring.html">QString</a> as a parameter, there is no <tt>char *</tt> to <a href="../qtcore/qstring.html">QString</a> conversion overhead.</p>
<p>Strings that are in "programmer space" (such as <a href="../qtcore/qobject.html">QObject</a> names and file format texts) need not use <a href="../qtcore/qstring.html">QString</a>; the traditional <tt>char *</tt> or the <a href="../qtcore/qbytearray.html">QByteArray</a> class will suffice.</p>
<p>You're unlikely to notice that you are using Unicode; <a href="../qtcore/qstring.html">QString</a>, and <a href="../qtcore/qchar.html">QChar</a> are just easier versions of the crude <tt>const char *</tt> and <tt>char</tt> from traditional C.</p>
<p><tt>char *</tt> 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 <a href="../qtcore/qstring.html">QString</a>. If your C string literal uses a different encoding, use <a href="../qtcore/qstring.html#fromLatin1">QString::fromLatin1</a>() or <a href="../qtcore/qtextcodec.html">QTextCodec</a> to convert the literal to a Unicode encoded <a href="../qtcore/qstring.html">QString</a>.</p>
<a name="using-tr-for-all-literal-text"></a>
<h2>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 <a href="../qtcore/qcoreapplication.html#translate">QCoreApplication::translate</a>() function. Essentially all that is necessary to achieve this is to use the <a href="../qtcore/qobject.html#tr">tr()</a> 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 <tt>LoginWidget</tt> is a subclass of <a href="../qtwidgets/qwidget.html">QWidget</a>:</p>
<pre class="cpp">LoginWidget<span class="operator">::</span>LoginWidget()
{
<span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(tr(<span class="string">"Password:"</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="../qtcore/qobject.html">QObject</a> subclass, use either the tr() function of an appropriate class, or the <a href="../qtcore/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="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(
LoginWidget<span class="operator">::</span>tr(<span class="string">"Password:"</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="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(
qApp<span class="operator">-</span><span class="operator">></span>translate(<span class="string">"LoginWidget"</span><span class="operator">,</span> <span class="string">"Password:"</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 <a href="../qtcore/qobject.html">QObject</a> subclass it is used in.</p>
<p>Translation contexts are defined for new <a href="../qtcore/qobject.html">QObject</a>-based classes by the use of the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro in each new class definition.</p>
<p>When tr() is called, it looks up the translatable string using a <a href="../qtcore/qtranslator.html">QTranslator</a> 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 <a href="../qtcore/qobject.html#tr">tr()</a>. 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>Defining a Translation Context</h2>
<p>The translation context for <a href="../qtcore/qobject.html">QObject</a> and each <a href="../qtcore/qobject.html">QObject</a> subclass is the class name itself. Developers subclassing <a href="../qtcore/qobject.html">QObject</a> must use the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> 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 <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro, implementing a new tr() that uses the <tt>MainWindow</tt> context:</p>
<pre class="cpp"><span class="keyword">class</span> MainWindow : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/qmainwindow.html">QMainWindow</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
MainWindow();
...</pre>
<p>If <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> is not used in a class definition, the context will be inherited from the base class. For example, since all <a href="../qtcore/qobject.html">QObject</a>-based classes in Qt provide a context, a new <a href="../qtwidgets/qwidget.html">QWidget</a> subclass defined without a <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro will use the <tt>QWidget</tt> context if its tr() function is invoked.</p>
<a name="using-tr-to-obtain-a-translation"></a>
<h2>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">></span>addMenu(tr(<span class="string">"&File"</span>));
...</pre>
<p>Here, the translation context is <tt>MainWindow</tt> because it is the <tt>MainWindow::tr()</tt> function that is invoked. The text returned by the tr() function is a translation of "&File" obtained from the <tt>MainWindow</tt> context.</p>
<p>When Qt's translation tool, <a href="../qtlinguist/linguist-manager.html#using-lupdate">lupdate</a>, 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"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> <span class="type"><a href="../qtwidgets/qscrollbar.html">QScrollBar</a></span><span class="operator">::</span>tr(<span class="string">"Page up"</span>);</pre>
<p>This call obtains the translated text for "Page up" from the <tt>QScrollBar</tt> context. Developers can also use the <a href="../qtcore/qcoreapplication.html#translate">QCoreApplication::translate</a>() function to obtain a translation for a particular translation context.</p>
<a name="using-tr-to-localize-numbers"></a>
<h2>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"><a href="../qtcore/qtime.html">QTime</a></span> <span class="operator">&</span>time)
{
<span class="keyword">if</span> (tr(<span class="string">"AMPM"</span>) <span class="operator">=</span><span class="operator">=</span> <span class="string">"AMPM"</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 "AMPM" 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>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="../qtcore/qobject.html">QObject</a> or use the <a href="../qtcore/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="../qtcore/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="../qtcore/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="../qtcore/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="translator-comments"></a>
<h2>Translator Comments</h2>
<p>Developers can include information about each translatable string to help translators with the translation process. These are extracted when <tt>lupdate</tt> 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><tt>//: ...</tt></p>
<p>or</p>
<p><tt><tt>/*</tt>: ... <tt>*/</tt></tt></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">></span>setText(tr(<span class="string">"Name:"</span>));
<span class="comment">/*: This text refers to a C++ code example. */</span>
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> example <span class="operator">=</span> tr(<span class="string">"Example"</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>Adding Meta-Data to Strings</h2>
<p>Additional data can be attached to each translatable message. These are extracted when <tt>lupdate</tt> 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><tt>//= <id></tt></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><tt>//~ <field name> <field contents></tt></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 "extra-" 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"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> MyMagicClass<span class="operator">::</span>tr(<span class="string">"Sim sala bim."</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>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 <a href="../qtcore/qobject.html#tr">tr()</a>. 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"><a href="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>senderLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(tr(<span class="string">"Name:"</span>));
<span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>recipientLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(tr(<span class="string">"Name:"</span><span class="operator">,</span> <span class="string">"recipient"</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>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 <a href="../qtcore/qobject.html#tr">tr()</a> 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 <tt>%n</tt> 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">"%n message(s) saved"</span><span class="operator">,</span> <span class="string">""</span><span class="operator">,</span> n));</pre>
<p>The table below shows what string is returned depending on the active translation:</p>
<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 >"0 message(s) saved"</td><td >"0 message sauvegardé"</td><td >"0 message<b>s</b> saved"</td></tr>
<tr valign="top" class="even"><td >1</td><td >"1 message(s) saved"</td><td >"1 message sauvegardé"</td><td >"1 message saved"</td></tr>
<tr valign="top" class="odd"><td >2</td><td >"2 message(s) saved"</td><td >"2 message<b>s</b> sauvegardé<b>s</b>"</td><td >"2 message<b>s</b> saved"</td></tr>
<tr valign="top" class="even"><td >37</td><td >"37 message(s) saved"</td><td >"37 message<b>s</b> sauvegardé<b>s</b>"</td><td >"37 message<b>s</b> saved"</td></tr>
</table>
<p>This idiom is more flexible than the traditional approach; e.g.,</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">"%n message saved"</span>) : tr(<span class="string">"%n messages saved"</span>)</pre>
<p>because it also works with target languages that have several plural forms (e.g., Irish has a special "dual" form that should be used when <tt>n</tt> 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 <tt>-pluralonly</tt> 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.digia.com/qq/">Qt Quarterly</a> Article <a href="http://doc.qt.digia.com/qq/qq19-plurals.html">Plural Forms in Translations</a> for further details on this issue.</p>
<p>Instead of <tt>%n</tt>, you can use <tt>%Ln</tt> to produce a localized representation of <i>n</i>. The conversion uses the default locale, set using <a href="../qtcore/qlocale.html#setDefault">QLocale::setDefault</a>(). (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>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 <a href="../qtcore/qobject.html">QObject</a> subclass, use either the tr() function of an appropriate class, or the <a href="../qtcore/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="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(
LoginWidget<span class="operator">::</span>tr(<span class="string">"Password:"</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="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span>(
qApp<span class="operator">-</span><span class="operator">></span>translate(<span class="string">"LoginWidget"</span><span class="operator">,</span> <span class="string">"Password:"</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: <a href="../qtcore/qtglobal.html#QT_TR_NOOP">QT_TR_NOOP</a>() and <a href="../qtcore/qtglobal.html#QT_TRANSLATE_NOOP">QT_TRANSLATE_NOOP</a>(). They merely mark the text for extraction by the <tt>lupdate</tt> tool. The macros expand to just the text (without the context).</p>
<p>Example of <a href="../qtcore/qtglobal.html#QT_TR_NOOP">QT_TR_NOOP</a>():</p>
<pre class="cpp"><span class="type"><a href="../qtcore/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">"Hello"</span>)<span class="operator">,</span>
QT_TR_NOOP(<span class="string">"Goodbye"</span>)
};
<span class="keyword">return</span> tr(greeting_strings<span class="operator">[</span>type<span class="operator">]</span>);
}</pre>
<p>Example of <a href="../qtcore/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">"FriendlyConversation"</span><span class="operator">,</span> <span class="string">"Hello"</span>)<span class="operator">,</span>
QT_TRANSLATE_NOOP(<span class="string">"FriendlyConversation"</span><span class="operator">,</span> <span class="string">"Goodbye"</span>)
};
<span class="type"><a href="../qtcore/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="../qtcore/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">></span>translate(<span class="string">"FriendlyConversation"</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="../qtcore/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="../qtcore/qstring.html#fromUtf8">QString::fromUtf8</a>() and <a href="../qtcore/qstring.html#fromLatin1">QString::fromLatin1</a>() for more information.</p>
<a name="using-qkeysequence-for-accelerator-values"></a>
<h2>Using QKeySequence() for Accelerator Values</h2>
<p>Accelerator values such as Ctrl+Q or Alt+F need to be translated too. If you hardcode <tt>Qt::CTRL + Qt::Key_Q</tt> for "quit" 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="../qtwidgets/qaction.html">QAction</a></span>(tr(<span class="string">"E&xit"</span>)<span class="operator">,</span> <span class="keyword">this</span>);
exitAct<span class="operator">-</span><span class="operator">></span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Quit);</pre>
<a name="using-numbered-arguments"></a>
<h2>Using Numbered Arguments</h2>
<p>The <a href="../qtcore/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="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>currentFile)
{
label<span class="operator">.</span>setText(tr(<span class="string">"%1 of %2 files copied.\nCopying: %3"</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="../qtcore/qstring.html">QString</a></span> s1 <span class="operator">=</span> <span class="string">"%1 of %2 files copied. Copying: %3"</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> s2 <span class="operator">=</span> <span class="string">"Kopierer nu %3. Av totalt %2 filer er %1 kopiert."</span>;
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</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">"somefile.txt"</span>);
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</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">"somefile.txt"</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>Further Reading</h2>
<p><a href="../qtlinguist/qtlinguist-index.html">Qt Linguist Manual</a>, <a href="../qtlinguist/qtlinguist-hellotr-example.html">Hello tr</a>() 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>
<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">©</acronym> 2013 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|