Merge pull request #1621 from nfk/locale

Fix regional settings #844
This commit is contained in:
Markus Heiser 2020-02-08 14:54:22 +00:00 committed by GitHub
commit 7f224713e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 15 deletions

View file

@ -268,8 +268,8 @@ class Preferences(object):
super(Preferences, self).__init__() super(Preferences, self).__init__()
self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories + ['none']), self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories + ['none']),
'language': SearchLanguageSetting(settings['search']['language'], 'language': SearchLanguageSetting(settings['ui']['default_locale'],
choices=LANGUAGE_CODES), choices=list(LANGUAGE_CODES) + ['']),
'locale': EnumStringSetting(settings['ui']['default_locale'], 'locale': EnumStringSetting(settings['ui']['default_locale'],
choices=list(settings['locales'].keys()) + ['']), choices=list(settings['locales'].keys()) + ['']),
'autocomplete': EnumStringSetting(settings['search']['autocomplete'], 'autocomplete': EnumStringSetting(settings['search']['autocomplete'],

View file

@ -5,7 +5,6 @@ general:
search: search:
safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict
autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default
language : "en-US"
ban_time_on_fail : 5 # ban time in seconds after engine errors ban_time_on_fail : 5 # ban time in seconds after engine errors
max_ban_time_on_fail : 120 # max ban time in seconds after engine errors max_ban_time_on_fail : 120 # max ban time in seconds after engine errors

View file

@ -159,18 +159,28 @@ outgoing_proxies = settings['outgoing'].get('proxies') or None
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
if 'locale' in request.form\ locale = "en-US"
and request.form['locale'] in settings['locales']:
return request.form['locale']
if 'locale' in request.args\ for lang in request.headers.get("Accept-Language", locale).split(","):
and request.args['locale'] in settings['locales']: locale = match_language(lang, settings['locales'].keys(), fallback=None)
return request.args['locale'] if locale is not None:
break
logger.debug("default locale from browser info is `%s`", locale)
if request.preferences.get_value('locale') != '': if request.preferences.get_value('locale') != '':
return request.preferences.get_value('locale') locale = request.preferences.get_value('locale')
return request.accept_languages.best_match(settings['locales'].keys()) if 'locale' in request.form\
and request.form['locale'] in settings['locales']:
locale = request.form['locale']
if locale == 'zh_TW':
locale = 'zh_Hant_TW'
logger.debug("selected locale is `%s`", locale)
return locale
# code-highlighter # code-highlighter
@ -348,7 +358,9 @@ def render(template_name, override_theme=None, **kwargs):
if 'autocomplete' not in kwargs: if 'autocomplete' not in kwargs:
kwargs['autocomplete'] = request.preferences.get_value('autocomplete') 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['rtl'] = True
kwargs['searx_version'] = VERSION_STRING kwargs['searx_version'] = VERSION_STRING
@ -361,7 +373,7 @@ def render(template_name, override_theme=None, **kwargs):
if 'current_language' not in kwargs: if 'current_language' not in kwargs:
kwargs['current_language'] = match_language(request.preferences.get_value('language'), kwargs['current_language'] = match_language(request.preferences.get_value('language'),
LANGUAGE_CODES, LANGUAGE_CODES,
fallback=settings['search']['language']) fallback=locale)
# override url_for function in templates # override url_for function in templates
kwargs['url_for'] = url_for_theme kwargs['url_for'] = url_for_theme
@ -431,6 +443,13 @@ def pre_request():
logger.exception('invalid settings') logger.exception('invalid settings')
request.errors.append(gettext('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
request.user_plugins = [] request.user_plugins = []
allowed_plugins = preferences.plugins.get_enabled() allowed_plugins = preferences.plugins.get_enabled()
@ -637,7 +656,7 @@ def index():
unresponsive_engines=result_container.unresponsive_engines, unresponsive_engines=result_container.unresponsive_engines,
current_language=match_language(search_query.lang, current_language=match_language(search_query.lang,
LANGUAGE_CODES, LANGUAGE_CODES,
fallback=settings['search']['language']), fallback=request.preferences.get_value("language")),
base_url=get_base_url(), base_url=get_base_url(),
theme=get_current_theme_name(), theme=get_current_theme_name(),
favicons=global_favicons[themes.index(get_current_theme_name())], favicons=global_favicons[themes.index(get_current_theme_name())],
@ -757,7 +776,7 @@ def preferences():
return render('preferences.html', return render('preferences.html',
locales=settings['locales'], locales=settings['locales'],
current_locale=get_locale(), current_locale=request.preferences.get_value("locale"),
image_proxy=image_proxy, image_proxy=image_proxy,
engines_by_category=engines_by_category, engines_by_category=engines_by_category,
stats=stats, stats=stats,