PyQT使用多语言

源代码处理

在代码中需要转换处加入 self.tr()

1
self.ui.txt_CMD.setPlainText(self.tr(u'CommandLog'))

生成 ts 文件

将 py 文件转换为 ts 文件

1
2
3
4
5
6
7
echo off
echo "**\_\_\_**python build translate**\_\_\_**"
cd src\app
"D:\Python2\Lib\site-packages\PyQt4\pylupdate4.exe" windows_main.py -ts ..\Lang\zh_CN.ts
"D:\Python2\Lib\site-packages\PyQt4\pylupdate4.exe" layout\ui_layout_main.py -ts ..\Lang\zh_CN.ts
"D:\Python2\Lib\site-packages\PyQt4\pylupdate4.exe" layout\ui_layout_product.py -ts ..\Lang\zh_CN.ts
"D:\Python2\Lib\site-packages\PyQt4\pylupdate4.exe" layout\ui_layout_comm.py -ts ..\Lang\zh_CN.ts

翻译

使用 linguist.exe 编辑翻译

生成翻译文件

发布为 qm 文件
或者使用指令转换

1
2
3
4
echo off
echo "**\_\_\_**python build translate**\_\_\_**"
cd src\app
"D:\Python2\Lib\site-packages\PyQt4\lrelease.exe" ..\Lang\zh_CN.ts

应用

在主程序中加载

1
2
3
4
5
# 加载翻译
trans = QtCore.QTranslator()
print trans.load(
"F:\\Python\\PythonStudy\\ScopeModbus\\src\\Lang\\zh_CN.qm")
QtApp.installTranslator(trans)
坚持原创技术分享,您的支持将鼓励我继续创作!