diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2010-10-29 16:48:58 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2010-10-29 16:48:58 +0000 |
| commit | ccc49029b8d84cf3eaaa3593df6370329f7b14e1 (patch) | |
| tree | 3e88f1a774a557cae46a7095073ccf6e05e7fff8 /django | |
| parent | 269e921756371bee6d35a967bc2ffe84d1ae39eb (diff) | |
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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14395 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/gis/templates/gis/google/google-map.js | 8 | ||||
| -rw-r--r-- | django/template/__init__.py | 2 | ||||
| -rw-r--r-- | django/template/context.py | 7 | ||||
| -rw-r--r-- | django/template/debug.py | 3 | ||||
| -rw-r--r-- | django/templatetags/l10n.py | 67 | ||||
| -rw-r--r-- | django/utils/formats.py | 49 |
6 files changed, 111 insertions, 25 deletions
diff --git a/django/contrib/gis/templates/gis/google/google-map.js b/django/contrib/gis/templates/gis/google/google-map.js index 06f11e35f3..5c1c8132b9 100644 --- a/django/contrib/gis/templates/gis/google/google-map.js +++ b/django/contrib/gis/templates/gis/google/google-map.js @@ -1,6 +1,8 @@ +{% load l10n %} {% autoescape off %} -{% block vars %}var geodjango = {};{% for icon in icons %} -var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON); +{% localize off %} +{% block vars %}var geodjango = {};{% for icon in icons %} +var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON); {% if icon.image %}{{ icon.varname }}.image = "{{ icon.image }}";{% endif %} {% if icon.shadow %}{{ icon.varname }}.shadow = "{{ icon.shadow }}";{% endif %} {% if icon.shadowsize %}{{ icon.varname }}.shadowSize = new GSize({{ icon.shadowsize.0 }}, {{ icon.shadowsize.1 }});{% endif %} {% if icon.iconanchor %}{{ icon.varname }}.iconAnchor = new GPoint({{ icon.iconanchor.0 }}, {{ icon.iconanchor.1 }});{% endif %} {% if icon.iconsize %}{{ icon.varname }}.iconSize = new GSize({{ icon.iconsize.0 }}, {{ icon.iconsize.1 }});{% endif %} @@ -32,4 +34,4 @@ var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON); alert("Sorry, the Google Maps API is not compatible with this browser."); } } -{% endblock load %}{% endblock functions %}{% endautoescape %} +{% endblock load %}{% endblock functions %}{% endlocalize %}{% endautoescape %} diff --git a/django/template/__init__.py b/django/template/__init__.py index c3167861fd..74403584be 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -825,7 +825,7 @@ def _render_value_in_context(value, context): means escaping, if required, and conversion to a unicode object. If value is a string, it is expected to have already been translated. """ - value = localize(value) + value = localize(value, use_l10n=context.use_l10n) value = force_unicode(value) if (context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData): return escape(value) diff --git a/django/template/context.py b/django/template/context.py index 323c1446b2..a5b417cdd3 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -66,8 +66,9 @@ class BaseContext(object): class Context(BaseContext): "A stack container for variable context" - def __init__(self, dict_=None, autoescape=True, current_app=None): + def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None): self.autoescape = autoescape + self.use_l10n = use_l10n self.current_app = current_app self.render_context = RenderContext() super(Context, self).__init__(dict_) @@ -139,8 +140,8 @@ class RequestContext(Context): Additional processors can be specified as a list of callables using the "processors" keyword argument. """ - def __init__(self, request, dict=None, processors=None, current_app=None): - Context.__init__(self, dict, current_app=current_app) + def __init__(self, request, dict=None, processors=None, current_app=None, use_l10n=None): + Context.__init__(self, dict, current_app=current_app, use_l10n=use_l10n) if processors is None: processors = () else: diff --git a/django/template/debug.py b/django/template/debug.py index c21fb50b3e..aa42b2b5ca 100644 --- a/django/template/debug.py +++ b/django/template/debug.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.template import Lexer, Parser, tag_re, NodeList, VariableNode, TemplateSyntaxError from django.utils.encoding import force_unicode from django.utils.html import escape @@ -87,7 +88,7 @@ class DebugVariableNode(VariableNode): def render(self, context): try: output = self.filter_expression.resolve(context) - output = localize(output) + output = localize(value, use_l10n=use_l10n) output = force_unicode(output) except TemplateSyntaxError, e: if not hasattr(e, 'source'): diff --git a/django/templatetags/l10n.py b/django/templatetags/l10n.py new file mode 100644 index 0000000000..5191e6936d --- /dev/null +++ b/django/templatetags/l10n.py @@ -0,0 +1,67 @@ +from django.conf import settings +from django.template import Node +from django.template import TemplateSyntaxError, Library +from django.utils import formats +from django.utils.encoding import force_unicode + + +register = Library() + +def localize(value): + """ + Forces a value to be rendered as a localized value, + regardless of the value of ``settings.USE_L10N``. + """ + return force_unicode(formats.localize(value, use_l10n=True)) +localize.is_safe = False + +def unlocalize(value): + """ + Forces a value to be rendered as a non-localized value, + regardless of the value of ``settings.USE_L10N``. + """ + return force_unicode(value) +unlocalize.is_safe = False + +class LocalizeNode(Node): + def __init__(self, nodelist, use_l10n): + self.nodelist = nodelist + self.use_l10n = use_l10n + + def __repr__(self): + return "<LocalizeNode>" + + def render(self, context): + old_setting = context.use_l10n + context.use_l10n = self.use_l10n + output = self.nodelist.render(context) + context.use_l10n = old_setting + return output + +@register.tag('localize') +def localize_tag(parser, token): + """ + Forces or prevents localization of values, regardless of the value of + `settings.USE_L10N`. + + Sample usage:: + + {% localize off %} + var pi = {{ 3.1415 }}; + {% endlocalize %} + + """ + use_l10n = None + bits = list(token.split_contents()) + if len(bits) == 1: + use_l10n = True + elif len(bits) > 2 or bits[1] not in ('on', 'off'): + raise TemplateSyntaxError("%r argument should be 'on' or 'off'" % bits[0]) + else: + use_l10n = bits[1] == 'on' + nodelist = parser.parse(('endlocalize',)) + parser.delete_first_token() + return LocalizeNode(nodelist, use_l10n) + +register.filter(localize) +register.filter(unlocalize) diff --git a/django/utils/formats.py b/django/utils/formats.py index e64cc4e95a..18fcbb600f 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -41,14 +41,17 @@ def get_format_modules(reverse=False): modules.reverse() return modules -def get_format(format_type, lang=None): +def get_format(format_type, lang=None, use_l10n=None): """ For a specific format type, returns the format for the current language (locale), defaults to the format in the settings. format_type is the name of the format, e.g. 'DATE_FORMAT' + + If use_l10n is provided and is not None, that will force the value to + be localized (or not), overriding the value of settings.USE_L10N. """ format_type = smart_str(format_type) - if settings.USE_L10N: + if use_l10n or (use_l10n is None and settings.USE_L10N): if lang is None: lang = get_language() cache_key = (format_type, lang) @@ -65,48 +68,60 @@ def get_format(format_type, lang=None): _format_cache[cache_key] = None return getattr(settings, format_type) -def date_format(value, format=None): +def date_format(value, format=None, use_l10n=None): """ Formats a datetime.date or datetime.datetime object using a localizable format + + If use_l10n is provided and is not None, that will force the value to + be localized (or not), overriding the value of settings.USE_L10N. """ - return dateformat.format(value, get_format(format or 'DATE_FORMAT')) + return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n)) -def time_format(value, format=None): +def time_format(value, format=None, use_l10n=None): """ Formats a datetime.time object using a localizable format + + If use_l10n is provided and is not None, that will force the value to + be localized (or not), overriding the value of settings.USE_L10N. """ - return dateformat.time_format(value, get_format(format or 'TIME_FORMAT')) + return dateformat.time_format(value, get_format(format or 'TIME_FORMAT', use_l10n=use_l10n)) -def number_format(value, decimal_pos=None): +def number_format(value, decimal_pos=None, use_l10n=None): """ Formats a numeric value using localization settings + + If use_l10n is provided and is not None, that will force the value to + be localized (or not), overriding the value of settings.USE_L10N. """ - if settings.USE_L10N: + if use_l10n or (use_l10n is None and settings.USE_L10N): lang = get_language() else: lang = None return numberformat.format( value, - get_format('DECIMAL_SEPARATOR', lang), + get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n), decimal_pos, - get_format('NUMBER_GROUPING', lang), - get_format('THOUSAND_SEPARATOR', lang), + get_format('NUMBER_GROUPING', lang, use_l10n=use_l10n), + get_format('THOUSAND_SEPARATOR', lang, use_l10n=use_l10n), ) -def localize(value): +def localize(value, use_l10n=None): """ Checks if value is a localizable type (date, number...) and returns it - formatted as a string using current locale format + formatted as a string using current locale format. + + If use_l10n is provided and is not None, that will force the value to + be localized (or not), overriding the value of settings.USE_L10N. """ if isinstance(value, (decimal.Decimal, float, int, long)): - return number_format(value) + return number_format(value, use_l10n=use_l10n) elif isinstance(value, datetime.datetime): - return date_format(value, 'DATETIME_FORMAT') + return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n) elif isinstance(value, datetime.date): - return date_format(value) + return date_format(value, use_l10n=use_l10n) elif isinstance(value, datetime.time): - return time_format(value, 'TIME_FORMAT') + return time_format(value, 'TIME_FORMAT', use_l10n=use_l10n) else: return value |
