新建Qt项目,使用tr对所有要展示的QString做包装
代码如下:
pro文件:
BIN_RELEASE = $$PWD/bin/ReleaseBIN_DEBUG = $$PWD/bin/DebugQT += core gui widgetsCONFIG += ordered debug_and_releaseCONFIG(debug, debug|release) {TARGET = pcsdTEMPLATE = appDESTDIR = $$BIN_DEBUG} else {TARGET = pcsTEMPLATE = appDESTDIR = $$BIN_RELEASE}TRANSLATIONS += language_zh.ts \language_en.tsSOURCES += main.cpp\QtGlobalizationDemo.cppHEADERS += \QtGlobalizationDemo.hFORMS += \QtGlobalizationDemo.ui
头文件
///////////////////////////////头文件////////////////////////////////////////////#ifndef QTGLOBALIZATIONDEMO_H#define QTGLOBALIZATIONDEMO_H#include <QMainWindow>namespace Ui {class QtGlobalizationDemo;}class QTranslator;class QtGlobalizationDemo : public QMainWindow{Q_OBJECTpublic:enum LanguageIds{UI_UNINIT = -1,UI_ZN = 0,UI_EN = 1,};public:explicit QtGlobalizationDemo(QWidget *parent = 0);~QtGlobalizationDemo();void initTranslator(QTranslator *trans);protected slots:void onSwitchLanguage(int ids);private:void updateText();bool loadLanguage(const QString &languageFile);private:Ui::QtGlobalizationDemo *ui;int m_languageIds;QTranslator* m_translator;};#endif // QTGLOBALIZATIONDEMO_H
源文件:
///////////////////////////////源文件////////////////////////////////////////////#include "QtGlobalizationDemo.h"#include "ui_QtGlobalizationDemo.h"#include <QString>#include <QTranslator>#include <QDir>#include <QDebug>QtGlobalizationDemo::QtGlobalizationDemo(QWidget *parent): QMainWindow(parent), ui(new Ui::QtGlobalizationDemo), m_languageIds(UI_UNINIT), m_translator(NULL){ui->setupUi(this);updateText();connect(ui->switchLang, SIGNAL(currentIndexChanged(int)), this, SLOT(onSwitchLanguage(int)));}QtGlobalizationDemo::~QtGlobalizationDemo(){delete ui;}void QtGlobalizationDemo::initTranslator(QTranslator *trans){m_translator = trans;}void QtGlobalizationDemo::onSwitchLanguage(int ids){QString language;switch (ids) {case UI_ZN:language = QString("resource/language_zh");break;case UI_EN:language = QString("resource/language_en");break;default:language = QString("resource/language_zh");break;}if (m_languageIds != ids) {loadLanguage(language);m_languageIds = ids;}}void QtGlobalizationDemo::updateText(){ui->curLag->setText(tr("Language"));ui->content->setWordWrap(true);ui->content->setText(tr("Young idler, an old beggar."));setWindowTitle(tr("Qt international programming code test"));if (ui->switchLang->count() <= 0) {ui->switchLang->insertItem(UI_ZN, tr("Chinese"));ui->switchLang->insertItem(UI_EN, tr("English"));} else {ui->switchLang->setItemText(UI_ZN, tr("Chinese"));ui->switchLang->setItemText(UI_EN, tr("English"));}}bool QtGlobalizationDemo::loadLanguage(const QString &languageFile){if (languageFile.isNull()) {return false;}if (m_translator == NULL) {return false;}qApp->removeTranslator(m_translator);if (m_translator->load(languageFile)) {qApp->installTranslator(m_translator);updateText();return true;} else {qDebug() << "------->> load language file error." << languageFile;}return false;}
制作ts文件
首先,必须再pro文件中加入如下代码:
TRANSLATIONS += language_zh.ts \language_en.ts
然后生成ts文件
操作步骤:工具->外部->qt语言家->更新翻译
生成qm文件
操作:使用Qt linguist.exe工具打开刚才生成的ts文件,编辑翻译:
编辑完成后,保存,发布:
这时候,我们就能再ts文件所在目录下发现生成了的qm文件
我们代码中load这个文件,就可以实现国际化的切换
总结:
首先,新建项目,在pro文件中编辑生成ts文件的路径
在代码中要显示的字符串都用tr()包含
使用Qt工具里面的更新翻译,生成ts文件
使用Qt工具linguist.exe 打开ts文件,进行翻译,最后发布生成qm文件
把这个qm文件放到程序exe路径下
代码中使用QTranslator加载这个qm文件,使用qApp->installTranslator(QTranslator * messageFile)方法安装翻译文件
