一张图说明上下文,源字符串二者的关系,如果代码中某个字符串被删除了但是 TS 文件中还存在,而且今后不需要这个翻译,就可以用记事本打开 TS 文件,直接删除以 vanished 标记的翻译:
回到 retranslateUi() 函数,这是一个自定义的函数,用户在这里翻译GUI界面,如果发现GUI上某个 widget 语言没有变化,就需要在这里翻译,如下面的代码翻译标签和按钮的文本,以及两个工具栏的文本:
```C++ // add your own translate here. act_language_zh->setText(QApplication::translate("QObject", "Chinese", nullptr)); act_language_en->setText(QApplication::translate("QObject", "English", nullptr)); lbl_hello->setText(QApplication::translate("QObject", "<h2><i>Hello</i><font color=red>Qt!</font></h2>", nullptr)); btn_quit->setText(QApplication::translate("QObject", "&Quit", nullptr)); ```上面的代码中标签文本是一个 HTML 代码,这个也可以翻译的, HTML 样式保留,只需要替换里面的 Hello 和 Qt! 即可。
源代码只列出关键的源代码,首先是布局文件 myUI_HelloQt.h
#pragma once #ifndef MYUI_HELLOQT_H #define MYUI_HELLOQT_H #include <QtCore/QVariant> #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenuBar> #include <QtWidgets/QStatusBar> #include <QtWidgets/QToolBar> #include <QtWidgets/QWidget> #include <QtWidgets/QMenu> #include <QtWidgets/QAction> #include <QtWidgets/QLabel> #include <QtWidgets/QPushButton> #include <QtWidgets/QVBoxLayout> QT_BEGIN_NAMESPACE class Ui_HelloQtClass { public: QMenuBar *menuBar; QToolBar *mainToolBar; QWidget *centralWidget; QStatusBar *statusBar; QAction *act_language_zh; QAction *act_language_en; QLabel *lbl_hello; QPushButton *btn_quit; void setupUi(QMainWindow *HelloQtClass) { if (HelloQtClass->objectName().isEmpty()) HelloQtClass->setObjectName(QStringLiteral("HelloQtClass")); //HelloQtClass->resize(600, 400); HelloQtClass->setFixedSize(400, 160); menuBar = new QMenuBar(HelloQtClass); menuBar->setObjectName(QStringLiteral("menuBar")); HelloQtClass->setMenuBar(menuBar); mainToolBar = new QToolBar(HelloQtClass); mainToolBar->setObjectName(QStringLiteral("mainToolBar")); HelloQtClass->addToolBar(mainToolBar); centralWidget = new QWidget(HelloQtClass); centralWidget->setObjectName(QStringLiteral("centralWidget")); // add actions here. act_language_zh = new QAction(QObject::tr("Chinese"), HelloQtClass); act_language_en = new QAction(QObject::tr("English"), HelloQtClass); mainToolBar->addAction(act_language_zh); mainToolBar->addAction(act_language_en); // add your widgets here. lbl_hello = new QLabel(HelloQtClass); lbl_hello->setText(QObject::tr("<h2><i>Hello</i><font color=red>Qt!</font></h2>")); lbl_hello->setAlignment(Qt::AlignHCenter); btn_quit = new QPushButton(QObject::tr("&Quit"), HelloQtClass); QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(lbl_hello); vLayout->addWidget(btn_quit); centralWidget->setLayout(vLayout); HelloQtClass->setCentralWidget(centralWidget); statusBar = new QStatusBar(HelloQtClass); statusBar->setObjectName(QStringLiteral("statusBar")); HelloQtClass->setStatusBar(statusBar); retranslateUi(HelloQtClass); QMetaObject::connectSlotsByName(HelloQtClass); } // setupUi void retranslateUi(QMainWindow *HelloQtClass) { // NOTE: the first parameter `context` in translate() must match the `context` in Qt Linguist. // [static] QString QCoreApplication::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) HelloQtClass->setWindowTitle(QApplication::translate("HelloQtClass", "HelloQt", nullptr)); // add your own translate here. act_language_zh->setText(QApplication::translate("QObject", "Chinese", nullptr)); act_language_en->setText(QApplication::translate("QObject", "English", nullptr)); lbl_hello->setText(QApplication::translate("QObject", "<h2><i>Hello</i><font color=red>Qt!</font></h2>", nullptr)); btn_quit->setText(QApplication::translate("QObject", "&Quit", nullptr)); } // retranslateUi }; namespace Ui { class HelloQtClass : public Ui_HelloQtClass {}; } // namespace Ui QT_END_NAMESPACE #endif // MYUI_HELLOQT_H