Skip to content

Commit

Permalink
Close sphinx-doc#7784: i18n: The alt text for image is translated by …
Browse files Browse the repository at this point in the history
…default

Make alt text for image translatable by default without settings of
gettext_additional_targets.
  • Loading branch information
tk0miya committed Jul 19, 2020
1 parent 82a149f commit 897be0e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -12,6 +12,7 @@ Incompatible changes

* #4826: py domain: The structure of python objects is changed. A boolean value
is added to indicate that the python object is canonical one
* #7784: i18n: The msgid for alt text of image is changed

Deprecated
----------
Expand All @@ -28,6 +29,8 @@ Features added

* #4826: py domain: Add ``:canonical:`` option to python directives to describe
the location where the object is defined
* #7784: i18n: The alt text for image is translated by default (without
:confval:`gettext_additional_targets` setting)
* #7830: Add debug logs for change detection of sources and templates

Bugs fixed
Expand Down
5 changes: 4 additions & 1 deletion doc/usage/configuration.rst
Expand Up @@ -801,13 +801,16 @@ documentation on :ref:`intl` for details.
:literal-block: literal blocks (``::`` annotation and ``code-block`` directive)
:doctest-block: doctest block
:raw: raw content
:image: image/figure uri and alt
:image: image/figure uri

For example: ``gettext_additional_targets = ['literal-block', 'image']``.

The default is ``[]``.

.. versionadded:: 1.3
.. versionchanged:: 4.0

The alt text for image is translated by default.

.. confval:: figure_language_filename

Expand Down
9 changes: 7 additions & 2 deletions sphinx/transforms/i18n.py
Expand Up @@ -237,6 +237,10 @@ def apply(self, **kwargs: Any) -> None:
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.
Expand Down Expand Up @@ -440,8 +444,9 @@ def get_ref_key(node: addnodes.pending_xref) -> Tuple[str, str, str]:
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

Expand Down
16 changes: 10 additions & 6 deletions sphinx/util/nodes.py
Expand Up @@ -197,6 +197,10 @@ def is_translatable(node: Node) -> bool:
if isinstance(node, addnodes.translatable):
return True

# image node marked as translatable or having alt text
if isinstance(node, nodes.image) and (node.get('translatable') or node.get('alt')):
return True

if isinstance(node, nodes.Inline) and 'translatable' not in node: # type: ignore
# inline node must not be translated if 'translatable' is not set
return False
Expand Down Expand Up @@ -224,9 +228,6 @@ def is_translatable(node: Node) -> bool:
return False
return True

if isinstance(node, nodes.image) and node.get('translatable'):
return True

if isinstance(node, addnodes.meta):
return True
if is_pending_meta(node):
Expand Down Expand Up @@ -259,10 +260,13 @@ def extract_messages(doctree: Element) -> Iterable[Tuple[Element, str]]:
msg = node.rawsource
if not msg:
msg = node.astext()
elif isinstance(node, IMAGE_TYPE_NODES):
msg = '.. image:: %s' % node['uri']
elif isinstance(node, nodes.image):
if node.get('alt'):
msg += '\n :alt: %s' % node['alt']
yield node, node['alt']
if node.get('translatable'):
msg = '.. image:: %s' % node['uri']
else:
msg = None
elif isinstance(node, META_TYPE_NODES):
msg = node.rawcontent
elif isinstance(node, nodes.pending) and is_pending_meta(node):
Expand Down
22 changes: 10 additions & 12 deletions tests/roots/test-intl/xx/LC_MESSAGES/figure.po
Expand Up @@ -37,19 +37,17 @@ msgstr "BLOCK"
msgid "image url and alt"
msgstr "IMAGE URL AND ALT"

msgid ""
".. image:: img.png\n"
" :alt: img"
msgstr ""
".. image:: i18n.png\n"
" :alt: IMG -> I18N"
msgid "img"
msgstr "IMG -> I18N"

msgid ""
".. image:: i18n.png\n"
" :alt: i18n"
msgstr ""
".. image:: img.png\n"
" :alt: I18N -> IMG"
msgid ".. image:: img.png"
msgstr ".. image:: i18n.png"

msgid "i18n"
msgstr "I18N -> IMG"

msgid ".. image:: i18n.png"
msgstr ".. image:: img.png"

msgid "image on substitution"
msgstr "IMAGE ON SUBSTITUTION"
Expand Down
12 changes: 6 additions & 6 deletions tests/test_intl.py
Expand Up @@ -345,9 +345,9 @@ def test_text_figure_captions(app):
"14.2. IMAGE URL AND ALT\n"
"=======================\n"
"\n"
"[image: i18n][image]\n"
"[image: I18N -> IMG][image]\n"
"\n"
" [image: img][image]\n"
" [image: IMG -> I18N][image]\n"
"\n"
"\n"
"14.3. IMAGE ON SUBSTITUTION\n"
Expand Down Expand Up @@ -1102,12 +1102,12 @@ def test_additional_targets_should_not_be_translated(app):

result = (app.outdir / 'figure.html').read_text()

# alt and src for image block should not be translated
expected_expr = """<img alt="i18n" src="_images/i18n.png" />"""
# src for image block should not be translated (alt is translated)
expected_expr = """<img alt="I18N -&gt; IMG" src="_images/i18n.png" />"""
assert_count(expected_expr, result, 1)

# alt and src for figure block should not be translated
expected_expr = """<img alt="img" src="_images/img.png" />"""
# src for figure block should not be translated (alt is translated)
expected_expr = """<img alt="IMG -&gt; I18N" src="_images/img.png" />"""
assert_count(expected_expr, result, 1)


Expand Down

0 comments on commit 897be0e

Please sign in to comment.