Ticket #7239: patch_django_7239.20080809.diff

File patch_django_7239.20080809.diff, 11.4 KB (added by David Larlet, 16 years ago)

New patch against r8255 which handle decision in r8211 and add tests for this situation

  • 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
     
    203204        There are {{ count }} objects.
    204205        {% endblocktrans %}
    205206
     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
    206213    This is much like ngettext, only in template syntax.
    207214    """
    208215    class BlockTranslateParser(TokenParser):
     
    218225                        raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'with value as variable'"
    219226                    extra_context[self.tag()] = VariableNode(
    220227                            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)
    221246                elif tag == 'count':
    222247                    counter = parser.compile_filter(self.value())
    223248                    if self.tag() != 'as':
  • tests/regressiontests/templates/tests.py

     
    753753            # translation of plural form
    754754            'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"),
    755755
     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           
    756780            # simple non-translation (only marking) of a string to german
    757             'i18n09': ('{% 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"),
    758782
    759783            # translation of a variable with a translated filter
    760             'i18n10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
     784            'i18n26': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
    761785
    762786            # translation of a variable with a non-translated filter
    763             'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
     787            'i18n27': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
    764788
    765789            # usage of the get_available_languages tag
    766             'i18n12': ('{% 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'),
    767791
    768792            # translation of constant strings
    769             'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
    770             'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
    771             'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
    772             'i18n16': ('{{ _("<") }}', {'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'}, '<'),
    773797
    774798            # Escaping inside blocktrans works as if it was directly in the
    775799            # template.
    776             'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    777             'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     800            'i18n33': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     801            'i18n34': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    778802
    779803            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
    780804
  • AUTHORS

     
    239239    Nick Lane <nick.lane.au@gmail.com>
    240240    Stuart Langridge <http://www.kryogenix.org/>
    241241    Paul Lanier <planier@google.com>
     242    David Larlet <http://david.larlet.fr>
    242243    Nicola Larosa <nico@teknico.net>
    243244    Lau Bech Lauritzen
    244245    Rune Rønde Laursen <runerl@skjoldhoej.dk>
  • 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
     
    302309    (keeping the comma intact).
    303310
    304311.. _Django templates: ../templates_python/
     312.. _url template tag: ../templates/#url
    305313
    306314Working with lazy translation objects
    307315-------------------------------------
Back to Top