Opened 7 years ago

Closed 7 years ago

#28877 closed Cleanup/optimization (fixed)

Improve translation flexibility of ordinal template tag results

Reported by: Tzu-ping Chung Owned by: Tzu-ping Chung
Component: contrib.humanize Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (6)

comment:1 by Claude Paroz, 7 years ago

Triage Stage: UnreviewedAccepted

Pull request welcome!

comment:2 by Tzu-ping Chung, 7 years ago

Has patch: set
Owner: changed from nobody to Tzu-ping Chung
Status: newassigned

comment:3 by Claude Paroz, 7 years ago

Patch needs improvement: set

comment:4 by Claude Paroz, 7 years ago

Patch needs improvement: unset

comment:5 by Tim Graham, 7 years ago

Summary: Improve humanize’s |ordinal formattingImprove translation flexibility of ordinal template tag results
Triage Stage: AcceptedReady for checkin
Type: New featureCleanup/optimization

comment:6 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 85e6a1c6:

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.

Note: See TracTickets for help on using tickets.
Back to Top