Skip to content

Instantly share code, notes, and snippets.

@xfenix
Last active March 5, 2018 04:25
Show Gist options
  • Save xfenix/4761104 to your computer and use it in GitHub Desktop.
Save xfenix/4761104 to your computer and use it in GitHub Desktop.
Russian pluralize django template tag
# pluralize for russian language
# {{someval|rupluralize:"товар,товара,товаров"}}
@register.filter(is_safe = False)
@stringfilter
def rupluralize(value, arg):
bits = arg.split(u',')
try:
value = str( 0 if not value or value <= 0 else value )[-1:] # patched version
return bits[ 0 if value=='1' else (1 if value in '234' else 2) ]
except:
raise TemplateSyntaxError
return ''
@dpetukhov
Copy link

Этот код ошибочный. Например, вместо "14 домов" он выдаст "14 дома".
Вот правильная версия: https://gist.github.com/dpetukhov/cb82a0f4d04f7373293bdf2f491863c8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment