Ticket #7239: patch_django_7239.20080722.diff

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

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

  • 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

     
    744744            # translation of plural form
    745745            'i18n08': ('{% load i18n %}{% blocktrans count number as counter %}singular{% plural %}{{ counter }} plural{% endblocktrans %}', {'number': 2}, "2 plural"),
    746746
     747            # translation with an url (same as url tag below)
     748            '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>'),
     749            '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>'),
     750            '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>'),
     751            '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>'),
     752            'i18n13': (u'{% load i18n %}{% blocktrans url метка_оператора v as url %}<a href="{{ url }}" title="">link</a>{% endblocktrans %}', {'v': u'Ω'},
     753                    '<a href="/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/" title="">link</a>'),
     754
     755            # translation with combined arguments (with, count and/or url)
     756            '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'),
     757            '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'),
     758            '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>'),
     759            '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>'),
     760            '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'),
     761            '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'),
     762            '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'),
     763            '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'),
     764           
    747765            # simple non-translation (only marking) of a string to german
    748             'i18n09': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),
     766            'i18n21': ('{% load i18n %}{% trans "Page not found" noop %}', {'LANGUAGE_CODE': 'de'}, "Page not found"),
    749767
    750768            # translation of a variable with a translated filter
    751             'i18n10': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
     769            'i18n22': ('{{ bool|yesno:_("yes,no,maybe") }}', {'bool': True, 'LANGUAGE_CODE': 'de'}, 'Ja'),
    752770
    753771            # translation of a variable with a non-translated filter
    754             'i18n11': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
     772            'i18n23': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'),
    755773
    756774            # usage of the get_available_languages tag
    757             'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),
     775            'i18n24': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'),
    758776
    759777            # translation of constant strings
    760             'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
    761             'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
    762             'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
    763             'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
     778            'i18n25': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'),
     779            'i18n26': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
     780            'i18n27': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
     781            'i18n28': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
    764782
    765783            # Escaping inside blocktrans works as if it was directly in the
    766784            # template.
    767             'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    768             'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     785            'i18n29': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     786            'i18n30': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    769787
    770788            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
    771789
  • AUTHORS

     
    235235    Nick Lane <nick.lane.au@gmail.com>
    236236    Stuart Langridge <http://www.kryogenix.org/>
    237237    Paul Lanier <planier@google.com>
     238    David Larlet <http://david.larlet.fr>
    238239    Nicola Larosa <nico@teknico.net>
    239240    Rune Rønde Laursen <runerl@skjoldhoej.dk>
    240241    Eugene Lazutkin <http://lazutkin.com/blog/>
  • 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