182 territory: str, languages: list[str] |
None =
None, regional: bool =
False, de_facto: bool =
True
183) -> set[babel.Locale]:
184 """Returns a list of :py:obj:`babel.Locale` with languages from
185 :py:obj:`babel.languages.get_official_languages`.
187 :param territory: The territory (country or region) code.
189 :param languages: A list of language codes the languages from
190 :py:obj:`babel.languages.get_official_languages` should be in
191 (intersection). If this argument is ``None``, all official languages in
192 this territory are used.
194 :param regional: If the regional flag is set, then languages which are
195 regionally official are also returned.
197 :param de_facto: If the de_facto flag is set to `False`, then languages
198 which are “de facto” official are not returned.
201 ret_val: set[babel.Locale] = set()
202 o_languages = babel.languages.get_official_languages(territory, regional=regional, de_facto=de_facto)
205 languages = [l.lower()
for l
in languages]
206 o_languages = set(l
for l
in o_languages
if l.lower()
in languages)
208 for lang
in o_languages:
210 locale = babel.Locale.parse(lang +
'_' + territory)
212 except babel.UnknownLocaleError:
218def get_engine_locale(searxng_locale: str, engine_locales: dict[str, str], default: str |
None =
None) -> str |
None:
219 """Return engine's language (aka locale) string that best fits to argument
222 Argument ``engine_locales`` is a python dict that maps *SearXNG locales* to
223 corresponding *engine locales*::
226 # SearXNG string : engine-string
243 The *SearXNG locale* string has to be known by babel!
245 If there is no direct 1:1 mapping, this functions tries to narrow down
246 engine's language (locale). If no value can be determined by these
247 approximation attempts the ``default`` value is returned.
251 A. When user select a language the results should be optimized according to
252 the selected language.
254 B. When user select a language and a territory the results should be
255 optimized with first priority on territory and second on language.
257 First approximation rule (*by territory*):
259 When the user selects a locale with territory (and a language), the
260 territory has priority over the language. If any of the official languages
261 in the territory is supported by the engine (``engine_locales``) it will
264 Second approximation rule (*by language*):
266 If "First approximation rule" brings no result or the user selects only a
267 language without a territory. Check in which territories the language
268 has an official status and if one of these territories is supported by the
274 engine_locale = engine_locales.get(searxng_locale)
276 if engine_locale
is not None:
282 locale = babel.Locale.parse(searxng_locale, sep=
'-')
283 except babel.core.UnknownLocaleError:
285 locale = babel.Locale.parse(searxng_locale.split(
'-')[0])
286 except babel.core.UnknownLocaleError:
290 engine_locale = engine_locales.get(searxng_lang)
291 if engine_locale
is not None:
300 for official_language
in babel.languages.get_official_languages(locale.territory, de_facto=
True):
301 searxng_locale = official_language +
'-' + locale.territory
302 engine_locale = engine_locales.get(searxng_locale)
303 if engine_locale
is not None:
315 terr_lang_dict: dict[str, dict[str, t.Any]] = {}
317 langs: dict[str, dict[str, t.Any]]
318 for territory, langs
in babel.core.get_global(
"territory_languages").items():
319 _lang = langs.get(searxng_lang)
320 if _lang
is None or _lang.get(
'official_status')
is None:
322 terr_lang_dict[territory] = _lang
327 territory = locale.language.upper()
328 if territory ==
'EN':
331 if terr_lang_dict.get(territory):
332 searxng_locale = locale.language +
'-' + territory
333 engine_locale = engine_locales.get(searxng_locale)
334 if engine_locale
is not None:
353 terr_lang_list: list[tuple[str, dict[str, t.Any]]] = []
354 for k, v
in terr_lang_dict.items():
355 terr_lang_list.append((k, v))
357 for territory, _lang
in sorted(terr_lang_list, key=
lambda item: item[1][
'population_percent'], reverse=
True):
358 searxng_locale = locale.language +
'-' + territory
359 engine_locale = engine_locales.get(searxng_locale)
360 if engine_locale
is not None:
366 if engine_locale
is None:
367 engine_locale = default