Qt语言动态切换
2014-10-09 11:15
337 查看
1、创建语言文件
打开工程.pro文件加入一下内容
TRANSLATIONS
=video_cn.ts
\
分别表示将生成汉语、英语、俄语、日语、阿拉伯语、西班牙语的*.ts可读的翻译文件
然后点QtCreator软件中工具->外部->Qt语言家->更新翻译,将生成翻译文件
video_cn.ts、video_en.ts....
2、编辑翻译文件
启动qt语言家Linguist打开xx.ts文件,会自动加载qt中用到的字符串。
配置源文和译文的设置
重复2步骤翻译其他语言
3、部署翻译文件
在qtcreator中工具->外部->Qt语言家->部署翻译,将在工程目录下生成各种语言文件xx.qm
以上步骤可随时进行。
4.在程序中动态切换语言
void
retranslateUi(QWidget
*test)
5、效果
2014.10.09cmj
打开工程.pro文件加入一下内容
TRANSLATIONS
=video_cn.ts
\
video_en.ts\
video_rn.ts\
video_jn.ts\
video_an.ts\
video_xn.ts
分别表示将生成汉语、英语、俄语、日语、阿拉伯语、西班牙语的*.ts可读的翻译文件
然后点QtCreator软件中工具->外部->Qt语言家->更新翻译,将生成翻译文件
video_cn.ts、video_en.ts....
2、编辑翻译文件
启动qt语言家Linguist打开xx.ts文件,会自动加载qt中用到的字符串。
配置源文和译文的设置
重复2步骤翻译其他语言
3、部署翻译文件
在qtcreator中工具->外部->Qt语言家->部署翻译,将在工程目录下生成各种语言文件xx.qm
以上步骤可随时进行。
4.在程序中动态切换语言
QTranslatortranslator;
translator.load("E:/Qt/test/video_cn.qm");//加载不同的语言文件显示不同的语言
qApp->installTranslator(&translator);
this->ui->retranslateUi(this);//更新界面语言
void
retranslateUi(QWidget
*test)
{
test->setWindowTitle(QApplication::translate("test","test",0,QApplication::UnicodeUTF8));
label->setText(QApplication::translate("test","hello",0,QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("test","\346\261\211\350\257\255",0,QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("test","\350\213\261\350\257\255",0,QApplication::UnicodeUTF8));
pushButton_3->setText(QApplication::translate("test","\344\277\204\350\257\255",0,QApplication::UnicodeUTF8));
pushButton_4->setText(QApplication::translate("test","\346\227\245\350\257\255",0,QApplication::UnicodeUTF8));
pushButton_5->setText(QApplication::translate("test","\351\230\277\346\213\211\344\274\257\350\257\255",0,QApplication::UnicodeUTF8));
pushButton_6->setText(QApplication::translate("test","\350\245\277\347\217\255\347\211\231\350\257\255",0,QApplication::UnicodeUTF8));
}
retranslateUi这里translate静态函数中就是根据安装的翻译文件翻译其中的文本,最后返回翻译结果,所以起到了翻译刷新的作用。
5、效果
2014.10.09cmj
相关文章推荐