From 15518bad12ddf39730f24249bfa3d6c630fdc9ef Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 22 Aug 2016 17:48:47 +0900 Subject: [PATCH] Fix could not translate the content of meta directive (ref: #1734) --- CHANGES | 1 + sphinx/directives/patches.py | 1 + sphinx/transforms.py | 7 ++++++- sphinx/util/nodes.py | 20 ++++++++++++++++++++ tests/roots/test-intl/contents.po | 6 ++++++ tests/roots/test-intl/contents.txt | 4 ++++ tests/test_intl.py | 8 ++++++++ 7 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 64b3f9bbb..519672b51 100644 --- a/CHANGES +++ b/CHANGES @@ -130,6 +130,7 @@ Bugs fixed directives * #2485: autosummary crashes with multiple source_suffix values * #1734: Could not translate the caption of toctree directive +* Could not translate the content of meta directive (ref: #1734) Documentation ------------- diff --git a/sphinx/directives/patches.py b/sphinx/directives/patches.py index 6a88db09a..041bee360 100644 --- a/sphinx/directives/patches.py +++ b/sphinx/directives/patches.py @@ -47,6 +47,7 @@ class Meta(html.Meta): meta = node.details['nodes'][0] meta.source = env.doc2path(env.docname) meta.line = self.lineno + meta.rawcontent = meta['content'] # docutils' meta nodes aren't picklable because the class is nested meta.__class__ = addnodes.meta diff --git a/sphinx/transforms.py b/sphinx/transforms.py index 8792d8f11..9f2aeb8d1 100644 --- a/sphinx/transforms.py +++ b/sphinx/transforms.py @@ -22,7 +22,7 @@ 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, IMAGE_TYPE_NODES, - apply_source_workaround, + apply_source_workaround, is_pending_meta, ) from sphinx.util.i18n import find_catalog, format_date from sphinx.util.pycompat import indent @@ -400,6 +400,11 @@ class Locale(Transform): node.apply_translated_message(msg, msgstr) continue + # update meta nodes + if is_pending_meta(node): + node.details['nodes'][0]['content'] = msgstr + continue + # Avoid "Literal block expected; none found." warnings. # If msgstr ends with '::' then it cause warning message at # parser.parse() processing. diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index e0a6e0195..cdbf44d76 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -85,6 +85,14 @@ IGNORED_NODES = ( ) +def is_pending_meta(node): + if (isinstance(node, nodes.pending) and + isinstance(node.details.get('nodes', [None])[0], addnodes.meta)): + return True + else: + return False + + def is_translatable(node): if isinstance(node, addnodes.translatable): return True @@ -106,6 +114,11 @@ def is_translatable(node): if isinstance(node, nodes.image) and node.get('translatable'): return True + if isinstance(node, addnodes.meta): + return True + if is_pending_meta(node): + return True + return False @@ -117,6 +130,9 @@ LITERAL_TYPE_NODES = ( IMAGE_TYPE_NODES = ( nodes.image, ) +META_TYPE_NODES = ( + addnodes.meta, +) def extract_messages(doctree): @@ -134,6 +150,10 @@ def extract_messages(doctree): msg = '.. image:: %s' % node['uri'] if node.get('alt'): msg += '\n :alt: %s' % node['alt'] + elif isinstance(node, META_TYPE_NODES): + msg = node.rawcontent + elif is_pending_meta(node): + msg = node.details['nodes'][0].rawcontent else: msg = node.rawsource.replace('\n', ' ').strip() diff --git a/tests/roots/test-intl/contents.po b/tests/roots/test-intl/contents.po index e7f71eafa..76ef049f0 100644 --- a/tests/roots/test-intl/contents.po +++ b/tests/roots/test-intl/contents.po @@ -18,3 +18,9 @@ msgstr "" msgid "Table of Contents" msgstr "TABLE OF CONTENTS" + +msgid "testdata for i18n" +msgstr "TESTDATA FOR I18N" + +msgid "i18n, sphinx, markup" +msgstr "I18N, SPHINX, MARKUP" diff --git a/tests/roots/test-intl/contents.txt b/tests/roots/test-intl/contents.txt index 20542dc81..e2336856c 100644 --- a/tests/roots/test-intl/contents.txt +++ b/tests/roots/test-intl/contents.txt @@ -1,6 +1,10 @@ CONTENTS ======== +.. meta:: + :description: testdata for i18n + :keywords: i18n, sphinx, markup + .. toctree:: :maxdepth: 2 :numbered: diff --git a/tests/test_intl.py b/tests/test_intl.py index 1f7242dfa..80e3eaa64 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -360,6 +360,14 @@ def test_gettext_builder(app, status, warning): def test_html_builder(app, status, warning): app.builder.build_all() + # --- test for meta + + result = (app.outdir / 'contents.html').text(encoding='utf-8') + expected_expr = '' + yield assert_in, expected_expr, result + expected_expr = '' + yield assert_in, expected_expr, result + # --- test for #955 cant-build-html-with-footnotes-when-using # expect no error by build