Use context of blocktrans templatetag

If blocktrans has context, then extractor should yield pgettext (or npgettext) function not to miss context
This commit is contained in:
Ivan Tsouvarev 2015-11-10 15:30:08 +03:00
parent 35ebeb8194
commit 5442d21f57
2 changed files with 58 additions and 11 deletions

View File

@ -25,6 +25,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
"""
intrans = False
inplural = False
message_context = None
singular = []
plural = []
lineno = 1
@ -40,21 +41,41 @@ def extract_django(fileobj, keywords, comment_tags, options):
pluralmatch = plural_re.match(t.contents)
if endbmatch:
if inplural:
yield (
lineno,
'ngettext',
(smart_text(u''.join(singular)),
smart_text(u''.join(plural))),
[])
if message_context:
yield (
lineno,
'npgettext',
[smart_text(message_context),
smart_text(u''.join(singular)),
smart_text(u''.join(plural))],
[],
)
else:
yield (
lineno,
'ngettext',
(smart_text(u''.join(singular)),
smart_text(u''.join(plural))),
[])
else:
yield (
lineno,
None,
smart_text(u''.join(singular)),
[])
if message_context:
yield (
lineno,
'pgettext',
[smart_text(message_context),
smart_text(u''.join(singular))],
[],
)
else:
yield (
lineno,
None,
smart_text(u''.join(singular)),
[])
intrans = False
inplural = False
message_context = None
singular = []
plural = []
elif pluralmatch:
@ -93,9 +114,12 @@ def extract_django(fileobj, keywords, comment_tags, options):
[smart_text(message_context), smart_text(g)],
[],
)
message_context = None
else:
yield lineno, None, smart_text(g), []
elif bmatch:
if bmatch.group(2):
message_context = bmatch.group(2)[1:-1]
for fmatch in constant_re.findall(t.contents):
yield lineno, None, smart_text(fmatch), []
intrans = True

View File

@ -176,3 +176,26 @@ class ExtractDjangoTestCase(unittest.TestCase):
[(1, None, u'"constant"', []), (1, None, u'%(foo)s', [])],
messages,
)
def test_extract_context_in_block(self):
test_tmpl = (
b'{% blocktrans context "banana" %}{{ foo }}{% endblocktrans %}'
)
buf = BytesIO(test_tmpl)
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual(
[(1, 'pgettext', [u'banana', u'%(foo)s'], [])],
messages,
)
def test_extract_context_in_plural_block(self):
test_tmpl = (
b'{% blocktrans context "banana" %}{{ foo }}'
b'{% plural %}{{ bar }}{% endblocktrans %}'
)
buf = BytesIO(test_tmpl)
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual(
[(1, 'npgettext', [u'banana', u'%(foo)s', u'%(bar)s'], [])],
messages,
)