Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

python - Internationalization (translation) of dialog and the main window in a pyqt5 application

I am trying to translate my small application written in pyside2/pyqt5 to several languages, for example, Chinese. After googling, I managed to change the main window to Chinese after select from the menu -> language -> Chinese. However, the pop up dialog from menu -> option still remains English version. It seems the translation info is not transferred to the dialog. How do I solve this?

Basically, I build two ui files in designer and convert to two python files:One mainui.py and one dialogui.py. I then convert the two python file into one *.ts file using

pylupdate5 -verbose mainui.py dialogui.py -ts zh_CN.ts

after that, in linguist input the translation words. I can see the items in the dialog, which means this information is not missing. Then I release the file as zh_CN.qm file. All this supporting file I attached below using google drive.

Supporting files for the question

The main file is as

import os
import sys

from PySide2 import QtCore, QtGui, QtWidgets
from mainui import Ui_MainWindow

from dialogui import Ui_Dialog

class OptionsDialog(QtWidgets.QDialog,Ui_Dialog):
    def __init__(self,parent):
        super().__init__(parent)
        self.setupUi(self)
        self.retranslateUi(self)


class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.actionConfigure.triggered.connect(self.showdialog)
        self.actionChinese.triggered.connect(self.change_lang)

    def showdialog(self):
        dlg = OptionsDialog(self)
        dlg.exec_()

    def change_lang(self):
        trans = QtCore.QTranslator()
        trans.load('zh_CN')
        QtCore.QCoreApplication.instance().installTranslator(trans)
    self.retranslateUi(self)



if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    ret = app.exec_()
    sys.exit(ret)

I think it should be a typical task because almost no application will only have a mainwindow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to overwrite the changeEvent() method and call retranslateUi() when the event is of type QEvent::LanguageChange, on the other hand the QTranslator object must be a member of the class but it will be deleted and it will not exist when the changeEvent() method is called.

Finally assuming that the Language menu is used to establish only translations, a possible option is to establish the name of the .qm as data of the QActions and to use the triggered method of the QMenu as I show below:

from PySide2 import QtCore, QtGui, QtWidgets
from mainui import Ui_MainWindow

from dialogui import Ui_Dialog

class OptionsDialog(QtWidgets.QDialog,Ui_Dialog):
    def __init__(self,parent):
        super().__init__(parent)
        self.setupUi(self)

    def changeEvent(self, event):
        if event.type() == QtCore.QEvent.LanguageChange:
            self.retranslateUi(self)
        super(OptionsDialog, self).changeEvent(event)

class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.m_translator = QtCore.QTranslator(self)
        self.actionConfigure.triggered.connect(self.showdialog)
        self.menuLanguage.triggered.connect(self.change_lang)
        # set translation for each submenu
        self.actionChinese.setData('zh_CN')

    @QtCore.Slot()
    def showdialog(self):
        dlg = OptionsDialog(self)
        dlg.exec_()

    @QtCore.Slot(QtWidgets.QAction)
    def change_lang(self, action):
        QtCore.QCoreApplication.instance().removeTranslator(self.m_translator)
        if self.m_translator.load(action.data()):
            QtCore.QCoreApplication.instance().installTranslator(self.m_translator)

    def changeEvent(self, event):
        if event.type() == QtCore.QEvent.LanguageChange:
            self.retranslateUi(self)
        super(MainWindow, self).changeEvent(event)

if __name__=='__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    ret = app.exec_()
    sys.exit(ret)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...