There are languages, e.g. Chinese, that do not use suffix, but prefix to indicate ordinals. The current implementation means that it is impossible to translate the suffixes to work with those languages. I understand this has its root in a much larger issue (#15156), but this can be fixed very trivially by including the whole format instead of just the suffix, something like:
@register.filter(is_safe=True)
def ordinal(value):
"""
Convert an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
3 is '3rd', etc. Works for any integer.
"""
try:
value = int(value)
except (TypeError, ValueError):
return value
formats = (_('%dth'), _('%dst'), _('%dnd'), _('%drd'), _('%dth'), _('%dth'), _('%dth'), _('%dth'), _('%dth'), _('%dth'))
if value % 100 in (11, 12, 13): # special case
return mark_safe(formats[0] % (value,))
return mark_safe(formats[value % 10] % (value,))
I can submit a pull request if this is considered reasonable.
Pull request welcome!