Add support for "trimmed" blocktrans content

This commit is contained in:
Rob Hudson 2015-12-30 10:30:04 -08:00
parent 5d953ef9e3
commit 0e74e0d113
2 changed files with 51 additions and 7 deletions

View File

@ -5,9 +5,32 @@ except ImportError:
# Django 1.8 moved most stuff to .base
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 (
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):
@ -25,6 +48,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
"""
intrans = False
inplural = False
trimmed = False
message_context = None
singular = []
plural = []
@ -53,16 +77,16 @@ def extract_django(fileobj, keywords, comment_tags, options):
lineno,
'npgettext',
[smart_text(message_context),
smart_text(u''.join(singular)),
smart_text(u''.join(plural))],
smart_text(join_tokens(singular, trimmed)),
smart_text(join_tokens(plural, trimmed))],
[],
)
else:
yield (
lineno,
'ngettext',
(smart_text(u''.join(singular)),
smart_text(u''.join(plural))),
(smart_text(join_tokens(singular, trimmed)),
smart_text(join_tokens(plural, trimmed))),
[])
else:
if message_context:
@ -70,14 +94,14 @@ def extract_django(fileobj, keywords, comment_tags, options):
lineno,
'pgettext',
[smart_text(message_context),
smart_text(u''.join(singular))],
smart_text(join_tokens(singular, trimmed))],
[],
)
else:
yield (
lineno,
None,
smart_text(u''.join(singular)),
smart_text(join_tokens(singular, trimmed)),
[])
intrans = False
@ -131,6 +155,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
yield lineno, None, smart_text(fmatch), []
intrans = True
inplural = False
trimmed = 'trimmed' in t.split_contents()
singular = []
plural = []
elif cmatches:

View File

@ -6,6 +6,7 @@ import pytest
from babel.messages import extract
from babel._compat import BytesIO
import 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'], [])],
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)