diff options
author | Michael Birtwell <michael.birtwell@starleaf.com> | 2016-07-08 14:33:12 +0100 |
---|---|---|
committer | Michael Birtwell <michael.birtwell@starleaf.com> | 2016-07-08 14:33:12 +0100 |
commit | 832e58ab0c564aa50ff93b7680c12f524c5948bc (patch) | |
tree | 16f812e78cfdce92a041b8f408dba92431df7277 | |
parent | 27a40c249a636cfdad52938610582eee84555616 (diff) | |
download | babel-832e58ab0c564aa50ff93b7680c12f524c5948bc.tar.gz |
read_po: Support multiline obsolete units
-rw-r--r-- | babel/messages/pofile.py | 55 | ||||
-rw-r--r-- | tests/messages/test_pofile.py | 42 |
2 files changed, 72 insertions, 25 deletions
diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 194e961..bb07b4b 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -164,6 +164,34 @@ class _PoFileParser(object): elif self.in_msgctxt: self.context.append(line.rstrip()) + def _process_comment(self, line): + + self.in_msgid = self.in_msgstr = False + if self.messages and self.translations: + self._add_message() + if line[1:].startswith(':'): + for location in line[2:].lstrip().split(): + pos = location.rfind(':') + if pos >= 0: + try: + lineno = int(location[pos + 1:]) + except ValueError: + continue + self.locations.append((location[:pos], lineno)) + else: + self.locations.append((location, None)) + elif line[1:].startswith(','): + for flag in line[2:].lstrip().split(','): + self.flags.append(flag.strip()) + elif line[1:].startswith('.'): + # These are called auto-comments + comment = line[2:].strip() + if comment: # Just check that we're not adding empty comments + self.auto_comments.append(comment) + else: + # These are called user comments + self.user_comments.append(line[1:].strip()) + def parse(self, fileobj): for lineno, line in enumerate(fileobj.readlines()): @@ -171,34 +199,11 @@ class _PoFileParser(object): if not isinstance(line, text_type): line = line.decode(self.catalog.charset) if line.startswith('#'): - self.in_msgid = self.in_msgstr = False - if self.messages and self.translations: - self._add_message() - if line[1:].startswith(':'): - for location in line[2:].lstrip().split(): - pos = location.rfind(':') - if pos >= 0: - try: - lineno = int(location[pos + 1:]) - except ValueError: - continue - self.locations.append((location[:pos], lineno)) - else: - self.locations.append((location, None)) - elif line[1:].startswith(','): - for flag in line[2:].lstrip().split(','): - self.flags.append(flag.strip()) - elif line[1:].startswith('~'): + if line[1:].startswith('~'): self.obsolete = True self._process_message_line(lineno, line[2:].lstrip()) - elif line[1:].startswith('.'): - # These are called auto-comments - comment = line[2:].strip() - if comment: # Just check that we're not adding empty comments - self.auto_comments.append(comment) else: - # These are called user comments - self.user_comments.append(line[1:].strip()) + self._process_comment(line) else: self._process_message_line(lineno, line) diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 1bac360..a271a84 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -164,6 +164,48 @@ msgstr "Bahr" self.assertEqual(1, len(catalog)) self.assertEqual(0, len(catalog.obsolete)) + def test_multi_line_obsolete_message(self): + buf = StringIO(r'''# This is an obsolete message +#~ msgid "" +#~ "foo" +#~ "foo" +#~ msgstr "" +#~ "Voh" +#~ "Vooooh" + +# This message is not obsolete +#: main.py:1 +msgid "bar" +msgstr "Bahr" +''') + catalog = pofile.read_po(buf) + self.assertEqual(1, len(catalog.obsolete)) + message = catalog.obsolete[u'foofoo'] + self.assertEqual(u'foofoo', message.id) + self.assertEqual(u'VohVooooh', message.string) + self.assertEqual(['This is an obsolete message'], message.user_comments) + + def test_unit_following_multi_line_obsolete_message(self): + buf = StringIO(r'''# This is an obsolete message +#~ msgid "" +#~ "foo" +#~ "fooooooo" +#~ msgstr "" +#~ "Voh" +#~ "Vooooh" + +# This message is not obsolete +#: main.py:1 +msgid "bar" +msgstr "Bahr" +''') + catalog = pofile.read_po(buf) + self.assertEqual(1, len(catalog)) + message = catalog[u'bar'] + self.assertEqual(u'bar', message.id) + self.assertEqual(u'Bahr', message.string) + self.assertEqual(['This message is not obsolete'], message.user_comments) + def test_with_context(self): buf = BytesIO(b'''# Some string in the menu #: main.py:1 |