Qt Linguist 示例#

此示例演示了如何使用 Qt Linguist 和相关工具将应用程序翻译成描述。有关详情,请参阅 翻译应用程序

下载 这个 示例

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import sys

from PySide6.QtCore import (QItemSelection, QLibraryInfo, QLocale, QTranslator,
                            Slot)
from PySide6.QtWidgets import (QAbstractItemView, QApplication, QListWidget,
                               QMainWindow)


import linguist_rc  # noqa: F401


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        file_menu = self.menuBar().addMenu(self.tr("&File"))
        quit_action = file_menu.addAction(self.tr("Quit"))
        quit_action.setShortcut(self.tr("CTRL+Q"))
        quit_action.triggered.connect(self.close)
        help_menu = self.menuBar().addMenu(self.tr("&Help"))
        about_qt_action = help_menu.addAction(self.tr("About Qt"))
        about_qt_action.triggered.connect(qApp.aboutQt)  # noqa: F821

        self._list_widget = QListWidget()
        self._list_widget.setSelectionMode(QAbstractItemView.MultiSelection)
        self._list_widget.selectionModel().selectionChanged.connect(self.selection_changed)
        self._list_widget.addItem("C++")
        self._list_widget.addItem("Java")
        self._list_widget.addItem("Python")
        self.setCentralWidget(self._list_widget)

    @Slot(QItemSelection, QItemSelection)
    def selection_changed(self, selected, deselected):
        count = len(self._list_widget.selectionModel().selectedRows())
        message = self.tr("%n language(s) selected", "", count)
        self.statusBar().showMessage(message)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    path = QLibraryInfo.path(QLibraryInfo.TranslationsPath)
    translator = QTranslator(app)
    if translator.load(QLocale.system(), 'qtbase', '_', path):
        app.installTranslator(translator)
    translator = QTranslator(app)
    path = ':/translations'
    if translator.load(QLocale.system(), 'example', '_', path):
        app.installTranslator(translator)

    window = Window()
    window.show()
    sys.exit(app.exec())
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="translations">
    <file>example_de.qm</file>
</qresource>
</RCC>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE" sourcelanguage="de_DE">
<context>
    <name>Window</name>
    <message>
        <location filename="main.py" line="56"/>
        <source>&amp;File</source>
        <translation>&amp;Datei</translation>
    </message>
    <message>
        <location filename="main.py" line="57"/>
        <source>Quit</source>
        <translation>Beenden</translation>
    </message>
    <message>
        <location filename="main.py" line="58"/>
        <source>CTRL+Q</source>
        <translation>CTRL+B</translation>
    </message>
    <message>
        <location filename="main.py" line="60"/>
        <source>&amp;Help</source>
        <translation>Hilfe</translation>
    </message>
    <message>
        <location filename="main.py" line="61"/>
        <source>About Qt</source>
        <translation>Über Qt</translation>
    </message>
    <message numerus="yes">
        <location filename="main.py" line="75"/>
        <source>%n language(s) selected</source>
        <translation>
            <numerusform>Eine Sprache ausgewählt</numerusform>
            <numerusform>%n Sprachen ausgewählt</numerusform>
        </translation>
    </message>
</context>
</TS>