Code

Ticket #7239: patch_django_7239.20080722.diff

File patch_django_7239.20080722.diff, 10.9 KB (added by david, 6 years ago)

New patch against r8053 with additional tests for combined arguments in url tag

Line 
1Index: django/templatetags/i18n.py
2===================================================================
3--- django/templatetags/i18n.py (revision 8053)
4+++ django/templatetags/i18n.py (working copy)
5@@ -3,6 +3,7 @@
6 from django.template import Node, Variable, VariableNode
7 from django.template import TemplateSyntaxError, TokenParser, Library
8 from django.template import TOKEN_TEXT, TOKEN_VAR
9+from django.template.defaulttags import URLNode
10 from django.utils import translation
11 from django.utils.encoding import force_unicode
12 
13@@ -203,6 +204,12 @@
14         There are {{ count }} objects.
15         {% endblocktrans %}
16 
17+    In latest development version it supports url too::
18+
19+        {% blocktrans url path.to.some_view arg1,arg2,name1=value1 as myurl %}
20+        This is a <a href="{{ myurl }}" title="">link</a>.
21+        {% endblocktrans %}
22+
23     This is much like ngettext, only in template syntax.
24     """
25     class BlockTranslateParser(TokenParser):
26@@ -218,6 +225,24 @@
27                         raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'with value as variable'"
28                     extra_context[self.tag()] = VariableNode(
29                             parser.compile_filter(value))
30+                elif tag == 'url':
31+                    args = []
32+                    kwargs = {}
33+                    value = self.value()
34+                    next_tag = self.tag()
35+                    if next_tag != 'as':
36+                        if self.tag() != 'as':
37+                            raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'url path.to.some_view arg1,arg2,name1=value1 as variable'"
38+                        else:
39+                            # can't find  way to do not duplicate code from url tag
40+                            for arg in next_tag.split(','):
41+                                if '=' in arg:
42+                                    k, v = arg.split('=', 1)
43+                                    k = k.strip()
44+                                    kwargs[k] = parser.compile_filter(v)
45+                                else:
46+                                    args.append(parser.compile_filter(arg))
47+                    extra_context[self.tag()] = URLNode(value, args, kwargs)
48                 elif tag == 'count':
49                     counter = parser.compile_filter(self.value())
50                     if self.tag() != 'as':
51Index: tests/regressiontests/templates/tests.py
52===================================================================
53--- tests/regressiontests/templates/tests.py    (revision 8053)
54+++ tests/regressiontests/templates/tests.py    (working copy)
55@@ -744,28 +744,46 @@
56             # translation of plural form
57             'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"),
58 
59+            # translation with an url (same as url tag below)
60+            'i18n09': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.client client.id as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'client': {'id': 1}}, '<a href="/url_tag/client/1/" title="">link</a>'),
61+            'i18n10': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.client_action client.id,action="update" as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'client': {'id': 1}}, '<a href="/url_tag/client/1/update/" title="">link</a>'),
62+            'i18n11': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.index as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {}, '<a href="/url_tag/" title="">link</a>'),
63+            'i18n12': ('{% load i18n %}{% blocktrans url named.client client.id as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'client': {'id': 1}}, '<a href="/url_tag/named-client/1/" title="">link</a>'),
64+            'i18n13': (u'{% load i18n %}{% blocktrans url метка_оператора v as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'v': u'Ω'},
65+                    '<a href="/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/" title="">link</a>'),
66+
67+            # translation with combined arguments (with, count and/or url)
68+            'i18n14': ('{% load i18n %}{% blocktrans with anton|lower as berta count number as counter %}{{ berta }} singular{% plural %}{{ berta }} {{ counter }} plural{% endblocktrans %}', {'anton': '\xc3\x85', 'number': 1}, u'å singular'),
69+            'i18n15': ('{% load i18n %}{% blocktrans count number as counter with anton|lower as berta %}{{ berta }} singular{% plural %}{{ berta }} {{ counter }} plural{% endblocktrans %}', {'anton': '\xc3\x85', 'number': 2}, u'å 2 plural'),
70+            'i18n16': ('{% load i18n %}{% blocktrans with anton|lower as berta url regressiontests.templates.views.client client.id as url %}{{ berta }} <a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'client': {'id': 1}, 'anton': '\xc3\x85'}, u'å <a href="/url_tag/client/1/" title="">link</a>'),
71+            'i18n16': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.client client.id as url with anton|lower as berta %}{{ berta }} <a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'client': {'id': 1}, 'anton': '\xc3\x85'}, u'å <a href="/url_tag/client/1/" title="">link</a>'),
72+            'i18n17': ('{% load i18n %}{% blocktrans count number as counter url regressiontests.templates.views.client client.id as url %}{{ url }} singular{% plural %}{{ url }} {{ counter }} plural{% endblocktrans %}', {'client': {'id': 1}, 'number': 1}, u'/url_tag/client/1/ singular'),
73+            'i18n18': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.client client.id as url count number as counter %}{{ url }} singular{% plural %}{{ url }} {{ counter }} plural{% endblocktrans %}', {'client': {'id': 1}, 'number': 2}, u'/url_tag/client/1/ 2 plural'),
74+            'i18n19': ('{% load i18n %}{% blocktrans with anton|lower as berta and foo as bar count number as counter url regressiontests.templates.views.client client.id as url %}{{ berta }} {{ bar }} {{ url }} singular{% plural %}{{ berta }} {{ bar }} {{ url }} {{ counter }} plural{% endblocktrans %}', {'client': {'id': 1}, 'number': 1, 'anton': '\xc3\x85', 'foo': 'baz'}, u'å baz /url_tag/client/1/ singular'),
75+            'i18n20': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.client client.id as url count number as counter with anton|lower as berta %}{{ berta }} {{ url }} singular{% plural %}{{ berta }} {{ url }} {{ counter }} plural{% endblocktrans %}', {'client': {'id': 1}, 'number': 2, 'anton': '\xc3\x85'}, u'å /url_tag/client/1/ 2 plural'),
76+           
77             # simple non-translation (only marking) of a string to german
78-            'i18n09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),
79+            'i18n21': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),
80 
81             # translation of a variable with a translated filter
82-            'i18n10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
83+            'i18n22': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
84 
85             # translation of a variable with a non-translated filter
86-            'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
87+            'i18n23': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
88 
89             # usage of the get_available_languages tag
90-            'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),
91+            'i18n24': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),
92 
93             # translation of constant strings
94-            'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
95-            'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
96-            'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
97-            'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
98+            'i18n25': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
99+            'i18n26': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
100+            'i18n27': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
101+            'i18n28': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
102 
103             # Escaping inside blocktrans works as if it was directly in the
104             # template.
105-            'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
106-            'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
107+            'i18n29': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
108+            'i18n30': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
109 
110             ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
111 
112
113Index: AUTHORS
114===================================================================
115--- AUTHORS     (revision 8053)
116+++ AUTHORS     (working copy)
117@@ -235,6 +235,7 @@
118     Nick Lane <nick.lane.au@gmail.com>
119     Stuart Langridge <http://www.kryogenix.org/>
120     Paul Lanier <planier@google.com>
121+    David Larlet <http://david.larlet.fr>
122     Nicola Larosa <nico@teknico.net>
123     Rune Rønde Laursen <runerl@skjoldhoej.dk>
124     Eugene Lazutkin <http://lazutkin.com/blog/>
125Index: docs/i18n.txt
126===================================================================
127--- docs/i18n.txt       (revision 8053)
128+++ docs/i18n.txt       (working copy)
129@@ -260,6 +260,13 @@
130     There are {{ counter }} {{ name }} objects.
131     {% endblocktrans %}
132 
133+**New in development version:** If you need to use `url template tag`_, you
134+can specify it within ``{% blocktrans %}``, as ``count`` above. Example::
135+
136+    {% blocktrans url path.to.some_view arg1,arg2,name1=value1 as myurl %}
137+    This is a <a href="{{ myurl }}" title="">link</a>.
138+    {% endblocktrans %}
139+
140 Internally, all block and inline translations use the appropriate
141 ``ugettext`` / ``ungettext`` call.
142 
143@@ -302,6 +309,7 @@
144     (keeping the comma intact).
145 
146 .. _Django templates: ../templates_python/
147+.. _url template tag: ../templates/#url
148 
149 Working with lazy translation objects
150 -------------------------------------