changeset 585:5c9dba5dd311 trunk

handle irregular multi-line msgstr (no "" as first line) gracefully (#171)
author fschwarz
date Sat, 04 Aug 2012 23:10:41 +0000
parents e0454b9c125c
children 46410022772a
files ChangeLog babel/messages/pofile.py babel/messages/tests/pofile.py
diffstat 3 files changed, 19 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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'])
 
--- 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()
Copyright (C) 2012-2017 Edgewall Software