Skip to content

Accept C and POSIX in Locale.parse - #1340

Open
gyanu2507 wants to merge 1 commit into
python-babel:masterfrom
gyanu2507:fix/parse-c-posix-locale
Open

gyanu2507 wants to merge 1 commit into
python-babel:masterfrom
gyanu2507:fix/parse-c-posix-locale

Conversation

@gyanu2507

@gyanu2507 gyanu2507 commented Sep 10, 2026

Copy link
Copy Markdown

Fixes #1236.

default_locale() maps C, POSIX, and C.UTF-8 to en_US_POSIX. Locale.parse() did not, so Locale.parse("C") raised UnknownLocaleError: unknown locale 'c'. That is the frame in the report.

parse() now applies the same mapping (case-insensitive, optional encoding suffix) before parse_locale. en_US_POSIX with a POSIX variant is unchanged.

default_locale already maps those to en_US_POSIX. parse() did not, so a
C locale from the environment raised UnknownLocaleError.
Comment thread babel/core.py

# C/POSIX is not a CLDR language. Same mapping default_locale uses.
posix_stem = identifier.split(".")[0].split("@")[0]
posix_stem = posix_stem.replace("-", "_")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this replace necessary? "C" and "POSIX" don't include dashes or underscores.

Comment thread babel/core.py
raise TypeError(f"Unexpected value for identifier: {identifier!r}")

# C/POSIX is not a CLDR language. Same mapping default_locale uses.
posix_stem = identifier.split(".")[0].split("@")[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the @ splitting for? The test case doesn't seem to exercise it, and in any case a @ modifier would be in the postfix already split out by .split(".").

Additionally, please use .partition(".") in cases like this for performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Babel fails to handle C locale properly

2 participants