Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#14181 closed (fixed)

Template tag needed to disable/force L10N formatting on a per-variable basis

Reported by: russellm Owned by: piquadrat
Component: Internationalization Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The L10N support added in Django 1.2 is great, but there are times when you don't want to use L10N, regardless of any system settings.

#14164 is one manifestation of this problem. Outside of sitemaps, any data being rendered to XML (or any other format for data interchange) is generally going to need data to be rendered in "english" locale format.

A simple workaround is to render the value to a string in the context, so that the template renders a string, not an integer/float. However, a more general appoach is required.

Some possible options: A template filter to disable localization:

   {{ value|unlocalized }} 

A template filter to force a specific localization:

   {{ value|locale:"en" }}

Or, template tag variants of the same:

   {% unlocalized %}
      {{ value }}
   {% endunlocalized %}

   {% localize "en" %}
      {{ value }}
   {% endlocalize %}

The "localize" filter is the easiest to implement, but probably not the easiest to use.

Attachments (3)

localize-template-tag.diff (11.3 KB) - added by piquadrat 4 years ago.
Implementation of {% localization %} template tag
localize-template-tag-2.diff (13.1 KB) - added by piquadrat 4 years ago.
Changes proposed by russellm on django-developers
t14181-rc1.diff (15.3 KB) - added by russellm 4 years ago.
Slightly cleaned up patch, ready for trunk

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

If/when this is accepted, the fix for #14164 should be updated to use this new technique.

comment:2 Changed 4 years ago by jezdez

  • Triage Stage changed from Design decision needed to Accepted

comment:3 Changed 4 years ago by piquadrat

  • milestone set to 1.3
  • Owner changed from nobody to piquadrat

How about

{% localize off %}
    {{ value }}
{% endlocalize %}

{% localize "de-ch" %}
    {{ value }}
{% endlocalize %}

using {% localize off %} instead of {% unlocalized %} would save us one additional tag in the global namespace. Granted, {% endlocalize %} to re-enable localization looks a little weird, but there's precedence in the {% autoescape %} tag.

I'll adapt my patch from #13617 to this syntax and upload it here.

Changed 4 years ago by piquadrat

Implementation of {% localization %} template tag

comment:4 Changed 4 years ago by piquadrat

  • Has patch set
  • Status changed from new to assigned

I rewrote the patch from #13617 to the proposed syntax. For {% localize on %} and {% localize "some-locale" %} to make any sense, the tag has to be able to override USE_L10N. This makes the patch a little more intrusive than I'd liked to (see django/utils/formats.py in the patch).

Changed 4 years ago by piquadrat

Changes proposed by russellm on django-developers

Changed 4 years ago by russellm

Slightly cleaned up patch, ready for trunk

comment:5 Changed 4 years ago by russellm

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

(In [14395]) Fixed #14181 -- Added a template tag and filters to allow localization to be disabled in a template. Thanks to Benjamin Wohlwend for the work on the patch.

comment:6 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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