| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LOCALE_NAMES = { | 
					
						
							| 
									
										
										
										
											2021-08-04 09:50:34 +02:00
										 |  |  |     "oc": "Occitan", | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  |     "nl-BE": "Vlaams (Dutch, Belgium)", | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  | """Mapping of locales and their description.  Locales e.g. 'fr' or 'pt-BR'
 | 
					
						
							|  |  |  | (delimiter is *underline* '-')"""
 | 
					
						
							| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | RTL_LOCALES: Set[str] = set() | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  | """List of *Right-To-Left* locales e.g. 'he' or 'fa-IR' (delimiter is
 | 
					
						
							|  |  |  | *underline* '-')"""
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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): | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |     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): | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  |     """Initialize global names :py:obj:`LOCALE_NAMES`, :py:obj:`RTL_LOCALES`.
 | 
					
						
							| 
									
										
										
										
											2021-08-03 18:17:23 +02:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											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-08-03 18:17:23 +02:00
										 |  |  |         if not os.path.isdir( os.path.join(directory, dirname, 'LC_MESSAGES') ): | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  |         locale_name = dirname.replace('_', '-') | 
					
						
							|  |  |  |         info = LOCALE_NAMES.get(locale_name) | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |         if not info: | 
					
						
							|  |  |  |             locale = Locale.parse(dirname) | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  |             LOCALE_NAMES[locale_name] = _get_locale_name(locale, dirname) | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |             if locale.text_direction == 'rtl': | 
					
						
							| 
									
										
										
										
											2021-10-06 10:26:40 +02:00
										 |  |  |                 RTL_LOCALES.add(locale_name) | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | initialize_locales(pathlib.Path(__file__).parent / 'translations') |