以英文翻译成中文为例:
1)在程序里需要翻译的地方用tr()包装起来,如 QString str = tr("baidu");
2)在.pro文件里加入一行 TRANSLATIONS=Language_Chn.ts
3)用控制台执行命令:lupdate yourapp.pro 或调用ide编译.这样程序会将程序中使用tr的部分提取到ts文件中。
4)打开 linguist 工具,编辑上面的ts文件,将每个源字符进行翻译,然后release一个Language_Chn.qm文件。
5)在程序里调用下面代码即可完成界面语言的翻译:
QTranslator * translator = new QTranslator();
translator->load("Language_Chn");
QApplication::instance()->installTranslator(translator);
QTranslator load函数的原型为
bool QTranslator::load ( const QString & filename, const QString
& directory = QString(), const QString & search_delimiters =
QString(), const QString & suffix = QString() );
For example, an application running in the fr_CA locale (French-speaking
Canada) might call load("foo.fr_ca", "/opt/foolib"). load() would then
try to open the first existing readable file from this list:
/opt/foolib/foo.fr_ca.qm
/opt/foolib/foo.fr_ca
/opt/foolib/foo.fr.qm
/opt/foolib/foo.fr
/opt/foolib/foo.qm
/opt/foolib/foo
阅读(1659) | 评论(0) | 转发(2) |