Linux和Windows上的Django语言环境设置,用于数字格式化

问题描述 投票:0回答:1

我有一个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的价值是:

  • Windows中的deu_deu
  • de_DEin Linux

问题是代码只能在Windows中运行,而不能在Linux中运行。任何的想法?

python django locale
1个回答
2
投票

使用setlocale()这种方式可能会有问题,特别是因为线程(IIRC,setlocale()应用程序范围,应该在产生新线程之前调用)。 babel(http://babel.edgewall.org/)完成了你想要实现的目标并与Django一起工作。

© www.soinside.com 2019 - 2024. All rights reserved.