From eec9c59fc589bc58b9ac47922a93616595365b4e Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 13 Jan 2020 14:25:51 +0900 Subject: [PATCH] Fix #7019: gettext: Absolute path used in message catalogs --- CHANGES | 1 + sphinx/builders/gettext.py | 14 +++++++++++--- sphinx/templates/gettext/message.pot_t | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 9a481b588..721c63cd8 100644 --- a/CHANGES +++ b/CHANGES @@ -47,6 +47,7 @@ Bugs fixed * #6559: Wrong node-ids are generated in glossary directive * #6986: apidoc: misdetects module name for .so file inside module * #6999: napoleon: fails to parse tilde in :exc: role +* #7019: gettext: Absolute path used in message catalogs Testing -------- diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 638408503..65f112510 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -30,7 +30,7 @@ from sphinx.util import split_index_msg, logging, status_iterator from sphinx.util.console import bold # type: ignore from sphinx.util.i18n import CatalogInfo, docname_to_domain from sphinx.util.nodes import extract_messages, traverse_translatable_index -from sphinx.util.osutil import ensuredir, canon_path +from sphinx.util.osutil import ensuredir, canon_path, relpath from sphinx.util.tags import Tags from sphinx.util.template import SphinxRenderer @@ -108,7 +108,8 @@ class MsgOrigin: class GettextRenderer(SphinxRenderer): - def __init__(self, template_path: str = None) -> None: + def __init__(self, template_path: str = None, outdir: str = None) -> None: + self.outdir = outdir if template_path is None: template_path = path.join(package_dir, 'templates', 'gettext') super().__init__(template_path) @@ -122,6 +123,13 @@ class GettextRenderer(SphinxRenderer): self.env.filters['e'] = escape self.env.filters['escape'] = escape + def render(self, filename: str, context: Dict) -> str: + def _relpath(s: str) -> str: + return canon_path(relpath(s, self.outdir)) + + context['relpath'] = _relpath + return super().render(filename, context) + class I18nTags(Tags): """Dummy tags module for I18nBuilder. @@ -297,7 +305,7 @@ class MessageCatalogBuilder(I18nBuilder): ensuredir(path.join(self.outdir, path.dirname(textdomain))) context['messages'] = list(catalog) - content = GettextRenderer().render('message.pot_t', context) + content = GettextRenderer(outdir=self.outdir).render('message.pot_t', context) pofn = path.join(self.outdir, textdomain + '.pot') if should_write(pofn, content): diff --git a/sphinx/templates/gettext/message.pot_t b/sphinx/templates/gettext/message.pot_t index 6bec78729..6138f54c9 100644 --- a/sphinx/templates/gettext/message.pot_t +++ b/sphinx/templates/gettext/message.pot_t @@ -18,7 +18,7 @@ msgstr "" {% for message in messages %} {% if display_location -%} {% for source, line in message.locations -%} -#: {{ source }}:{{ line }} +#: {{ relpath(source) }}:{{ line }} {% endfor -%} {% endif -%}