Ticket #7239: patch_django_7239.20080809.diff
File patch_django_7239.20080809.diff, 11.4 KB (added by , 16 years ago) |
---|
-
django/templatetags/i18n.py
3 3 from django.template import Node, Variable, VariableNode 4 4 from django.template import TemplateSyntaxError, TokenParser, Library 5 5 from django.template import TOKEN_TEXT, TOKEN_VAR 6 from django.template.defaulttags import URLNode 6 7 from django.utils import translation 7 8 from django.utils.encoding import force_unicode 8 9 … … 203 204 There are {{ count }} objects. 204 205 {% endblocktrans %} 205 206 207 In latest development version it supports url too:: 208 209 {% blocktrans url path.to.some_view arg1,arg2,name1=value1 as myurl %} 210 This is a <a href="{{ myurl }}" title="">link</a>. 211 {% endblocktrans %} 212 206 213 This is much like ngettext, only in template syntax. 207 214 """ 208 215 class BlockTranslateParser(TokenParser): … … 218 225 raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'with value as variable'" 219 226 extra_context[self.tag()] = VariableNode( 220 227 parser.compile_filter(value)) 228 elif tag == 'url': 229 args = [] 230 kwargs = {} 231 value = self.value() 232 next_tag = self.tag() 233 if next_tag != 'as': 234 if self.tag() != 'as': 235 raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'url path.to.some_view arg1,arg2,name1=value1 as variable'" 236 else: 237 # can't find way to do not duplicate code from url tag 238 for arg in next_tag.split(','): 239 if '=' in arg: 240 k, v = arg.split('=', 1) 241 k = k.strip() 242 kwargs[k] = parser.compile_filter(v) 243 else: 244 args.append(parser.compile_filter(arg)) 245 extra_context[self.tag()] = URLNode(value, args, kwargs) 221 246 elif tag == 'count': 222 247 counter = parser.compile_filter(self.value()) 223 248 if self.tag() != 'as': -
tests/regressiontests/templates/tests.py
753 753 # translation of plural form 754 754 'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"), 755 755 756 # translation with an url, success 757 '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>'), 758 '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>'), 759 '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>'), 760 '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>'), 761 'i18n13': (u'{% load i18n %}{% blocktrans url метка_оператора v as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'v': u'Ω'}, 762 '<a href="/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/" title="">link</a>'), 763 764 # translation with an url, failure 765 'i18n13': ('{% load i18n %}{% blocktrans url %}{% endblocktrans %}', {}, template.TemplateSyntaxError), 766 'i18n14': ('{% load i18n %}{% blocktrans url no_such_view %}{% endblocktrans %}', {}, template.TemplateSyntaxError), 767 'i18n15': ('{% load i18n %}{% blocktrans url no_such_view as no_such_view_url %}{% endblocktrans %}', {}, urlresolvers.NoReverseMatch), 768 'i18n16': ('{% load i18n %}{% blocktrans url regressiontests.templates.views.client as client_url %}{% endblocktrans %}', {}, urlresolvers.NoReverseMatch), 769 770 # translation with combined arguments (with, count and/or url) 771 'i18n17': ('{% 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'), 772 'i18n18': ('{% 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'), 773 'i18n19': ('{% 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>'), 774 'i18n20': ('{% 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>'), 775 'i18n21': ('{% 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'), 776 'i18n22': ('{% 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'), 777 'i18n23': ('{% 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'), 778 'i18n24': ('{% 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'), 779 756 780 # simple non-translation (only marking) of a string to german 757 'i18n 09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),781 'i18n25': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"), 758 782 759 783 # translation of a variable with a translated filter 760 'i18n 10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),784 'i18n26': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'), 761 785 762 786 # translation of a variable with a non-translated filter 763 'i18n 11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),787 'i18n27': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'), 764 788 765 789 # usage of the get_available_languages tag 766 'i18n 12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),790 'i18n28': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), 767 791 768 792 # translation of constant strings 769 'i18n 13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),770 'i18n 14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),771 'i18n 15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),772 'i18n 16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),793 'i18n29': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'), 794 'i18n30': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'), 795 'i18n31': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 796 'i18n32': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), 773 797 774 798 # Escaping inside blocktrans works as if it was directly in the 775 799 # template. 776 'i18n 17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'),777 'i18n 18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'),800 'i18n33': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 801 'i18n34': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 778 802 779 803 ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### 780 804 -
AUTHORS
239 239 Nick Lane <nick.lane.au@gmail.com> 240 240 Stuart Langridge <http://www.kryogenix.org/> 241 241 Paul Lanier <planier@google.com> 242 David Larlet <http://david.larlet.fr> 242 243 Nicola Larosa <nico@teknico.net> 243 244 Lau Bech Lauritzen 244 245 Rune Rønde Laursen <runerl@skjoldhoej.dk> -
docs/i18n.txt
260 260 There are {{ counter }} {{ name }} objects. 261 261 {% endblocktrans %} 262 262 263 **New in development version:** If you need to use `url template tag`_, you 264 can specify it within ``{% blocktrans %}``, as ``count`` above. Example:: 265 266 {% blocktrans url path.to.some_view arg1,arg2,name1=value1 as myurl %} 267 This is a <a href="{{ myurl }}" title="">link</a>. 268 {% endblocktrans %} 269 263 270 Internally, all block and inline translations use the appropriate 264 271 ``ugettext`` / ``ungettext`` call. 265 272 … … 302 309 (keeping the comma intact). 303 310 304 311 .. _Django templates: ../templates_python/ 312 .. _url template tag: ../templates/#url 305 313 306 314 Working with lazy translation objects 307 315 -------------------------------------