Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#14181 closed (fixed)

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

Reported by: Russell Keith-Magee Owned by: Benjamin Wohlwend
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: no UI/UX: no

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 Benjamin Wohlwend 14 years ago.
Implementation of {% localization %} template tag
localize-template-tag-2.diff (13.1 KB ) - added by Benjamin Wohlwend 14 years ago.
Changes proposed by russellm on django-developers
t14181-rc1.diff (15.3 KB ) - added by Russell Keith-Magee 14 years ago.
Slightly cleaned up patch, ready for trunk

Download all attachments as: .zip

Change History (9)

comment:1 by Russell Keith-Magee, 14 years ago

Triage Stage: UnreviewedDesign decision needed

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

comment:2 by Jannis Leidel, 14 years ago

Triage Stage: Design decision neededAccepted

comment:3 by Benjamin Wohlwend, 14 years ago

milestone: 1.3
Owner: changed from nobody to Benjamin Wohlwend

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.

by Benjamin Wohlwend, 14 years ago

Attachment: localize-template-tag.diff added

Implementation of {% localization %} template tag

comment:4 by Benjamin Wohlwend, 14 years ago

Has patch: set
Status: newassigned

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).

by Benjamin Wohlwend, 14 years ago

Changes proposed by russellm on django-developers

by Russell Keith-Magee, 14 years ago

Attachment: t14181-rc1.diff added

Slightly cleaned up patch, ready for trunk

comment:5 by Russell Keith-Magee, 14 years ago

Resolution: fixed
Status: assignedclosed

(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 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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