Internationalization refers to the design of the code to be easily ported to other countries and regions of the features, to add internationalization support for PYQT five steps
I. Preparation of gui.py
In the text to be translated, the TR method is used to enclose it.
#-*-coding:utf-8-*- fromPyQt4ImportQtcore, QtguiclassMwindow (qtgui.qdialog):def __init__(self): Super (Mwindow, self).__init__() self.resize (100, 60) Self.down1= Qtgui.qpushbutton (self.tr (U'Download'), self) self.down1.setGeometry (Qtcore.qrect (10, 5, 90, 23)) Self.down2= Qtgui.qpushbutton (self.tr (U'Upload'), self) self.down2.setGeometry (Qtcore.qrect (10, 30, 90, 23))if __name__=='__main__': Importsys app=qtgui.qapplication (SYS.ARGV) Mainwin=Mwindow () mainwin.show () Sys.exit (App.exec_ ())
Effect:
Two. Create a language file
CMD use command:
Pylupdate4 Ui_main.py-ts Zh_cn.ts
A zh_cn.ts language file is generated
In fact, the. ts file is an XML file, you can also open XML directly to repair
Three. Translating language files
Open zh_cn.ts with Qt's language home
QT Language Home Linguist.exe directory: python/lib/site-packages/pyqt4/
Four. Convert to QM File
Conversion command:
Lrelease Zh_cn.ts
QT Lrelease.exe Directory: python/lib/site-packages/pyqt4/
You can also use the QT Language Home menu--post
Five. Introducing QM files into the code
Reference:
from Import Qtranslator
trans = Qtranslator () trans.load ("zh_cn") # No suffix. QM app.installtranslator (trans)
Full code:
1 #-*-coding:utf-8-*-2 fromPyQt4ImportQtcore, Qtgui3 fromPyqt4.qtcoreImportQtranslator4 classMwindow (qtgui.qdialog):5 def __init__(self):6Super (Mwindow, self).__init__()7Self.resize (100, 60)8Self.down1 = Qtgui.qpushbutton (self.tr (U'Download'), self)9Self.down1.setGeometry (Qtcore.qrect (10, 5, 90, 23))Ten OneSELF.DOWN2 = Qtgui.qpushbutton (self.tr (U'Upload'), self) ASelf.down2.setGeometry (Qtcore.qrect (10, 30, 90, 23)) - - the if __name__=='__main__': - ImportSYS -App =qtgui.qapplication (SYS.ARGV) -trans =Qtranslator () +Trans.load ("ZH_CN")#no suffix. QM - App.installtranslator (trans) +Mainwin =Mwindow () A mainwin.show () atSys.exit (App.exec_ ())
Effect:
PYQT International Multi-lingual support