I'm trying to set up the formatting of dates in templates based on a locale supplied by the user. As the rest of the page will remain in the original local ('en'), I only want my users' supplied data to be formatted.
for example, dates.
users in the uk should be able to use l10n
on the dates on their pages, but I don't want to set the whole site to be en_GB
.
is there a way to set the locale in a template within a block, eg. something like
{% locale|'en_GB' %}
{{ my_date|localize }}
{% endlocale %}
You don't need to do anything explicit in the template.
Inside your
settings.py
define theFORMAT_MODULE_PATH
setting. Like:under the
formats
directory create one python package per supported language(other than your default) of your project. Inside each of these you should have aformats.py
which should have any localized formatting options.In my case the default language for my project is
en
, but I also supportel
(greek). So I have this in mysettings.py
:Inside the
myproject/websiteapp/formats
directory I have ael
package with aformats.py
file, like:Inside the
formats.py
I have this:which is the greek specific representation of a date.
So when I use a datetime field inside my templates:
It prints the default
en
representation when locale is set to the default:and my custom greek one when the locale is set to
el
.More info here
Edit
Hmm, I just realized that you asked for specific template blocks or values. Maybe the localize template filter or the localize template tag are more relevant to your specific case?