From 96ef8f7fa8f4b939cec1f74b3add24d08a62f570 Mon Sep 17 00:00:00 2001 From: echo666939 <198456500+echo666939@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:32:18 +0800 Subject: [PATCH] messages: Decode extended PO header parameters Decode RFC 2231 values returned by the email parser before consuming the catalog charset and plural forms. Leave ordinary string parameters untouched. Fixes #1533 --- babel/messages/catalog.py | 6 +++++- tests/messages/test_pofile.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 88a303fe8..69a54d7ac 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -354,10 +354,14 @@ class TranslationError(Exception): def parse_separated_header(value: str) -> dict[str, str]: # Adapted from https://peps.python.org/pep-0594/#cgi from email.message import Message + from email.utils import collapse_rfc2231_value m = Message() m['content-type'] = value - return dict(m.get_params()) + return { + key: collapse_rfc2231_value(param) if isinstance(param, tuple) else param + for key, param in m.get_params() + } def _force_text(s: str | bytes, encoding: str = 'utf-8', errors: str = 'strict') -> str: diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index fdc70b36f..1de306ba4 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -51,6 +51,19 @@ def test_wrapped_comments_roundtrip(attribute): assert second.getvalue() == first.getvalue() +def test_read_po_extended_header_parameters(): + po = ( + b'msgid ""\nmsgstr ""\n' + b'"Content-Type: text/plain; charset*=us-ascii\'\'ISO-8859-1\\n"\n' + b'"Plural-Forms: nplurals*=3; plural*=\'\'%28n%20%25%203%29;\\n"\n' + b'\nmsgid "coffee"\nmsgstr "caf\xe9"\n' + ) + catalog = pofile.read_po(BytesIO(po)) + assert catalog.charset == 'iso-8859-1' + assert catalog['coffee'].string == 'café' + assert catalog.plural_forms == 'nplurals=3; plural=(n % 3);' + + def test_enclosed_filenames_in_location_comment(): catalog = Catalog() catalog.add("foo", lineno=2, locations=[("main 1.py", 1)], string="")