From 439faab7588d38219dcdcb3528b7472c7341ed6f Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 18 Jul 2024 12:18:10 +0300 Subject: [PATCH] Allow falling back to modifier-less locale data when modified data is missing 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 | 8 ++++++-- tests/test_dates.py | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/babel/core.py b/babel/core.py index 56a4ee50..a2e1e1de 100644 --- a/babel/core.py +++ b/babel/core.py @@ -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: diff --git a/tests/test_dates.py b/tests/test_dates.py index fb901314..0e0c97d8 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -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")) -- 2.47.2