summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2020-09-15 09:56:39 +0200
committerAlexandre Flament <alex@al-f.net>2020-09-22 11:49:15 +0200
commitf9664037a6424bffc6a7bb7af9dcccc07e7c3d84 (patch)
tree52c2a551e9655a00ce310e68827c5a7cf8031251
parent2fd582d457bc5e4ed76b96b6157d3b529f62783b (diff)
downloadsearxng-f9664037a6424bffc6a7bb7af9dcccc07e7c3d84.tar.gz
searxng-f9664037a6424bffc6a7bb7af9dcccc07e7c3d84.zip
[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.
-rwxr-xr-xsearx/webapp.py27
1 files 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()})