refs #1235: i18n: 'uri' and 'alt' attribute on 'image' and 'figure' nodes can be translated if gettext_additional_targets has 'image'.

This commit is contained in:
shimizukawa 2015-02-22 19:24:14 +09:00
parent 28dc3ab053
commit bb85c1c87f
9 changed files with 121 additions and 40 deletions

View File

@ -29,6 +29,7 @@ Features added
- 'literal-block'
- 'doctest-block'
- 'raw'
- 'image'
Bugs fixed
----------

View File

@ -497,6 +497,9 @@ documentation on :ref:`intl` for details.
:literal-block: literal blocks: ``::`` and ``code-block``.
:doctest-block: doctest block
:raw: raw content
:image: image/figure uri and alt
For example: ``gettext_additional_targets = ['literal-block', 'image']``.
The default is ``[]``.

View File

@ -21,7 +21,7 @@ from sphinx import addnodes
from sphinx.locale import _, init as init_locale
from sphinx.util import split_index_msg
from sphinx.util.nodes import (
traverse_translatable_index, extract_messages, LITERAL_TYPE_NODES,
traverse_translatable_index, extract_messages, LITERAL_TYPE_NODES, IMAGE_TYPE_NODES,
)
from sphinx.util.osutil import ustrftime, find_catalog
from sphinx.util.pycompat import indent
@ -164,6 +164,7 @@ TRANSLATABLE_NODES = {
'doctest-block': nodes.doctest_block,
'raw': nodes.raw,
'index': addnodes.index,
'image': nodes.image,
}
class ExtraTranslatableNodes(Transform):
"""
@ -377,7 +378,8 @@ class Locale(Transform):
except IndexError: # empty node
pass
# XXX doctest and other block markup
if not isinstance(patch, (nodes.paragraph,) + LITERAL_TYPE_NODES):
if not isinstance(patch,
(nodes.paragraph,) + LITERAL_TYPE_NODES + IMAGE_TYPE_NODES):
continue # skip for now
# auto-numbered foot note reference should use original 'ids'.
@ -509,6 +511,9 @@ class Locale(Transform):
if isinstance(node, LITERAL_TYPE_NODES):
node.rawsource = node.astext()
if isinstance(node, IMAGE_TYPE_NODES):
node.update_all_atts(patch)
node['translated'] = True
if 'index' in env.config.gettext_additional_targets:

View File

@ -89,6 +89,9 @@ def is_translatable(node):
return False
return True
if isinstance(node, nodes.image) and node.get('translatable'):
return True
return False
@ -97,11 +100,18 @@ LITERAL_TYPE_NODES = (
nodes.doctest_block,
nodes.raw,
)
IMAGE_TYPE_NODES = (
nodes.image,
)
def extract_messages(doctree):
"""Extract translatable messages from a document tree."""
for node in doctree.traverse(is_translatable):
if isinstance(node, LITERAL_TYPE_NODES):
msg = node.rawsource
elif isinstance(node, IMAGE_TYPE_NODES):
msg = '.. image:: %s' % node['uri']
if node.get('alt'):
msg += '\n :alt: %s' % node['alt']
else:
msg = node.rawsource.replace('\n', ' ').strip()

View File

@ -14,7 +14,7 @@ CONTENTS
literalblock
seealso
definition_terms
figure_caption
figure
index_entries
role_xref
glossary_terms

View File

@ -1,35 +1,52 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2012, foof
# This file is distributed under the same license as the foo package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: sphinx 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-01-04 07:00+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "i18n with figure caption"
msgstr "I18N WITH FIGURE CAPTION"
msgid "My caption of the figure"
msgstr "MY CAPTION OF THE FIGURE"
msgid "My description paragraph1 of the figure."
msgstr "MY DESCRIPTION PARAGRAPH1 OF THE FIGURE."
msgid "My description paragraph2 of the figure."
msgstr "MY DESCRIPTION PARAGRAPH2 OF THE FIGURE."
msgid "figure in the block"
msgstr "FIGURE IN THE BLOCK"
msgid "block"
msgstr "BLOCK"
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2012, foof
# This file is distributed under the same license as the foo package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: sphinx 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-01-04 07:00+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "i18n with figure caption"
msgstr "I18N WITH FIGURE CAPTION"
msgid "My caption of the figure"
msgstr "MY CAPTION OF THE FIGURE"
msgid "My description paragraph1 of the figure."
msgstr "MY DESCRIPTION PARAGRAPH1 OF THE FIGURE."
msgid "My description paragraph2 of the figure."
msgstr "MY DESCRIPTION PARAGRAPH2 OF THE FIGURE."
msgid "figure in the block"
msgstr "FIGURE IN THE BLOCK"
msgid "block"
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 ""
".. image:: i18n.png\n"
" :alt: i18n"
msgstr ""
".. image:: img.png\n"
" :alt: I18N -> IMG"

View File

@ -24,3 +24,13 @@ block
My description paragraph2 of the figure.
image url and alt
-------------------
.. image:: i18n.png
:alt: i18n
.. figure:: img.png
:alt: img

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@ -222,7 +222,7 @@ def test_text_builder(app, status, warning):
# --- figure captions: regression test for #940
result = (app.outdir / 'figure_caption.txt').text(encoding='utf-8')
result = (app.outdir / 'figure.txt').text(encoding='utf-8')
expect = (u"\nI18N WITH FIGURE CAPTION"
u"\n************************\n"
u"\n [image]MY CAPTION OF THE FIGURE\n"
@ -234,7 +234,16 @@ def test_text_builder(app, status, warning):
u"\nBLOCK\n"
u"\n [image]MY CAPTION OF THE FIGURE\n"
u"\n MY DESCRIPTION PARAGRAPH1 OF THE FIGURE.\n"
u"\n MY DESCRIPTION PARAGRAPH2 OF THE FIGURE.\n")
u"\n MY DESCRIPTION PARAGRAPH2 OF THE FIGURE.\n"
u"\n"
u"\n"
u"IMAGE URL AND ALT\n"
u"=================\n"
u"\n"
u"[image: i18n][image]\n"
u"\n"
u" [image: img][image]\n"
)
yield assert_equal, result, expect
# --- rubric: regression test for pull request #190
@ -662,6 +671,18 @@ def test_additional_targets_should_not_be_translated(app, status, warning):
expected_expr = """<iframe src="http://sphinx-doc.org"></iframe></div>"""
yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)
## figure.txt
result = (app.outdir / 'figure.html').text(encoding='utf-8')
# alt and src for image block should not be translated
expected_expr = """<img alt="i18n" src="_images/i18n.png" />"""
yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)
# alt and src for figure block should not be translated
expected_expr = """<img alt="img" src="_images/img.png" />"""
yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)
@gen_with_intl_app('html', freshenv=True,
confoverrides={
@ -670,6 +691,7 @@ def test_additional_targets_should_not_be_translated(app, status, warning):
'literal-block',
'doctest-block',
'raw',
'image',
],
})
def test_additional_targets_should_be_translated(app, status, warning):
@ -708,3 +730,16 @@ def test_additional_targets_should_be_translated(app, status, warning):
# raw block should be translated
expected_expr = """<iframe src="HTTP://SPHINX-DOC.ORG"></iframe></div>"""
yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)
## figure.txt
result = (app.outdir / 'figure.html').text(encoding='utf-8')
# alt and src for image block should be translated
expected_expr = """<img alt="I18N -&gt; IMG" src="_images/img.png" />"""
yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)
# alt and src for figure block should be translated
expected_expr = """<img alt="IMG -&gt; I18N" src="_images/i18n.png" />"""
yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)