<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QTranslator Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QTranslator Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QTranslator class provides internationalization support for text output. <a href="#details">More...</a></p> <p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qtranslator.html#QTranslator">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None)</li><li><div class="fn" />bool <b><a href="qtranslator.html#isEmpty">isEmpty</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtranslator.html#load">load</a></b> (<i>self</i>, QString <i>fileName</i>, QString <i>directory</i> = QString(), QString <i>searchDelimiters</i> = QString(), QString <i>suffix</i> = QString())</li><li><div class="fn" />bool <b><a href="qtranslator.html#load-2">load</a></b> (<i>self</i>, QLocale <i>locale</i>, QString <i>fileName</i>, QString <i>prefix</i> = QString(), QString <i>directory</i> = QString(), QString <i>suffix</i> = QString())</li><li><div class="fn" />bool <b><a href="qtranslator.html#loadFromData">loadFromData</a></b> (<i>self</i>, str <i>data</i>)</li><li><div class="fn" />QString <b><a href="qtranslator.html#translate">translate</a></b> (<i>self</i>, str <i>context</i>, str <i>sourceText</i>, str <i>disambiguation</i> = None)</li><li><div class="fn" />QString <b><a href="qtranslator.html#translate-2">translate</a></b> (<i>self</i>, str <i>context</i>, str <i>sourceText</i>, str <i>comment</i>, int <i>n</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QTranslator 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. QTranslator provides functions to look up translations in a translation file. Translation files are created using <a href="linguist-manual.html#qt-linguist">Qt Linguist</a>.</p> <p>The most common use of QTranslator is to: load a translation file, install it using <a href="qcoreapplication.html#installTranslator">QApplication.installTranslator</a>(), and use it via <a href="qobject.html#tr">QObject.tr</a>(). Here's the <tt>main()</tt> function from the <a href="linguist-hellotr.html">Hello tr()</a> example:</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); <span class="type">QTranslator</span> translator; translator<span class="operator">.</span>load(<span class="string">"hellotr_la"</span>); app<span class="operator">.</span>installTranslator(<span class="operator">&</span>translator); <span class="type"><a href="qpushbutton.html">QPushButton</a></span> hello(<span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">.</span>tr(<span class="string">"Hello world!"</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 id="looking-up-translations" name="looking-up-translations" /> <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">QApplication.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 "Cancel" in a dialog might have "Anuluj" when the program runs in Polish (in this case the source text would be "Cancel"). The context would (normally) be the dialog's class name; there would normally be no comment, and the translated text would be "Anuluj".</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 "Activado" and "Activada" as translations for "Enabled". In this case the source text would be "Enabled" in both cases, and the context would be the dialog's class name, but the two items would have disambiguations such as "two-sided printing" for one and "binding" 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 id="using-multiple-translations" name="using-multiple-translations" /> <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 "selected" or given priority over the others; simply uninstall the translator from the application by passing it to the <a href="qcoreapplication.html#removeTranslator">QApplication.removeTranslator</a>() function and reinstall it with <a href="qcoreapplication.html#installTranslator">QApplication.installTranslator</a>(). It will then be the first translation to be searched for matching strings.</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QTranslator" />QTranslator.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs an empty message file object with parent <i>parent</i> that is not connected to any file.</p> <h3 class="fn"><a name="isEmpty" />bool QTranslator.isEmpty (<i>self</i>)</h3><p>Returns true if this translator is empty, otherwise returns false. This function works with stripped and unstripped translation files.</p> <h3 class="fn"><a name="load" />bool QTranslator.load (<i>self</i>, QString <i>fileName</i>, QString <i>directory</i> = QString(), QString <i>searchDelimiters</i> = QString(), QString <i>suffix</i> = QString())</h3><p>Loads <i>filename</i> + <i>suffix</i> (".qm" if the <i>suffix</i> is not specified), which may be an absolute file name or relative to <i>directory</i>. Returns true if the translation is successfully loaded; otherwise returns false.</p> <p>If <i>directory</i> is not specified, the directory of the application's executable is used (i.e., as <a href="qcoreapplication.html#applicationDirPath">applicationDirPath()</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"> <li>File name without <i>suffix</i> appended.</li> <li>File name with text after a character in <i>search_delimiters</i> stripped ("<a href="index.html">_</a>." 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("foo.fr_ca", "/opt/foolib"). load() would then try to open the first existing readable file from this list:</p> <ol class="1"> <li><tt>/opt/foolib/foo.fr_ca.qm</tt></li> <li><tt>/opt/foolib/foo.fr_ca</tt></li> <li><tt>/opt/foolib/foo.fr.qm</tt></li> <li><tt>/opt/foolib/foo.fr</tt></li> <li><tt>/opt/foolib/foo.qm</tt></li> <li><tt>/opt/foolib/foo</tt></li> </ol> <h3 class="fn"><a name="load-2" />bool QTranslator.load (<i>self</i>, <a href="qlocale.html">QLocale</a> <i>locale</i>, QString <i>fileName</i>, QString <i>prefix</i> = QString(), QString <i>directory</i> = QString(), QString <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> (".qm" if the <i>suffix</i> is not specified), which may be an absolute file name or relative to <i>directory</i>. Returns true if the translation is successfully loaded; otherwise returns false.</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"> <li>File name without <i>suffix</i> appended.</li> <li>File name with ui language part after a "<a href="index.html">_</a>" 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 locale with the following <a href="qlocale.html#uiLanguages">ui languages</a> - "es", "fr-CA", "de" might call load(<a href="qlocale.html#system">QLocale.system</a>(), "foo", ".", "/opt/foolib", ".qm"). <a href="qtranslator.html#load">load</a>() would replace '-' (dash) with '<a href="index.html">_</a>' (underscore) in the ui language and then try to open the first existing readable file from this list:</p> <ol class="1"> <li><tt>/opt/foolib/foo.es.qm</tt></li> <li><tt>/opt/foolib/foo.es</tt></li> <li><tt>/opt/foolib/foo.fr_CA.qm</tt></li> <li><tt>/opt/foolib/foo.fr_CA</tt></li> <li><tt>/opt/foolib/foo.de.qm</tt></li> <li><tt>/opt/foolib/foo.de</tt></li> <li><tt>/opt/foolib/foo.fr.qm</tt></li> <li><tt>/opt/foolib/foo.fr</tt></li> <li><tt>/opt/foolib/foo.qm</tt></li> <li><tt>/opt/foolib/foo</tt>.</li> <li><tt>/opt/foolib/foo</tt></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> <h3 class="fn"><a name="loadFromData" />bool QTranslator.loadFromData (<i>self</i>, str <i>data</i>)</h3><h3 class="fn"><a name="translate" />QString QTranslator.translate (<i>self</i>, str <i>context</i>, str <i>sourceText</i>, str <i>disambiguation</i> = None)</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>, ""). If that still fails, returns an empty string.</p> <p>If you need to programatically insert translations in to 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> <h3 class="fn"><a name="translate-2" />QString QTranslator.translate (<i>self</i>, str <i>context</i>, str <i>sourceText</i>, str <i>comment</i>, int <i>n</i>)</h3><p>This function overloads <a href="qtranslator.html#translate">translate</a>().</p> <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>, ""). If that still fails, returns an empty string.</p> <p>If <i>n</i> is not -1, it is used to choose an appropriate form for the translation (e.g. "%n file found" vs. "%n files found").</p> <p><b>See also</b> <a href="qtranslator.html#load">load</a>().</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>