Skip to content

Commit

Permalink
Keep "translated" attribute on translated nodes
Browse files Browse the repository at this point in the history
`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 sphinx-doc#11157
  • Loading branch information
humitos committed Jul 23, 2023
1 parent 71db08c commit bde0d9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 0 additions & 5 deletions sphinx/transforms/i18n.py
Expand Up @@ -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):
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_intl.py
Expand Up @@ -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')
Expand Down

0 comments on commit bde0d9c

Please sign in to comment.