From ebf3a7fd302ed8663626aefc7034858f0b75d30e Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 12 Feb 2019 18:12:06 +0100 Subject: [PATCH] Some changhes because of i18n --- lib/webhooks/base_app.py | 28 ++++++++++++++++++++++++++-- lib/webhooks/xlate.py | 7 ++++++- requirements.txt | 1 + update-env.sh | 6 ++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index bd3cf5d..71e5c61 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -33,6 +33,8 @@ import six import yaml import pytz +from webob.acceptparse import create_accept_language_header + # Own modules from fb_tools.common import pp, to_bytes, to_bool from fb_tools.colored import ColoredFormatter @@ -49,7 +51,7 @@ from .module_list import ModuleInfoDict from .xlate import __module_dir__ as __xlate_module_dir__ from .xlate import __base_dir__ as __xlate_base_dir__ from .xlate import __mo_file__ as __xlate_mo_file__ -from .xlate import XLATOR, LOCALE_DIR, DOMAIN +from .xlate import XLATOR, LOCALE_DIR, DOMAIN, SUPPORTED_LANGS __version__ = __pkg_version__ @@ -148,10 +150,13 @@ class BaseHookApp(BaseApplication): re_selbstlaut = re.compile(r'^[ieaouy]', re.IGNORECASE) + default_charset = 'utf-8' + default_language = 'en_US' + open_args = {} if six.PY3: open_args = { - 'encoding': 'utf-8', + 'encoding': default_charset, 'errors': 'surrogateescape', } @@ -246,6 +251,8 @@ class BaseHookApp(BaseApplication): self.search_curl_bin() + self.init_locales() + self.handler = BaseHandler( appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, quiet=self.quiet, simulate=self.simulate) @@ -255,6 +262,20 @@ class BaseHookApp(BaseApplication): return + # ------------------------------------------------------------------------- + def init_locales(self): + + accept_langs = os.getenv('HTTP_ACCEPT_LANGUAGE', None) + accept = create_accept_language_header(accept_langs) + lang = self.default_language + '.' + self.default_charset + + if accept: + match = accept.best_match(SUPPORTED_LANGS) + if match: + lang = match + '.' + self.default_charset + LOG.debug("Setting 'LC_ALL' to {!r}.".format(lang)) + os.putenv('LC_ALL', lang) + # ------------------------------------------------------------------------- def init_arg_parser(self): """ @@ -536,6 +557,8 @@ class BaseHookApp(BaseApplication): res['mail_subject'] = self.mail_subject res['mail_headline'] = self.mail_headline res['sort_by_name'] = self.sort_by_name + res['default_charset'] = self.default_charset + res['default_language'] = self.default_language if 'xlate' not in res: res['xlate'] = {} @@ -544,6 +567,7 @@ class BaseHookApp(BaseApplication): '__base_dir__': __xlate_base_dir__, 'LOCALE_DIR': LOCALE_DIR, 'DOMAIN': DOMAIN, + 'SUPPORTED_LANGS': SUPPORTED_LANGS, '__mo_file__': __xlate_mo_file__, } diff --git a/lib/webhooks/xlate.py b/lib/webhooks/xlate.py index d0473c6..babbda2 100644 --- a/lib/webhooks/xlate.py +++ b/lib/webhooks/xlate.py @@ -23,7 +23,7 @@ DOMAIN = 'puppetmaster_webhooks' LOG = logging.getLogger(__name__) -__version__ = '1.0.1' +__version__ = '1.0.2' __me__ = Path(__file__).resolve() __module_dir__ = __me__.parent @@ -45,6 +45,11 @@ if __mo_file__: else: XLATOR = gettext.NullTranslations() +SUPPORTED_LANGS = ( + 'de_DE', + 'en_US' +) + _ = XLATOR.gettext # ============================================================================= diff --git a/requirements.txt b/requirements.txt index a0cfdad..a0e3e1e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ docker-py pathlib git+https://git.pixelpark.com/frabrehm/python_fb_tools.git@master Babel +webob diff --git a/update-env.sh b/update-env.sh index 5de1efe..3acf7b1 100755 --- a/update-env.sh +++ b/update-env.sh @@ -52,6 +52,12 @@ echo pip install --upgrade --upgrade-strategy eager pip echo +echo "---------------------------------------------------" +echo "Upgrading setuptools + wheel + six ..." +echo +pip install --upgrade --upgrade-strategy eager setuptools wheel six +echo + echo "---------------------------------------------------" echo "Installing and/or upgrading necessary modules ..." echo -- 2.39.5