diff options
author | Markus Heiser <markus.heiser@darmarit.de> | 2024-10-27 10:25:42 +0100 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2024-10-28 15:53:57 +0100 |
commit | b176323e89f87e52effc44ab0310fbf736efdc74 (patch) | |
tree | 1292c20e2af290d2ff1d1dc931ba4819f8f4e9e1 /searx | |
parent | da28f5280b8f5cac79963e10c79ddc2a3e65f93a (diff) | |
download | searxng-b176323e89f87e52effc44ab0310fbf736efdc74.tar.gz searxng-b176323e89f87e52effc44ab0310fbf736efdc74.zip |
[fix] calculator: use locale from UI (not from selected language)
Closes: https://github.com/searxng/searxng/issues/3956
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx')
-rw-r--r-- | searx/plugins/calculator.py | 14 | ||||
-rwxr-xr-x | searx/webapp.py | 2 |
2 files changed, 10 insertions, 6 deletions
diff --git a/searx/plugins/calculator.py b/searx/plugins/calculator.py index 403b04d57..e92ff9d91 100644 --- a/searx/plugins/calculator.py +++ b/searx/plugins/calculator.py @@ -8,7 +8,8 @@ import operator from multiprocessing import Process, Queue from typing import Callable -import babel.numbers +import flask +import babel from flask_babel import gettext from searx.plugins import logger @@ -100,14 +101,17 @@ def post_search(_request, search): # replace commonly used math operators with their proper Python operator query = query.replace("x", "*").replace(":", "/") + # use UI language + ui_locale = babel.Locale.parse(flask.request.preferences.get_value('locale'), sep='-') + # parse the number system in a localized way def _decimal(match: re.Match) -> str: val = match.string[match.start() : match.end()] - val = babel.numbers.parse_decimal(val, search.search_query.locale, numbering_system="latn") + val = babel.numbers.parse_decimal(val, ui_locale, numbering_system="latn") return str(val) - decimal = search.search_query.locale.number_symbols["latn"]["decimal"] - group = search.search_query.locale.number_symbols["latn"]["group"] + decimal = ui_locale.number_symbols["latn"]["decimal"] + group = ui_locale.number_symbols["latn"]["group"] query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query) # only numbers and math operators are accepted @@ -121,6 +125,6 @@ def post_search(_request, search): result = timeout_func(0.05, _eval_expr, query_py_formatted) if result is None or result == "": return True - result = babel.numbers.format_decimal(result, locale=search.search_query.locale) + result = babel.numbers.format_decimal(result, locale=ui_locale) search.result_container.answers['calculate'] = {'answer': f"{search.search_query.query} = {result}"} return True diff --git a/searx/webapp.py b/searx/webapp.py index 19c477794..d2d486d20 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -517,7 +517,7 @@ def pre_request(): preferences.parse_dict({"language": language}) logger.debug('set language %s (from browser)', preferences.get_value("language")) - # locale is defined neither in settings nor in preferences + # UI locale is defined neither in settings nor in preferences # use browser headers if not preferences.get_value("locale"): locale = _get_browser_language(request, LOCALE_NAMES.keys()) |