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

📄 linguist-programmer.leaf

📁 Linux下的基于X11的图形开发环境。
💻 LEAF
📖 第 1 页 / 共 3 页
字号:
    setenv LANG fr\endcode\index\index autoexec.bat\index set!Windows CommandIn Windows, either modify \c autoexec.bat or run\code    set LANG=fr\endcodeWhen you run the program, you should now see the French version:\img tt2_fr.png\caption Tutorial 2 Screenshot, French versionTry the same with Dutch, by setting \c LANG=nl. Now the Dutchversion should appear:\img tt2_nl.png\caption Tutorial 2 Screenshot, Dutch version\section3 ExercisesMark one of the translations in \e {Qt Linguist} as not done, i.e.by unchecking the "done" checkbox; run \l lupdate, then \l lrelease,then the example. What effect did this change have?\index Canada\index French CanadaSet \c LANG=fr_CA (French Canada) and run the example program again. Explain why the result is the same as with \c LANG=fr.Change one of the accelerators in the Dutch translation to eliminate theconflict between \e \&Bestand and \e \&Boven.\section2 Tutorial 3: Disambiguating Identical Strings\img tt3_10_en.png\caption Tutorial 3 Screenshot, "Troll Print 1.0", English version\include tt3/tt3.pro\caption \c tt3.pro\index Portuguese Language\index Brazilian LanguageWe've included a translation file, \c tt3_pt.ts, which contains somePortuguese translations for this example.\index Troll PrintWe will consider two releases of the same application: Troll Print1.0 and 1.1. We will learn to reuse the translations created for onerelease in a subsequent release. (In this tutorial, you need to editsome source files. It's probably best to copy all the files to a newtemporary directory and work from there.)Troll Print is a toy example application that lets the user chooseprinter settings. It comes in two versions: English and Portuguese.Version 1.0 consists of these files:\index tt3.pro\index tt3_pt.ts\list\i \c printpanel.h contains the definition of PrintPanel;\i \c printpanel.cpp contains the implementation of PrintPanel;\i \c mainwindow.h contains the definition of \c MainWindow;\i \c mainwindow.cpp contains the implementation of \c MainWindow;\i \c main.cpp contains main();\i \c tt3.pro is the \e qmake project file.\i \c tt3_pt.ts is the Portuguese message file.\endlist\section3 Line by Line Walk-throughThe PrintPanel is defined in \c printpanel.h.\quotefile tt3/printpanel.h\skipto QVBox\printline QVBox\printuntil Q_OBJECT\index Q_OBJECT\index PrintPanel!in Translation TutorialPrintPanel is a \l QWidget. It needs the \c Q_OBJECT macro for \ctr() to work properly.The implementation file is \c printpanel.cpp.\quotefile tt3/printpanel.cpp\skipto setSpacing\skipto /\printline /\printline\printline\printline\index Troll PrintSome of the code is commented out in Troll Print 1.0; you will uncommentit later, for Troll Print 1.1.\quotefile tt3/printpanel.cpp\skipto twoSided\printline twoSided\printuntil toggle\printline\printuntil toggleNotice the two occurrences of \c tr("Enabled") and of \ctr("Disabled") in PrintPanel. Since both "Enabled"s and "Disabled"sappear in the same context \e {Qt Linguist} will only display oneoccurrence of each and will use the same translations for theduplicates that it doesn't display. Whilst this is a usefultimesaver, in some languages, such as Portuguese, the secondoccurrence requires a separate translation. We will see how \e {QtLinguist} can be made to display all the occurrences for separatetranslation shortly.\index MainWindow!in Translation TutorialThe header file for \c MainWindow, \c mainwindow.h, contains nosurprises. In the implementation, \c mainwindow.cpp, we have someuser-visible source texts that must be marked for translation.\quotefile tt3/mainwindow.cpp\skipto setCaption\printline setCaptionWe must translate the window's caption.\skipto quit\printline quit\printuntil HelpWe also need to translate the menu items. Note that the two argumentform of \c tr() is used for the keyboard accelerator, "Ctrl+Q", sincethe second argument is the only clue the translator has to indicatewhat function that accelerator will perform.\quotefile tt3/main.cpp\skipto QTranslator\printuntil installTranslator\index main()The \c main() function in \c main.cpp is the same as the one in \link{Tutorial 2...} Tutorial 2 \endlink. In particular it chooses atranslation file based on the current locale.\section3 Running Troll Print 1.0 in English and in PortugueseWe will use the translations in the \c tt3_pt.ts file that is provided.Set the \c LANG environment variable to \c pt, and then run \c tt3. You should still see the English version, as shown in the \e{Tutorial 3 Screenshot, "Troll Print 1.0", English version}, above.Now run \l lrelease, e.g. \c {lrelease tt3.pro}, and then run theexample again. Now you should see the Portuguese edition (TrollImprimir 1.0):\img tt3_10_pt_bad.png\caption Tutorial 3 Screenshot, "Troll Imprimir 1.0", (Bad) Portuguese versionWhilst the translation has appeared correctly, it is in fact wrong. Ingood Portuguese, the second occurrence of "Enabled" should be"Ativadas", not "Ativado" and the ending for the second translation of"Disabled" must change similarly too.If you open \c tt3_pt.ts using \e {Qt Linguist}, you will see thatthere is just one occurrence of "Enabled" and of "Disabled" in thetranslation source file, even though there are two of each in thesource code. This is because \e {Qt Linguist} tries to minimize thetranslator's work by using the same translation for duplicate sourcetexts. In cases such as this where an identical translation is wrong,the programmer must disambiguate the duplicate occurrences. This iseasily achieved by using the two argument form of \c tr().We can easily determine which file must be changed because thetranslator's "context" is in fact the class name for the class wherethe texts that must be changed appears. In this case the file is \cprintpanel.cpp, where the there are four lines to change. Add thesecond argument "two-sided" in the appropriate \c tr() calls to thefirst pair of radio buttons:\code    but = new QRadioButton( tr("Enabled", "two-sided"), twoSided );    but = new QRadioButton( tr("Disabled", "two-sided"), twoSided );\endcodeand add the second argument "colors" in the appropriate \c tr() callsfor the second pair of radio buttons:\code    but = new QRadioButton( tr("Enabled", "colors"), colors );    but = new QRadioButton( tr("Disabled", "colors"), colors );\endcode\index lupdate\index tt3_pt.tsNow run \l lupdate and open \c tt3_pt.ts with \e {Qt Linguist}. Youshould now see two changes.First, the translation source file now contains \e three "Enabled","Disabled" pairs. The first pair is marked "(obs.)" signifying that theyare obsolete. This is because these texts appeared in \c tr() calls thathave been replaced by new calls with two arguments. The second pair has"two-sided" as their comment, and the third pair has "colors" as theircomment. The comments are shown in the \e {Source text and comments}area in \e {Qt Linguist}.Second, the translation text "Ativado" and "Desativado" have beenautomatically used as translations for the new "Enabled" and "Disabled"texts, again to minimize the translator's work. Of course in this casethese are not correct for the second occurrence of each word, but theyprovide a good starting point.Change the second "Ativado" into "Ativadas" and the second"Desativado" into "Desativadas", then save and quit. Run \l lreleaseto obtain an up-to-date binary \c tt3_pt.qm file, and run Troll Print(or rather Troll Imprimir).\img tt3_10_pt_good.png\caption Tutorial 3 Screenshot, "Troll Imprimir 1.0", (Good) Portuguese version\index Translator Comments\index Comments!for TranslatorsThe second argument to \c tr() calls, called "comments" in \e {QtLinguist}, distinguish between identical source texts that occur inthe same context (class). They are also useful in other cases to giveclues to the translator, and in the case of Ctrl key accelerators arethe only means of conveying the function performed by the accelerator tothe translator.\index TRANSLATOR!in Comments\index Translator Comments\index Comments!for TranslatorsAn additional way of helping the translator is to provide information onhow to navigate to the particular part of the application that containsthe source texts they must translate. This helps them see the contextin which the translation appears and also helps them to find and testthe translations. This can be achieved by using a \e TRANSLATOR commentin the source code:\code    /* 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.    */\endcodeTry adding these comments to some source files, particularly todialog classes, describing the navigation necessary to reach thedialogs. You could also add them to the example files, e.g. \cmainwindow.cpp and \c printpanel.cpp are appropriate files. Run \llupdate and then start \e {Qt Linguist} and load in \c tt3_pt.ts. You should see the comments in the \e {Source text and comments} areaas you browse through the list of source texts.Sometimes, particularly with large programs, it can be difficult forthe translator to find their translations and check that they'recorrect. Comments that provide good navigation information can savethem time:\code    /* 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.    */\endcode\section3 Troll Print 1.1We'll now prepare release 1.1 of Troll Print. Start your favorite texteditor and follow these steps:\list\i Uncomment the two lines that create a \l QLabel with the text     "\<b\>TROLL PRINT\</b\>" in \c printpanel.cpp.\i Word-tidying: Replace "2-sided" by "Two-sided" in \c printpanel.cpp.\i Replace "1.0" with "1.1" everywhere it occurs in \c mainwindow.cpp.\i Update the copyright year to 1999-2000 in \c mainwindow.cpp.\endlist(Of course the version number and copyright year would be consts or#defines in a real application.)Once finished, run \l lupdate, then open \c tt3_pt.ts in \e {QtLinguist}. The following items are of special interest:\list\i \c MainWindow     \list     \i Troll Print 1.0 - marked "(obs.)", obsolete     \i About Troll Print 1.0 - marked "(obs.)", obsolete     \i Troll Print 1.0. Copyright 1999 Macroshaft, Inc. -	  marked "(obs.)", obsolete     \i Troll Print 1.1 - automatically translated as	  "Troll Imprimir 1.1"     \i About Troll Print 1.1 - automatically translated as	  "Troll Imprimir 1.1"     \i Troll Print 1.1. Copyright 1999-2000 Macroshaft,	  Inc. - automatically translated as "Troll Imprimir 1.1.	  Copyright 1999-2000 Macroshaft, Inc."     \endlist\i \c PrintPanel     \list     \i 2-sided - marked "(obs.)", obsolete     \i \<b\>TROLL PRINT\</b\> - unmarked, i.e. untranslated     \i Two-sided - unmarked, i.e. untranslated.     \endlist\endlistNotice that \l lupdate works hard behind the scenes to make revisionseasier, and it's pretty smart with numbers.Go over the translations in \c MainWindow and mark these as "done".Translate "\<b\>TROLL PRINT\</b\>" as "\<b\>TROLL IMPRIMIR\</b\>".When you're translating "Two-sided", press the \e {Guess Again}button to translate "Two-sided", but change the "2" into "Dois".Save and quit, then run \l lrelease. The Portuguese versionshould look like this:\img tt3_11_pt.png\caption Tutorial 3 Screenshot, "Troll Imprimir 1.1", Portuguese versionChoose \e{Ajuda|Sobre}, (\e{Help|About}), to see the about box\img tt3_11_about_pt.png\caption Tutorial 3 Screenshot, About box, Portuguese version\index English Language\index Translating Qt\index Qt!Translating QtIf you choose \e {Ajuda|Sobre Qt}, (\e {Help|About Qt}), you'll getan English dialog. Oops! Qt itself needs to be translated. See thedocument \link i18n.html#qt-itself Internationalization with Qt\endlink for details.Now set \c LANG=en to get the original English version:\img tt3_11_en.png\caption Tutorial 3 Screenshot, "Troll Print 1.1", English version\section2 SummaryThese tutorials cover all that you need to know to prepare your Qtapplications for translation.At the beginning of a project add the translation source files to beused to the project file and add calls to \l lupdate and \l lrelease tothe make file.During the project all the programmer must do is wrap any user-visibletext in \c tr() calls. They should also use the two argument form forCtrl key accelerators, or when asked by the translator for the caseswhere the same text translates into two different forms in the samecontext. The programmer should also include \e TRANSLATION comments tohelp the translator navigate the application.

⌨️ 快捷键说明

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