Guide to the use and avoidance of QT translation

1. Introduction

The benefits of using translators

1. When qt uses the msvc compiler, Chinese displays garbled characters, which can be effectively solved by using translation

2. The text prompts on the interface often need to be changed. You only need to re-publish a translation file without changing the source code.

2. Translation process

 

1. Configuration: Project pro is added to TRANSLATIONS  

TRANSLATIONS  +=  xxx_zh.ts  \
        xxx_en.ts

2. The text to be translated needs to be included with tr()

    ui->ribbonTabWidget->addTab(tr("Project"));
    ui->ribbonTabWidget->addTab(tr("Setting"));
    ui->ribbonTabWidget->addTab(tr("Basic tools"));
    ui->ribbonTabWidget->addTab(tr("Advanced tools"));

3. Update: Open cmd and run lupdate xxx.pro to extract the text to be translated from the source code

Note: You must use cmd under qt

 

 4. Translation: use the translation tool Qt Linguist, open the ts file, and complete the translation work

5. Publish: Qt Linguist file = "Publish, generate xxx.qm

6. Load the translation file in main.cpp

QTranslator appTranslator;
appTranslator.load(app_path + "/translations/czvision_zh.qm");
app.installTranslator(&appTranslator);

3. Frequently Asked Questions

1. QDialogButtonBox translation does not take effect

This is a bug in qt, https://bugreports.qt.io/browse/QTBUG-39180

The solution is as follows: Edit src/qttranslations/translations/qt_zh_CN.ts, replace QDialogButtonBox with QPlatformTheme, and republish

2. Multiple pro projects, multiple translation problems, defining a translator does not take effect

Solution: Multiple translators must be defined, and one cannot be shared

QTranslator qtTranslator;
QTranslator appTranslator;
QTranslator coreTranslator;

qtTranslator.load(app_path + "/translations/qt_zh_CN.qm");
appTranslator.load(app_path + "/translations/czvision_zh.qm");
coreTranslator.load(app_path + "/translations/CZCore_zh.qm");

app.installTranslator(&qtTranslator);
app.installTranslator(&appTranslator);
app.installTranslator(&coreTranslator);

3. Only classes that inherit QObject can use the tr() function. How to translate non-Qt classes?

Workaround: use QCoreApplication::tr(),

splash.showProgressMessage(1, QCoreApplication::tr("Load config file"));

Guess you like

Origin blog.csdn.net/qq_40602000/article/details/123881731