GNU gettext Example¶
This example demonstrates the use of GNU gettext for translating applications as described in translations.
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations
from pathlib import Path
import gettext
import sys
from PySide6.QtCore import QItemSelection, Slot
from PySide6.QtWidgets import (QAbstractItemView, QApplication, QListWidget,
QMainWindow)
_ = None
ngettext = None
class Window(QMainWindow):
def __init__(self):
super().__init__()
file_menu = self.menuBar().addMenu(_("&File"))
quit_action = file_menu.addAction(_("Quit"))
quit_action.setShortcut(_("CTRL+Q"))
quit_action.triggered.connect(self.close)
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 = ngettext("{0} language selected",
"{0} languages selected", count).format(count)
self.statusBar().showMessage(message)
if __name__ == '__main__':
app = QApplication(sys.argv)
src_dir = Path(__file__).resolve().parent
try:
translation = gettext.translation('example', localedir=src_dir / 'locales')
if translation:
translation.install()
_ = translation.gettext
ngettext = translation.ngettext
except FileNotFoundError:
pass
if not _:
_ = gettext.gettext
ngettext = gettext.ngettext
print('No translation found')
window = Window()
window.show()
sys.exit(app.exec())
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PySide6 gettext example\n"
"POT-Creation-Date: 2021-07-05 14:16+0200\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural==n != 1;\n"
#: main.py:57
msgid "&File"
msgstr ""
#: main.py:58
msgid "Quit"
msgstr ""
#: main.py:59
msgid "CTRL+Q"
msgstr ""
#: main.py:73
#, python-brace-format
msgid "{0} language selected"
msgid_plural "{0} languages selected"
msgstr[0] ""
msgstr[1] ""
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PySide6 gettext example\n"
"POT-Creation-Date: 2021-07-05 14:16+0200\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
#: main.py:57
msgid "&File"
msgstr "&Datei"
#: main.py:58
msgid "Quit"
msgstr "Beenden"
#: main.py:59
msgid "CTRL+Q"
msgstr "CTRL+B"
#: main.py:73
#, python-brace-format
msgid "{0} language selected"
msgid_plural "{0} languages selected"
msgstr[0] "{0} Sprache ausgewählt"
msgstr[1] "{0} Sprachen ausgewählt"