summaryrefslogtreecommitdiff
path: root/babel/messages
diff options
context:
space:
mode:
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>2011-03-19 19:34:40 +0000
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>2011-03-19 19:34:40 +0000
commit77bd66d325ec00c639540a7422ee4eb52d1be21d (patch)
tree6e31b06189bb0f0dab6e8422ca5fd52143a3e115 /babel/messages
parentb6d95780538ba947bdf324d5f83e54a76b51d01c (diff)
downloadbabel-77bd66d325ec00c639540a7422ee4eb52d1be21d.tar.gz
Catalog class should not do decoding of input strings (fixes #256)
Diffstat (limited to 'babel/messages')
-rw-r--r--babel/messages/catalog.py25
-rw-r--r--babel/messages/tests/pofile.py30
2 files changed, 45 insertions, 10 deletions
diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py
index be6b1c8..ffd22be 100644
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -324,14 +324,7 @@ class Catalog(object):
def _set_mime_headers(self, headers):
for name, value in headers:
- if name.lower() == 'content-type':
- mimetype, params = parse_header(value)
- if 'charset' in params:
- self.charset = params['charset'].lower()
- break
- for name, value in headers:
- name = name.lower().decode(self.charset)
- value = value.decode(self.charset)
+ name = name.lower()
if name == 'project-id-version':
parts = value.split(' ')
self.project = u' '.join(parts[:-1])
@@ -342,6 +335,10 @@ class Catalog(object):
self.last_translator = value
elif name == 'language-team':
self.language_team = value
+ elif name == 'content-type':
+ mimetype, params = parse_header(value)
+ if 'charset' in params:
+ self.charset = params['charset'].lower()
elif name == 'plural-forms':
_, params = parse_header(' ;' + value)
self._num_plurals = int(params.get('nplurals', 2))
@@ -590,8 +587,16 @@ class Catalog(object):
message = current
elif id == '':
# special treatment for the header message
- headers = message_from_string(message.string.encode(self.charset))
- self.mime_headers = headers.items()
+ def _parse_header(header_string):
+ # message_from_string only works for str, not for unicode
+ headers = message_from_string(header_string.encode('utf8'))
+ decoded_headers = {}
+ for name, value in headers.items():
+ name = name.decode('utf8')
+ value = value.decode('utf8')
+ decoded_headers[name] = value
+ return decoded_headers
+ self.mime_headers = _parse_header(message.string).items()
self.header_comment = '\n'.join(['# %s' % comment for comment
in message.user_comments])
self.fuzzy = message.fuzzy
diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py
index 348fcf6..698d2ca 100644
--- a/babel/messages/tests/pofile.py
+++ b/babel/messages/tests/pofile.py
@@ -35,6 +35,27 @@ msgstr "Voh"''')
catalog = pofile.read_po(buf, domain='mydomain')
self.assertEqual('mydomain', catalog.domain)
+ def test_applies_specified_encoding_during_read(self):
+ buf = StringIO(u'''
+msgid ""
+msgstr ""
+"Project-Id-Version: 3.15\\n"
+"Report-Msgid-Bugs-To: Fliegender Zirkus <fliegender@zirkus.de>\\n"
+"POT-Creation-Date: 2007-09-27 11:19+0700\\n"
+"PO-Revision-Date: 2007-09-27 21:42-0700\\n"
+"Last-Translator: John <cleese@bavaria.de>\\n"
+"Language-Team: German Lang <de@babel.org>\\n"
+"Plural-Forms: nplurals=2; plural=(n != 1)\\n"
+"MIME-Version: 1.0\\n"
+"Content-Type: text/plain; charset=iso-8859-1\\n"
+"Content-Transfer-Encoding: 8bit\\n"
+"Generated-By: Babel 1.0dev-r313\\n"
+
+msgid "foo"
+msgstr "bär"'''.encode('iso-8859-1'))
+ catalog = pofile.read_po(buf, locale='de_DE')
+ self.assertEqual(u'bär', catalog.get('foo').string)
+
def test_read_multiline(self):
buf = StringIO(r'''msgid ""
"Here's some text that\n"
@@ -248,6 +269,15 @@ class WritePoTestCase(unittest.TestCase):
msgid "foo"
msgstr ""''', buf.getvalue().strip())
+ def test_write_po_file_with_specified_charset(self):
+ catalog = Catalog(charset='iso-8859-1')
+ catalog.add('foo', u'äöü', locations=[('main.py', 1)])
+ buf = StringIO()
+ pofile.write_po(buf, catalog, omit_header=False)
+ po_file = buf.getvalue().strip()
+ assert r'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file
+ assert u'msgstr "äöü"'.encode('iso-8859-1') in po_file
+
def test_duplicate_comments(self):
catalog = Catalog()
catalog.add(u'foo', auto_comments=['A comment'])