TR () and Internationalization

Source: Internet
Author: User

If our program needs to be translated into another language, the QT linguist and QT translation tools have provided us with a set of solutions to solve this problem, they provide methods for organizing and processing translated strings.

To prepare code translation, you must use the qobject: TR () function to include the string to be translated. When you apply the string to a non-static function, the Class Name of the object provided by qmetaobject is translated as the "context" of the string grouping to be translated"

The TR () function has two functions:

  1. It enables the QT lupate tool to extract all the printable strings.

  2. If the translation file is available and the language has been selected, the function returns the translated string.

If no translation file is available, the tr () function returns the original string.

Note: Make sure that each printable string is completely inside the tr () function and is extracted at the time of compilation. This is very important. For strings with parameters, you can use qstring:: Arg () function to place parameters in translated strings:

 label->setText(tr("%1 + %2 = %3")) .arg(para1) .arg(para2) .arg(para3)

Procedure:

  1. Add the following in the Pro file:

TRANSLATIONS = $$PWD/lang/chinese.ts \                $$PWD/lang/english.ts

2. method 1> use the lupdate pro file name on the qt terminal to generate the translation Files "Chinese. Ts" and "English. Ts ".

Method 2: Tools-> external-> QT language expert-> Update the translation. The translation files chinies. Ts and English. Ts are generated.

3. Use QT linguist to edit *. Ts files for translation

4. method 1> use the lrelease command on the qt Terminal

Method 2> Find "publish" in QT linguist and generate *. QM files based on *. Ts. They can have applications to load their translations.

Method 3> Tools> external> QT language Home> deployment and translation, the language file QM is generated.

5. Load the translation File

QTranslator translator;translator.load("cn.qm");qApp->installTranslator(&translator);


There are two scenarios for implementing multi-language support using QT:

First, you can use the UI editor to automatically generate an interface. It is easy to implement dynamic switching between multiple languages.

void MainWindow::changeLanguage(const ELangType& langType){    switch(langType)    {        case ELangType_Chinese:        {            this->languageState = ELangType_Chinese;            this->translator.load("chinese") ;            qApp->installTranslator(&(this->translator));            break;        }        case ELangType_English:        {            this->languageState = ELangType_English;            this->translator.load("english") ;            qApp->installTranslator(&(this->translator));            break;        }        default:        {            break;        }    }}

This function enables Dynamic Language switching. languagetype is used to record the status of the current language, and qapp is a qapplication object. We only need to load different files using qtranslator to implement dynamic multi-language switching.

Type 2: Do not use the UI editor. You can design the application interface by yourself. This method is cumbersome to implement dynamic switching between multiple languages;

void MainWindow::changeLanguage(const ELangType& langType){    switch(langType)    {        case ELangType_Chinese:        {            this->languageState = ELangType_Chinese;            this->translator.load("chinese") ;            this->pOwner->installTranslator(&(this->translator));            break;        }        case ELangType_English:        {            this->languageState = ELangType_English;            this->translator.load("english") ;            this->pOwner->installTranslator(&(this->translator));            break;        }        default:        {            break;        }    }        this->labelname->setText(tr("name"));    this->labeltel->setText(tr("tel"));    ........}

Therefore, this is especially troublesome. Every interface must perform such operations. Generally, I will use a retranslateui () function on each interface

Put the control to be translated in it. You can call it every time you translate it.

Note: TR () is not set again in the first method. The reason is as follows:

Void mainwindow: changeevent (qevent * E) {qmainwindow: changeevent (E); Switch (e-> type () {Case qevent: languagechange: UI-> retranslateui (this); // update the language translator. .......}

TR () and Internationalization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.