Package: pyqt5-auto-translate
PyQt5 Auto Translate中文pyqt5_auto_translateis an automatic translation library for PyQt5, which allows you to easily add multi-language support to your PyQt5 programs with minimal modification to your existing code and without the need for additional toolchains.InstallationInstall using pip:pipinstallpyqt5_auto_translateCurrently Supported Translated ComponentsOriginal ComponentTranslated ComponentQLabelTranslatedQLabelQPushButtonTranslatedQPushButtonQCheckBoxTranslatedQCheckBoxQMenuTranslatedQMenuQActionTranslatedQActionExampleTranslating the Python Code InterfaceHere is an example code without integrated translation:importsysfromPyQt5.QtCoreimportQtfromPyQt5.QtWidgetsimport(QApplication,QMainWindow,QWidget,QVBoxLayout,QHBoxLayout,QLabel,QLineEdit,QPushButton,QProgressBar,QSlider,QLCDNumber,QGroupBox)classExampleUI(QWidget):def__init__(self):super().__init__()self.layout=QVBoxLayout()self.title_label=QLabel("Example UI")self.layout.addWidget(self.title_label)self.hbox_layout=QHBoxLayout()self.layout.addLayout(self.hbox_layout)self.hbox_layout.addWidget(QLabel("Username: "))self.username_input=QLineEdit()self.hbox_layout.addWidget(self.username_input)self.hbox_layout2=QHBoxLayout()self.layout.addLayout(self.hbox_layout2)self.hbox_layout2.addWidget(QLabel("Password: "))self.password_input=QLineEdit()self.password_input.setEchoMode(QLineEdit.Password)self.hbox_layout2.addWidget(self.password_input)self.submit_button=QPushButton("Submit")self.layout.addWidget(self.submit_button)self.progress_bar=QProgressBar()self.progress_bar.setRange(0,100)self.layout.addWidget(self.progress_bar)self.slider=QSlider(Qt.Horizontal)self.layout.addWidget(self.slider)self.lcd_number=QLCDNumber()self.lcd_number.setSegmentStyle(QLCDNumber.Flat)self.layout.addWidget(self.lcd_number)self.group_box=QGroupBox("Options")self.group_box_layout=QVBoxLayout()self.group_box.setLayout(self.group_box_layout)self.checkbox1=QPushButton("Option 1")self.checkbox2=QPushButton("Option 2")self.checkbox3=QPushButton("Option 3")self.group_box_layout.addWidget(self.checkbox1)self.group_box_layout.addWidget(self.checkbox2)self.group_box_layout.addWidget(self.checkbox3)self.layout.addWidget(self.group_box)self.setLayout(self.layout)if__name__=='__main__':app=QApplication(sys.argv)window=QMainWindow()ui=ExampleUI()window.setCentralWidget(ui)window.show()sys.exit(app.exec_())To enable translation support, only a few lines of code are needed, and there's no need to modify the original code. Here is the modified code that supports translation:importsysfromPyQt5.QtCoreimportQtfromPyQt5.QtWidgetsimport(QApplication,QMainWindow,QWidget,QVBoxLayout,QHBoxLayout,QLabel,QLineEdit,QPushButton,QProgressBar,QSlider,QLCDNumber,QGroupBox)#frompyqt5_auto_translateimport\TranslatedQLabelasQLabel,\TranslatedQPushButtonasQPushButton,translater,\ExampleTranslateMenuBartranslater.set_translation_file('translations.yml')classExampleUI(QWidget):def__init__(self):super().__init__()self.layout=QVBoxLayout()self.title_label=QLabel("Example UI")self.layout.addWidget(self.title_label)self.hbox_layout=QHBoxLayout()self.layout.addLayout(self.hbox_layout)self.hbox_layout.addWidget(QLabel("Username: "))self.username_input=QLineEdit()self.hbox_layout.addWidget(self.username_input)self.hbox_layout2=QHBoxLayout()self.layout.addLayout(self.hbox_layout2)self.hbox_layout2.addWidget(QLabel("Password: "))self.password_input=QLineEdit()self.password_input.setEchoMode(QLineEdit.Password)self.hbox_layout2.addWidget(self.password_input)self.submit_button=QPushButton("Submit")self.layout.addWidget(self.submit_button)self.progress_bar=QProgressBar()self.progress_bar.setRange(0,100)self.layout.addWidget(self.progress_bar)self.slider=QSlider(Qt.Horizontal)self.layout.addWidget(self.slider)self.lcd_number=QLCDNumber()self.lcd_number.setSegmentStyle(QLCDNumber.Flat)self.layout.addWidget(self.lcd_number)self.group_box=QGroupBox("Options")self.group_box_layout=QVBoxLayout()self.group_box.setLayout(self.group_box_layout)self.checkbox1=QPushButton("Option 1")self.checkbox2=QPushButton("Option 2")self.checkbox3=QPushButton("Option 3")self.group_box_layout.addWidget(self.checkbox1)self.group_box_layout.addWidget(self.checkbox2)self.group_box_layout.addWidget(self.checkbox3)self.layout.addWidget(self.group_box)self.setLayout(self.layout)if__name__=='__main__':app=QApplication(sys.argv)window=QMainWindow()window.setMenuBar(ExampleTranslateMenuBar())# add the translated MenuBarui=ExampleUI()window.setCentralWidget(ui)window.show()sys.exit(app.exec_())from pyqt5_auto_translate import \
    TranslatedQLabel as QLabel, \
    TranslatedQPushButton as QPushButton, translater, \
    ExampleTranslateMenuBar

