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

📄 linguist-manual-4.html

📁 QT 下载资料仅供参考
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<h5><a name="1-7-2"></a>Using QT_TR_NOOP and QT_TRANSLATE_NOOP</h5><p>If you need to have translatable text completely outside a function, there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP(). These macros merely mark the text for extraction by <a href="linguist-manual-2.html#2">lupdate</a>. The macros expand to just the text (without the context).</p><p>Example of QT_TR_NOOP():</p><pre>    QString FriendlyConversation::greeting( int greet_type )    {        static const char* greeting_strings[] = {            QT_TR_NOOP( "Hello" ),            QT_TR_NOOP( "Goodbye" )        };        return tr( greeting_strings[greet_type] );    }</pre><p>Example of QT_TRANSLATE_NOOP():</p><pre>    static const char* greeting_strings[] = {        QT_TRANSLATE_NOOP( "FriendlyConversation", "Hello" ),        QT_TRANSLATE_NOOP( "FriendlyConversation", "Goodbye" )    };    QString FriendlyConversation::greeting( int greet_type )    {        return tr( greeting_strings[greet_type] );    }    QString global_greeting( int greet_type )    {        return qApp-&gt;translate( "FriendlyConversation",                                greeting_strings[greet_type] );    }</pre><h3><a name="2"></a>Tutorials</h3><p>Three tutorials are presented. The first demonstrates the creation of a <a href="qtranslator.html">QTranslator</a> object. It also shows the simplest use of the <tt>tr()</tt> function to mark user-visible source text for translation. The second tutorial explains how to make the application load the translation file applicable to the current locale. It also shows the use of the two-argument form of <tt>tr()</tt> which provides additional information to the translator. The third tutorial explains how identical source texts can be distinguished even when they occur in the same context. This tutorial also discusses how the translation tools help minimize the translator's work when an application is upgraded.</p><h4><a name="2-1"></a>Tutorial 1: Loading and Using Translations</h4><p align="center"><img align="middle" src="tt1_en.png" width="112" height="49"></p><blockquote><p align="center"><em>Tutorial 1 Screenshot, English version</em></p></blockquote><pre>TEMPLATE        = appCONFIG          += qt warn_onSOURCES         = main.cppTRANSLATIONS    = tt1_la.ts</pre><blockquote><p align="center"><em><tt>tt1.pro</tt></em></p></blockquote><pre>/******************************************************************** Translation tutorial 1******************************************************************/#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;#include &lt;<a href="qtranslator-h.html">qtranslator.h</a>&gt;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 );    <a href="qpushbutton.html">QPushButton</a> hello( QPushButton::<a href="qobject.html#tr">tr</a>( "Hello world!" ), 0 );    app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;hello );    hello.<a href="qwidget.html#show">show</a>();    return app.<a href="qapplication.html#exec">exec</a>();}</pre><blockquote><p align="center"><em><tt>main.cpp</tt></em></p></blockquote><p>This example is a reworking of the <a href="tutorial1-01.html">"hello-world"</a> example from <a href="tutorial.html">Tutorial #1</a>, with a Latin translation. The <em>Tutorial 1 Screenshot, English version</em>, above, shows the English version.</p><h5><a name="2-1-1"></a>Line by Line Walk-through</h5><pre>    #include &lt;<a href="qtranslator-h.html">qtranslator.h</a>&gt;</pre><!-- index QTranslator --> <p>This line includes the definition of the <a href="qtranslator.html">QTranslator</a> class. Objects of this class provide translations for user-visible text.</p><pre>        <a href="qtranslator.html">QTranslator</a> translator( 0 );</pre> <p>Creates a <a href="qtranslator.html">QTranslator</a> object without a parent.</p><pre>        translator.<a href="qtranslator.html#load">load</a>( "tt1_la", "." );</pre><!-- index tt1_la.qm --> <p>Try to load a file called <tt>tt1_la.qm</tt> (the <tt>.qm</tt> file extension is implicit) that contains Latin translations for the source texts used in the program. No error will occur if the file is not found.</p><!-- index QApplication!installTranslator() --><!-- index installTranslator()!QApplication --><pre>        app.<a href="qapplication.html#installTranslator">installTranslator</a>( &amp;translator );</pre> <p>Add the translations from <tt>tt1_la.qm</tt> to the pool of translations used by the program. No error will occur if the file is not found.</p><!-- index Hello World --><pre>        <a href="qpushbutton.html">QPushButton</a> hello( QPushButton::<a href="qobject.html#tr">tr</a>( "Hello world!" ), 0 );</pre> <p>Creates a push button that displays "Hello world!". If <tt>tt1_la.qm</tt> was found and contains a translation for "Hello world!", the translation appears; if not, the source text appears.</p><!-- index tr() --><!-- index QObject!tr() --><p>All classes that inherit <a href="qobject.html">QObject</a> have a <tt>tr()</tt> function. Inside a member function of a <a href="qobject.html">QObject</a> class, we simply write <tt>tr("Hello world!")</tt> instead of <tt>QPushButton::tr("Hello world!")</tt> or <tt>QObject::tr("Hello world!")</tt>.</p><h5><a name="2-1-2"></a>Running the Application in English</h5><!-- index English Language --><p>Since we haven't made the translation file <tt>tt1_la.qm</tt>, the source text is shown when we run the application:</p><p align="center"><img align="middle" src="tt1_en.png" width="112" height="49"></p><blockquote><p align="center"><em>Tutorial 1 Screenshot, English version</em></p></blockquote><h5><a name="2-1-3"></a>Creating a Latin Message File</h5><!-- index tt1.pro --><!-- index Latin --><p>The first step is to create a project file, <tt>tt1.pro</tt>, that lists all the source files for the project. The project file can be a qmake project file, or even an ordinary makefile. Any file that contains</p><!-- index SOURCES!in Project Files --><!-- index TRANSLATIONS!in Project Files --><pre>    SOURCES         = main.cpp    TRANSLATIONS    = tt1_la.ts</pre> <p>will work. <em>TRANSLATIONS</em> specifies the message files we want to maintain. In this example, we just maintain one set of translations, namely Latin.</p><!-- index .ts Files --><!-- index Translation Source Files --><!-- index .qm Files --><!-- index Qt Message Files --><p>Note that the file extension is <tt>.ts</tt>, not <tt>.qm</tt>. The <tt>.ts</tt> translation source format is designed for use during the application's development. Programmers or release managers run the <a href="linguist-manual-2.html#2">lupdate</a> program to generate and update <tt>.ts</tt> files with the source text that is extracted from the source code. Translators read and update the <tt>.ts</tt> files using <em>Qt Linguist</em> adding and editing their translations.</p><!-- index XML --><p>The <tt>.ts</tt> format is human-readable XML that can be emailed directly and is easy to put under version control. If you edit this file manually, be aware that the default encoding for XML is UTF-8, not Latin-1 (ISO 8859-1). One way to type in a Latin-1 character such as '&oslash;' (Norwegian o with slash) is to use an XML entity: "&amp;#xf8;". This will work for any Unicode character.</p><p>Once the translations are complete the <a href="linguist-manual-2.html#3">lrelease</a> program is used to convert the <tt>.ts</tt> files into the <tt>.qm</tt> Qt message file format. The <tt>.qm</tt> format is a compact binary format designed to deliver very fast lookup performance. Both <a href="linguist-manual-2.html#2">lupdate</a> and <a href="linguist-manual-2.html#3">lrelease</a> read all the project's source and header files (as specified in the HEADERS and SOURCES lines of the project file) and extract the strings that appear in <tt>tr()</tt> function calls.</p><!-- index lupdate --><p><a href="linguist-manual-2.html#2">lupdate</a> is used to create and update the message files (<tt>tt1_la.ts</tt> in this case) to keep them in sync with the source code. It is safe to run <a href="linguist-manual-2.html#2">lupdate</a> at any time, as <a href="linguist-manual-2.html#2">lupdate</a> does not remove any information. For example, you can put it in the makefile, so the <tt>.ts</tt> files are updated whenever the source changes.</p><p>Try running <a href="linguist-manual-2.html#2">lupdate</a> right now, like this:</p><pre>    lupdate tt1.pro</pre><!-- index .ts Files --><!-- index Translation Source Files --><!-- index XML --><p>You should now have a file <tt>tt1_la.ts</tt> in the current directory, containing this</p><pre>    &lt;!DOCTYPE TS&gt;&lt;TS&gt;    &lt;context&gt;        &lt;name&gt;QPushButton&lt;/name&gt;        &lt;message&gt;            &lt;source&gt;Hello world!&lt;/source&gt;            &lt;translation type="unfinished"&gt;&lt;/translation&gt;        &lt;/message&gt;    &lt;/context&gt;    &lt;/TS&gt;</pre><p>You don't need to understand the file format since it is read and updated using tools, e.g. <a href="linguist-manual-2.html#2">lupdate</a> and <em>Qt Linguist</em>.</p><h5><a name="2-1-4"></a>Translating to Latin with Qt Linguist</h5><!-- index Qt Linguist --><!-- index Linguist --><p>We will use <em>Qt Linguist</em> to provide the translation, although you can use any XML or plain text editor to enter a translation into a <tt>.ts</tt> file.</p><p>To start <em>Qt Linguist</em>, type</p><pre>    linguist tt1_la.ts</pre><p>You should now see the text "QPushButton" in the top left pane. Double-click it, then click on "Hello world!" and enter "Orbis, te saluto!" in the <em>Translation</em> pane (the middle right of the window). Don't forget the exclamation mark!</p><p>Click the <em>Done</em> checkbox and choose <em>File|Save</em> from the menu bar. The <tt>.ts</tt> file will no longer contain</p><pre>    &lt;translation type='unfinished'&gt;&lt;/translation&gt;</pre><p>but instead will have</p><pre>    &lt;translation&gt;Orbis, te saluto!&lt;/translation&gt;</pre><h5><a name="2-1-5"></a>Running the Application in Latin</h5><!-- index Latin --><!-- index lrelease --><p>To see the application running in Latin, we have to generate a <tt>.qm</tt> file from the <tt>.ts</tt> file. Generating a <tt>.qm</tt> file can be achieved either from within <em>Qt Linguist</em> (for a single <tt>.ts</tt> file), or by using the command line program <a href="linguist-manual-2.html#3">lrelease</a> which will produce one <tt>.qm</tt> file for each of the <tt>.ts</tt> files listed in the project file. Generate <tt>tt1_la.qm</tt> from <tt>tt1_la.ts</tt> by choosing <em>File|Release</em> from <em>Qt Linguist</em>'s menu bar and pressing <em>Save</em> in the file save dialog that pops up. Now run the <em>tt1</em> example program again. This time the button will be labelled "Orbis, te saluto!".</p><p align="center"><img align="middle" src="tt1_la.png" width="112" height="49"></p><blockquote><p align="center"><em>Tutorial 1 Screenshot, Latin version</em></p></blockquote><h4><a name="2-2"></a>Tutorial 2: Using Two or More Languages</h4><p align="center"><img align="middle" src="tt2_en.png" width="170" height="157">

⌨️ 快捷键说明

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