181 territory: str, languages=
None, regional: bool =
False, de_facto: bool =
True
182) -> set[babel.Locale]:
183 """Returns a list of :py:obj:`babel.Locale` with languages from
184 :py:obj:`babel.languages.get_official_languages`.
186 :param territory: The territory (country or region) code.
188 :param languages: A list of language codes the languages from
189 :py:obj:`babel.languages.get_official_languages` should be in
190 (intersection). If this argument is ``None``, all official languages in
191 this territory are used.
193 :param regional: If the regional flag is set, then languages which are
194 regionally official are also returned.
196 :param de_facto: If the de_facto flag is set to `False`, then languages
197 which are “de facto” official are not returned.
201 o_languages = babel.languages.get_official_languages(territory, regional=regional, de_facto=de_facto)
204 languages = [l.lower()
for l
in languages]
205 o_languages = set(l
for l
in o_languages
if l.lower()
in languages)
207 for lang
in o_languages:
209 locale = babel.Locale.parse(lang +
'_' + territory)
211 except babel.UnknownLocaleError:
218 """Return engine's language (aka locale) string that best fits to argument
221 Argument ``engine_locales`` is a python dict that maps *SearXNG locales* to
222 corresponding *engine locales*::
225 # SearXNG string : engine-string
242 The *SearXNG locale* string has to be known by babel!
244 If there is no direct 1:1 mapping, this functions tries to narrow down
245 engine's language (locale). If no value can be determined by these
246 approximation attempts the ``default`` value is returned.
250 A. When user select a language the results should be optimized according to
251 the selected language.
253 B. When user select a language and a territory the results should be
254 optimized with first priority on territory and second on language.
256 First approximation rule (*by territory*):
258 When the user selects a locale with territory (and a language), the
259 territory has priority over the language. If any of the official languages
260 in the territory is supported by the engine (``engine_locales``) it will
263 Second approximation rule (*by language*):
265 If "First approximation rule" brings no result or the user selects only a
266 language without a territory. Check in which territories the language
267 has an official status and if one of these territories is supported by the
273 engine_locale = engine_locales.get(searxng_locale)
275 if engine_locale
is not None:
281 locale = babel.Locale.parse(searxng_locale, sep=
'-')
282 except babel.core.UnknownLocaleError:
284 locale = babel.Locale.parse(searxng_locale.split(
'-')[0])
285 except babel.core.UnknownLocaleError:
288 searxng_lang = language_tag(locale)
289 engine_locale = engine_locales.get(searxng_lang)
290 if engine_locale
is not None:
299 for official_language
in babel.languages.get_official_languages(locale.territory, de_facto=
True):
300 searxng_locale = official_language +
'-' + locale.territory
301 engine_locale = engine_locales.get(searxng_locale)
302 if engine_locale
is not None:
315 for territory, langs
in babel.core.get_global(
"territory_languages").items():
316 if not langs.get(searxng_lang, {}).get(
'official_status'):
318 terr_lang_dict[territory] = langs.get(searxng_lang)
323 territory = locale.language.upper()
324 if territory ==
'EN':
327 if terr_lang_dict.get(territory):
328 searxng_locale = locale.language +
'-' + territory
329 engine_locale = engine_locales.get(searxng_locale)
330 if engine_locale
is not None:
350 for k, v
in terr_lang_dict.items():
351 terr_lang_list.append((k, v))
353 for territory, _lang
in sorted(terr_lang_list, key=
lambda item: item[1][
'population_percent'], reverse=
True):
354 searxng_locale = locale.language +
'-' + territory
355 engine_locale = engine_locales.get(searxng_locale)
356 if engine_locale
is not None:
362 if engine_locale
is None:
363 engine_locale = default
368def match_locale(searxng_locale: str, locale_tag_list: list[str], fallback: str |
None =
None) -> str |
None:
369 """Return tag from ``locale_tag_list`` that best fits to ``searxng_locale``.
371 :param str searxng_locale: SearXNG's internal representation of locale (de,
372 de-DE, fr-BE, zh, zh-CN, zh-TW ..).
374 :param list locale_tag_list: The list of locale tags to select from
376 :param str fallback: fallback locale tag (if unset --> ``None``)
378 The rules to find a match are implemented in :py:obj:`get_engine_locale`,
379 the ``engine_locales`` is build up by :py:obj:`build_engine_locales`.
383 The *SearXNG locale* string and the members of ``locale_tag_list`` has to
384 be known by babel! The :py:obj:`ADDITIONAL_TRANSLATIONS` are used in the
385 UI and are not known by babel --> will be ignored.
391 if not searxng_locale:
394 locale = get_locale(searxng_locale)
400 searxng_locale = language_tag(locale)
402 searxng_locale = region_tag(locale)
407 for tag
in locale_tag_list:
408 if tag
in (
'all',
'auto')
or tag
in ADDITIONAL_TRANSLATIONS: