Description: Qt provides an international solution that enables dynamic language switching without restarting the application during program use.
1. Steps to achieve language internationalization
To achieve language internationalization, you need to perform the following steps:
A. All text information visible to the user is encapsulated using TR ()
Note: The source code with the Utf-8 character set, the source code needs to translate the string must be in English, in order to avoid garbled.
B. Creating a language file
Open a. Pro file, add a language file
Translations = $ $PWD/i18n/lidar_fr.ts \
$ $PWD/i18n/lidar_en.ts \
$ $PWD/i18n/lidar_ja.ts \
$ $PWD/i18n/lidar_zh.ts
Use the Lupdate tool to scan and extract the string that needs to be translated from the source code, generating a ". ts" file, i.e.:
"Update translation (Lupdate)", "external", "QT Language Home", "Tools", menu bar, will generate the language file lidar_fr.ts,lidar_en.ts,lidar_ja.ts,lidar_zh.ts;
Alternatively, open the Qt 5.7 for Desktop (MinGW 5.3.0 32bit) command-line window and go to the ". Pro" project directory to execute the command:
Lupdate Xxx.pro, you can generate a ". ts" Translation file
c. Editing a translation language file
Use the linguist tool (Qt language home) to help with your translation efforts. That is, after you start the linguist tool, open the ". ts" file that you generated earlier with Lupdate, the string of which is translated and saved, mainly by the source text corresponding to the translation.
Another: Because the ". Ts" file is in XML format, you can also use a different editor to open the ". Ts" file and translate it
D. Publishing translation Files
When the translation is complete, use the Lrelease tool to process the translated ". Ts" file, producing a more compact ". QM" file that only Qtranslator can correctly identify, namely:
The ". QM" file can be generated by "Publish translation (Lrelease)", "external", "QT Language Home", "Tools", menu bar.
E. Loading translation files
Qtranslator Translator;
if (Translator.load ("LIDAR_EN.QM", ":/translations")) {//Can not have ". QM" suffix name
qapp->installtranslator (& translator);
}
You can also use aliases:
Qtranslator Translator;
if (Translator.load ("en", ":/translations")) {
qapp->installtranslator (&translator);
}
F. Switching languages
Method 1:
void Mainwindow::refresh () {Ui->listwidget->additem (tr ("item1"));
Ui->listwidget->additem (tr ("item2"));
Ui->listwidget->additem (tr ("Item3")); } void Mainwindow::actionenglishtoggled (bool state) {if (state) {Qapp->removetranslator (&translator
);
if (Translator.load ("LIDAR_EN.QM", ":/translations")) {qapp->installtranslator (&translator);
This->refresh (); }}} void mainwindow::actionfrancaistoggled (bool state) {if (state) {Qapp->removetranslator (&tr
Anslator);
if (Translator.load ("LIDAR_FR.QM", ":/translations")) {qapp->installtranslator (&translator);
This->refresh (); }}} void mainwindow::actionjapanesetoggled (bool state) {if (state) {Qapp->removetranslator (&tr
Anslator); if (Translator.load ("LIDAR_JA.QM", ":/translations")) {Qapp->installtranslator (&translAtor);
This->refresh (); }}} void mainwindow::actionchinesetoggled (bool state) {if (state) {Qapp->removetranslator (&tr
Anslator);
if (Translator.load ("LIDAR_ZH.QM", ":/translations")) {qapp->installtranslator (&translator);
This->refresh ();
}
}
}
Method 2:
When the language is switched, it needs to call Ui->retranslateui (this); Updates the main window. If the non-main window, the Installtranslator function triggers the void ChangeEvent (Qevent *e) event. The reasons are as follows:
After the system calls the Installtranslator function, the system automatically sends the Qevent::languagechange () signal to all qwidget and its subclasses in the program and informs the ChangeEvent slot event to be generated. Therefore, the ChangeEvent function that accepts the qevent::languagechange () signal is rewritten in each form in which the language is to be switched, enabling dynamic language switching.
Note: When loading the main program and the child plug-in program to translate the file alias or path is not the same, or the child plug-in translation may not take effect.
/*********************************************
* ChangeEvent is typically triggered after the current widget state changes
, such as font changes, language changes, and so on.
* This method mainly captures the change events, and when the language changes, the relevant actions are performed.
********************************************/
void plotviewerplugin::changeevent (qevent* e)//overridden event handling method
{
qwidget::changeevent (e); Let the base class execute the event-handling method
switch (E->type ()) {case
Qevent::languagechange: //If it is a language change event if
(UI) ui-> Retranslateui (this); Update the language of the UI break
;
Default: Break
;
}
}
Note: To update the translation, repeat the "C" ~ "D" Step
2. Examples
Langswitch.pro
QT + + core GUI
GreaterThan (qt_major_version, 4): qt + = widgets
TARGET = langswitch
TEMPLATE = App
SOURCES + = main.cpp\
langswitch.cpp
HEADERS + = langswitch.h
translations = lang_en.ts \
lang_ Zh.ts \
lang_la.ts
Main.cpp
#include "langswitch.h"
#include <QApplication>
int main (int argc, char *argv[])
{
Qapplication A (argc, argv);
Langswitch W;
W.show ();
return a.exec ();
}
Langswitch.h
#ifndef langswitch_h
#define LANGSWITCH_H
#include <QWidget>
#include <QComboBox>
# Include <QLabel>
class Langswitch:public qwidget
{
q_object public
:
langswitch (Qwidget * parent = 0);
~langswitch ();
Private slots:
void Changelang (int index);
Private:
void Createscreen ();
void Changetr (const qstring& langcode);
void Refreshlabel ();
qcombobox* Combo;
qlabel* label;
};
#endif//Langswitch_h
Langswitch.cpp
#include "langswitch.h" #include <QVBoxLayout> #include <QTranslator> #include <QApplication> #
Include <QDebug> Langswitch::langswitch (Qwidget *parent): Qwidget (parent) {Createscreen ();}
Langswitch::~langswitch () {} void Langswitch::createscreen () {combo = new Qcombobox;
Combo->additem ("中文版", "en");
Combo->additem ("Chinese", "zh");
Combo->additem ("Latin", "la");
label = new Qlabel;
Refreshlabel ();
qvboxlayout* layout = new Qvboxlayout;
Layout->addwidget (combo, 1);
Layout->addwidget (label, 5);
SetLayout (layout);
Connect (combo, SIGNAL (currentindexchanged (int)), This,slot (Changelang (int)));
Changelang (0);
} void Langswitch::refreshlabel () {Label->settext (tr ("Txt_hello_world", "HELLO World");}
void Langswitch::changelang (int index) {QString Langcode = combo->itemdata (index). toString ();
CHANGETR (Langcode);
Refreshlabel (); } void Langswitch::changetr(const qstring& Langcode)
{Static qtranslator* translator;
if (translator! = NULL) {qapp->removetranslator (translator);
Delete translator;
translator = NULL;
} translator = new Qtranslator;
QString qmfilename = "Lang_" + langcode;
Qdebug () << qmfilename;
if (Translator->load (QString ("e:/.../langswitch/") +qmfilename)) {qapp->installtranslator (translator);
}
}