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
4 changes: 4 additions & 0 deletions babel/messages/mofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def read_mo(fileobj: SupportsRead[bytes]) -> Catalog:
elif lastkey:
headers[lastkey] += b'\n' + item

# 2026-10-10: Decode metadata using its declared charset.

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.

Suggested change
# 2026-10-10: Decode metadata using its declared charset.
# Decode metadata using its declared charset.

if b'content-type' in headers:
catalog.mime_headers = [(b'content-type', headers[b'content-type'])]

ctxt: str | None = None
msg: list[str] | str
tmsg: list[str] | str
Expand Down
19 changes: 19 additions & 0 deletions tests/messages/test_mofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,22 @@ def test_read_mo_decodes_message_context() -> None:
assert message.context == 'ほげ'
assert message.string == 'åäöÅÄÖ'
assert not catalog.get('x') # Not without the context


def test_read_mo_decodes_non_ascii_headers():
# 2026-10-10: The declared charset also applies to non-ASCII metadata.
for charset in ['utf-8', 'iso-8859-1', 'cp1252']:
catalog = Catalog(
charset=charset,
project='Caf\u00e9',
last_translator='Ren\u00e9 <[email protected]>',
)
catalog.add('caf\u00e9', 'ol\u00e9', context='m\u00e9ta')
buf = BytesIO()
mofile.write_mo(buf, catalog)
buf.seek(0)
restored = mofile.read_mo(buf)
assert restored.charset == charset
assert restored.project == 'Caf\u00e9'
assert restored.last_translator == 'Ren\u00e9 <[email protected]>'
assert restored.get('caf\u00e9', context='m\u00e9ta').string == 'ol\u00e9'
Comment on lines +112 to +125

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.

Please use the actual character instead of \u00e9 here.