Qt国际化1

来源:互联网 发布:淘宝客视频教程 编辑:程序博客网 时间:2024/05/17 21:52

在代码中用可以用英文命名各个元件,但是main.cpp中加入以下语句:
QTranslator translator;
translator.load("hellotr_la");
app.installTranslator(&translator);
然后运行qmake -project
在生成的.pro文件中加一下语句:
TRANSLATIONS=hellotr_la.ts
运行 qmake
运行 lupdate -verbose hellotr_la.pro 生成.ts文件 (.ts format is human-readable XML that can be emailed directly and is easy to put under version control)
运行 linguist hellotr_la.ts 打开QT linguist Trolltech 选中你要改名的元件,在Translation下写要改成的中文名.
然后File->Release
File->Save
最后就可以make你的程序并运行了.
其实在qt4的assistant-Tutorial and Example-QT Linguist Examples中有详细介绍.本文参考它得来的.
QT中的例子:

QApplication app(argc, argv);

     QString locale = QLocale::system().name();
     QTranslator translator;
     translator.load(QString("arrowpad_") + locale);
     app.installTranslator(&translator);
面说一下qt国际化编程的操作步骤:

1、编写源代码

2、在*.pro文件中添加TRANSLATIONS += *.ts ,有多少中语言就添加多少个ts文件。

3、运行lupdate *.pro 生成ts文件。lupdate会根据源代码中的内容提取出待翻译的字段,然后生成ts文件,ts文件是xml格式的。

4、用qt linguist打开ts文件,并翻译相应字段

5、运行lrelease *.pro生成qm文件。lrelease会根据ts文件生成二进制的qm翻译文件。

6、在*.qrc文件中添加qm文件的路径,并编译源代码