您的位置:首页 > 编程语言 > Qt开发

QT 国际化和本地化,附上实际案例以及一些使用心得和注意点

2014-07-11 17:32 267 查看
最近在做一个国际化和本地化的工作,对QT的国际化本地化有了一些了解。

转载请注明出处: http://blog.csdn.net/elfprincexu
先简单介绍下主要逻辑

1. 首先要有一个translator

QTranslator* translator = new QTranslator();
qApp->installTranslator(translator)      // install this translator, qApp is a macro from QCoreApplication.


2.把要国际化的地方都放在一个函数里面便于调动(便于我们选择语言切换时候哪些需要翻译的组件):

void Myclass::translatorLanguage()
{
_ui._reasonCombo->setItemText(0, QApplication::translate("Myclass","Periodic-Reboot",0,QApplication::UnicodeUTF8));
_ui._reasonCombo->setItemText(1, QApplication::translate("Myclass","Periodic-Reboot",0,QApplication::UnicodeUTF8));
_ui._reasonCombo->setItemText(2, QApplication::translate("Myclass","Periodic-Reboot",0,QApplication::UnicodeUTF8));
_ui._reasonCombo->setItemText(3, QApplication::translate("Myclass","Periodic-Reboot",0,QApplication::UnicodeUTF8));
/*
other compoments translation
*/
}


3. 链接好信号和翻译slot (这里是一个qcombox的选择引起的动作)

connect (_ui._languageCBox, SIGNAL(currentIndexChanged(int)), this, SLOT(languageCBoxChanged() );


4 语言自动切换函数

void Myclass::languageCBoxChanged()
{
if (_ui._languageCBox->currentIndex() == 0) 	// English
{
translator->load(":/qm/en.qm");// Attention, here, we use resource file , so we use : to indicate its location.
this->translateLanguage();//after loading, translate these places we want to internationalizer
}
else if  (_ui._languageCBox->currentIndex() == 1) 	// chinese
{
translator->load(":/qm/cn.qm");// Attention, here, we use resource file , so we use : to indicate its location.
this->translateLanguage();//after loading, translate these places we want to internationalizer
}
else if  (_ui._languageCBox->currentIndex() == 2) 	// korean
{
translator->load(":/qm/kor.qm");	// Attention, here, we use resource file , so we use : to indicate its location.
this->translateLanguage();//after loading, translate these places we want to internationalizer
}
else if  (_ui._languageCBox->currentIndex() == 3) 	// jpn
{
translator->load(":/qm/jpn.qm");// Attention, here, we use resource file , so we use : to indicate its location.
this->translateLanguage();//after loading, translate these places we want to internationalizer
}
else 					//default
{
translator->load(":/qm/en.qm");// Attention, here, we use resource file , so we use : to indicate its location.
this->translateLanguage();//after loading, translate these places we want to internationalizer
}
}


5. 好了, 现在开始准备 翻译的文件吧,

5.1 使用lupdate 来自动分析所需文件

lupdate     -recursive 	../../GuiElements/*	-ts en.ts   cn.ts kor.ts jpn.ts		// 指定生成那些ts文件,并且指定扫描那些文件

5.2 使用linguist 来编辑翻译文件

linguist cn.ts




5. 3. lrelease 来生成ts的二进制文件 (en.ts 可以不编辑如果源码是用英语写的)

lrelease cn.ts kor.ts en.ts jpn.ts

会生成相应的 *.qm文件

5.4 建立resource文件(translator.qrc)把上述文件装入我们最后的 APP内

打开 我们的工程文件 (.pro工程文件)

加上两行

RESOURCE   = translation.qrc
TRANSLATIONS = cn.ts en.ts jpn.ts kor.ts


同时新建一个qrc (qt 资源性文件) (见下图,注意上面我们load的时候那个路径 :/qm/en.qm 的原因了)



最后编译通过,运行程序就可以了。

注意点:

1. 有时候会出现QT designer编辑的ui 可以转换语言,而程序中动态加入的元件没有成功,试下

QApplication::translate ()

不要使用 tr(),在我尝试过程中,发现了QT designer设计的ui可以转换,但是tr()内的内容却没有转换,原因在于 ui文件QT会通过UIC编译器转换为 ui_uiname.h文件,其中关于组件的国际化已经自动帮我们实现,使用的正是 QApplication::translate()函数进行的。

2. 一旦上述的翻译文件 *.ts文件发生变化,需要clean 后再次complete build 才能把新的东西集成到APP中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: