Commits

Pedro Algarvio committed f008662

Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.

Comments (0)

Files changed (2)

babel/messages/extract.py

         elif tok == COMMENT:
             # Strip the comment token from the line
             value = value[1:].strip()
-            if in_translator_comments is True:
+            if in_translator_comments is True and \
+                    translator_comments[-1][0] == lineno - 1:
                 # We're already inside a translator comment, continue appending
                 # XXX: Should we check if the programmer keeps adding the
                 # comment_tag for every comment line??? probably not!
-                translator_comments.append(value)
+                translator_comments.append((lineno, value))
                 continue
             # If execution reaches this point, let's see if comment line
             # starts with one of the comment tags
                     if in_translator_comments is not True:
                         in_translator_comments = True
                     comment = value[len(comment_tag):].strip()
-                    translator_comments.append(comment)
+                    translator_comments.append((lineno, comment))
                     break
         elif funcname and in_args:
             if tok == OP and value == ')':
                         messages = tuple(messages)
                     else:
                         messages = messages[0]
-                    yield lineno, funcname, messages, translator_comments
+                    # Comments don't apply unless they immediately preceed the
+                    # message
+                    if translator_comments and \
+                            translator_comments[-1][0] < lineno - 1:
+                        translator_comments = []
+
+                    yield (lineno, funcname, messages,
+                           [comment[1] for comment in translator_comments])
                 funcname = lineno = None
                 messages = []
                 translator_comments = []

babel/messages/tests/extract.py

         messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
         self.assertEqual('Foo Bar', messages[0][2])
         self.assertEqual(['This one will be'], messages[0][3])
-        
+
     def test_multiple_comment_tags(self):
         buf = StringIO("""
 # NOTE1: A translation comment for tag1
                           'with a second line'], messages[0][3])
         self.assertEqual('Foo Bar2', messages[1][2])
         self.assertEqual(['A translation comment for tag2'], messages[1][3])
+
+    def test_two_succeeding_comments(self):
+        buf = StringIO("""
+# NOTE: one
+# NOTE: two
+msg = _(u'Foo Bar')
+""")
+        messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
+        self.assertEqual('Foo Bar', messages[0][2])
+        self.assertEqual(['one', 'NOTE: two'], messages[0][3])
         
+    def test_invalid_translator_comments(self):
+        buf = StringIO("""
+# NOTE: this shouldn't apply to any messages
+hello = 'there'
+
+msg = _(u'Foo Bar')
+""")
+        messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
+        self.assertEqual('Foo Bar', messages[0][2])
+        self.assertEqual([], messages[0][3])
+
+    def test_invalid_translator_comments2(self):
+        buf = StringIO("""
+# NOTE: Hi!
+hithere = _('Hi there!')
+
+# NOTE: you should not be seeing this in the .po
+rows = [[v for v in range(0,10)] for row in range(0,10)]
+
+# this (NOTE:) should not show up either
+hello = _('Hello')
+""")
+        messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
+        self.assertEqual('Hi there!', messages[0][2])
+        self.assertEqual(['Hi!'], messages[0][3])
+        self.assertEqual('Hello', messages[1][2])
+        self.assertEqual([], messages[1][3])
+
+    def test_invalid_translator_comments3(self):
+        buf = StringIO("""
+# NOTE: Hi,
+
+# there!
+hithere = _('Hi there!')
+""")
+        messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
+        self.assertEqual('Hi there!', messages[0][2])
+        self.assertEqual([], messages[0][3])
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(extract))