From 0ad1c154c059fd96736a1842a56dba625b08493d Mon Sep 17 00:00:00 2001 From: shimizukawa Date: Sun, 22 Feb 2015 15:36:43 +0900 Subject: [PATCH] refs #1235: i18n: 'doctest-block' and 'raw' node can be translated if `gettext_additional_targets` has that string. --- CHANGES | 8 +++++-- doc/config.rst | 2 ++ sphinx/transforms.py | 2 ++ sphinx/util/nodes.py | 4 ++++ tests/roots/test-intl/contents.txt | 1 + tests/roots/test-intl/literalblock.po | 19 +++++++++++++++ tests/roots/test-intl/literalblock.txt | 13 ++++++++++ tests/roots/test-intl/raw.po | 21 ++++++++++++++++ tests/roots/test-intl/raw.txt | 8 +++++++ tests/test_intl.py | 33 ++++++++++++++++++++++++++ 10 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/roots/test-intl/raw.po create mode 100644 tests/roots/test-intl/raw.txt diff --git a/CHANGES b/CHANGES index 2facbe0be..0adec1e8f 100644 --- a/CHANGES +++ b/CHANGES @@ -23,8 +23,12 @@ Features added nav-item-0, 1, 2...). * New option `sphinx-quickstart --use-make-mode` for generating Makefile that use sphinx-build make-mode. -* #1235: i18n: 'literal-block' node can be translated if 'literal-block' - is set to `gettext_additional_targets`. +* #1235: i18n: several node can be translated if it is set to + `gettext_additional_targets` in conf.py. Supported nodes are: + + - 'literal-block' + - 'doctest-block' + - 'raw' Bugs fixed ---------- diff --git a/doc/config.rst b/doc/config.rst index ace0f96e4..f25560df8 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -495,6 +495,8 @@ documentation on :ref:`intl` for details. :index: index terms :literal-block: literal blocks: ``::`` and ``code-block``. + :doctest-block: doctest block + :raw: raw content The default is ``[]``. diff --git a/sphinx/transforms.py b/sphinx/transforms.py index 598427d4e..3e09a2717 100644 --- a/sphinx/transforms.py +++ b/sphinx/transforms.py @@ -161,6 +161,8 @@ class CitationReferences(Transform): TRANSLATABLE_NODES = { 'literal-block': nodes.literal_block, + 'doctest-block': nodes.doctest_block, + 'raw': nodes.raw, } class ExtraTranslatableNodes(Transform): """ diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index 76778fdcc..e7be08b8b 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -75,6 +75,8 @@ IGNORED_NODES = ( ) LITERAL_TYPE_NODES = ( nodes.literal_block, + nodes.doctest_block, + nodes.raw, ) def extract_messages(doctree): """Extract translatable messages from a document tree.""" @@ -92,6 +94,8 @@ def extract_messages(doctree): if isinstance(node, LITERAL_TYPE_NODES): msg = node.rawsource + if not msg: + msg = node.astext() else: msg = node.rawsource.replace('\n', ' ').strip() diff --git a/tests/roots/test-intl/contents.txt b/tests/roots/test-intl/contents.txt index b08fc3617..b968ee031 100644 --- a/tests/roots/test-intl/contents.txt +++ b/tests/roots/test-intl/contents.txt @@ -21,3 +21,4 @@ CONTENTS glossary_terms_inconsistency versionchange docfields + raw diff --git a/tests/roots/test-intl/literalblock.po b/tests/roots/test-intl/literalblock.po index 16712dc37..5721efdaf 100644 --- a/tests/roots/test-intl/literalblock.po +++ b/tests/roots/test-intl/literalblock.po @@ -65,3 +65,22 @@ msgstr "" "{\n" " return 0;\n" "}" + +msgid "doctest blocks" +msgstr "DOCTEST-BLOCKS" + +msgid "" +">>> import sys # sys importing\n" +">>> def main(): # define main function\n" +"... sys.stdout.write('hello') # call write method of stdout object\n" +">>>\n" +">>> if __name__ == '__main__': # if run this py file as python script\n" +"... main() # call main" +msgstr "" +">>> import sys # SYS IMPORTING\n" +">>> def main(): # DEFINE MAIN FUNCTION\n" +"... sys.stdout.write('hello') # CALL WRITE METHOD OF STDOUT OBJECT\n" +">>>\n" +">>> if __name__ == '__main__': # IF RUN THIS PY FILE AS PYTHON SCRIPT\n" +"... main() # CALL MAIN" + diff --git a/tests/roots/test-intl/literalblock.txt b/tests/roots/test-intl/literalblock.txt index 42f75cd68..b5a425094 100644 --- a/tests/roots/test-intl/literalblock.txt +++ b/tests/roots/test-intl/literalblock.txt @@ -39,3 +39,16 @@ code blocks return 0; } + +doctest blocks +============== + +.. highlight:: python + +>>> import sys # sys importing +>>> def main(): # define main function +... sys.stdout.write('hello') # call write method of stdout object +>>> +>>> if __name__ == '__main__': # if run this py file as python script +... main() # call main + diff --git a/tests/roots/test-intl/raw.po b/tests/roots/test-intl/raw.po new file mode 100644 index 000000000..f2e88937e --- /dev/null +++ b/tests/roots/test-intl/raw.po @@ -0,0 +1,21 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) 2015, dev +# This file is distributed under the same license as the 1235 package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: 1235 1.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-02-22 15:22+0900\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +msgid "" +msgstr "" + diff --git a/tests/roots/test-intl/raw.txt b/tests/roots/test-intl/raw.txt new file mode 100644 index 000000000..fe77f6c80 --- /dev/null +++ b/tests/roots/test-intl/raw.txt @@ -0,0 +1,8 @@ +=== +Raw +=== + +.. raw:: html + + + diff --git a/tests/test_intl.py b/tests/test_intl.py index 732047ff0..5c9e4a541 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -628,6 +628,7 @@ def test_xml_builder(app, status, warning): def test_additional_targets_should_not_be_translated(app, status, warning): app.builder.build_all() + ## literalblock.txt result = (app.outdir / 'literalblock.html').text(encoding='utf-8') # title should be translated @@ -646,17 +647,35 @@ def test_additional_targets_should_not_be_translated(app, status, warning): expected_expr = """#include <stdio.h>""" yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result) + # doctest block should not be translated but be highlighted + expected_expr = ( + """>>> """ + """import sys """ + """# sys importing""") + yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result) + + ## raw.txt + + result = (app.outdir / 'raw.html').text(encoding='utf-8') + + # raw block should not be translated + expected_expr = """""" + yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result) + @gen_with_intl_app('html', freshenv=True, confoverrides={ 'gettext_additional_targets': [ 'index', 'literal-block', + 'doctest-block', + 'raw', ], }) def test_additional_targets_should_be_translated(app, status, warning): app.builder.build_all() + ## literalblock.txt result = (app.outdir / 'literalblock.html').text(encoding='utf-8') # title should be translated @@ -675,3 +694,17 @@ def test_additional_targets_should_be_translated(app, status, warning): expected_expr = """#include <STDIO.H>""" yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result) + # doctest block should not be translated but be highlighted + expected_expr = ( + """>>> """ + """import sys """ + """# SYS IMPORTING""") + yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result) + + ## raw.txt + + result = (app.outdir / 'raw.html').text(encoding='utf-8') + + # raw block should be translated + expected_expr = """""" + yield assert_equal, len(re.findall(expected_expr, result)), 1, (expected_expr, result)