Django i18n and python locales (and dates)

烈酒焚心 提交于 2019-12-05 00:53:04

问题


I've been playing with Django's i18n system and it seems to be mostly working. However, dates in model code seem to be causing a problem.

I use datetime.strftime to populate a few CHOICES tuples which are then used in forms.

From what I understood, django will set the locale to the user's choice so that datetime.strftime() will output in the appropriate language, but this doesn't seem to happen.

What am I missing here?

If I set the locale manually (eg. locale.setlocale(locale.LC_TIME,'de_DE.UTF-8') ) datetime does translate correctly.

Thanks,

Tom


回答1:


Django does not set locale for translation, just loads translation catalog. To get desired effect you have either set locale (which is not a good option since it works process-wide) or use specialized library. I recommend Babel which has nice Django integration.




回答2:


I've recently faced similar problem but managed to solve the problem by using format instead of the python strftime



来源:https://stackoverflow.com/questions/1318744/django-i18n-and-python-locales-and-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!