Skip to content

Commit

Permalink
Percent signs escaping in templatetags
Browse files Browse the repository at this point in the history
Closes python-babel#43

In Django >= 1.9, `%` signs are escaped to `%%` during extraction
then replaced by `%` during the rendering. [1]

The extraction now behaves in the same way.

[1] django/django@b750889
  • Loading branch information
Mickaël Guérin committed May 28, 2018
1 parent f2ddaba commit beae7e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django_babel/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ def extract_django(fileobj, keywords, comment_tags, options):
else:
singular.append('%%(%s)s' % t.contents)
elif t.token_type == TOKEN_TEXT:
contents = t.contents.replace('%', '%%')
if inplural:
plural.append(t.contents)
plural.append(contents)
else:
singular.append(t.contents)
singular.append(contents)
else:
if t.token_type == TOKEN_BLOCK:
imatch = inline_re.match(t.contents)
Expand All @@ -124,6 +125,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
if imatch:
g = imatch.group(1)
g = strip_quotes(g)
g = g.replace('%', '%%')
message_context = imatch.group(3)
if message_context:
# strip quotes
Expand Down
10 changes: 10 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,13 @@ def test_blocktrans_with_whitespace_trimmed(self):
buf = BytesIO(test_tmpl)
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(4, None, u'foo bar', [])], messages)

def test_extract_trans_percents(self):
buf = BytesIO(b'{% trans "100% Bunny" %}')
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(1, None, u'100%% Bunny', [])], messages)

def test_extract_blocktrans_percents(self):
buf = BytesIO(b'{% blocktrans %}100% Bunny{% endblocktrans %}')
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(1, None, u'100%% Bunny', [])], messages)

0 comments on commit beae7e4

Please sign in to comment.