Merge pull request #7979 from tk0miya/7784_translate_image_alt_text_by_default

Close #7784: i18n: The alt text for image is translated by default
This commit is contained in:
Takeshi KOMIYA 2020-07-19 13:18:08 +09:00 committed by GitHub
commit dc63eaf196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 27 deletions

View File

@ -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
----------
@ -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

View File

@ -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

View File

@ -237,6 +237,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.
@ -440,8 +444,9 @@ 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

View File

@ -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
@ -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):
@ -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):

View File

@ -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"

View File

@ -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"
@ -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)