From f4a47f1401e2ba758adb953c279dce3787338d11 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Sun, 23 Jul 2023 22:00:24 +0200 Subject: [PATCH] Keep the ``translated`` attribute on translated nodes (#11502) This particular commit ensures that the ``translated`` attribute is retained on translated nodes so that developers may 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. --- CHANGES | 1 + sphinx/transforms/i18n.py | 5 ----- tests/test_intl.py | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 9d8264873fb..f834864a60a 100644 --- a/CHANGES +++ b/CHANGES @@ -18,6 +18,7 @@ Deprecated Features added -------------- +* #11157: Keep ``translated: True`` attribute on translated nodes. * #11415: Add a checksum to JavaScript and CSS asset URIs included within generated HTML, using the CRC32 algorithm. * :meth:`~sphinx.application.Sphinx.require_sphinx` now allows the version 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..2faf8518892 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -615,6 +615,20 @@ def test_gettext_buildr_ignores_only_directive(app): assert expect_msg.id in [m.id for m in actual if m.id] +@sphinx_intl +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')