Skip to content

Commit 42d793c

Browse files
authored
Allow falling back to modifier-less locale data when modified data is missing (#1104)
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
1 parent 32f41c2 commit 42d793c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

babel/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ def __init__(
201201

202202
identifier = str(self)
203203
identifier_without_modifier = identifier.partition('@')[0]
204-
if not localedata.exists(identifier_without_modifier):
204+
if localedata.exists(identifier):
205+
self.__data_identifier = identifier
206+
elif localedata.exists(identifier_without_modifier):
207+
self.__data_identifier = identifier_without_modifier
208+
else:
205209
raise UnknownLocaleError(identifier)
206210

207211
@classmethod
@@ -436,7 +440,7 @@ def __str__(self) -> str:
436440
@property
437441
def _data(self) -> localedata.LocaleDataDict:
438442
if self.__data is None:
439-
self.__data = localedata.LocaleDataDict(localedata.load(str(self)))
443+
self.__data = localedata.LocaleDataDict(localedata.load(self.__data_identifier))
440444
return self.__data
441445

442446
def get_display_name(self, locale: Locale | str | None = None) -> str | None:

tests/test_dates.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,8 @@ def test_issue_892():
751751
assert dates.format_timedelta(timedelta(days=1), format='narrow', locale='pt_BR') == '1 dia'
752752
assert dates.format_timedelta(timedelta(days=30), format='narrow', locale='pt_BR') == '1 mês'
753753
assert dates.format_timedelta(timedelta(days=365), format='narrow', locale='pt_BR') == '1 ano'
754+
755+
756+
def test_issue_1089():
757+
assert dates.format_datetime(datetime.utcnow(), locale="ja_JP@mod")
758+
assert dates.format_datetime(datetime.utcnow(), locale=Locale.parse("ja_JP@mod"))

0 commit comments

Comments
 (0)