Django 在 Linux 和 Windows 上的数字格式化区域设置
我有一个Django的自定义模板标签
@register.filter("numformat")
@stringfilter
def numformat(value, uLocale=''):
if uLocale.count('%') > 0 :
return str((float(value)) *100) + "%"
uLocale = uLocale.encode('utf8').strip("%")
try :
locale.setlocale(locale.LC_ALL, uLocale)
except :
return str(locale.format('%f',float(value), True)) + ' Unknown loacale '+uLocale
locale.setlocale(locale.LC_ALL, "")
return str(locale.format('%f',float(value), True)) + ' in loacale '+uLocale
它在模板文件中被这样调用
{% if val_i.NumberFormat %}
{{ val_i.value|urldecode|numformat:val_i.NumberFormat }}
{% else %}
{{ val_i.value|urldecode }}
{% endif %}
val_i.NumberFormat
的值是:
deu_deu
在 Windows 系统中de_DE
在 Linux 系统中
问题是这个代码在 Windows 上能正常工作,但在 Linux 上却不行。有什么想法吗?
1 个回答
2
这样使用setlocale()可能会有问题,特别是涉及到多线程的时候(如果我没记错的话,setlocale()是对整个程序有效的,应该在创建新线程之前调用)。babel(http://babel.edgewall.org/)可以实现你想要的功能,并且可以和Django一起使用。