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 strip_quotes (s ):
@@ -31,6 +54,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
31
54
"""
32
55
intrans = False
33
56
inplural = False
57
+ trimmed = False
34
58
message_context = None
35
59
singular = []
36
60
plural = []
@@ -59,31 +83,31 @@ def extract_django(fileobj, keywords, comment_tags, options):
59
83
lineno ,
60
84
'npgettext' ,
61
85
[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 ))],
64
88
[],
65
89
)
66
90
else :
67
91
yield (
68
92
lineno ,
69
93
'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 ))),
72
96
[])
73
97
else :
74
98
if message_context :
75
99
yield (
76
100
lineno ,
77
101
'pgettext' ,
78
102
[smart_text (message_context ),
79
- smart_text (u'' . join (singular ))],
103
+ smart_text (join_tokens (singular , trimmed ))],
80
104
[],
81
105
)
82
106
else :
83
107
yield (
84
108
lineno ,
85
109
None ,
86
- smart_text (u'' . join (singular )),
110
+ smart_text (join_tokens (singular , trimmed )),
87
111
[])
88
112
89
113
intrans = False
@@ -135,6 +159,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
135
159
yield lineno , None , smart_text (stripped_fmatch ), []
136
160
intrans = True
137
161
inplural = False
162
+ trimmed = 'trimmed' in t .split_contents ()
138
163
singular = []
139
164
plural = []
140
165
elif cmatches :
0 commit comments