Skip to content

Commit edbf90b

Browse files
committed
Merge pull request #22 from tsouvarev/extract-context-from-blocktrans
Use context of blocktrans templatetag
2 parents 35ebeb8 + 5442d21 commit edbf90b

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

django_babel/extract.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
2525
"""
2626
intrans = False
2727
inplural = False
28+
message_context = None
2829
singular = []
2930
plural = []
3031
lineno = 1
@@ -40,21 +41,41 @@ def extract_django(fileobj, keywords, comment_tags, options):
4041
pluralmatch = plural_re.match(t.contents)
4142
if endbmatch:
4243
if inplural:
43-
yield (
44-
lineno,
45-
'ngettext',
46-
(smart_text(u''.join(singular)),
47-
smart_text(u''.join(plural))),
48-
[])
44+
if message_context:
45+
yield (
46+
lineno,
47+
'npgettext',
48+
[smart_text(message_context),
49+
smart_text(u''.join(singular)),
50+
smart_text(u''.join(plural))],
51+
[],
52+
)
53+
else:
54+
yield (
55+
lineno,
56+
'ngettext',
57+
(smart_text(u''.join(singular)),
58+
smart_text(u''.join(plural))),
59+
[])
4960
else:
50-
yield (
51-
lineno,
52-
None,
53-
smart_text(u''.join(singular)),
54-
[])
61+
if message_context:
62+
yield (
63+
lineno,
64+
'pgettext',
65+
[smart_text(message_context),
66+
smart_text(u''.join(singular))],
67+
[],
68+
)
69+
else:
70+
yield (
71+
lineno,
72+
None,
73+
smart_text(u''.join(singular)),
74+
[])
5575

5676
intrans = False
5777
inplural = False
78+
message_context = None
5879
singular = []
5980
plural = []
6081
elif pluralmatch:
@@ -93,9 +114,12 @@ def extract_django(fileobj, keywords, comment_tags, options):
93114
[smart_text(message_context), smart_text(g)],
94115
[],
95116
)
117+
message_context = None
96118
else:
97119
yield lineno, None, smart_text(g), []
98120
elif bmatch:
121+
if bmatch.group(2):
122+
message_context = bmatch.group(2)[1:-1]
99123
for fmatch in constant_re.findall(t.contents):
100124
yield lineno, None, smart_text(fmatch), []
101125
intrans = True

tests/test_extract.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,26 @@ def test_extract_constant_in_block(self):
176176
[(1, None, u'"constant"', []), (1, None, u'%(foo)s', [])],
177177
messages,
178178
)
179+
180+
def test_extract_context_in_block(self):
181+
test_tmpl = (
182+
b'{% blocktrans context "banana" %}{{ foo }}{% endblocktrans %}'
183+
)
184+
buf = BytesIO(test_tmpl)
185+
messages = list(extract_django(buf, default_keys, [], {}))
186+
self.assertEqual(
187+
[(1, 'pgettext', [u'banana', u'%(foo)s'], [])],
188+
messages,
189+
)
190+
191+
def test_extract_context_in_plural_block(self):
192+
test_tmpl = (
193+
b'{% blocktrans context "banana" %}{{ foo }}'
194+
b'{% plural %}{{ bar }}{% endblocktrans %}'
195+
)
196+
buf = BytesIO(test_tmpl)
197+
messages = list(extract_django(buf, default_keys, [], {}))
198+
self.assertEqual(
199+
[(1, 'npgettext', [u'banana', u'%(foo)s', u'%(bar)s'], [])],
200+
messages,
201+
)

0 commit comments

Comments
 (0)