Skip to content

Commit 05d906c

Browse files
committed
Merge pull request #26 from tsouvarev/strip-quotes-from-constants
Strip quotes from translations via _()
2 parents 42493cb + 00e92e7 commit 05d906c

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

django_babel/extract.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
from django.utils.encoding import smart_text
1111

1212

13+
def strip_quotes(s):
14+
if (s[0] == s[-1]) and s.startswith(("'", '"')):
15+
return s[1:-1]
16+
return s
17+
18+
1319
def extract_django(fileobj, keywords, comment_tags, options):
1420
"""Extract messages from Django template files.
1521
@@ -107,10 +113,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
107113
cmatches = constant_re.findall(t.contents)
108114
if imatch:
109115
g = imatch.group(1)
110-
if g[0] == '"':
111-
g = g.strip('"')
112-
elif g[0] == "'":
113-
g = g.strip("'")
116+
g = strip_quotes(g)
114117
message_context = imatch.group(3)
115118
if message_context:
116119
# strip quotes
@@ -128,28 +131,28 @@ def extract_django(fileobj, keywords, comment_tags, options):
128131
if bmatch.group(2):
129132
message_context = bmatch.group(2)[1:-1]
130133
for fmatch in constant_re.findall(t.contents):
131-
yield lineno, None, smart_text(fmatch), []
134+
stripped_fmatch = strip_quotes(fmatch)
135+
yield lineno, None, smart_text(stripped_fmatch), []
132136
intrans = True
133137
inplural = False
134138
singular = []
135139
plural = []
136140
elif cmatches:
137141
for cmatch in cmatches:
138-
yield lineno, None, smart_text(cmatch), []
142+
stripped_cmatch = strip_quotes(cmatch)
143+
yield lineno, None, smart_text(stripped_cmatch), []
139144
elif t.token_type == TOKEN_VAR:
140145
parts = t.contents.split('|')
141146
cmatch = constant_re.match(parts[0])
142147
if cmatch:
143-
yield lineno, None, smart_text(cmatch.group(1)), []
148+
stripped_cmatch = strip_quotes(cmatch.group(1))
149+
yield lineno, None, smart_text(stripped_cmatch), []
144150
for p in parts[1:]:
145151
if p.find(':_(') >= 0:
146152
p1 = p.split(':', 1)[1]
147153
if p1[0] == '_':
148154
p1 = p1[1:]
149155
if p1[0] == '(':
150156
p1 = p1.strip('()')
151-
if p1[0] == "'":
152-
p1 = p1.strip("'")
153-
elif p1[0] == '"':
154-
p1 = p1.strip('"')
157+
p1 = strip_quotes(p1)
155158
yield lineno, None, smart_text(p1), []

django_babel/templatetags/babel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
23

34
from babel import support as babel_support
45
from babel import core as babel_core

tests/test_extract.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ def test_extract_filters_default_translatable_single_quotes(self):
154154
def test_extract_constant_single_quotes(self):
155155
buf = BytesIO(b"{{ _('constant') }}")
156156
messages = list(extract_django(buf, default_keys, [], {}))
157-
self.assertEqual([(1, None, u"'constant'", [])], messages)
157+
self.assertEqual([(1, None, u'constant', [])], messages)
158158

159159
def test_extract_constant_double_quotes(self):
160160
buf = BytesIO(b'{{ _("constant") }}')
161161
messages = list(extract_django(buf, default_keys, [], {}))
162-
self.assertEqual([(1, None, u'"constant"', [])], messages)
162+
self.assertEqual([(1, None, u'constant', [])], messages)
163163

164164
def test_extract_constant_block(self):
165165
buf = BytesIO(b'{% _("constant") %}')
166166
messages = list(extract_django(buf, default_keys, [], {}))
167-
self.assertEqual([(1, None, u'"constant"', [])], messages)
167+
self.assertEqual([(1, None, u'constant', [])], messages)
168168

169169
def test_extract_constant_in_block(self):
170170
test_tmpl = (
@@ -173,7 +173,7 @@ def test_extract_constant_in_block(self):
173173
buf = BytesIO(test_tmpl)
174174
messages = list(extract_django(buf, default_keys, [], {}))
175175
self.assertEqual(
176-
[(1, None, u'"constant"', []), (1, None, u'%(foo)s', [])],
176+
[(1, None, u'constant', []), (1, None, u'%(foo)s', [])],
177177
messages,
178178
)
179179

0 commit comments

Comments
 (0)