在代碼中用可以用英文命名各個元件,但是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文件的路徑,並編譯源代碼