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/blog/blog.py | 17 ++- talermerchantdemos/blog/templates/base.html.j2 | 9 +- .../blog/templates/confirm_refund.html.j2 | 2 +- talermerchantdemos/donations/translations | 1 + talermerchantdemos/survey/survey.py | 60 +++++++++-- talermerchantdemos/survey/templates/base.html | 59 ----------- talermerchantdemos/survey/templates/base.html.j2 | 115 +++++++++++++++++++++ talermerchantdemos/survey/templates/error.html | 22 ---- talermerchantdemos/survey/templates/error.html.j2 | 24 +++++ talermerchantdemos/survey/templates/index.html | 26 ----- talermerchantdemos/survey/templates/index.html.j2 | 26 +++++ .../survey/templates/survey_stats.html | 15 --- talermerchantdemos/survey/translations | 1 + 13 files changed, 237 insertions(+), 140 deletions(-) create mode 120000 talermerchantdemos/donations/translations delete mode 100644 talermerchantdemos/survey/templates/base.html create mode 100644 talermerchantdemos/survey/templates/base.html.j2 delete mode 100644 talermerchantdemos/survey/templates/error.html create mode 100644 talermerchantdemos/survey/templates/error.html.j2 delete mode 100644 talermerchantdemos/survey/templates/index.html create mode 100644 talermerchantdemos/survey/templates/index.html.j2 delete mode 100644 talermerchantdemos/survey/templates/survey_stats.html create mode 120000 talermerchantdemos/survey/translations (limited to 'talermerchantdemos') diff --git a/talermerchantdemos/blog/blog.py b/talermerchantdemos/blog/blog.py index 7112b33..ba6571b 100644 --- a/talermerchantdemos/blog/blog.py +++ b/talermerchantdemos/blog/blog.py @@ -61,6 +61,7 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__)) app = flask.Flask( __name__, template_folder=BASE_DIR, static_folder=BASE_DIR + "/../static/" ) +app.debug = True app.secret_key = base64.b64encode(os.urandom(64)).decode("utf-8") LOGGER = logging.getLogger(__name__) @@ -81,7 +82,8 @@ babel = Babel(app) print("Using translations from:") print(list(babel.translation_directories)) translations = [str(translation) for translation in babel.list_translations()] -translations.append('en') +if not 'en' in translations: + translations.append('en') print("Operating with the following translations available:") print(translations) @@ -126,6 +128,19 @@ def index(): target = flask.request.accept_languages.best_match(translations, default) return flask.redirect("/" + target + "/", code=302) +## +# Serve the /favicon.ico requests. +# +# @return the favicon.ico file. +@app.route("/favicon.ico") +def favicon(): + 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", + mimetype="image/vnd.microsoft.ico" + ) + @babel.localeselector def get_locale(): parts = request.path.split('/', 2) diff --git a/talermerchantdemos/blog/templates/base.html.j2 b/talermerchantdemos/blog/templates/base.html.j2 index 58ce857..1303276 100644 --- a/talermerchantdemos/blog/templates/base.html.j2 +++ b/talermerchantdemos/blog/templates/base.html.j2 @@ -69,8 +69,8 @@
-

Taler Demo

-

Shop

+

{{ gettext("Taler Demo")}}

+

{{ gettext("Shop") }}

