emails can now be translated instead of having an individual template for each language

This commit is contained in:
Andy Friedman 2023-10-19 13:14:28 -04:00
parent eb7338c76c
commit 8e476d4986
5 changed files with 14 additions and 1 deletions

2
.gitignore vendored
View file

@ -18,3 +18,5 @@ ihatemoney/budget.db
.python-version .python-version
.coverage* .coverage*
prof prof
*.ipynb
*/.ipynb_checkpoints/

View file

@ -1,3 +1,5 @@
Hi, Hi,
Someone using the email address {{ g.project.contact_email }} invited you to share your expenses for "{{ g.project.name }}". Someone using the email address {{ g.project.contact_email }} invited you to share your expenses for "{{ g.project.name }}".

View file

@ -0,0 +1,3 @@
{% trans %}
{{ untranslated_text }}
{% endtrans %}

View file

@ -430,6 +430,10 @@ def render_localized_template(template_name_prefix, **context):
# render_template() supports a list of templates to try in order # render_template() supports a list of templates to try in order
return render_template(templates, **context) 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): def format_form_errors(form, prefix):
"""Format all form errors into a single string, with a string prefix in """Format all form errors into a single string, with a string prefix in

View file

@ -68,6 +68,7 @@ from ihatemoney.utils import (
list_of_dicts2csv, list_of_dicts2csv,
list_of_dicts2json, list_of_dicts2json,
render_localized_template, render_localized_template,
translate_template_text,
send_email, send_email,
) )
@ -601,7 +602,8 @@ def invite():
if request.method == "POST": if request.method == "POST":
if form.validate(): if form.validate():
# send the email # 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 = _( message_title = _(
"You have been invited to share your expenses for %(project)s", "You have been invited to share your expenses for %(project)s",
project=g.project.name, project=g.project.name,