Ticket #10369: i18n_autoescape_t10369_r10178.2.diff
File i18n_autoescape_t10369_r10178.2.diff, 4.1 KB (added by , 16 years ago) |
---|
-
django/templatetags/i18n.py
1 1 import re 2 2 3 from django.template import Node, Variable, VariableNode 3 from django.template import Node, Variable, VariableNode, _render_value_in_context 4 4 from django.template import TemplateSyntaxError, TokenParser, Library 5 5 from django.template import TOKEN_TEXT, TOKEN_VAR 6 6 from django.utils import translation … … 43 43 if self.noop: 44 44 return value 45 45 else: 46 return translation.ugettext(value)46 return _render_value_in_context(translation.ugettext(value), context) 47 47 48 48 class BlockTranslateNode(Node): 49 49 def __init__(self, extra_context, singular, plural=None, countervar=None, … … 82 82 result = translation.ugettext(singular) 83 83 # Escape all isolated '%' before substituting in the context. 84 84 result = re.sub(u'%(?!\()', u'%%', result) 85 data = dict([(v, force_unicode(context[v])) for v in vars])85 data = dict([(v, _render_value_in_context(context[v], context)) for v in vars]) 86 86 context.pop() 87 87 return result % data 88 88 -
django/template/__init__.py
802 802 803 803 def render(self, context): 804 804 return self.s 805 806 def _render_value_in_context(value, context): 807 """ 808 Converts any value to a string to become part of a rendered template. This 809 means escaping, if required, and conversion to a unicode object. If value 810 is a string, it is expected to have already been translated. 811 """ 812 value = force_unicode(value) 813 if (context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData): 814 return escape(value) 815 else: 816 return value 805 817 806 818 class VariableNode(Node): 807 819 def __init__(self, filter_expression): … … 812 824 813 825 def render(self, context): 814 826 try: 815 output = force_unicode(self.filter_expression.resolve(context))827 output = self.filter_expression.resolve(context) 816 828 except UnicodeDecodeError: 817 829 # Unicode conversion can fail sometimes for reasons out of our 818 830 # control (e.g. exception rendering). In that case, we fail quietly. 819 831 return '' 820 if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData): 821 return force_unicode(escape(output)) 822 else: 823 return force_unicode(output) 832 return _render_value_in_context(output, context) 824 833 825 834 def generic_tag_compiler(params, defaults, name, node_class, parser, token): 826 835 "Returns a template.Node subclass." -
tests/regressiontests/templates/tests.py
806 806 'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 807 807 'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), 808 808 809 # Escaping inside blocktrans works as if it was directly in the809 # Escaping inside blocktrans and trans works as if it was directly in the 810 810 # template. 811 811 'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 812 812 'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 813 'i18n19': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': 'a & b'}, u'a & b'), 814 'i18n20': ('{% load i18n %}{% trans andrew %}', {'andrew': 'a & b'}, u'a & b'), 813 815 814 816 ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### 815 817