Qt 之 中英文切换的使用
//写的不对的地方请指点
1 在代码中有tr("work") 有需要中英文切换的字符串
ui.cbxPapersType->addItem(tr("IdCard")); //0身份证
ui.cbxPapersType->addItem(tr("Student"));//("学生证"));
ui.cbxPapersType->addItem(tr("WorkPermit"));//("工作证"));
ui.cbxPapersType->addItem(tr("Soldier"));//("士兵证"));
ui.cbxPapersType->addItem(tr("GasCardin"));//("军官证"));
ui.cbxPapersType->addItem(tr("Passport"));//("护照"));
ui.cbxPapersType->addItem(tr("HouseholdRegister"));//("户口本"));
ui.cbxPapersType->addItem(tr("deformed")); //2残疾证
ui.cbxPapersType->addItem(tr("other")); //3其他
2 在cmd命令行执行 生成ts文件
lupdate.exe -ts XXX.ts -pro XXXX.pro
3 下载pyhon,编写脚本 将XX.db 中的中英文翻译数据与XXX.ts文件中用到需要翻译的都翻译一下。生成XXXS.ts文件
import re
#import MySQLdb
import sqlite3
def getLan(lanName,mLanFile):
contents_end = ""
mWords = ""
mRes = ""
try:
#traslate language from english to other language
f = open('S:\py\XXX.ts','r')
regex_msg=re.compile("<message>")
regex_source=re.compile("<source>(.*)</source>")
regex_translate=re.compile("<translation type=\"unfinished\">.*</translation>")
regex_endmsg=re.compile("</message>")
#conn = MySQLdb.connect(host='localhost', user='root', passwd='',db='convert')
conn = sqlite3.connect("S:\py\XXX.db")
for line in f:
mMsg=regex_msg.search(line)
mSource=regex_source.search(line)
mTranslate=regex_translate.search(line)
mEndMsg=regex_msg.search(line)
if mMsg != None:
bMsg = 1
elif mSource != None:
bSource = 1
mWords = mSource.group(1)
#cursor = conn.cursor()
mSql = 'SELECT Chinese FROM translate WHERE English = "' + mWords + '" limit 1'
#mSql = 'SELECT cn FROM lang WHERE en = "' + mWords + '" limit 1'
#print mSql
count = conn.execute(mSql)
r = count.fetchone()
#print count
if r != None:
#mRes = cursor.fetchone()
mRes = r[0]
else :
mRes = ""
#print mRes
elif mTranslate != None:
if (bMsg == 1) and (bSource == 1):
line = "\t<translation>" + mRes + "</translation>\n"
#print line
bSource = 0
elif mEndMsg:
bMsg = 0
#print line
contents_end += line
#print contents_end
conn.close()
f.close()
f.tell()
except ValueError as ioerror:
print('file already closed')
finally:
print('Translate languages end')
mLanFile = 'S:/py/XXS.ts'
#print contents_end
f = open(mLanFile,'w')
f.write(contents_end)
f.close()
4 lrelease 命令生成XXQM.qm文件,需要将XXX.ts文件的编码格式改成unicode
lrelease.exe XXXS.ts
4 修改程序,在转换前(如main.cpp里)增加代码
5 编译程序,运行程序。
需要将qm文件放置在运行目录下