searx.locales: improve support for languages not supported by babel

* refactor get_translations() to rely on ADDITIONAL_TRANSLATIONS and LOCALE_BEST_MATCH
* update RTL_LOCALES for languages in ADDITIONAL_TRANSLATIONS
This commit is contained in:
Alexandre FLAMENT 2022-11-04 16:47:02 +00:00
parent 7438e48d13
commit 20d951fc5c
1 changed files with 10 additions and 9 deletions

View File

@ -80,15 +80,11 @@ def localeselector():
def get_translations(): def get_translations():
"""Monkey patch of :py:obj:`flask_babel.get_translations`""" """Monkey patch of :py:obj:`flask_babel.get_translations`"""
if has_request_context() and flask.request.form.get('use-translation') == 'oc': if has_request_context():
babel_ext = flask_babel.current_app.extensions['babel'] use_translation = flask.request.form.get('use-translation')
return Translations.load(next(babel_ext.translation_directories), 'oc') if use_translation in ADDITIONAL_TRANSLATIONS:
if has_request_context() and flask.request.form.get('use-translation') == 'szl': babel_ext = flask_babel.current_app.extensions['babel']
babel_ext = flask_babel.current_app.extensions['babel'] return Translations.load(next(babel_ext.translation_directories), use_translation)
return Translations.load(next(babel_ext.translation_directories), 'szl')
if has_request_context() and flask.request.form.get('use-translation') == 'pap':
babel_ext = flask_babel.current_app.extensions['babel']
return Translations.load(next(babel_ext.translation_directories), 'pap')
return _flask_babel_get_translations() return _flask_babel_get_translations()
@ -136,13 +132,18 @@ def locales_initialize(directory=None):
flask_babel.get_translations = get_translations flask_babel.get_translations = get_translations
for tag, descr in ADDITIONAL_TRANSLATIONS.items(): for tag, descr in ADDITIONAL_TRANSLATIONS.items():
locale = Locale.parse(LOCALE_BEST_MATCH[tag], sep='-')
LOCALE_NAMES[tag] = descr LOCALE_NAMES[tag] = descr
if locale.text_direction == 'rtl':
RTL_LOCALES.add(tag)
for tag in LOCALE_BEST_MATCH: for tag in LOCALE_BEST_MATCH:
descr = LOCALE_NAMES.get(tag) descr = LOCALE_NAMES.get(tag)
if not descr: if not descr:
locale = Locale.parse(tag, sep='-') locale = Locale.parse(tag, sep='-')
LOCALE_NAMES[tag] = get_locale_descr(locale, tag.replace('-', '_')) LOCALE_NAMES[tag] = get_locale_descr(locale, tag.replace('-', '_'))
if locale.text_direction == 'rtl':
RTL_LOCALES.add(tag)
for dirname in sorted(os.listdir(directory)): for dirname in sorted(os.listdir(directory)):
# Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations # Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations