Skip to content
Open
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
16 changes: 8 additions & 8 deletions babel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def __init__(
def default(
cls,
category: str | None = None,
aliases: Mapping[str, str] = LOCALE_ALIASES,
aliases: Mapping[str, str] | None = LOCALE_ALIASES,
) -> Locale:
"""Return the system default locale for the specified category.

Expand All @@ -250,7 +250,7 @@ def default(
- ``LANG``

:param category: one of the ``LC_XXX`` environment variable names
:param aliases: a dictionary of aliases for locale identifiers
:param aliases: a dictionary of aliases for locale identifiers, or `None` to disable it
"""
# XXX: use likely subtag expansion here instead of the
# aliases dictionary.
Expand All @@ -263,7 +263,7 @@ def negotiate(
preferred: Iterable[str],
available: Iterable[str],
sep: str = '_',
aliases: Mapping[str, str] = LOCALE_ALIASES,
aliases: Mapping[str, str] | None = LOCALE_ALIASES,
) -> Locale | None:
"""Find the best match between available and requested locale strings.

Expand All @@ -282,7 +282,7 @@ def negotiate(

:param preferred: the list of locale identifiers preferred by the user
:param available: the list of locale identifiers available
:param aliases: a dictionary of aliases for locale identifiers
:param aliases: a dictionary of aliases for locale identifiers, or `None` to disable it
:param sep: separator for parsing; e.g. Windows tends to use '-' instead of '_'.
"""
identifier = negotiate_locale(preferred, available, sep=sep, aliases=aliases)
Expand Down Expand Up @@ -1154,7 +1154,7 @@ def unit_display_names(self) -> localedata.LocaleDataDict:

def default_locale(
category: str | tuple[str, ...] | list[str] | None = None,
aliases: Mapping[str, str] = LOCALE_ALIASES,
aliases: Mapping[str, str] | None = LOCALE_ALIASES,
) -> str | None:
"""Returns the system default locale for a given category, based on
environment variables.
Expand All @@ -1180,7 +1180,7 @@ def default_locale(
- ``LANG``

:param category: one or more of the ``LC_XXX`` environment variable names
:param aliases: a dictionary of aliases for locale identifiers
:param aliases: a dictionary of aliases for locale identifiers, or `None` to disable it
"""

varnames = ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def negotiate_locale(
preferred: Iterable[str],
available: Iterable[str],
sep: str = '_',
aliases: Mapping[str, str] = LOCALE_ALIASES,
aliases: Mapping[str, str] | None = LOCALE_ALIASES,
) -> str | None:
"""Find the best match between available and requested locale strings.

Expand Down Expand Up @@ -1261,7 +1261,7 @@ def negotiate_locale(
:param available: the list of locale strings available
:param sep: character that separates the different parts of the locale
strings
:param aliases: a dictionary of aliases for locale identifiers
:param aliases: a dictionary of aliases for locale identifiers, or `None` to disable it
"""
available = [a.lower() for a in available if a]
for locale in preferred:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ def test_negotiate_locale():
assert core.negotiate_locale(['no', 'sv'], ['nb_NO', 'sv_SE']) == 'nb_NO'


def test_locale_aliases_can_be_disabled(monkeypatch):
monkeypatch.setenv('LC_MESSAGES', 'en')
assert default_locale('LC_MESSAGES') == 'en_US'
assert default_locale('LC_MESSAGES', aliases=None) == 'en'
assert Locale.default('LC_MESSAGES', aliases=None) == Locale('en')

assert core.negotiate_locale(['en'], ['en_US']) == 'en_US'
assert core.negotiate_locale(['en'], ['en_US'], aliases=None) is None
assert Locale.negotiate(['en'], ['en_US'], aliases=None) is None


def test_parse_locale():
assert core.parse_locale('zh_CN') == ('zh', 'CN', None, None)
assert core.parse_locale('zh_Hans_CN') == ('zh', 'CN', 'Hans', None)
Expand Down