diff options
| author | Matthew Somerville <matthew-github@dracos.co.uk> | 2015-06-12 13:51:28 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-07-01 10:03:00 -0400 |
| commit | 839edcebb39f55acf163266f1ce1f0dc537de95b (patch) | |
| tree | afca4d9d29d470d54673e87e8147f48a3d0c8d33 /django/templatetags | |
| parent | b35b43dff81d46e930ffa1e05eb50968b8557102 (diff) | |
Fixed #21695 -- Added asvar option to blocktrans.
Thanks Bojan Mihelac for the initial patch.
Diffstat (limited to 'django/templatetags')
| -rw-r--r-- | django/templatetags/i18n.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py index ace6504ffe..4e77aaece4 100644 --- a/django/templatetags/i18n.py +++ b/django/templatetags/i18n.py @@ -96,7 +96,7 @@ class TranslateNode(Node): class BlockTranslateNode(Node): def __init__(self, extra_context, singular, plural=None, countervar=None, - counter=None, message_context=None, trimmed=False): + counter=None, message_context=None, trimmed=False, asvar=None): self.extra_context = extra_context self.singular = singular self.plural = plural @@ -104,6 +104,7 @@ class BlockTranslateNode(Node): self.counter = counter self.message_context = message_context self.trimmed = trimmed + self.asvar = asvar def render_token_list(self, tokens): result = [] @@ -166,7 +167,11 @@ class BlockTranslateNode(Node): "string returned by gettext: %r using %r" % (result, data)) with translation.override(None): result = self.render(context, nested=True) - return result + if self.asvar: + context[self.asvar] = result + return '' + else: + return result class LanguageNode(Node): @@ -429,6 +434,13 @@ def do_block_translate(parser, token): {% blocktrans with foo|filter as bar and baz|filter as boo %} {% blocktrans count var|length as count %} + The translated string can be stored in a variable using `asvar`:: + + {% blocktrans with bar=foo|filter boo=baz|filter asvar var %} + This is {{ bar }} and {{ boo }}. + {% endblocktrans %} + {{ var }} + Contextual translations are supported:: {% blocktrans with bar=foo|filter context "greeting" %} @@ -442,6 +454,7 @@ def do_block_translate(parser, token): options = {} remaining_bits = bits[1:] + asvar = None while remaining_bits: option = remaining_bits.pop(0) if option in options: @@ -468,6 +481,13 @@ def do_block_translate(parser, token): six.reraise(TemplateSyntaxError, TemplateSyntaxError(msg), sys.exc_info()[2]) elif option == "trimmed": value = True + elif option == "asvar": + try: + value = remaining_bits.pop(0) + except IndexError: + msg = "No argument provided to the '%s' tag for the asvar option." % bits[0] + six.reraise(TemplateSyntaxError, TemplateSyntaxError(msg), sys.exc_info()[2]) + asvar = value else: raise TemplateSyntaxError('Unknown argument for %r tag: %r.' % (bits[0], option)) @@ -506,7 +526,8 @@ def do_block_translate(parser, token): raise TemplateSyntaxError("'blocktrans' doesn't allow other block tags (seen %r) inside it" % token.contents) return BlockTranslateNode(extra_context, singular, plural, countervar, - counter, message_context, trimmed=trimmed) + counter, message_context, trimmed=trimmed, + asvar=asvar) @register.tag |
