PYQT International Multi-lingual support

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.