Django i18n和python地区(和日期) [英] Django i18n and python locales (and dates)

查看:87
本文介绍了Django i18n和python地区(和日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩Django的i18n系统,而且它似乎主要工作。但是,模型代码中的日期似乎正在引起一个问题。



我使用datetime.strftime填充一些选择元组,然后在表单中使用。



根据我的理解,django会将语言环境设置为用户的选择,以便datetime.strftime()以相应的语言输出,但这似乎不



我在这里缺少什么?



如果我手动设置区域设置(例如locale.setlocale (locale.LC_TIME,'de_DE.UTF-8'))datetime确实正确翻译。



谢谢,



Tom

解决方案

Django不设置区域设置进行翻译,只需加载翻译目录。要获得所需的效果,您可以设置区域设置(这不是一个很好的选择,因为它在整个过程范围内)或使用专门的库。我推荐 Babel ,它具有很好的Django集成。


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

解决方案

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.

这篇关于Django i18n和python地区(和日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