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 Windowsde_DE
in 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.
精彩评论