-
-
Save dpetukhov/cb82a0f4d04f7373293bdf2f491863c8 to your computer and use it in GitHub Desktop.
Russian pluralize django template tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@register.filter(is_safe = False) | |
@stringfilter | |
def pluralize(value, forms): | |
""" | |
Подбирает окончание существительному после числа | |
{{someval|pluralize:"товар,товара,товаров"}} | |
""" | |
try: | |
one, two, many = forms.split(u',') | |
value = str(value)[-2:] # 314 -> 14 | |
if (21 > int(value) > 4): | |
return many | |
if value.endswith('1'): | |
return one | |
elif value.endswith(('2', '3', '4')): | |
return two | |
else: | |
return many | |
except (ValueError, TypeError): | |
return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Думаю да, можно. Много вариантов есть. Мой метод отличается повышенной читаемостью.
А вот тот, о котором вы говорите. Из библиотеки
pytils.numeral
: