Opened 7 months ago

Last modified 6 months ago

#35333 new Bug

Template tag `unlocalize` does not work with `date` and `time` filters.

Reported by: Natalia Bidart Owned by:
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Following #35306, it was noticed that using the date or time template filters would not honor the unlocalize template tag. See regression tests:

  • tests/i18n/tests.py

    diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py
    index 355505a10d..90cf4630bf 100644
    a b class FormattingTests(SimpleTestCase):  
    13511351                self.assertEqual(template2.render(context), output2)
    13521352                self.assertEqual(template3.render(context), output3)
    13531353
     1354    def test_unlocalize_honor_date_settings(self):
     1355        filter_template = Template(
     1356            "{% load l10n %}Localized: {{ my_value }}. "
     1357            "Unlocalized: {{ my_value|unlocalize }}."
     1358        )
     1359        tag_template = Template(
     1360            "{% load l10n %}Localized: {{ my_value }}. {% localize off %}"
     1361            "Unlocalized: {{ my_value }}{% endlocalize %}."
     1362        )
     1363        filter_inside_unlocalize = Template(
     1364            "{% load l10n %}Localized: {{ my_value|date }}. {% localize off %}"
     1365            "Unlocalized: {{ my_value|date:'DATE_FORMAT' }}{% endlocalize %}."
     1366        )
     1367        context = Context({"my_value": datetime.date(2024, 12, 15)})
     1368        expected_result = "Localized: 15. Dezember 2024. Unlocalized: 15-12-2024."
     1369        for case in (filter_template, tag_template, filter_inside_unlocalize):
     1370            with (
     1371                self.subTest(case=str(case)),
     1372                translation.override("de", deactivate=True),
     1373                self.settings(DATE_FORMAT="j-m-Y"),
     1374            ):
     1375                self.assertEqual(case.render(context), expected_result)
     1376
     1377    def test_unlocalize_honor_time_settings(self):
     1378        filter_template = Template(
     1379            "{% load l10n %}Localized: {{ my_value }}. "
     1380            "Unlocalized: {{ my_value|unlocalize }}."
     1381        )
     1382        tag_template = Template(
     1383            "{% load l10n %}Localized: {{ my_value }}. {% localize off %}"
     1384            "Unlocalized: {{ my_value }}{% endlocalize %}."
     1385        )
     1386        filter_inside_unlocalize = Template(
     1387            "{% load l10n %}Localized: {{ my_value|time }}. {% localize off %}"
     1388            "Unlocalized: {{ my_value|time }}{% endlocalize %}."
     1389        )
     1390        context = Context({"my_value": datetime.time(1, 2, 3)})
     1391        expected_result = "Localized: 01:02. Unlocalized: 01h 02m."
     1392        for case in (filter_template, tag_template, filter_inside_unlocalize):
     1393            with (
     1394                self.subTest(case=str(case)),
     1395                translation.override("de", deactivate=True),
     1396                self.settings(TIME_FORMAT="H\\h i\\m"),
     1397            ):
     1398                self.assertEqual(case.render(context), expected_result)
     1399
    13541400    def test_localized_off_numbers(self):
    13551401        """A string representation is returned for unlocalized numbers."""
    13561402        template = Template(

Change History (2)

comment:1 by Claude Paroz, 7 months ago

I'm not yet sure if this can be resolved in the code, or if we should rather complement the template filter docs, telling that if you apply an l10n-sensible filter inside an unlocalized section, you should specify an explicit format argument. The problem being that the way filters are currently implemented, it's not possible to transmit any context. That explains for example that floatformat gained an u parameter to force unlocalization (see #30086).

So in your example in the tests above, you should use |date:settings.DATE_FORMAT and not just DATE_FORMAT. Thoughts?

comment:2 by Claude Paroz, 6 months ago

Owner: Claude Paroz removed
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top