Skip to content

Commit

Permalink
fix: translate invalid login message on demand.
Browse files Browse the repository at this point in the history
Co-Authored-by: Pascal Repond <[email protected]>
  • Loading branch information
PascalRepond committed Dec 22, 2022
1 parent e9e8e9c commit 706a153
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions rero_ils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

0 comments on commit 706a153

Please sign in to comment.