注:目标板跟这类似
主机环境:Debian
编译器:gcc version 4.1.3 20080704 (prerelease) (Debian 4.1.2-25)
1. 将你要设置的字体库文件放到Qt安装目录下的fonds目录下
比如我的是:
/usr/local/Trolltech/QtEmbedded-4.5.1-x86/lib/fonts/。我用到的字体是文泉驿和simsun.ttf
2.用tr()函数提取要翻译的字符串
确保你要翻译的字符串都被放到tr()函数中。比如:
4 ControllerDialog::ControllerDialog(QWidget *parent)
5 :QDialog(parent)
6 {
7 tabWidget = new QTabWidget;
10 tabWidget->addTab(new MicroOvenTab,tr("MicroWave"));//提取
11 tabWidget->addTab(new DianGuoTab,tr("DianGuo"));//提取
17 QVBoxLayout *mainLayout = new QVBoxLayout;
18 mainLayout->addWidget(tabWidget);
19 mainLayout->setContentsMargins(20,20,5,5);//设置周围的margin,以致全屏
20 setLayout(mainLayout);
21
22 setWindowTitle(tr("Controller Dialog"));//提取
23 }
3.修改生成的工程文件
修改生成的工程文件,以便说明你要支持哪种语言。例如,如果想除了支持英语之外还支持德语、法语、汉语,则要在smarthome.pro文件中添加如下TRANSLATIONS选项(我的工程文件是smarthome.pro,这是你要改的)我的如下:
#############################################################
# Automatically generated by qmake (2.01a) ?? 6? 30 16:16:07 2010
##############################################################
4
5 TEMPLATE = app
6 TARGET =
7 DEPENDPATH += .
8 INCLUDEPATH += /home/zyx/project/gui/x86/smarthome/. .
10 TRANSLATIONS = smarthome_de.ts/ //要添加的
smarthome_fr.ts/
smarthome_zh.ts
12 # Input
13 HEADERS += AeroButton.h connection.h smarthome.h
14 SOURCES += AeroButton.cpp main.cpp smarthome.cpp
15 QT +=sql
这里给定了三个翻译文件:一个用于德语,一个用于法语,最后一个用于汉语。
4. 用lupdate命令操作.pro文件,将要翻译的提取到.ts文件。(.ts文件是XML格式文件)命令是:
#lupdate -verbose smarthome.pro
这里的-verbose选项告诉lupdate提供更多的反馈信息。以下给出的是预期的输出结果:
updating ‘smarthome_de.ts’...
Found 98 source texts (98 new and 0 already existing)
updating ‘smarthome_fr.ts’...
Found 98 source texts (98 new and 0 already existing)
updating ‘smarthome_zh.ts’...
Found 98 source texts (98 new and 0 already existing)
在应用程序源代码的每个tr()函数调用中所出现的每一个字符串,都会存储到这些.ts文件中,后面会跟一个空白翻译。
默认情况下,lupdate工具会假设tr()中的参数都是Latin-1字符串。如果不是这种情况,那么就必须在.pro文件中添加一个CODECFORTR项。例如:
CODECFORTR=EUC-JP
除了在应用程序的main()函数中调用QTextCodec::setCodecForTr()之外,还必须完成这一点。
5. 翻译
用Qt Linguist把这些翻译添加到smarthome_de.ts、smarthome_fr.ts、smarthome_zh.ts文件中。Debian终端下输入:/usr/local/Trolltech/Qt-4.5.1/bin/linguist & 后台启动该程序。
“1”部包含3项"对号"、"上下文"和"项"。"对号"表示翻译完成;"上下文"表示lingust根据文件将要翻译的程序分成了三个程序段;"项"表示每个程序段中需要翻译的个数和成功翻译的个数。
“2”部"源文"表示每个程序段中要翻译的文字,比如:本图中的“MicroWave”要翻译成汉语的“微波炉”,“dianguo”翻译成“电饭锅”,“Controller Dialog”翻译成“控制中心”。
“3”部里面显示的是程序源码。
“4”部:每点击一次“2”部中要翻译的词条,“4”部中“源文”中显示对应的词条,“汉语译文”中是要添加的汉语翻译。本图中添加的是“微波炉”。
(另一种方法:直接修改.ts文件,<source>MicroWave</source>是源文,<translation>微波炉</translation>中间部分是要添加的。比如我的smarthome_zh.ts文件如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE TS>
3 <TS version="2.0" language="zh_CN">
4 <context>
5 <name></name>
6 <message>
7 <location filename="connection.h" line="14"/>
8 <source>Cannot open database</source>
9 <translation>数据库打开出现问题</translation>
10 </message>
11 <message>
12 <location filename="connection.h" line="15"/>
13 <source>Unable to establish a database connection.
14 This example needs SQlite support.Please readthe Qt SQL driver documentation for information howto build it.
15
16 Click Cancel to exit.</source>
17 <translation>不能建立数据库连接</translation>
18 </message>
19 </context>
20 <context>
21 <name>ControllerDialog</name>
22 <message>
23 <location filename="smarthome.cpp" line="10"/>
24 <source>MicroWave</source>
25 <translation>微波炉</translation>
26 </message>
27 <message>
28 <location filename="smarthome.cpp" line="11"/>
29 <source>DianGuo</source>
30 <translation>电锅</translation>
31 </message>
32 <message>
33 <location filename="smarthome.cpp" line="22"/>
34 <source>Controller Dialog</source>
35 <translation>控制中心</translation>
36 </message>
37 </context> //省啰了一部分
66 <context>
67 <name>MicroOvenTab</name>
68 <message>
69 <location filename="smarthome.cpp" line="70"/>
70 <source>10m</source>
71 <translation>10分</translation>
72 </message>
73 <message>
74 <location filename="smarthome.cpp" line="80"/>
75 <source>1m</source>
76 <translation>1分</translation>
77 </message>
78 <message>
79 <location filename="smarthome.cpp" line="88"/>
80 <source>10s</source>
81 <translation>10秒</translation>
82 </message>
83 <message>
84 <location filename="smarthome.cpp" line="96"/>
85 <source>1s</source>
86 <translation>1秒</translation>
87 </message>
88 <message>
89 <location filename="smarthome.cpp" line="110"/>
90 <source>Start</source>
91 <translation>启动</translation>
92 </message>
93 <message>
94 <location filename="smarthome.cpp" line="120"/>
95 <source>Clear</source>
96 <translation>取消</translation>
97 </message>
98 <message>
99 <location filename="smarthome.cpp" line="128"/>
100 <source>MicroWave</source>
101 <translation>微波</translation>
102 </message>
103 <message>
104 <location filename="smarthome.cpp" line="135"/>
105 <source>Memory</source>
106 <translation>记忆</translation>
107 </message>
108 <message>
109 <location filename="smarthome.cpp" line="149"/>
110 <source>PowerLevel</source>
111 <translation>火力</translation>
112 </message>
113 <message>
114 <location filename="smarthome.cpp" line="158"/>
115 <source>PreSet</source>
116 <translation>预置</translation>
117 </message>
118 <message>
119 <location filename="smarthome.cpp" line="167"/>
120 <source>JetDefRost</source>
121 <translation>快速解冻</translation>
122 </message>
123 <message>
124 <location filename="smarthome.cpp" line="175"/>
125 <source>ByeWeight</source>
126 <translation>按重解冻</translation>
127 </message>
128 <message>
129 <location filename="smarthome.cpp" line="189"/>
130 <source>Grill</source>
131 <translation>快速烧烤</translation>
132 </message>
133 <message>
134 <location filename="smarthome.cpp" line="197"/>
135 <source>Express</source>
136 <translation>快速烹调</translation>
137 </message>
138 <message>
139 <location filename="smarthome.cpp" line="205"/>
140 <source>Combination
141 Barbecue1</source>
142 <translation>组合烧烤1</translation>
143 </message>
144 <message>
145 <location filename="smarthome.cpp" line="215"/>
146 <source>Combination
147 Barbecue2</source>
148 <translation>组合烧烤2</translation>
149 </message>
150 </context>
151 </TS>
)
6.生成可用的.qm文件
一旦有了一个翻译过的.ts文件,就需要把它转换成一个可以由QTranslator使用的二进制.qm文件。为了在Qt Linguist中完成这一点,可以单击“文件”->“发布”。
另一种常用的方法是用命令生成。命令是:
lrelease -verbose smarthome.pro
假设我们把26个字符串翻译成了汉语,并且为他们中的19个单击了“对号”标记。那么lrelease将会产生下面的输出:
Updating 'smarthome_zh.qm'...
26 finished, 0 unfinished and 0 untranslated messages
当程序运行的时候,还没有被翻译的字符串将会按最初的语言显示出来。
7.使用.qm文件
在你的应用程序中使用刚才得到的.qm文件。在头文件中要包含#include <QTranslator>
7 int main(int argc,char *argv[])
8 {
9 QApplication app(argc,argv);
10 app.setOverrideCursor(QCursor(Qt::BlankCursor));//去掉鼠标
11
12 QTranslator translator;//生成翻译器
13 translator.load("smarthome_zh"); //载入qm文件,与刚才得到的qm的文件名对应
14 app.installTranslator(&translator);//安装翻译器
17
18 app.setFont(QFont("simsun",15));//设置字体为simsun
19
20 ControllerDialog *controllerdialog=new ControllerDialog;
21 //controllerdialog->resize(600,440);
22 //controllerdialog->show();
23 controllerdialog->showFullScreen();//将当前窗口显示出来
24 return app.exec();
程序在运行时是动态提取和源语言对应的本地语言信息并显示在用户界面上。设计中一定要在界面出来之前安装翻译器,否则就不能在界面中显示翻译后的界面。举个例子 ,下面的顺序是对的
QTranslator translator(0);
translator.load("french.qm", ".");
app.installTranslator(&translator);
MyWidget m;
app.setMainWidget(&m);
m.show();
下面的顺序就不对,它错在界面出来之后才才安装翻译器
MyWidget m;
app.setMainWidget(&m);
m.show();
QTranslator translator(0);
translator.("myexec_zh.qm", ".");//要跟刚才得到的qm的文件名对应
app.installTranslator(&translator);
注:当第一次运行lupdate时,将会创建这三个文件,并且以后每次执行lupdate的时候,都会更新这三个文件。
当修改应用程序的源代码时,翻译文件就可能过时了。解决这个问题的方法是再次运行lupdate,这样就可以为新的字符串重新提供翻译,然后再重新生成这些.qm文件。
参考的资料:
(1)在design.pro文件里面加入TRANSLATIONS = design.ts。
(2) 用lupdate 操作pro,以提取出.ts文件供下面的lingusit工具翻译成汉化所需要的.qm文件。命令是#lupdate design.pro。*.ts文件是翻译源文件,它是基于XML语言描述的。通过编辑*.ts文件,已经指定了对应的翻译信息。这一步是翻译的过程,所 谓翻译就是把tr("english")中的english 提取出来,以翻译成相应的语言,供程序加载使用。
(3)用linguist打开刚才 的design.ts文件,linguist是在qt的bin的目录下的一个界面工具。如果使用QT/E 2.3.7的版本,没有自带这一个工具,可以在windows下单独下载linguist工具进行这一步。在linguist中用菜单栏file ->open 打开相应的.ts文件,如该设计中的design.ts。打开后会看到左边是相应的类,右边的上半部是相应的类里面提取出来供翻译的内容,下半部是要翻译 的语言的相应的东西,即为需要输入中文的地方。在翻译中,要注意标点符号的翻译最好还是用英文输入状态下的标点符号。当翻译状态前出现绿色勾的提示为翻译 成功,叹号的表示没有翻译对,交叉的表示没有翻译。
(4)先用linguist 菜单保存翻译好的.ts文件,接着用linguist界面工具里面菜单file里面的release...,点击这个弹出对话框,提示输入.qm文件的文 件名,用默认的即可。按确定后你会在.ts文件所在的目录看到一个.qm后缀名的文件,这就是程序翻译器要用到的文件。
(5)最后,把design.qm文件copy到design目录下, 在main.cpp程序里面使用刚才得到的.qm文件 ,方法如下:
view plaincopy to clipboardprint?
- QTranslator translator(0); //生成翻译器
- translator.load("design.qm", "."); //载入qm文件,与刚才得到的qm的文件名对应
- app.installTranslator(&translator); //安装翻译器
- MyWidget m;
- m.setFont(QFont("unifont", 16)); //设置中文字体
- app.setMainWidget(&m); // 将m设为当前窗口
- m.show(); //将当前窗口显示出来
程序在运行时是动态提取和源语言对应的本地语言信息并显示在用户界面上。设计中一定要在界面出来之前安装翻译器,否则就不能在界面中显示翻译后的界面。