From 775a6e694fedc6b057dc560da8e4a07dc7e9457c Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sat, 19 Mar 2011 12:56:38 +0000 Subject: [PATCH] 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. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15882 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/translation/trans_real.py | 14 +++++- .../i18n/commands/extraction.py | 14 +++++- .../i18n/commands/templates/test.html | 46 ++++++++++++++++++- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 675fb3f4ae..b8478354bd 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -435,7 +435,8 @@ def templatize(src, origin=None): 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 @@ -446,7 +447,16 @@ def templatize(src, origin=None): 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: diff --git a/tests/regressiontests/i18n/commands/extraction.py b/tests/regressiontests/i18n/commands/extraction.py index 1f36b8a9b4..0d70d2641d 100644 --- a/tests/regressiontests/i18n/commands/extraction.py +++ b/tests/regressiontests/i18n/commands/extraction.py @@ -49,7 +49,19 @@ class BasicExtractorTests(ExtractorTests): 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) diff --git a/tests/regressiontests/i18n/commands/templates/test.html b/tests/regressiontests/i18n/commands/templates/test.html index ff8485ead5..86c7772580 100644 --- a/tests/regressiontests/i18n/commands/templates/test.html +++ b/tests/regressiontests/i18n/commands/templates/test.html @@ -1,8 +1,52 @@ {% 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 #}

{% blocktrans %}I think that 100% is more that 50% of anything.{% endblocktrans %}

{% 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" %}