Opened 2 years ago

Closed 2 years ago

Last modified 13 months ago

#19915 closed Bug (fixed)

templatetag "blocktrans" ignores the TEMPLATE_STRING_IF_INVALID setting

Reported by: nessita 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 Changed 2 years ago by nessita

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 2 years ago by carljm

  • Component changed from Uncategorized to Template system
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 2 years ago by matiasb

  • Owner changed from nobody to matiasb
  • Status changed from new to assigned

comment:4 Changed 2 years ago by matiasb

  • Has patch set

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

comment:5 Changed 2 years ago by Ramiro Morales <cramm0@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

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 Changed 13 months ago by Claude Paroz <claude@…>

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