但是最友好的方式,自动检测操作系统的语言的,根据语言自动加载相关界面翻译文件。查阅QT帮助文档,发现QSystemLocale 是有语言和国家检测功能,这里如果要区别简,繁体区别,必须查CountryId. 经过实验,代码成功如下:
#include <QSystemLocale>
#include <QVariant >
#include<QTranslator >
#include "mainwindow.h"
#include "logindialog.h"
void LoadTranlateFile(QApplication * app)
{
//增加多语言支持
QSystemLocale locale;
QVariant val(QLocale::UnitedStates);
QString translatorFileName = "";
QTranslator *translator = new QTranslator(app);
//查询当前国别代码
val = locale.query(QSystemLocale::CountryId,val);
// QDebug()<< "val.int" << val.Int << ",china=" <<QLocale::China;
switch(val.toInt())
{
case QLocale::China:
translatorFileName = "BD4P-zh_CN.qm";
break;
case QLocale::*:
translatorFileName = "BD4P-zh_TW.qm";
break;
case QLocale::HongKong:
translatorFileName = "BD4P-zh_HK.qm";
break;
}
if (translatorFileName!="")
{
#ifdef QT_DEBUG
translatorFileName = "D:\QT_project\BD4P-Manager\"+translatorFileName;
#endif
if (translator->load(translatorFileName))
{
app->installTranslator(translator);
}
}
}