diff options
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | doc/config.rst | 10 | ||||
-rw-r--r-- | sphinx/builders/gettext.py | 19 | ||||
-rw-r--r-- | sphinx/config.py | 1 | ||||
-rw-r--r-- | sphinx/transforms.py | 33 | ||||
-rw-r--r-- | tests/roots/test-intl/conf.py | 1 | ||||
-rw-r--r-- | tests/test_build_gettext.py | 32 | ||||
-rw-r--r-- | tests/test_build_latex.py | 5 |
8 files changed, 78 insertions, 27 deletions
@@ -20,6 +20,8 @@ Incompatible changes templates directory. * Custom domains should implement the new `Domain.resolve_any_xref` method to make the `any` role work properly. +* gettext builder: disable extracting/apply 'index' node by default. Please set + 'index' to :confval:`gettext_enables` to enable extracting index entries. Features added -------------- @@ -87,6 +89,8 @@ Features added target. Thanks to Takeshi Komiya. * PR#298: Add new API: :meth:`~sphinx.application.Sphinx.add_latex_package`. Thanks to Takeshi Komiya. +* #1344: add :confval:`gettext_enables` to enable extracting 'index' to gettext + catalog output / applying translation catalog to generated documentation. Bugs fixed ---------- diff --git a/doc/config.rst b/doc/config.rst index a11254ea..f181e5c5 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -447,6 +447,16 @@ documentation on :ref:`intl` for details. .. versionadded:: 1.3 +.. confval:: gettext_enables + + To specify names to enable gettext extracting and translation applying for + i18n. You can specify below names: + + :index: index terms + + The default is ``[]``. + + .. versionadded:: 1.3 .. _html-options: diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 082059fd..01fa06a6 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -108,15 +108,16 @@ class I18nBuilder(Builder): for node, msg in extract_messages(doctree): catalog.add(msg, node) - # Extract translatable messages from index entries. - for node, entries in traverse_translatable_index(doctree): - for typ, msg, tid, main in entries: - for m in split_index_msg(typ, msg): - if typ == 'pair' and m in pairindextypes.values(): - # avoid built-in translated message was incorporated - # in 'sphinx.util.nodes.process_index_entry' - continue - catalog.add(m, node) + if 'index' in self.env.config.gettext_enables: + # Extract translatable messages from index entries. + for node, entries in traverse_translatable_index(doctree): + for typ, msg, tid, main in entries: + for m in split_index_msg(typ, msg): + if typ == 'pair' and m in pairindextypes.values(): + # avoid built-in translated message was incorporated + # in 'sphinx.util.nodes.process_index_entry' + continue + catalog.add(m, node) # determine tzoffset once to remain unaffected by DST change during build diff --git a/sphinx/config.py b/sphinx/config.py index 19aaf674..8593a190 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -211,6 +211,7 @@ class Config(object): gettext_location = (True, 'gettext'), gettext_uuid = (True, 'gettext'), gettext_auto_build = (True, 'env'), + gettext_enables = ([], 'env'), # XML options xml_pretty = (True, 'env'), diff --git a/sphinx/transforms.py b/sphinx/transforms.py index 72917f53..a62fa99a 100644 --- a/sphinx/transforms.py +++ b/sphinx/transforms.py @@ -462,22 +462,23 @@ class Locale(Transform): node.children = patch.children node['translated'] = True - # Extract and translate messages for index entries. - for node, entries in traverse_translatable_index(self.document): - new_entries = [] - for type, msg, tid, main in entries: - msg_parts = split_index_msg(type, msg) - msgstr_parts = [] - for part in msg_parts: - msgstr = catalog.gettext(part) - if not msgstr: - msgstr = part - msgstr_parts.append(msgstr) - - new_entries.append((type, ';'.join(msgstr_parts), tid, main)) - - node['raw_entries'] = entries - node['entries'] = new_entries + if 'index' in env.config.gettext_enables: + # Extract and translate messages for index entries. + for node, entries in traverse_translatable_index(self.document): + new_entries = [] + for type, msg, tid, main in entries: + msg_parts = split_index_msg(type, msg) + msgstr_parts = [] + for part in msg_parts: + msgstr = catalog.gettext(part) + if not msgstr: + msgstr = part + msgstr_parts.append(msgstr) + + new_entries.append((type, ';'.join(msgstr_parts), tid, main)) + + node['raw_entries'] = entries + node['entries'] = new_entries class RemoveTranslatableInline(Transform): diff --git a/tests/roots/test-intl/conf.py b/tests/roots/test-intl/conf.py index 4c37f771..1b20244c 100644 --- a/tests/roots/test-intl/conf.py +++ b/tests/roots/test-intl/conf.py @@ -6,3 +6,4 @@ keep_warnings = True templates_path = ['_templates'] html_additional_pages = {'index': 'index.html'} release = version = '2013.120' +gettext_enables = ['index'] diff --git a/tests/test_build_gettext.py b/tests/test_build_gettext.py index d7189443..1de1306e 100644 --- a/tests/test_build_gettext.py +++ b/tests/test_build_gettext.py @@ -113,6 +113,38 @@ def test_gettext_index_entries(app, status, warning): "Exception", "Statement", "Builtin", + ] + for expect in expected_msgids: + assert expect in msgids + msgids.remove(expect) + + # unexpected msgid existent + assert msgids == [] + + +@with_app('gettext', testroot='intl', + confoverrides={'gettext_compact': False, 'gettext_enables': []}) +def test_gettext_disable_index_entries(app, status, warning): + # regression test for #976 + app.builder.build(['index_entries']) + + _msgid_getter = re.compile(r'msgid "(.*)"').search + + def msgid_getter(msgid): + m = _msgid_getter(msgid) + if m: + return m.groups()[0] + return None + + pot = (app.outdir / 'index_entries.pot').text(encoding='utf-8') + msgids = [_f for _f in map(msgid_getter, pot.splitlines()) if _f] + + expected_msgids = [ + "i18n with index entries", + "index target section", + "this is :index:`Newsletter` target paragraph.", + "various index entries", + "That's all.", ] for expect in expected_msgids: assert expect in msgids diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 01f70e78..374b54a2 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -98,5 +98,6 @@ def test_latex_add_latex_package(app, status, warning): app.add_latex_package('foo') app.add_latex_package('bar', 'baz') app.builder.build_all() - assert '\\usepackage{foo}' in (app.outdir / 'SphinxTests.tex').text() - assert '\\usepackage[baz]{bar}' in (app.outdir / 'SphinxTests.tex').text() + result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') + assert '\\usepackage{foo}' in result + assert '\\usepackage[baz]{bar}' in result |