Python Linux和Windows上用于数字格式设置的Django区域设置

Python Linux和Windows上用于数字格式设置的Django区域设置,python,django,locale,Python,Django,Locale,我有一个Django自定义模板标记 @register.filter("numformat") @stringfilter def numformat(value, uLocale=''): if uLocale.count('%') > 0 : return str((float(value)) *100) + "%" uLocale = uLocale.encode('utf8').strip("%") try : loca

我有一个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
    inWindows

  • de_de
    inLinux

问题是代码只能在Windows中工作,而不能在Linux中工作。有什么想法吗?

以这种方式使用setlocale()可能会有问题,特别是因为线程(IIRC,setlocale()适用于程序范围,应该在生成新线程之前调用)。巴别塔(http://babel.edgewall.org/)做你想做的事情,并与Django合作