diff options
author | Alexandre FLAMENT <alexandre.flament@hesge.ch> | 2022-11-04 16:47:02 +0000 |
---|---|---|
committer | Alexandre FLAMENT <alexandre.flament@hesge.ch> | 2022-11-05 21:26:07 +0000 |
commit | 20d951fc5cd258280e11871ee33d36e5227474c5 (patch) | |
tree | 8b85c35ebde160e216960d0817ba3132f215f076 /searx/locales.py | |
parent | 7438e48d139a809d704f2d088a4d06bde3657b36 (diff) | |
download | searxng-20d951fc5cd258280e11871ee33d36e5227474c5.tar.gz searxng-20d951fc5cd258280e11871ee33d36e5227474c5.zip |
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
Diffstat (limited to 'searx/locales.py')
-rw-r--r-- | searx/locales.py | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/searx/locales.py b/searx/locales.py index 620132340..2cf06796a 100644 --- a/searx/locales.py +++ b/searx/locales.py @@ -80,15 +80,11 @@ def localeselector(): def get_translations(): """Monkey patch of :py:obj:`flask_babel.get_translations`""" - if has_request_context() and flask.request.form.get('use-translation') == 'oc': - babel_ext = flask_babel.current_app.extensions['babel'] - return Translations.load(next(babel_ext.translation_directories), 'oc') - if has_request_context() and flask.request.form.get('use-translation') == 'szl': - babel_ext = flask_babel.current_app.extensions['babel'] - 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') + if has_request_context(): + use_translation = flask.request.form.get('use-translation') + if use_translation in ADDITIONAL_TRANSLATIONS: + babel_ext = flask_babel.current_app.extensions['babel'] + return Translations.load(next(babel_ext.translation_directories), use_translation) return _flask_babel_get_translations() @@ -136,13 +132,18 @@ def locales_initialize(directory=None): flask_babel.get_translations = get_translations for tag, descr in ADDITIONAL_TRANSLATIONS.items(): + locale = Locale.parse(LOCALE_BEST_MATCH[tag], sep='-') LOCALE_NAMES[tag] = descr + if locale.text_direction == 'rtl': + RTL_LOCALES.add(tag) for tag in LOCALE_BEST_MATCH: descr = LOCALE_NAMES.get(tag) if not descr: locale = Locale.parse(tag, sep='-') 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)): # Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations |