I want to do localization in Django based on the country code / locale set in users UserProfile instead of using whatever the browser/session/webserver thinks is the locale.
Is there a way to do this in django templates?
e.g how to change the behaviour of
{% load l10n %}
{{ value|time|localize }}
so that it formats the date based on what’s stored in the profile, not based on some request context?
Django provides LocaleMiddleware that does most of what you are after:
The method it uses to guess the language is the same that is used by the translation machinery; that is it looks for a
django_languagevariable in the session, if that exists and is a valid language, it will set that language for the request. See how django discovers language preference.Once you enable
LocaleMiddlewareand setdjango_languagefrom the user’s profile at your login view everything should work.