From 0ed840d92ef519790fa48c3d5941260e88093e77 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 7 Jul 2022 13:20:44 +0200 Subject: add text to the home page --- fietsboek/locale/de/html/home.html | 11 +++++++++++ fietsboek/locale/en/html/home.html | 11 +++++++++++ fietsboek/templates/home.jinja2 | 2 ++ fietsboek/util.py | 29 +++++++++++++++++++++++++++++ fietsboek/views/default.py | 6 +++++- 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 fietsboek/locale/de/html/home.html create mode 100644 fietsboek/locale/en/html/home.html diff --git a/fietsboek/locale/de/html/home.html b/fietsboek/locale/de/html/home.html new file mode 100644 index 0000000..9e866e6 --- /dev/null +++ b/fietsboek/locale/de/html/home.html @@ -0,0 +1,11 @@ +

Willkommen bei Fietsboek!

+

+ Hier kannst Du … +

+

diff --git a/fietsboek/locale/en/html/home.html b/fietsboek/locale/en/html/home.html new file mode 100644 index 0000000..bac0391 --- /dev/null +++ b/fietsboek/locale/en/html/home.html @@ -0,0 +1,11 @@ +

Welcome to Fietsboek!

+

+ Here you can … +

+

diff --git a/fietsboek/templates/home.jinja2 b/fietsboek/templates/home.jinja2 index 5c7e0c8..6825c62 100644 --- a/fietsboek/templates/home.jinja2 +++ b/fietsboek/templates/home.jinja2 @@ -22,6 +22,8 @@

{{ _("page.home.total") }}: {{ (summary.total_length / 1000) | round(2) | format_decimal }} km

+ {% elif home_content %} + {{ home_content | safe }} {% endif %} {% endblock %} diff --git a/fietsboek/util.py b/fietsboek/util.py index 368cfcd..e24f96a 100644 --- a/fietsboek/util.py +++ b/fietsboek/util.py @@ -2,6 +2,7 @@ import random import string import datetime +import importlib.resources import babel import markdown @@ -260,3 +261,31 @@ def check_password_constraints(password, repeat_password=None): raise ValueError(_("password_constraint.mismatch")) if len(password) < 8: raise ValueError(_("password_constraint.length")) + + +def read_localized_resource(locale_name, path): + """Reads a localized resource. + + Localized resources are located in the ``fietsboek/locale/**`` directory. + Note that ``path`` may contain slashes, which are automatically replaced + with the right separators. + + If the resource could not be found, a placeholder string is returned instead. + + :param locale_name: Name of the locale. + :type locale_name: str + :return: The text content of the resource. + :rtype: str + """ + parts = path.split("/") + package = ".".join(parts[:-1]) + package = f"fietsboek.locale.{locale_name}.{package}" + try: + return importlib.resources.read_text(package, parts[-1]) + except (FileNotFoundError, ModuleNotFoundError, NotADirectoryError): + # Second chance: If the locale is a specific form of a more general + # locale, try the general locale as well. + if "_" in locale_name: + main_locale = locale_name.split("_", 1)[0] + return read_localized_resource(main_locale, path) + return f"{locale_name}:{path}" diff --git a/fietsboek/views/default.py b/fietsboek/views/default.py index 913266d..042e48c 100644 --- a/fietsboek/views/default.py +++ b/fietsboek/views/default.py @@ -22,7 +22,11 @@ def home(request): :rtype: pyramid.response.Response """ if not request.identity: - return {} + locale = request.localizer.locale_name + content = util.read_localized_resource(locale, "html/home.html") + return { + 'home_content': content, + } all_tracks = request.identity.all_tracks summary = summaries.Summary() -- cgit v1.2.3