uic.loadUi
如果您使用 loadUi 方法,它不会实现 retranslateUi 方法,因此如果您使用该方法,则解决方案与上一篇文章相同。
演示.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Demo</class>
<widget class="QWidget" name="Demo">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>102</width>
<height>108</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="combo"/>
</item>
<item>
<widget class="QPushButton" name="button">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Hello, World</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
主文件
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class Demo(QtWidgets.QWidget):
def __init__(self):
super(Demo, self).__init__()
uic.loadUi("demo.ui", self)
self.combo.currentIndexChanged.connect(self.change_func)
self.trans = QtCore.QTranslator(self)
options = [
("English", ""),
("français", "eng-fr"),
("中文", "eng-chs"),
]
for i, (text, lang) in enumerate(options):
self.combo.addItem(text)
self.combo.setItemData(i, lang)
self.retranslateUi()
@QtCore.pyqtSlot(int)
def change_func(self, index):
data = self.combo.itemData(index)
if data:
self.trans.load(data)
QtWidgets.QApplication.instance().installTranslator(self.trans)
else:
QtWidgets.QApplication.instance().removeTranslator(self.trans)
def changeEvent(self, event):
if event.type() == QtCore.QEvent.LanguageChange:
self.retranslateUi()
super(Demo, self).changeEvent(event)
def retranslateUi(self):
self.button.setText(QtWidgets.QApplication.translate("Demo", "Start"))
self.label.setText(QtWidgets.QApplication.translate("Demo", "Hello, World"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())
然后生成.ts:
pylupdate5 main.py -ts eng-chs.ts
pylupdate5 main.py -ts eng-fr.ts
然后使用 Qt Linguist 进行翻译。
最后是.qm:
lrelease eng-fr.ts eng-fr.qm
lrelease eng-chs.ts eng-chs.qm
皮尤克
另一方面,如果您使用 pyuic5 来转换 .py ,那么如果实现了 retranslateUi 方法,那么您可以使用它:
pyuic5 demo.ui -o demo_ui.py
pylupdate5 demo_ui.py -ts eng-chs.ts
pylupdate5 demo_ui.py -ts eng-fr.ts
然后使用 Qt Linguist 进行翻译。
最后是.qm:
lrelease eng-fr.ts eng-fr.qm
lrelease eng-chs.ts eng-chs.qm
主文件
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from demo_ui import Ui_Demo
class Demo(QtWidgets.QWidget, Ui_Demo):
def __init__(self):
super(Demo, self).__init__()
self.setupUi(self)
self.combo.currentIndexChanged.connect(self.change_func)
self.trans = QtCore.QTranslator(self)
options = [
("English", ""),
("français", "eng-fr"),
("中文", "eng-chs"),
]
for i, (text, lang) in enumerate(options):
self.combo.addItem(text)
self.combo.setItemData(i, lang)
self.retranslateUi(self)
@QtCore.pyqtSlot(int)
def change_func(self, index):
data = self.combo.itemData(index)
if data:
self.trans.load(data)
QtWidgets.QApplication.instance().installTranslator(self.trans)
else:
QtWidgets.QApplication.instance().removeTranslator(self.trans)
def changeEvent(self, event):
if event.type() == QtCore.QEvent.LanguageChange:
self.retranslateUi(self)
super(Demo, self).changeEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())