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

PyQt Localization: Translating a text through QPushButton w/ use of .po file

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

无论如何,我想在按下特定 QPushButton 时将特定文本从英语翻译成爱尔兰语,反之亦然。每当按下 QPushButton 时,我都尝试调用该函数,但按钮不会为我执行 "magic translation"。关于如何做到这一点的任何想法?

一如既往,如有任何帮助,我们将不胜感激!

这是我的完整 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?"

问题如下:

  • 您必须在安装翻译后调用 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