Realize multilingual function in qt

introduction

When working on a project, sometimes we hope that our program can be used in different countries, so the best way is that a set of programs can be adapted to multiple languages.
Qt provides such a function, so that a set of programs can present different language interfaces. This article will introduce how QT implements multilingual.

example

environment

windows10 + QtCreator4.8.2(Qt 5.12.2)+MSVC2017_32bit
insert image description here

demo

Create application-based qt projects.
insert image description here
dialog.h

#define DIALOG_H

#include <QDialog>

namespace Ui {
    
    
class Dialog;
}

class Dialog : public QDialog
{
    
    
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

    void testMultiLangguage();//多语言测试
protected:
private slots:
private:
    Ui::Dialog *ui;  
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QLabel>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    
    
    ui->setupUi(this);

    testMultiLangguage();
}

Dialog::~Dialog()
{
    
    
    delete ui;
}

void Dialog::testMultiLangguage()
{
    
    
    QLabel *label = new QLabel(tr("hello Qt!"),this);
    label->move(QPoint(100,30));
    QLabel *label2 = new QLabel(tr("password"),this);
    label2->move(100,80);
    QLabel *label3 = new QLabel(tr("yafei"),this);
    label3->move(280,30);
}

main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);

    Dialog w;
    w.show();
    
    return a.exec();
}

Make the project multilingual

Its essence is to generate ts files, then manually translate, then generate qm files, and finally load different qm files at the beginning of the main program to achieve multilingualism.

step one

After writing the code, add the following statement to the pro file:

TRANSLATIONS += hahh_zh_CN.ts \
                english_eg.ts

In this way, ts files suitable for Chinese and English can be generated finally.
== Note: There is no need to manually create the ts file yourself, just add the name of the ts file to be generated in the pro file. ==
The name of the Chinese ts file generated here is hahh_zh_CN.ts, where the zh_CN in the file name is often used as the second half of the generated Chinese ts file name, and the previous file name can be used casually. The generated English ts file is named english_eg.ts.
After writing the pro file, click Save. Then click Tools - "External -" Linguist - "update translation (lupdate). That is, use the lupdate tool to generate the ts file added in the pro file.
You will see the figure below in the summary information of qt:
insert image description here
At this time, the ts file has been generated using the lupdate tool.

step two

After generating the ts file, click the start menu of the computer, find the Qt installation folder in the application list bar that appears, find Linguist, select the Linguist tool that suits you, and double-click to open it. The choice of Linguist here needs to be selected according to the compiler of your own project. Since I use MSVC2017_32bit, Linguist of MSVC2017_32bit.
insert image description here
After opening Linguist, click File-"Open, select the ts file generated by the project in the file selection box, and click Open.
insert image description here
The strings and class names that need to be translated will be displayed in Linguist. There is a hello icon in front of the list under the string, and you can move up and down to different items by clicking the arrows in the menu bar. Click the front and back arrows to move to the item you need to translate, and perform manual translation in the translation area below. The second column is the translation comment content. You can not write the comment. After translating an item, use Ctrl+Enter to automatically translate Jump to the next string to be translated. After a string is translated, the question mark icon in front of the string list above will turn into a green check mark. The picture above shows the translated interface.
** Remarks: Here you can set the language you want to translate into. Click Edit -> Translate Text Settings, a dialog box will pop up. Select the desired language and country in Target Language. **
insert image description here

step three

After the translation is complete,If it needs to be saved, you can use the ctrl+s shortcut key to save, or you can click File-"Save in the menu bar. After saving, it will be published and a qm file will be generated.
There are two operation methods to generate qm files.

method one

Click File -> Publish in Linguist.

way two

In QtCreator, click Tools - "External -" Linguist - "Publish Translation (lrealse).

step four

After generating the qm file, add the following code to the main.cpp file to load the qm file. Generally add code to QApplication a(argc, argv); next line.
main.cpp

#include "dialog.h"
#include <QApplication>
#include <QTranslator>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    QTranslator translator;
//    translator.load("../QTabWidgetDemo/hahh_zh_CN.qm");
    translator.load("../QTabWidgetDemo/english_eg.qm");
    a.installTranslator(&translator);
    Dialog w;
    w.show();
    return a.exec();
}

Loading different qm files will display different languages ​​on the interface.

attach

[1] When there are double quotation marks in the Chinese-English translation, you can translate the English double quotation marks into Chinese double quotation marks, and then insert the text to be emphasized by the double quotation marks into the double quotation marks.
Something like this:

QString strDoubleMark = QObject::tr("\"\"");  
QString strTechnology = QObject::tr("Technology");
QString strStenosisMsg +=
            // 在双引号中插入字符串
            strDoubleMark.insert(1, strTechnology);

Among them, m_translate.strDoubleMark corresponds to English double quotation marks during translation. Here, you only need to insert text into the first position of the double quotation mark, and the display effect can be realized: "technical".
[2] The translated text can be added with tr or QObject::tr. The difference between the two is as follows:

QString strTechnology = QObject::tr("Technology");
QString strTechnology = tr("Technology");

Adding tr to the above code will generate translation text in the current class, as shown in the figure below.
insert image description here
And the translated text in QObject::tr will be generated in the QObject file. As shown below:
insert image description here

Guess you like

Origin blog.csdn.net/blqzj214817/article/details/127396411