Author: mtredinnick
Date: 2007-11-16 22:04:12 -0600 (Fri, 16 Nov 2007)
New Revision: 6679
Modified:
django/trunk/django/template/__init__.py
django/trunk/tests/regressiontests/templates/tests.py
Log:
Fixed #4713 -- Fixed handling of _() in template tag arguments. Based on
patched from Indy and SmileyChris.
Modified: django/trunk/django/template/__init__.py
===================================================================
--- django/trunk/django/template/__init__.py 2007-11-15 09:26:57 UTC (rev
6678)
+++ django/trunk/django/template/__init__.py 2007-11-17 04:04:12 UTC (rev
6679)
@@ -678,6 +678,7 @@
self.var = var
self.literal = None
self.lookups = None
+ self.translate = False
try:
# First try to treat this variable as a number.
@@ -698,11 +699,15 @@
except ValueError:
# A ValueError means that the variable isn't a number.
+ if var.startswith('_(') and var.endswith(')'):
+ # The result of the lookup should be translated at rendering
+ # time.
+ self.translate = True
+ var = var[2:-1]
# If it's wrapped with quotes (single or double), then
# we're also dealing with a literal.
if var[0] in "\"'" and var[0] == var[-1]:
self.literal = var[1:-1]
-
else:
# Otherwise we'll set self.lookups so that resolve() knows
we're
# dealing with a bonafide variable
@@ -712,10 +717,13 @@
"""Resolve this variable against a given context."""
if self.lookups is not None:
# We're dealing with a variable that needs to be resolved
- return self._resolve_lookup(context)
+ value = self._resolve_lookup(context)
else:
# We're dealing with a literal, so it's already been "resolved"
- return self.literal
+ value = self.literal
+ if self.translate:
+ return _(value)
+ return value
def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.var)
Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py 2007-11-15
09:26:57 UTC (rev 6678)
+++ django/trunk/tests/regressiontests/templates/tests.py 2007-11-17
04:04:12 UTC (rev 6679)
@@ -734,8 +734,10 @@
# usage of the get_available_languages tag
'i18n12': ('{% load i18n %}{% get_available_languages as langs
%}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal
%}{% endfor %}', {}, 'de'),
- # translation of a constant string
+ # translation of constant strings
'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'},
'Seite nicht gefunden'),
+ 'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %}
{% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
+ 'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE':
'de', 'absent': ""}, 'Passwort'),
### HANDLING OF TEMPLATE_STRING_IF_INVALID
###################################
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---