Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Translation of notification texts #1

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ coverage.xml
.pytest_cache/

# Translations
*.mo
*.pot
#*.mo
#*.pot

# Django stuff:
*.log
Expand Down
65 changes: 65 additions & 0 deletions locales/notifiers.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2021-03-03 21:07+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"


#: pyouroboros/notifiers.py:41
msgid "Could not add notifier %s"
msgstr ""

#: pyouroboros/notifiers.py:48
msgid "Ouroboros has started"
msgstr ""

#: pyouroboros/notifiers.py:50
msgid "Host: %s"
msgstr ""

#: pyouroboros/notifiers.py:51
msgid "%Y-%m-%d %H:%M:%S"
msgstr ""

#: pyouroboros/notifiers.py:51
msgid "Time: %s"
msgstr ""

#: pyouroboros/notifiers.py:52
msgid "Next Run: %s"
msgstr ""

#: pyouroboros/notifiers.py:54
msgid "Ouroboros has updated containers!"
msgstr ""

#: pyouroboros/notifiers.py:56
msgid "Host/Socket: %s / %s"
msgstr ""

#: pyouroboros/notifiers.py:57
msgid "Containers Monitored: %d"
msgstr ""

#: pyouroboros/notifiers.py:58
msgid "Total Containers Updated: %d"
msgstr ""

#: pyouroboros/notifiers.py:59
msgid "Containers updated this pass: %d"
msgstr ""

#: pyouroboros/notifiers.py:63
msgid "{} updated from {} to {}"
msgstr ""

3 changes: 2 additions & 1 deletion pyouroboros/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Config(object):
'PROMETHEUS_PORT', 'NOTIFIERS', 'REPO_USER', 'REPO_PASS', 'CLEANUP', 'RUN_ONCE', 'CRON',
'INFLUX_URL', 'INFLUX_PORT', 'INFLUX_USERNAME', 'INFLUX_PASSWORD', 'INFLUX_DATABASE', 'INFLUX_SSL',
'INFLUX_VERIFY_SSL', 'DATA_EXPORT', 'SELF_UPDATE', 'LABEL_ENABLE', 'DOCKER_TLS', 'LABELS_ONLY',
'DRY_RUN', 'HOSTNAME', 'DOCKER_TLS_VERIFY', 'SWARM', 'SKIP_STARTUP_NOTIFICATIONS']
'DRY_RUN', 'HOSTNAME', 'DOCKER_TLS_VERIFY', 'SWARM', 'SKIP_STARTUP_NOTIFICATIONS', 'LANGUAGE']

hostname = environ.get('HOSTNAME')
interval = 300
Expand All @@ -27,6 +27,7 @@ class Config(object):
self_update = False
label_enable = False
labels_only = False
language = 'en'

repo_user = None
repo_pass = None
Expand Down
34 changes: 22 additions & 12 deletions pyouroboros/notifiers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import apprise
import gettext

from logging import getLogger
from datetime import datetime, timezone
Expand All @@ -11,6 +12,15 @@ def __init__(self, config, data_manager):
self.logger = getLogger()

self.apprise = self.build_apprise()
self._ = None

try:
language = gettext.translation('notifiers', localedir='locales', languages=self.config.language)
language.install()
self._ = language.gettext
except FileNotFoundError:
self.logger.error("Can't find the '%s' language", self.config.language)
self._ = gettext.gettext

def build_apprise(self):
asset = apprise.AppriseAsset(
Expand All @@ -28,29 +38,29 @@ def build_apprise(self):
for notifier in self.config.notifiers:
add = apprise_obj.add(notifier)
if not add:
self.logger.error('Could not add notifier %s', notifier)
self.logger.error(_('Could not add notifier %s'), notifier)

return apprise_obj

def send(self, container_tuples=None, socket=None, kind='update', next_run=None, mode='container'):
if kind == 'startup':
now = datetime.now(timezone.utc).astimezone()
title = f'Ouroboros has started'
now = datetime.now(timezone.utc).astimezone()
title = _('Ouroboros has started')
body_fields = [
f'Host: {self.config.hostname}',
f'Time: {now.strftime("%Y-%m-%d %H:%M:%S")}',
f'Next Run: {next_run}']
_('Host: %s') % self.config.hostname,
_('Time: %s') % now.strftime(_("%Y-%m-%d %H:%M:%S")),
_('Next Run: %s') % next_run]
else:
title = 'Ouroboros has updated containers!'
title = _('Ouroboros has updated containers!')
body_fields = [
f"Host/Socket: {self.config.hostname} / {socket.split('//')[1]}",
f"Containers Monitored: {self.data_manager.monitored_containers[socket]}",
f"Total Containers Updated: {self.data_manager.total_updated[socket]}",
f"Containers updated this pass: {len(container_tuples)}"
_('Host/Socket: %s / %s') % (self.config.hostname, socket.split('//')[1]),
_('Containers Monitored: %d') % self.data_manager.monitored_containers[socket],
_('Total Containers Updated: %d') % self.data_manager.total_updated[socket],
_('Containers updated this pass: %d') % len(container_tuples)
]
body_fields.extend(
[
"{} updated from {} to {}".format(
_("{} updated from {} to {}").format(
container.name,
old_image if mode == 'service' else old_image.short_id.split(':')[1],
new_image.short_id.split(':')[1]
Expand Down
3 changes: 3 additions & 0 deletions pyouroboros/ouroboros.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def main():
'EXAMPLE: -N discord://1234123412341234/jasdfasdfasdfasddfasdf '
'mailto://user:pass@gmail.com')

core_group.add_argument('-la', '--language', nargs='+', default=Config.language, dest='LANGUAGE',
help='Set the language of the translation\nDEFAULT: en')

docker_group = parser.add_argument_group("Docker", "Configuration of docker functionality")
docker_group.add_argument('-m', '--monitor', nargs='+', default=Config.monitor, dest='MONITOR',
help='Which container(s) to monitor\n'
Expand Down