{{ gettext("On this page you can buy articles using an imaginary currency.") + "
" + gettext("The articles are chapters from Richard Stallman's book "Free Software, Free Society".") + "
" + @@ -82,9 +82,9 @@

-
{% block main %} diff --git a/talermerchantdemos/blog/templates/confirm_refund.html.j2 b/talermerchantdemos/blog/templates/confirm_refund.html.j2 index 09f3730..a371645 100644 --- a/talermerchantdemos/blog/templates/confirm_refund.html.j2 +++ b/talermerchantdemos/blog/templates/confirm_refund.html.j2 @@ -17,6 +17,6 @@
- +
{% endblock main %} diff --git a/talermerchantdemos/donations/translations b/talermerchantdemos/donations/translations new file mode 120000 index 0000000..0a951f7 --- /dev/null +++ b/talermerchantdemos/donations/translations @@ -0,0 +1 @@ +../../translations/ \ No newline at end of file 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")) diff --git a/talermerchantdemos/survey/templates/base.html b/talermerchantdemos/survey/templates/base.html deleted file mode 100644 index 2eeace1..0000000 --- a/talermerchantdemos/survey/templates/base.html +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - Taler Survey Demo - - - - {% block styles %}{% endblock %} - {% block scripts %}{% endblock %} - - - -
-

Taler Demo

-

Survey

-

This is the survey page, which demonatrates how merchants can reward their users by paying directly into their wallet.

- -

You can learn more about Taler on our main website.

-
- -
-

Taler Survey Demo

- {% block main %} - This is the main content of the page. - {% endblock %} -
- -
- - diff --git a/talermerchantdemos/survey/templates/base.html.j2 b/talermerchantdemos/survey/templates/base.html.j2 new file mode 100644 index 0000000..b6d1c62 --- /dev/null +++ b/talermerchantdemos/survey/templates/base.html.j2 @@ -0,0 +1,115 @@ + + + + + + {{ gettext("Taler Survey Demo") }} + + + + + + {% block styles %}{% endblock %} + {% block scripts %}{% endblock %} + + + +
+

{{ gettext("Taler Demo") }}

+

{{ gettext("Survey") }}

+

{{ + gettext("This is the Taler survey demonstration.") + "
" + + gettext("It demonatrates how merchants can reward their users by granting tips.") + "
" + + gettext("Tipping is a way for offer cash rewards that go directly into a user's wallet.") + }} +

+
+ + +
+ {% block main %} + This is the main content of the page. + {% endblock %} +
+
+

{{ gettext('You can learn more about Taler on our main website.').format(site="https://taler.net/") }}

+
+

Copyright © 2014—2020 Taler Systems SA

+
+
+ + diff --git a/talermerchantdemos/survey/templates/error.html b/talermerchantdemos/survey/templates/error.html deleted file mode 100644 index 0d4bd02..0000000 --- a/talermerchantdemos/survey/templates/error.html +++ /dev/null @@ -1,22 +0,0 @@ -{% extends "templates/base.html" %} -{% block main %} -

An Error Occurred

- -

{{ message }}

- - {% if status_code %} -

The backend returned status code {{ status_code }}.

- {% endif %} - - {% if json %} -

Backend Response:

-
{{ json }}
- {% endif %} - - {% if stack %} -

Stack trace:

-
-    {{ stack }}
-  
- {% endif %} -{% endblock main %} diff --git a/talermerchantdemos/survey/templates/error.html.j2 b/talermerchantdemos/survey/templates/error.html.j2 new file mode 100644 index 0000000..ffc2e1f --- /dev/null +++ b/talermerchantdemos/survey/templates/error.html.j2 @@ -0,0 +1,24 @@ +{% extends "templates/base.html.j2" %} +{% block main %} +

{{ gettext("Error encountered") }}

+ +

{{ message }}

+ + {% if status_code %} +

+ {{ gettext ("The backend returned status code {code}.").format(code=status_code) }}. +

+ {% endif %} + + {% if json %} +

{{gettext("Backend response:")}}

+
{{ json }}
+ {% endif %} + + {% if stack %} +

{{gettext("Stack trace:")}}

+
+    {{ stack }}
+  
+ {% endif %} +{% endblock main %} diff --git a/talermerchantdemos/survey/templates/index.html b/talermerchantdemos/survey/templates/index.html deleted file mode 100644 index f40c235..0000000 --- a/talermerchantdemos/survey/templates/index.html +++ /dev/null @@ -1,26 +0,0 @@ -{% extends "templates/base.html" %} - -{% block main %} -
-

- In this page, you can participate in our survey about payment systems - and get a nice tip - via your Taler wallet - from this shop! -

-
-
-
- What do you prefer? -
- - -
- -
-
-{% endblock %} diff --git a/talermerchantdemos/survey/templates/index.html.j2 b/talermerchantdemos/survey/templates/index.html.j2 new file mode 100644 index 0000000..9c9df5b --- /dev/null +++ b/talermerchantdemos/survey/templates/index.html.j2 @@ -0,0 +1,26 @@ +{% extends "templates/base.html.j2" %} +{% block main %} +
+

+ {{ + gettext("On this page, you can participate in our survey about payment systems and receive a tip in return.") + }} +

+
+
+
+ {{ gettext("Which payment system do you prefer?") }} +
+ + +
+ +
+
+{% endblock %} diff --git a/talermerchantdemos/survey/templates/survey_stats.html b/talermerchantdemos/survey/templates/survey_stats.html deleted file mode 100644 index 41cb437..0000000 --- a/talermerchantdemos/survey/templates/survey_stats.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends "templates/base.html" %} - -{% block main %} -
-

This page shows information about the tipping configuration of the - merchant. Usually this should not be visible to users.

-
    -
  • Reserve pub: {{ stats['reserve_pub'] }}
  • -
  • Reserve expiration: {{ prettydate(stats['reserve_expiration']) }}
  • -
  • Amount available {{ stats['amount_available'] }}
  • -
  • Amount picked up {{ stats['amount_picked_up'] }}
  • -
  • Amount authorized {{ stats['amount_authorized'] }}
  • -
      -
-{% endblock %} diff --git a/talermerchantdemos/survey/translations b/talermerchantdemos/survey/translations new file mode 120000 index 0000000..0a951f7 --- /dev/null +++ b/talermerchantdemos/survey/translations @@ -0,0 +1 @@ +../../translations/ \ No newline at end of file -- cgit v1.2.3