Skip to content

Commit

Permalink
Remove f-string based translations
Browse files Browse the repository at this point in the history
F-strings are a bad idea for translations, because they cause Babel to
crash when collecting strings to translate:
python-babel/babel#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.
  • Loading branch information
Baptiste Jonglez authored and zorun committed Feb 3, 2023
1 parent 4a0e33e commit 1b9535b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
12 changes: 8 additions & 4 deletions ihatemoney/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "[email protected]" 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,
)
Expand Down
27 changes: 19 additions & 8 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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"))

Expand Down

0 comments on commit 1b9535b

Please sign in to comment.