# HG changeset patch # User fschwarz # Date 1344121841 0 # Node ID 5c9dba5dd3119f02cd126a8fee86561f58fd73a6 # Parent e0454b9c125ce868aa93c6238f3ac54793529d10 handle irregular multi-line msgstr (no "" as first line) gracefully (#171) diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -43,7 +43,8 @@ with more than 7 significant digits (#183) * fix format_date() with datetime parameter (#282, patch from Xavier Morel) * fix format_decimal() with small Decimal values (#214, patch from George Lund) - * fix handling of messages containing '\\n' (#171) + * fix handling of messages containing '\\n' (#198) + * handle irregular multi-line msgstr (no "" as first line) gracefully (#171) Version 0.9.6 diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -75,10 +75,11 @@ :return: the denormalized string :rtype: `unicode` or `str` """ - if string.startswith('""'): - lines = [] - for line in string.splitlines()[1:]: - lines.append(unescape(line)) + if '\n' in string: + escaped_lines = string.splitlines() + if string.startswith('""'): + escaped_lines = escaped_lines[1:] + lines = map(unescape, escaped_lines) return ''.join(lines) else: return unescape(string) @@ -110,10 +111,10 @@ ... print (message.id, message.string) ... print ' ', (message.locations, message.flags) ... print ' ', (message.user_comments, message.auto_comments) - (u'foo %(name)s', '') + (u'foo %(name)s', u'') ([(u'main.py', 1)], set([u'fuzzy', u'python-format'])) ([], []) - ((u'bar', u'baz'), ('', '')) + ((u'bar', u'baz'), (u'', u'')) ([(u'main.py', 3)], set([])) ([u'A user comment'], [u'An auto comment']) diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -539,6 +539,16 @@ # regression test for #198 self.assertEqual(r'\n', pofile.unescape(r'"\\n"')) + def test_denormalize_on_msgstr_without_empty_first_line(self): + # handle irregular multi-line msgstr (no "" as first line) + # gracefully (#171) + msgstr = '"multi-line\\n"\n" translation"' + expected_denormalized = u'multi-line\n translation' + + self.assertEqual(expected_denormalized, pofile.denormalize(msgstr)) + self.assertEqual(expected_denormalized, + pofile.denormalize('""\n' + msgstr)) + def suite(): suite = unittest.TestSuite()