aboutsummaryrefslogtreecommitdiff
path: root/ihatemoney/utils.py
diff options
context:
space:
mode:
authorzorun <github@bitsofnetworks.org>2020-04-26 23:12:33 +0200
committerGitHub <noreply@github.com>2020-04-26 23:12:33 +0200
commit08bb95422bd43ce729992673b27117088f5454d9 (patch)
treef1167b18e25a779a5f5acdca362804bcd2969f0a /ihatemoney/utils.py
parent342292ca9f080d2600edf07f28d1798ebe814559 (diff)
downloadihatemoney-mirror-08bb95422bd43ce729992673b27117088f5454d9.zip
ihatemoney-mirror-08bb95422bd43ce729992673b27117088f5454d9.tar.gz
ihatemoney-mirror-08bb95422bd43ce729992673b27117088f5454d9.tar.bz2
Fix crash when a localized email template is missing (#592)
Diffstat (limited to 'ihatemoney/utils.py')
-rw-r--r--ihatemoney/utils.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py
index c8daa56..7fdad61 100644
--- a/ihatemoney/utils.py
+++ b/ihatemoney/utils.py
@@ -9,7 +9,8 @@ import os
import re
from babel import Locale
-from flask import current_app, redirect
+from flask import current_app, redirect, render_template
+from flask_babel import get_locale
import jinja2
from werkzeug.routing import HTTPException, RoutingException
@@ -278,3 +279,17 @@ class FormEnum(Enum):
def __str__(self):
return str(self.value)
+
+
+def render_localized_template(template_name_prefix, **context):
+ """Like render_template(), but selects the right template according to the
+ current user language. Fallback to English if a template for the
+ current language does not exist.
+ """
+ fallback = "en"
+ templates = [
+ f"{template_name_prefix}.{lang}.j2"
+ for lang in (get_locale().language, fallback)
+ ]
+ # render_template() supports a list of templates to try in order
+ return render_template(templates, **context)