| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  | """Initialize :py:obj:`LOCALE_NAMES`, :py:obj:`RTL_LOCALES`.
 | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  | from typing import Set | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | import pathlib | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from babel import Locale | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | from babel.support import Translations | 
					
						
							|  |  |  | import flask_babel | 
					
						
							|  |  |  | import flask | 
					
						
							|  |  |  | from flask.ctx import has_request_context | 
					
						
							|  |  |  | from searx import logger | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | logger = logger.getChild('locales') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # safe before monkey patching flask_babel.get_translations | 
					
						
							|  |  |  | _flask_babel_get_translations = flask_babel.get_translations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LOCALE_NAMES = {} | 
					
						
							|  |  |  | """Mapping of locales and their description.  Locales e.g. 'fr' or 'pt-BR' (see
 | 
					
						
							|  |  |  | :py:obj:`locales_initialize`)."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RTL_LOCALES: Set[str] = set() | 
					
						
							|  |  |  | """List of *Right-To-Left* locales e.g. 'he' or 'fa-IR' (see
 | 
					
						
							|  |  |  | :py:obj:`locales_initialize`)."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ADDITIONAL_TRANSLATIONS = { | 
					
						
							| 
									
										
										
										
											2021-08-04 09:50:34 +02:00
										 |  |  |     "oc": "Occitan", | 
					
						
							| 
									
										
										
										
											2022-05-06 11:40:45 +02:00
										 |  |  |     "szl": "Ślōnski (Silesian)", | 
					
						
							| 
									
										
										
										
											2022-07-08 10:00:20 +02:00
										 |  |  |     "pap": "Papiamento", | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | """Additional languages SearXNG has translations for but not supported by
 | 
					
						
							|  |  |  | python-babel (see :py:obj:`locales_initialize`)."""
 | 
					
						
							| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | LOCALE_BEST_MATCH = { | 
					
						
							|  |  |  |     "oc": 'fr-FR', | 
					
						
							|  |  |  |     "szl": "pl", | 
					
						
							|  |  |  |     "nl-BE": "nl", | 
					
						
							|  |  |  |     "zh-HK": "zh-Hant-TW", | 
					
						
							| 
									
										
										
										
											2022-07-08 10:00:20 +02:00
										 |  |  |     "pap": "pt-BR", | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | """Map a locale we do not have a translations for to a locale we have a
 | 
					
						
							|  |  |  | translation for. By example: use Taiwan version of the translation for Hong | 
					
						
							|  |  |  | Kong."""
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | def localeselector(): | 
					
						
							|  |  |  |     locale = 'en' | 
					
						
							|  |  |  |     if has_request_context(): | 
					
						
							|  |  |  |         value = flask.request.preferences.get_value('locale') | 
					
						
							|  |  |  |         if value: | 
					
						
							|  |  |  |             locale = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # first, set the language that is not supported by babel | 
					
						
							|  |  |  |     if locale in ADDITIONAL_TRANSLATIONS: | 
					
						
							|  |  |  |         flask.request.form['use-translation'] = locale | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # second, map locale to a value python-babel supports | 
					
						
							|  |  |  |     locale = LOCALE_BEST_MATCH.get(locale, locale) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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('-', '_') | 
					
						
							|  |  |  |     return locale | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_translations(): | 
					
						
							| 
									
										
										
										
											2022-06-14 16:31:41 +02:00
										 |  |  |     """Monkey patch of :py:obj:`flask_babel.get_translations`""" | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  |     if has_request_context() and flask.request.form.get('use-translation') == 'oc': | 
					
						
							|  |  |  |         babel_ext = flask_babel.current_app.extensions['babel'] | 
					
						
							|  |  |  |         return Translations.load(next(babel_ext.translation_directories), 'oc') | 
					
						
							|  |  |  |     if has_request_context() and flask.request.form.get('use-translation') == 'szl': | 
					
						
							|  |  |  |         babel_ext = flask_babel.current_app.extensions['babel'] | 
					
						
							|  |  |  |         return Translations.load(next(babel_ext.translation_directories), 'szl') | 
					
						
							| 
									
										
										
										
											2022-07-08 10:00:20 +02:00
										 |  |  |     if has_request_context() and flask.request.form.get('use-translation') == 'pap': | 
					
						
							|  |  |  |         babel_ext = flask_babel.current_app.extensions['babel'] | 
					
						
							|  |  |  |         return Translations.load(next(babel_ext.translation_directories), 'pap') | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  |     return _flask_babel_get_translations() | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | def get_locale_descr(locale, locale_name): | 
					
						
							| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  |     """Get locale name e.g. 'Français - fr' or 'Português (Brasil) - pt-BR'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :param locale: instance of :py:class:`Locale` | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  |     :param locale_name: name e.g. 'fr'  or 'pt_BR' (delimiter is *underscore*) | 
					
						
							| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     native_language, native_territory = _get_locale_descr(locale, locale_name) | 
					
						
							|  |  |  |     english_language, english_territory = _get_locale_descr(locale, 'en') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |     if native_territory == english_territory: | 
					
						
							|  |  |  |         english_territory = None | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |     if not native_territory and not english_territory: | 
					
						
							|  |  |  |         if native_language == english_language: | 
					
						
							|  |  |  |             return native_language | 
					
						
							|  |  |  |         return native_language + ' (' + english_language + ')' | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |     result = native_language + ', ' + native_territory + ' (' + english_language | 
					
						
							|  |  |  |     if english_territory: | 
					
						
							|  |  |  |         return result + ', ' + english_territory + ')' | 
					
						
							|  |  |  |     return result + ')' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  | def _get_locale_descr(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 locales_initialize(directory=None): | 
					
						
							|  |  |  |     """Initialize locales environment of the SearXNG session.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 16:31:41 +02:00
										 |  |  |     - monkey patch :py:obj:`flask_babel.get_translations` by :py:obj:`get_translations` | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  |     - init global names :py:obj:`LOCALE_NAMES`, :py:obj:`RTL_LOCALES` | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     directory = directory or pathlib.Path(__file__).parent / 'translations' | 
					
						
							|  |  |  |     logger.debug("locales_initialize: %s", directory) | 
					
						
							|  |  |  |     flask_babel.get_translations = get_translations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for tag, descr in ADDITIONAL_TRANSLATIONS.items(): | 
					
						
							|  |  |  |         LOCALE_NAMES[tag] = descr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for tag in LOCALE_BEST_MATCH: | 
					
						
							|  |  |  |         descr = LOCALE_NAMES.get(tag) | 
					
						
							|  |  |  |         if not descr: | 
					
						
							|  |  |  |             locale = Locale.parse(tag, sep='-') | 
					
						
							|  |  |  |             LOCALE_NAMES[tag] = get_locale_descr(locale, tag.replace('-', '_')) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |     for dirname in sorted(os.listdir(directory)): | 
					
						
							|  |  |  |         # Based on https://flask-babel.tkte.ch/_modules/flask_babel.html#Babel.list_translations | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         if not os.path.isdir(os.path.join(directory, dirname, 'LC_MESSAGES')): | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  |         tag = dirname.replace('_', '-') | 
					
						
							|  |  |  |         descr = LOCALE_NAMES.get(tag) | 
					
						
							|  |  |  |         if not descr: | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |             locale = Locale.parse(dirname) | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  |             LOCALE_NAMES[tag] = get_locale_descr(locale, dirname) | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |             if locale.text_direction == 'rtl': | 
					
						
							| 
									
										
										
										
											2022-06-10 17:01:12 +02:00
										 |  |  |                 RTL_LOCALES.add(tag) |