summaryrefslogtreecommitdiff
path: root/talermerchantdemos
diff options
context:
space:
mode:
Diffstat (limited to 'talermerchantdemos')
-rw-r--r--talermerchantdemos/blog/blog.py6
-rw-r--r--talermerchantdemos/httpcommon/__init__.py28
-rw-r--r--talermerchantdemos/survey/survey.py32
-rw-r--r--talermerchantdemos/survey/templates/base.html.j24
-rw-r--r--talermerchantdemos/survey/templates/error.html.j22
-rw-r--r--talermerchantdemos/survey/templates/index.html.j22
6 files changed, 48 insertions, 26 deletions
diff --git a/talermerchantdemos/blog/blog.py b/talermerchantdemos/blog/blog.py
index ba6571b..be9b0a2 100644
--- a/talermerchantdemos/blog/blog.py
+++ b/talermerchantdemos/blog/blog.py
@@ -79,13 +79,11 @@ BACKEND_URL = urljoin(BACKEND_BASE_URL, "instances/blog/")
app.config.from_object(__name__)
babel = Babel(app)
-print("Using translations from:")
-print(list(babel.translation_directories))
+LOGGER.info("Using translations from:" + ':'.join(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)
+LOGGER.info("Operating with the following translations available: " + ' '.join(translations))
app.jinja_env.globals.update(self_localized=self_localized)
diff --git a/talermerchantdemos/httpcommon/__init__.py b/talermerchantdemos/httpcommon/__init__.py
index d07fe69..650228d 100644
--- a/talermerchantdemos/httpcommon/__init__.py
+++ b/talermerchantdemos/httpcommon/__init__.py
@@ -4,7 +4,7 @@ from urllib.parse import urljoin
from flask import request
from datetime import datetime
import time
-
+from flask_babel import gettext
##
# Return a error response to the client.
@@ -32,19 +32,19 @@ def backend_post(backend_url, endpoint, json):
final_url, json=json, headers=headers
)
except requests.ConnectionError:
- err_abort(500, message="Could not establish connection to backend")
+ err_abort(500, message=gettext("Could not establish connection to backend"))
try:
response_json = resp.json()
except ValueError:
err_abort(
500,
- message="Could not parse response from backend",
+ message=gettext("Could not parse response from backend"),
status_code=resp.status_code
)
if resp.status_code != 200:
err_abort(
500,
- message="Backend returned error status",
+ message=gettext("Backend returned error status"),
json=response_json,
status_code=resp.status_code
)
@@ -68,21 +68,29 @@ def backend_get(backend_url, endpoint, params):
final_url, params=params, headers=headers
)
except requests.ConnectionError:
- err_abort(500, message="Could not establish connection to backend")
+ err_abort(500, message=gettext("Could not establish connection to backend"))
try:
response_json = resp.json()
except ValueError:
- err_abort(500, message="Could not parse response from backend")
+ err_abort(500, message=gettext("Could not parse response from backend"))
if resp.status_code != 200:
err_abort(
500,
- message="Backend returned error status",
+ message=gettext("Backend returned error status"),
json=response_json,
status_code=resp.status_code
)
print("Backend responds to {}: {}".format(final_url, str(response_json)))
return response_json
+def get_locale():
+ parts = request.path.split('/', 2)
+ if (2 >= len(parts)):
+ # Totally unexpected path format, do not localize
+ return "en"
+ lang = parts[1]
+ return lang
+
##
# Helper function used inside Jinja2 logic to create a links
# to the current page but in a different language. Used to
@@ -107,7 +115,11 @@ def self_localized(lang):
# @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.j2", **params)
+ t = flask.render_template(
+ "templates/error.html.j2",
+ lang=get_locale(),
+ **params
+ )
flask.abort(flask.make_response(t, abort_status_code))
diff --git a/talermerchantdemos/survey/survey.py b/talermerchantdemos/survey/survey.py
index 140088d..38065f1 100644
--- a/talermerchantdemos/survey/survey.py
+++ b/talermerchantdemos/survey/survey.py
@@ -57,13 +57,11 @@ app.config.from_object(__name__)
babel = Babel(app)
INSTANCED_URL = urljoin(BACKEND_URL, f"instances/survey/")
-print("Using translations from:")
-print(list(babel.translation_directories))
+LOGGER.info("Using translations from:" + ':'.join(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)
+LOGGER.info("Operating with the following translations available: " + ' '.join(translations))
app.jinja_env.globals.update(self_localized=self_localized)
@@ -110,10 +108,10 @@ def internal_error(e):
return flask.render_template(
"templates/error.html.j2",
message=gettext("Internal error"),
- stack=traceback.format_exc()
+ stack=traceback.format_exc(),
+ lang=get_locale()
)
-
##
# Serve the /favicon.ico requests.
#
@@ -133,7 +131,10 @@ 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.j2", **params)
+ t = flask.render_template(
+ "templates/error.html.j2",
+ lang=get_locale(),
+ **params)
flask.abort(flask.make_response(t, abort_status_code))
##
@@ -144,8 +145,8 @@ def err_abort(abort_status_code, **params):
# @return the URL where to redirect the browser, in order
# for the wallet to pick the tip up, or a error page
# otherwise.
-@app.route("/submit-survey", methods=["POST"])
-def submit_survey():
+@app.route("/<lang>/submit-survey", methods=["POST"])
+def submit_survey(lang):
tip_spec = dict(
amount=CURRENCY + ":1.0",
next_url=os.environ.get("TALER_ENV_URL_INTRO", "https://taler.net/"),
@@ -177,4 +178,15 @@ def start(lang):
@app.errorhandler(404)
def handler(e):
return flask.render_template(
- "templates/error.html.j2", message=gettext("Page not found"))
+ "templates/error.html.j2",
+ message=gettext("Page not found"),
+ lang=get_locale()
+ )
+
+@app.errorhandler(405)
+def handler(e):
+ return flask.render_template(
+ "templates/error.html.j2",
+ message=gettext("HTTP method not allowed for this page"),
+ lang=get_locale()
+ )
diff --git a/talermerchantdemos/survey/templates/base.html.j2 b/talermerchantdemos/survey/templates/base.html.j2
index b6d1c62..1beb0d5 100644
--- a/talermerchantdemos/survey/templates/base.html.j2
+++ b/talermerchantdemos/survey/templates/base.html.j2
@@ -90,10 +90,10 @@
<br>
<!--<hr style="width: 100%;">-->
{% if lang != 'en' %}
- <a href="{{ self_localized('en') }}" class="navbtn">English [en]</a><br>
+ <a href="/en/" class="navbtn">English [en]</a><br>
{% endif %}
{% if lang != 'de' %}
- <a href="{{ self_localized('de') }}" class="navbtn">Deutsch [de]</a><br>
+ <a href="/de/" class="navbtn">Deutsch [de]</a><br>
{% endif %}
</div>
</span>
diff --git a/talermerchantdemos/survey/templates/error.html.j2 b/talermerchantdemos/survey/templates/error.html.j2
index ffc2e1f..844da08 100644
--- a/talermerchantdemos/survey/templates/error.html.j2
+++ b/talermerchantdemos/survey/templates/error.html.j2
@@ -6,7 +6,7 @@
{% if status_code %}
<p>
- {{ gettext ("The backend returned status code {code}.").format(code=status_code) }}.
+ {{ gettext ("The backend returned status code {code}.").format(code=status_code) }}
</p>
{% endif %}
diff --git a/talermerchantdemos/survey/templates/index.html.j2 b/talermerchantdemos/survey/templates/index.html.j2
index 9c9df5b..3539bf8 100644
--- a/talermerchantdemos/survey/templates/index.html.j2
+++ b/talermerchantdemos/survey/templates/index.html.j2
@@ -8,7 +8,7 @@
</p>
</div>
<div>
- <form action="{{ url_for('submit_survey') }}" method="post" class="pure-form pure-form-stacked">
+ <form action="{{ "/" + lang + "/submit-survey" }}" method="post" class="pure-form pure-form-stacked">
<legend>{{ gettext("Which payment system do you prefer?") }}</legend>
<fieldset>
<label for="option-taler">