mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Correctly support custom gettext output templates (#12645)
Co-authored-by: Jeremy Bowman <jmbowman@alum.mit.edu> Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
This commit is contained in:
@@ -47,6 +47,8 @@ Bugs fixed
|
||||
Patch by James Addison.
|
||||
* #12639: Fix singular and plural search results text.
|
||||
Patch by Hugo van Kemenade.
|
||||
* #12645: Correctly support custom gettext output templates.
|
||||
Patch by Jeremy Bowman.
|
||||
|
||||
Testing
|
||||
-------
|
||||
|
@@ -119,6 +119,11 @@ section describe an easy way to translate with *sphinx-intl*.
|
||||
$ make gettext
|
||||
|
||||
The generated pot files will be placed in the ``_build/gettext`` directory.
|
||||
If you want to customize the output beyond what can be done via the
|
||||
:ref:`intl-options`, the
|
||||
:download:`default pot file template <../../../sphinx/templates/gettext/message.pot.jinja>`
|
||||
can be replaced by a custom :file:`message.pot.jinja` file placed in any
|
||||
directory listed in :confval:`templates_path`.
|
||||
|
||||
#. Generate po files.
|
||||
|
||||
|
@@ -7,6 +7,7 @@ import time
|
||||
from codecs import open
|
||||
from collections import defaultdict
|
||||
from os import getenv, path, walk
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, Literal
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -28,7 +29,7 @@ from sphinx.util.template import SphinxRenderer
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import os
|
||||
from collections.abc import Iterable, Iterator
|
||||
from collections.abc import Iterable, Iterator, Sequence
|
||||
|
||||
from docutils.nodes import Element
|
||||
|
||||
@@ -36,6 +37,8 @@ if TYPE_CHECKING:
|
||||
from sphinx.config import Config
|
||||
from sphinx.util.typing import ExtensionMetadata
|
||||
|
||||
DEFAULT_TEMPLATE_PATH = Path(package_dir, 'templates', 'gettext')
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -91,13 +94,14 @@ class MsgOrigin:
|
||||
|
||||
class GettextRenderer(SphinxRenderer):
|
||||
def __init__(
|
||||
self, template_path: list[str | os.PathLike[str]] | None = None,
|
||||
self, template_path: Sequence[str | os.PathLike[str]] | None = None,
|
||||
outdir: str | os.PathLike[str] | None = None,
|
||||
) -> None:
|
||||
self.outdir = outdir
|
||||
if template_path is None:
|
||||
template_path = [path.join(package_dir, 'templates', 'gettext')]
|
||||
super().__init__(template_path)
|
||||
super().__init__([DEFAULT_TEMPLATE_PATH])
|
||||
else:
|
||||
super().__init__([*template_path, DEFAULT_TEMPLATE_PATH])
|
||||
|
||||
def escape(s: str) -> str:
|
||||
s = s.replace('\\', r'\\')
|
||||
@@ -287,7 +291,12 @@ class MessageCatalogBuilder(I18nBuilder):
|
||||
ensuredir(path.join(self.outdir, path.dirname(textdomain)))
|
||||
|
||||
context['messages'] = list(catalog)
|
||||
content = GettextRenderer(outdir=self.outdir).render('message.pot.jinja', context)
|
||||
template_path = [
|
||||
self.app.srcdir / rel_path
|
||||
for rel_path in self.config.templates_path
|
||||
]
|
||||
renderer = GettextRenderer(template_path, outdir=self.outdir)
|
||||
content = renderer.render('message.pot.jinja', context)
|
||||
|
||||
pofn = path.join(self.outdir, textdomain + '.pot')
|
||||
if should_write(pofn, content):
|
||||
|
@@ -0,0 +1,21 @@
|
||||
# EVEN MORE DESCRIPTIVE TITLE.
|
||||
# Copyright (C) {{ copyright }}
|
||||
# This file is distributed under the same license as the {{ project }} package.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
||||
#
|
||||
#, fuzzy
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: {{ project|e }} {{ version|e }}\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: {{ ctime|e }}\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: {{ last_translator|e }}\n"
|
||||
"Language-Team: {{ language_team|e }}\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
{% for message in messages %}
|
||||
msgid "{{ message.text|e }}"
|
||||
msgstr ""
|
||||
{% endfor -%}
|
1
tests/roots/test-gettext-custom-output-template/conf.py
Normal file
1
tests/roots/test-gettext-custom-output-template/conf.py
Normal file
@@ -0,0 +1 @@
|
||||
templates_path = ['_templates']
|
@@ -0,0 +1,7 @@
|
||||
CONTENTS
|
||||
========
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:numbered:
|
||||
:caption: Table of Contents
|
@@ -201,6 +201,15 @@ def test_gettext_template_msgid_order_in_sphinxpot(app):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.sphinx('gettext', testroot='gettext-custom-output-template')
|
||||
def test_gettext_custom_output_template(app):
|
||||
app.build(force_all=True)
|
||||
assert (app.outdir / 'index.pot').is_file()
|
||||
|
||||
result = (app.outdir / 'index.pot').read_text(encoding='utf8')
|
||||
assert 'EVEN MORE DESCRIPTIVE TITLE' in result
|
||||
|
||||
|
||||
@pytest.mark.sphinx(
|
||||
'gettext',
|
||||
srcdir='root-gettext',
|
||||
|
Reference in New Issue
Block a user