# HG changeset patch # User palgarvio # Date 1181606394 0 # Node ID f008662b5d6e184ba1c29643ef9cb2ced6ddc211 # Parent ccb9da614597161d2d2e644f195e7ed2b2f6ce9a Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message. diff --git a/babel/messages/extract.py b/babel/messages/extract.py --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -295,11 +295,12 @@ 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 @@ -308,7 +309,7 @@ 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 == ')': @@ -321,7 +322,14 @@ 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 = [] diff --git a/babel/messages/tests/extract.py b/babel/messages/tests/extract.py --- a/babel/messages/tests/extract.py +++ b/babel/messages/tests/extract.py @@ -69,7 +69,7 @@ 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 @@ -86,7 +86,56 @@ '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))