📄 linguist-manual-4.html
字号:
</p><blockquote><p align="center"><em>Tutorial 2 Screenshot, English version</em></p></blockquote><!-- index .pro Files --><!-- index Project Files --><!-- index qmake!Project Files --><pre>TEMPLATE = appCONFIG += qt warn_onHEADERS = arrowpad.h \ mainwindow.hSOURCES = arrowpad.cpp \ main.cpp \ mainwindow.cppTRANSLATIONS = tt2_fr.ts \ tt2_nl.ts</pre><blockquote><p align="center"><em>tt2.pro</em></p></blockquote><!-- index Translation Contexts --><!-- index Contexts!for Translation --><p>This example is a slightly more involved and introduces a key <em>Qt Linguist</em> concept: "contexts".</p><ul><li><p><tt>arrowpad.h</tt> contains the definition of <tt>ArrowPad</tt>, a custom widget;</p><li><p><tt>arrowpad.cpp</tt> contains the implementation of <tt>ArrowPad</tt>;</p><li><p><tt>mainwindow.h</tt> contains the definition of <tt>MainWindow</tt>, a subclass of <a href="qmainwindow.html">QMainWindow</a></p><li><p><tt>mainwindow.cpp</tt> contains the implementation of <tt>MainWindow</tt>;</p><li><p><tt>main.cpp</tt> contains main().</p></ul><!-- index tt2.pro --><!-- index French Language --><!-- index Dutch Language --><p>We will use two translations, French and Dutch, although there is no effective limit on the number of possible translations that can be used with an application. The relevant lines of <tt>tt2.pro</tt> are</p><pre> HEADERS = arrowpad.h \ mainwindow.h SOURCES = arrowpad.cpp \ main.cpp \ mainwindow.cpp TRANSLATIONS = tt2_fr.ts \ tt2_nl.ts</pre><!-- index lupdate --><!-- index tt2_fr.ts --><!-- index tt2_nl.ts --> <p>Run <a href="linguist-manual-2.html#2">lupdate</a>; it should produce two identical message files <tt>tt2_fr.ts</tt> and <tt>tt2_nl.ts</tt>. These files will contain all the source texts marked for translation with <tt>tr()</tt> calls and their contexts.</p><h5><a name="2-2-1"></a>Line by Line Walk-through</h5><!-- index ArrowPad!in Translation Tutorial --><!-- index English Language --><p>In <tt>arrowpad.h</tt> we define the <tt>ArrowPad</tt> subclass which is a subclass of <a href="qwidget.html">QWidget</a>. In the <em>Tutorial 2 Screenshot, English version</em>, above, the central widget with the four buttons is an <tt>ArrowPad</tt>.</p><pre> class ArrowPad : public <a href="qgrid.html">QGrid</a></pre><!-- index Q_OBJECT --><!-- index tr() --><!-- index QObject!tr() --><!-- index Translation Contexts --><!-- index Contexts!for Translation --> <p>When <a href="linguist-manual-2.html#2">lupdate</a> is run it not only extracts the source texts but it also groups them into contexts. A context is the name of the class in which the source text appears. Thus, in this example, "ArrowPad" is a context: it is the context of the texts in the <tt>ArrowPad</tt> class. The <tt>Q_OBJECT</tt> macro defines <tt>tr(x)</tt> in <tt>ArrowPad</tt> like this</p><!-- index QApplication!translate() --><!-- index translate()!QApplication --><pre> qApp->translate( "ArrowPad", x )</pre><p>Knowing which class each source text appears in enables <em>Qt Linguist</em> to group texts that are logically related together, e.g. all the text in a dialog will have the context of the dialog's class name and will be shown together. This provides useful information for the translator since the context in which text appears may influence how it should be translated. For some translations keyboard accelerators may need to be changed and having all the source texts in a particular context (class) grouped together makes it easier for the translator to perform any accelerator changes without introducing conflicts.</p><p>In <tt>arrowpad.cpp</tt> we implement the <tt>ArrowPad</tt> class.</p><pre> (void) new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "&Up" ), this );</pre> <p>We call <tt>ArrowPad::tr()</tt> for each button's label since the labels are user-visible text.</p><p align="center"><img align="middle" src="tt2_en.png" width="170" height="157"></p><blockquote><p align="center"><em>Tutorial 2 Screenshot, English version</em></p></blockquote><!-- index Q_OBJECT --><!-- index MainWindow!in Translation Tutorial --><pre> class MainWindow : public <a href="qmainwindow.html">QMainWindow</a> { Q_OBJECT</pre> <p>In the <em>Tutorial 2 Screenshot, English version</em>, above, the whole window is a <tt>MainWindow</tt>. This is defined in the <tt>mainwindow.h</tt> header file. Here too, we use <tt>Q_OBJECT</tt>, so that <tt>MainWindow</tt> will become a context in <em>Qt Linguist</em>.</p><p>In the implementation of <tt>MainWindow</tt>, <tt>mainwindow.cpp</tt>, we create an instance of our <tt>ArrowPad</tt> class</p><pre> ArrowPad *ap = new ArrowPad( this, "arrow pad" );</pre> <p>We also call <tt>MainWindow::tr()</tt> twice, once for the menu item and once for the accelerator.</p><!-- index QAccel!stringToKey() --><!-- index stringToKey()!QAccel --><!-- index Ctrl Key --><!-- index Alt Key --><pre> file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "E&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>Note the use of <a href="qaccel.html#stringToKey">QAccel::stringToKey</a>() to support different keys in other languages. "Ctrl+Q" is a good choice for Quit in English, but a Dutch translator might want to use "Ctrl+A" (for Afsluiten) and a German translator "Strg+E" (for Beenden). When using <tt>tr()</tt> for Ctrl key accelerators the two argument form should be used with the second argument describing the function that the accelerator performs.</p><!-- index main() --><p>Our <tt>main()</tt> function is defined in <tt>main.cpp</tt> as usual.</p><pre> <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>( &translator );</pre><!-- index QTextCodec!locale() --><!-- index locale()!QTextCodec --><!-- index LANG!Environment Variable --><!-- index Environment Variables!LANG --> <p>We choose which translation to use according to the current locale. <a href="qtextcodec.html#locale">QTextCodec::locale</a>() can be influenced by setting the <tt>LANG</tt> environment variable, for example. Notice that the use of a naming convention that incorporates the locale for <tt>.qm</tt> message files, (and <tt>.ts</tt> files), makes it easy to implement choosing the translation file according to locale.</p><p>If there is no <tt>.qm</tt> message file for the locale chosen the original source text will be used and no error raised.</p><h5><a name="2-2-2"></a>Translating to French and Dutch</h5><p>We'll begin by translating the example application into French. Start <em>Qt Linguist</em> with <tt>tt2_fr.ts</tt>. You should get the seven source texts ("&Up", "&Left", etc.) grouped in two contexts ("ArrowPad" and "MainWindow").</p><p>Now, enter the following translations:</p><ul><li><p><tt>ArrowPad</tt></p><ul><li><p>&Up - &Haut</p><li><p>&Left - &Gauche</p><li><p>&Right - &Droite</p><li><p>&Down - &Bas</p></ul><li><p><tt>MainWindow</tt></p><ul><li><p>E&xit - &Quitter</p><li><p>Ctrl+Q - Ctrl+Q</p><li><p>&File - &Fichier</p></ul></ul><p>It's quickest to press <b>Alt+D</b> (which clicks the <em>Done & Next</em> button) after typing each translation, since this marks the translation as done and moves on to the next source text.</p><p>Save the file and do the same for Dutch working with <tt>tt2_nl.ts</tt>:</p><ul><li><p><tt>ArrowPad</tt></p><ul><li><p>&Up - &Boven</p><li><p>&Left - &Links</p><li><p>&Right - &Rechts</p><li><p>&Down - &Onder</p></ul><li><p><tt>MainWindow</tt></p><ul><li><p>E&xit - &Afsluiten</p><li><p>Ctrl+Q - Ctrl+A</p><li><p>File - &Bestand</p></ul></ul><p>We have to convert the <tt>tt1_fr.ts</tt> and <tt>tt1_nl.ts</tt> translation source files into <tt>.qm</tt> files. We could use <em>Qt Linguist</em> as we've done before; however using the command line tool <a href="linguist-manual-2.html#3">lrelease</a> ensures that <em>all</em> the <tt>.qm</tt> files for the application are created without us having to remember to load and <em>File|Release</em> each one individually from <em>Qt Linguist</em>.</p><p>In practice we would include calls to <a href="linguist-manual-2.html#2">lupdate</a> and <a href="linguist-manual-2.html#3">lrelease</a> in the application's makefile to ensure that the latest translations are used.</p><p>Type</p><pre> lrelease tt2.pro</pre><!-- index LANG!Environment Variable --><!-- index export!Unix Command --><!-- index setenv!Unix Command --><p>This should create both <tt>tt2_fr.qm</tt> and <tt>tt2_nl.qm</tt>. Set the <tt>LANG</tt> environment variable to <tt>fr</tt>. In Unix, one of the two following commands should work</p><pre> export LANG=fr setenv LANG fr</pre><!-- index --><!-- index autoexec.bat --><!-- index set!Windows Command --><p>In Windows, either modify <tt>autoexec.bat</tt> or run</p><pre> set LANG=fr</pre><p>When you run the program, you should now see the French version:</p><p align="center"><img align="middle" src="tt2_fr.png" width="208" height="177"></p><blockquote><p align="center"><em>Tutorial 2 Screenshot, French version</em></p></blockquote><p>Try the same with Dutch, by setting <tt>LANG=nl</tt>. Now the Dutch version should appear:</p><p align="center"><img align="middle" src="tt2_nl.png" width="208" height="177"></p><blockquote><p align="center"><em>Tutorial 2 Screenshot, Dutch version</em></p></blockquote><h5><a name="2-2-3"></a>Exercises</h5><p>Mark one of the translations in <em>Qt Linguist</em> as not done, i.e. by unchecking the "done" checkbox; run <a href="linguist-manual-2.html#2">lupdate</a>, then <a href="linguist-manual-2.html#3">lrelease</a>, then the example. What effect did this change have?</p><!-- index Canada --><!-- index French Canada --><p>Set <tt>LANG=fr_CA</tt> (French Canada) and run the example program again. Explain why the result is the same as with <tt>LANG=fr</tt>.</p><p>Change one of the accelerators in the Dutch translation to eliminate the conflict between <em></em>&Bestand and <em></em>&Boven.</p><h4><a name="2-3"></a>Tutorial 3: Disambiguating Identical Strings</h4><p align="center"><img align="middle" src="tt3_10_en.png" width="208" height="177"></p><blockquote><p align="center"><em>Tutorial 3 Screenshot, "Troll Print 1.0", English version</em></p></blockquote><pre>TEMPLATE = appCONFIG += qt warn_onHEADERS = mainwindow.h \ printpanel.hSOURCES = main.cpp \ mainwindow.cpp \ printpanel.cppTRANSLATIONS = tt3_pt.ts</pre><blockquote><p align="center"><em><tt>tt3.pro</tt></em></p></blockquote><!-- index Portuguese Language --><!-- index Brazilian Language --><p>We've included a translation file, <tt>tt3_pt.ts</tt>, which contains some Portuguese translations for this example.</p><!-- index Troll Print --><p>We will consider two releases of the same application: Troll Print 1.0 and 1.1. We will learn to reuse the translations created for one release in a subsequent release. (In this tutorial, you have to edit some source files. It's probably best to copy all the files to a new temporary directory and work from there.)</p><p>Troll Print is a toy example application that lets the user choose printer settings. It comes in two versions: English and Portuguese.</p><p>Version 1.0 consists of these files:</p><!-- index tt3.pro --><!-- index tt3_pt.ts --><ul><li><p><tt>printpanel.h</tt> contains the definition of PrintPanel;</p><li><p><tt>printpanel.cpp</tt> contains the implementation of PrintPanel;</p><li><p><tt>mainwindow.h</tt> contains the definition of <tt>MainWindow</tt>;</p><li><p><tt>mainwindow.cpp</tt> contains the implementation of <tt>MainWindow</tt>;</p><li><p><tt>main.cpp</tt> contains main();</p><li><p><tt>tt3.pro</tt> is the <em>qmake</em> project file.</p><li><p><tt>tt3_pt.ts</tt> is the Portuguese message file.</p></ul><h5><a name="2-3-1"></a>Line by Line Walk-through</h5><p>The PrintPanel is defined in <tt>printpanel.h</tt>.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -