⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linguist-manual-4.html

📁 QT 下载资料仅供参考
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/tools/linguist/book/linguist-programmer.leaf:3 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Programmers</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><p align="right">[<a href="linguist-manual-3.html">Prev: Translators</a>] [<a href="linguist-manual.html">Home</a>]</p><h2 align="center">Programmers</h2><p>Support for multiple languages is extremely simple in Qt applications and adds little overhead to the programmer's workload.</p><p>Qt minimizes the performance cost of using translations by translating the phrases for each window as they are created. In most applications the main window is created just once. Dialogs are often created once and then shown and hidden as required. Once the initial translation has taken place there is no further runtime overhead for the translated windows. Only those windows that are created, destroyed and subsequently created will have a translation performance cost -- although the overhead is still very low.</p><p>Creating applications that can switch language at runtime is possible with Qt, but requires a certain amount of programmer intervention and will of course incur some runtime performance cost.</p><h3><a name="1"></a>Making the Application Translation Aware</h3><p>Programmers should make their application look for and load the appropriate translation file and mark user-visible text and Ctrl keyboard accelerators as targets for translation.</p><p>Each piece of text that requires translating requires context to help the translator identify where in the program the text occurs. In the case of multiple identical texts that require different translations the translator also requires some information to disambiguate the source texts. Marking text for translation will automatically cause the class name to be used as basic context information. In some cases the programmer may be required to add additional information to help the translator.</p><h4><a name="1-1"></a>Creating Translation Files</h4><!-- index .ts Files --><!-- index Translation Source Files --><p>Translation files consist of all the user-visible text and Ctrl key accelerators in an application and translations of that text. Translation files are created as follows:</p><!-- index lupdate --><!-- index lrelease --><ol type=1><li><p>Run <a href="linguist-manual-2.html#2">lupdate</a> initially to generate the first set of <tt>.ts</tt> translation source files with all the user-visible text but no translations.</p><li><p>The <tt>.ts</tt> files are given to the translator who adds translations using <em>Qt Linguist</em>. <em>Qt Linguist</em> takes care of any changed or deleted source text.</p><li><p>Run <a href="linguist-manual-2.html#2">lupdate</a> to incorporate any new text added to the application. <a href="linguist-manual-2.html#2">lupdate</a> synchronizes the user-visible text from the application with the translations; it does not destroy any data.</p><li><p>Steps 2 and 3 are repeated as often as necessary.</p><li><p>When a release of the application is needed <a href="linguist-manual-2.html#3">lrelease</a> is run to read the <tt>.ts</tt> files and produce the <tt>.qm</tt> files used by the application at runtime.</p></ol><!-- index .pro Files --><!-- index Project Files --><!-- index qmake!Project Files --><p>For <a href="linguist-manual-2.html#2">lupdate</a> to work successfully, it must know which translation files to produce. The files are simply listed in the application's <tt>.pro</tt> Qt project file, for example:</p><pre>    TRANSLATIONS    = tt2_fr.ts \                      tt2_nl.ts</pre> <p>See the <a href="linguist-manual-2.html#2">"lupdate"</a> and <a href="linguist-manual-2.html#3">"lrelease"</a> sections.</p><h4><a name="1-2"></a>Loading Translations</h4><pre>    int main( int argc, char **argv )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );</pre><!-- index main() --> <p>This is how a simple <tt>main()</tt> function of a Qt application begins.</p><!-- index QTranslator!load() --><!-- index load()!QTranslator --><!-- index QApplication!installTranslator() --><!-- index installTranslator()!QApplication --><pre>    int main( int argc, char **argv )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        <a href="qtranslator.html">QTranslator</a> translator( 0 );        translator.<a href="qtranslator.html#load">load</a>( "tt1_la", "." );        app.<a href="qapplication.html#installTranslator">installTranslator</a>( &amp;translator );</pre> <p>For a translation-aware application a translator object is created, a translation is loaded and the translator object installed into the application.</p><pre>    int main( int argc, char **argv )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        <a href="qtranslator.html">QTranslator</a> translator( 0 );        translator.<a href="qtranslator.html#load">load</a>( QString( "tt2_" ) + QTextCodec::locale(), "." );        app.<a href="qapplication.html#installTranslator">installTranslator</a>( &amp;translator );</pre> <p>In production applications a more flexible approach, for example, loading translations according to locale, might be more appropriate. If the <tt>.ts</tt> files are all named according to a convention such as <em>appname_locale</em>, e.g. <tt>tt2_fr</tt>, <tt>tt2_de</tt> etc, then the code above will load the current locale's translation at runtime.</p><p>If there is no translation file for the current locale the application will fall back to using the original source text.</p><h4><a name="1-3"></a>Making the Application Translate User-Visible Strings</h4><!-- index tr() --><!-- index QObject!tr() --><p>User-visible strings are marked as translation targets by wrapping them in a <tt>tr()</tt> call, for example:</p><pre>    button = new QPushButton( "&amp;Quit", this );</pre><p>would become</p><pre>    button = new QPushButton( tr( "&amp;Quit" ), this);</pre><!-- index Q_OBJECT --><p>All <a href="qobject.html">QObject</a> subclasses that use the <tt>Q_OBJECT</tt> macro have a reimplementation of the <tt>tr()</tt> function.</p><p>Although the <tt>tr()</tt> call is normally made directly since it is usually called as a member function of a <a href="qobject.html">QObject</a> subclass, in other cases an explicit class name can be supplied, for example:</p><pre>    QPushButton::tr( "&amp;Quit" )</pre><p>or</p><pre>    QObject::tr( "&amp;Quit" )</pre><h4><a name="1-4"></a>Distinguishing Identical Strings That Require Different Translations</h4><!-- index Translation Contexts --><!-- index Contexts!for Translation --><!-- index lupdate --><p>The <a href="linguist-manual-2.html#2">lupdate</a> program automatically provides a <em>context</em> for every source text. This context is the class name of the class that contains the <tt>tr()</tt> call. This is sufficient in the vast majority of cases. Sometimes however, the translator will need further information to uniquely identify a source text; for example, a dialog that contained two separate frames, each of which contained an "Enabled" option would need each identified because in some languages the translation would differ between the two. This is easily achieved using the two argument form of the <tt>tr()</tt> call, e.g.</p><pre>    rbc = new QRadioButton( tr( "Enabled", "Color frame" ), this);</pre><p>and</p><pre>    rbh = new QRadioButton( tr( "Enabled", "Hue frame" ), this);</pre><!-- index Ctrl Key --><p>Ctrl key accelerators are also translatable:</p><pre>        file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "E&amp;xit" ), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()),                          QAccel::<a href="qaccel.html#stringToKey">stringToKey</a>( <a href="qobject.html#tr">tr</a>("Ctrl+Q", "Quit") ) );</pre> <p>It is strongly recommended that the two argument form of <tt>tr()</tt> is used for Ctrl key accelerators. The second argument is the only clue the translator has as to the function performed by the accelerator.</p><h4><a name="1-5"></a>Helping The Translator With Navigation Information</h4><!-- index TRANSLATOR!in Comments --><!-- index Translator Comments --><!-- index Comments!for Translators --><p>In large complex applications it may be difficult for the translator to see where a particular source text comes from. This problem can be solved by adding a comment using the keyword <em>TRANSLATOR</em> which describes the navigation steps to reach the text in question; e.g.</p><pre>    /*  TRANSLATOR FindDialog	Choose Edit|Find from the menu bar or press Ctrl+F to pop up the	Find dialog.    */</pre><p>These comments are particularly useful for widget classes.</p><h4><a name="1-6"></a>Coping With C++ Namespaces</h4><!-- index Namespaces --><!-- index C++!Namespaces --><!-- index lupdate --><p>C++ namespaces and the <tt>using namespace</tt> statement can confuse <a href="linguist-manual-2.html#2">lupdate</a>. It will interpret <tt>MyClass::tr()</tt> as meaning just that, not as <tt>MyNamespace::MyClass::tr()</tt>, even if <tt>MyClass</tt> is defined in the <tt>MyNamespace</tt> namespace. Run-time translation of these strings will fail because of that.</p><!-- index TRANSLATOR!in Comments --><!-- index Translator Comments --><!-- index Comments!for Translators --><p>You can work around this limitation by putting a <em>TRANSLATOR</em> comment at the beginning of the source files that use <tt>MyClass::tr()</tt>:</p><pre>    /* TRANSLATOR MyNamespace::MyClass */</pre><p>After the comment, all references to <tt>MyClass::tr()</tt> will be understood as meaning <tt>MyNamespace::MyClass::tr()</tt>.</p><h4><a name="1-7"></a>Translating Text that is Outside of a QObject subclass</h4><h5><a name="1-7-1"></a>Using QApplication::translate()</h5><p>If the quoted text is not in a member function of a QObject subclass, use either the tr() function of an appropriate class, or the QApplication::translate() function directly:</p><pre>    void some_global_function( LoginWidget *logwid )    {        QLabel *label = new QLabel(                LoginWidget::tr("Password:"), logwid );    }    void same_global_function( LoginWidget *logwid )    {        QLabel *label = new QLabel(                qApp-&gt;translate("LoginWidget", "Password:"),                logwid );    }</pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -