Skip to content

Let ordinal translations interpolate the number (#270)#361

Closed
RavinduPabasara wants to merge 1 commit into
python-humanize:mainfrom
RavinduPabasara:fix/ordinal-interpolation
Closed

Let ordinal translations interpolate the number (#270)#361
RavinduPabasara wants to merge 1 commit into
python-humanize:mainfrom
RavinduPabasara:fix/ordinal-interpolation

Conversation

@RavinduPabasara

Copy link
Copy Markdown
Contributor

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:

  • 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'

Full local run: 786 passed, ruff/black clean.

@RavinduPabasara

Copy link
Copy Markdown
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impossible to translate to Romanian

1 participant