translater.set_translation_file('translations.yml')

    window.setMenuBar(ExampleTranslateMenuBar())  # add the translated MenuBarTranslation file:#translations.ymlsupported_languages:-en-zh-CN-zh-TWtranslations:en:en:Englishzh-CN:英语zh-TW:英語zh-CN:en:Simplified Chinesezh-CN:简体中文zh-TW:簡體中文zh-TW:en:Traditional Chinesezh-CN:繁体中文zh-TW:繁體中文language:en:languagezh-CN:语言zh-TW:語言Language:en:Languagezh-CN:语言zh-TW:語言Options:en:Optionszh-CN:选项zh-TW:選項Option 1:en:Option 1zh-CN:选项1zh-TW:選項1Option 2:en:Option 2zh-CN:选项2zh-TW:選項2Option 3:en:Option 3zh-CN:选项3zh-TW:選項3'Password:':en:Passwordzh-CN:密码zh-TW:密碼Submit:en:Submitzh-CN:提交zh-TW:提交Example UI:en:Example UIzh-CN:示例界面zh-TW:範例介面'Username:':en:Usernamezh-CN:用户名zh-TW:使用者名稱Translating UIHere is an example code without integrated translation:importsysfromPyQt5.QtWidgetsimportQApplication,QMainWindowfromPyQt5.uicimportloadUiapp=QApplication(sys.argv)main_window=QMainWindow()load_ui_with_translation('example.ui',main_window)main_window.show()sys.exit(app.exec_())Here is an example code with integrated translation:frompyqt5_auto_translateimportExampleTranslateMenuBar,load_ui_with_translation#here changedimportsysfromPyQt5.QtWidgetsimportQApplication,QMainWindowapp=QApplication(sys.argv)main_window=QMainWindow()load_ui_with_translation('example.ui',main_window)#here changedmain_window.setMenuBar(ExampleTranslateMenuBar())#here changedmain_window.show()sys.exit(app.exec_())In the code with integrated translation, theExampleTranslateMenuBar()is added to provide a translated menu bar. Also, theload_ui_with_translationfunction is imported frompyqt5_auto_translate.ContributionIf you find any errors or would like to improve this library, please feel free to raise issues or pull requests. We welcome your contributions!