Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow falling back to modifier-less locale data #1104

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))