Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
Notification system
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
17
Issues
17
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Turris
Notification system
Commits
cea73ce1
Commit
cea73ce1
authored
Aug 30, 2018
by
Martin Matějek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Gettext translations per module
parent
34ab3146
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
17 deletions
+43
-17
notifylib/notificationskeleton.py
notifylib/notificationskeleton.py
+32
-8
notifylib/pluginstorage.py
notifylib/pluginstorage.py
+1
-0
templates/simple/complex.j2
templates/simple/complex.j2
+6
-6
templates/simple/simple.j2
templates/simple/simple.j2
+4
-3
No files found.
notifylib/notificationskeleton.py
View file @
cea73ce1
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
...
...
notifylib/pluginstorage.py
View file @
cea73ce1
...
...
@@ -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
()
...
...
templates/simple/complex.j2
View file @
cea73ce1
{% 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 %}
templates/simple/simple.j2
View file @
cea73ce1
{% 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 %}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment