Ticket #7239: patch_django_7239.20080617.diff

File patch_django_7239.20080617.diff, 7.5 KB (added by David Larlet, 16 years ago)

All in one with regression tests

  • django/templatetags/i18n.py

     
    33from django.template import Node, Variable, VariableNode
    44from django.template import TemplateSyntaxError, TokenParser, Library
    55from django.template import TOKEN_TEXT, TOKEN_VAR
     6from django.template.defaulttags import URLNode
    67from django.utils import translation
    78from django.utils.encoding import force_unicode
    89
     
    218219                        raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'with value as variable'"
    219220                    extra_context[self.tag()] = VariableNode(
    220221                            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)
    221240                elif tag == 'count':
    222241                    counter = parser.compile_filter(self.value())
    223242                    if self.tag() != 'as':
  • tests/regressiontests/templates/tests.py

     
    735735            # translation of plural form
    736736            'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"),
    737737
     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
    738746            # simple non-translation (only marking) of a string to german
    739             'i18n09': ('{% 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"),
    740748
    741749            # translation of a variable with a translated filter
    742             'i18n10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
     750            'i18n15': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
    743751
    744752            # translation of a variable with a non-translated filter
    745             'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
     753            'i18n16': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
    746754
    747755            # usage of the get_available_languages tag
    748             'i18n12': ('{% 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'),
    749757
    750758            # translation of constant strings
    751             'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
    752             'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
    753             'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
    754             'i18n16': ('{{ _("<") }}', {'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'}, '<'),
    755763
    756764            # Escaping inside blocktrans works as if it was directly in the
    757765            # template.
    758             'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    759             'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     766            'i18n22': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     767            'i18n23': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    760768
    761769            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
    762770
  • docs/i18n.txt

     
    260260    There are {{ counter }} {{ name }} objects.
    261261    {% endblocktrans %}
    262262
     263**New in development version:** If you need to use `url template tag`_, you
     264can 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
    263270Internally, all block and inline translations use the appropriate
    264271``ugettext`` / ``ungettext`` call.
    265272
     
    301308    (keeping the comma intact).
    302309
    303310.. _Django templates: ../templates_python/
     311.. _url template tag: ../templates/#url
    304312
    305313Working with lazy translation objects
    306314-------------------------------------
Back to Top