diff options
author | Alexandre Flament <alex@al-f.net> | 2022-12-16 20:28:57 +0000 |
---|---|---|
committer | Alexandre Flament <alex@al-f.net> | 2023-02-17 15:17:36 +0000 |
commit | 6748e8e2d5eff3c2202b2a714afb5534b1573101 (patch) | |
tree | 57f6fcf3d5b9bb5ee3b4a03aaf8aac2a53ee7106 /searx/search | |
parent | 54389a29feb3feea5a868f7b3b83c9718fb71014 (diff) | |
download | searxng-6748e8e2d5eff3c2202b2a714afb5534b1573101.tar.gz searxng-6748e8e2d5eff3c2202b2a714afb5534b1573101.zip |
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.
Diffstat (limited to 'searx/search')
-rw-r--r-- | searx/search/__init__.py | 44 | ||||
-rw-r--r-- | searx/search/models.py | 13 |
2 files changed, 56 insertions, 1 deletions
diff --git a/searx/search/__init__.py b/searx/search/__init__.py index 9d337916c..c5f225aa4 100644 --- a/searx/search/__init__.py +++ b/searx/search/__init__.py @@ -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): """ diff --git a/searx/search/models.py b/searx/search/models.py index bbca1cd1d..91e5d5982 100644 --- a/searx/search/models.py +++ b/searx/search/models.py @@ -109,3 +109,16 @@ class SearchQuery: self.external_bang, ) ) + + def __copy__(self): + return SearchQuery( + self.query, + self.engineref_list, + self.lang, + self.safesearch, + self.pageno, + self.time_range, + self.timeout_limit, + self.external_bang, + self.engine_data, + ) |