1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-22 12:10:11 +01:00

[mod] settings.yml: remove locales

There are detected from the searx/translations directory
This commit is contained in:
Alexandre Flament 2021-08-03 15:13:00 +02:00
parent bd17544f82
commit f30d01ffab
7 changed files with 70 additions and 63 deletions

57
searx/locales.py Normal file
View File

@ -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')

View File

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

View File

@ -1651,53 +1651,6 @@ engines:
# chars: ' '
# 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:
oadoi.org: 'https://oadoi.org/'
doi.org: 'https://doi.org/'

View File

@ -106,6 +106,7 @@ from searx.flaskfix import patch_application
from searx.autocomplete import search_autocomplete, backends as autocomplete_backends
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.network import stream as http_stream
from searx.search.checker import get_result as checker_get_result
@ -176,12 +177,6 @@ if (not werkzeug_reloader
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
_category_names = (
gettext('files'),
@ -258,7 +253,7 @@ def _get_browser_or_settings_language(req, lang_list):
@babel.localeselector
def get_locale():
if 'locale' in request.form\
and request.form['locale'] in settings['locales']:
and request.form['locale'] in LOCALE_NAMES:
# use locale from the form
locale = request.form['locale']
locale_source = 'form'
@ -268,7 +263,7 @@ def get_locale():
locale_source = 'preferences'
else:
# 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_source = 'browser'
@ -463,7 +458,7 @@ def render(template_name, override_theme=None, **kwargs):
kwargs['translations'] = json.dumps(get_translations(), separators=(',', ':'))
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
if 'current_language' not in kwargs:
kwargs['current_language'] = match_language(
@ -1042,7 +1037,7 @@ def preferences():
return render(
'preferences.html',
selected_categories = get_selected_categories(request.preferences, request.form),
locales = settings['locales'],
locales = LOCALE_NAMES,
current_locale = request.preferences.get_value("locale"),
image_proxy = image_proxy,
engines_by_category = engines_by_category,
@ -1315,7 +1310,7 @@ def config():
'engines': _engines,
'plugins': _plugins,
'instance_name': settings['general']['instance_name'],
'locales': settings['locales'],
'locales': LOCALE_NAMES,
'default_locale': settings['ui']['default_locale'],
'autocomplete': settings['search']['autocomplete'],
'safe_search': settings['search']['safe_search'],

View File

@ -8,7 +8,8 @@ import json
from sys import path
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
@ -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)))

View File

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

View File

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