summaryrefslogtreecommitdiff
path: root/searx/locales.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarit.de>2022-08-14 13:38:50 +0200
committerMarkus Heiser <markus.heiser@darmarit.de>2022-08-14 13:55:42 +0200
commitef81d14ccf6838d6ea58ab4d27cb1fc93c2c88c3 (patch)
tree1add7c7f1f426b3f18f3837412b7c2d6a3661bbe /searx/locales.py
parent2bd3c2079ea73a12c37fef0667d654d0528df89b (diff)
downloadsearxng-ef81d14ccf6838d6ea58ab4d27cb1fc93c2c88c3.tar.gz
searxng-ef81d14ccf6838d6ea58ab4d27cb1fc93c2c88c3.zip
[fix] harden get_engine_locale: handle UnknownLocaleError exceptions
When a user selects an unknown or invalid locale by using the search syntax: !qw siemens :de-TW Before this patch a UnknownLocaleError exception will be rasied: ``` Traceback (most recent call last): File "SearXNG/searx/search/processors/online.py", line 154, in search search_results = self._search_basic(query, params) File "SearXNG/searx/search/processors/online.py", line 128, in _search_basic self.engine.request(query, params) File "SearXNG/searx/engines/qwant.py", line 98, in request q_locale = get_engine_locale(params['language'], supported_languages, default='en_US') File "SearXNG/searx/locales.py", line 216, in get_engine_locale locale = babel.Locale.parse(searxng_locale, sep='-') File "SearXNG/local/py3/lib/python3.8/site-packages/babel/core.py", line 330, in parse raise UnknownLocaleError(input_id) ``` This patch implements a simple exception handling, since e.g. `de-TW` does not exists `de` will be used to get engines locale. On invalid terms like `xy-XY` the default will be returned. Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/locales.py')
-rw-r--r--searx/locales.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/searx/locales.py b/searx/locales.py
index 227c95051..a7c037aa0 100644
--- a/searx/locales.py
+++ b/searx/locales.py
@@ -213,7 +213,13 @@ def get_engine_locale(searxng_locale, engine_locales, default=None):
# need to narrow language nor territory.
return engine_locale
- locale = babel.Locale.parse(searxng_locale, sep='-')
+ try:
+ locale = babel.Locale.parse(searxng_locale, sep='-')
+ except babel.core.UnknownLocaleError:
+ try:
+ locale = babel.Locale.parse(searxng_locale.split('-')[1])
+ except babel.core.UnknownLocaleError:
+ return default
# SearXNG's selected locale is not supported by the engine ..