From f9664037a6424bffc6a7bb7af9dcccc07e7c3d84 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Tue, 15 Sep 2020 09:56:39 +0200 Subject: [PATCH] [mod] refactor searx.webapp.get_locale * Log each call to get_locale: display the URL, the locale and the source (browser, preferences, form). * Rename _get_browser_language to _get_browser_or_settings_language to match the actual code. --- searx/webapp.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index f9133e6fe..f2ef8b209 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -159,7 +159,7 @@ def _get_translations(): flask_babel.get_translations = _get_translations -def _get_browser_language(request, lang_list): +def _get_browser_or_settings_language(request, lang_list): for lang in request.headers.get("Accept-Language", "en").split(","): if ';' in lang: lang = lang.split(';')[0] @@ -171,26 +171,31 @@ def _get_browser_language(request, lang_list): @babel.localeselector def get_locale(): - locale = _get_browser_language(request, settings['locales'].keys()) - - logger.debug("default locale from browser info is `%s`", locale) - - if request.preferences.get_value('locale') != '': - locale = request.preferences.get_value('locale') - if 'locale' in request.form\ and request.form['locale'] in settings['locales']: + # use locale from the form locale = request.form['locale'] + locale_source = 'form' + elif request.preferences.get_value('locale') != '': + # use locale from the preferences + locale = request.preferences.get_value('locale') + locale_source = 'preferences' + else: + # use local from the browser + locale = _get_browser_or_settings_language(request, settings['locales'].keys()) + locale_source = 'browser' + # if locale == 'zh_TW': locale = 'zh_Hant_TW' + # see _get_translations function + # and https://github.com/searx/searx/pull/1863 if locale == 'oc': request.form['use-translation'] = 'oc' locale = 'fr_FR' - logger.debug("selected locale is `%s`", locale) - + logger.debug("%s uses locale `%s` from %s", request.url, locale, locale_source) return locale @@ -472,7 +477,7 @@ def pre_request(): # init search language and locale if not preferences.get_value("language"): - preferences.parse_dict({"language": _get_browser_language(request, LANGUAGE_CODES)}) + preferences.parse_dict({"language": _get_browser_or_settings_language(request, LANGUAGE_CODES)}) if not preferences.get_value("locale"): preferences.parse_dict({"locale": get_locale()})