Commit 9430c901 authored by Martin Matějek's avatar Martin Matějek

Refactoring of gettext translations

parent 270b4f8f
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import gettext
import json import json
import logging import logging
import os import os
...@@ -125,6 +126,11 @@ def setup_logging(): ...@@ -125,6 +126,11 @@ def setup_logging():
logger.addHandler(handler_stderr) logger.addHandler(handler_stderr)
def setup_gettext(locale):
translation = gettext.translation("notification-system", localedir='/usr/share/locale', languages=[locale]) or gettext.NullTranslations()
_ = translation.gettext
def process_args(parser, args): def process_args(parser, args):
"""Call module interface based on args""" """Call module interface based on args"""
if args.config: if args.config:
...@@ -195,6 +201,7 @@ def process_args(parser, args): ...@@ -195,6 +201,7 @@ def process_args(parser, args):
def main(): def main():
setup_logging() setup_logging()
setup_gettext('cs_CZ')
parser = create_argparser() parser = create_argparser()
args = parser.parse_args() args = parser.parse_args()
......
import gettext import gettext
import jinja2 import jinja2
import yaml
class NotificationSkeleton: class NotificationSkeleton:
...@@ -49,56 +50,15 @@ class NotificationSkeleton: ...@@ -49,56 +50,15 @@ class NotificationSkeleton:
return None return None
def translate_actions(self, lang): def translate_actions(self, lang):
# Work in progress!
actions = {} actions = {}
for a in self.actions: for a in self.actions:
actions[a] = self._translate(self.actions[a]['title'], lang) parsed = yaml.safe_load(self.jinja_plugin_template.render())
actions[a] = parsed['actions']['title']
return actions return actions
def _translate(self, message, lang):
"""Translate single variable content"""
transl = self._get_translation(lang)
if transl:
transl.install()
translated = gettext.gettext(_(message))
# reset translation to default
transl = gettext.NullTranslations()
transl.install()
return translated
return message
def _fetch_translation(self, lang):
self.translations[lang] = gettext.translation(self.plugin_name, localedir='locale', languages=[lang])
def _get_translation(self, lang):
if lang in self.translations:
return self.translations[lang]
try:
self._fetch_translation(lang)
return self.translations[lang]
except FileNotFoundError:
return None
def _set_jinja_translation(self, lang):
"""
Set gettext translation for jinja env
Try to load translation otherwise use NullTranslations
"""
transl = self._get_translation(lang)
if transl:
self.jinja_env.install_gettext_translations(transl, newstyle=True)
else:
self.jinja_env.install_null_translations()
def init_jinja_env(self): def init_jinja_env(self):
""" """
Init jinja environment Init jinja environment
...@@ -109,14 +69,15 @@ class NotificationSkeleton: ...@@ -109,14 +69,15 @@ class NotificationSkeleton:
template_loader = jinja2.FileSystemLoader(self.template_dir) template_loader = jinja2.FileSystemLoader(self.template_dir)
self.jinja_env = jinja2.Environment( self.jinja_env = jinja2.Environment(
loader=template_loader, loader=template_loader,
autoescape=True,
extensions=['jinja2.ext.i18n'] extensions=['jinja2.ext.i18n']
) )
self.jinja_template = self.jinja_env.get_template(self.template['src']) self.jinja_message_template = self.jinja_env.get_template(self.template['src'])
self.jinja_plugin_template = self.jinja_env.get_template(self.template['plugin_src'])
def render(self, data, media_type, lang): def render(self, data, media_type, lang):
"""Render using jinja in given language""" """Render using jinja in given language"""
self._set_jinja_translation(lang) output = self.jinja_message_template.render(media=media_type, **data)
output = self.jinja_template.render(media=media_type, **data)
return output return output
......
actions: actions:
- name: reject - name: reject
title: Reject current update title: "{% trans %}Reject current update{% endtrans %}"
command: updater --reject-update command: updater --reject-update
- name: dummy - name: dummy
title: Dummy action title: "{% trans %}Dummy action{% endtrans %}"
command: /bin/true command: /bin/true
templates: templates:
- type: simple_message - type: simple_message
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment