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:
It enables the QT lupate tool to extract all the printable strings.
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:
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