Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15632 -- Ignore unrelated content in template multi-line comme…

…nt 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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15882 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 775a6e694fedc6b057dc560da8e4a07dc7e9457c 1 parent 1a6d98d
Ramiro Morales authored March 19, 2011
14  django/utils/translation/trans_real.py
@@ -435,7 +435,8 @@ def templatize(src, origin=None):
435 435
     does so by translating the Django translation tags into standard gettext
436 436
     function invocations.
437 437
     """
438  
-    from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, TOKEN_COMMENT
  438
+    from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK,
  439
+            TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK)
439 440
     out = StringIO()
440 441
     intrans = False
441 442
     inplural = False
@@ -446,7 +447,16 @@ def templatize(src, origin=None):
446 447
     for t in Lexer(src, origin).tokenize():
447 448
         if incomment:
448 449
             if t.token_type == TOKEN_BLOCK and t.contents == 'endcomment':
449  
-                out.write(' # %s' % ''.join(comment))
  450
+                content = u''.join(comment)
  451
+                translators_comment_start = None
  452
+                for lineno, line in enumerate(content.splitlines(True)):
  453
+                    if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
  454
+                        translators_comment_start = lineno
  455
+                for lineno, line in enumerate(content.splitlines(True)):
  456
+                    if translators_comment_start is not None and lineno >= translators_comment_start:
  457
+                        out.write(u' # %s' % line)
  458
+                    else:
  459
+                        out.write(u' #\n')
450 460
                 incomment = False
451 461
                 comment = []
452 462
             else:
14  tests/regressiontests/i18n/commands/extraction.py
@@ -49,7 +49,19 @@ def test_comments_extractor(self):
49 49
         self.assertTrue('This comment should not be extracted' not in po_contents)
50 50
         # Comments in templates
51 51
         self.assertTrue('#. Translators: Django template comment for translators' in po_contents)
52  
-        self.assertTrue('#. Translators: Django comment block for translators' in po_contents)
  52
+        self.assertTrue("#. Translators: Django comment block for translators\n#. string's meaning unveiled" in po_contents)
  53
+
  54
+        self.assertTrue('#. Translators: One-line translator comment #1' in po_contents)
  55
+        self.assertTrue('#. Translators: Two-line translator comment #1\n#. continued here.' in po_contents)
  56
+
  57
+        self.assertTrue('#. Translators: One-line translator comment #2' in po_contents)
  58
+        self.assertTrue('#. Translators: Two-line translator comment #2\n#. continued here.' in po_contents)
  59
+
  60
+        self.assertTrue('#. Translators: One-line translator comment #3' in po_contents)
  61
+        self.assertTrue('#. Translators: Two-line translator comment #3\n#. continued here.' in po_contents)
  62
+
  63
+        self.assertTrue('#. Translators: One-line translator comment #4' in po_contents)
  64
+        self.assertTrue('#. Translators: Two-line translator comment #4\n#. continued here.' in po_contents)
53 65
 
54 66
     def test_templatize(self):
55 67
         os.chdir(self.test_dir)
46  tests/regressiontests/i18n/commands/templates/test.html
... ...
@@ -1,8 +1,52 @@
1 1
 {% load i18n %}
2  
-{% comment %}Translators: Django comment block for translators {% endcomment %}
  2
+{% comment %}Translators: Django comment block for translators
  3
+string's meaning unveiled
  4
+{% endcomment %}
3 5
 {% trans "This literal should be included." %}
4 6
 {% trans "This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option." %}
5 7
 
6 8
 {# Translators: Django template comment for translators #}
7 9
 <p>{% blocktrans %}I think that 100% is more that 50% of anything.{% endblocktrans %}</p>
8 10
 {% blocktrans with 'txt' as obj %}I think that 100% is more that 50% of {{ obj }}.{% endblocktrans %}
  11
+
  12
+{% comment %}Some random comment
  13
+Some random comment
  14
+Translators: One-line translator comment #1
  15
+{% endcomment %}
  16
+{% trans "Translatable literal #1a" %}
  17
+
  18
+{% comment %}Some random comment
  19
+Some random comment
  20
+Translators: Two-line translator comment #1
  21
+continued here.
  22
+{% endcomment %}
  23
+{% trans "Translatable literal #1b" %}
  24
+
  25
+{% comment %}Some random comment
  26
+Translators: One-line translator comment #2
  27
+{% endcomment %}
  28
+{% trans "Translatable literal #2a" %}
  29
+
  30
+{% comment %}Some random comment
  31
+Translators: Two-line translator comment #2
  32
+continued here.
  33
+{% endcomment %}
  34
+{% trans "Translatable literal #2b" %}
  35
+
  36
+{% comment %}
  37
+    Translators: One-line translator comment #3
  38
+{% endcomment %}
  39
+{% trans "Translatable literal #3a" %}
  40
+
  41
+{% comment %}
  42
+Translators: Two-line translator comment #3
  43
+continued here.
  44
+{% endcomment %}
  45
+{% trans "Translatable literal #3b" %}
  46
+
  47
+{% comment %} Translators: One-line translator comment #4{% endcomment %}
  48
+{% trans "Translatable literal #4a" %}
  49
+
  50
+{% comment %}  Translators: Two-line translator comment #4
  51
+continued here.{% endcomment %}
  52
+{% trans "Translatable literal #4b" %}

0 notes on commit 775a6e6

Please sign in to comment.
Something went wrong with that request. Please try again.