Skip to content

Commit 0e74e0d

Browse files
committed
Add support for "trimmed" blocktrans content
1 parent 5d953ef commit 0e74e0d

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

django_babel/extract.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,32 @@
55
# Django 1.8 moved most stuff to .base
66
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
77

8+
try:
9+
from django.utils.translation import trim_whitespace as trim_django
10+
except ImportError:
11+
trim_django = False
12+
13+
from django.utils.encoding import smart_text
814
from django.utils.translation.trans_real import (
915
inline_re, block_re, endblock_re, plural_re, constant_re)
10-
from django.utils.encoding import smart_text
16+
17+
18+
def trim_whitespace(string):
19+
"""Trim whitespace.
20+
21+
This is only supported in Django>=1.7. This method help in cases of older
22+
Django versions.
23+
"""
24+
if trim_django:
25+
return trim_django(string)
26+
return string
27+
28+
29+
def join_tokens(tokens, trim=False):
30+
message = ''.join(tokens)
31+
if trim:
32+
message = trim_whitespace(message)
33+
return message
1134

1235

1336
def extract_django(fileobj, keywords, comment_tags, options):
@@ -25,6 +48,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
2548
"""
2649
intrans = False
2750
inplural = False
51+
trimmed = False
2852
message_context = None
2953
singular = []
3054
plural = []
@@ -53,31 +77,31 @@ def extract_django(fileobj, keywords, comment_tags, options):
5377
lineno,
5478
'npgettext',
5579
[smart_text(message_context),
56-
smart_text(u''.join(singular)),
57-
smart_text(u''.join(plural))],
80+
smart_text(join_tokens(singular, trimmed)),
81+
smart_text(join_tokens(plural, trimmed))],
5882
[],
5983
)
6084
else:
6185
yield (
6286
lineno,
6387
'ngettext',
64-
(smart_text(u''.join(singular)),
65-
smart_text(u''.join(plural))),
88+
(smart_text(join_tokens(singular, trimmed)),
89+
smart_text(join_tokens(plural, trimmed))),
6690
[])
6791
else:
6892
if message_context:
6993
yield (
7094
lineno,
7195
'pgettext',
7296
[smart_text(message_context),
73-
smart_text(u''.join(singular))],
97+
smart_text(join_tokens(singular, trimmed))],
7498
[],
7599
)
76100
else:
77101
yield (
78102
lineno,
79103
None,
80-
smart_text(u''.join(singular)),
104+
smart_text(join_tokens(singular, trimmed)),
81105
[])
82106

83107
intrans = False
@@ -131,6 +155,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
131155
yield lineno, None, smart_text(fmatch), []
132156
intrans = True
133157
inplural = False
158+
trimmed = 'trimmed' in t.split_contents()
134159
singular = []
135160
plural = []
136161
elif cmatches:

tests/test_extract.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from babel.messages import extract
77
from babel._compat import BytesIO
88

9+
import django
910
from django_babel.extract import extract_django
1011

1112

@@ -199,3 +200,21 @@ def test_extract_context_in_plural_block(self):
199200
[(1, 'npgettext', [u'banana', u'%(foo)s', u'%(bar)s'], [])],
200201
messages,
201202
)
203+
204+
def test_blocktrans_with_whitespace_not_trimmed(self):
205+
test_tmpl = (
206+
b'{% blocktrans %}\n\tfoo\n\tbar\n{% endblocktrans %}'
207+
)
208+
buf = BytesIO(test_tmpl)
209+
messages = list(extract_django(buf, default_keys, [], {}))
210+
self.assertEqual([(4, None, u'\n\tfoo\n\tbar\n', [])], messages)
211+
212+
@pytest.mark.skipif(django.VERSION < (1, 7),
213+
reason='Trimmed whitespace is a Django >= 1.7 feature')
214+
def test_blocktrans_with_whitespace_trimmed(self):
215+
test_tmpl = (
216+
b'{% blocktrans trimmed %}\n\tfoo\n\tbar\n{% endblocktrans %}'
217+
)
218+
buf = BytesIO(test_tmpl)
219+
messages = list(extract_django(buf, default_keys, [], {}))
220+
self.assertEqual([(4, None, u'foo bar', [])], messages)

0 commit comments

Comments
 (0)