Skip to content

Commit

Permalink
Merge pull request #25 from robhudson/trimmed-support
Browse files Browse the repository at this point in the history
Add support for "trimmed" blocktrans content
  • Loading branch information
mrunge committed Mar 30, 2016
2 parents 05d906c + 0e74e0d commit 3e4efa0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
39 changes: 32 additions & 7 deletions django_babel/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,32 @@
# 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 strip_quotes(s):
Expand All @@ -31,6 +54,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
"""
intrans = False
inplural = False
trimmed = False
message_context = None
singular = []
plural = []
Expand Down Expand Up @@ -59,31 +83,31 @@ 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:
yield (
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
Expand Down Expand Up @@ -135,6 +159,7 @@ def extract_django(fileobj, keywords, comment_tags, options):
yield lineno, None, smart_text(stripped_fmatch), []
intrans = True
inplural = False
trimmed = 'trimmed' in t.split_contents()
singular = []
plural = []
elif cmatches:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from babel.messages import extract
from babel._compat import BytesIO

import django
from django_babel.extract import extract_django


Expand Down Expand Up @@ -199,3 +200,21 @@ def test_extract_context_in_plural_block(self):
[(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)

0 comments on commit 3e4efa0

Please sign in to comment.