Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#19915 closed Bug (fixed)

templatetag "blocktrans" ignores the TEMPLATE_STRING_IF_INVALID setting

Reported by: Natalia Bidart Owned by: matiasb
Component: Template system Version: 1.4
Severity: Normal Keywords: blocktrans, template_string_if_invalid
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I define the setting TEMPLATE_STRING_IF_INVALID to something other than the empty string, for example:

TEMPLATE_STRING_IF_INVALID = 'INVALID'

when I have a template that uses the templatetag blocktrans, all those variables not defined in any context (neither local nor global context) are not shown as INVALID but they are rendered as the empty string instead.

You can reproduce this by:

  • define the setting to be:
TEMPLATE_STRING_IF_INVALID = 'INVALID'
  • create a dummy template foo.txt:
{% load i18n %}

{% blocktrans %}Hello {{ thing }}!

Django {{ action }} big time!
{% endblocktrans %}
  • render the template with an incomplete context:
>>> from django.template.loader import render_to_string
>>> print render_to_string('foo.txt', {'thing': 'world'})

Hello world!

Django  big time!
  • then, remove the blocktrans tags, and re-render, you will get instead:
>>> print render_to_string('foo.txt', {'thing': 'world'})

Hello world!

Django INVALID big time!

I would expect the same behaviour with or without the use of blocktrans.

Change History (6)

comment:1 by Natalia Bidart, 12 years ago

A simple solution would be to apply something like this (if approved I can propose a proper patch with tests):

  • django/templatetags/i18n.py

    a b class BlockTranslateNode(Node):  
    143143                result = translation.pgettext(message_context, singular)
    144144            else:
    145145                result = translation.ugettext(singular)
    146         data = dict([(v, render_value_in_context(context.get(v, ''), context)) for v in vars])
     146        from django.conf import settings
     147        data = dict([(v, render_value_in_context(context.get(v, settings.TEMPLATE_STRING_IF_INVALID), context)) for v in vars])
    147148        context.pop()
    148149        try:
    149150            result = result % data

comment:2 by Carl Meyer, 12 years ago

Component: UncategorizedTemplate system
Triage Stage: UnreviewedAccepted

comment:3 by matiasb, 12 years ago

Owner: changed from nobody to matiasb
Status: newassigned

comment:4 by matiasb, 12 years ago

Has patch: set

Proposed fix, pull request: https://github.com/django/django/pull/865
(all tests passing using SQLite).

comment:5 by Ramiro Morales <cramm0@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In 804366327d728d23a9f7a25ff77a6eed3c9f9323:

Fixed #19915 - Made blocktrans tag honor TEMPLATE_STRING_IF_INVALID.

Thanks Natalia Bidart for the report and Matías Bordese for the fix.

comment:6 by Claude Paroz <claude@…>, 11 years ago

In 02add43568b694ef25afeae474b5b1da883826c6:

Fixed #21417 -- Expanded TEMPLATE_STRING_IF_INVALID in blocktrans

Thanks keturn for the reporti, Chris Medrela for details and
Tim Graham for the review.
Refs #19915.

Note: See TracTickets for help on using tickets.
Back to Top