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 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 :
0 commit comments