QT development (9) - Qt realizes dynamic switching of languages within the application, and uses Qt language home to compile font packages

QT's multi-language is relatively good, let's see how to do it

It can be roughly divided into the following steps

  • 1.update generates ts file
  • 2. Use the QT language home to generate the qm file
  • 3. Load the language pack

Let's first look at how to generate a ts file. In fact, this is very simple. We have two ways. The first is to use the IDE to compile it ourselves. We need to add configuration information to the pro file.

TRANSLATIONS = Tranlate_EN.ts\
               Tranlate_CH.ts

This statement supports the languages ​​of two countries, CH and EN, we click Tools-External-Qt Oracle-Update Translation

write picture description here

You can, of course, if you like to use the command line, you can do it in the root directory of the project

lupdate -verbose xx.pro

, we are using the IDE method, I think it is more convenient, and then you can see the console output

write picture description here

The prompt is to find five files. Now you can see these two files when you go back to the root directory of our project.

write picture description here

The second step is the specific translation, we open the prophet Linguist

write picture description here

Let's translate English directly

After the translation is complete, click File - Publish, you can see the qm file in the project root directory

write picture description here

Now ready to use in our code

Here I draw a UI

write picture description here

You can see a very simple UI, just switch the language, so how should we write in our code?

QTranslator * translator;

Just declare a QTranslator pointer object in the header file, pay attention to new

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

    translator = new QTranslator(this);
}

MainWindow::~MainWindow()
{
    delete translator;
    delete ui;
}
//切换语言
void MainWindow::on_comboBox_activated(int index)
{
    qDebug() << QString::number(index);
    switch (index) {
    //Chinese
    case 0:
        translator->load("Tranlate_CH.qm");
        qDebug() << "安装中文";
        break;
    //English
    case 1:
        translator->load("Tranlate_EN.qm");
        qDebug() << "安装英文";
        break;
    }
    qApp->installTranslator(this->translator);
    //刷新UI
    this->ui->retranslateUi(this);
}

In this code, we will load the corresponding language qm file with the language you switch to your user, and finally install the language pack through installTranslator and refresh the UI

So we can see the effect

write picture description here

But there are actually a few problems here

First of all, you can't switch languages?

That's because you can't find the qm file, you need to copy it to your build directory

write picture description here

Then there is the language option. He is always in Simplified Chinese, which is caused by retranslateUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
        label_4->setText(QApplication::translate("MainWindow", "\350\257\255\350\250\200\357\274\232", 0));
        comboBox->clear();
        comboBox->insertItems(0, QStringList()
         << QApplication::translate("MainWindow", "\347\256\200\344\275\223\344\270\255\346\226\207", 0)
         << QApplication::translate("MainWindow", "English", 0)
        );
        tv_title->setText(QApplication::translate("MainWindow", "\346\210\221\346\230\257\346\240\207\351\242\230", 0));
        tv_content->setText(QApplication::translate("MainWindow", "\346\210\221\346\230\257\345\206\205\345\256\271", 0));
        tv_version->setText(QApplication::translate("MainWindow", "\346\210\221\346\230\257\347\211\210\346\234\254\345\217\267", 0));
    } // retranslateUi

From the source code we can see

comboBox->clear();
comboBox->insertItems(0, QStringList()

As long as the UI is refreshed, it will reset to 0 subscript, which leads to a bug that is always in simplified Chinese. How can we solve it?

The idea is to monitor the language switch, determine whether it is English to set the subscript, and look at the code

void MainWindow::changeEvent(QEvent* e)
{
    if(e->type() == QEvent::LanguageChange)
    {
        //刷新UI
        this->ui->retranslateUi(this);
        if(!isCH)
        {
            this->ui->comboBox->setCurrentIndex(1);
        }
    }
}

If you switch the language, I will refresh the UI and judge the subscript corresponding to the language type setting, so that it can be perfectly realized.

write picture description here

Those who need source code can join the group: 690351511

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325915677&siteId=291194637