summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRamiro Morales <cramm0@gmail.com>2013-01-20 15:07:10 -0300
committerRamiro Morales <cramm0@gmail.com>2013-01-29 19:13:23 -0300
commit47ddd6a4082d55d8856b7e6beac553485dd627f7 (patch)
treec2c8e33dcab4ce8028a6e8a273ffe318057cab82 /tests
parenteb9430fc4be16f2256f8ff4d8ee90577fb0a8f5a (diff)
Fixed #19552 -- Enhanced makemessages handling of ``{# #}``-style template comments.
They are simply ignored now. This allows for a more correct behavior when they are placed before translatable constructs on the same line. Previously, the latter were wrongly ignored because the former were preserved when converting template code to the internal Python-syntax form later fed to xgettext but Python has no ``/* ... */``-style comments. Also, special comments directed to translators are now only taken in account when they are located at the end of a line. e.g.:: {# Translators: ignored #}{% trans "Literal A" %}{# Translators: valid, associated with "Literal B" below #} {% trans "Literal B" %} Behavior of ``{% comment %}...{% endcomment %}``tags remains unchanged. Thanks juneih at redpill-linpro dot com for the report and Claude for his work on the issue.
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/i18n/commands/extraction.py60
-rw-r--r--tests/regressiontests/i18n/commands/templates/comments.thtml13
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/regressiontests/i18n/commands/extraction.py b/tests/regressiontests/i18n/commands/extraction.py
index ac8d8c1a09..0367d23ec6 100644
--- a/tests/regressiontests/i18n/commands/extraction.py
+++ b/tests/regressiontests/i18n/commands/extraction.py
@@ -4,6 +4,7 @@ from __future__ import unicode_literals
import os
import re
import shutil
+import warnings
from django.core import management
from django.test import SimpleTestCase
@@ -11,6 +12,7 @@ from django.utils.encoding import force_text
from django.utils._os import upath
from django.utils import six
from django.utils.six import StringIO
+from django.utils.translation import TranslatorCommentWarning
LOCALE='de'
@@ -120,6 +122,7 @@ class BasicExtractorTests(ExtractorTests):
self.assertFalse(os.path.exists('./templates/template_with_error.tpl.py'))
def test_extraction_warning(self):
+ """test xgettext warning about multiple bare interpolation placeholders"""
os.chdir(self.test_dir)
shutil.copyfile('./code.sample', './code_sample.py')
stdout = StringIO()
@@ -172,6 +175,63 @@ class BasicExtractorTests(ExtractorTests):
self.assertTrue('msgctxt "Special blocktrans context wrapped in double quotes"' in po_contents)
self.assertTrue('msgctxt "Special blocktrans context wrapped in single quotes"' in po_contents)
+ def test_template_comments(self):
+ """Template comment tags on the same line of other constructs (#19552)"""
+ os.chdir(self.test_dir)
+ # Test detection/end user reporting of old, incorrect templates
+ # translator comments syntax
+ with warnings.catch_warnings(record=True) as ws:
+ warnings.simplefilter('always')
+ management.call_command('makemessages', locale=LOCALE, extensions=['thtml'], verbosity=0)
+ self.assertEqual(len(ws), 3)
+ for w in ws:
+ self.assertTrue(issubclass(w.category, TranslatorCommentWarning))
+ six.assertRegex(self, str(ws[0].message),
+ r"The translator-targeted comment 'Translators: ignored i18n comment #1' \(file templates/comments.thtml, line 4\) was ignored, because it wasn't the last item on the line\."
+ )
+ six.assertRegex(self, str(ws[1].message),
+ r"The translator-targeted comment 'Translators: ignored i18n comment #3' \(file templates/comments.thtml, line 6\) was ignored, because it wasn't the last item on the line\."
+ )
+ six.assertRegex(self, str(ws[2].message),
+ r"The translator-targeted comment 'Translators: ignored i18n comment #4' \(file templates/comments.thtml, line 8\) was ignored, because it wasn't the last item on the line\."
+ )
+ # Now test .po file contents
+ self.assertTrue(os.path.exists(self.PO_FILE))
+ with open(self.PO_FILE, 'r') as fp:
+ po_contents = force_text(fp.read())
+
+ self.assertMsgId('Translatable literal #9a', po_contents)
+ self.assertFalse('ignored comment #1' in po_contents)
+
+ self.assertFalse('Translators: ignored i18n comment #1' in po_contents)
+ self.assertMsgId("Translatable literal #9b", po_contents)
+
+ self.assertFalse('ignored i18n comment #2' in po_contents)
+ self.assertFalse('ignored comment #2' in po_contents)
+ self.assertMsgId('Translatable literal #9c', po_contents)
+
+ self.assertFalse('ignored comment #3' in po_contents)
+ self.assertFalse('ignored i18n comment #3' in po_contents)
+ self.assertMsgId('Translatable literal #9d', po_contents)
+
+ self.assertFalse('ignored comment #4' in po_contents)
+ self.assertMsgId('Translatable literal #9e', po_contents)
+ self.assertFalse('ignored comment #5' in po_contents)
+
+ self.assertFalse('ignored i18n comment #4' in po_contents)
+ self.assertMsgId('Translatable literal #9f', po_contents)
+ self.assertTrue('#. Translators: valid i18n comment #5' in po_contents)
+
+ self.assertMsgId('Translatable literal #9g', po_contents)
+ self.assertTrue('#. Translators: valid i18n comment #6' in po_contents)
+ self.assertMsgId('Translatable literal #9h', po_contents)
+ self.assertTrue('#. Translators: valid i18n comment #7' in po_contents)
+ self.assertMsgId('Translatable literal #9i', po_contents)
+
+ six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #8')
+ six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #9')
+ self.assertMsgId("Translatable literal #9j", po_contents)
+
class JavascriptExtractorTests(ExtractorTests):
diff --git a/tests/regressiontests/i18n/commands/templates/comments.thtml b/tests/regressiontests/i18n/commands/templates/comments.thtml
new file mode 100644
index 0000000000..90eb5f1792
--- /dev/null
+++ b/tests/regressiontests/i18n/commands/templates/comments.thtml
@@ -0,0 +1,13 @@
+{% load i18n %}
+
+{# ignored comment #1 #}{% trans "Translatable literal #9a" %}
+{# Translators: ignored i18n comment #1 #}{% trans "Translatable literal #9b" %}
+{# Translators: ignored i18n comment #2 #}{# ignored comment #2 #}{% trans "Translatable literal #9c" %}
+{# ignored comment #3 #}{# Translators: ignored i18n comment #3 #}{% trans "Translatable literal #9d" %}
+{# ignored comment #4 #}{% trans "Translatable literal #9e" %}{# ignored comment #5 #}
+{# Translators: ignored i18n comment #4 #}{% trans "Translatable literal #9f" %}{# Translators: valid i18n comment #5 #}
+{% trans "Translatable literal #9g" %}{# Translators: valid i18n comment #6 #}
+{# ignored comment #6 #}{% trans "Translatable literal #9h" %}{# Translators: valid i18n comment #7 #}
+{% trans "Translatable literal #9i" %}
+{# Translators: valid i18n comment #8 #}{# Translators: valid i18n comment #9 #}
+{% trans "Translatable literal #9j" %}