Merge pull request #247 from dalf/remote_settings_locales

settings.yml: remove locales section.
This commit is contained in:
Alexandre Flament 2021-08-04 11:05:13 +02:00 committed by GitHub
commit 4e2ec9b8b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 97 additions and 64 deletions

View File

@ -0,0 +1,8 @@
.. _searx.locales:
=======
Locales
=======
.. automodule:: searx.locales
:members:

75
searx/locales.py Normal file
View File

@ -0,0 +1,75 @@
# -*- 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
from babel import Locale
LOCALE_NAMES = {
"oc": "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):
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):
"""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:
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):
"""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
if not os.path.isdir( os.path.join(directory, dirname, 'LC_MESSAGES') ):
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')

View File

@ -11,6 +11,7 @@ from urllib.parse import parse_qs, urlencode
from searx import settings, autocomplete from searx import settings, autocomplete
from searx.languages import language_codes as languages from searx.languages import language_codes as languages
from searx.locales import LOCALE_NAMES
from searx.webutils import VALID_LANGUAGE_CODE from searx.webutils import VALID_LANGUAGE_CODE
@ -340,7 +341,7 @@ class Preferences:
'locale': EnumStringSetting( 'locale': EnumStringSetting(
settings['ui']['default_locale'], settings['ui']['default_locale'],
is_locked('locale'), is_locked('locale'),
choices=list(settings['locales'].keys()) + [''] choices=list(LOCALE_NAMES.keys()) + ['']
), ),
'autocomplete': EnumStringSetting( 'autocomplete': EnumStringSetting(
settings['search']['autocomplete'], settings['search']['autocomplete'],

View File

@ -1651,53 +1651,6 @@ engines:
# chars: ' ' # chars: ' '
# keys: ['line'] # keys: ['line']
locales:
en: English
ar: العَرَبِيَّة (Arabic)
bg: Български (Bulgarian)
bo: བོད་སྐད་ (Tibetian)
ca: Català (Catalan)
cs: Čeština (Czech)
cy: Cymraeg (Welsh)
da: Dansk (Danish)
de: Deutsch (German)
el_GR: Ελληνικά (Greek_Greece)
eo: Esperanto (Esperanto)
es: Español (Spanish)
et: Eesti (Estonian)
eu: Euskara (Basque)
fa_IR: (fārsī) فارسى (Persian)
fi: Suomi (Finnish)
fil: Wikang Filipino (Filipino)
fr: Français (French)
gl: Galego (Galician)
he: עברית (Hebrew)
hr: Hrvatski (Croatian)
hu: Magyar (Hungarian)
ia: Interlingua (Interlingua)
it: Italiano (Italian)
ja: 日本語 (Japanese)
lt: Lietuvių (Lithuanian)
nl: Nederlands (Dutch)
nl_BE: Vlaams (Dutch_Belgium)
oc: Lenga D'òc (Occitan)
pl: Polski (Polish)
pt: Português (Portuguese)
pt_BR: Português (Portuguese_Brazil)
ro: Română (Romanian)
ru: Русский (Russian)
sk: Slovenčina (Slovak)
sl: Slovenski (Slovene)
sr: српски (Serbian)
sv: Svenska (Swedish)
te: తెలుగు (telugu)
ta: தமிழ் (Tamil)
tr: Türkçe (Turkish)
uk: українська мова (Ukrainian)
vi: tiếng việt (Vietnamese)
zh: 中文 (Chinese)
zh_TW: 國語 (Taiwanese Mandarin)
doi_resolvers: doi_resolvers:
oadoi.org: 'https://oadoi.org/' oadoi.org: 'https://oadoi.org/'
doi.org: 'https://doi.org/' doi.org: 'https://doi.org/'

View File

@ -79,7 +79,7 @@ class SearxRobotLayer():
def run_robot_tests(tests): def run_robot_tests(tests):
print('Running {0} tests'.format(len(tests))) print('Running {0} tests'.format(len(tests)))
for test in tests: for test in tests:
with Browser('firefox', headless=True) as browser: with Browser('firefox', headless=True, profile_preferences={'intl.accept_languages': 'en'}) as browser:
test(browser) test(browser)

View File

@ -106,6 +106,7 @@ from searx.flaskfix import patch_application
from searx.autocomplete import search_autocomplete, backends as autocomplete_backends from searx.autocomplete import search_autocomplete, backends as autocomplete_backends
from searx.languages import language_codes as languages from searx.languages import language_codes as languages
from searx.locales import LOCALE_NAMES, UI_LOCALE_CODES, RTL_LOCALES
from searx.search import SearchWithPlugins, initialize as search_initialize from searx.search import SearchWithPlugins, initialize as search_initialize
from searx.network import stream as http_stream from searx.network import stream as http_stream
from searx.search.checker import get_result as checker_get_result from searx.search.checker import get_result as checker_get_result
@ -176,12 +177,6 @@ if (not werkzeug_reloader
babel = Babel(app) babel = Babel(app)
rtl_locales = [
'ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'fa_IR', 'glk', 'he',
'ku', 'mzn', 'pnb', 'ps', 'sd', 'ug', 'ur', 'yi'
]
ui_locale_codes = [l.replace('_', '-') for l in settings['locales'].keys()]
# used when translating category names # used when translating category names
_category_names = ( _category_names = (
gettext('files'), gettext('files'),
@ -258,7 +253,7 @@ def _get_browser_or_settings_language(req, lang_list):
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
if 'locale' in request.form\ if 'locale' in request.form\
and request.form['locale'] in settings['locales']: and request.form['locale'] in LOCALE_NAMES:
# use locale from the form # use locale from the form
locale = request.form['locale'] locale = request.form['locale']
locale_source = 'form' locale_source = 'form'
@ -268,7 +263,7 @@ def get_locale():
locale_source = 'preferences' locale_source = 'preferences'
else: else:
# use local from the browser # use local from the browser
locale = _get_browser_or_settings_language(request, ui_locale_codes) locale = _get_browser_or_settings_language(request, UI_LOCALE_CODES)
locale = locale.replace('-', '_') locale = locale.replace('-', '_')
locale_source = 'browser' locale_source = 'browser'
@ -463,7 +458,7 @@ def render(template_name, override_theme=None, **kwargs):
kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':')) kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':'))
locale = request.preferences.get_value('locale') locale = request.preferences.get_value('locale')
if locale in rtl_locales and 'rtl' not in kwargs: if locale in RTL_LOCALES and 'rtl' not in kwargs:
kwargs['rtl'] = True kwargs['rtl'] = True
if 'current_language' not in kwargs: if 'current_language' not in kwargs:
kwargs['current_language'] = match_language( kwargs['current_language'] = match_language(
@ -1042,7 +1037,7 @@ def preferences():
return render( return render(
'preferences.html', 'preferences.html',
selected_categories = get_selected_categories(request.preferences, request.form), selected_categories = get_selected_categories(request.preferences, request.form),
locales = settings['locales'], locales = LOCALE_NAMES,
current_locale = request.preferences.get_value("locale"), current_locale = request.preferences.get_value("locale"),
image_proxy = image_proxy, image_proxy = image_proxy,
engines_by_category = engines_by_category, engines_by_category = engines_by_category,
@ -1315,7 +1310,7 @@ def config():
'engines': _engines, 'engines': _engines,
'plugins': _plugins, 'plugins': _plugins,
'instance_name': settings['general']['instance_name'], 'instance_name': settings['general']['instance_name'],
'locales': settings['locales'], 'locales': LOCALE_NAMES,
'default_locale': settings['ui']['default_locale'], 'default_locale': settings['ui']['default_locale'],
'autocomplete': settings['search']['autocomplete'], 'autocomplete': settings['search']['autocomplete'],
'safe_search': settings['search']['safe_search'], 'safe_search': settings['search']['safe_search'],

View File

@ -8,7 +8,8 @@ import json
from sys import path from sys import path
from os.path import realpath, dirname, join from os.path import realpath, dirname, join
from searx import searx_dir, settings from searx import searx_dir
from searx.locales import LOCALE_NAMES
from searx.engines.wikidata import send_wikidata_query from searx.engines.wikidata import send_wikidata_query
@ -44,7 +45,7 @@ ORDER BY ?iso4217 ?article_name
""" """
LANGUAGES = settings['locales'].keys() LANGUAGES = LOCALE_NAMES.keys()
LANGUAGES_SPARQL = ', '.join(set(map(lambda l: repr(l.split('_')[0]), LANGUAGES))) LANGUAGES_SPARQL = ', '.join(set(map(lambda l: repr(l.split('_')[0]), LANGUAGES)))

View File

@ -8,6 +8,7 @@ from lxml.html import fromstring
from searx.engines.wikidata import send_wikidata_query from searx.engines.wikidata import send_wikidata_query
from searx.utils import extract_text from searx.utils import extract_text
from searx.locales import LOCALE_NAMES
import searx import searx
import searx.search import searx.search
import searx.network import searx.network
@ -35,7 +36,7 @@ WHERE {
ORDER BY ?itemLang ORDER BY ?itemLang
""" """
LANGUAGES = searx.settings['locales'].keys() LANGUAGES = LOCALE_NAMES.keys()
LANGUAGES_SPARQL = ', '.join(set(map(lambda l: repr(l.split('_')[0]), LANGUAGES))) LANGUAGES_SPARQL = ', '.join(set(map(lambda l: repr(l.split('_')[0]), LANGUAGES)))
IDS = None IDS = None

View File

@ -41,7 +41,6 @@ class TestDefaultSettings(SearxTestCase):
self.assertTrue(isinstance(settings['server']['port'], int)) self.assertTrue(isinstance(settings['server']['port'], int))
self.assertTrue(isinstance(settings['server']['bind_address'], str)) self.assertTrue(isinstance(settings['server']['bind_address'], str))
self.assertTrue(isinstance(settings['engines'], list)) self.assertTrue(isinstance(settings['engines'], list))
self.assertTrue(isinstance(settings['locales'], dict))
self.assertTrue(isinstance(settings['doi_resolvers'], dict)) self.assertTrue(isinstance(settings['doi_resolvers'], dict))
self.assertTrue(isinstance(settings['default_doi_resolver'], str)) self.assertTrue(isinstance(settings['default_doi_resolver'], str))