Fall back to English for messages with damaged format placeholders - #14666
Open
shashb27 wants to merge 2 commits into
Open
Fall back to English for messages with damaged format placeholders#14666shashb27 wants to merge 2 commits into
shashb27 wants to merge 2 commits into
Conversation
Some translated catalogues contain msgstrs with damaged str.format placeholders. Six call sites formatted translated text eagerly, so a single damaged placeholder in any locale crashed the build with a KeyError. Add sphinx.locale.safe_format, which formats the translation and falls back to the untranslated message when the placeholder sets do not match or formatting raises, and route the eager call sites through it. Refs sphinx-doc#14664
Author
|
Heads-up on the red test matrix: the failing set here ( For what it's worth, the suites covering the code this branch touches pass in 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.
Refs #14664.
Problem
As detailed in #14664, some translated catalogues (e.g.
el,fa) contain msgstrs with damagedstr.formatplaceholders:Six call sites format translated text eagerly, so a single damaged placeholder in any locale kills the build with a
KeyErrorthat surfaces as anExtensionErrorblaming an extension (sphinx/config.pyx3,sphinx/_cli/__init__.pyx2,sphinx/transforms/i18n.py).Approach
Add
sphinx.locale.safe_format: pass the untranslated message and the format arguments; it formats the translation and falls back to the original English message when the translation is damaged.Damage detection compares the set of
str.formatfield names between the msgid and its translation, plus a try/except around the format call:{current.__name__}->{current__name__}) and dropped placeholders are both caught. A plain try/except alone cannot catch dropped placeholders, becausestr.formatsilently ignores surplus arguments and would emit an incomplete message.{1} ... {0}) working..format(the untranslated message formats itself).Affected messages degrade to an English warning instead of a dead build. The catalogue data itself (the Transifex half of #14664) is not touched here — that needs someone with Transifex access, per the discussion in the issue.
Tests
tests/test_locale.py: unit tests for the helper (healthy translation, renamed keyword, dropped positional, mixed corruption, reordered placeholders, other namespaces).tests/test_config/test_config.py: regression test driving a corrupted catalogue throughcheck_confval_types, asserting the English fallback message.tests/test_intl/test_intl.pysuite passes (61 tests) — thecompare_referenceswarnings now format through the helper.