Skip to main content

A lightweight and easy-to-use library for automating the translation of PyQt5 widgets.

Project description

PyQt5 Auto Translate

中文

pyqt5_auto_translate is 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.

Installation

Install using pip:

pip install pyqt5_auto_translate

Currently Supported Translated Components

Original Component Translated Component
QLabel TranslatedQLabel
QPushButton TranslatedQPushButton
QCheckBox TranslatedQCheckBox
QMenu TranslatedQMenu
QAction TranslatedQAction

Example

Translating the Python Code Interface

Here is an example code without integrated translation:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel,
    QLineEdit, QPushButton, QProgressBar, QSlider, QLCDNumber, QGroupBox
)
class ExampleUI(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:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel,
    QLineEdit, QPushButton, QProgressBar, QSlider, QLCDNumber, QGroupBox
)
# 
from pyqt5_auto_translate import \
    TranslatedQLabel as QLabel, \
    TranslatedQPushButton as QPushButton, translater, \
    ExampleTranslateMenuBar

translater.set_translation_file('translations.yml')


class ExampleUI(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 MenuBar

    ui = 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 MenuBar

Translation file:

#translations.yml
supported_languages:
  - en
  - zh-CN
  - zh-TW

translations:
  en:
    en: English
    zh-CN: 英语
    zh-TW: 英語
  zh-CN:
    en: Simplified Chinese
    zh-CN: 简体中文
    zh-TW: 簡體中文
  zh-TW:
    en: Traditional Chinese
    zh-CN: 繁体中文
    zh-TW: 繁體中文
  language:
    en: language
    zh-CN: 语言
    zh-TW: 語言
  Language:
    en: Language
    zh-CN: 语言
    zh-TW: 語言
  Options:
    en: Options
    zh-CN: 选项
    zh-TW: 選項

  Option 1:
    en: Option 1
    zh-CN: 选项1
    zh-TW: 選項1

  Option 2:
    en: Option 2
    zh-CN: 选项2
    zh-TW: 選項2

  Option 3:
    en: Option 3
    zh-CN: 选项3
    zh-TW: 選項3

  'Password: ':
    en: Password
    zh-CN: 密码
    zh-TW: 密碼

  Submit:
    en: Submit
    zh-CN: 提交
    zh-TW: 提交

  Example UI:
    en: Example UI
    zh-CN: 示例界面
    zh-TW: 範例介面

  'Username: ':
    en: Username
    zh-CN: 用户名
    zh-TW: 使用者名稱

Translating UI

Here is an example code without integrated translation:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi

app = 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:

from pyqt5_auto_translate import ExampleTranslateMenuBar,load_ui_with_translation #here changed
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)

main_window = QMainWindow()

load_ui_with_translation('example.ui', main_window)#here changed
main_window.setMenuBar(ExampleTranslateMenuBar()) #here changed
main_window.show()

sys.exit(app.exec_())

In the code with integrated translation, the ExampleTranslateMenuBar() is added to provide a translated menu bar. Also, the load_ui_with_translation function is imported from pyqt5_auto_translate.

Contribution

If you find any errors or would like to improve this library, please feel free to raise issues or pull requests. We welcome your contributions!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqt5_auto_translate-0.1.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyqt5_auto_translate-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pyqt5_auto_translate-0.1.1.tar.gz.

File metadata

  • Download URL: pyqt5_auto_translate-0.1.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for pyqt5_auto_translate-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b6ca7c59f237ed65321d818ab46c5014e2c3d5cb8d67f41492f7ad6b728c06d8
MD5 f241572db10a1b8d0fec1a0d9081c3e8
BLAKE2b-256 e3b4af1ca101013c47d4f9e8898b490f98262e38cd57d6609b888bc33f03f5d4

See more details on using hashes here.

File details

Details for the file pyqt5_auto_translate-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pyqt5_auto_translate-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3256508960fe77073f946669bc90b2bc1fdf975259eb7eb9337c23346f43b8f2
MD5 337d8392058063d82bea68e6859d549f
BLAKE2b-256 53b5ab9f70bc065a6839b0830bf9b0b2590ae1213af6d67e654c3655e9d3553d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page