diff options
Diffstat (limited to 'sphinx/transforms/i18n.py')
-rw-r--r-- | sphinx/transforms/i18n.py | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/sphinx/transforms/i18n.py b/sphinx/transforms/i18n.py index 6bea6c6ee..8f6d37d8d 100644 --- a/sphinx/transforms/i18n.py +++ b/sphinx/transforms/i18n.py @@ -10,7 +10,7 @@ from os import path from textwrap import indent -from typing import Any, Dict, List, Tuple, TypeVar +from typing import TYPE_CHECKING, Any, Dict, List, Tuple, Type, TypeVar from docutils import nodes from docutils.io import StringInput @@ -28,10 +28,7 @@ from sphinx.util.i18n import docname_to_domain from sphinx.util.nodes import (IMAGE_TYPE_NODES, LITERAL_TYPE_NODES, NodeMatcher, extract_messages, is_pending_meta, traverse_translatable_index) -if False: - # For type annotation - from typing import Type # for python3.5.1 - +if TYPE_CHECKING: from sphinx.application import Sphinx @@ -245,6 +242,10 @@ class Locale(SphinxTransform): node.details['nodes'][0]['content'] = msgstr continue + if isinstance(node, nodes.image) and node.get('alt') == msg: + node['alt'] = msgstr + continue + # Avoid "Literal block expected; none found." warnings. # If msgstr ends with '::' then it cause warning message at # parser.parse() processing. @@ -274,10 +275,10 @@ class Locale(SphinxTransform): patch = patch.next_node() # ignore unexpected markups in translation message - unexpected = ( + unexpected: Tuple[Type[Element], ...] = ( nodes.paragraph, # expected form of translation nodes.title # generated by above "Subelements phase2" - ) # type: Tuple[Type[Element], ...] + ) # following types are expected if # config.gettext_additional_targets is configured @@ -295,8 +296,8 @@ class Locale(SphinxTransform): lst.append(new) is_autofootnote_ref = NodeMatcher(nodes.footnote_reference, auto=Any) - old_foot_refs = node.traverse(is_autofootnote_ref) # type: List[nodes.footnote_reference] # NOQA - new_foot_refs = patch.traverse(is_autofootnote_ref) # type: List[nodes.footnote_reference] # NOQA + old_foot_refs: List[nodes.footnote_reference] = node.traverse(is_autofootnote_ref) + new_foot_refs: List[nodes.footnote_reference] = patch.traverse(is_autofootnote_ref) if len(old_foot_refs) != len(new_foot_refs): old_foot_ref_rawsources = [ref.rawsource for ref in old_foot_refs] new_foot_ref_rawsources = [ref.rawsource for ref in new_foot_refs] @@ -304,7 +305,7 @@ class Locale(SphinxTransform): ' original: {0}, translated: {1}') .format(old_foot_ref_rawsources, new_foot_ref_rawsources), location=node) - old_foot_namerefs = {} # type: Dict[str, List[nodes.footnote_reference]] + old_foot_namerefs: Dict[str, List[nodes.footnote_reference]] = {} for r in old_foot_refs: old_foot_namerefs.setdefault(r.get('refname'), []).append(r) for newf in new_foot_refs: @@ -338,8 +339,8 @@ class Locale(SphinxTransform): # * use translated refname for section refname. # * inline reference "`Python <...>`_" has no 'refname'. is_refnamed_ref = NodeMatcher(nodes.reference, refname=Any) - old_refs = node.traverse(is_refnamed_ref) # type: List[nodes.reference] - new_refs = patch.traverse(is_refnamed_ref) # type: List[nodes.reference] + old_refs: List[nodes.reference] = node.traverse(is_refnamed_ref) + new_refs: List[nodes.reference] = patch.traverse(is_refnamed_ref) if len(old_refs) != len(new_refs): old_ref_rawsources = [ref.rawsource for ref in old_refs] new_ref_rawsources = [ref.rawsource for ref in new_refs] @@ -367,7 +368,7 @@ class Locale(SphinxTransform): is_refnamed_footnote_ref = NodeMatcher(nodes.footnote_reference, refname=Any) old_foot_refs = node.traverse(is_refnamed_footnote_ref) new_foot_refs = patch.traverse(is_refnamed_footnote_ref) - refname_ids_map = {} # type: Dict[str, List[str]] + refname_ids_map: Dict[str, List[str]] = {} if len(old_foot_refs) != len(new_foot_refs): old_foot_ref_rawsources = [ref.rawsource for ref in old_foot_refs] new_foot_ref_rawsources = [ref.rawsource for ref in new_foot_refs] @@ -384,8 +385,8 @@ class Locale(SphinxTransform): # citation should use original 'ids'. is_citation_ref = NodeMatcher(nodes.citation_reference, refname=Any) - old_cite_refs = node.traverse(is_citation_ref) # type: List[nodes.citation_reference] # NOQA - new_cite_refs = patch.traverse(is_citation_ref) # type: List[nodes.citation_reference] # NOQA + old_cite_refs: List[nodes.citation_reference] = node.traverse(is_citation_ref) + new_cite_refs: List[nodes.citation_reference] = patch.traverse(is_citation_ref) refname_ids_map = {} if len(old_cite_refs) != len(new_cite_refs): old_cite_ref_rawsources = [ref.rawsource for ref in old_cite_refs] @@ -446,15 +447,16 @@ class Locale(SphinxTransform): if isinstance(node, LITERAL_TYPE_NODES): node.rawsource = node.astext() - if isinstance(node, IMAGE_TYPE_NODES): - node.update_all_atts(patch) + if isinstance(node, nodes.image) and node.get('alt') != msg: + node['uri'] = patch['uri'] + continue # do not mark translated node['translated'] = True # to avoid double translation if 'index' in self.config.gettext_additional_targets: # Extract and translate messages for index entries. for node, entries in traverse_translatable_index(self.document): - new_entries = [] # type: List[Tuple[str, str, str, str, str]] + new_entries: List[Tuple[str, str, str, str, str]] = [] for type, msg, tid, main, key_ in entries: msg_parts = split_index_msg(type, msg) msgstr_parts = [] |