Commits

ram...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 5caff48

Fixed #15632 -- Ignore unrelated content in template multi-line comment blocks when looking for tokens that identify comments for translators. Thanks andrew AT ie-grad DOT ru for the report and Claude Paroz for spotting the problem and helping to fix it.

  • Participants
  • Parent commits a427ac8

Comments (0)

Files changed (3)

File django/utils/translation/trans_real.py

     does so by translating the Django translation tags into standard gettext
     function invocations.
     """
-    from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, TOKEN_COMMENT
+    from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK,
+            TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK)
     out = StringIO()
     intrans = False
     inplural = False
     for t in Lexer(src, origin).tokenize():
         if incomment:
             if t.token_type == TOKEN_BLOCK and t.contents == 'endcomment':
-                out.write(' # %s' % ''.join(comment))
+                content = u''.join(comment)
+                translators_comment_start = None
+                for lineno, line in enumerate(content.splitlines(True)):
+                    if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
+                        translators_comment_start = lineno
+                for lineno, line in enumerate(content.splitlines(True)):
+                    if translators_comment_start is not None and lineno >= translators_comment_start:
+                        out.write(u' # %s' % line)
+                    else:
+                        out.write(u' #\n')
                 incomment = False
                 comment = []
             else:

File tests/regressiontests/i18n/commands/extraction.py

         self.assertTrue('This comment should not be extracted' not in po_contents)
         # Comments in templates
         self.assertTrue('#. Translators: Django template comment for translators' in po_contents)
-        self.assertTrue('#. Translators: Django comment block for translators' in po_contents)
+        self.assertTrue("#. Translators: Django comment block for translators\n#. string's meaning unveiled" in po_contents)
+
+        self.assertTrue('#. Translators: One-line translator comment #1' in po_contents)
+        self.assertTrue('#. Translators: Two-line translator comment #1\n#. continued here.' in po_contents)
+
+        self.assertTrue('#. Translators: One-line translator comment #2' in po_contents)
+        self.assertTrue('#. Translators: Two-line translator comment #2\n#. continued here.' in po_contents)
+
+        self.assertTrue('#. Translators: One-line translator comment #3' in po_contents)
+        self.assertTrue('#. Translators: Two-line translator comment #3\n#. continued here.' in po_contents)
+
+        self.assertTrue('#. Translators: One-line translator comment #4' in po_contents)
+        self.assertTrue('#. Translators: Two-line translator comment #4\n#. continued here.' in po_contents)
 
     def test_templatize(self):
         os.chdir(self.test_dir)

File tests/regressiontests/i18n/commands/templates/test.html

 {% load i18n %}
-{% comment %}Translators: Django comment block for translators {% endcomment %}
+{% comment %}Translators: Django comment block for translators
+string's meaning unveiled
+{% endcomment %}
 {% trans "This literal should be included." %}
 {% trans "This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option." %}
 
 {# Translators: Django template comment for translators #}
 <p>{% blocktrans %}I think that 100% is more that 50% of anything.{% endblocktrans %}</p>
 {% blocktrans with 'txt' as obj %}I think that 100% is more that 50% of {{ obj }}.{% endblocktrans %}
+
+{% comment %}Some random comment
+Some random comment
+Translators: One-line translator comment #1
+{% endcomment %}
+{% trans "Translatable literal #1a" %}
+
+{% comment %}Some random comment
+Some random comment
+Translators: Two-line translator comment #1
+continued here.
+{% endcomment %}
+{% trans "Translatable literal #1b" %}
+
+{% comment %}Some random comment
+Translators: One-line translator comment #2
+{% endcomment %}
+{% trans "Translatable literal #2a" %}
+
+{% comment %}Some random comment
+Translators: Two-line translator comment #2
+continued here.
+{% endcomment %}
+{% trans "Translatable literal #2b" %}
+
+{% comment %}
+    Translators: One-line translator comment #3
+{% endcomment %}
+{% trans "Translatable literal #3a" %}
+
+{% comment %}
+Translators: Two-line translator comment #3
+continued here.
+{% endcomment %}
+{% trans "Translatable literal #3b" %}
+
+{% comment %} Translators: One-line translator comment #4{% endcomment %}
+{% trans "Translatable literal #4a" %}
+
+{% comment %}  Translators: Two-line translator comment #4
+continued here.{% endcomment %}
+{% trans "Translatable literal #4b" %}