1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-22 20:17:45 +01:00

Merge pull request #423 from dalf/available-languages

[enh] reduce the available language list
This commit is contained in:
Alexandre Flament 2021-10-26 10:31:11 +02:00 committed by GitHub
commit 9fe1ee4ed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 13 deletions

View File

@ -10,14 +10,11 @@ from zlib import compress, decompress
from urllib.parse import parse_qs, urlencode from urllib.parse import parse_qs, urlencode
from searx import settings, autocomplete from searx import settings, autocomplete
from searx.languages import language_codes as languages
from searx.locales import LOCALE_NAMES from searx.locales import LOCALE_NAMES
from searx.webutils import VALID_LANGUAGE_CODE from searx.webutils import VALID_LANGUAGE_CODE
COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years
LANGUAGE_CODES = [l[0] for l in languages]
LANGUAGE_CODES.append('all')
DISABLED = 0 DISABLED = 0
ENABLED = 1 ENABLED = 1
DOI_RESOLVERS = list(settings['doi_resolvers']) DOI_RESOLVERS = list(settings['doi_resolvers'])
@ -336,7 +333,7 @@ class Preferences:
'language': SearchLanguageSetting( 'language': SearchLanguageSetting(
settings['search']['default_lang'], settings['search']['default_lang'],
is_locked('language'), is_locked('language'),
choices=list(LANGUAGE_CODES) + [''] choices=settings['search']['languages'] + ['']
), ),
'locale': EnumStringSetting( 'locale': EnumStringSetting(
settings['ui']['default_locale'], settings['ui']['default_locale'],

View File

@ -3,6 +3,7 @@
from abc import abstractmethod, ABC from abc import abstractmethod, ABC
import re import re
from searx import settings
from searx.languages import language_codes from searx.languages import language_codes
from searx.engines import categories, engines, engine_shortcuts from searx.engines import categories, engines, engine_shortcuts
from searx.external_bang import get_bang_definition_and_autocomplete from searx.external_bang import get_bang_definition_and_autocomplete
@ -120,11 +121,17 @@ class LanguageParser(QueryPartParser):
def _autocomplete(self, value): def _autocomplete(self, value):
if not value: if not value:
# show some example queries # show some example queries
for lang in [":en", ":en_us", ":english", ":united_kingdom"]: if len(settings['search']['languages']) < 10:
self.raw_text_query.autocomplete_list.append(lang) for lang in settings['search']['languages']:
self.raw_text_query.autocomplete_list.append(':' + lang)
else:
for lang in [":en", ":en_us", ":english", ":united_kingdom"]:
self.raw_text_query.autocomplete_list.append(lang)
return return
for lc in language_codes: for lc in language_codes:
if lc[0] not in settings['search']['languages']:
continue
lang_id, lang_name, country, english_name = map(str.lower, lc) lang_id, lang_name, country, english_name = map(str.lower, lc)
# check if query starts with language-id # check if query starts with language-id

View File

@ -20,6 +20,13 @@ search:
# Default search language - leave blank to detect from browser information or # Default search language - leave blank to detect from browser information or
# use codes from 'languages.py' # use codes from 'languages.py'
default_lang: "" default_lang: ""
# Available languages
# languages:
# - all
# - es
# - de
# - it-IT
# - en-GB
# ban time in seconds after engine errors # ban time in seconds after engine errors
ban_time_on_fail: 5 ban_time_on_fail: 5
# max ban time in seconds after engine errors # max ban time in seconds after engine errors

View File

@ -17,7 +17,7 @@ searx_dir = abspath(dirname(__file__))
logger = logging.getLogger('searx') logger = logging.getLogger('searx')
OUTPUT_FORMATS = ['html', 'csv', 'json', 'rss'] OUTPUT_FORMATS = ['html', 'csv', 'json', 'rss']
LANGUAGE_CODES = ('', 'all') + tuple(l[0] for l in languages) LANGUAGE_CODES = ['all'] + list(l[0] for l in languages)
OSCAR_STYLE = ('logicodev', 'logicodev-dark', 'pointhi') OSCAR_STYLE = ('logicodev', 'logicodev-dark', 'pointhi')
CATEGORY_ORDER = [ CATEGORY_ORDER = [
'general', 'general',
@ -98,6 +98,18 @@ class SettingsValue:
self.check_type_definition(value) self.check_type_definition(value)
return value return value
class SettingSublistValue(SettingsValue):
"""Check the value is a sublist of type definition.
"""
def check_type_definition(self, value: typing.Any) -> typing.Any:
if not isinstance(value, list):
raise ValueError('The value has to a list')
for item in value:
if not item in self.type_definition[0]:
raise ValueError('{} not in {}'.format(item, self.type_definition))
class SettingsDirectoryValue(SettingsValue): class SettingsDirectoryValue(SettingsValue):
"""Check and update a setting value that is a directory path """Check and update a setting value that is a directory path
""" """
@ -148,7 +160,8 @@ SCHEMA = {
'search': { 'search': {
'safe_search': SettingsValue((0,1,2), 0), 'safe_search': SettingsValue((0,1,2), 0),
'autocomplete': SettingsValue(str, ''), 'autocomplete': SettingsValue(str, ''),
'default_lang': SettingsValue(LANGUAGE_CODES, ''), 'default_lang': SettingsValue(tuple(LANGUAGE_CODES + ['']), ''),
'languages': SettingSublistValue(LANGUAGE_CODES, LANGUAGE_CODES),
'ban_time_on_fail': SettingsValue(numbers.Real, 5), 'ban_time_on_fail': SettingsValue(numbers.Real, 5),
'max_ban_time_on_fail': SettingsValue(numbers.Real, 120), 'max_ban_time_on_fail': SettingsValue(numbers.Real, 120),
'formats': SettingsValue(list, OUTPUT_FORMATS), 'formats': SettingsValue(list, OUTPUT_FORMATS),

View File

@ -90,7 +90,6 @@ from searx.plugins.oa_doi_rewrite import get_doi_resolver
from searx.preferences import ( from searx.preferences import (
Preferences, Preferences,
ValidationException, ValidationException,
LANGUAGE_CODES,
) )
from searx.answerers import ( from searx.answerers import (
answerers, answerers,
@ -432,7 +431,7 @@ def render(template_name, override_theme=None, **kwargs):
kwargs['categories'] = _get_enable_categories(kwargs['all_categories']) kwargs['categories'] = _get_enable_categories(kwargs['all_categories'])
# i18n # i18n
kwargs['language_codes'] = languages # from searx.languages kwargs['language_codes'] = [ l for l in languages if l[0] in settings['search']['languages'] ]
kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':')) kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':'))
locale = request.preferences.get_value('locale') locale = request.preferences.get_value('locale')
@ -442,7 +441,7 @@ def render(template_name, override_theme=None, **kwargs):
kwargs['rtl'] = True kwargs['rtl'] = True
if 'current_language' not in kwargs: if 'current_language' not in kwargs:
kwargs['current_language'] = match_language( kwargs['current_language'] = match_language(
request.preferences.get_value('language'), LANGUAGE_CODES ) request.preferences.get_value('language'), settings['search']['languages'] )
# values from settings # values from settings
kwargs['search_formats'] = [ kwargs['search_formats'] = [
@ -524,7 +523,7 @@ def pre_request():
# language is defined neither in settings nor in preferences # language is defined neither in settings nor in preferences
# use browser headers # use browser headers
if not preferences.get_value("language"): if not preferences.get_value("language"):
language = _get_browser_language(request, LANGUAGE_CODES) language = _get_browser_language(request, settings['search']['languages'])
preferences.parse_dict({"language": language}) preferences.parse_dict({"language": language})
# locale is defined neither in settings nor in preferences # locale is defined neither in settings nor in preferences
@ -807,7 +806,7 @@ def search():
), ),
current_language = match_language( current_language = match_language(
search_query.lang, search_query.lang,
LANGUAGE_CODES, settings['search']['languages'],
fallback=request.preferences.get_value("language") fallback=request.preferences.get_value("language")
), ),
theme = get_current_theme_name(), theme = get_current_theme_name(),