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 |     intrans = False | ||||||
|     inplural = False |     inplural = False | ||||||
|  |     message_context = None | ||||||
|     singular = [] |     singular = [] | ||||||
|     plural = [] |     plural = [] | ||||||
|     lineno = 1 |     lineno = 1 | ||||||
| @@ -40,12 +41,31 @@ 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: | ||||||
|  |                         if message_context: | ||||||
|  |                             yield ( | ||||||
|  |                                 lineno, | ||||||
|  |                                 'npgettext', | ||||||
|  |                                 [smart_text(message_context), | ||||||
|  |                                  smart_text(u''.join(singular)), | ||||||
|  |                                  smart_text(u''.join(plural))], | ||||||
|  |                                 [], | ||||||
|  |                             ) | ||||||
|  |                         else: | ||||||
|                             yield ( |                             yield ( | ||||||
|                                 lineno, |                                 lineno, | ||||||
|                                 'ngettext', |                                 'ngettext', | ||||||
|                                 (smart_text(u''.join(singular)), |                                 (smart_text(u''.join(singular)), | ||||||
|                                  smart_text(u''.join(plural))), |                                  smart_text(u''.join(plural))), | ||||||
|                                 []) |                                 []) | ||||||
|  |                     else: | ||||||
|  |                         if message_context: | ||||||
|  |                             yield ( | ||||||
|  |                                 lineno, | ||||||
|  |                                 'pgettext', | ||||||
|  |                                 [smart_text(message_context), | ||||||
|  |                                  smart_text(u''.join(singular))], | ||||||
|  |                                 [], | ||||||
|  |                             ) | ||||||
|                         else: |                         else: | ||||||
|                             yield ( |                             yield ( | ||||||
|                                 lineno, |                                 lineno, | ||||||
| @@ -55,6 +75,7 @@ def extract_django(fileobj, keywords, comment_tags, options): | |||||||
|  |  | ||||||
|                     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 | ||||||
|   | |||||||
| @@ -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, | ||||||
|  |         ) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Thomas Grainger
					Thomas Grainger