gpt4 book ai didi

python - pyqt4如何实现多语言支持

转载 作者:行者123 更新时间:2023-11-30 23:30:14 35 4
gpt4 key购买 nike

我有一个 pyqt4 程序,想要实现多语言支持。我拥有所有 .qm 文件,但不知道如何使用它们。

我真的找不到太多关于此的文档,而且我尝试的任何方法似乎都不起作用。

最佳答案

关于这个主题有大量文档,可以在明显的地方找到:

下面是一个简单的演示脚本(使用-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 - pyqt4如何实现多语言支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757952/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com