From 0ed840d92ef519790fa48c3d5941260e88093e77 Mon Sep 17 00:00:00 2001
From: Daniel Schadt Willkommen bei Fietsboek!
+ Hier kannst Du …
+
+
+
Welcome to Fietsboek!
++ Here you can … +
{{ _("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