From 09012839233f0e6b57cd590251797b71b3127f26 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Fri, 22 May 2020 21:01:11 +0000 Subject: [PATCH] Add Babel aliases to get_available_languages This pulls in the alias mapping from Babel[0] that previously made some two character locale names available. It simply appends each alias that is found by gettext.find to the list of available languages. This change in behavior was initially reported as a result of a breaking Glance unit test. After looking a bit closer at the test in question, it appears they mock out gettext.find to assume the existence of that locale. Whether it would actually exist in a real world deployment I have no idea. However, I don't think it hurts anything to include these aliases in the list checked by get_available_languages so this should be fine. Note that I did no mapping of the aliases in our override of gettext.find. There was no aliasing going on there for these two letter locales before either, so if they were found before they will be found now. The same is true if they were not found before, so this maintains the previous behavior. Change-Id: Ie57ed2ca0228634ed9a3def99bf606ce1832a195 0: https://github.com/python-babel/babel/blob/e7e4265d9a037ac38bba99f8513fb9e48a1081ba/babel/core.py#L80 Closes-Bug: 1877386 --- oslo_i18n/_gettextutils.py | 16 ++++++++++++++++ oslo_i18n/tests/test_gettextutils.py | 11 +++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/oslo_i18n/_gettextutils.py b/oslo_i18n/_gettextutils.py index bc6946f..9872a9c 100644 --- a/oslo_i18n/_gettextutils.py +++ b/oslo_i18n/_gettextutils.py @@ -50,6 +50,19 @@ def install(domain): _AVAILABLE_LANGUAGES = {} +# Copied from Babel so anyone using aliases that were previously provided by +# the Babel implementation of get_available_languages continues to work. These +# are not recommended for use in new code. +_BABEL_ALIASES = { + 'ar': 'ar_SY', 'bg': 'bg_BG', 'bs': 'bs_BA', 'ca': 'ca_ES', 'cs': 'cs_CZ', + 'da': 'da_DK', 'de': 'de_DE', 'el': 'el_GR', 'en': 'en_US', 'es': 'es_ES', + 'et': 'et_EE', 'fa': 'fa_IR', 'fi': 'fi_FI', 'fr': 'fr_FR', 'gl': 'gl_ES', + 'he': 'he_IL', 'hu': 'hu_HU', 'id': 'id_ID', 'is': 'is_IS', 'it': 'it_IT', + 'ja': 'ja_JP', 'km': 'km_KH', 'ko': 'ko_KR', 'lt': 'lt_LT', 'lv': 'lv_LV', + 'mk': 'mk_MK', 'nl': 'nl_NL', 'nn': 'nn_NO', 'no': 'nb_NO', 'pl': 'pl_PL', + 'pt': 'pt_PT', 'ro': 'ro_RO', 'ru': 'ru_RU', 'sk': 'sk_SK', 'sl': 'sl_SI', + 'sv': 'sv_SE', 'th': 'th_TH', 'tr': 'tr_TR', 'uk': 'uk_UA' +} def get_available_languages(domain): @@ -72,6 +85,9 @@ def get_available_languages(domain): language_list.extend( language for language in locale_identifiers if find(language) ) + language_list.extend( + alias for alias, _ in _BABEL_ALIASES.items() if find(alias) + ) _AVAILABLE_LANGUAGES[domain] = language_list return copy.copy(language_list) diff --git a/oslo_i18n/tests/test_gettextutils.py b/oslo_i18n/tests/test_gettextutils.py index 49f95e1..4e0999b 100644 --- a/oslo_i18n/tests/test_gettextutils.py +++ b/oslo_i18n/tests/test_gettextutils.py @@ -78,7 +78,8 @@ class GettextTest(test_base.BaseTestCase): def _mock_gettext_find(domain, localedir=None, languages=None, all=0): languages = languages or [] if domain == 'domain_1': - if any(x in ['en_GB', 'es_ES', 'fil_PH'] for x in languages): + if any(x in ['en_GB', 'es_ES', 'fil_PH', 'it'] + for x in languages): return 'translation-file' elif domain == 'domain_2': if any(x in ['fr_FR', 'zh_HK'] for x in languages): @@ -101,12 +102,14 @@ class GettextTest(test_base.BaseTestCase): self.assertEqual('en_US', domain_1_languages[0]) self.assertEqual('en_US', domain_2_languages[0]) - self.assertEqual(4, len(domain_1_languages), domain_1_languages) + self.assertEqual(5, len(domain_1_languages), domain_1_languages) self.assertEqual( - {'en_US', 'fil_PH', 'en_GB', 'es_ES'}, set(domain_1_languages), + {'en_US', 'fil_PH', 'en_GB', 'es_ES', 'it'}, + set(domain_1_languages), ) self.assertEqual(3, len(domain_2_languages), domain_2_languages) - self.assertEqual({'en_US', 'fr_FR', 'zh_HK'}, set(domain_2_languages)) + self.assertEqual({'en_US', 'fr_FR', 'zh_HK'}, + set(domain_2_languages)) self.assertEqual(2, len(_gettextutils._AVAILABLE_LANGUAGES)) # Now test an unknown domain, only en_US should be included