Disable localization in templates by default

The Django Templating automatically localizes numbers. I’m looking for a way to disable this default behavior and make it opt-in, as in only localize values where I explicitly want it.

I find this default behavior very irritating because usually an unformatted value is not a (technical) problem, but a formatted one can cause issues due to no longer being a number[^] but a “string”. One instance where this is tricky is for example in a DataTables table where you want to show a formatted value in the table cell but want to use the actual number without formatting inside a data-* attribute for sorting.

I stumbled upon the setting USE_L10N that was removed in Django 5. I gathered that this setting would have disabled this default behavior and would be exactly what I wanted.

Now I’m left with the localize template tags. I’m thinking of just wrapping my base template with {% localize off %}, i.e.:

{% load l10n %}
<!doctype html>
<html>
<head>
	...
</head>
<body>
		...
		
		{% localize off %}
		
		{% block content %}{% endblock %}
		
		{% endlocalize %}
		
		...
</body>
</html>

This actually works as I would expect, and other templates included in the content block have localization off. And I can apply the |localize tag when needed.

But I’m unsure if this can cause other issues due to the “large scope” of such a localize off block?

Another consideration would be to just always be explicit and use |[un]localize on any value, and by doing so not “breaking” with an established default inside Django templates. Tough, it feels a bit cumbersome and comes back to this “magic” potentially breaking stuff if the unlocalize is forgotten.

So any ideas or comments regarding this topic would be appreciated! :wink:

1 Like

I agree. I have gone the other route at work and made numbers format according to Swedish specs (non-breaking whitespace for thousands separators, comma for decimals), and this has turned out to be a mistake I think. I have plenty of bugs because I forgot to use |raw on numbers.

Doing it localized first breaks things. Doing it non-localized first makes things slightly uglier sometimes. It seems pretty clear which of those alternatives is better :wink:

To play devils advocate: There’s a third option. Crash hard on number rendering in templates and force the user to either do |number or |localize. I am not saying this is a good option in the normal case, but it would be nice if it was possible to do this for your app imo.