forked from Icycoide/searxng
Add "Auto-detected" as a language.
When the user choose "Auto-detected", the choice remains on the following queries. The detected language is displayed. For example "Auto-detected (en)": * the next query language is going to be auto detected * for the current query, the detected language is English. This replace the autodetect_search_language plugin.
This commit is contained in:
parent
54389a29fe
commit
6748e8e2d5
14 changed files with 143 additions and 115 deletions
|
@ -3,10 +3,12 @@
|
|||
# pylint: disable=missing-module-docstring, too-few-public-methods
|
||||
|
||||
import threading
|
||||
from copy import copy
|
||||
from timeit import default_timer
|
||||
from uuid import uuid4
|
||||
|
||||
import flask
|
||||
import babel
|
||||
|
||||
from searx import settings
|
||||
from searx.answerers import ask
|
||||
|
@ -20,6 +22,7 @@ from searx.network import initialize as initialize_network, check_network_config
|
|||
from searx.metrics import initialize as initialize_metrics, counter_inc, histogram_observe_time
|
||||
from searx.search.processors import PROCESSORS, initialize as initialize_processors
|
||||
from searx.search.checker import initialize as initialize_checker
|
||||
from searx.utils import detect_language
|
||||
|
||||
|
||||
logger = logger.getChild('search')
|
||||
|
@ -37,18 +40,57 @@ def initialize(settings_engines=None, enable_checker=False, check_network=False,
|
|||
initialize_checker()
|
||||
|
||||
|
||||
def replace_auto_language(search_query: SearchQuery):
|
||||
"""
|
||||
Do nothing except if `search_query.lang` is "auto".
|
||||
In this case:
|
||||
* the value "auto" is replaced by the detected language of the query.
|
||||
The default value is "all" when no language is detected.
|
||||
* `search_query.locale` is updated accordingly
|
||||
|
||||
Use :py:obj:`searx.utils.detect_language` with `only_search_languages=True` to keep
|
||||
only languages supported by the engines.
|
||||
"""
|
||||
if search_query.lang != 'auto':
|
||||
return
|
||||
|
||||
detected_lang = detect_language(search_query.query, threshold=0.0, only_search_languages=True)
|
||||
if detected_lang is None:
|
||||
# fallback to 'all' if no language has been detected
|
||||
search_query.lang = 'all'
|
||||
search_query.locale = None
|
||||
return
|
||||
search_query.lang = detected_lang
|
||||
try:
|
||||
search_query.locale = babel.Locale.parse(search_query.lang)
|
||||
except babel.core.UnknownLocaleError:
|
||||
search_query.locale = None
|
||||
|
||||
|
||||
class Search:
|
||||
"""Search information container"""
|
||||
|
||||
__slots__ = "search_query", "result_container", "start_time", "actual_timeout"
|
||||
|
||||
def __init__(self, search_query: SearchQuery):
|
||||
"""Initialize the Search
|
||||
|
||||
search_query is copied
|
||||
"""
|
||||
# init vars
|
||||
super().__init__()
|
||||
self.search_query = search_query
|
||||
self.result_container = ResultContainer()
|
||||
self.start_time = None
|
||||
self.actual_timeout = None
|
||||
self.search_query = copy(search_query)
|
||||
self.update_search_query(self.search_query)
|
||||
|
||||
def update_search_query(self, search_query: SearchQuery):
|
||||
"""Update search_query.
|
||||
|
||||
call replace_auto_language to replace the "auto" language
|
||||
"""
|
||||
replace_auto_language(search_query)
|
||||
|
||||
def search_external_bang(self):
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue