Skip to content

Commit 35ebeb8

Browse files
committed
Merge pull request #19 from tsouvarev/patch-1
Use context of trans templatetag
2 parents 0940f09 + 111ed4b commit 35ebeb8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

django_babel/extract.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,18 @@ def extract_django(fileobj, keywords, comment_tags, options):
8383
g = g.strip('"')
8484
elif g[0] == "'":
8585
g = g.strip("'")
86-
yield lineno, None, smart_text(g), []
86+
message_context = imatch.group(3)
87+
if message_context:
88+
# strip quotes
89+
message_context = message_context[1:-1]
90+
yield (
91+
lineno,
92+
'pgettext',
93+
[smart_text(message_context), smart_text(g)],
94+
[],
95+
)
96+
else:
97+
yield lineno, None, smart_text(g), []
8798
elif bmatch:
8899
for fmatch in constant_re.findall(t.contents):
89100
yield lineno, None, smart_text(fmatch), []

tests/test_extract.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ def test_extract_simple_single_quotes(self):
3030
messages = list(extract_django(buf, default_keys, [], {}))
3131
self.assertEqual([(1, None, u'Bunny', [])], messages)
3232

33+
def test_extract_simple_with_context_single_quotes(self):
34+
buf = BytesIO(b"{% trans 'Bunny' context 'carrot' %}")
35+
messages = list(extract_django(buf, default_keys, [], {}))
36+
self.assertEqual([(1, 'pgettext',
37+
[u'carrot', u'Bunny'], [])], messages)
38+
39+
def test_extract_simple_with_context_double_quotes(self):
40+
buf = BytesIO(b"{% trans 'Bunny' context \"carrot\" %}")
41+
messages = list(extract_django(buf, default_keys, [], {}))
42+
self.assertEqual([(1, 'pgettext',
43+
[u'carrot', u'Bunny'], [])], messages)
44+
45+
def test_extract_simple_with_context_with_single_quotes(self):
46+
buf = BytesIO(b"{% trans 'Bunny' context \"'carrot'\" %}")
47+
messages = list(extract_django(buf, default_keys, [], {}))
48+
self.assertEqual([(1, 'pgettext',
49+
[u'\'carrot\'', u'Bunny'], [])], messages)
50+
51+
def test_extract_simple_with_context_with_double_quotes(self):
52+
buf = BytesIO(b"{% trans 'Bunny' context '\"carrot\"' %}")
53+
messages = list(extract_django(buf, default_keys, [], {}))
54+
self.assertEqual([(1, 'pgettext',
55+
[u'"carrot"', u'Bunny'], [])], messages)
56+
3357
def test_extract_var(self):
3458
buf = BytesIO(b'{% blocktrans %}{{ anton }}{% endblocktrans %}')
3559
messages = list(extract_django(buf, default_keys, [], {}))

0 commit comments

Comments
 (0)