Django

Code

Changeset 6679

Show
Ignore:
Timestamp:
11/16/07 22:04:12 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4713 -- Fixed handling of _() in template tag arguments. Based on
patched from Indy and SmileyChris?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/__init__.py

    r6671 r6679  
    679679        self.literal = None 
    680680        self.lookups = None 
     681        self.translate = False 
    681682 
    682683        try: 
     
    699700        except ValueError: 
    700701            # A ValueError means that the variable isn't a number. 
     702            if var.startswith('_(') and var.endswith(')'): 
     703                # The result of the lookup should be translated at rendering 
     704                # time. 
     705                self.translate = True 
     706                var = var[2:-1] 
    701707            # If it's wrapped with quotes (single or double), then 
    702708            # we're also dealing with a literal. 
    703709            if var[0] in "\"'" and var[0] == var[-1]: 
    704710                self.literal = var[1:-1] 
    705  
    706711            else: 
    707712                # Otherwise we'll set self.lookups so that resolve() knows we're 
     
    713718        if self.lookups is not None: 
    714719            # We're dealing with a variable that needs to be resolved 
    715             return self._resolve_lookup(context) 
     720            value = self._resolve_lookup(context) 
    716721        else: 
    717722            # We're dealing with a literal, so it's already been "resolved" 
    718             return self.literal 
     723            value = self.literal 
     724        if self.translate: 
     725            return _(value) 
     726        return value 
    719727 
    720728    def __repr__(self): 
  • django/trunk/tests/regressiontests/templates/tests.py

    r6673 r6679  
    735735            'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), 
    736736 
    737             # translation of a constant string 
     737            # translation of constant strings 
    738738            'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'), 
     739            'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'), 
     740            'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 
    739741 
    740742            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################