From 58a059b94afee29767303b399b48629985a48942 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 22 Apr 2021 13:57:43 +0200 Subject: refactor base template, language selector --- talermerchantdemos/landing/landing.py | 51 +++++++---------------------------- 1 file changed, 9 insertions(+), 42 deletions(-) (limited to 'talermerchantdemos/landing') diff --git a/talermerchantdemos/landing/landing.py b/talermerchantdemos/landing/landing.py index 3c1f304..5b6376e 100644 --- a/talermerchantdemos/landing/landing.py +++ b/talermerchantdemos/landing/landing.py @@ -29,7 +29,12 @@ from flask_babel import force_locale from flask_babel import gettext import traceback from taler.util.talerconfig import TalerConfig, ConfigurationError -from ..httpcommon import backend_get, backend_post, self_localized +from ..httpcommon import ( + backend_get, + backend_post, + self_localized, + make_utility_processor, +) import sys if not sys.version_info.major == 3 and sys.version_info.minor >= 6: @@ -68,47 +73,9 @@ LOGGER.info( "Operating with the following translations available: " + " ".join(translations) ) -app.add_template_global(self_localized) - - -@babel.localeselector -def get_locale(): - parts = request.path.split("/", 2) - if 2 >= len(parts): - # Totally unexpected path format, do not localize - return "en" - lang = parts[1] - if lang in translations: - return lang - return "en" - -## -# Make the environment available into templates. -# -# @return the environment-reading function. -@app.context_processor -def utility_processor(): - - def getactive(): - return "landing" - - def getlang(): - return get_locale() - - def env(name, default=None): - return os.environ.get(name, default) - - def prettydate(talerdate): - parsed_time = re.search(r"/Date\(([0-9]+)\)/", talerdate) - if not parsed_time: - return "malformed date given" - parsed_time = int(parsed_time.group(1)) - timestamp = datetime.datetime.fromtimestamp(parsed_time) - # returns the YYYY-MM-DD date format. - return timestamp.strftime("%Y-%b-%d") - - return dict(env=env, prettydate=prettydate, getactive=getactive, getlang=getlang) - +# Add context processor that will make additional variables +# and functions available in the template. +app.context_processor(make_utility_processor("landing")) ## # Exception handler to capture all the unmanaged errors. -- cgit v1.2.3