Ticket #12104: blocktrans_display_error.diff

File blocktrans_display_error.diff, 1.5 KB (added by philipn, 15 years ago)

displays templateerror instead of keyerror

  • django/templatetags/i18n.py

     
    11import re
    22
    3 from django.template import Node, Variable, VariableNode, _render_value_in_context
     3from django.template import Node, Variable, VariableDoesNotExist, VariableNode, _render_value_in_context
    44from django.template import TemplateSyntaxError, TokenParser, Library
    55from django.template import TOKEN_TEXT, TOKEN_VAR
    66from django.utils import translation
     
    8282            result = translation.ugettext(singular)
    8383        # Escape all isolated '%' before substituting in the context.
    8484        result = re.sub(u'%(?!\()', u'%%', result)
    85         data = dict([(v, _render_value_in_context(context[v], context)) for v in vars])
     85        data = []
     86        for v in vars:
     87            if not context.has_key(v):
     88                try:
     89                    Variable(v).resolve(context)
     90                except VariableDoesNotExist:
     91                    # This is what should happen
     92                    pass
     93                else:
     94                    raise TemplateSyntaxError, ("'blocktrans' doesn't allow "
     95                        "variable.attribute lookup.  "
     96                        "Use {% blocktrans variable.attr as foo .. %} instead")
     97            data.append((v, _render_value_in_context(context[v], context)))
     98        data = dict(data)
    8699        context.pop()
    87100        return result % data
    88101
Back to Top