Ticket #7239: patch_django_7239.20080617.diff
File patch_django_7239.20080617.diff, 7.5 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 … … 218 219 raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'with value as variable'" 219 220 extra_context[self.tag()] = VariableNode( 220 221 parser.compile_filter(value)) 222 elif tag == 'url': 223 args = [] 224 kwargs = {} 225 value = self.value() 226 next_tag = self.tag() 227 if next_tag != 'as': 228 if self.tag() != 'as': 229 raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'url path.to.some_view arg1,arg2,name1=value1 as variable'" 230 else: 231 # can't find way to do not duplicate code from url tag 232 for arg in next_tag.split(','): 233 if '=' in arg: 234 k, v = arg.split('=', 1) 235 k = k.strip() 236 kwargs[k] = parser.compile_filter(v) 237 else: 238 args.append(parser.compile_filter(arg)) 239 extra_context[self.tag()] = URLNode(value, args, kwargs) 221 240 elif tag == 'count': 222 241 counter = parser.compile_filter(self.value()) 223 242 if self.tag() != 'as': -
tests/regressiontests/templates/tests.py
735 735 # translation of plural form 736 736 'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"), 737 737 738 # translation with an url (same as url tag below) 739 '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>'), 740 '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>'), 741 '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>'), 742 '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>'), 743 'i18n13' : (u'{% load i18n %}{% blocktrans url метка_оператора v as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'v': u'Ω'}, 744 '<a href="/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/" title="">link</a>'), 745 738 746 # simple non-translation (only marking) of a string to german 739 'i18n 09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),747 'i18n14': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"), 740 748 741 749 # translation of a variable with a translated filter 742 'i18n1 0': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),750 'i18n15': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'), 743 751 744 752 # translation of a variable with a non-translated filter 745 'i18n1 1': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),753 'i18n16': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'), 746 754 747 755 # usage of the get_available_languages tag 748 'i18n1 2': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),756 'i18n17': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), 749 757 750 758 # translation of constant strings 751 'i18n1 3': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),752 'i18n1 4': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),753 'i18n 15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),754 'i18n 16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),759 'i18n18': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'), 760 'i18n19': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'), 761 'i18n20': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 762 'i18n21': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), 755 763 756 764 # Escaping inside blocktrans works as if it was directly in the 757 765 # template. 758 'i18n 17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'),759 'i18n 18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'),766 'i18n22': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 767 'i18n23': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 760 768 761 769 ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### 762 770 -
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 … … 301 308 (keeping the comma intact). 302 309 303 310 .. _Django templates: ../templates_python/ 311 .. _url template tag: ../templates/#url 304 312 305 313 Working with lazy translation objects 306 314 -------------------------------------