mirror of https://github.com/searxng/searxng.git
[mod] locale: use hyphen everywhere except for Babel
This commit is contained in:
parent
988910d451
commit
a1d1aec6e2
|
@ -1,10 +1,10 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
# lint: pylint
|
# lint: pylint
|
||||||
"""Initialize :py:obj:`LOCALE_NAMES`, :py:obj:`UI_LOCALE_CODES` and
|
"""Initialize :py:obj:`LOCALE_NAMES`, :py:obj:`RTL_LOCALES`.
|
||||||
:py:obj:`RTL_LOCALES`."""
|
"""
|
||||||
|
|
||||||
from typing import List, Set
|
from typing import Set
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
|
@ -12,17 +12,14 @@ from babel import Locale
|
||||||
|
|
||||||
LOCALE_NAMES = {
|
LOCALE_NAMES = {
|
||||||
"oc": "Occitan",
|
"oc": "Occitan",
|
||||||
"nl_BE": "Vlaams (Dutch, Belgium)",
|
"nl-BE": "Vlaams (Dutch, Belgium)",
|
||||||
}
|
}
|
||||||
"""Mapping of locales and their description. Locales e.g. 'fr' or 'pt_BR'
|
"""Mapping of locales and their description. Locales e.g. 'fr' or 'pt-BR'
|
||||||
(delimiter is *underline* '_')"""
|
(delimiter is *underline* '-')"""
|
||||||
|
|
||||||
UI_LOCALE_CODES: List[str] = []
|
|
||||||
"""List of locales e.g. 'fr' or 'pt-BR' (delimiter is '-')"""
|
|
||||||
|
|
||||||
RTL_LOCALES: Set[str] = set()
|
RTL_LOCALES: Set[str] = set()
|
||||||
"""List of *Right-To-Left* locales e.g. 'he' or 'fa_IR' (delimiter is
|
"""List of *Right-To-Left* locales e.g. 'he' or 'fa-IR' (delimiter is
|
||||||
*underline* '_')"""
|
*underline* '-')"""
|
||||||
|
|
||||||
|
|
||||||
def _get_name(locale, language_code):
|
def _get_name(locale, language_code):
|
||||||
|
@ -37,7 +34,7 @@ def _get_locale_name(locale, locale_name):
|
||||||
"""Get locale name e.g. 'Français - fr' or 'Português (Brasil) - pt-BR'
|
"""Get locale name e.g. 'Français - fr' or 'Português (Brasil) - pt-BR'
|
||||||
|
|
||||||
:param locale: instance of :py:class:`Locale`
|
:param locale: instance of :py:class:`Locale`
|
||||||
:param locale_name: name e.g. 'fr' or 'pt_BR'
|
:param locale_name: name e.g. 'fr' or 'pt_BR' (delimiter is *underscore*)
|
||||||
"""
|
"""
|
||||||
native_language, native_territory = _get_name(locale, locale_name)
|
native_language, native_territory = _get_name(locale, locale_name)
|
||||||
english_language, english_territory = _get_name(locale, 'en')
|
english_language, english_territory = _get_name(locale, 'en')
|
||||||
|
@ -54,22 +51,19 @@ def _get_locale_name(locale, locale_name):
|
||||||
|
|
||||||
|
|
||||||
def initialize_locales(directory):
|
def initialize_locales(directory):
|
||||||
"""Initialize global names :py:obj:`LOCALE_NAMES`, :py:obj:`UI_LOCALE_CODES` and
|
"""Initialize global names :py:obj:`LOCALE_NAMES`, :py:obj:`RTL_LOCALES`.
|
||||||
:py:obj:`RTL_LOCALES`.
|
|
||||||
"""
|
"""
|
||||||
global UI_LOCALE_CODES # pylint: disable=global-statement
|
|
||||||
for dirname in sorted(os.listdir(directory)):
|
for dirname in sorted(os.listdir(directory)):
|
||||||
# Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations
|
# 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') ):
|
if not os.path.isdir( os.path.join(directory, dirname, 'LC_MESSAGES') ):
|
||||||
continue
|
continue
|
||||||
info = LOCALE_NAMES.get(dirname)
|
locale_name = dirname.replace('_', '-')
|
||||||
|
info = LOCALE_NAMES.get(locale_name)
|
||||||
if not info:
|
if not info:
|
||||||
locale = Locale.parse(dirname)
|
locale = Locale.parse(dirname)
|
||||||
LOCALE_NAMES[dirname] = _get_locale_name(locale, dirname)
|
LOCALE_NAMES[locale_name] = _get_locale_name(locale, dirname)
|
||||||
if locale.text_direction == 'rtl':
|
if locale.text_direction == 'rtl':
|
||||||
RTL_LOCALES.add(dirname)
|
RTL_LOCALES.add(locale_name)
|
||||||
|
|
||||||
UI_LOCALE_CODES = [l.replace('_', '-') for l in LOCALE_NAMES]
|
|
||||||
|
|
||||||
|
|
||||||
initialize_locales(pathlib.Path(__file__).parent / 'translations')
|
initialize_locales(pathlib.Path(__file__).parent / 'translations')
|
||||||
|
|
|
@ -109,7 +109,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.locales import LOCALE_NAMES, 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, set_context_network_name
|
from searx.network import stream as http_stream, set_context_network_name
|
||||||
from searx.search.checker import get_result as checker_get_result
|
from searx.search.checker import get_result as checker_get_result
|
||||||
|
@ -223,6 +223,12 @@ def get_locale():
|
||||||
if locale == 'oc':
|
if locale == 'oc':
|
||||||
request.form['use-translation'] = 'oc'
|
request.form['use-translation'] = 'oc'
|
||||||
locale = 'fr_FR'
|
locale = 'fr_FR'
|
||||||
|
if locale == '':
|
||||||
|
# if there is an error loading the preferences
|
||||||
|
# the locale is going to be ''
|
||||||
|
locale = 'en'
|
||||||
|
# babel uses underscore instead of hyphen.
|
||||||
|
locale = locale.replace('-', '_')
|
||||||
logger.debug("%s uses locale `%s`", urllib.parse.quote(request.url), locale)
|
logger.debug("%s uses locale `%s`", urllib.parse.quote(request.url), locale)
|
||||||
return locale
|
return locale
|
||||||
|
|
||||||
|
@ -240,6 +246,16 @@ def _get_browser_language(req, lang_list):
|
||||||
return 'en'
|
return 'en'
|
||||||
|
|
||||||
|
|
||||||
|
def _get_locale_rfc5646(locale):
|
||||||
|
"""Get locale name for <html lang="...">
|
||||||
|
Chrom* browsers don't detect the language when there is a subtag (ie a territory).
|
||||||
|
For example "zh-TW" is detected but not "zh-Hant-TW".
|
||||||
|
This function returns a locale without the subtag.
|
||||||
|
"""
|
||||||
|
parts = locale.split('-')
|
||||||
|
return parts[0].lower() + '-' + parts[-1].upper()
|
||||||
|
|
||||||
|
|
||||||
# code-highlighter
|
# code-highlighter
|
||||||
@app.template_filter('code_highlighter')
|
@app.template_filter('code_highlighter')
|
||||||
def code_highlighter(codelines, language=None):
|
def code_highlighter(codelines, language=None):
|
||||||
|
@ -398,11 +414,6 @@ def _get_enable_categories(all_categories):
|
||||||
return [x for x in all_categories if x in enabled_categories]
|
return [x for x in all_categories if x in enabled_categories]
|
||||||
|
|
||||||
|
|
||||||
def get_locale_rfc5646(locale):
|
|
||||||
parts = locale.split('_')
|
|
||||||
return parts[0].lower() + '-' + parts[-1].upper()
|
|
||||||
|
|
||||||
|
|
||||||
def render(template_name, override_theme=None, **kwargs):
|
def render(template_name, override_theme=None, **kwargs):
|
||||||
# values from the HTTP requests
|
# values from the HTTP requests
|
||||||
kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint
|
kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint
|
||||||
|
@ -425,7 +436,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')
|
||||||
kwargs['locale_rfc5646'] = get_locale_rfc5646(locale)
|
kwargs['locale_rfc5646'] = _get_locale_rfc5646(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
|
||||||
|
@ -519,8 +530,7 @@ def pre_request():
|
||||||
# locale is defined neither in settings nor in preferences
|
# locale is defined neither in settings nor in preferences
|
||||||
# use browser headers
|
# use browser headers
|
||||||
if not preferences.get_value("locale"):
|
if not preferences.get_value("locale"):
|
||||||
locale = _get_browser_language(request, UI_LOCALE_CODES)
|
locale = _get_browser_language(request, LOCALE_NAMES.keys())
|
||||||
locale = locale.replace('-', '_')
|
|
||||||
preferences.parse_dict({"locale": locale})
|
preferences.parse_dict({"locale": locale})
|
||||||
|
|
||||||
# request.user_plugins
|
# request.user_plugins
|
||||||
|
|
|
@ -211,12 +211,12 @@ class ViewsTestCase(SearxTestCase):
|
||||||
result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
|
result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
|
||||||
self.assertEqual(result.status_code, 200)
|
self.assertEqual(result.status_code, 200)
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
b'<option value="zh_Hant_TW" selected="selected">',
|
b'<option value="zh-Hant-TW" selected="selected">',
|
||||||
result.data,
|
result.data,
|
||||||
'Interface locale ignored browser preference.'
|
'Interface locale ignored browser preference.'
|
||||||
)
|
)
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
b'<option value="zh_Hant_TW" selected="selected">',
|
b'<option value="zh-Hant-TW" selected="selected">',
|
||||||
result.data,
|
result.data,
|
||||||
'Search language ignored browser preference.'
|
'Search language ignored browser preference.'
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue