]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Allow falling back to modifier-less locale data when modified data is missing (#1104)
authorAarni Koskela <akx@iki.fi>
Thu, 25 Jul 2024 09:38:57 +0000 (12:38 +0300)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2024 09:38:57 +0000 (12:38 +0300)
IOW, e.g. the data loaded by `ja_JP@mod` is `ja_JP` in the absence of data that would have the modifier present.

Fixes #1089

babel/core.py
tests/test_dates.py

index 56a4ee503674c40b97cc82c16106eff946b5a722..a2e1e1de507d94ebfd733246ead134fdedd5d780 100644 (file)
@@ -201,7 +201,11 @@ class Locale:
 
         identifier = str(self)
         identifier_without_modifier = identifier.partition('@')[0]
-        if not localedata.exists(identifier_without_modifier):
+        if localedata.exists(identifier):
+            self.__data_identifier = identifier
+        elif localedata.exists(identifier_without_modifier):
+            self.__data_identifier = identifier_without_modifier
+        else:
             raise UnknownLocaleError(identifier)
 
     @classmethod
@@ -436,7 +440,7 @@ class Locale:
     @property
     def _data(self) -> localedata.LocaleDataDict:
         if self.__data is None:
-            self.__data = localedata.LocaleDataDict(localedata.load(str(self)))
+            self.__data = localedata.LocaleDataDict(localedata.load(self.__data_identifier))
         return self.__data
 
     def get_display_name(self, locale: Locale | str | None = None) -> str | None:
index fb90131438271e325dc45f921e4ead08f99bb182..0e0c97d89b30aefe9da0c946220cccba17e937b2 100644 (file)
@@ -751,3 +751,8 @@ def test_issue_892():
     assert dates.format_timedelta(timedelta(days=1), format='narrow', locale='pt_BR') == '1 dia'
     assert dates.format_timedelta(timedelta(days=30), format='narrow', locale='pt_BR') == '1 mês'
     assert dates.format_timedelta(timedelta(days=365), format='narrow', locale='pt_BR') == '1 ano'
+
+
+def test_issue_1089():
+    assert dates.format_datetime(datetime.utcnow(), locale="ja_JP@mod")
+    assert dates.format_datetime(datetime.utcnow(), locale=Locale.parse("ja_JP@mod"))