Add support for "trimmed" blocktrans content
This commit is contained in:
parent
5d953ef9e3
commit
0e74e0d113
|
@ -5,9 +5,32 @@ except ImportError:
|
||||||
# Django 1.8 moved most stuff to .base
|
# Django 1.8 moved most stuff to .base
|
||||||
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
|
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
|
||||||
|
|
||||||
|
try:
|
||||||
|
from django.utils.translation import trim_whitespace as trim_django
|
||||||
|
except ImportError:
|
||||||
|
trim_django = False
|
||||||
|
|
||||||
|
from django.utils.encoding import smart_text
|
||||||
from django.utils.translation.trans_real import (
|
from django.utils.translation.trans_real import (
|
||||||
inline_re, block_re, endblock_re, plural_re, constant_re)
|
inline_re, block_re, endblock_re, plural_re, constant_re)
|
||||||
from django.utils.encoding import smart_text
|
|
||||||
|
|
||||||
|
def trim_whitespace(string):
|
||||||
|
"""Trim whitespace.
|
||||||
|
|
||||||
|
This is only supported in Django>=1.7. This method help in cases of older
|
||||||
|
Django versions.
|
||||||
|
"""
|
||||||
|
if trim_django:
|
||||||
|
return trim_django(string)
|
||||||
|
return string
|
||||||
|
|
||||||
|
|
||||||
|
def join_tokens(tokens, trim=False):
|
||||||
|
message = ''.join(tokens)
|
||||||
|
if trim:
|
||||||
|
message = trim_whitespace(message)
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
def extract_django(fileobj, keywords, comment_tags, options):
|
def extract_django(fileobj, keywords, comment_tags, options):
|
||||||
|
@ -25,6 +48,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
|
||||||
"""
|
"""
|
||||||
intrans = False
|
intrans = False
|
||||||
inplural = False
|
inplural = False
|
||||||
|
trimmed = False
|
||||||
message_context = None
|
message_context = None
|
||||||
singular = []
|
singular = []
|
||||||
plural = []
|
plural = []
|
||||||
|
@ -53,16 +77,16 @@ def extract_django(fileobj, keywords, comment_tags, options):
|
||||||
lineno,
|
lineno,
|
||||||
'npgettext',
|
'npgettext',
|
||||||
[smart_text(message_context),
|
[smart_text(message_context),
|
||||||
smart_text(u''.join(singular)),
|
smart_text(join_tokens(singular, trimmed)),
|
||||||
smart_text(u''.join(plural))],
|
smart_text(join_tokens(plural, trimmed))],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
yield (
|
yield (
|
||||||
lineno,
|
lineno,
|
||||||
'ngettext',
|
'ngettext',
|
||||||
(smart_text(u''.join(singular)),
|
(smart_text(join_tokens(singular, trimmed)),
|
||||||
smart_text(u''.join(plural))),
|
smart_text(join_tokens(plural, trimmed))),
|
||||||
[])
|
[])
|
||||||
else:
|
else:
|
||||||
if message_context:
|
if message_context:
|
||||||
|
@ -70,14 +94,14 @@ def extract_django(fileobj, keywords, comment_tags, options):
|
||||||
lineno,
|
lineno,
|
||||||
'pgettext',
|
'pgettext',
|
||||||
[smart_text(message_context),
|
[smart_text(message_context),
|
||||||
smart_text(u''.join(singular))],
|
smart_text(join_tokens(singular, trimmed))],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
yield (
|
yield (
|
||||||
lineno,
|
lineno,
|
||||||
None,
|
None,
|
||||||
smart_text(u''.join(singular)),
|
smart_text(join_tokens(singular, trimmed)),
|
||||||
[])
|
[])
|
||||||
|
|
||||||
intrans = False
|
intrans = False
|
||||||
|
@ -131,6 +155,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
|
||||||
yield lineno, None, smart_text(fmatch), []
|
yield lineno, None, smart_text(fmatch), []
|
||||||
intrans = True
|
intrans = True
|
||||||
inplural = False
|
inplural = False
|
||||||
|
trimmed = 'trimmed' in t.split_contents()
|
||||||
singular = []
|
singular = []
|
||||||
plural = []
|
plural = []
|
||||||
elif cmatches:
|
elif cmatches:
|
||||||
|
|
|
@ -6,6 +6,7 @@ import pytest
|
||||||
from babel.messages import extract
|
from babel.messages import extract
|
||||||
from babel._compat import BytesIO
|
from babel._compat import BytesIO
|
||||||
|
|
||||||
|
import django
|
||||||
from django_babel.extract import extract_django
|
from django_babel.extract import extract_django
|
||||||
|
|
||||||
|
|
||||||
|
@ -199,3 +200,21 @@ class ExtractDjangoTestCase(unittest.TestCase):
|
||||||
[(1, 'npgettext', [u'banana', u'%(foo)s', u'%(bar)s'], [])],
|
[(1, 'npgettext', [u'banana', u'%(foo)s', u'%(bar)s'], [])],
|
||||||
messages,
|
messages,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_blocktrans_with_whitespace_not_trimmed(self):
|
||||||
|
test_tmpl = (
|
||||||
|
b'{% blocktrans %}\n\tfoo\n\tbar\n{% endblocktrans %}'
|
||||||
|
)
|
||||||
|
buf = BytesIO(test_tmpl)
|
||||||
|
messages = list(extract_django(buf, default_keys, [], {}))
|
||||||
|
self.assertEqual([(4, None, u'\n\tfoo\n\tbar\n', [])], messages)
|
||||||
|
|
||||||
|
@pytest.mark.skipif(django.VERSION < (1, 7),
|
||||||
|
reason='Trimmed whitespace is a Django >= 1.7 feature')
|
||||||
|
def test_blocktrans_with_whitespace_trimmed(self):
|
||||||
|
test_tmpl = (
|
||||||
|
b'{% blocktrans trimmed %}\n\tfoo\n\tbar\n{% endblocktrans %}'
|
||||||
|
)
|
||||||
|
buf = BytesIO(test_tmpl)
|
||||||
|
messages = list(extract_django(buf, default_keys, [], {}))
|
||||||
|
self.assertEqual([(4, None, u'foo bar', [])], messages)
|
||||||
|
|
Loading…
Reference in New Issue