Permalink
Browse files

Merge b3f9ab0 into d233391

  • Loading branch information...
2 parents d233391 + b3f9ab0 commit 268627b8e943b32deb3ad4a9d5ea2659a20acda6 @sergeykolosov sergeykolosov committed Oct 20, 2017
@@ -23,10 +23,6 @@
LANGUAGE_SESSION_KEY = '_language'
-class TranslatorCommentWarning(SyntaxWarning):
- pass
-
-
# Here be dragons, so a short explanation of the logic won't hurt:
# We are trying to solve two problems: (1) access settings, in particular
# settings.USE_I18N, as late as possible, so that modules can be imported
@@ -1,13 +1,12 @@
import re
-import warnings
from io import StringIO
from django.template.base import (
TOKEN_BLOCK, TOKEN_COMMENT, TOKEN_TEXT, TOKEN_VAR, TRANSLATOR_COMMENT_MARK,
Lexer,
)
-from . import TranslatorCommentWarning, trim_whitespace
+from . import trim_whitespace
dot_re = re.compile(r'\S')
@@ -50,8 +49,6 @@ def templatize(src, origin=None):
plural = []
incomment = False
comment = []
- lineno_comment_map = {}
- comment_lineno_cache = None
# Adding the u prefix allows gettext to recognize the string (#26093).
raw_prefix = 'u'
@@ -70,8 +67,18 @@ def join_tokens(tokens, trim=False):
if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
translators_comment_start = lineno
for lineno, line in enumerate(content.splitlines(True)):
+ multiline_comment_block = len(content.splitlines(True)) > 1
if translators_comment_start is not None and lineno >= translators_comment_start:
- out.write(' # %s' % line)
+ if not multiline_comment_block:
+ # move comment into last new line at the end of line
+ new_line_pos = out.getvalue().rindex('\n')
+ out.seek(new_line_pos + 1)
+ temp_buffer = out.read()
+ out.seek(new_line_pos)
+ comment_block_content = ' # %s \n%s' % (line, temp_buffer)
+ else:
+ comment_block_content = ' # %s ' % line
+ out.write(comment_block_content)
else:
out.write(' #\n')
incomment = False
@@ -142,27 +149,6 @@ def join_tokens(tokens, trim=False):
else:
singular.append(contents)
else:
- # Handle comment tokens (`{# ... #}`) plus other constructs on
- # the same line:
- if comment_lineno_cache is not None:
- cur_lineno = t.lineno + t.contents.count('\n')
- if comment_lineno_cache == cur_lineno:
- if t.token_type != TOKEN_COMMENT:
- for c in lineno_comment_map[comment_lineno_cache]:
- filemsg = ''
- if origin:
- filemsg = 'file %s, ' % origin
- warn_msg = (
- "The translator-targeted comment '%s' "
- "(%sline %d) was ignored, because it wasn't "
- "the last item on the line."
- ) % (c, filemsg, comment_lineno_cache)
- warnings.warn(warn_msg, TranslatorCommentWarning)
- lineno_comment_map[comment_lineno_cache] = []
- else:
- out.write('# %s' % ' | '.join(lineno_comment_map[comment_lineno_cache]))
- comment_lineno_cache = None
-
if t.token_type == TOKEN_BLOCK:
imatch = inline_re.match(t.contents)
bmatch = block_re.match(t.contents)
@@ -223,8 +209,13 @@ def join_tokens(tokens, trim=False):
out.write(blankout(p, 'F'))
elif t.token_type == TOKEN_COMMENT:
if t.contents.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
- lineno_comment_map.setdefault(t.lineno, []).append(t.contents)
- comment_lineno_cache = t.lineno
+ new_line_pos = out.getvalue().rindex('\n')
+ out.seek(new_line_pos + 1)
+ temp_buffer = out.read()
+ out.seek(new_line_pos)
+ comment_block_content = '# %s ' % t.contents
+ out.write(comment_block_content + '\n' + temp_buffer)
+
else:
out.write(blankout(t.contents, 'X'))
return out.getvalue()
@@ -1,13 +1,19 @@
{% 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" %}
+{# Translators: i18n comment #1 #}{% trans "Translatable literal #9b" %}
+{# Translators: i18n comment #2 #}{# ignored comment #2 #}{% trans "Translatable literal #9c" %}
+{# ignored comment #3 #}{# Translators: 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 #}
+{# Translators: 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" %}
+{% comment %}Translators: i18n comment #9w{% endcomment %}{% trans "Translatable literal #9w" %} {% comment %}Translators: i18n comment #9w after{% endcomment %}
+something {% comment %}Translators: i18n comment #9z{% endcomment %} DEMO DATA IN BETWEEN {% trans "Translatable literal #9z" %}
+{% comment %}Translators: i18n comment #9x{% endcomment %}
+{% trans "Translatable literal #9x" %}
+
+{# Translators: valid i18n comment #10 #}{# Translators: valid i18n comment #11 #} {% trans "Translatable literal #10a" %}
@@ -0,0 +1,27 @@
+{% load i18n %}
+
+{# Translators: Abbreviated month name 'Jan' #}
+{% trans "Jan" %}
+
+{# Translators: Abbreviated month name 'Feb'#}{% trans "Feb" %}
+
+{% comment %}Translators: Abbreviated month name 'Mar'{% endcomment %}{% trans "Mar" %}
+
+{# Translators: Abbreviated month name 'Apr' #} {% trans "Apr" %}
+
+{% comment %}Translators: Abbreviated month name 'May'{% endcomment %} {% trans "May" %}
+
+{# Translators: Abbreviated month name 'Jun'#}
+{% trans "Jun" %}
+
+{# Translators: Abbreviated month name 'Jul'#}
+
+{% trans "Jul" %}
+
+{% comment %}Translators: Abbreviated month name 'Aug'{% endcomment %}
+
+{% trans "Aug" %}
+
+{% trans "Sep" %} {#Translators: Abbreviated month name 'Sep'#} some text after
+
+{% trans "Oct" %} some text in between {% comment %}Translators: Abbreviated month name 'Oct'{% endcomment %}
@@ -18,7 +18,6 @@
from django.test import SimpleTestCase, override_settings
from django.test.utils import captured_stderr, captured_stdout
from django.utils._os import symlinks_supported
-from django.utils.translation import TranslatorCommentWarning
from .utils import POFileAssertionMixin, RunInTmpDirMixin, copytree
@@ -274,58 +273,34 @@ def test_context_in_single_quotes(self):
def test_template_comments(self):
"""Template comment tags on the same line of other constructs (#19552)"""
- # 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))
- self.assertRegex(
- str(ws[0].message),
- r"The translator-targeted comment 'Translators: ignored i18n "
- r"comment #1' \(file templates[/\\]comments.thtml, line 4\) "
- r"was ignored, because it wasn't the last item on the line\."
- )
- self.assertRegex(
- str(ws[1].message),
- r"The translator-targeted comment 'Translators: ignored i18n "
- r"comment #3' \(file templates[/\\]comments.thtml, line 6\) "
- r"was ignored, because it wasn't the last item on the line\."
- )
- self.assertRegex(
- str(ws[2].message),
- r"The translator-targeted comment 'Translators: ignored i18n "
- r"comment #4' \(file templates[/\\]comments.thtml, line 8\) "
- r"was ignored, because it wasn't the last item on the line\."
- )
- # Now test .po file contents
+
+ management.call_command('makemessages', locale=[LOCALE], extensions=['thtml'], verbosity=0)
+
self.assertTrue(os.path.exists(self.PO_FILE))
with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read()
self.assertMsgId('Translatable literal #9a', po_contents)
self.assertNotIn('ignored comment #1', po_contents)
- self.assertNotIn('Translators: ignored i18n comment #1', po_contents)
+ self.assertIn('Translators: i18n comment #1', po_contents)
self.assertMsgId("Translatable literal #9b", po_contents)
- self.assertNotIn('ignored i18n comment #2', po_contents)
+ self.assertIn('i18n comment #2', po_contents)
self.assertNotIn('ignored comment #2', po_contents)
self.assertMsgId('Translatable literal #9c', po_contents)
self.assertNotIn('ignored comment #3', po_contents)
- self.assertNotIn('ignored i18n comment #3', po_contents)
+ self.assertIn('i18n comment #3', po_contents)
self.assertMsgId('Translatable literal #9d', po_contents)
self.assertNotIn('ignored comment #4', po_contents)
self.assertMsgId('Translatable literal #9e', po_contents)
self.assertNotIn('ignored comment #5', po_contents)
- self.assertNotIn('ignored i18n comment #4', po_contents)
+ self.assertIn('i18n comment #4', po_contents)
+ self.assertIn('valid i18n comment #5', po_contents)
self.assertMsgId('Translatable literal #9f', po_contents)
- self.assertIn('#. Translators: valid i18n comment #5', po_contents)
self.assertMsgId('Translatable literal #9g', po_contents)
self.assertIn('#. Translators: valid i18n comment #6', po_contents)
@@ -337,6 +312,70 @@ def test_template_comments(self):
self.assertRegex(po_contents, r'#\..+Translators: valid i18n comment #9')
self.assertMsgId("Translatable literal #9j", po_contents)
+ self.assertRegex(po_contents, r'#\..+Translators: i18n comment #9w')
+ self.assertMsgId("Translatable literal #9w", po_contents)
+
+ self.assertRegex(po_contents, r'#\..+Translators: i18n comment #9z')
+ self.assertMsgId("Translatable literal #9z", po_contents)
+
+ self.assertRegex(po_contents, r'#\..+Translators: i18n comment #9x')
+ self.assertMsgId("Translatable literal #9x", po_contents)
+
+ def test_inline_translators_template_comments(self):
+ """Test translations along with inline translators comments (#21963)"""
+
+ # No warnings expected, TranslatorCommentWarning has been removed from Django
+ 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), 0)
+
+ # Test .po file contents and ensure that all translations are in correct
+ # locations with correct inline comments
+ self.assertTrue(os.path.exists(self.PO_FILE))
+ with open(self.PO_FILE, 'r') as fp:
+ po_contents = fp.read()
+
+ self.assertMsgId('Jan', po_contents)
+ self.assertIn("#. Translators: Abbreviated month name 'Jan'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 4, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Feb', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Feb'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 6, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Mar', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Mar'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 8, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Apr', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Apr'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 10, 'templates', 'translators.thtml')
+
+ self.assertMsgId('May', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'May'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 12, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Jun', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Jun'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 15, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Jul', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Jul'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 19, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Aug', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Aug'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 23, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Sep', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Sep'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 25, 'templates', 'translators.thtml')
+
+ self.assertMsgId('Oct', po_contents)
+ self.assertIn("Translators: Abbreviated month name 'Oct'", po_contents)
+ self.assertLocationCommentPresent(self.PO_FILE, 27, 'templates', 'translators.thtml')
+
def test_makemessages_find_files(self):
"""
find_files only discover files having the proper extensions.

0 comments on commit 268627b

Please sign in to comment.