# HG changeset patch # User cmlenz # Date 1182371484 0 # Node ID ba5150e9544e63eb74a867f832e022dc22d55802 # Parent 37f4875bad88b5266607ed12b0c9b04568865570 Respect charset specified in PO headers in `read_po()`. Fixes #17. diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -13,6 +13,7 @@ """Data structures for message catalogs.""" +from cgi import parse_header from datetime import datetime from email import message_from_string import re @@ -37,7 +38,7 @@ class Message(object): """Representation of a single message in a catalog.""" - def __init__(self, id, string='', locations=(), flags=(), auto_comments=(), + def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(), user_comments=()): """Create the message object. @@ -263,6 +264,10 @@ int(tzoffset[2:])) dt = datetime.fromtimestamp(ts) self.creation_date = dt.replace(tzinfo=tzoffset) + elif name == 'content-type': + mimetype, params = parse_header(value) + if 'charset' in params: + self.charset = params['charset'].lower() mime_headers = property(_get_mime_headers, _set_mime_headers, doc="""\ The MIME headers of the catalog, used for the special ``msgid ""`` entry. diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -60,12 +60,12 @@ ... print (message.id, message.string) ... print ' ', (message.locations, message.flags) ... print ' ', (message.user_comments, message.auto_comments) - ('foo %(name)s', '') - ([('main.py', 1)], set(['fuzzy', 'python-format'])) + (u'foo %(name)s', '') + ([(u'main.py', 1)], set([u'fuzzy', u'python-format'])) ([], []) - (('bar', 'baz'), ('', '')) - ([('main.py', 3)], set([])) - (['A user comment'], ['An auto comment']) + ((u'bar', u'baz'), ('', '')) + ([(u'main.py', 3)], set([])) + ([u'A user comment'], [u'An auto comment']) :param fileobj: the file-like object to read the PO file from :return: an iterator over ``(message, translation, location)`` tuples @@ -97,7 +97,7 @@ del flags[:]; del auto_comments[:]; del user_comments[:] for line in fileobj.readlines(): - line = line.strip() + line = line.strip().decode(catalog.charset) if line.startswith('#'): in_msgid = in_msgstr = False if messages: