Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Percent signs escaping in templatetags #44

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions django_babel/extract.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# -*- coding: utf-8 -*-
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
from django.template.base import Lexer
from django.utils.translation import trim_whitespace
from django.utils.encoding import smart_text

try:
from django.template.base import TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
except ImportError:
# Django 2.1+
from django.template.base import TokenType
TOKEN_TEXT = TokenType.TEXT
TOKEN_VAR = TokenType.VAR
TOKEN_BLOCK = TokenType.BLOCK

try:
from django.utils.translation.trans_real import (
inline_re, block_re, endblock_re, plural_re, constant_re)
Expand All @@ -11,6 +20,12 @@
from django.utils.translation.template import (
inline_re, block_re, endblock_re, plural_re, constant_re)

try:
from django.utils.translation.trans_real import one_percent_re
except ImportError:
# Django 1.9+
one_percent_re = None


def join_tokens(tokens, trim=False):
message = ''.join(tokens)
Expand Down Expand Up @@ -112,10 +127,14 @@ def extract_django(fileobj, keywords, comment_tags, options):
else:
singular.append('%%(%s)s' % t.contents)
elif t.token_type == TOKEN_TEXT:
if one_percent_re:
contents = one_percent_re.sub('%%', t.contents)
else:
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 +143,10 @@ def extract_django(fileobj, keywords, comment_tags, options):
if imatch:
g = imatch.group(1)
g = strip_quotes(g)
if one_percent_re:
g = one_percent_re.sub('%%', g)
else:
g = g.replace('%', '%%')
message_context = imatch.group(3)
if message_context:
# strip quotes
Expand Down
30 changes: 30 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,33 @@ 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)

@pytest.mark.skipif(django.VERSION >= (1, 9),
reason='%-sign escaping changed in django 1.9')
def test_extract_trans_percents_old_way(self):
"""Before Django 1.9, only a signle %-sign was escaped to %%"""
buf = BytesIO(b'{% trans "1 %, 2 %%, 3 %%%" %}')
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(1, None, u'1 %%, 2 %%, 3 %%%', [])], messages)

@pytest.mark.skipif(django.VERSION >= (1, 9),
reason='%-sign escaping changed in django 1.9')
def test_extract_blocktrans_percents_old_way(self):
"""Before Django 1.9, only a signle %-sign was escaped to %%"""
buf = BytesIO(b'{% blocktrans %}1 %, 2 %%, 3 %%%{% endblocktrans %}')
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(1, None, u'1 %%, 2 %%, 3 %%%', [])], messages)

@pytest.mark.skipif(django.VERSION < (1, 9),
reason='%-sign escaping changed in django 1.9')
def test_extract_trans_percents(self):
buf = BytesIO(b'{% trans "1 %, 2 %%, 3 %%%" %}')
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(1, None, u'1 %%, 2 %%%%, 3 %%%%%%', [])], messages)

@pytest.mark.skipif(django.VERSION < (1, 9),
reason='%-sign escaping changed in django 1.9')
def test_extract_blocktrans_percents(self):
buf = BytesIO(b'{% blocktrans %}1 %, 2 %%, 3 %%%{% endblocktrans %}')
messages = list(extract_django(buf, default_keys, [], {}))
self.assertEqual([(1, None, u'1 %%, 2 %%%%, 3 %%%%%%', [])], messages)