你如何实现对pyqt4的多语言支持 [英] How do you implement multilingual support for pyqt4

查看:67
本文介绍了你如何实现对pyqt4的多语言支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pyqt4 program and would like to implement multilingual support. I have all the .qm files, but can't figure out how to use them.

我真的找不到太多关于这方面的文档,而且我尝试的任何东西似乎都不起作用.

I can't really find much documentation on this, and nothing I try seems to work right.

推荐答案

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

There's tons of documentation on this subject, which can be found in the obvious places:

下面是一个简单的演示脚本(使用-h运行):

Below is a simple demo script (run with -h for usage):

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_())

这篇关于你如何实现对pyqt4的多语言支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