Ticket #13617: noloc-template-tag.diff

File noloc-template-tag.diff, 7.1 KB (added by Benjamin Wohlwend, 14 years ago)

Implementation of {% noloc %} template tag

  • django/contrib/gis/templates/gis/google/google-map.js

    diff --git a/django/contrib/gis/templates/gis/google/google-map.js b/django/contrib/gis/templates/gis/google/google-map.js
    index 06f11e3..9eac3a4 100644
    a b  
    11{% autoescape off %}
     2{% noloc %}
    23{% block vars %}var geodjango = {};{% for icon in icons %}
    34var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON);
    45{% if icon.image %}{{ icon.varname }}.image = "{{ icon.image }}";{% endif %}
    var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON);  
    3233    alert("Sorry, the Google Maps API is not compatible with this browser.");
    3334  }
    3435}
    35 {% endblock load %}{% endblock functions %}{% endautoescape %}
     36{% endblock load %}{% endblock functions %}{% endnoloc %}{% endautoescape %}
  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index c316786..d51c773 100644
    a b def _render_value_in_context(value, context):  
    825825    means escaping, if required, and conversion to a unicode object. If value
    826826    is a string, it is expected to have already been translated.
    827827    """
    828     value = localize(value)
     828    if '_no_l10n' not in context:
     829        value = localize(value)
    829830    value = force_unicode(value)
    830831    if (context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData):
    831832        return escape(value)
  • django/template/debug.py

    diff --git a/django/template/debug.py b/django/template/debug.py
    index c21fb50..2f5715d 100644
    a b class DebugVariableNode(VariableNode):  
    8787    def render(self, context):
    8888        try:
    8989            output = self.filter_expression.resolve(context)
    90             output = localize(output)
     90            if '_no_l10n' not in context:
     91                output = localize(output)
    9192            output = force_unicode(output)
    9293        except TemplateSyntaxError, e:
    9394            if not hasattr(e, 'source'):
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 1b07413..4be54f5 100644
    a b class WithNode(Node):  
    433433        context.pop()
    434434        return output
    435435
     436class NoLocalizationNode(Node):
     437    def __init__(self, nodelist):
     438        self.nodelist = nodelist
     439
     440    def __repr__(self):
     441        return "<NoLocalizationNode>"
     442
     443    def render(self, context):
     444        context['_no_l10n'] = True
     445        output = self.nodelist.render(context)
     446        del context['_no_l10n']
     447        return output
     448
    436449#@register.tag
    437450def autoescape(parser, token):
    438451    """
    def do_with(parser, token):  
    12161229    parser.delete_first_token()
    12171230    return WithNode(var, name, nodelist)
    12181231do_with = register.tag('with', do_with)
     1232
     1233@register.tag('noloc')
     1234def do_noloc(parser, token):
     1235    """
     1236    Temporarely deactivates localization inside of this block, e.g.
     1237    to avoid rendering syntactically incorrect JavaScript code.
     1238    """
     1239    bits = list(token.split_contents())
     1240    if len(bits) != 1:
     1241        raise TemplateSyntaxError("%r doesn't take any arguments" % bits[0])
     1242    nodelist = parser.parse(('endnoloc',))
     1243    parser.delete_first_token()
     1244    return NoLocalizationNode(nodelist)
  • docs/ref/templates/builtins.txt

    diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
    index 70f0ffd..077caec 100644
    a b Load a custom template tag set.  
    639639
    640640See :doc:`Custom tag and filter libraries </howto/custom-template-tags>` for more information.
    641641
     642.. templatetag:: noloc
     643
     644noloc
     645~~~~~
     646
     647.. versionadded:: 1.3
     648
     649Deactivates localization of variables in the contained block.
     650
     651While localization of variables is generally useful, sometimes it has undesirable
     652effects, e.g. when you render JavaScript code through the template system. Consider this
     653template code::
     654
     655    var pi = {{ 3.1415 }};
     656
     657If localization is enabled, the output from this template is not syntetically correct
     658JavaScript in some locales (e.g. German)::
     659
     660    var pi = 3,1415;
     661
     662To avoid this issue, wrap the affected template code in a ``{% noloc %}`` tag::
     663
     664    {% noloc %}
     665        var pi = {{ 3.1415 }};
     666    {% endnoloc %}
     667
    642668.. templatetag:: now
    643669
    644670now
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 4aa52b6..f5bcdee 100644
    a b class FormattingTests(TestCase):  
    453453            settings.FORMAT_MODULE_PATH = old_format_module_path
    454454            deactivate()
    455455
     456    def test_noloc_templatetag(self):
     457        """
     458        Tests the {% noloc %} templatetag
     459        """
     460        activate('de-de')
     461        context = Context({'the_var': 3.14 })
     462        template = Template("{{ the_var }};{% noloc %}{{ the_var }};{% endnoloc %}{{ the_var }}")
     463        old_debug = settings.TEMPLATE_DEBUG
     464        try:
     465            for debug in (True, False):
     466                settings.TEMPLATE_DEBUG = debug
     467                settings.USE_L10N = True
     468                self.assertEqual(template.render(context), '3,14;3.14;3,14')
     469        finally:
     470            deactivate()
     471            settings.TEMPLATE_DEBUG = old_debug
     472
    456473class MiscTests(TestCase):
    457474
    458475    def test_parse_spec_http_header(self):
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 6683ebb..0e060c3 100644
    a b class Templates(unittest.TestCase):  
    375375                activate(vals[1]['LANGUAGE_CODE'])
    376376            else:
    377377                activate('en-us')
    378 
     378            old_l10n = settings.USE_L10N
     379            settings.USE_L10N = vals[1].get('USE_L10N', old_l10n)
    379380            for invalid_str, result in [('', normal_string_result),
    380381                                        (expected_invalid_str, invalid_string_result)]:
    381382                settings.TEMPLATE_STRING_IF_INVALID = invalid_str
    class Templates(unittest.TestCase):  
    408409            if 'LANGUAGE_CODE' in vals[1]:
    409410                deactivate()
    410411
     412            settings.USE_L10N = old_l10n
     413
    411414            if template.invalid_var_format_string:
    412415                expected_invalid_str = 'INVALID'
    413416                template.invalid_var_format_string = False
    class Templates(unittest.TestCase):  
    11221125            # translation of singular form in russian (#14126)
    11231126            'i18n27': ('{% load i18n %}{% blocktrans count number as counter %}1 result{% plural %}{{ counter }} results{% endblocktrans %}', {'number': 1, 'LANGUAGE_CODE': 'ru'}, u'1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442'),
    11241127
     1128            ### L10N ###
     1129 
     1130            # test the {% noloc %} tag
     1131            'l10n01': ('{{ the_var }};{% noloc %}{{ the_var }};{% endnoloc %}{{ the_var }}', {'the_var': 3.14, 'LANGUAGE_CODE': 'de-de', 'USE_L10N': True}, u'3,14;3.14;3,14'),
    11251132            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
    11261133
    11271134            'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),
Back to Top