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
6 changes: 5 additions & 1 deletion babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="")
Expand Down