Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hindi or Farsi numbers in django templating engine

I want to print {{forloop.counter}} with persian or Hindi encoding means to have "۱ ۲ ۳ ۴ .." instead of "1 2 3 4 ...". I searched a lot but I couldn't find any related functions. Would you mind helping me?

Regards

like image 358
Amin Fazli Avatar asked Aug 23 '12 12:08

Amin Fazli


1 Answers

You could use a custom template filter. I'm not familiar enough with Django's l10n library to know if they do this for you.

def devanagari_int(arabic_int):
    """ Converts an arabic numeral (ex. 5817) to Devanagari (ex. ५८१७) """
    devanagari_nums = ('०','१','२','३','४','५','६','७','८','९')
    #    arabic_nums = ('۰','١','۲'....)
    #     farsi_nums = (...)
    number = str(arabic_int)
    return ''.join(devanagari_nums[int(digit)] for digit in number)

# register your filter, and then:
{{forloop.counter|devanagari_int}}

Make sure you save your filter file as UTF-8 (or instead use the appropriate unicode representations).

like image 62
atp Avatar answered Nov 05 '22 13:11

atp