From bde0d9caa551fe6840cfcc92476dcf7f184daae6 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Sun, 23 Jul 2023 16:11:49 +0200 Subject: [PATCH] Keep "translated" attribute on translated nodes `sphinx.transforms.Locale` iterates over all the nodes to replace its content with the translated version of the text if found. Since the process works in two phases, it adds an "internal" `translated: True` attribute to the docutils node when the translated text is found in phase 1, allowing phase 2 to skip this nodes and avoid re-processing them. Finally, this "internal" `translated` attribute is removed from all the nodes. This particular commit deletes the code that removes the `translated` attribute from the nodes so developers can use it to improve the experience on translations. This attribute can be useful to calculate the translated percentage of a particular document, to visually mark paragraphs as not translated and many other applications. Closes #11157 --- sphinx/transforms/i18n.py | 5 ----- tests/test_intl.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sphinx/transforms/i18n.py b/sphinx/transforms/i18n.py index 719f0631d63..83fa77aa180 100644 --- a/sphinx/transforms/i18n.py +++ b/sphinx/transforms/i18n.py @@ -512,11 +512,6 @@ def apply(self, **kwargs: Any) -> None: node['raw_entries'] = entries node['entries'] = new_entries - # remove translated attribute that is used for avoiding double translation. - matcher = NodeMatcher(translated=Any) - for translated in self.document.findall(matcher): # type: nodes.Element - translated.delattr('translated') - class RemoveTranslatableInline(SphinxTransform): """ diff --git a/tests/test_intl.py b/tests/test_intl.py index bcb602c819c..a6e68cbffdf 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -615,6 +615,21 @@ def test_gettext_buildr_ignores_only_directive(app): assert expect_msg.id in [m.id for m in actual if m.id] +@sphinx_intl +@pytest.mark.test_params(shared_result='test_intl_gettext') +def test_node_translated_attribute(app): + app.build() + + expected = 23 + translated_nodes = 0 + + doctree = app.env.get_doctree('admonitions') + for node in doctree.traverse(): + if hasattr(node, 'get') and node.get('translated', False): + translated_nodes += 1 + assert translated_nodes == expected + + @sphinx_intl # use individual shared_result directory to avoid "incompatible doctree" error @pytest.mark.sphinx(testroot='builder-gettext-dont-rebuild-mo')