From bd842dc367730c13465e0d99440e30b4a3593b94 Mon Sep 17 00:00:00 2001 From: arod19 <23423093+arod19@users.noreply.github.com> Date: Fri, 4 Dec 2026 14:51:49 -0500 Subject: [PATCH] Allow None for Babel locale alias mappings Co-Authored-By: Codex --- babel/core.py | 16 ++++++++-------- tests/test_core.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/babel/core.py b/babel/core.py index ed271f97c..0fb76f482 100644 --- a/babel/core.py +++ b/babel/core.py @@ -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. @@ -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. @@ -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. @@ -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) @@ -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. @@ -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') @@ -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. @@ -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: diff --git a/tests/test_core.py b/tests/test_core.py index 2595d1f27..66081ebc8 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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)