更新用于稍后发行的翻译。
Troll Print 是让用户选取打印机设置的范例应用程序。它带有 2 个版本:英语和葡萄牙语。
We've included a translation file,
trollprint_pt.ts
, which contains some Portuguese translations for this example.
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 need to edit some source files. It's probably best to copy all the files to a new temporary directory and work from there.)
见 Qt Linguist 手册 了解翻译 Qt 应用程序的更多有关信息。
The
PrintPanel
类的定义在
printpanel.h
.
class PrintPanel : public QWidget { Q_OBJECT
PrintPanel
是 QWidget。它需要
Q_OBJECT
宏为
tr()
to work properly.
实现文件是
printpanel.cpp
.
PrintPanel::PrintPanel(QWidget *parent) : QWidget(parent) { /* QLabel *label = new QLabel(tr("<b>TROLL PRINT</b>")); label->setAlignment(Qt::AlignCenter); */
Some of the code is commented out in Troll Print 1.0; you will uncomment it later, for Troll Print 1.1.
twoSidedGroupBox = new QGroupBox(tr("2-sided")); twoSidedEnabledRadio = new QRadioButton(tr("Enabled")); twoSidedDisabledRadio = new QRadioButton(tr("Disabled")); twoSidedDisabledRadio->setChecked(true); colorsGroupBox = new QGroupBox(tr("Colors")); colorsEnabledRadio = new QRadioButton(tr("Enabled")); colorsDisabledRadio = new QRadioButton(tr("Disabled"));
Notice the two occurrences of
tr("Enabled")
and of
tr("Disabled")
in PrintPanel. Since both "Enabled"s and "Disabled"s appear in the same context
Qt Linguist
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
Qt Linguist
can be made to display all the occurrences for separate translation shortly.
The header file for
MainWindow
,
mainwindow.h
, contains no surprises. In the implementation,
mainwindow.cpp
, we have some user-visible source texts that must be marked for translation.
setWindowTitle(tr("Troll Print 1.0"));
We must translate the window title.
void MainWindow::createActions() { exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q", "Quit")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); aboutAct = new QAction(tr("&About"), this); aboutAct->setShortcut(Qt::Key_F1); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); aboutQtAct = new QAction(tr("About &Qt"), this); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); } void MainWindow::createMenus() { QMenu *fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(exitAct); menuBar()->addSeparator(); QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(aboutAct); helpMenu->addAction(aboutQtAct); }
We also need to translate the actions and menus. Note that the two argument form of
tr()
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.
QTranslator translator; if (translator.load(locale, u"trollprint"_s, u"_"_s)) app.installTranslator(&translator);
The
main()
函数在
main.cpp
is the same as the one in the
Arrow Pad
example. In particular, it chooses a translation file based on the current locale.
We will use the translations in the
trollprint_pt.ts
file that is provided.
设置
LANG
环境变量到
pt
, and then run
trollprint
. You should still see the English version. Now run
lrelease
,如
lrelease trollprint.pro
, and then run the example again. Now you should see the Portuguese edition (Troll Imprimir 1.0):
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.
若打开
trollprint_pt.ts
使用
Qt Linguist
, 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
Qt Linguist
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
tr()
.
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
printpanel.cpp
, where there are four lines to change. Add the second argument "two-sided" in the appropriate
tr()
calls to the first pair of radio buttons:
twoSidedEnabledRadio = new QRadioButton(tr("Enabled", "two-sided")); twoSidedDisabledRadio = new QRadioButton(tr("Disabled", "two-sided"));
and add the second argument "colors" in the appropriate
tr()
calls for the second pair of radio buttons:
colorsEnabledRadio = new QRadioButton(tr("Enabled", "colors"), colors); colorsDisabledRadio = new QRadioButton(tr("Disabled", "colors"), colors);
现在运行
lupdate
and open
trollprint_pt.ts
with
Qt Linguist
. You should now see two changes.
First, the translation source file now contains
three
"Enabled", "Disabled" pairs. The first pair is marked "(obs.)" signifying that they are obsolete. This is because these texts appeared in
tr()
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
Source text and comments
area in
Qt Linguist
.
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.
Change the second "Ativado" into "Ativadas" and the second "Desativado" into "Desativadas", then save and quit. Run
lrelease
to obtain an up-to-date binary
trollprint_pt.qm
file, and run Troll Print (or rather Troll Imprimir).
The second argument to
tr()
calls, called "comments" in
Qt Linguist
, 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.
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
TRANSLATOR
comment in the source 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.
...
*/
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.
mainwindow.cpp
and
printpanel.cpp
are appropriate files. Run
lupdate
and then start
Qt Linguist
and load in
trollprint_pt.ts
. You should see the comments in the
Source text and comments
area as you browse through the list of source texts.
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:
/*
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.
*/
We'll now prepare release 1.1 of Troll Print. Start your favorite text editor and follow these steps:
printpanel.cpp
.
printpanel.cpp
.
mainwindow.cpp
.
mainwindow.cpp
.
(Of course the version number and copyright year would be consts or #defines in a real application.)
Once finished, run
lupdate
, then open
trollprint_pt.ts
in
Qt Linguist
. The following items are of special interest:
MainWindow
PrintPanel
预告
lupdate
works hard behind the scenes to make revisions easier, and it's pretty smart with numbers.
Go over the translations in
MainWindow
and mark these as "done". Translate "<b>TROLL PRINT</b>" as "<b>TROLL IMPRIMIR</b>". When you're translating "Two-sided", press the
Guess Again
button to translate "Two-sided", but change the "2" into "Dois".
保存并离开,然后运行
lrelease
。Portuguese (葡萄牙语) 版本应该外观像这样:
Choose Ajuda|Sobre ( Help|About ) to see the about box.
If you choose Ajuda|Sobre Qt ( Help|About Qt ), you'll get an English dialog. Oops! Qt itself needs to be translated. See Qt 国际化 了解细节。
现在设置
LANG=en
以获取原始 English 版: