From f30d01ffabd50d7bb1a17da04e768c075bb8789d Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Tue, 3 Aug 2021 15:13:00 +0200 Subject: [mod] settings.yml: remove locales There are detected from the searx/translations directory --- searx/locales.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 searx/locales.py (limited to 'searx/locales.py') diff --git a/searx/locales.py b/searx/locales.py new file mode 100644 index 000000000..828f0608d --- /dev/null +++ b/searx/locales.py @@ -0,0 +1,57 @@ +from typing import List, Set +import os +import pathlib + +from babel import Locale + +LOCALE_NAMES = { + "ar": "العَرَبِيَّة (Arabic)", + "fil": "Wikang Filipino (Filipino)", + "oc": "Lenga D'òc (Occitan)", + "nl_BE": "Vlaams (Dutch, Belgium)", +} +UI_LOCALE_CODES: List[str] = [] +RTL_LOCALES: Set[str] = set() + + +def _get_name(locale, language_code): + language_name = locale.get_language_name(language_code).capitalize() + if language_name and ('a' <= language_name[0] <= 'z'): + language_name = language_name.capitalize() + terrirtory_name = locale.get_territory_name(language_code) + return language_name, terrirtory_name + + +def _get_locale_name(locale, locale_name): + native_language, native_territory = _get_name(locale, locale_name) + english_language, english_territory = _get_name(locale, 'en') + if native_territory == english_territory: + english_territory = None + if not native_territory and not english_territory: + if native_language == english_language: + return native_language + return native_language + ' (' + english_language + ')' + result = native_language + ', ' + native_territory + ' (' + english_language + if english_territory: + return result + ', ' + english_territory + ')' + return result + ')' + + +def initialize_locales(directory): + global LOCALE_NAMES, UI_LOCALE_CODES, RTL_LOCALES + for dirname in sorted(os.listdir(directory)): + # Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations + locale_dir = os.path.join(directory, dirname, 'LC_MESSAGES') + if not os.path.isdir(locale_dir): + continue + info = LOCALE_NAMES.get(dirname) + if not info: + locale = Locale.parse(dirname) + LOCALE_NAMES[dirname] = _get_locale_name(locale, dirname) + if locale.text_direction == 'rtl': + RTL_LOCALES.add(dirname) + # + UI_LOCALE_CODES = [l.replace('_', '-') for l in LOCALE_NAMES] + + +initialize_locales(pathlib.Path(__file__).parent / 'translations') -- cgit v1.2.3 From 809bf1a1056584e96b6c05bfe1aa55a9a0e11d4d Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Tue, 3 Aug 2021 18:17:23 +0200 Subject: [mod] pylint & document searx.locales (settings.yml: remove locales) - Add ``# lint: pylint`` header to pylint this python file. - Fix issues reported by pylint. - Add source code documentation of modul searx.locales Signed-off-by: Markus Heiser --- searx/locales.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'searx/locales.py') diff --git a/searx/locales.py b/searx/locales.py index 828f0608d..4dfb2b668 100644 --- a/searx/locales.py +++ b/searx/locales.py @@ -1,3 +1,9 @@ +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Initialize :py:obj:`LOCALE_NAMES`, :py:obj:`UI_LOCALE_CODES` and +:py:obj:`RTL_LOCALES`.""" + from typing import List, Set import os import pathlib @@ -10,8 +16,15 @@ LOCALE_NAMES = { "oc": "Lenga D'òc (Occitan)", "nl_BE": "Vlaams (Dutch, Belgium)", } +"""Mapping of locales and their description. Locales e.g. 'fr' or 'pt_BR' +(delimiter is *underline* '_')""" + UI_LOCALE_CODES: List[str] = [] +"""List of locales e.g. 'fr' or 'pt-BR' (delimiter is '-')""" + RTL_LOCALES: Set[str] = set() +"""List of *Right-To-Left* locales e.g. 'he' or 'fa_IR' (delimiter is +*underline* '_')""" def _get_name(locale, language_code): @@ -23,6 +36,11 @@ def _get_name(locale, language_code): def _get_locale_name(locale, locale_name): + """Get locale name e.g. 'Français - fr' or 'Português (Brasil) - pt-BR' + + :param locale: instance of :py:class:`Locale` + :param locale_name: name e.g. 'fr' or 'pt_BR' + """ native_language, native_territory = _get_name(locale, locale_name) english_language, english_territory = _get_name(locale, 'en') if native_territory == english_territory: @@ -38,11 +56,13 @@ def _get_locale_name(locale, locale_name): def initialize_locales(directory): - global LOCALE_NAMES, UI_LOCALE_CODES, RTL_LOCALES + """Initialize global names :py:obj:`LOCALE_NAMES`, :py:obj:`UI_LOCALE_CODES` and + :py:obj:`RTL_LOCALES`. + """ + global LOCALE_NAMES, UI_LOCALE_CODES, RTL_LOCALES # pylint: disable=global-statement for dirname in sorted(os.listdir(directory)): # Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations - locale_dir = os.path.join(directory, dirname, 'LC_MESSAGES') - if not os.path.isdir(locale_dir): + if not os.path.isdir( os.path.join(directory, dirname, 'LC_MESSAGES') ): continue info = LOCALE_NAMES.get(dirname) if not info: @@ -50,7 +70,7 @@ def initialize_locales(directory): LOCALE_NAMES[dirname] = _get_locale_name(locale, dirname) if locale.text_direction == 'rtl': RTL_LOCALES.add(dirname) - # + UI_LOCALE_CODES = [l.replace('_', '-') for l in LOCALE_NAMES] -- cgit v1.2.3 From 0d20e5dfe39b371b550b888db9e6cd7a664bddbb Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Wed, 4 Aug 2021 09:50:34 +0200 Subject: [mod] searx/locales.py: language names based on Unicode CLDR rename "oc" to "Occitan": * https://github.com/unicode-org/cldr/blob/35.1/seed/main/oc.xml#L115 * https://oc.wikipedia.org/wiki/Occitan see https://github.com/searxng/searxng/pull/247#issuecomment-892382001 --- searx/locales.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'searx/locales.py') diff --git a/searx/locales.py b/searx/locales.py index 4dfb2b668..80defceb7 100644 --- a/searx/locales.py +++ b/searx/locales.py @@ -11,9 +11,7 @@ import pathlib from babel import Locale LOCALE_NAMES = { - "ar": "العَرَبِيَّة (Arabic)", - "fil": "Wikang Filipino (Filipino)", - "oc": "Lenga D'òc (Occitan)", + "oc": "Occitan", "nl_BE": "Vlaams (Dutch, Belgium)", } """Mapping of locales and their description. Locales e.g. 'fr' or 'pt_BR' -- cgit v1.2.3