localization - django format date on user supplied locale -


i'm trying set formatting of dates in templates based on locale supplied user. rest of page remain in original local ('en'), want users' supplied data formatted.

for example, dates.

users in uk should able use l10n on dates on pages, don't want set whole site en_gb.

is there way set locale in template within block, eg. like

{% locale|'en_gb' %}     {{ my_date|localize }} {% endlocale %} 

you don't need explicit in template.

inside settings.py define format_module_path setting. like:

format_module_path = 'myproject.myapp.formats' 

under formats directory create 1 python package per supported language(other default) of project. inside each of these should have formats.py should have localized formatting options.

in case default language project en, support el(greek). have in settings.py:

format_module_path = 'myproject.websiteapp.formats' 

inside myproject/websiteapp/formats directory have el package formats.py file, like:

el/  __init__.py  formats.py 

inside formats.py have this:

datetime_format="l j m y, g:i a" 

which greek specific representation of date.

so when use datetime field inside templates:

{{ mymodel.pub_date }} 

it prints default en representation when locale set default:

published on: feb. 22, 2013, 1:47 p.m. 

and custom greek 1 when locale set el.

Δημοσιεύτηκε: Τετάρτη 6 Φεβ 2013, 5:39 μμ. 

more info here

edit

hmm, realized asked specific template blocks or values. maybe localize template filter or localize template tag more relevant specific case?


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -