python-如何实现对pyqt4的多语言支持
作者:互联网
我有一个pyqt4程序,希望实现多语言支持.我拥有所有.qm文件,但无法弄清楚如何使用它们.
我真的找不到太多的文档,而且我尝试的任何方法似乎都无法正常工作.
解决方法:
关于此主题的文档很多,可以在明显的地方找到这些文档:
> Internationalization with
Qt
> Qt Linguist
Manual
> Internationalisation of PyQt4
Applications
下面是一个简单的演示脚本(使用-h进行使用):
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
message = self.tr('Hello World')
label = QtGui.QLabel('<center><b>%s</b><center>' % message, self)
buttonbox = QtGui.QDialogButtonBox(self)
buttonbox.addButton(QtGui.QDialogButtonBox.Yes)
buttonbox.addButton(QtGui.QDialogButtonBox.No)
buttonbox.addButton(QtGui.QDialogButtonBox.Cancel)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(label)
layout.addWidget(buttonbox)
if __name__ == '__main__':
import sys, os, getopt
options, args = getopt.getopt(sys.argv[1:], 'hl:')
options = dict(options)
if '-h' in options:
print("""
Usage: %s [opts] [path/to/other.qm]
Options:
-h display this help and exit
-l [LOC] specify locale (e.g. fr, de, es, etc)
""" % os.path.basename(__file__))
sys.exit(2)
app = QtGui.QApplication(sys.argv)
translator = QtCore.QTranslator(app)
if '-l' in options:
locale = options['-l']
else:
locale = QtCore.QLocale.system().name()
# translator for built-in qt strings
translator.load('qt_%s' % locale,
QtCore.QLibraryInfo.location(
QtCore.QLibraryInfo.TranslationsPath))
app.installTranslator(translator)
if args:
# translator for app-specific strings
translator = QtCore.QTranslator(app)
translator.load(args[0])
app.installTranslator(translator)
window = Window()
window.setGeometry(500, 300, 200, 60)
window.show()
sys.exit(app.exec_())
标签:python,qt,python-2-x,pyqt4,multilingual 来源: https://codeday.me/bug/20191012/1900802.html