Merge pull request #22 from tsouvarev/extract-context-from-blocktrans
Use context of blocktrans templatetag
This commit is contained in:
		@@ -25,6 +25,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
 | 
			
		||||
    """
 | 
			
		||||
    intrans = False
 | 
			
		||||
    inplural = False
 | 
			
		||||
    message_context = None
 | 
			
		||||
    singular = []
 | 
			
		||||
    plural = []
 | 
			
		||||
    lineno = 1
 | 
			
		||||
@@ -40,12 +41,31 @@ def extract_django(fileobj, keywords, comment_tags, options):
 | 
			
		||||
                pluralmatch = plural_re.match(t.contents)
 | 
			
		||||
                if endbmatch:
 | 
			
		||||
                    if inplural:
 | 
			
		||||
                        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:
 | 
			
		||||
                        if message_context:
 | 
			
		||||
                            yield (
 | 
			
		||||
                                lineno,
 | 
			
		||||
                                'pgettext',
 | 
			
		||||
                                [smart_text(message_context),
 | 
			
		||||
                                 smart_text(u''.join(singular))],
 | 
			
		||||
                                [],
 | 
			
		||||
                            )
 | 
			
		||||
                        else:
 | 
			
		||||
                            yield (
 | 
			
		||||
                                lineno,
 | 
			
		||||
@@ -55,6 +75,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
 | 
			
		||||
 | 
			
		||||
                    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
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
        )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user