PyQt本地化:使用.po文件通过QPushButton翻译文本

时间:2017-04-25 03:00:53

标签: python pyqt qpushbutton po

所以我已经尝试过研究这个看似简单的问题的解决方案,但令我惊讶的是,我找不到任何关于它的信息。

无论如何,当按下特定的QPushButton时,我想将特定文本从英语翻译成爱尔兰语,反之亦然。我试着在按下QPushButton时调用该函数,但按钮不为我做“魔术翻译”。关于如何做到这一点的任何想法?

与往常一样,任何帮助都将不胜感激!

这是我的完整PyQt代码:

from PyQt4 import QtCore, QtGui
import sys
import gettext
import locale

#Utf-8 Encoding generated from Qt Designer
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__()
        #Calling the Slides Widget class and the MainWindow setup
        self.setupUi(self)

    def setupUi(self, MainWindow):
        MainWindow.resize(1269, 688)

        self.label_4 = QtGui.QLabel(self)
        self.label_4.setGeometry(QtCore.QRect(110, 30, 191, 21))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Lucida Sans"))
        font.setPointSize(12)
        font.setBold(False)
        font.setWeight(50)
        self.label_4.setFont(font)

        self.pushButton_EN = QtGui.QPushButton(self)
        self.pushButton_EN.setGeometry(QtCore.QRect(1000, 30, 75, 23))
        self.pushButton_EN.clicked.connect(self.engTranslate)

        self.pushButton_IE = QtGui.QPushButton(self)
        self.pushButton_IE.setGeometry(QtCore.QRect(1100, 30, 75, 23))
        self.pushButton_IE.clicked.connect(self.ieTranslate)

        #---------------
        #Switch both of these around and you will get a different value
        self.ieTranslate(MainWindow)
        self.engTranslate(MainWindow)
        #Only the bottom line between this two lines is being read
        #---------------

        self.retranslateUi(MainWindow)


    def engTranslate(self, MainWindow):
        self.messages = gettext.translation('messages', localedir='locale', languages=['en'])
        self.messages.install()

    def ieTranslate(self, MainWindow):
        self.messages = gettext.translation('messages', localedir='locale', languages=['ie'])
        self.messages.install()

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Erasmus", None))
        self.pushButton_EN.setText(_translate("MainWindow", _("English"), None))
        self.pushButton_IE.setText(_translate("MainWindow", _("Irish"), None))
        self.label_4.setText(_translate("MainWindow", _("Don\'t have an account?"), None))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Win = MainWindow()
    Win.show()
    sys.exit(app.exec_())

以防万一需要,.po文件的英文翻译:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2017-04-25 03:52+0100\n"
"PO-Revision-Date: 2017-04-25 03:52+0100\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.0.1\n"
"Last-Translator: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: en\n"

#: erasmus_test.py:68
msgid "English"
msgstr "English"

#: erasmus_test.py:69
msgid "Irish"
msgstr "Irish"

#: erasmus_test.py:70
msgid "Don't have an account?"
msgstr "Don't have an account?"

.po文件的爱尔兰语翻译:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2017-04-25 03:52+0100\n"
"PO-Revision-Date: 2017-04-25 03:53+0100\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.0.1\n"
"Last-Translator: \n"
"Plural-Forms: nplurals=5; plural=(n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4);\n"
"Language: ga\n"

#: erasmus_test.py:68
msgid "English"
msgstr "Béarla"

#: erasmus_test.py:69
msgid "Irish"
msgstr "Gaeilge"

#: erasmus_test.py:70
msgid "Don't have an account?"
msgstr "Ná bhfuil cuntas?"

1 个答案:

答案 0 :(得分:1)

问题如下:

  • 安装翻译后,您必须调用retranslateUi函数。例如:
def ieTranslate(self, MainWindow):
    self.messages = gettext.translation('messages', localedir='locale', languages=['ie'])
    self.messages.install()
    self.retranslateUi(MainWindow)
  • 您必须使用lambda函数将Mainwindow作为参数传递给转换的函数。例如:
self.pushButton_EN.clicked.connect(lambda: self.enTranslate(MainWindow))

complete code