Author: jezdez
Date: 2011-08-30 05:09:45 -0700 (Tue, 30 Aug 2011)
New Revision: 16712

Modified:
   django/trunk/django/templatetags/i18n.py
   django/trunk/docs/releases/1.4.txt
   django/trunk/docs/topics/i18n/internationalization.txt
   django/trunk/tests/regressiontests/templates/tests.py
Log:
Fixed #16717 -- Added ability to store result of trans template tag in context 
variable.

Modified: django/trunk/django/templatetags/i18n.py
===================================================================
--- django/trunk/django/templatetags/i18n.py    2011-08-30 08:14:35 UTC (rev 
16711)
+++ django/trunk/django/templatetags/i18n.py    2011-08-30 12:09:45 UTC (rev 
16712)
@@ -70,8 +70,9 @@
 
 
 class TranslateNode(Node):
-    def __init__(self, filter_expression, noop):
+    def __init__(self, filter_expression, noop, asvar=None):
         self.noop = noop
+        self.asvar = asvar
         self.filter_expression = filter_expression
         if isinstance(self.filter_expression.var, basestring):
             self.filter_expression.var = Variable(u"'%s'" % 
self.filter_expression.var)
@@ -79,7 +80,12 @@
     def render(self, context):
         self.filter_expression.var.translate = not self.noop
         output = self.filter_expression.resolve(context)
-        return _render_value_in_context(output, context)
+        value = _render_value_in_context(output, context)
+        if self.asvar:
+            context[self.asvar] = value
+            return ''
+        else:
+            return value
 
 
 class BlockTranslateNode(Node):
@@ -296,16 +302,21 @@
                 elif value[-1] == "'":
                     value = '"%s"' % value[1:-1].replace('"','\\"')
 
-            if self.more():
-                if self.tag() == 'noop':
+            noop = False
+            asvar = None
+
+            while self.more():
+                tag = self.tag()
+                if tag == 'noop':
                     noop = True
+                elif tag == 'as':
+                    asvar = self.tag()
                 else:
-                    raise TemplateSyntaxError("only option for 'trans' is 
'noop'")
-            else:
-                noop = False
-            return (value, noop)
-    value, noop = TranslateParser(token.contents).top()
-    return TranslateNode(parser.compile_filter(value), noop)
+                    raise TemplateSyntaxError(
+                        "only options for 'trans' are 'noop' and 'as VAR.")
+            return (value, noop, asvar)
+    value, noop, asvar = TranslateParser(token.contents).top()
+    return TranslateNode(parser.compile_filter(value), noop, asvar)
 
 @register.tag("blocktrans")
 def do_block_translate(parser, token):

Modified: django/trunk/docs/releases/1.4.txt
===================================================================
--- django/trunk/docs/releases/1.4.txt  2011-08-30 08:14:35 UTC (rev 16711)
+++ django/trunk/docs/releases/1.4.txt  2011-08-30 12:09:45 UTC (rev 16712)
@@ -283,6 +283,10 @@
   about :ref:`the 403 (HTTP Forbidden) view<http_forbidden_view>` for more
   information.
 
+* The :ttag:`trans` template tag now takes an optional ``as`` argument to
+  be able to retrieve a translation string without displaying it but setting
+  a template context variable instead.
+
 .. _backwards-incompatible-changes-1.4:
 
 Backwards incompatible changes in 1.4

Modified: django/trunk/docs/topics/i18n/internationalization.txt
===================================================================
--- django/trunk/docs/topics/i18n/internationalization.txt      2011-08-30 
08:14:35 UTC (rev 16711)
+++ django/trunk/docs/topics/i18n/internationalization.txt      2011-08-30 
12:09:45 UTC (rev 16712)
@@ -451,6 +451,32 @@
 %}``. If your translations require strings with variables (placeholders), use
 ``{% blocktrans %}`` instead.
 
+.. versionchanged:: 1.4
+
+If you'd like to retrieve a translated string without displaying it, you can
+use the following syntax::
+
+    {% trans "This is the title" as the_title %}
+
+    <title>{{ the_title }}</title>
+    <meta name="description" content="{{ the_title }}">
+
+In practice you'll use this to get strings that are used in multiple places
+or should be used as arguments for other template tags or filters::
+
+    {% trans "starting point" as start %}
+    {% trans "end point" as end %}
+    {% trans "La Grande Boucle" as race %}
+
+    <h1>
+      <a href="/" title="{% blocktrans %}Back to '{{ race }}' homepage{% 
endblocktrans %}">{{ race }}</a>
+    </h1>
+    <p>
+    {% for stage in tour_stages %}
+        {% cycle start end %}: {{ stage }}{% if forloop.counter|divisibleby:2 
%}<br />{% else %}, {% endif %}
+    {% endfor %}
+    </p>
+
 .. templatetag:: blocktrans
 
 ``blocktrans`` template tag

Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py       2011-08-30 
08:14:35 UTC (rev 16711)
+++ django/trunk/tests/regressiontests/templates/tests.py       2011-08-30 
12:09:45 UTC (rev 16712)
@@ -1294,6 +1294,12 @@
             # blocktrans handling of variables which are not in the context.
             'i18n34': ('{% load i18n %}{% blocktrans %}{{ missing }}{% 
endblocktrans %}', {}, u''),
 
+            # trans tag with as var
+            'i18n35': ('{% load i18n %}{% trans "Page not found" as 
page_not_found %}{{ page_not_found }}', {'LANGUAGE_CODE': 'de'}, "Seite nicht 
gefunden"),
+            'i18n36': ('{% load i18n %}{% trans "Page not found" noop as 
page_not_found %}{{ page_not_found }}', {'LANGUAGE_CODE': 'de'}, "Page not 
found"),
+            'i18n36': ('{% load i18n %}{% trans "Page not found" as 
page_not_found noop %}{{ page_not_found }}', {'LANGUAGE_CODE': 'de'}, "Page not 
found"),
+            'i18n37': ('{% load i18n %}{% trans "Page not found" as 
page_not_found %}{% blocktrans %}Error: {{ page_not_found }}{% endblocktrans 
%}', {'LANGUAGE_CODE': 'de'}, "Error: Seite nicht gefunden"),
+
             ### HANDLING OF TEMPLATE_STRING_IF_INVALID 
###################################
 
             'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','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.

Reply via email to