Skip to content

Commit 3e4efa0

Browse files
committed
Merge pull request #25 from robhudson/trimmed-support
Add support for "trimmed" blocktrans content
2 parents 05d906c + 0e74e0d commit 3e4efa0

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 strip_quotes(s):
@@ -31,6 +54,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
3154
"""
3255
intrans = False
3356
inplural = False
57+
trimmed = False
3458
message_context = None
3559
singular = []
3660
plural = []
@@ -59,31 +83,31 @@ def extract_django(fileobj, keywords, comment_tags, options):
5983
lineno,
6084
'npgettext',
6185
[smart_text(message_context),
62-
smart_text(u''.join(singular)),
63-
smart_text(u''.join(plural))],
86+
smart_text(join_tokens(singular, trimmed)),
87+
smart_text(join_tokens(plural, trimmed))],
6488
[],
6589
)
6690
else:
6791
yield (
6892
lineno,
6993
'ngettext',
70-
(smart_text(u''.join(singular)),
71-
smart_text(u''.join(plural))),
94+
(smart_text(join_tokens(singular, trimmed)),
95+
smart_text(join_tokens(plural, trimmed))),
7296
[])
7397
else:
7498
if message_context:
7599
yield (
76100
lineno,
77101
'pgettext',
78102
[smart_text(message_context),
79-
smart_text(u''.join(singular))],
103+
smart_text(join_tokens(singular, trimmed))],
80104
[],
81105
)
82106
else:
83107
yield (
84108
lineno,
85109
None,
86-
smart_text(u''.join(singular)),
110+
smart_text(join_tokens(singular, trimmed)),
87111
[])
88112

89113
intrans = False
@@ -135,6 +159,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
135159
yield lineno, None, smart_text(stripped_fmatch), []
136160
intrans = True
137161
inplural = False
162+
trimmed = 'trimmed' in t.split_contents()
138163
singular = []
139164
plural = []
140165
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)