ordinal() builds its result as f"{value}{suffix}", so a translation can only ever supply a trailing suffix. As reported in #270, that makes languages whose ordinals place the number inside the word impossible to translate — e.g. Romanian al 2-lea (m) / a 2-a (f), where the number sits between a prefix and a suffix.
Changes proposed in this pull request:
In ordinal(), if the (context-aware) translated string contains a %s/%d placeholder, format the number into it (suffix % value) so the translation controls where the number goes; otherwise keep the existing "<number><suffix>" behaviour.
Add regression tests for both the interpolated path (Romanian-style al 2-lea / a 2-a, plus a %d variant) and the unchanged suffix path.
Backward compatibility:
All 35 existing locales store bare suffixes ("nd" -> "e", "th" -> "º", …) with no placeholder, so they take the unchanged branch and their output is identical. The English default is unaffected (ordinal(2) == "2nd"). No msgid changes, so no existing translation needs re-merging.
Verified with a throwaway Romanian locale loaded through the real gettext path (activate("ro", path=...)):
>>> humanize.ordinal(2) # al %s-lea
'al 2-lea'
>>> humanize.ordinal(23) # al %s-lea
'al 23-lea'
>>> humanize.ordinal(2, gender="female") # a %s-a
'a 2-a'
>>> humanize.i18n.deactivate()
>>> humanize.ordinal(2)
'2nd'
Closing this — I hadn't seen #331, which already addresses #270 (and ships the Romanian locale, including the special primul/prima forms). No need for a competing PR. Thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #270
ordinal() builds its result as f"{value}{suffix}", so a translation can only ever supply a trailing suffix. As reported in #270, that makes languages whose ordinals place the number inside the word impossible to translate — e.g. Romanian al 2-lea (m) / a 2-a (f), where the number sits between a prefix and a suffix.
Changes proposed in this pull request:
Backward compatibility:
Verified with a throwaway Romanian locale loaded through the real gettext path (activate("ro", path=...)):
Full local run: 786 passed, ruff/black clean.