📄 linguist-manual-4.html
字号:
<pre> class PrintPanel : public <a href="qvbox.html">QVBox</a> { Q_OBJECT</pre><!-- index Q_OBJECT --><!-- index PrintPanel!in Translation Tutorial --> <p>PrintPanel is a <a href="qwidget.html">QWidget</a>. It needs the <tt>Q_OBJECT</tt> macro for <tt>tr()</tt> to work properly.</p><p>The implementation file is <tt>printpanel.cpp</tt>.</p><pre> /* <a href="qlabel.html">QLabel</a> *lab = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "<b>TROLL PRINT</b>" ), this ); lab-><a href="qlabel.html#setAlignment">setAlignment</a>( <a href="qt.html#AlignmentFlags-enum">AlignCenter</a> ); */</pre><!-- index Troll Print --> <p>Some of the code is commented out in Troll Print 1.0; you will uncomment it later, for Troll Print 1.1.</p><pre> <a href="qhbuttongroup.html">QHButtonGroup</a> *twoSided = new <a href="qhbuttongroup.html">QHButtonGroup</a>( this ); twoSided-><a href="qgroupbox.html#setTitle">setTitle</a>( <a href="qobject.html#tr">tr</a>( "2-sided" ) ); but = new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Enabled" ), twoSided ); but = new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Disabled" ), twoSided ); but-><a href="qbutton.html#toggle">toggle</a>(); <a href="qhbuttongroup.html">QHButtonGroup</a> *colors = new <a href="qhbuttongroup.html">QHButtonGroup</a>( this ); colors-><a href="qgroupbox.html#setTitle">setTitle</a>( <a href="qobject.html#tr">tr</a>( "Colors" ) ); but = new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Enabled" ), colors ); but = new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Disabled" ), colors ); but-><a href="qbutton.html#toggle">toggle</a>();</pre> <p>Notice the two occurrences of <tt>tr("Enabled")</tt> and of <tt>tr("Disabled")</tt> in PrintPanel. Since both "Enabled"s and "Disabled"s appear in the same context <em>Qt Linguist</em> will only display one occurrence of each and will use the same translations for the duplicates that it doesn't display. Whilst this is a useful timesaver, in some languages, such as Portuguese, the second occurrence requires a separate translation. We will see how <em>Qt Linguist</em> can be made to display all the occurrences for separate translation shortly.</p><!-- index MainWindow!in Translation Tutorial --><p>The header file for <tt>MainWindow</tt>, <tt>mainwindow.h</tt>, contains no surprises. In the implementation, <tt>mainwindow.cpp</tt>, we have some user-visible source texts that must be marked for translation.</p><pre> <a href="qwidget.html#setCaption">setCaption</a>( <a href="qobject.html#tr">tr</a>( "Troll Print 1.0" ) );</pre> <p>We must translate the window's caption.</p><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") ) ); <a href="qpopupmenu.html">QPopupMenu</a> *help = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); help-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "&About" ), this, SLOT(about()), Key_F1 ); help-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "About &Qt" ), this, SLOT(aboutQt()) ); <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>( "&File" ), file ); <a href="qmainwindow.html#menuBar">menuBar</a>()->insertSeparator(); <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>( "&Help" ), help );</pre> <p>We also need to translate the menu items. Note that the two argument form of <tt>tr()</tt> is used for the keyboard accelerator, "Ctrl+Q", since the second argument is the only clue the translator has to indicate what function that accelerator will perform.</p><pre> <a href="qtranslator.html">QTranslator</a> translator( 0 ); translator.<a href="qtranslator.html#load">load</a>( QString( "tt3_" ) + QTextCodec::locale(), "." ); app.<a href="qapplication.html#installTranslator">installTranslator</a>( &translator );</pre><!-- index main() --> <p>The <tt>main()</tt> function in <tt>main.cpp</tt> is the same as the one in <a href="linguist-manual-4.html#2-2">Tutorial 2</a>. In particular it chooses a translation file based on the current locale.</p><h5><a name="2-3-2"></a>Running Troll Print 1.0 in English and in Portuguese</h5><p>We will use the translations in the <tt>tt3_pt.ts</tt> file that is provided.</p><p>Set the <tt>LANG</tt> environment variable to <tt>pt</tt>, and then run <tt>tt3</tt>. You should still see the English version, as shown in the <em>Tutorial 3 Screenshot, "Troll Print 1.0", English version</em>, above. Now run <a href="linguist-manual-2.html#3">lrelease</a>, e.g. <tt>lrelease tt3.pro</tt>, and then run the example again. Now you should see the Portuguese edition (Troll Imprimir 1.0):</p><p align="center"><img align="middle" src="tt3_10_pt_bad.png" width="208" height="177"></p><blockquote><p align="center"><em>Tutorial 3 Screenshot, "Troll Imprimir 1.0", (Bad) Portuguese version</em></p></blockquote><p>Whilst the translation has appeared correctly, it is in fact wrong. In good Portuguese, the second occurrence of "Enabled" should be "Ativadas", not "Ativado" and the ending for the second translation of "Disabled" must change similarly too.</p><p>If you open <tt>tt3_pt.ts</tt> using <em>Qt Linguist</em>, you will see that there is just one occurrence of "Enabled" and of "Disabled" in the translation source file, even though there are two of each in the source code. This is because <em>Qt Linguist</em> tries to minimize the translator's work by using the same translation for duplicate source texts. In cases such as this where an identical translation is wrong, the programmer must disambiguate the duplicate occurrences. This is easily achieved by using the two argument form of <tt>tr()</tt>.</p><p>We can easily determine which file must be changed because the translator's "context" is in fact the class name for the class where the texts that must be changed appears. In this case the file is <tt>printpanel.cpp</tt>, where the there are four lines to change. Add the second argument "two-sided" in the appropriate <tt>tr()</tt> calls to the first pair of radio buttons:</p><pre> but = new QRadioButton( tr("Enabled", "two-sided"), twoSided ); but = new QRadioButton( tr("Disabled", "two-sided"), twoSided );</pre><p>and add the second argument "colors" in the appropriate <tt>tr()</tt> calls for the second pair of radio buttons:</p><pre> but = new QRadioButton( tr("Enabled", "colors"), colors ); but = new QRadioButton( tr("Disabled", "colors"), colors );</pre><!-- index lupdate --><!-- index tt3_pt.ts --><p>Now run <a href="linguist-manual-2.html#2">lupdate</a> and open <tt>tt3_pt.ts</tt> with <em>Qt Linguist</em>. You should now see two changes.</p><p>First, the translation source file now contains <em>three</em> "Enabled", "Disabled" pairs. The first pair is marked "(obs.)" signifying that they are obsolete. This is because these texts appeared in <tt>tr()</tt> calls that have been replaced by new calls with two arguments. The second pair has "two-sided" as their comment, and the third pair has "colors" as their comment. The comments are shown in the <em>Source text and comments</em> area in <em>Qt Linguist</em>.</p><p>Second, the translation text "Ativado" and "Desativado" have been automatically used as translations for the new "Enabled" and "Disabled" texts, again to minimize the translator's work. Of course in this case these are not correct for the second occurrence of each word, but they provide a good starting point.</p><p>Change the second "Ativado" into "Ativadas" and the second "Desativado" into "Desativadas", then save and quit. Run <a href="linguist-manual-2.html#3">lrelease</a> to obtain an up-to-date binary <tt>tt3_pt.qm</tt> file, and run Troll Print (or rather Troll Imprimir).</p><p align="center"><img align="middle" src="tt3_10_pt_good.png" width="208" height="177"></p><blockquote><p align="center"><em>Tutorial 3 Screenshot, "Troll Imprimir 1.0", (Good) Portuguese version</em></p></blockquote><!-- index Translator Comments --><!-- index Comments!for Translators --><p>The second argument to <tt>tr()</tt> calls, called "comments" in <em>Qt Linguist</em>, distinguish between identical source texts that occur in the same context (class). They are also useful in other cases to give clues to the translator, and in the case of Ctrl key accelerators are the only means of conveying the function performed by the accelerator to the translator.</p><!-- index TRANSLATOR!in Comments --><!-- index Translator Comments --><!-- index Comments!for Translators --><p>An additional way of helping the translator is to provide information on how to navigate to the particular part of the application that contains the source texts they must translate. This helps them see the context in which the translation appears and also helps them to find and test the translations. This can be achieved by using a <em>TRANSLATOR</em> comment in the source code:</p><pre> /* TRANSLATOR MainWindow In this application the whole application is a MainWindow. Choose Help|About from the menu bar to see some text belonging to MainWindow. */</pre><p>Try adding these comments to some source files, particularly to dialog classes, describing the navigation necessary to reach the dialogs. You could also add them to the example files, e.g. <tt>mainwindow.cpp</tt> and <tt>printpanel.cpp</tt> are appropriate files. Run <a href="linguist-manual-2.html#2">lupdate</a> and then start <em>Qt Linguist</em> and load in <tt>tt3_pt.ts</tt>. You should see the comments in the <em>Source text and comments</em> area as you browse through the list of source texts.</p><p>Sometimes, particularly with large programs, it can be difficult for the translator to find their translations and check that they're correct. Comments that provide good navigation information can save them time:</p><pre> /* TRANSLATOR ZClientErrorDialog Choose Client|Edit to reach the Client Edit dialog, then choose Client Specification from the drop down list at the top and pick client Bartel Leendert van der Waerden. Now check the Profile checkbox and then click the Start Processing button. You should now see a pop up window with the text "Error: Name too long!". This window is a ZClientErrorDialog. */</pre><h5><a name="2-3-3"></a>Troll Print 1.1</h5><p>We'll now prepare release 1.1 of Troll Print. Start your favorite text editor and follow these steps:</p><ul><li><p>Uncomment the two lines that create a <a href="qlabel.html">QLabel</a> with the text "<b>TROLL PRINT</b>" in <tt>printpanel.cpp</tt>.</p><li><p>Word-tidying: Replace "2-sided" by "Two-sided" in <tt>printpanel.cpp</tt>.</p><li><p>Replace "1.0" with "1.1" everywhere it occurs in <tt>mainwindow.cpp</tt>.</p><li><p>Update the copyright year to 1999-2000 in <tt>mainwindow.cpp</tt>.</p></ul><p>(Of course the version number and copyright year would be consts or #defines in a real application.)</p><p>Once finished, run <a href="linguist-manual-2.html#2">lupdate</a>, then open <tt>tt3_pt.ts</tt> in <em>Qt Linguist</em>. The following items are of special interest:</p><ul><li><p><tt>MainWindow</tt></p><ul><li><p>Troll Print 1.0 - marked "(obs.)", obsolete</p><li><p>About Troll Print 1.0 - marked "(obs.)", obsolete</p><li><p>Troll Print 1.0. Copyright 1999 Macroshaft, Inc. - marked "(obs.)", obsolete</p><li><p>Troll Print 1.1 - automatically translated as "Troll Imprimir 1.1"</p><li><p>About Troll Print 1.1 - automatically translated as "Troll Imprimir 1.1"</p><li><p>Troll Print 1.1. Copyright 1999-2000 Macroshaft, Inc. - automatically translated as "Troll Imprimir 1.1. Copyright 1999-2000 Macroshaft, Inc."</p></ul><li><p><tt>PrintPanel</tt></p><ul><li><p>2-sided - marked "(obs.)", obsolete</p><li><p><b>TROLL PRINT</b> - unmarked, i.e. untranslated</p><li><p>Two-sided - unmarked, i.e. untranslated.</p></ul></ul><p>Notice that <a href="linguist-manual-2.html#2">lupdate</a> works hard behind the scenes to make revisions easier, and it's pretty smart with numbers.</p><p>Go over the translations in <tt>MainWindow</tt> and mark these as "done". Translate "<b>TROLL PRINT</b>" as "<b>TROLL IMPRIMIR</b>". When you're translating "Two-sided", press the <em>Guess Again</em> button to translate "Two-sided", but change the "2" into "Dois".</p><p>Save and quit, then run <a href="linguist-manual-2.html#3">lrelease</a>. The Portuguese version should look like this:</p><p align="center"><img align="middle" src="tt3_11_pt.png" width="198" height="187"></p><blockquote><p align="center"><em>Tutorial 3 Screenshot, "Troll Imprimir 1.1", Portuguese version</em></p></blockquote><p>Choose <em>Ajuda|Sobre</em>, (<em>Help|About</em>), to see the about box</p><p align="center"><img align="middle" src="tt3_11_about_pt.png" width="262" height="139"></p><blockquote><p align="center"><em>Tutorial 3 Screenshot, About box, Portuguese version</em></p></blockquote><!-- index English Language --><!-- index Translating Qt --><!-- index Qt!Translating Qt --><p>If you choose <em>Ajuda|Sobre Qt</em>, (<em>Help|About Qt</em>), you'll get an English dialog... oops! Qt itself needs to be translated. See the document <a href="i18n.html#qt-itself">Internationalization with Qt</a> for details.</p><p>Now set <tt>LANG=en</tt> to get the original English version:</p><p align="center"><img align="middle" src="tt3_11_en.png" width="208" height="189"></p><blockquote><p align="center"><em>Tutorial 3 Screenshot, "Troll Print 1.1", English version</em></p></blockquote><h4><a name="2-4"></a>Summary</h4><p>These tutorials cover all that you need to know to prepare your Qt applications for translation.</p><p>At the beginning of a project add the translation source files to be used to the project file and add calls to <a href="linguist-manual-2.html#2">lupdate</a> and <a href="linguist-manual-2.html#3">lrelease</a> to the make file.</p><p>During the project all the programmer must do is wrap any user-visible text in <tt>tr()</tt> calls. They should also use the two argument form for Ctrl key accelerators, or when asked by the translator for the cases where the same text translates into two different forms in the same context. The programmer should also include <em>TRANSLATION</em> comments to help the translator navigate the application.</p><!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -