Add inital i18n translation support

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 }}"
This commit is contained in:
Alexander Grothe 2017-12-18 17:55:53 +01:00
parent 767807e3e4
commit 3051e04c53
2 changed files with 9 additions and 11 deletions

View File

@ -12,23 +12,18 @@ ANSIBLE_METADATA = {
'supported_by': 'yavdr' 'supported_by': 'yavdr'
} }
import gettext
from ansible.errors import AnsibleFilterError 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 from ansible.utils import helpers
def translate_yavdr(text, language=None, **kwargs): def translate_yavdr(text):
if language is None: gettext.textdomain('yavdr')
language = kwargs.get('system_language', 'en') try:
translation = kwargs.get('translations', {}).get(language, {}).get(text, None) return gettext.gettext(text)
if translation: except:
return translation
else:
return text return text
# ---- Ansible filters ---- # ---- Ansible filters ----
class FilterModule(object): class FilterModule(object):
''' URI filter ''' ''' URI filter '''

View File

@ -1,6 +1,9 @@
--- ---
# file: group_vars/all # file: group_vars/all
# for translation filter
locale: "{{ ansible_env.LANG }}"
# this is the standard text to put in templates # this is the standard text to put in templates
ansible_managed_file: "*** YAVDR: ANSIBLE MANAGED FILE ***" ansible_managed_file: "*** YAVDR: ANSIBLE MANAGED FILE ***"
branch: experimental branch: experimental