Skip to content

Commit

Permalink
Allow falling back to modifier-less locale data when modified data is…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
akx committed Jul 18, 2024
1 parent 2f87363 commit 439faab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions babel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ def __init__(

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
Expand Down Expand Up @@ -436,7 +440,7 @@ def __str__(self) -> str:
@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:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

0 comments on commit 439faab

Please sign in to comment.