巨魔打印示例

为后续版本更新翻译。

Troll Print 是一款允许用户选择打印机设置的示例应用程序。它有两个版本:英语和葡萄牙语。

我们提供了一个翻译文件trollprint_pt.ts ,其中包含该示例的一些葡萄牙语翻译。

我们将考虑同一应用程序的两个版本:Troll Print 1.0 和 1.1。我们将学习在后续版本中重复使用为一个版本创建的翻译。(在本教程中,您需要编辑一些源文件。最好将所有文件复制到一个新的临时目录,然后从那里开始工作)。

有关翻译 Qt 应用程序的更多信息,请参阅Qt Linguist Manual

逐行查看

PrintPanel 类定义在printpanel.h 中。

class PrintPanel : public QWidget
{
    Q_OBJECT

PrintPanel 是一个 QWidget。它需要 宏 才能正常工作。Q_OBJECT tr()

实现文件是printpanel.cpp

PrintPanel::PrintPanel(QWidget *parent)
    : QWidget(parent)
{
/*
    QLabel *label = new QLabel(tr("<b>TROLL PRINT</b>"));
    label->setAlignment(Qt::AlignCenter);
*/

在 Troll Print 1.0 中,部分代码已注释;在 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"));

请注意 PrintPanel 中出现的两个tr("Enabled")tr("Disabled") 。由于 "已启用 "和 "已禁用 "出现在同一上下文中 Qt Linguist因此,PrintPanel 将只显示其中一个,并对不显示的重复内容使用相同的翻译。虽然这样可以节省时间,但在某些语言(如葡萄牙语)中,第二次出现时需要单独翻译。我们将看到 Qt Linguist可以显示所有出现的单独翻译。

MainWindow 的头文件mainwindow.h 并不包含意外内容。在mainwindow.cpp 的实现中,我们有一些用户可见的源文本必须标记为需要翻译。

    setWindowTitle(tr("Troll Print %1").arg(version));

我们必须翻译窗口标题。

void MainWindow::createActions()
{
    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q", "Quit"));
    connect(exitAct, &QAction::triggered, this, &MainWindow::close);

    aboutAct = new QAction(tr("&About"), this);
    aboutAct->setShortcut(Qt::Key_F1);
    connect(aboutAct, &QAction::triggered, this, &MainWindow::about);

    aboutQtAct = new QAction(tr("About &Qt"), this);
    connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::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);
}

我们还需要翻译操作和菜单。请注意,tr() 的两个参数形式用于键盘加速器 "Ctrl+Q",因为第二个参数是翻译人员指明该加速器将执行什么功能的唯一线索。

