Opened 10 days ago

Closed 7 days 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: master
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 Changed 9 days ago by Claude Paroz

Triage Stage: UnreviewedAccepted

Pull request welcome!

comment:2 Changed 8 days ago by Tzu-ping Chung

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

comment:3 Changed 8 days ago by Claude Paroz

Patch needs improvement: set

comment:4 Changed 8 days ago by Claude Paroz

Patch needs improvement: unset

comment:5 Changed 7 days ago by Tim Graham

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

comment:6 Changed 7 days ago by Tim Graham <timograham@…>

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