Localized date strftime in Django view

Andrew picture Andrew · Aug 4, 2011 · Viewed 10.3k times · Source

I would like to send localized date in JSON from django view

Normal text translation via

ugettext

is OK

Following code in view has no effect:

translation.activate("ru")
print datetime.now().strtime("%B")

Output is "August", instead of "Август"

I read about python's "locale" module, but it's named as thread unsafe

How to force strftime to use django's locale?

Answer

Andrew picture Andrew · Aug 9, 2011

Finally i used date filter from django templates:

from django.template.defaultfilters import date as _date
from datetime import datetime

_date(datetime.now(), "d b, D")