182 territory: str, languages=
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.
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:
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:
316 for territory, langs
in babel.core.get_global(
"territory_languages").items():
317 if not langs.get(searxng_lang, {}).get(
'official_status'):
319 terr_lang_dict[territory] = langs.get(searxng_lang)
324 territory = locale.language.upper()
325 if territory ==
'EN':
328 if terr_lang_dict.get(territory):
329 searxng_locale = locale.language +
'-' + territory
330 engine_locale = engine_locales.get(searxng_locale)
331 if engine_locale
is not None:
351 for k, v
in terr_lang_dict.items():
352 terr_lang_list.append((k, v))
354 for territory, _lang
in sorted(terr_lang_list, key=
lambda item: item[1][
'population_percent'], reverse=
True):
355 searxng_locale = locale.language +
'-' + territory
356 engine_locale = engine_locales.get(searxng_locale)
357 if engine_locale
is not None:
363 if engine_locale
is None:
364 engine_locale = default