python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.34k stars 448 forks source link

`read_mo` does not decode `msgctxt` #1147

Open tomasr8 opened 3 weeks ago

tomasr8 commented 3 weeks ago

When reading an MO file, the message context is kept as bytes unlike the msgid and msgstr which are decoded to utf8. This is surprising because in order to use for example Catalog.get, one must pass the context encoded as bytes. I wonder if this is intentional or just an omission?

Example: When I read an MO file compiled from this PO file:

msgctxt "fooctxt"
msgid "foo"
msgstr "bar"

The message key is ('foo', b'fooctxt') (notice the that fooctxt is a bytes object):

>>> f = open('messages.mo', 'rb')
>>> cat = read_mo(f)
>>> cat._messages
OrderedDict({('foo', b'fooctxt'): <Message 'foo' (flags: [])>})