Ticket #4713: 4713.patch

File 4713.patch, 1.8 KB (added by Chris Beaven, 17 years ago)

Better take on it

  • django/template/__init__.py

     
    642642    """
    643643    return Variable(path).resolve(context)
    644644
     645trans_re = re.compile(r'_\((.*)\)$')
     646
    645647class Variable(object):
    646648    """
    647649    A template variable, resolvable against a given context. The variable may be
     
    667669        self.var = var
    668670        self.literal = None
    669671        self.lookups = None
     672        self.translate = False
    670673       
    671674        try:
    672675            # First try to treat this variable as a number.
     
    687690
    688691        except ValueError:
    689692            # A ValueError means that the variable isn't a number.
     693
     694            # Check to see if we're dealing with a translatable var:
     695            translate = trans_re.match(var)
     696            if translate:
     697                self.translate = True
     698                var = translate.group(1)
     699
    690700            # If it's wrapped with quotes (single or double), then
    691701            # we're also dealing with a literal.
    692702            if var[0] in "\"'" and var[0] == var[-1]:
     
    701711        """Resolve this variable against a given context."""
    702712        if self.lookups is not None:
    703713            # We're dealing with a variable that needs to be resolved
    704             return self._resolve_lookup(context)
     714            var = self._resolve_lookup(context)
    705715        else:
    706716            # We're dealing with a literal, so it's already been "resolved"
    707             return self.literal
     717            var = self.literal
     718        if self.translate:
     719            # We need to translate the variable
     720            var = _(var)
     721        return var
    708722           
    709723    def __repr__(self):
    710724        return "<%s: %r>" % (self.__class__.__name__, self.var)
Back to Top