Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 837

qtbase5-doc-5.9.4-1.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" />
<!-- qtranslator.cpp -->
  <title>QTranslator Class | Qt Core 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 >Qt 5.9</td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ Classes</a></td><td >QTranslator</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">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
<li class="level2"><a href="#looking-up-translations">Looking up Translations</a></li>
<li class="level2"><a href="#using-multiple-translations">Using Multiple Translations</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QTranslator Class</h1>
<!-- $$$QTranslator-brief -->
<p>The <a href="qtranslator.html">QTranslator</a> class provides internationalization support for text output. <a href="#details">More...</a></p>
<!-- @@@QTranslator -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QTranslator&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <a href="qobject.html">QObject</a></td></tr></table></div><ul>
<li><a href="qtranslator-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h2 id="public-functions">Public Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#QTranslator">QTranslator</a></b>(QObject *<i>parent</i> = Q_NULLPTR)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#dtor.QTranslator">~QTranslator</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual bool </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#isEmpty">isEmpty</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#load">load</a></b>(const QString &amp;<i>filename</i>, const QString &amp;<i>directory</i> = QString(), const QString &amp;<i>search_delimiters</i> = QString(), const QString &amp;<i>suffix</i> = QString())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#load-1">load</a></b>(const QLocale &amp;<i>locale</i>, const QString &amp;<i>filename</i>, const QString &amp;<i>prefix</i> = QString(), const QString &amp;<i>directory</i> = QString(), const QString &amp;<i>suffix</i> = QString())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#load-2">load</a></b>(const uchar *<i>data</i>, int <i>len</i>, const QString &amp;<i>directory</i> = QString())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QString </td><td class="memItemRight bottomAlign"><b><a href="qtranslator.html#translate">translate</a></b>(const char *<i>context</i>, const char *<i>sourceText</i>, const char *<i>disambiguation</i> = Q_NULLPTR, int <i>n</i> = -1) const</td></tr>
</table></div>
<ul>
<li class="fn">32 public functions inherited from <a href="qobject.html#public-functions">QObject</a></li>
</ul>
<h3>Additional Inherited Members</h3>
<ul>
<li class="fn">1 property inherited from <a href="qobject.html#properties">QObject</a></li>
<li class="fn">1 public slot inherited from <a href="qobject.html#public-slots">QObject</a></li>
<li class="fn">2 signals inherited from <a href="qobject.html#signals">QObject</a></li>
<li class="fn">11 static public members inherited from <a href="qobject.html#static-public-members">QObject</a></li>
<li class="fn">9 protected functions inherited from <a href="qobject.html#protected-functions">QObject</a></li>
</ul>
<a name="details"></a>
<!-- $$$QTranslator-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qtranslator.html">QTranslator</a> class provides internationalization support for text output.</p>
<p>An object of this class contains a set of translations from a source language to a target language. <a href="qtranslator.html">QTranslator</a> provides functions to look up translations in a translation file. Translation files are created using Qt Linguist.</p>
<p>The most common use of <a href="qtranslator.html">QTranslator</a> is to: load a translation file, install it using <a href="qcoreapplication.html#installTranslator">QCoreApplication::installTranslator</a>(), and use it via <a href="qobject.html#tr">QObject::tr</a>(). Here's an example <code>main()</code> function using the <a href="qtranslator.html">QTranslator</a>:</p>
<pre class="cpp">

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="type"><a href="../qtwidgets/qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

      <span class="type"><a href="qtranslator.html#QTranslator">QTranslator</a></span> translator;
      <span class="comment">// look up e.g. :/translations/myapp_de.qm</span>
      <span class="keyword">if</span> (translator<span class="operator">.</span>load(<span class="type"><a href="qlocale.html">QLocale</a></span>()<span class="operator">,</span> QLatin1String(<span class="string">&quot;myapp&quot;</span>)<span class="operator">,</span> QLatin1String(<span class="string">&quot;_&quot;</span>)<span class="operator">,</span> QLatin1String(<span class="string">&quot;:/translations&quot;</span>)))
          app<span class="operator">.</span>installTranslator(<span class="operator">&amp;</span>translator);

      <span class="type"><a href="../qtwidgets/qpushbutton.html">QPushButton</a></span> hello(<span class="type"><a href="qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>translate(<span class="string">&quot;main&quot;</span><span class="operator">,</span> <span class="string">&quot;Hello world!&quot;</span>));
      hello<span class="operator">.</span>resize(<span class="number">100</span><span class="operator">,</span> <span class="number">30</span>);

      hello<span class="operator">.</span>show();
      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>Note that the translator must be created <i>before</i> the application's widgets.</p>
<p>Most applications will never need to do anything else with this class. The other functions provided by this class are useful for applications that work on translator files.</p>
<a name="looking-up-translations"></a>
<h3 >Looking up Translations</h3>
<p>It is possible to look up a translation using <a href="qtranslator.html#translate">translate</a>() (as <a href="qobject.html#tr">tr</a>() and <a href="qcoreapplication.html#translate">QCoreApplication::translate</a>() do). The <a href="qtranslator.html#translate">translate</a>() function takes up to three parameters:</p>
<ul>
<li>The <i>context</i> - usually the class name for the <a href="qobject.html#tr">tr</a>() caller.</li>
<li>The <i>source text</i> - usually the argument to <a href="qobject.html#tr">tr</a>().</li>
<li>The <i>disambiguation</i> - an optional string that helps disambiguate different uses of the same text in the same context.</li>
</ul>
<p>For example, the &quot;Cancel&quot; in a dialog might have &quot;Anuluj&quot; when the program runs in Polish (in this case the source text would be &quot;Cancel&quot;). The context would (normally) be the dialog's class name; there would normally be no comment, and the translated text would be &quot;Anuluj&quot;.</p>
<p>But it's not always so simple. The Spanish version of a printer dialog with settings for two-sided printing and binding would probably require both &quot;Activado&quot; and &quot;Activada&quot; as translations for &quot;Enabled&quot;. In this case the source text would be &quot;Enabled&quot; in both cases, and the context would be the dialog's class name, but the two items would have disambiguations such as &quot;two-sided printing&quot; for one and &quot;binding&quot; for the other. The disambiguation enables the translator to choose the appropriate gender for the Spanish version, and enables Qt to distinguish between translations.</p>
<a name="using-multiple-translations"></a>
<h3 >Using Multiple Translations</h3>
<p>Multiple translation files can be installed in an application. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched for translations first and the earliest translation file is searched last. The search stops as soon as a translation containing a matching string is found.</p>
<p>This mechanism makes it possible for a specific translation to be &quot;selected&quot; or given priority over the others; simply uninstall the translator from the application by passing it to the <a href="qcoreapplication.html#removeTranslator">QCoreApplication::removeTranslator</a>() function and reinstall it with <a href="qcoreapplication.html#installTranslator">QCoreApplication::installTranslator</a>(). It will then be the first translation to be searched for matching strings.</p>
</div>
<p><b>See also </b><a href="qcoreapplication.html#installTranslator">QCoreApplication::installTranslator</a>(), <a href="qcoreapplication.html#removeTranslator">QCoreApplication::removeTranslator</a>(), <a href="qobject.html#tr">QObject::tr</a>(), <a href="qcoreapplication.html#translate">QCoreApplication::translate</a>(), <a href="../qtwidgets/qtwidgets-tools-i18n-example.html">I18N Example</a>, Hello tr() Example, Arrow Pad Example, and Troll Print Example.</p>
<!-- @@@QTranslator -->
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QTranslator[overload1]$$$QTranslatorQObject* -->
<h3 class="fn" id="QTranslator"><a name="QTranslator"></a>QTranslator::<span class="name">QTranslator</span>(<span class="type"><a href="qobject.html#QObject">QObject</a></span> *<i>parent</i> = Q_NULLPTR)</h3>
<p>Constructs an empty message file object with parent <i>parent</i> that is not connected to any file.</p>
<!-- @@@QTranslator -->
<!-- $$$~QTranslator[overload1]$$$~QTranslator -->
<h3 class="fn" id="dtor.QTranslator"><a name="dtor.QTranslator"></a>QTranslator::<span class="name">~QTranslator</span>()</h3>
<p>Destroys the object and frees any allocated resources.</p>
<!-- @@@~QTranslator -->
<!-- $$$isEmpty[overload1]$$$isEmpty -->
<h3 class="fn" id="isEmpty"><a name="isEmpty"></a><code>[virtual] </code><span class="type">bool</span> QTranslator::<span class="name">isEmpty</span>() const</h3>
<p>Returns <code>true</code> if this translator is empty, otherwise returns <code>false</code>. This function works with stripped and unstripped translation files.</p>
<!-- @@@isEmpty -->
<!-- $$$load[overload1]$$$loadconstQString&constQString&constQString&constQString& -->
<h3 class="fn" id="load"><a name="load"></a><span class="type">bool</span> QTranslator::<span class="name">load</span>(const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>filename</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>directory</i> = QString(), const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>search_delimiters</i> = QString(), const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>suffix</i> = QString())</h3>
<p>Loads <i>filename</i> + <i>suffix</i> (&quot;.qm&quot; if the <i>suffix</i> is not specified), which may be an absolute file name or relative to <i>directory</i>. Returns <code>true</code> if the translation is successfully loaded; otherwise returns <code>false</code>.</p>
<p>If <i>directory</i> is not specified, the current directory is used (i.e&#x2e;, as <a href="qdir.html#currentPath">currentPath()</a>).</p>
<p>The previous contents of this translator object are discarded.</p>
<p>If the file name does not exist, other file names are tried in the following order:</p>
<ol class="1" type="1"><li>File name without <i>suffix</i> appended.</li>
<li>File name with text after a character in <i>search_delimiters</i> stripped (&quot;_.&quot; is the default for <i>search_delimiters</i> if it is an empty string) and <i>suffix</i>.</li>
<li>File name stripped without <i>suffix</i> appended.</li>
<li>File name stripped further, etc.</li>
</ol>
<p>For example, an application running in the fr_CA locale (French-speaking Canada) might call load(&quot;foo.fr_ca&quot;, &quot;/opt/foolib&quot;). load() would then try to open the first existing readable file from this list:</p>
<ol class="1" type="1"><li><code>/opt/foolib/foo.fr_ca.qm</code></li>
<li><code>/opt/foolib/foo.fr_ca</code></li>
<li><code>/opt/foolib/foo.fr.qm</code></li>
<li><code>/opt/foolib/foo.fr</code></li>
<li><code>/opt/foolib/foo.qm</code></li>
<li><code>/opt/foolib/foo</code></li>
</ol>
<p>Usually, it is better to use the QTranslator::load(const <a href="qlocale.html">QLocale</a> &amp;, const <a href="qstring.html">QString</a> &amp;, const <a href="qstring.html">QString</a> &amp;, const <a href="qstring.html">QString</a> &amp;, const <a href="qstring.html">QString</a> &amp;) function instead, because it uses <a href="qlocale.html#uiLanguages">QLocale::uiLanguages</a>() and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.</p>
<!-- @@@load -->
<!-- $$$load$$$loadconstQLocale&constQString&constQString&constQString&constQString& -->
<h3 class="fn" id="load-1"><a name="load-1"></a><span class="type">bool</span> QTranslator::<span class="name">load</span>(const <span class="type"><a href="qlocale.html">QLocale</a></span> &amp;<i>locale</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>filename</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>prefix</i> = QString(), const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>directory</i> = QString(), const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>suffix</i> = QString())</h3>
<p>Loads <i>filename</i> + <i>prefix</i> + <a href="qlocale.html#uiLanguages">ui language name</a> + <i>suffix</i> (&quot;.qm&quot; if the <i>suffix</i> is not specified), which may be an absolute file name or relative to <i>directory</i>. Returns <code>true</code> if the translation is successfully loaded; otherwise returns <code>false</code>.</p>
<p>The previous contents of this translator object are discarded.</p>
<p>If the file name does not exist, other file names are tried in the following order:</p>
<ol class="1" type="1"><li>File name without <i>suffix</i> appended.</li>
<li>File name with ui language part after a &quot;_&quot; character stripped and <i>suffix</i>.</li>
<li>File name with ui language part stripped without <i>suffix</i> appended.</li>
<li>File name with ui language part stripped further, etc.</li>
</ol>
<p>For example, an application running in the <i>locale</i> with the following <a href="qlocale.html#uiLanguages">ui languages</a> - &quot;es&quot;, &quot;fr-CA&quot;, &quot;de&quot; might call load(QLocale(), &quot;foo&quot;, &quot;.&quot;, &quot;/opt/foolib&quot;, &quot;.qm&quot;). <a href="qtranslator.html#load">load</a>() would replace '-' (dash) with '_' (underscore) in the ui language and then try to open the first existing readable file from this list:</p>
<ol class="1" type="1"><li><code>/opt/foolib/foo.es.qm</code></li>
<li><code>/opt/foolib/foo.es</code></li>
<li><code>/opt/foolib/foo.fr_CA.qm</code></li>
<li><code>/opt/foolib/foo.fr_CA</code></li>
<li><code>/opt/foolib/foo.de.qm</code></li>
<li><code>/opt/foolib/foo.de</code></li>
<li><code>/opt/foolib/foo.fr.qm</code></li>
<li><code>/opt/foolib/foo.fr</code></li>
<li><code>/opt/foolib/foo.qm</code></li>
<li><code>/opt/foolib/foo</code>.</li>
<li><code>/opt/foolib/foo</code></li>
</ol>
<p>On operating systems where file system is case sensitive, <a href="qtranslator.html">QTranslator</a> also tries to load a lower-cased version of the locale name.</p>
<p>This function was introduced in  Qt 4.8.</p>
<!-- @@@load -->
<!-- $$$load$$$loadconstuchar*intconstQString& -->
<h3 class="fn" id="load-2"><a name="load-2"></a><span class="type">bool</span> QTranslator::<span class="name">load</span>(const <span class="type"><a href="qtglobal.html#uchar-typedef">uchar</a></span> *<i>data</i>, <span class="type">int</span> <i>len</i>, const <span class="type"><a href="qstring.html">QString</a></span> &amp;<i>directory</i> = QString())</h3>
<p>This function overloads <a href="qtranslator.html#load">load</a>().</p>
<p>Loads the QM file data <i>data</i> of length <i>len</i> into the translator.</p>
<p>The data is not copied. The caller must be able to guarantee that <i>data</i> will not be deleted or modified.</p>
<p><i>directory</i> is only used to specify the base directory when loading the dependencies of a QM file. If the file does not have dependencies, this argument is ignored.</p>
<!-- @@@load -->
<!-- $$$translate[overload1]$$$translateconstchar*constchar*constchar*int -->
<h3 class="fn" id="translate"><a name="translate"></a><code>[virtual] </code><span class="type"><a href="qstring.html">QString</a></span> QTranslator::<span class="name">translate</span>(const <span class="type">char</span> *<i>context</i>, const <span class="type">char</span> *<i>sourceText</i>, const <span class="type">char</span> *<i>disambiguation</i> = Q_NULLPTR, <span class="type">int</span> <i>n</i> = -1) const</h3>
<p>Returns the translation for the key (<i>context</i>, <i>sourceText</i>, <i>disambiguation</i>). If none is found, also tries (<i>context</i>, <i>sourceText</i>, &quot;&quot;). If that still fails, returns a null string.</p>
<p><b>Note: </b>Incomplete translations may result in unexpected behavior: If no translation for (<i>context</i>, <i>sourceText</i>, &quot;&quot;) is provided, the method might in this case actually return a translation for a different <i>disambiguation</i>.</p><p>If <i>n</i> is not -1, it is used to choose an appropriate form for the translation (e.g&#x2e; &quot;%n file found&quot; vs. &quot;%n files found&quot;).</p>
<p>If you need to programatically insert translations into a <a href="qtranslator.html">QTranslator</a>, this function can be reimplemented.</p>
<p><b>See also </b><a href="qtranslator.html#load">load</a>().</p>
<!-- @@@translate -->
</div>
        </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>