From d5639f1fd0c7de2c5bfd6b81df72bc7841a7330b Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Tue, 31 Jan 2023 16:06:27 +0100 Subject: [PATCH] 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. --- ihatemoney/utils.py | 12 ++++++++---- ihatemoney/web.py | 27 +++++++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 491eaf077..554d0eecc 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -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, ) diff --git a/ihatemoney/web.py b/ihatemoney/web.py index 9417ace57..527c24448 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -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"))