fix: pass grammatical context through naturaldelta for i18n case-aware translations - #371
Open
deepakganesh78 wants to merge 2 commits into
Conversation
…e translations naturaltime() composes naturaldelta() output with format strings like '%s ago' / '%s from now'. Languages such as German require different grammatical cases depending on the surrounding preposition (e.g. nominative 'eine Stunde' vs dative 'einer Stunde' after 'vor'). The two-step composition (naturaldelta → wrap) made it impossible for translators to provide case-correct forms because naturaldelta had no knowledge of the context in which its result would be used. Changes: - Add _npgettext() to humanize.i18n (plural + context gettext). - Add an optional 'context' parameter to naturaldelta(). When set, all internal gettext/ngettext calls are replaced with their pgettext/npgettext counterparts, keyed by the supplied context. - naturaltime() now passes 'naturaltime-past' or 'naturaltime-future' as context so translators can provide case-aware forms via msgctxt entries in .po files. The change is fully backward-compatible: without a context (or without matching msgctxt translations), output is identical to before. Fixes python-humanize#263 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #263
Problem
naturaltime()composesnaturaldelta()output with format strings like%s ago/%s from now. Languages such as German require different grammatical cases depending on the surrounding preposition (e.g. nominative eine Stunde vs dative einer Stunde after vor). The two-step composition made it impossible for translators to provide case-correct forms becausenaturaldeltahad no knowledge of the tense context in which its result would be used.Root cause
naturaldelta()uses_ngettext()/_()for all its translation strings.naturaltime()then wraps the result with_("%s ago"). There is no mechanism for translators to supply alternate grammatical forms of the time-unit strings depending on context.Fix
humanize.i18n– added_npgettext()(plural +msgctxtgettext, mirrors the existing_pgettext).naturaldelta()– added an optionalcontextparameter. When set, all internal_()/_ngettext()calls are replaced with_pgettext(context, …)/_npgettext(context, …)so translators can provide context-specific forms viamsgctxtentries in.pofiles.naturaltime()– now passes"naturaltime-past"or"naturaltime-future"as context tonaturaldelta().How translators use this
German translators can now add
msgctxtentries like:`po
msgctxt "naturaltime-past"
msgid "a second"
msgstr "einer Sekunde"
msgctxt "naturaltime-future"
msgid "a second"
msgstr "einer Sekunde"
`
Without matching
msgctxttranslations, output is identical to before (full backward compatibility).Validation
ruff checkclean on all touched filescontextparameter produces identical English output (no translation active)_npgettext/_pgettextare called with correct context stringsnaturaltimepasses correct tense context tonaturaldelta