From a984947846c7b83d3151fc4987ca615edcb168ef Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 11 Oct 2020 15:05:16 +0200 Subject: i18n for survey --- talermerchantdemos/survey/survey.py | 60 ++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'talermerchantdemos/survey/survey.py') diff --git a/talermerchantdemos/survey/survey.py b/talermerchantdemos/survey/survey.py index b6a5ceb..140088d 100644 --- a/talermerchantdemos/survey/survey.py +++ b/talermerchantdemos/survey/survey.py @@ -1,6 +1,6 @@ ## # This file is part of GNU TALER. -# Copyright (C) 2017 Taler Systems SA +# Copyright (C) 2017, 2020 Taler Systems SA # # TALER is free software; you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License as published by the Free Software @@ -23,9 +23,14 @@ import base64 import logging from urllib.parse import urljoin import flask +from flask import request +from flask_babel import Babel +from flask_babel import refresh +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, fallback_404 +from ..httpcommon import backend_get, backend_post, self_localized import sys if not sys.version_info.major == 3 and sys.version_info.minor >= 6: @@ -37,6 +42,8 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__)) app = flask.Flask(__name__, template_folder=BASE_DIR) app.debug = True app.secret_key = base64.b64encode(os.urandom(64)).decode('utf-8') + +LOGGER = logging.getLogger(__name__) TC = TalerConfig.from_env() try: BACKEND_URL = TC["frontends"]["backend"].value_string(required=True) @@ -47,9 +54,30 @@ except ConfigurationError as ce: exit(1) app.config.from_object(__name__) -LOGGER = logging.getLogger(__name__) +babel = Babel(app) INSTANCED_URL = urljoin(BACKEND_URL, f"instances/survey/") +print("Using translations from:") +print(list(babel.translation_directories)) +translations = [str(translation) for translation in babel.list_translations()] +if not 'en' in translations: + translations.append('en') +print("Operating with the following translations available:") +print(translations) + +app.jinja_env.globals.update(self_localized=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. # @@ -80,8 +108,8 @@ def utility_processor(): @app.errorhandler(Exception) def internal_error(e): return flask.render_template( - "templates/error.html", - message="Internal error", + "templates/error.html.j2", + message=gettext("Internal error"), stack=traceback.format_exc() ) @@ -92,7 +120,7 @@ def internal_error(e): # @return the favicon.ico file. @app.route("/favicon.ico") def favicon(): - print("will look into: " + os.path.join(app.root_path, 'static')) + LOGGER.info("will look into: " + os.path.join(app.root_path, 'static')) return flask.send_from_directory( os.path.join(app.root_path, 'static'), "favicon.ico", @@ -105,7 +133,7 @@ def favicon(): # @param abort_status_code status code to return along the response. # @param params _kw_ arguments to passed verbatim to the templating engine. def err_abort(abort_status_code, **params): - t = flask.render_template("templates/error.html", **params) + t = flask.render_template("templates/error.html.j2", **params) flask.abort(flask.make_response(t, abort_status_code)) ## @@ -127,16 +155,26 @@ def submit_survey(): return flask.redirect(backend_resp["tip_status_url"]) ## -# Serve the main index page. +# Serve the main index page, redirecting to // # # @return response object of the index page. -@app.route("/", methods=["GET"]) +@app.route("/") def index(): + default = 'en' + target = flask.request.accept_languages.best_match(translations, default) + return flask.redirect("/" + target + "/", code=302) + +## +# Serve the internationalized main index page. +# +# @return response object of the index page. +@app.route("//", methods=["GET"]) +def start(lang): return flask.render_template( - "templates/index.html", merchant_currency=CURRENCY + "templates/index.html.j2", merchant_currency=CURRENCY, lang=lang ) @app.errorhandler(404) def handler(e): return flask.render_template( - "templates/error.html", message="Page not found") + "templates/error.html.j2", message=gettext("Page not found")) -- cgit v1.2.3