    QTranslator translator;
    if (translator.load(locale, u"trollprint"_s, u"_"_s))
        app.installTranslator(&translator);

main.cpp 中的main() 函数与箭头垫示例中的函数相同。特别是,它根据当前的本地语言选择翻译文件。

以英语和葡萄牙语运行 Troll Print 1.0

我们将使用trollprint_pt.ts 文件中提供的翻译。

LANG 环境变量设置为pt ,然后运行trollprint 。你应该仍然能看到英文版本。现在运行lrelease ,例如lrelease trollprint.pro ,然后再次运行示例。现在你应该能看到葡萄牙语版本(Troll Imprimir 1.0):

虽然翻译正确,但实际上是错误的。在良好的葡萄牙语中,"Enabled "的第二次出现应该是 "Ativadas",而不是 "Ativado","Disabled "的第二次翻译的结尾也必须做类似的更改。

如果使用trollprint_pt.ts 打开 Qt Linguist打开 ,您会发现翻译源文件中只有一个 "Enabled "和一个 "Disabled",而源代码中却各有两个。这是因为 Qt Linguist试图通过对重复的源代码文本使用相同的翻译来减少译员的工作量。在这种情况下,如果相同的译文是错误的,程序员就必须消除重复出现的译文。使用tr() 的两个参数形式就可以轻松做到这一点。

我们可以很容易地确定哪个文件必须修改,因为译者的 "上下文 "实际上就是必须修改的文本所在类的类名。本例中的文件是printpanel.cpp ,其中有四行需要修改。在对第一对单选按钮的相应tr() 调用中添加第二个参数 "two-sided":

twoSidedEnabledRadio = new QRadioButton(tr("Enabled", "two-sided"));
twoSidedDisabledRadio = new QRadioButton(tr("Disabled", "two-sided"));

并在tr() 中为第二对单选按钮的相应调用添加第二个参数 "colors":

colorsEnabledRadio = new QRadioButton(tr("Enabled", "colors"), colors);
colorsDisabledRadio = new QRadioButton(tr("Disabled", "colors"), colors);

现在重新编译、运行lupdate 并用以下命令打开trollprint_pt.ts Qt Linguist.现在您应该看到两个变化。

首先,翻译源文件现在包含三个"启用"、"禁用 "对。第一对标记为"(obs.)",表示它们已经过时。这是因为这些文本出现在tr() 调用中,但已被带有两个参数的新调用所取代。第二对的注释是 "双面",第三对的注释是 "颜色"。注释显示在Source text and comments 中的 Qt Linguist.

其次,翻译文本 "Ativado "和 "Desativado "被自动用作新的 "Enabled "和 "Disabled "文本的翻译,这也是为了尽量减少翻译工作。当然,在这种情况下,这两个词的第二次出现并不正确,但它们提供了一个很好的起点。

将第二个 "Ativado "改为 "Ativadas",将第二个 "Desativado "改为 "Desativadas",然后保存并退出。运行lrelease 获取最新的二进制trollprint_pt.qm 文件,然后运行 Troll Print(或者说 Troll Imprimir)。

tr() 调用的第二个参数称为 "注释"。 Qt Linguist中称为 "注释",用于区分出现在同一上下文(类)中的相同源文本。在其他情况下,它们还能为译者提供线索,而在 Ctrl 键加速器的情况下,它们是向译者传达加速器功能的唯一途径。

帮助译员的另一种方法是提供信息,说明如何导航到应用程序中包含他们必须翻译的源文本的特定部分。这可以帮助译员了解译文出现的上下文,还可以帮助他们查找和测试译文。这可以通过在源代码中使用TRANSLATOR 注释来实现:

/*
   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.

   ...
*/

尝试在一些源文件中添加这些注释,特别是在对话框类中,描述到达对话框所需的导航。您也可以将这些注释添加到示例文件中,例如mainwindow.cppprintpanel.cpp 就是合适的文件。运行lupdate ,然后启动 Qt Linguist并载入trollprint_pt.ts 。当您浏览源代码文本列表时,应该会在Source text and comments 区域看到注释。

有时,特别是在大型程序中,译员很难找到自己的译文并检查它们是否正确。提供良好导航信息的注释可以节省他们的时间:

/*
   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.
*/

巨魔打印 1.1

我们现在准备发布 Troll Print 1.1 版。启动你最喜欢的文本编辑器,然后按照以下步骤操作:

  • 取消注释在printpanel.cpp 中创建 QLabel 的两行,其中包含文本"<b>TROLL PRINT</b>"。
  • 整理文字:将printpanel.cpp 中的 "2-sided "替换为 "Two-sided"。
  • mainwindow.cpp 中的 "1.0 "改为 "1.1"。
  • mainwindow.cpp 中的版权年份更新为 1999-2000 年。

(当然,在实际应用中,版本号和版权年都是常量或 #defines)。

完成后,运行lupdate ,然后在trollprint_pt.ts 中打开 Qt Linguist.以下项目值得特别关注:

  • PrintPanel
    • 双面 - 标记为"(obs.)",已过时
    • <b>滚动打印</b> - 无标记,即未翻译
    • 双面 - 无标记,即未翻译。

为了便于修订,您可以使用 tr("Troll Print %1").arg("1.0") 或类似字串来代替 tr("Troll Print 1.0"),以避免每次发布时都要更新字串。

查看MainWindow 中的翻译,并将其标记为 "已完成"。将"<b>TROLL PRINT</b>"翻译为"<b>TROLL IMPRIMIR</b>"。翻译 "Two-sided "时,按Guess Again 按钮翻译 "Two-sided",但将 "2 "改为 "Dois"。

保存并退出,然后运行lrelease 。葡萄牙语版本应该是这样的:

选择Ajuda|Sobre (Help|About) 查看 "关于 "框。

如果选择Ajuda|Sobre Qt (Help|About Qt),则会出现英文对话框。哎呀!Qt 本身需要翻译。有关详情,请参阅Qt 的国际化

现在设置LANG=en 以获得原始英文版本:

示例项目 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.