开发者

Django locale setting on Linux and Windows for number formatting

I have开发者_C百科 a Django custom template tag

@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

And it is called in the template file like

{% if val_i.NumberFormat %}
    {{ val_i.value|urldecode|numformat:val_i.NumberFormat }}
{% else %}
    {{ val_i.value|urldecode }}
{% endif %}

value of val_i.NumberFormat is :

  • deu_deu in Windows

  • de_DEin Linux

Issue is that code works only in Windows and not in Linux. Any idea?


Using setlocale() this way might prove problematic, particularly because of threads (IIRC, setlocale() applies program-wide and should be called before spawning new threads). babel (http://babel.edgewall.org/) does what you are trying to achieve and works with Django.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