Fixed #28544 -- Made unlocalize template filter behave like {% localize off %} tag

Thanks Beda Kosata for the report and Tim Graham for the review.
This commit is contained in:
Claude Paroz 2017-10-22 11:32:06 +02:00
parent 327f0f37ce
commit 1b7780ea08
2 changed files with 22 additions and 12 deletions

View File

@ -19,7 +19,7 @@ def unlocalize(value):
Force a value to be rendered as a non-localized value, Force a value to be rendered as a non-localized value,
regardless of the value of ``settings.USE_L10N``. regardless of the value of ``settings.USE_L10N``.
""" """
return str(value) return str(formats.localize(value, use_l10n=False))
class LocalizeNode(Node): class LocalizeNode(Node):

View File

@ -1017,20 +1017,30 @@ class FormattingTests(SimpleTestCase):
def test_localize_templatetag_and_filter(self): def test_localize_templatetag_and_filter(self):
""" """
Tests the {% localize %} templatetag Test the {% localize %} templatetag and the localize/unlocalize filters.
""" """
context = Context({'value': 3.14}) context = Context({'float': 3.14, 'date': datetime.date(2016, 12, 31)})
template1 = Template( template1 = Template(
'{% load l10n %}{% localize %}{{ value }}{% endlocalize %};' '{% load l10n %}{% localize %}{{ float }}/{{ date }}{% endlocalize %}; '
'{% localize on %}{{ value }}{% endlocalize %}' '{% localize on %}{{ float }}/{{ date }}{% endlocalize %}'
) )
template2 = Template("{% load l10n %}{{ value }};{% localize off %}{{ value }};{% endlocalize %}{{ value }}") template2 = Template(
template3 = Template('{% load l10n %}{{ value }};{{ value|unlocalize }}') '{% load l10n %}{{ float }}/{{ date }}; '
template4 = Template('{% load l10n %}{{ value }};{{ value|localize }}') '{% localize off %}{{ float }}/{{ date }};{% endlocalize %} '
output1 = '3,14;3,14' '{{ float }}/{{ date }}'
output2 = '3,14;3.14;3,14' )
output3 = '3,14;3.14' template3 = Template(
output4 = '3.14;3,14' '{% load l10n %}{{ float }}/{{ date }}; {{ float|unlocalize }}/{{ date|unlocalize }}'
)
template4 = Template(
'{% load l10n %}{{ float }}/{{ date }}; {{ float|localize }}/{{ date|localize }}'
)
expected_localized = '3,14/31. Dezember 2016'
expected_unlocalized = '3.14/Dez. 31, 2016'
output1 = '; '.join([expected_localized, expected_localized])
output2 = '; '.join([expected_localized, expected_unlocalized, expected_localized])
output3 = '; '.join([expected_localized, expected_unlocalized])
output4 = '; '.join([expected_unlocalized, expected_localized])
with translation.override('de', deactivate=True): with translation.override('de', deactivate=True):
with self.settings(USE_L10N=False): with self.settings(USE_L10N=False):
self.assertEqual(template1.render(context), output1) self.assertEqual(template1.render(context), output1)