5
5
# Django 1.8 moved most stuff to .base
6
6
from django .template .base import Lexer , TOKEN_TEXT , TOKEN_VAR , TOKEN_BLOCK
7
7
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
8
14
from django .utils .translation .trans_real import (
9
15
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
11
34
12
35
13
36
def extract_django (fileobj , keywords , comment_tags , options ):
@@ -25,6 +48,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
25
48
"""
26
49
intrans = False
27
50
inplural = False
51
+ trimmed = False
28
52
message_context = None
29
53
singular = []
30
54
plural = []
@@ -53,31 +77,31 @@ def extract_django(fileobj, keywords, comment_tags, options):
53
77
lineno ,
54
78
'npgettext' ,
55
79
[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 ))],
58
82
[],
59
83
)
60
84
else :
61
85
yield (
62
86
lineno ,
63
87
'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 ))),
66
90
[])
67
91
else :
68
92
if message_context :
69
93
yield (
70
94
lineno ,
71
95
'pgettext' ,
72
96
[smart_text (message_context ),
73
- smart_text (u'' . join (singular ))],
97
+ smart_text (join_tokens (singular , trimmed ))],
74
98
[],
75
99
)
76
100
else :
77
101
yield (
78
102
lineno ,
79
103
None ,
80
- smart_text (u'' . join (singular )),
104
+ smart_text (join_tokens (singular , trimmed )),
81
105
[])
82
106
83
107
intrans = False
@@ -131,6 +155,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
131
155
yield lineno , None , smart_text (fmatch ), []
132
156
intrans = True
133
157
inplural = False
158
+ trimmed = 'trimmed' in t .split_contents ()
134
159
singular = []
135
160
plural = []
136
161
elif cmatches :
0 commit comments