Ticket #5885: template_simple_tag_i18n_fix.2.diff

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

Previous patch was dirty. This one is a little better. It allows single or double quotes, and throws AttributeError if there are non-matching quotes...

  • django/template/__init__.py

     
    127127        self.params = params
    128128
    129129    def __str__(self):
    130         return self.msg % self.params
    131 
     130        try:
     131            return self.msg % self.params
     132        except UnicodeDecodeError:
     133            return self.msg % (self.params[0],str(self.params[1]))
     134       
    132135class InvalidTemplateLibrary(Exception):
    133136    pass
    134137
     
    692695            if var[0] in "\"'" and var[0] == var[-1]:
    693696                self.literal = var[1:-1]
    694697           
     698            elif var[0:2] == "_(" and var[-1] == ")":
     699                match = re.match("\_\(('(?:[^']+')|(?:\"[^\"]+)\")\)",var)
     700                if match is not None:
     701                    self.literal = _(match.group(1)[1:-2])
     702                else:
     703                    raise AttributeError('Invalid translation string %s.'%var)
    695704            else:
    696705                # Otherwise we'll set self.lookups so that resolve() knows we're
    697706                # dealing with a bonafide variable
Back to Top