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
814from 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
1336def 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 :
0 commit comments