Skip to content

Commit

Permalink
Add inital i18n translation support
Browse files Browse the repository at this point in the history
The filter_plugin translate allows to translate strings using yavdr.mo
files from the package yavdr-i18n.

ansible-playbook must be called with an UTF-8 locale (e.g. de_DE.UTF-8), LANG=C or
LANG=POSIX are not allowed.

Usage of the new filter:

"{{ "my string" | translate }}"
  • Loading branch information
seahawk1986 committed Dec 18, 2017
1 parent 767807e commit 3051e04
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
17 changes: 6 additions & 11 deletions filter_plugins/translate_yavdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@
'supported_by': 'yavdr'
}


import gettext
from ansible.errors import AnsibleFilterError
from ansible.module_utils.six.moves.urllib.parse import urlsplit
from ansible.module_utils._text import to_text
from ansible.utils import helpers


def translate_yavdr(text, language=None, **kwargs):
if language is None:
language = kwargs.get('system_language', 'en')
translation = kwargs.get('translations', {}).get(language, {}).get(text, None)
if translation:
return translation
else:
def translate_yavdr(text):
gettext.textdomain('yavdr')
try:
return gettext.gettext(text)
except:
return text


# ---- Ansible filters ----
class FilterModule(object):
''' URI filter '''
Expand Down
3 changes: 3 additions & 0 deletions group_vars/all
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
# file: group_vars/all

# for translation filter
locale: "{{ ansible_env.LANG }}"

# this is the standard text to put in templates
ansible_managed_file: "*** YAVDR: ANSIBLE MANAGED FILE ***"
branch: experimental
Expand Down

0 comments on commit 3051e04

Please sign in to comment.