Ticket #14181: localize-template-tag-2.diff
File localize-template-tag-2.diff, 13.1 KB (added by , 14 years ago) |
---|
-
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..a8bcba5 100644
a b 1 1 {% autoescape off %} 2 {% localize off %} 2 3 {% block vars %}var geodjango = {};{% for icon in icons %} 3 4 var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON); 4 5 {% if icon.image %}{{ icon.varname }}.image = "{{ icon.image }}";{% endif %} … … var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON); 32 33 alert("Sorry, the Google Maps API is not compatible with this browser."); 33 34 } 34 35 } 35 {% endblock load %}{% endblock functions %}{% end autoescape %}36 {% endblock load %}{% endblock functions %}{% endlocalize %}{% endautoescape %} -
django/template/__init__.py
diff --git a/django/template/__init__.py b/django/template/__init__.py index c316786..ac8f117 100644
a b class NodeList(list): 789 789 # extend_nodelist(). 790 790 contains_nontext = False 791 791 792 def render(self, context ):792 def render(self, context, localization=None): 793 793 bits = [] 794 794 for node in self: 795 795 if isinstance(node, Node): 796 bits.append(self.render_node(node, context ))796 bits.append(self.render_node(node, context, localization)) 797 797 else: 798 798 bits.append(node) 799 799 return mark_safe(''.join([force_unicode(b) for b in bits])) … … class NodeList(list): 805 805 nodes.extend(node.get_nodes_by_type(nodetype)) 806 806 return nodes 807 807 808 def render_node(self, node, context): 808 def render_node(self, node, context, localization=None): 809 if isinstance(node, VariableNode): 810 return node.render(context, localization) 809 811 return node.render(context) 810 812 811 813 class TextNode(Node): … … class TextNode(Node): 819 821 def render(self, context): 820 822 return self.s 821 823 822 def _render_value_in_context(value, context ):824 def _render_value_in_context(value, context, localization=None): 823 825 """ 824 826 Converts any value to a string to become part of a rendered template. This 825 827 means escaping, if required, and conversion to a unicode object. If value 826 828 is a string, it is expected to have already been translated. 827 829 """ 828 value = localize(value )830 value = localize(value, localization) 829 831 value = force_unicode(value) 830 832 if (context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData): 831 833 return escape(value) … … class VariableNode(Node): 839 841 def __repr__(self): 840 842 return "<Variable Node: %s>" % self.filter_expression 841 843 842 def render(self, context ):844 def render(self, context, localization): 843 845 try: 844 846 output = self.filter_expression.resolve(context) 845 847 except UnicodeDecodeError: 846 848 # Unicode conversion can fail sometimes for reasons out of our 847 849 # control (e.g. exception rendering). In that case, we fail quietly. 848 850 return '' 849 return _render_value_in_context(output, context )851 return _render_value_in_context(output, context, localization) 850 852 851 853 def generic_tag_compiler(params, defaults, name, node_class, parser, token): 852 854 "Returns a template.Node subclass." -
django/template/debug.py
diff --git a/django/template/debug.py b/django/template/debug.py index c21fb50..be2f3e4 100644
a b class DebugNodeList(NodeList): 84 84 return result 85 85 86 86 class DebugVariableNode(VariableNode): 87 def render(self, context ):87 def render(self, context, localization=None): 88 88 try: 89 89 output = self.filter_expression.resolve(context) 90 output = localize( output)90 output = localize(value, context, localization) 91 91 output = force_unicode(output) 92 92 except TemplateSyntaxError, e: 93 93 if not hasattr(e, 'source'): -
django/template/defaultfilters.py
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 1500a05..44e71b7 100644
a b linebreaksbr.is_safe = True 434 434 linebreaksbr.needs_autoescape = True 435 435 linebreaksbr = stringfilter(linebreaksbr) 436 436 437 def localize(value, localization='on'): 438 """ 439 Localizes (or prevents localization) of a value, not respecting 440 ``settings.USE_L10N``. 441 """ 442 if localization not in ('on', 'off'): 443 return value 444 localization = localization == 'on' 445 return force_unicode(formats.localize(value, localization)) 446 localize.is_safe = False 447 437 448 def safe(value): 438 449 """ 439 450 Marks the value as a string that should not be auto-escaped. … … register.filter(linebreaks) 922 933 register.filter(linebreaksbr) 923 934 register.filter(linenumbers) 924 935 register.filter(ljust) 936 register.filter(localize) 925 937 register.filter(lower) 926 938 register.filter(make_list) 927 939 register.filter(phone2numeric) -
django/template/defaulttags.py
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 1b07413..d0f654d 100644
a b class WithNode(Node): 433 433 context.pop() 434 434 return output 435 435 436 class LocalizeNode(Node): 437 def __init__(self, nodelist, localization): 438 self.nodelist = nodelist 439 self.localization = localization 440 441 def __repr__(self): 442 return "<LocalizeNode>" 443 444 def render(self, context): 445 return self.nodelist.render(context, self.localization) 446 436 447 #@register.tag 437 448 def autoescape(parser, token): 438 449 """ … … def load(parser, token): 929 940 return LoadNode() 930 941 load = register.tag(load) 931 942 943 @register.tag 944 def localize(parser, token): 945 """ 946 Forces or prevents localization without respect to `settings.USE_L10N`. 947 948 Sample usage:: 949 950 {% localize off %} 951 var pi = {{ 3.1415 }}; 952 {% endlocalize %} 953 954 """ 955 localization = None 956 bits = list(token.split_contents()) 957 if len(bits) == 1: 958 localization = True 959 elif len(bits) > 2 or bits[1] not in ('on', 'off'): 960 raise TemplateSyntaxError("%r argument should be 'on' or 'off'" % bits[0]) 961 else: 962 localization = bits[1] == 'on' 963 nodelist = parser.parse(('endlocalize',)) 964 parser.delete_first_token() 965 return LocalizeNode(nodelist, localization) 966 932 967 #@register.tag 933 968 def now(parser, token): 934 969 """ -
django/utils/formats.py
diff --git a/django/utils/formats.py b/django/utils/formats.py index e64cc4e..9ace738 100644
a b def get_format_modules(reverse=False): 41 41 modules.reverse() 42 42 return modules 43 43 44 def get_format(format_type, lang=None ):44 def get_format(format_type, lang=None, localization=None): 45 45 """ 46 46 For a specific format type, returns the format for the current 47 47 language (locale), defaults to the format in the settings. 48 48 format_type is the name of the format, e.g. 'DATE_FORMAT' 49 49 """ 50 50 format_type = smart_str(format_type) 51 if settings.USE_L10N:51 if (localization is None and settings.USE_L10N) or localization: 52 52 if lang is None: 53 53 lang = get_language() 54 54 cache_key = (format_type, lang) … … def get_format(format_type, lang=None): 65 65 _format_cache[cache_key] = None 66 66 return getattr(settings, format_type) 67 67 68 def date_format(value, format=None ):68 def date_format(value, format=None, localization=None): 69 69 """ 70 70 Formats a datetime.date or datetime.datetime object using a 71 71 localizable format 72 72 """ 73 return dateformat.format(value, get_format(format or 'DATE_FORMAT' ))73 return dateformat.format(value, get_format(format or 'DATE_FORMAT', localization=localization)) 74 74 75 def time_format(value, format=None ):75 def time_format(value, format=None, localization=None): 76 76 """ 77 77 Formats a datetime.time object using a localizable format 78 78 """ 79 return dateformat.time_format(value, get_format(format or 'TIME_FORMAT' ))79 return dateformat.time_format(value, get_format(format or 'TIME_FORMAT', localization=localization)) 80 80 81 def number_format(value, decimal_pos=None ):81 def number_format(value, decimal_pos=None, localization=None): 82 82 """ 83 83 Formats a numeric value using localization settings 84 84 """ 85 if settings.USE_L10N:85 if (localization is None and settings.USE_L10N) or localization: 86 86 lang = get_language() 87 87 else: 88 88 lang = None 89 89 return numberformat.format( 90 90 value, 91 get_format('DECIMAL_SEPARATOR', lang ),91 get_format('DECIMAL_SEPARATOR', lang, localization), 92 92 decimal_pos, 93 get_format('NUMBER_GROUPING', lang ),94 get_format('THOUSAND_SEPARATOR', lang ),93 get_format('NUMBER_GROUPING', lang, localization), 94 get_format('THOUSAND_SEPARATOR', lang, localization), 95 95 ) 96 96 97 def localize(value ):97 def localize(value, localization=None): 98 98 """ 99 99 Checks if value is a localizable type (date, number...) and returns it 100 formatted as a string using current locale format 100 formatted as a string using current locale format. 101 If the value of `localization` is True or False, localization is enabled or 102 disabled without respect to `settings.USE_L10N`. 101 103 """ 104 if localization is False: # can't use truth value, None has special meaning 105 return value 102 106 if isinstance(value, (decimal.Decimal, float, int, long)): 103 return number_format(value )107 return number_format(value, localization=localization) 104 108 elif isinstance(value, datetime.datetime): 105 return date_format(value, 'DATETIME_FORMAT' )109 return date_format(value, 'DATETIME_FORMAT', localization) 106 110 elif isinstance(value, datetime.date): 107 return date_format(value )111 return date_format(value, localization=localization) 108 112 elif isinstance(value, datetime.time): 109 return time_format(value, 'TIME_FORMAT' )113 return time_format(value, 'TIME_FORMAT', localization) 110 114 else: 111 115 return value 112 116 -
docs/ref/templates/builtins.txt
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 70f0ffd..9e86ee3 100644
a b Load a custom template tag set. 639 639 640 640 See :doc:`Custom tag and filter libraries </howto/custom-template-tags>` for more information. 641 641 642 .. templatetag:: localize 643 644 localize 645 ~~~~~~~~ 646 647 .. versionadded:: 1.3 648 649 Enables or disables localization for contained block. 650 651 This tag allows a more fine grained control of localization than 652 :setting:`USE_L10N`. 653 654 To activate or deactivate localization for a template block, use:: 655 656 {% localize on %} 657 {{ value }} 658 {% endlocalize %} 659 660 {% localize off %} 661 {{ value }} 662 {% endlocalize %} 663 664 .. note:: 665 666 The value of :setting:`USE_L10N` is not respected inside of a 667 `{% localize %}` block. 668 669 See :tfilter:`localize` for the analog template filter. 670 642 671 .. templatetag:: now 643 672 644 673 now … … For example:: 1561 1590 1562 1591 If ``value`` is ``Django``, the output will be ``"Django "``. 1563 1592 1593 .. templatefilter:: localize 1594 1595 localize 1596 ~~~~~~~~ 1597 1598 .. versionadded:: 1.3 1599 1600 Localizes (or prevents localization) of a value. 1601 1602 **Argument:** "on" or "off" 1603 1604 To prevent a value to be localized when :setting:`USE_L10N` is ``True``:: 1605 1606 {{ value|localize:"off" }} 1607 1608 To force localization of a value:: 1609 1610 {{ value|localize:"on" }} 1611 1612 or a shorthand:: 1613 1614 {{ value|localize }} 1615 1616 See :ttag:`localize` for the analog template tag. 1617 1564 1618 .. templatefilter:: lower 1565 1619 1566 1620 lower -
tests/regressiontests/i18n/tests.py
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 4aa52b6..6f8c3de 100644
a b class FormattingTests(TestCase): 453 453 settings.FORMAT_MODULE_PATH = old_format_module_path 454 454 deactivate() 455 455 456 def test_localize_templatetag_and_filter(self): 457 """ 458 Tests the {% localize %} templatetag 459 """ 460 context = Context({'value': 3.14 }) 461 template1 = Template("{% localize %}{{ value }}{% endlocalize %};{% localize on %}{{ value }}{% endlocalize %}") 462 template2 = Template("{{ value }};{% localize off %}{{ value }};{% endlocalize %}{{ value }}") 463 template3 = Template('{{ value }};{{ value|localize:"off" }}') 464 template4 = Template('{{ value }};{{ value|localize }};{{ value|localize:"on" }}') 465 output1 = '3,14;3,14' 466 output2 = '3,14;3.14;3,14' 467 output3 = '3,14;3.14' 468 output4 = '3.14;3,14;3,14' 469 old_localize = settings.USE_L10N 470 try: 471 activate('de') 472 settings.USE_L10N = False 473 self.assertEqual(template1.render(context), output1) 474 self.assertEqual(template4.render(context), output4) 475 settings.USE_L10N = True 476 self.assertEqual(template1.render(context), output1) 477 self.assertEqual(template2.render(context), output2) 478 self.assertEqual(template3.render(context), output3) 479 finally: 480 deactivate() 481 settings.USE_L10N = old_localize 482 456 483 class MiscTests(TestCase): 457 484 458 485 def test_parse_spec_http_header(self):