0

我对部分翻译有疑问。我使用 Qt Designer 创建我的 ui 图形界面。根据一本关于​​ PyQt5 的书中的一章,我过去能够在不使用 Qt Designer 的情况下翻译应用程序。

但我从未翻译过用它制作的界面。事实上,准确地说,我有部分翻译。它涉及我在应用程序代码中引入的字符串(setToolTip、setStatusTip、....),但不涉及其余部分。我可以在无忧无虑的英语和法语之间切换,要么不做任何事情(对于法语),要么强迫它:

python start_app.py en_EN

我在 Manjaro,python 3.8,pyqt5.14.2。

我的代码不好吗?我错过了什么?

  • 关于这些翻译,我还有 3 点需要澄清:是否有必要包含所有项目文件才能从所有这些翻译中受益?如您所见,我只将 qdvgrab 放入我的代码中。

  • 当我用 Qt Linguist 翻译我的文件时,我没有要翻译的 ui 文件的字符串。这是正常的吗?翻译不是自动的吗?怎么做 ?我是否必须将它们包含在我的专业文件中并重做我的翻译链(pylupdate、Qt Linguist?)

  • 目前,我的 ts、qm、pro 文件与我的程序 py 文件处于同一级别。但稍后,我打算将它们放在一个名为 locale 或 translation 的文件夹中。知道启动应用程序的文件是 start_app.py 并且它位于根包(见截图),如何导入它们?那时我的 python 文件也将位于 windows 文件夹中,因此与其他文件处于同一级别(ui、图像、翻译)。如何将这些文件导入 start_app.py 以便它可以读取它们。

这是我在 start_app.py 中介绍的代码

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTranslator, QLocale
from qdvgrab import QdvGrab

if __name__ == "__main__":
    application = QApplication(sys.argv)

    # Translate application
    enNativeLanguage = len(sys.argv) == 1
    if enNativeLanguage:
        locale = QLocale()
        # translator.load(locale, "qdvgrab", ".")

    else:
        languageCountry = sys.argv[1]
    translators = []
    for prefixQm in ("qdvgrab.", "qt_", "qtbase_"):
        translator = QTranslator()
        translators.append(translator)
        if enNativeLanguage:
            translator.load(locale, prefixQm)
        else:
            translator.load(prefixQm+languageCountry)
    application.installTranslator(translator)

这是结构:

在此处输入图像描述

用最少的代码更新以重现此行为:

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTranslator, QLocale

from minimal import MiniMal


if __name__ == "__main__":
    application = QApplication(sys.argv)

    # Translate application
    enNativeLanguage = len(sys.argv) == 1
    if enNativeLanguage:
        locale = QLocale()
    else:
        languageCountry = sys.argv[1]
    translators = []
    for prefixQm in ("minimal.", "qt_", "qtbase_"):
        translator = QTranslator()
        translators.append(translator)
        if enNativeLanguage:
            translator.load(locale, prefixQm)
        else:
            translator.load(prefixQm+languageCountry)
    application.installTranslator(translator)

    window = MiniMal()
    window.show()
    rc = application.exec_()
    sys.exit(rc)

最小的.py 代码

from PyQt5.QtWidgets import QMainWindow, QMessageBox
from ui.exempleui import Ui_MainWindow


class MiniMal(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MiniMal, self).__init__(*args, **kwargs)
        self.setupUi()
        self.connectActions()

        format_capture = ['Dv Raw (.dv)', 'DV 2 (.avi)', 'Dv (.avi)', 'Hdv (.m2t)', 'Mpeg 2 (.mpg)', 'Mov (.mov)']
        for format in format_capture:
            self.ui.cmbformatacquisition.addItem(format)
            self.ui.cmbformatacquisition.setCurrentIndex(0)

    def setupUi(self):

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.lnetext.setText(self.tr("My Awesome Movie"))
        self.ui.btnsomething.setToolTip(self.tr("Choose the Dv Acquisition mode"))
        self.ui.lnetext.setToolTip(self.tr("Set the Film Name"))
        self.ui.cmbformatacquisition.setToolTip(self.tr("Choose a format"))
        self.ui.chksystray.setToolTip(self.tr("Application in the systray"))
        self.ui.lnetext.setStatusTip(self.tr('The Film name is '))
        self.ui.btnsomething.setStatusTip(self.tr('Dv Acquisition mode is used'))
        self.ui.cmbformatacquisition.setStatusTip(self.tr("Acquisition Mode Changed"))
        self.ui.chksystray.setStatusTip(self.tr('Show this window in the systray'))

    def connectActions(self):
        self.ui.cmbformatacquisition.currentIndexChanged.connect(self.callSomething)
        self.ui.lnetext.textChanged.connect(self.showName)
        self.ui.chksystray.toggled.connect(self.putSystray)
        self.ui.btnsomething.clicked.connect(self.callSomething)

    def changeChoice(self):
        pass

    def putSystray(self):
        pass

    def showName(self):
        pass

    def callSomething(self):
        pass

以及ui文件代码

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(560, 339)
        MainWindow.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedKingdom))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lnetext = QtWidgets.QLineEdit(self.centralwidget)
        self.lnetext.setGeometry(QtCore.QRect(210, 30, 251, 26))
        self.lnetext.setObjectName("lnetext")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(36, 30, 101, 20))
        self.label.setObjectName("label")
        self.btnsomething = QtWidgets.QPushButton(self.centralwidget)
        self.btnsomething.setGeometry(QtCore.QRect(70, 90, 361, 71))
        self.btnsomething.setObjectName("btnsomething")
        self.cmbformatacquisition = QtWidgets.QComboBox(self.centralwidget)
        self.cmbformatacquisition.setGeometry(QtCore.QRect(80, 175, 351, 51))
        self.cmbformatacquisition.setObjectName("cmbformatacquisition")
        self.chksystray = QtWidgets.QCheckBox(self.centralwidget)
        self.chksystray.setGeometry(QtCore.QRect(60, 250, 371, 26))
        self.chksystray.setObjectName("chksystray")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 560, 28))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuHelp = QtWidgets.QMenu(self.menubar)
        self.menuHelp.setObjectName("menuHelp")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionQuit = QtWidgets.QAction(MainWindow)
        self.actionQuit.setObjectName("actionQuit")
        self.actionAbout = QtWidgets.QAction(MainWindow)
        self.actionAbout.setObjectName("actionAbout")
        self.menuFile.addAction(self.actionQuit)
        self.menuHelp.addAction(self.actionAbout)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuHelp.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "Name"))
        self.btnsomething.setText(_translate("MainWindow", "Click me"))
        self.chksystray.setText(_translate("MainWindow", "Put in this window in the systray"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuHelp.setTitle(_translate("MainWindow", "Help"))
        self.actionQuit.setText(_translate("MainWindow", "Quit"))
        self.actionAbout.setText(_translate("MainWindow", "About"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
4

0 回答 0