Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5972 - Allow the template filters to be used with the trans ta…

…g. Thanks for the initial patch, Dmitri Fedortchenko.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12472 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 84f8213d74817b8d5aaf81dfa611e919d5266360 1 parent b459f5b
Jannis Leidel authored February 21, 2010
4  django/template/__init__.py
@@ -551,8 +551,8 @@ def __init__(self, token, parser):
551 551
                 elif var_arg:
552 552
                     args.append((True, Variable(var_arg)))
553 553
                 filter_func = parser.find_filter(filter_name)
554  
-                self.args_check(filter_name,filter_func, args)
555  
-                filters.append( (filter_func,args))
  554
+                self.args_check(filter_name, filter_func, args)
  555
+                filters.append((filter_func, args))
556 556
             upto = match.end()
557 557
         if upto != len(token):
558 558
             raise TemplateSyntaxError("Could not parse the remainder: '%s' from '%s'" % (token[upto:], token))
4  django/template/defaulttags.py
@@ -3,10 +3,6 @@
3 3
 import sys
4 4
 import re
5 5
 from itertools import cycle as itertools_cycle
6  
-try:
7  
-    reversed
8  
-except NameError:
9  
-    from django.utils.itercompat import reversed     # Python 2.3 fallback
10 6
 
11 7
 from django.template import Node, NodeList, Template, Context, Variable
12 8
 from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
30  django/templatetags/i18n.py
@@ -34,16 +34,16 @@ def render(self, context):
34 34
         return ''
35 35
 
36 36
 class TranslateNode(Node):
37  
-    def __init__(self, value, noop):
38  
-        self.value = Variable(value)
  37
+    def __init__(self, filter_expression, noop):
39 38
         self.noop = noop
  39
+        self.filter_expression = filter_expression
  40
+        if isinstance(self.filter_expression.var, basestring):
  41
+            self.filter_expression.var = Variable(u"'%s'" % self.filter_expression.var)
40 42
 
41 43
     def render(self, context):
42  
-        value = self.value.resolve(context)
43  
-        if self.noop:
44  
-            return value
45  
-        else:
46  
-            return _render_value_in_context(translation.ugettext(value), context)
  44
+        self.filter_expression.var.translate = not self.noop
  45
+        output = self.filter_expression.resolve(context)
  46
+        return _render_value_in_context(output, context)
47 47
 
48 48
 class BlockTranslateNode(Node):
49 49
     def __init__(self, extra_context, singular, plural=None, countervar=None,
@@ -174,6 +174,20 @@ def do_translate(parser, token):
174 174
     class TranslateParser(TokenParser):
175 175
         def top(self):
176 176
             value = self.value()
  177
+
  178
+            # Backwards Compatiblity fix:
  179
+            # FilterExpression does not support single-quoted strings,
  180
+            # so we make a cheap localized fix in order to maintain
  181
+            # backwards compatibility with existing uses of ``trans``
  182
+            # where single quote use is supported.
  183
+            if value[0] == "'":
  184
+                pos = None
  185
+                m = re.match("^'([^']+)'(\|.*$)",value)
  186
+                if m:
  187
+                    value = '"%s"%s' % (m.group(1).replace('"','\\"'),m.group(2))
  188
+                elif value[-1] == "'":
  189
+                    value = '"%s"' % value[1:-1].replace('"','\\"')
  190
+
177 191
             if self.more():
178 192
                 if self.tag() == 'noop':
179 193
                     noop = True
@@ -183,7 +197,7 @@ def top(self):
183 197
                 noop = False
184 198
             return (value, noop)
185 199
     value, noop = TranslateParser(token.contents).top()
186  
-    return TranslateNode(value, noop)
  200
+    return TranslateNode(parser.compile_filter(value), noop)
187 201
 
188 202
 def do_block_translate(parser, token):
189 203
     """
5  tests/regressiontests/templates/tests.py
@@ -907,6 +907,11 @@ def get_template_tests(self):
907 907
             'i18n21': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': mark_safe('a & b')}, u'a & b'),
908 908
             'i18n22': ('{% load i18n %}{% trans andrew %}', {'andrew': mark_safe('a & b')}, u'a & b'),
909 909
 
  910
+            # Use filters with the {% trans %} tag, #5972
  911
+            'i18n23': ('{% load i18n %}{% trans "Page not found"|capfirst|slice:"6:" %}', {'LANGUAGE_CODE': 'de'}, u'nicht gefunden'),
  912
+            'i18n24': ("{% load i18n %}{% trans 'Page not found'|upper %}", {'LANGUAGE_CODE': 'de'}, u'SEITE NICHT GEFUNDEN'),
  913
+            'i18n25': ('{% load i18n %}{% trans somevar|upper %}', {'somevar': 'Page not found', 'LANGUAGE_CODE': 'de'}, u'SEITE NICHT GEFUNDEN'),
  914
+
910 915
             ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
911 916
 
912 917
             'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),

0 notes on commit 84f8213

Please sign in to comment.
Something went wrong with that request. Please try again.