Merge pull request #22 from tsouvarev/extract-context-from-blocktrans

Use context of blocktrans templatetag
This commit is contained in:
Thomas Grainger
2015-11-12 08:42:14 +00:00
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 intrans = False
inplural = False inplural = False
message_context = None
singular = [] singular = []
plural = [] plural = []
lineno = 1 lineno = 1
@@ -40,21 +41,41 @@ def extract_django(fileobj, keywords, comment_tags, options):
pluralmatch = plural_re.match(t.contents) pluralmatch = plural_re.match(t.contents)
if endbmatch: if endbmatch:
if inplural: if inplural:
yield ( if message_context:
lineno, yield (
'ngettext', lineno,
(smart_text(u''.join(singular)), 'npgettext',
smart_text(u''.join(plural))), [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: else:
yield ( if message_context:
lineno, yield (
None, lineno,
smart_text(u''.join(singular)), 'pgettext',
[]) [smart_text(message_context),
smart_text(u''.join(singular))],
[],
)
else:
yield (
lineno,
None,
smart_text(u''.join(singular)),
[])
intrans = False intrans = False
inplural = False inplural = False
message_context = None
singular = [] singular = []
plural = [] plural = []
elif pluralmatch: elif pluralmatch:
@@ -93,9 +114,12 @@ def extract_django(fileobj, keywords, comment_tags, options):
[smart_text(message_context), smart_text(g)], [smart_text(message_context), smart_text(g)],
[], [],
) )
message_context = None
else: else:
yield lineno, None, smart_text(g), [] yield lineno, None, smart_text(g), []
elif bmatch: elif bmatch:
if bmatch.group(2):
message_context = bmatch.group(2)[1:-1]
for fmatch in constant_re.findall(t.contents): for fmatch in constant_re.findall(t.contents):
yield lineno, None, smart_text(fmatch), [] yield lineno, None, smart_text(fmatch), []
intrans = True intrans = True

View File

@@ -176,3 +176,26 @@ class ExtractDjangoTestCase(unittest.TestCase):
[(1, None, u'"constant"', []), (1, None, u'%(foo)s', [])], [(1, None, u'"constant"', []), (1, None, u'%(foo)s', [])],
messages, 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,
)