Operating system: WINXP
QT library:4.8.5
ide:vs2010.
Basic principle
In order to use multiple languages, you have to do two things:
1, put the string you want to translate into TR (), such as QString str=tr ("Legal");
2, at the beginning, load the prepared translation document (. QM).
Use the tools provided by QT to make the translation files and load them in the program.
1, the production of TS files
QT internationalization requires TS files, and we can create TS files in the following ways
2, Lupdate
3. Run QT linguist, open the *.ts file generated in the previous step, translate it after translation lrelease
4. Add the newly generated QM file to the QRC file:
Double-click the QRC file and configure it with the following configuration, mainly for the load () function
Attach the code before the modification:
#include <Qpushbutton>
#include <QApplication>
#include <Qwidget>
#include <QTextCodec>
#include <QObject>
#include <QTranslator>
int main (int argc, char *argv[])
{
Qapplication app (argc, argv);
Qwidget windows;
Windows.setminimumsize (300,200);
Windows.setmaximumsize (300,200);
Qpushbutton Qpushbutton_test1 (Qobject::tr ("Test1"), &windows);
Qpushbutton_test1.setgeometry (20,20,100,30);
Qpushbutton Qpushbutton_test2 (Qobject::tr ("Test2"), &windows);
Qpushbutton_test2.setgeometry (20,60,100,30);
Qpushbutton Qpushbutton_test3 (Qobject::tr ("Test3"), &windows);
Qpushbutton_test3.setgeometry (20,100,100,30);
Windows.show ();
return App.exec ();
}
Modified code:
#include <Qpushbutton>
#include <QApplication>
#include <Qwidget>
#include <QTextCodec>
#include <QObject>
#include <QTranslator>
int main (int argc, char *argv[])
{
Qapplication app (argc, argv);
/* Create a new Translation object and load the local QM file * *
Qtranslator *translator=new qtranslator (0);
Translator->load (":/test_zh.qm");
App.installtranslator (translator);
Qwidget windows;
Windows.setminimumsize (300,200);
Windows.setmaximumsize (300,200);
Qpushbutton Qpushbutton_test1 (Qobject::tr ("Test1"), &windows);
Qpushbutton_test1.setgeometry (20,20,100,30);
Qpushbutton Qpushbutton_test2 (Qobject::tr ("Test2"), &windows);
Qpushbutton_test2.setgeometry (20,60,100,30);
Qpushbutton Qpushbutton_test3 (Qobject::tr ("Test3"), &windows);
Qpushbutton_test3.setgeometry (20,100,100,30);
Windows.show ();
return App.exec ();
}