/usr/share/qt5/doc/qtdoc/i18n-source-translation.html is in qt5-doc-html 5.9.5-0ubuntu1.
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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | <?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.5 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"> ◦ </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 <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 <code>char *</code> 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 <code>char *</code> 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 <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 <a href="../qtcore/qstring.html">QString</a>. If your C string literal uses a different encoding, use <a href="../qtcore/qstring.html#fromLatin1-1">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 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 <a href="../qtcore/qcoreapplication-obsolete.html#translate-1">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 <code>LoginWidget</code> 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 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"><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>(
<span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></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 id="defining-a-translation-context">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 <code>MainWindow</code> 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 <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">></span>addMenu(tr(<span class="string">"&File"</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 "&File" obtained from the <code>MainWindow</code> 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 <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"><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 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 <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 <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 <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 <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>//: ...</code></p>
<p>or</p>
<p><code><code>/*</code>: ... <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">></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 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>//= <id></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>//~ <field name> <field contents></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 "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 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 <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 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 <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 <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">"%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>
<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 >"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></div>
<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 <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 <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 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 <a href="../qtcore/qobject.html">QObject</a> 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"><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>(
<span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></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 <code>lupdate</code> 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> <span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></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 <code>const char *</code> to <a href="../qtcore/qstring.html">QString</a> 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 <a href="../qtcore/qstring.html#fromUtf8-1">QString::fromUtf8</a>() and <a href="../qtcore/qstring.html#fromLatin1-1">QString::fromLatin1</a>() 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 "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 id="using-numbered-arguments">Using Numbered Arguments</h2>
<p>The <a href="../qtcore/qstring.html#arg-10">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 id="further-reading">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>
<span class="naviSeparator"> ◦ </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">©</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>
|