Mithun Sreedharan
Mithun Sreedharan

Reputation: 51282

Django locale setting on Linux and Windows for number formatting

I have 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 :

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

Upvotes: 0

Views: 661

Answers (1)

jd.
jd.

Reputation: 10958

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.

Upvotes: 2

Related Questions