diff --git a/django_babel/extract.py b/django_babel/extract.py index 0626040..209761e 100644 --- a/django_babel/extract.py +++ b/django_babel/extract.py @@ -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 diff --git a/tests/test_extract.py b/tests/test_extract.py index 2a7536b..4f64a89 100644 --- a/tests/test_extract.py +++ b/tests/test_extract.py @@ -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, + )