Ticket #5972: trans_understands_filter_alternate_version.4.diff
File trans_understands_filter_alternate_version.4.diff, 3.0 KB (added by , 17 years ago) |
---|
-
django/templatetags/i18n.py
34 34 return '' 35 35 36 36 class TranslateNode(Node): 37 def __init__(self, value, noop):38 self. value = Variable(value)37 def __init__(self, filter_expression, noop): 38 self.filter_expression = filter_expression 39 39 self.noop = noop 40 40 41 41 def render(self, context): 42 value = self.value.resolve(context) 43 if self.noop: 44 return value 45 else: 46 return translation.ugettext(value) 42 43 if not self.noop: 44 translated = translation.ugettext(self.filter_expression.var.resolve(context)) 45 self.filter_expression.var = Variable('"'+translated+'"') 46 # Filters are applied here 47 return self.filter_expression.resolve(context) 47 48 48 49 class BlockTranslateNode(Node): 49 50 def __init__(self, extra_context, singular, plural=None, countervar=None, … … 171 174 class TranslateParser(TokenParser): 172 175 def top(self): 173 176 value = self.value() 177 178 # Backwards Compatiblity fix: 179 # FilterExpression does not support single-quoted strings, 180 # so we make a cheap localized fix in order to maintain 181 # backwards compatibility with existing uses of ``trans`` 182 # where single quote use is supported. 183 if value[0] == "'": 184 pos = None 185 m = re.match("^'([^']+)'(\|.*$)",value) 186 if m: 187 value = '"%s"%s' % (m.group(1).replace('"','\\"'),m.group(2)) 188 elif value[-1] == "'": 189 value = '"%s"' % value[1:-1].replace('"','\\"') 190 174 191 if self.more(): 175 192 if self.tag() == 'noop': 176 193 noop = True … … 180 197 noop = False 181 198 return (value, noop) 182 199 value, noop = TranslateParser(token.contents).top() 183 return TranslateNode( value, noop)200 return TranslateNode(parser.compile_filter(value), noop) 184 201 185 202 def do_block_translate(parser, token): 186 203 """ -
django/template/__init__.py
547 557 if var == None: 548 558 var, constant, i18n_constant = match.group("var", "constant", "i18n_constant") 549 559 if i18n_constant: 550 var = '"%s"' % _(i18n_constant)560 var = "'%s'" % _(i18n_constant.replace(r'\"', '"')) 551 561 elif constant: 552 var = '"%s"' % constant562 var = "'%s'" % constant.replace(r'\"', '"') 553 563 upto = match.end() 554 564 if var == None: 555 565 raise TemplateSyntaxError, "Could not find variable at start of %s" % token