Commit cea73ce1 authored by Martin Matějek's avatar Martin Matějek

Gettext translations per module

parent 34ab3146
import gettext
import shlex
import subprocess
......@@ -5,10 +6,11 @@ import jinja2
class NotificationSkeleton:
ATTRS = ['name', 'template', 'actions', 'template_dir', 'timeout', 'severity', 'persistent']
ATTRS = ['name', 'plugin_name', 'template', 'actions', 'template_dir', 'timeout', 'severity', 'persistent']
def __init__(self, name, template, actions, template_dir, timeout=None, severity='info', persistent=False):
def __init__(self, name, plugin_name, template, actions, template_dir, timeout=None, severity='info', persistent=False):
self.name = name
self.plugin_name = plugin_name
self.template = template
self.actions = actions
self.template_dir = template_dir
......@@ -18,6 +20,7 @@ class NotificationSkeleton:
self.persistent = persistent
self.init_jinja_env()
self.translations = {}
def get_media_types(self):
return self.template['supported_media']
......@@ -57,6 +60,25 @@ class NotificationSkeleton:
else:
print("Command exited succesfully")
def _get_translation(self, lang):
self.translations[lang] = gettext.translation(self.plugin_name, localedir='locale', languages=[lang])
def _set_translation(self, lang):
"""
Set gettext translation for jinja env
Try to load translation otherwise use NullTranslations
"""
if lang in self.translations:
self.jinja_env.install_gettext_translations(self.translations[lang], newstyle=True)
else:
try:
self._get_translation(lang)
self.jinja_env.install_gettext_translations(self.translations[lang], newstyle=True)
except FileNotFoundError:
self.jinja_env.install_null_translations()
def init_jinja_env(self):
"""
Init jinja environment
......@@ -64,14 +86,16 @@ class NotificationSkeleton:
Prepare template for later use
For now it will be initiated when creating new skeleton instance
"""
template_loader = jinja2.FileSystemLoader(searchpath=self.template_dir)
template_env = jinja2.Environment(loader=template_loader)
self.jinja_template = template_env.get_template(self.template['src'])
template_loader = jinja2.FileSystemLoader(self.template_dir)
self.jinja_env = jinja2.Environment(
loader=template_loader,
extensions=['jinja2.ext.i18n']
)
self.jinja_template = self.jinja_env.get_template(self.template['src'])
def render(self, media_type, lang, data):
"""Render using jinja"""
# TODO: render with babel/gettext
"""Render using jinja in given language"""
self._set_translation(lang)
output = self.jinja_template.render(media=media_type, **data)
return output
......
......@@ -60,6 +60,7 @@ class PluginStorage:
# TODO: refactor/simplify this code
notification_args = {}
notification_args['name'] = notification_types[skel_name]['name']
notification_args['plugin_name'] = plugin_name
skel_actions = {}
plugin_actions = self.plugins[plugin_name].get_actions()
......
{% if media == 'html' %}
<h1>Complex message</h1>
<h1>{{ _("Complex message") }}</h1>
<p>Lorem ipsum dolor sit amet</p>
<p>
Message: {{ message }}
{{ _("First message") }}: {{ message }}
<br>
Message2: {{ message2 }}
{{ _("Second message") }}: {{ message2 }}
</p>
{% else %}
[Complex message]
Message: {{message}}
Message2: {{ message2 }}
[{{ _("Complex message") }}]
{{ _("First message") }}: {{message}}
{{ _("Second message") }}: {{ message2 }}
{% endif %}
{% if media == 'html' %}
<h3>{{ _("Simple message") }}</h3>
<p>
html rendered message: {{ message }}
{{ _("HTML rendered message") }}: {{ message }}
</p>
{% else %}
= Simple message =
Message: {{message}}
= {{ _("Simple message") }} =
{{ _("Message") }}: {{message}}
{% endif %}
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