.oO SearXNG Developer Documentation Oo.
|
Functions | |
localeselector () | |
get_translations () | |
list[str] | get_translation_locales () |
locales_initialize () | |
str | region_tag (babel.Locale locale) |
str | language_tag (babel.Locale locale) |
babel.Locale|None | get_locale (str locale_tag) |
set[babel.Locale] | get_official_locales (str territory, languages=None, bool regional=False, bool de_facto=True) |
get_engine_locale (searxng_locale, engine_locales, default=None) | |
str|None | match_locale (str searxng_locale, list[str] locale_tag_list, str|None fallback=None) |
build_engine_locales (list[str] tag_list) | |
Variables | |
logger = logger.getChild('locales') | |
_flask_babel_get_translations = flask_babel.get_translations | |
dict | LOCALE_NAMES = {} |
set | RTL_LOCALES = set() |
dict | ADDITIONAL_TRANSLATIONS |
dict | LOCALE_BEST_MATCH |
list | _TR_LOCALES = [] |
SearXNG’s locale data ===================== The variables :py:obj:`RTL_LOCALES` and :py:obj:`LOCALE_NAMES` are loaded from :origin:`searx/data/locales.json` / see :py:obj:`locales_initialize` and :ref:`update_locales.py`. .. hint:: Whenever the value of :py:obj:`ADDITIONAL_TRANSLATIONS` or :py:obj:`LOCALE_BEST_MATCH` is modified, the :origin:`searx/data/locales.json` needs to be rebuild:: ./manage data.locales SearXNG's locale codes ====================== .. automodule:: searx.sxng_locales :members: SearXNG’s locale implementations ================================
searx.locales.build_engine_locales | ( | list[str] | tag_list | ) |
From a list of locale tags a dictionary is build that can be passed by argument ``engine_locales`` to :py:obj:`get_engine_locale`. This function is mainly used by :py:obj:`match_locale` and is similar to what the ``fetch_traits(..)`` function of engines do. If there are territory codes in the ``tag_list`` that have a *script code* additional keys are added to the returned dictionary. .. code:: python >>> import locales >>> engine_locales = locales.build_engine_locales(['en', 'en-US', 'zh', 'zh-CN', 'zh-TW']) >>> engine_locales { 'en': 'en', 'en-US': 'en-US', 'zh': 'zh', 'zh-CN': 'zh-CN', 'zh_Hans': 'zh-CN', 'zh-TW': 'zh-TW', 'zh_Hant': 'zh-TW' } >>> get_engine_locale('zh-Hans', engine_locales) 'zh-CN' This function is a good example to understand the language/region model of SearXNG: SearXNG only distinguishes between **search languages** and **search regions**, by adding the *script-tags*, languages with *script-tags* can be assigned to the **regions** that SearXNG supports.
Definition at line 418 of file locales.py.
References get_locale(), language_tag(), and region_tag().
Referenced by match_locale().
searx.locales.get_engine_locale | ( | searxng_locale, | |
engine_locales, | |||
default = None ) |
Return engine's language (aka locale) string that best fits to argument ``searxng_locale``. Argument ``engine_locales`` is a python dict that maps *SearXNG locales* to corresponding *engine locales*:: <engine>: { # SearXNG string : engine-string 'ca-ES' : 'ca_ES', 'fr-BE' : 'fr_BE', 'fr-CA' : 'fr_CA', 'fr-CH' : 'fr_CH', 'fr' : 'fr_FR', ... 'pl-PL' : 'pl_PL', 'pt-PT' : 'pt_PT' .. 'zh' : 'zh' 'zh_Hans' : 'zh' 'zh_Hant' : 'zh_TW' } .. hint:: The *SearXNG locale* string has to be known by babel! If there is no direct 1:1 mapping, this functions tries to narrow down engine's language (locale). If no value can be determined by these approximation attempts the ``default`` value is returned. Assumptions: A. When user select a language the results should be optimized according to the selected language. B. When user select a language and a territory the results should be optimized with first priority on territory and second on language. First approximation rule (*by territory*): When the user selects a locale with territory (and a language), the territory has priority over the language. If any of the official languages in the territory is supported by the engine (``engine_locales``) it will be used. Second approximation rule (*by language*): If "First approximation rule" brings no result or the user selects only a language without a territory. Check in which territories the language has an official status and if one of these territories is supported by the engine.
Definition at line 218 of file locales.py.
References language_tag().
Referenced by match_locale().
babel.Locale | None searx.locales.get_locale | ( | str | locale_tag | ) |
Returns a :py:obj:`babel.Locale` object parsed from argument ``locale_tag``
Definition at line 170 of file locales.py.
Referenced by build_engine_locales(), and match_locale().
set[babel.Locale] searx.locales.get_official_locales | ( | str | territory, |
languages = None, | |||
bool | regional = False, | ||
bool | de_facto = True ) |
Returns a list of :py:obj:`babel.Locale` with languages from :py:obj:`babel.languages.get_official_languages`. :param territory: The territory (country or region) code. :param languages: A list of language codes the languages from :py:obj:`babel.languages.get_official_languages` should be in (intersection). If this argument is ``None``, all official languages in this territory are used. :param regional: If the regional flag is set, then languages which are regionally official are also returned. :param de_facto: If the de_facto flag is set to `False`, then languages which are “de facto” official are not returned.
Definition at line 181 of file locales.py.
list[str] searx.locales.get_translation_locales | ( | ) |
Returns the list of translation locales (*underscore*). The list is generated from the translation folders in :origin:`searx/translations`
Definition at line 123 of file locales.py.
searx.locales.get_translations | ( | ) |
Monkey patch of :py:obj:`flask_babel.get_translations`
Definition at line 110 of file locales.py.
References _flask_babel_get_translations.
str searx.locales.language_tag | ( | babel.Locale | locale | ) |
Returns SearXNG's language tag from the locale and if exits, the tag includes the script name (e.g. en, zh_Hant).
Definition at line 160 of file locales.py.
Referenced by build_engine_locales(), get_engine_locale(), and match_locale().
searx.locales.locales_initialize | ( | ) |
Initialize locales environment of the SearXNG session. - monkey patch :py:obj:`flask_babel.get_translations` by :py:obj:`get_translations` - init global names :py:obj:`LOCALE_NAMES`, :py:obj:`RTL_LOCALES`
Definition at line 142 of file locales.py.
searx.locales.localeselector | ( | ) |
Definition at line 86 of file locales.py.
str | None searx.locales.match_locale | ( | str | searxng_locale, |
list[str] | locale_tag_list, | ||
str | None | fallback = None ) |
Return tag from ``locale_tag_list`` that best fits to ``searxng_locale``. :param str searxng_locale: SearXNG's internal representation of locale (de, de-DE, fr-BE, zh, zh-CN, zh-TW ..). :param list locale_tag_list: The list of locale tags to select from :param str fallback: fallback locale tag (if unset --> ``None``) The rules to find a match are implemented in :py:obj:`get_engine_locale`, the ``engine_locales`` is build up by :py:obj:`build_engine_locales`. .. hint:: The *SearXNG locale* string and the members of ``locale_tag_list`` has to be known by babel! The :py:obj:`ADDITIONAL_TRANSLATIONS` are used in the UI and are not known by babel --> will be ignored.
Definition at line 369 of file locales.py.
References build_engine_locales(), get_engine_locale(), get_locale(), language_tag(), and region_tag().
str searx.locales.region_tag | ( | babel.Locale | locale | ) |
Returns SearXNG's region tag from the locale (e.g. zh-TW , en-US).
Definition at line 153 of file locales.py.
Referenced by build_engine_locales(), and match_locale().
|
protected |
Definition at line 51 of file locales.py.
Referenced by get_translations().
|
protected |
Definition at line 120 of file locales.py.
dict searx.locales.ADDITIONAL_TRANSLATIONS |
Definition at line 64 of file locales.py.
dict searx.locales.LOCALE_BEST_MATCH |
Definition at line 73 of file locales.py.
dict searx.locales.LOCALE_NAMES = {} |
Definition at line 53 of file locales.py.
searx.locales.logger = logger.getChild('locales') |
Definition at line 47 of file locales.py.
set searx.locales.RTL_LOCALES = set() |
Definition at line 60 of file locales.py.