From 706a1534038f55024b7a2050a1efb765530240be Mon Sep 17 00:00:00 2001 From: PascalRepond Date: Wed, 21 Dec 2022 11:13:03 +0100 Subject: [PATCH] fix: translate invalid login message on demand. Co-Authored-by: Pascal Repond --- rero_ils/config.py | 8 +++++--- rero_ils/utils.py | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/rero_ils/config.py b/rero_ils/config.py index 63a164deb8..2b2fe02430 100644 --- a/rero_ils/config.py +++ b/rero_ils/config.py @@ -133,7 +133,7 @@ wiki_edit_ui_permission, wiki_edit_view_permission from .query import and_i18n_term_filter, and_term_filter, \ exclude_terms_filter, or_terms_filter_by_criteria -from .utils import get_current_language +from .utils import TranslatedList, get_current_language def _(x): @@ -2883,8 +2883,10 @@ def _(x): # Login Configuration # =================== #: Supercharge flask_security invalid password or user message. -SECURITY_MSG_INVALID_PASSWORD = (_('INVALID_USER_OR_PASSWORD'), 'error') -SECURITY_MSG_USER_DOES_NOT_EXIST = (_('INVALID_USER_OR_PASSWORD'), 'error') +#: flask_security uses its own translation domain, so we need +#: translate the message on demand with a custom list class. +SECURITY_MSG_INVALID_PASSWORD = TranslatedList(('INVALID_USER_OR_PASSWORD', 'error')) +SECURITY_MSG_USER_DOES_NOT_EXIST = TranslatedList(('INVALID_USER_OR_PASSWORD', 'error')) #: Allow password change by users. SECURITY_CHANGEABLE = True diff --git a/rero_ils/utils.py b/rero_ils/utils.py index b2b147bfdf..d672bb9012 100644 --- a/rero_ils/utils.py +++ b/rero_ils/utils.py @@ -22,6 +22,7 @@ import iso639 from flask import current_app +from flask_babelex import gettext from flask_security.confirmable import confirm_user from invenio_accounts.ext import hash_password from invenio_accounts.models import User as BaseUser @@ -137,3 +138,11 @@ def language_mapping(lang): """ return current_app.config.get('RERO_ILS_LANGUAGE_MAPPING', {})\ .get(lang, lang) + + +class TranslatedList(list): + """Translation on demand of elements in a list.""" + + def __getitem__(self, item): + """.""" + return gettext(list.__getitem__(self, item))