From be1a29219c5e1d9e99cec88e94e0f2e834c89359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20P=2E=20M=C3=B6ller?= Date: Sat, 12 Sep 2020 22:40:15 -0300 Subject: [PATCH] add support for internationalization with gettext and updated setup script for .po format --- debian/changelog | 6 ++ debian/control | 1 + lubuntu-notifier.py | 42 +++++++----- lubuntu-upgrader | 44 +++++++------ po/es.po | 114 +++++++++++++++++++++++++++++++++ po/lubuntu-update-notifier.pot | 110 +++++++++++++++++++++++++++++++ setup.py | 43 +++++++++++-- translation_helper | 2 + 8 files changed, 323 insertions(+), 39 deletions(-) create mode 100644 po/es.po create mode 100644 po/lubuntu-update-notifier.pot create mode 100644 translation_helper diff --git a/debian/changelog b/debian/changelog index 585f21d..fb408f5 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +lubuntu-update-notifier (0.4dev) UNRELEASED; urgency=medium + + * added internationalization support + + -- Hans P Möller Sat, 12 Sep 2020 13:59:25 -0300 + lubuntu-update-notifier (0.3) groovy; urgency=medium * Added affected packages info. diff --git a/debian/control b/debian/control index b5d1985..66cc7c3 100644 --- a/debian/control +++ b/debian/control @@ -4,6 +4,7 @@ Priority: optional Maintainer: Hans P Möller Build-Depends: debhelper-compat (=13), dh-python, + gettext, python3-all, python3-apt, python3-setuptools diff --git a/lubuntu-notifier.py b/lubuntu-notifier.py index 3b8c0d4..060b4ab 100755 --- a/lubuntu-notifier.py +++ b/lubuntu-notifier.py @@ -23,6 +23,7 @@ import subprocess from pathlib import Path import apt_pkg from argparse import ArgumentParser +import gettext from PyQt5.QtWidgets import (QWidget, QApplication, QLabel, QPushButton, QHBoxLayout, QVBoxLayout, QTreeWidget, @@ -45,7 +46,7 @@ class Dialog(QWidget): try: self.cache = apt_pkg.Cache() except SystemError as e: - sys.stderr.write("Error: Opening the cache (%s)" % e) + sys.stderr.write(_("Error: Opening the cache (%s)") % e) sys.exit(-1) self.depcache = apt_pkg.DepCache(self.cache) @@ -60,7 +61,7 @@ class Dialog(QWidget): self.tw = QTreeWidget() self.tw.setColumnCount(1) - self.tw.setHeaderLabels(['Affected Packages']) + self.tw.setHeaderLabels([_('Affected Packages')]) self.tw.setHeaderHidden(True) self.upgradeBtn = QPushButton("Upgrade") @@ -98,26 +99,28 @@ class Dialog(QWidget): pkg_install.append(p.name) elif self.depcache.marked_upgrade(p): pkg_upgrade.append(p.name) - text = "There are upgrades available. Do you want to do a system" - text += " upgrade?\nThis will mean packages could be upgraded," - text += " installed, or removed." + text = _("There are upgrades available. Do you want to do a system " + "upgrade?") + text += "\n" + text += _("This will mean packages could be upgraded, installed, or " + "removed.") if len(pkg_delete) > 0: - toDelete = QTreeWidgetItem(['Remove']) + toDelete = QTreeWidgetItem([_('Remove')]) for p in pkg_delete: td_child = QTreeWidgetItem([p]) toDelete.addChild(td_child) toDelete.setIcon(0, QIcon.fromTheme("edit-delete")) self.tw.addTopLevelItem(toDelete) if len(pkg_install) > 0: - toInstall = QTreeWidgetItem(['Install']) + toInstall = QTreeWidgetItem([_('Install')]) for p in pkg_install: td_child = QTreeWidgetItem([p]) toInstall.addChild(td_child) toInstall.setIcon(0, QIcon.fromTheme("system-software-install")) self.tw.addTopLevelItem(toInstall) if len(pkg_upgrade) > 0: - toUpgrade = QTreeWidgetItem(['Upgrade']) + toUpgrade = QTreeWidgetItem([_('Upgrade')]) for p in pkg_upgrade: td_child = QTreeWidgetItem([p]) toUpgrade.addChild(td_child) @@ -126,10 +129,11 @@ class Dialog(QWidget): if self.reboot_required: if text == "": - text = "Reboot is needed" + text = _("Reboot required") self.upgradeBtn.setVisible(False) else: - text = text + "\nReboot is needed" + text += "\n" + text += _("Reboot required") self.label.setText(text) @@ -148,7 +152,7 @@ class Dialog(QWidget): def call_upgrade(self): ''' starts upgrade process ''' - self.label.setText("Upgrading...") + self.label.setText(_("Upgrading...")) # TODO maybe open another thread so notifier won't freeze if self.upg_path == "terminal": # cmd = ['qterminal', '-e', 'sudo', 'apt', 'dist-upgrade'] @@ -163,11 +167,11 @@ class Dialog(QWidget): process.wait() if self.upg_path == "terminal": - text = "Upgrade finished" + text = _("Upgrade finished") reboot_required_path = Path("/var/run/reboot-required") if reboot_required_path.exists(): - text = text + "\n" + "Restart required" + text += "\n" + _("Reboot required") self.label.setText(text) self.closeBtn.setVisible(True) self.closeBtn.setEnabled(True) @@ -195,21 +199,27 @@ def main(args, upgrades, security_upgrades, reboot_required, upg_path): if __name__ == "__main__": + localesApp ="lubuntu-update-notifier" + localesDir ="/usr/share/locale" + gettext.bindtextdomain(localesApp, localesDir) + gettext.textdomain(localesApp) + _ = gettext.gettext + parser = ArgumentParser() parser.add_argument("-p", "--upgrader-sw", dest="upg_path", - help="Define software/app to open for upgrade", + help=_("Define software/app to open for upgrade"), metavar="APP") parser.add_argument("-u", "--upgrades", dest="upgrades", - help="How many upgrades are available", + help=_("How many upgrades are available"), metavar="APP") parser.add_argument("-s", "--security-upg", dest="security_upgrades", - help="How many security upgrades are available", + help=_("How many security upgrades are available"), metavar="APP") options = parser.parse_args() diff --git a/lubuntu-upgrader b/lubuntu-upgrader index c9dbacb..28e0172 100755 --- a/lubuntu-upgrader +++ b/lubuntu-upgrader @@ -22,7 +22,7 @@ import sys import os from pathlib import Path -# from optparse import OptionParser +import gettext from argparse import ArgumentParser from PyQt5.QtWidgets import (QWidget, QApplication, QLabel, QPushButton, QHBoxLayout, QVBoxLayout, QProgressBar, @@ -126,7 +126,7 @@ class DialogUpg(QWidget): '''upgrade progressbar during update''' self.progressBar.setVisible(True) self.progressBar.setValue(progress) - self.label.setText("Updating cache...") + self.label.setText(_("Updating cache...")) def update_progress_download(self, transaction, uri, status, short_desc, total_size, current_size, msg): @@ -183,11 +183,11 @@ class DialogUpg(QWidget): '''print update detail info''' if total_items > 0: self.plainTextEdit.setVisible(True) - if self.detailText != "Fetching " + str( - current_items) + " of " + str(total_items): + if self.detailText != _("Fetching") + str( + current_items) + " " + _("of") + " " + str(total_items): self.detailText = ( - "Fetching " + str(current_items) + " of " - + str(total_items) + _("Fetching") + " " + str(current_items) + " " + _("of") + + " " + str(total_items) ) self.label.setText(self.detailText + "\n" + self.downloadText) @@ -196,11 +196,11 @@ class DialogUpg(QWidget): '''print upgrade detail info''' if total_items > 0: self.plainTextEdit.setVisible(True) - if self.detailText != "Downloaded " + str( - current_items) + " of " + str(total_items): + if self.detailText != _("Downloaded") + " " + str( + current_items) + " " + _("of") + " " + str(total_items): self.detailText = ( - "Downloaded " + str(current_items) + " of " - + str(total_items) + _("Downloaded") + " " + str(current_items) + " " + + _("of") + " " + str(total_items) ) self.label.setText(self.detailText + "\n" + self.downloadText) @@ -211,16 +211,16 @@ class DialogUpg(QWidget): error_desc = get_error_description_from_enum( transaction.error.code) - text = "Upgrade finished" + text = _("Upgrade finished") reboot_required_path = Path("/var/run/reboot-required") if reboot_required_path.exists(): - text = text + "\n" + "Restart required" + text = text + "\n" + _("Reboot required") self.progressBar.setVisible(False) if len(self.errors) > 0: - text = text + "\n With some Errors" - self.plainTextEdit.appendPlainText("Error Resume:\n") + text += "\n" + _("With some Errors") + self.plainTextEdit.appendPlainText(_("Error Resume:") + "\n") for error in self.errors: self.plainTextEdit.setEnabled(False) self.plainTextEdit.insertPlainText(error + "\n") @@ -271,7 +271,7 @@ class DialogUpg(QWidget): def update_finish(self, transaction, exit_state): '''when update finish''' - self.label.setText("Update Cache Finished") + self.label.setText(_("Update Cache Finished")) if exit_state == EXIT_FAILED: error_string = get_error_string_from_enum(transaction.error.code) error_desc = get_error_description_from_enum( @@ -366,8 +366,8 @@ def main(args, options): # Check for root permissions if os.geteuid() != 0: - text = "Please run this software with administrative rights." - text += "To do so, run this program with lxqt-sudo." + text = _("Please run this software with administrative rights." + "To do so, run this program with lxqt-sudo.") title = "Need administrative powers" QMessageBox.critical(None, title, text) sys.exit() @@ -376,18 +376,24 @@ def main(args, options): if __name__ == "__main__": + localesApp ="lubuntu-update-notifier" + localesDir ="/usr/share/locale" + gettext.bindtextdomain(localesApp, localesDir) + gettext.textdomain(localesApp) + _ = gettext.gettext + # check arguments parser = ArgumentParser() parser.add_argument("-c", "--cache-update", action="store_true", dest="cacheUpdate", - help="Update Cache Before Upgrade") + help=_("Update Cache Before Upgrade")) parser.add_argument("-f", "--full-upgrade", action="store_true", dest="fullUpgrade", - help="Full upgrade same as dist-upgrade") + help=_("Full upgrade same as dist-upgrade")) parser.add_argument('--version', action='version', version='%(prog)s 0.1') diff --git a/po/es.po b/po/es.po new file mode 100644 index 0000000..83a58fc --- /dev/null +++ b/po/es.po @@ -0,0 +1,114 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2020-09-12 13:43-0300\n" +"PO-Revision-Date: 2020-09-12 13:49-0300\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 2.3\n" +"Last-Translator: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Language: es\n" + +#: lubuntu-upgrader:129 +msgid "Updating cache..." +msgstr "Actualizando cache..." + +#: lubuntu-upgrader:186 lubuntu-upgrader:189 +msgid "Fetching" +msgstr "Atrayendo" + +#: lubuntu-upgrader:187 lubuntu-upgrader:189 lubuntu-upgrader:200 +#: lubuntu-upgrader:203 +msgid "of" +msgstr "de" + +#: lubuntu-upgrader:199 lubuntu-upgrader:202 +msgid "Downloaded" +msgstr "Descargado" + +#: lubuntu-upgrader:214 lubuntu-notifier.py:170 +msgid "Upgrade finished" +msgstr "Actualización completada" + +#: lubuntu-upgrader:218 lubuntu-notifier.py:132 lubuntu-notifier.py:136 +#: lubuntu-notifier.py:174 +msgid "Reboot required" +msgstr "Requiere reinicio" + +#: lubuntu-upgrader:222 +msgid "With some Errors" +msgstr "Con algunos Errores" + +#: lubuntu-upgrader:223 +msgid "Error Resume:" +msgstr "Resumen de Errores:" + +#: lubuntu-upgrader:274 +msgid "Update Cache Finished" +msgstr "Actualización de Cache Terminada" + +#: lubuntu-upgrader:369 +msgid "" +"Please run this software with administrative rights.To do so, run this " +"program with lxqt-sudo." +msgstr "" +"Por favor corre este software como administrador. Para hacerlo " +"ejecútarlo con lxqt-sudo." + +#: lubuntu-upgrader:391 +msgid "Update Cache Before Upgrade" +msgstr "Actualiza el Cache antes de la Actualización" + +#: lubuntu-upgrader:396 +msgid "Full upgrade same as dist-upgrade" +msgstr "\"Full upgrade\" lo mismo que \"dist-upgrade\"" + +#: lubuntu-notifier.py:64 +msgid "Affected Packages" +msgstr "Paquetes adectados" + +#: lubuntu-notifier.py:102 +msgid "There are upgrades available. Do you want to do a system upgrade?" +msgstr "" +"Hay actualizaciones disponibles ¿Quiere hacer una actualización del sistema?" + +#: lubuntu-notifier.py:105 +msgid "This will mean packages could be upgraded, installed, or removed." +msgstr "Esto implica que paquetes pueden actualizarse, instalarse o removerse" + +#: lubuntu-notifier.py:109 +msgid "Remove" +msgstr "Quitar" + +#: lubuntu-notifier.py:116 +msgid "Install" +msgstr "Instalar" + +#: lubuntu-notifier.py:123 +msgid "Upgrade" +msgstr "Actualizar" + +#: lubuntu-notifier.py:155 +msgid "Upgrading..." +msgstr "Actualizando..." + +#: lubuntu-notifier.py:212 +msgid "Define software/app to open for upgrade" +msgstr "Define el software o app para actualizar" + +#: lubuntu-notifier.py:217 +msgid "How many upgrades are available" +msgstr "Cuántas actualizaciones hay disponibles" + +#: lubuntu-notifier.py:222 +msgid "How many security upgrades are available" +msgstr "Cuántas actualizaciones de seguridad hay disponibles" diff --git a/po/lubuntu-update-notifier.pot b/po/lubuntu-update-notifier.pot new file mode 100644 index 0000000..14eda8c --- /dev/null +++ b/po/lubuntu-update-notifier.pot @@ -0,0 +1,110 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2020-09-12 13:43-0300\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: lubuntu-upgrader:129 +msgid "Updating cache..." +msgstr "" + +#: lubuntu-upgrader:186 lubuntu-upgrader:189 +msgid "Fetching" +msgstr "" + +#: lubuntu-upgrader:187 lubuntu-upgrader:189 lubuntu-upgrader:200 +#: lubuntu-upgrader:203 +msgid "of" +msgstr "" + +#: lubuntu-upgrader:199 lubuntu-upgrader:202 +msgid "Downloaded" +msgstr "" + +#: lubuntu-upgrader:214 lubuntu-notifier.py:170 +msgid "Upgrade finished" +msgstr "" + +#: lubuntu-upgrader:218 lubuntu-notifier.py:132 lubuntu-notifier.py:136 +#: lubuntu-notifier.py:174 +msgid "Reboot required" +msgstr "" + +#: lubuntu-upgrader:222 +msgid "With some Errors" +msgstr "" + +#: lubuntu-upgrader:223 +msgid "Error Resume:" +msgstr "" + +#: lubuntu-upgrader:274 +msgid "Update Cache Finished" +msgstr "" + +#: lubuntu-upgrader:369 +msgid "" +"Please run this software with administrative rights.To do so, run this " +"program with lxqt-sudo." +msgstr "" + +#: lubuntu-upgrader:391 +msgid "Update Cache Before Upgrade" +msgstr "" + +#: lubuntu-upgrader:396 +msgid "Full upgrade same as dist-upgrade" +msgstr "" + +#: lubuntu-notifier.py:64 +msgid "Affected Packages" +msgstr "" + +#: lubuntu-notifier.py:102 +msgid "There are upgrades available. Do you want to do a system upgrade?" +msgstr "" + +#: lubuntu-notifier.py:105 +msgid "This will mean packages could be upgraded, installed, or removed." +msgstr "" + +#: lubuntu-notifier.py:109 +msgid "Remove" +msgstr "" + +#: lubuntu-notifier.py:116 +msgid "Install" +msgstr "" + +#: lubuntu-notifier.py:123 +msgid "Upgrade" +msgstr "" + +#: lubuntu-notifier.py:155 +msgid "Upgrading..." +msgstr "" + +#: lubuntu-notifier.py:212 +msgid "Define software/app to open for upgrade" +msgstr "" + +#: lubuntu-notifier.py:217 +msgid "How many upgrades are available" +msgstr "" + +#: lubuntu-notifier.py:222 +msgid "How many security upgrades are available" +msgstr "" diff --git a/setup.py b/setup.py index 6fa3523..069d247 100644 --- a/setup.py +++ b/setup.py @@ -17,13 +17,48 @@ # along with this program. If not, see . from setuptools import setup +import subprocess +import os +from distutils.command.clean import clean + + +class MyClean(clean): + def run(self): + # Execute the classic clean command + super().run() + + # Custom clean + print("Removing translations") + subprocess.run(['rm', '-rf', 'translations']) + + +def add_mo_files(data_files): + app_name = 'lubuntu-update-notifier' + mo = '' + + subprocess.run(['mkdir', 'translations/']) + for f in os.listdir('po'): + loc, ext = os.path.splitext(f) + if ext == '.po': + mo = app_name + '.mo' + subprocess.run(['mkdir', 'translations/' + loc]) + subprocess.run(['msgfmt', '-o', 'translations/' + loc + '/' + mo, + 'po/' + f], check=True) + + data_files.append(('share/locale/' + loc + '/LC_MESSAGES/', + ['translations/' + loc + '/' + mo])) + + return data_files + +data_files=[('libexec/lubuntu-update-notifier/', + ['lubuntu-upg-notifier.sh', 'lubuntu-notifier.py']), + ('share/applications', ['data/upg-apply.desktop'])] setup( name="lubuntu-update-notifier", - version="0.1", + version="0.4dev", packages=['lubuntu-update-notifier'], scripts=['lubuntu-upgrader'], - data_files=[('libexec/lubuntu-update-notifier/', - ['lubuntu-upg-notifier.sh', 'lubuntu-notifier.py']), - ('share/applications', ['data/upg-apply.desktop'])] + data_files=add_mo_files(data_files), + cmdclass={'clean': MyClean} ) diff --git a/translation_helper b/translation_helper new file mode 100644 index 0000000..52455f7 --- /dev/null +++ b/translation_helper @@ -0,0 +1,2 @@ +xgettext lubuntu-upgrader lubuntu-notifier.py --language=Python +mv messages.po po/lubuntu-update-notifier.pot