madLad
madLad

Reputation: 155

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

So I've tried researching for solutions for this what-seem-to-be simple question I have but to my surprise, I could not find anything about it.

Anyway, I would like to translate a specific text from English to Irish and vice versa when the specific QPushButton is pressed. I tried calling the function whenever the QPushButton is pressed but the buttons doesn't do the "magic translation" for me. Any ideas on how to do this?

As always, any help would be appreciated!

Here's my full PyQt code:

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

Just in case it's needed, .po file of the English translation:

# 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 file of the Irish translation:

# 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?"

Upvotes: 2

Views: 2012

Answers (1)

eyllanesc
eyllanesc

Reputation: 243975

The problems are as follows:

  • You must call the retranslateUi function after installing the translations. For example:

def ieTranslate(self, MainWindow):
    self.messages = gettext.translation('messages', localedir='locale', languages=['ie'])
    self.messages.install()
    self.retranslateUi(MainWindow)
  • You must use a lambda function to pass Mainwindow as a parameter to the functions that translate. For example:

self.pushButton_EN.clicked.connect(lambda: self.enTranslate(MainWindow))

complete code

Upvotes: 1

Related Questions