pyqt5应用程序中对话框和主窗口的国际化(翻译) [英] Internationalization (translation) of dialog and the main window in a pyqt5 application

查看:346
本文介绍了pyqt5应用程序中对话框和主窗口的国际化(翻译)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将pyside2/pyqt5编写的小型应用程序翻译成多种语言,例如中文.谷歌搜索后,我设法从菜单->语言->中文中选择了主窗口,将其更改为中文.但是,菜单->选项的弹出对话框仍为英文版本.似乎翻译信息没有传输到对话框.我该如何解决?

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?

基本上,我在设计器中构建了两个ui文件,并转换为两个python文件:一个mainui.py和一个dialogui.py.然后,我使用

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

然后,在语言学家中输入翻译词.我可以在对话框中看到项目,这意味着此信息不丢失.然后我将文件发布为zh_CN.qm文件.我在下面使用Google云端硬盘所附的所有支持文件.

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.

支持该问题的文件

主文件为

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.

推荐答案

当事件的类型为QEvent::LanguageChange时,您必须覆盖changeEvent()方法并调用retranslateUi(),另一方面,是QTranslator对象必须是该类的成员,但是它将被删除,并且在调用changeEvent()方法时将不存在.

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.

最后假设语言"菜单仅用于建立翻译,一个可能的选择是建立.qm的名称作为QAction的数据,并使用QMenu的触发方法下方:

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)

这篇关于pyqt5应用程序中对话框和主窗口的国际化(翻译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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