From 0e74e0d11389c2d88d78492bb0945ba188183f3b Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Wed, 30 Dec 2015 10:30:04 -0800 Subject: [PATCH] Add support for "trimmed" blocktrans content --- django_babel/extract.py | 39 ++++++++++++++++++++++++++++++++------- tests/test_extract.py | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/django_babel/extract.py b/django_babel/extract.py index 1cd92a4..d17e83d 100644 --- a/django_babel/extract.py +++ b/django_babel/extract.py @@ -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: diff --git a/tests/test_extract.py b/tests/test_extract.py index 4f64a89..ab18a25 100644 --- a/tests/test_extract.py +++ b/tests/test_extract.py @@ -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)