diff options
author | Alexandre Flament <alex@al-f.net> | 2021-08-03 15:13:00 +0200 |
---|---|---|
committer | Alexandre Flament <alex@al-f.net> | 2021-08-03 15:44:45 +0200 |
commit | f30d01ffabd50d7bb1a17da04e768c075bb8789d (patch) | |
tree | 4db07dd442d08eb801f295b4290256925331e7ff /searx/locales.py | |
parent | bd17544f82809ef57f09aa95dc735e7c9056717e (diff) | |
download | searxng-f30d01ffabd50d7bb1a17da04e768c075bb8789d.tar.gz searxng-f30d01ffabd50d7bb1a17da04e768c075bb8789d.zip |
[mod] settings.yml: remove locales
There are detected from the searx/translations directory
Diffstat (limited to 'searx/locales.py')
-rw-r--r-- | searx/locales.py | 57 |
1 files changed, 57 insertions, 0 deletions
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') |