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
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
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).
You can create your own template filter.
@register.filter(name='persian_int')
def persian_int(english_int):
devanagari_nums = ('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹')
number = str(english_int)
return ''.join(devanagari_nums[int(digit)] for digit in number)
@register.filter(name='hindi_int')
def hindi_int(english_int):
devanagari_nums = ('०','१','२','३','४','५','६','७','८','९')
number = str(english_int)
return ''.join(devanagari_nums[int(digit)] for digit in number)
You could make a templatefilter that converts a number to the appropriate encoding. You could then use it as such:
{{ forloop.counter|convert_to_hindi }}
You can use the django internationalization there is a library l10n is define in django
You can create your own custom template tag.
To do so you need to create the following files assuming your app is called "myapp"
myapp/
__init__.py
models.py
templatetags/
__init__.py
extra_tags.py
views.py
Then add the following code in the file extra_tags.py
from django import template
register = template.Library()
@register.filter(name='persianize_digits')
def persian_int(string):
persianize = dict(zip("0123456789",'۰۱۲۳۴۵۶۷۸۹'))
return ''.join(persianize[digit] if digit in persianize else digit for digit in str(string))
Now you can simply do {{ value|persianize_digits }}
in your templates and the digits will become Persian. This method also works for strings that are not numbers as a whole but include some digits in them, e.g. 10:27:33
which is a time duration.
You might try the unicode version of template filter as follows:
register = template.Library()
to_arabic_number_trans = dict(
zip((ord(s) for s in u'0123456789'),
u'\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669')
)
to_english_number_trans = dict(
zip((ord(s) for s in u'٠١٢٣٤٥٦٧٨٩'),
u'\u2080\u2081\u2082\u2083\u2084\u2085\u2086\u2087\u2088\u2089')
)
@register.filter
def format_number(value):
cur_lang = get_language()
if hasattr(value, 'translate'):
if cur_lang == 'ar':
return value.translate(to_arabic_number_trans)
return value.translate(to_english_number_trans)
return value
This works well for stand alone numbers, however any ideas about numbers in date? like in 2 يونيو، 2020 how can i get it right without messing with datetime objects!
This worked for me to turn english numbers to persian numbers
from django import template
register = template.Library()
@register.filter(name='persian_int')
def persian_int(english_int):
persian_nums = {'0':'۰', '1':'۱', '2':'۲', '3':'۳', '4':'۴', '5':'۵', '6':'۶', '7':'۷', '8':'۸', '9':'۹'}
number = str(english_int)
persian_dict = number.maketrans(persian_nums)
result = number.translate(persian_dict)
return result
© 2022 - 2024 — McMap. All rights reserved.
l10n
template library already? – InvigilateU+0660..U+-669
) and Eastern Arabic-Indic Digits (U+06F0..U+06F9
). – Farinaceous