Opened 8 years ago
Closed 8 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 , 8 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 8 years ago
| Patch needs improvement: | set |
|---|
comment:4 by , 8 years ago
| Patch needs improvement: | unset |
|---|
comment:5 by , 8 years ago
| Summary: | Improve humanize’s |ordinal formatting → Improve translation flexibility of ordinal template tag results |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
| Type: | New feature → Cleanup/optimization |
Note:
See TracTickets
for help on using tickets.
Pull request welcome!