# HG changeset patch # User cmlenz # Date 1183311927 0 # Node ID 5041d90edf0c18bad3a1250ea6813e3ab576896f # Parent cdb266cd9a197966cfc28f27d632faaefb8a0e8a Correctly write out obsolete messages spanning multiple lines. Fixes #33. diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -235,7 +235,7 @@ .replace('\n', '\\n') \ .replace('\"', '\\"') -def normalize(string, width=76): +def normalize(string, prefix='', width=76): r"""Convert a string into a format that is appropriate for .po files. >>> print normalize('''Say: @@ -255,22 +255,24 @@ " elit, \"\n" :param string: the string to normalize + :param prefix: a string that should be prepended to every line :param width: the maximum line width; use `None`, 0, or a negative number to completely disable line wrapping :return: the normalized string :rtype: `unicode` """ if width and width > 0: + prefixlen = len(prefix) lines = [] for idx, line in enumerate(string.splitlines(True)): - if len(escape(line)) > width: + if len(escape(line)) + prefixlen > width: chunks = WORD_SEP.split(line) chunks.reverse() while chunks: buf = [] size = 2 while chunks: - l = len(escape(chunks[-1])) - 2 + l = len(escape(chunks[-1])) - 2 + prefixlen if size + l < width: buf.append(chunks.pop()) size += l @@ -293,7 +295,7 @@ if lines and not lines[-1]: del lines[-1] lines[-1] += '\n' - return u'""\n' + u'\n'.join([escape(l) for l in lines]) + return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines]) def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, sort_output=False, sort_by_file=False): @@ -329,9 +331,9 @@ :param omit_header: do not include the ``msgid ""`` entry at the top of the output """ - def _normalize(key): - return normalize(key, width=width).encode(catalog.charset, - 'backslashreplace') + def _normalize(key, prefix=''): + return normalize(key, prefix=prefix, width=width) \ + .encode(catalog.charset, 'backslashreplace') def _write(text): if isinstance(text, unicode): @@ -347,14 +349,19 @@ def _write_message(message, prefix=''): if isinstance(message.id, (list, tuple)): - _write('%smsgid %s\n' % (prefix, _normalize(message.id[0]))) - _write('%smsgid_plural %s\n' % (prefix, _normalize(message.id[1]))) + _write('%smsgid %s\n' % (prefix, _normalize(message.id[0], prefix))) + _write('%smsgid_plural %s\n' % ( + prefix, _normalize(message.id[1], prefix) + )) for i, string in enumerate(message.string): - _write('%smsgstr[%d] %s\n' % (prefix, i, - _normalize(message.string[i]))) + _write('%smsgstr[%d] %s\n' % ( + prefix, i, _normalize(message.string[i], prefix) + )) else: - _write('%smsgid %s\n' % (prefix, _normalize(message.id))) - _write('%smsgstr %s\n' % (prefix, _normalize(message.string or ''))) + _write('%smsgid %s\n' % (prefix, _normalize(message.id, prefix))) + _write('%smsgstr %s\n' % ( + prefix, _normalize(message.string or '', prefix) + )) messages = list(catalog) if sort_output: 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 @@ -150,7 +150,7 @@ msgid "bar" msgstr ""''', buf.getvalue().strip()) - def test_po_with_obsolete_messages(self): + def test_po_with_obsolete_message(self): catalog = Catalog() catalog.add(u'foo', u'Voh', locations=[('main.py', 1)]) catalog.obsolete['bar'] = Message(u'bar', u'Bahr', @@ -166,6 +166,34 @@ #~ msgid "bar" #~ msgstr "Bahr"''', buf.getvalue().strip()) + def test_po_with_multiline_obsolete_message(self): + catalog = Catalog() + catalog.add(u'foo', u'Voh', locations=[('main.py', 1)]) + msgid = r"""Here's a message that covers +multiple lines, and should still be handled +correctly. +""" + msgstr = r"""Here's a message that covers +multiple lines, and should still be handled +correctly. +""" + catalog.obsolete[msgid] = Message(msgid, msgstr, + locations=[('utils.py', 3)]) + buf = StringIO() + pofile.write_po(buf, catalog, omit_header=True) + self.assertEqual(r'''#: main.py:1 +msgid "foo" +msgstr "Voh" + +#~ msgid "" +#~ "Here's a message that covers\n" +#~ "multiple lines, and should still be handled\n" +#~ "correctly.\n" +#~ msgstr "" +#~ "Here's a message that covers\n" +#~ "multiple lines, and should still be handled\n" +#~ "correctly.\n"''', buf.getvalue().strip()) + def suite(): suite = unittest.TestSuite()