{{{
@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.
--
Ticket URL: <https://code.djangoproject.com/ticket/28877>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
Pull request welcome!
--
Ticket URL: <https://code.djangoproject.com/ticket/28877#comment:1>
* owner: nobody => Tzu-ping Chung
* status: new => assigned
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/9422 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/28877#comment:2>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28877#comment:3>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/28877#comment:4>
* stage: Accepted => Ready for checkin
* type: New feature => Cleanup/optimization
--
Ticket URL: <https://code.djangoproject.com/ticket/28877#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"85e6a1c634fed55c43090e37b802c721d9e7eaaa" 85e6a1c6]:
{{{
#!CommitTicketReference repository=""
revision="85e6a1c634fed55c43090e37b802c721d9e7eaaa"
Fixed #28877 -- Made ordinal template filter results more localizable.
Marked the whole pattern (e.g. "{value}th") as translatable, instead of
just this suffix, so that languages not using suffixes for ordinals can
use this tag.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28877#comment:6>