summaryrefslogtreecommitdiff
path: root/searx/webapp.py
diff options
context:
space:
mode:
authorNicolas Gelot <nicofonk@gmail.com>2019-03-28 18:07:03 +0100
committerNicolas Gelot <nicofonk@gmail.com>2019-06-17 23:19:15 +0200
commit373a66be3046bbb7deca20f08468cb5dea44eaf9 (patch)
tree3962814873918aa7ed07dd634c44ca67f193ca9b /searx/webapp.py
parenta818d3241df2cf010086f837a504b11983c3d55f (diff)
downloadsearxng-373a66be3046bbb7deca20f08468cb5dea44eaf9.tar.gz
searxng-373a66be3046bbb7deca20f08468cb5dea44eaf9.zip
Fix locale and search language
Locale and search language was always defined with english value. This patch inits the locale on `pre_request` in order to define the default value of locale and language preferences. Plus the `best_match` function provided by flask babel library did not work as expected. So the function `match_language` provided by searx is used to detect that the language from Accepted-Language header can be used in searx project.
Diffstat (limited to 'searx/webapp.py')
-rw-r--r--searx/webapp.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/searx/webapp.py b/searx/webapp.py
index c44612a44..542c2c002 100644
--- a/searx/webapp.py
+++ b/searx/webapp.py
@@ -152,7 +152,14 @@ outgoing_proxies = settings['outgoing'].get('proxies') or None
@babel.localeselector
def get_locale():
- locale = request.accept_languages.best_match(settings['locales'].keys())
+ locale = "en-US"
+
+ for lang in request.headers.get("Accept-Language", locale).split(","):
+ locale = match_language(lang, settings['locales'].keys(), fallback=None)
+ if locale is not None:
+ break
+
+ logger.debug("default locale from browser info is `%s`", locale)
if request.preferences.get_value('locale') != '':
locale = request.preferences.get_value('locale')
@@ -164,6 +171,8 @@ def get_locale():
if locale == 'zh_TW':
locale = 'zh_Hant_TW'
+ logger.debug("selected locale is `%s`", locale)
+
return locale
@@ -342,7 +351,9 @@ def render(template_name, override_theme=None, **kwargs):
if 'autocomplete' not in kwargs:
kwargs['autocomplete'] = request.preferences.get_value('autocomplete')
- if get_locale() in rtl_locales and 'rtl' not in kwargs:
+ locale = request.preferences.get_value('locale')
+
+ if locale in rtl_locales and 'rtl' not in kwargs:
kwargs['rtl'] = True
kwargs['searx_version'] = VERSION_STRING
@@ -355,7 +366,7 @@ def render(template_name, override_theme=None, **kwargs):
if 'current_language' not in kwargs:
kwargs['current_language'] = match_language(request.preferences.get_value('language'),
LANGUAGE_CODES,
- fallback=settings['search']['language'])
+ fallback=locale)
# override url_for function in templates
kwargs['url_for'] = url_for_theme
@@ -423,6 +434,13 @@ def pre_request():
logger.exception('invalid settings')
request.errors.append(gettext('Invalid settings'))
+ # init search language and locale
+ locale = get_locale()
+ if not preferences.get_value("language"):
+ preferences.parse_dict({"language": locale})
+ if not preferences.get_value("locale"):
+ preferences.parse_dict({"locale": locale})
+
# request.user_plugins
request.user_plugins = []
allowed_plugins = preferences.plugins.get_enabled()
@@ -593,7 +611,7 @@ def index():
unresponsive_engines=result_container.unresponsive_engines,
current_language=match_language(search_query.lang,
LANGUAGE_CODES,
- fallback=settings['search']['language']),
+ fallback=request.preferences.get_value("language")),
base_url=get_base_url(),
theme=get_current_theme_name(),
favicons=global_favicons[themes.index(get_current_theme_name())]
@@ -702,7 +720,7 @@ def preferences():
return render('preferences.html',
locales=settings['locales'],
- current_locale=get_locale(),
+ current_locale=request.preferences.get_value("locale"),
image_proxy=image_proxy,
engines_by_category=categories,
stats=stats,