Ticket #5972: trans_understands_filter_alternate_version.2.diff

File trans_understands_filter_alternate_version.2.diff, 2.2 KB (added by Dmitri Fedortchenko <zeraien@…>, 7 years ago)

the backwards compatiblity fix in the previous patch was a little weak. This one does the trick a little better.

  • django/templatetags/i18n.py

     
    3434        return ''
    3535
    3636class 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
    3939        self.noop = noop
    4040
    4141    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            filter_expression = self.filter_expression.var.resolve(context)
     45            translated = translation.ugettext(filter_expression)
     46            self.filter_expression.var = Variable('"'+translated+'"')
     47        # Filters are applied here, since this is a FilterExpression
     48        return self.filter_expression.resolve(context)
    4749
    4850class BlockTranslateNode(Node):
    4951    def __init__(self, extra_context, singular, plural=None, countervar=None,
     
    171173    class TranslateParser(TokenParser):
    172174        def top(self):
    173175            value = self.value()
     176
     177            # Backwards Compatiblity fix:
     178            # FilterExpression does not support single-quoted strings,
     179            # so we make a cheap localized fix in order to maintain
     180            # backwards compatibility with existing uses of ``trans``
     181            # where single quote use is supported.
     182            if value[0] == "'":
     183                pos = None
     184                m = re.match("^'([^']+)'(\|.*$)",value)
     185                if m:
     186                    value = '"%s"%s' % (m.group(1),m.group(2))
     187                elif value[-1] == "'":
     188                    value = '"%s"' % value[1:-1]
     189               
    174190            if self.more():
    175191                if self.tag() == 'noop':
    176192                    noop = True
     
    180196                noop = False
    181197            return (value, noop)
    182198    value, noop = TranslateParser(token.contents).top()
    183     return TranslateNode(value, noop)
     199    return TranslateNode(parser.compile_filter(value), noop)
    184200
    185201def do_block_translate(parser, token):
    186202    """
Back to Top