Remove f-string based translations

F-strings are a bad idea for translations, because they cause Babel to
crash when collecting strings to translate:
https://github.com/python-babel/babel/issues/715

But even if we replaced f-strings with new-style string interpolation such
as `_("{foo}").format(foo=foo)`, it's still a bad idea, because a wrong
translation can crash Ihatemoney at runtime with a KeyError.

Instead, we must really use old-style python formatting since they are
well supported in Babel.  Wrong translations that mess with string
interpolations will cause Babel to give an error when compiling
translation files, which is exactly what we want.
This commit is contained in:
Baptiste Jonglez 2023-01-31 16:06:27 +01:00 committed by zorun
parent 081f8dcf49
commit 72f252b9f9
2 changed files with 27 additions and 12 deletions

View file

@ -64,15 +64,19 @@ def flash_email_error(error_message, category="danger"):
(admin_name, admin_email) = email.utils.parseaddr(
current_app.config.get("MAIL_DEFAULT_SENDER")
)
error_extension = "."
error_extension = _("Please check the email configuration of the server.")
if admin_email != "admin@example.com" and current_app.config.get(
"SHOW_ADMIN_EMAIL"
):
error_extension = f" or contact the administrator at {admin_email}."
error_extension = _(
"Please check the email configuration of the server "
"or contact the administrator: %(admin_email)s",
admin_email=admin_email,
)
flash(
_(
f"{error_message} Please check the email configuration of the server{error_extension}"
"{error_message} {error_extension}".format(
error_message=error_message, error_extension=error_extension
),
category=category,
)

View file

@ -336,8 +336,10 @@ def create_project():
# Display the error as a simple "info" alert, because it's
# not critical and doesn't prevent using the project.
flash_email_error(
"We tried to send you an reminder email, but there was an error. "
"You can still use the project normally.",
_(
"We tried to send you an reminder email, but there was an error. "
"You can still use the project normally."
),
category="info",
)
return redirect(url_for(".list_bills", project_id=project.id))
@ -363,8 +365,10 @@ def remind_password():
return redirect(url_for(".password_reminder_sent"))
else:
flash_email_error(
"Sorry, there was an error while sending you an email with "
"password reset instructions."
_(
"Sorry, there was an error while sending you an email with "
"password reset instructions."
)
)
# Fall-through: we stay on the same page and display the form again
return render_template("password_reminder.html", form=form)
@ -469,7 +473,9 @@ def import_project():
b["currency"] = g.project.default_currency
for a in attr:
if a not in b:
raise ValueError(_("Missing attribute {}").format(a))
raise ValueError(
_("Missing attribute: %(attribute)s", attribute=a)
)
currencies.add(b["currency"])
# Additional checks if project has no default currency
@ -586,7 +592,7 @@ def invite():
# send the email
message_body = render_localized_template("invitation_mail")
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,
)
msg = Message(
@ -600,7 +606,9 @@ def invite():
return redirect(url_for(".list_bills"))
else:
flash_email_error(
"Sorry, there was an error while trying to send the invitation emails."
_(
"Sorry, there was an error while trying to send the invitation emails."
)
)
# Fall-through: we stay on the same page and display the form again
@ -797,7 +805,10 @@ def change_lang(lang):
session["lang"] = lang
session.update()
else:
flash(_(f"{lang} is not a supported language"), category="warning")
flash(
_("%(lang)s is not a supported language", lang=lang),
category="warning",
)
return redirect(request.headers.get("Referer") or url_for(".home"))