json
builder incorrectly caches translation when building multiple languages
#13448
Labels
json
builder incorrectly caches translation when building multiple languages
#13448
Describe the bug
I have a project that uses Sphinx as a library (using
sphinx.application.Sphinx
) and that builds documentation in several different languages.We noticed that some translated strings would incorrectly persist between multiple builds. That is to say, building language A then language B would result in documentation B having some strings from language A.
Adding a combination of
patch_docutils
,docutils_namespace
, and_clean_up_global_state
helped, but not completely. In particular the string "Python Module Index" would keep whichever translation it had in the first built language.I believe the cause of this issue is two-fold:
sphinxcontrib.serializinghtml
was not updated correctly after the refactor in 363cdc0 (lazily translated strings are no longer a subclass ofUserString
)sphinx.domains.python.PythonModuleIndex.localname
) and is therefore cached the first time that module is imported. If a language is active at the time of import, the translation will stay cached even if a different language is activated.How to Reproduce
Our translation infrastructure is fairly complex, but I managed to narrow down the bug to this testcase:
Click to unfold (80 lines)
This script should be run somewhere where
sphinx
is installed, or if you haveuv
you can use this oneliner (assuming you saved the code asscript.py
):(Note that this script will generate two directories
docs
andbuild
in the current directory)You can see that the script builds the same document first in French, then in English. Then it loads a json file from the English build and checks whether the "Python Module Index" string is correctly translated. I've included an
apply_fix()
function that fixes the issue in two steps.Environment Information
Sphinx extensions
Additional context
No response
The text was updated successfully, but these errors were encountered: