From 8e476d49869db8f7d482dab2a20a8e6d3d25c4fc Mon Sep 17 00:00:00 2001 From: Andy Friedman Date: Thu, 19 Oct 2023 13:14:28 -0400 Subject: [PATCH] emails can now be translated instead of having an individual template for each language --- .gitignore | 2 ++ .../templates/{invitation_mail.en.j2 => invitation_mail.j2} | 2 ++ ihatemoney/templates/translation_template.j2 | 3 +++ ihatemoney/utils.py | 4 ++++ ihatemoney/web.py | 4 +++- 5 files changed, 14 insertions(+), 1 deletion(-) rename ihatemoney/templates/{invitation_mail.en.j2 => invitation_mail.j2} (99%) create mode 100644 ihatemoney/templates/translation_template.j2 diff --git a/.gitignore b/.gitignore index 1ec6964a..109ace2b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ ihatemoney/budget.db .python-version .coverage* prof +*.ipynb +*/.ipynb_checkpoints/ \ No newline at end of file diff --git a/ihatemoney/templates/invitation_mail.en.j2 b/ihatemoney/templates/invitation_mail.j2 similarity index 99% rename from ihatemoney/templates/invitation_mail.en.j2 rename to ihatemoney/templates/invitation_mail.j2 index bb38b9aa..c0ee55ff 100644 --- a/ihatemoney/templates/invitation_mail.en.j2 +++ b/ihatemoney/templates/invitation_mail.j2 @@ -1,3 +1,5 @@ + + Hi, Someone using the email address {{ g.project.contact_email }} invited you to share your expenses for "{{ g.project.name }}". diff --git a/ihatemoney/templates/translation_template.j2 b/ihatemoney/templates/translation_template.j2 new file mode 100644 index 00000000..55a5cc87 --- /dev/null +++ b/ihatemoney/templates/translation_template.j2 @@ -0,0 +1,3 @@ +{% trans %} +{{ untranslated_text }} +{% endtrans %} \ No newline at end of file diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 281cdf51..99a586ae 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -430,6 +430,10 @@ def render_localized_template(template_name_prefix, **context): # render_template() supports a list of templates to try in order return render_template(templates, **context) +def translate_template_text(text, **context): + """Passes `text` to a basic Jinja translation template. A generalizable solution for translating outgoing emails.""" + + return render_template("translation_template.j2", untranslated_text=text, **context) def format_form_errors(form, prefix): """Format all form errors into a single string, with a string prefix in diff --git a/ihatemoney/web.py b/ihatemoney/web.py index bbb19d3d..9221243a 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -68,6 +68,7 @@ from ihatemoney.utils import ( list_of_dicts2csv, list_of_dicts2json, render_localized_template, + translate_template_text, send_email, ) @@ -601,7 +602,8 @@ def invite(): if request.method == "POST": if form.validate(): # send the email - message_body = render_localized_template("invitation_mail") + untranslated_body = render_template("invitation_mail.j2") + message_body = translate_template_text(untranslated_body) message_title = _( "You have been invited to share your expenses for %(project)s", project=g.project.name,