2010-05-29 18:14:42 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
2011-01-08 17:32:32 +01:00
|
|
|
sphinx.builders.gettext
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
2010-05-29 18:14:42 +02:00
|
|
|
|
|
|
|
|
The MessageCatalogBuilder class.
|
|
|
|
|
|
2018-01-01 01:06:58 +09:00
|
|
|
:copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
|
2010-05-29 18:14:42 +02:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
|
"""
|
|
|
|
|
|
2014-04-30 23:04:21 +09:00
|
|
|
from __future__ import unicode_literals
|
2013-11-17 08:37:28 +00:00
|
|
|
|
2010-08-15 12:26:37 +02:00
|
|
|
from codecs import open
|
2018-02-17 14:15:27 +09:00
|
|
|
from collections import defaultdict, OrderedDict
|
2018-01-28 01:52:16 +09:00
|
|
|
from datetime import datetime, tzinfo, timedelta
|
|
|
|
|
from os import path, walk, getenv
|
|
|
|
|
from time import time
|
2013-03-10 22:07:31 +09:00
|
|
|
from uuid import uuid4
|
2010-06-02 09:35:33 +02:00
|
|
|
|
2017-03-02 14:57:39 +09:00
|
|
|
from six import iteritems, StringIO
|
2014-04-29 21:20:56 +09:00
|
|
|
|
2010-05-29 18:14:42 +02:00
|
|
|
from sphinx.builders import Builder
|
2018-02-24 16:48:12 +09:00
|
|
|
from sphinx.domains.python import pairindextypes
|
2018-02-25 22:16:09 +09:00
|
|
|
from sphinx.locale import __
|
2017-01-05 13:41:17 +09:00
|
|
|
from sphinx.util import split_index_msg, logging, status_iterator
|
2018-01-28 01:52:16 +09:00
|
|
|
from sphinx.util.console import bold # type: ignore
|
|
|
|
|
from sphinx.util.i18n import find_catalog
|
2013-10-04 08:12:20 +00:00
|
|
|
from sphinx.util.nodes import extract_messages, traverse_translatable_index
|
2018-04-01 17:46:44 +09:00
|
|
|
from sphinx.util.osutil import relpath, ensuredir, canon_path
|
2018-01-28 01:52:16 +09:00
|
|
|
from sphinx.util.tags import Tags
|
2010-05-29 18:14:42 +02:00
|
|
|
|
2018-03-13 23:01:11 +09:00
|
|
|
if False:
|
|
|
|
|
# For type annotation
|
2017-05-07 14:09:54 +09:00
|
|
|
from typing import Any, DefaultDict, Dict, Iterable, List, Set, Tuple # NOQA
|
2016-11-08 17:47:52 +09:00
|
|
|
from docutils import nodes # NOQA
|
|
|
|
|
from sphinx.util.i18n import CatalogInfo # NOQA
|
|
|
|
|
from sphinx.application import Sphinx # NOQA
|
|
|
|
|
|
2016-12-22 13:41:56 +09:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2014-04-30 23:04:21 +09:00
|
|
|
POHEADER = r"""
|
2010-06-02 09:45:13 +02:00
|
|
|
# SOME DESCRIPTIVE TITLE.
|
|
|
|
|
# Copyright (C) %(copyright)s
|
|
|
|
|
# This file is distributed under the same license as the %(project)s package.
|
|
|
|
|
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
|
|
|
|
#
|
|
|
|
|
#, fuzzy
|
|
|
|
|
msgid ""
|
|
|
|
|
msgstr ""
|
2011-10-08 16:04:42 +02:00
|
|
|
"Project-Id-Version: %(project)s %(version)s\n"
|
2010-06-02 09:45:13 +02:00
|
|
|
"Report-Msgid-Bugs-To: \n"
|
2010-06-26 12:25:53 +02:00
|
|
|
"POT-Creation-Date: %(ctime)s\n"
|
2010-06-02 09:45:13 +02:00
|
|
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
|
|
|
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
|
|
|
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
|
|
|
|
"MIME-Version: 1.0\n"
|
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\n"
|
|
|
|
|
"Content-Transfer-Encoding: 8bit\n"
|
|
|
|
|
|
|
|
|
|
"""[1:]
|
|
|
|
|
|
2010-08-21 19:34:05 +02:00
|
|
|
|
2011-07-06 08:25:25 +02:00
|
|
|
class Catalog(object):
|
2011-07-06 08:31:40 +02:00
|
|
|
"""Catalog of translatable messages."""
|
|
|
|
|
|
2011-07-06 08:25:25 +02:00
|
|
|
def __init__(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> None
|
|
|
|
|
self.messages = [] # type: List[unicode]
|
|
|
|
|
# retain insertion order, a la OrderedDict
|
2018-02-19 09:20:04 +09:00
|
|
|
self.metadata = OrderedDict() # type: Dict[unicode, List[Tuple[unicode, int, unicode]]] # NOQA
|
2018-02-18 22:28:37 +09:00
|
|
|
# msgid -> file, line, uid
|
2011-07-06 08:31:40 +02:00
|
|
|
|
2011-07-06 08:25:25 +02:00
|
|
|
def add(self, msg, origin):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (unicode, MsgOrigin) -> None
|
2014-03-02 14:40:29 +09:00
|
|
|
if not hasattr(origin, 'uid'):
|
2014-03-02 08:40:48 +01:00
|
|
|
# Nodes that are replicated like todo don't have a uid,
|
|
|
|
|
# however i18n is also unnecessary.
|
2014-03-02 14:40:29 +09:00
|
|
|
return
|
2011-07-06 08:25:25 +02:00
|
|
|
if msg not in self.metadata: # faster lookup in hash
|
|
|
|
|
self.messages.append(msg)
|
|
|
|
|
self.metadata[msg] = []
|
|
|
|
|
self.metadata[msg].append((origin.source, origin.line, origin.uid))
|
|
|
|
|
|
|
|
|
|
|
2013-03-10 22:07:31 +09:00
|
|
|
class MsgOrigin(object):
|
|
|
|
|
"""
|
|
|
|
|
Origin holder for Catalog message origin.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, source, line):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (unicode, int) -> None
|
2013-03-10 22:07:31 +09:00
|
|
|
self.source = source
|
|
|
|
|
self.line = line
|
|
|
|
|
self.uid = uuid4().hex
|
|
|
|
|
|
|
|
|
|
|
2016-08-20 20:44:17 +09:00
|
|
|
class I18nTags(Tags):
|
|
|
|
|
"""Dummy tags module for I18nBuilder.
|
|
|
|
|
|
|
|
|
|
To translate all text inside of only nodes, this class
|
|
|
|
|
always returns True value even if no tags are defined.
|
|
|
|
|
"""
|
|
|
|
|
def eval_condition(self, condition):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (Any) -> bool
|
2016-08-20 20:44:17 +09:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2011-01-08 00:06:02 +01:00
|
|
|
class I18nBuilder(Builder):
|
2010-06-04 18:38:16 +02:00
|
|
|
"""
|
2010-08-21 19:34:05 +02:00
|
|
|
General i18n builder.
|
2010-06-04 18:38:16 +02:00
|
|
|
"""
|
2010-08-15 12:13:04 +02:00
|
|
|
name = 'i18n'
|
2011-01-08 17:32:32 +01:00
|
|
|
versioning_method = 'text'
|
2017-03-20 10:39:47 +09:00
|
|
|
versioning_compare = None # type: bool
|
|
|
|
|
# be set by `gettext_uuid`
|
2017-03-20 08:54:36 +09:00
|
|
|
use_message_catalog = False
|
2014-10-05 21:50:44 +09:00
|
|
|
|
2010-06-02 09:17:42 +02:00
|
|
|
def init(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> None
|
2010-08-15 12:13:04 +02:00
|
|
|
Builder.init(self)
|
2017-03-19 23:16:37 +09:00
|
|
|
self.env.set_versioning_method(self.versioning_method,
|
|
|
|
|
self.env.config.gettext_uuid)
|
2016-08-20 20:44:17 +09:00
|
|
|
self.tags = I18nTags()
|
2017-05-07 14:09:54 +09:00
|
|
|
self.catalogs = defaultdict(Catalog) # type: DefaultDict[unicode, Catalog]
|
2010-05-29 18:14:42 +02:00
|
|
|
|
|
|
|
|
def get_target_uri(self, docname, typ=None):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (unicode, unicode) -> unicode
|
2010-05-29 18:14:42 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
def get_outdated_docs(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> Set[unicode]
|
2010-05-29 18:14:42 +02:00
|
|
|
return self.env.found_docs
|
|
|
|
|
|
|
|
|
|
def prepare_writing(self, docnames):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (Set[unicode]) -> None
|
2010-05-29 18:14:42 +02:00
|
|
|
return
|
|
|
|
|
|
2014-08-03 16:22:08 +09:00
|
|
|
def compile_catalogs(self, catalogs, message):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (Set[CatalogInfo], unicode) -> None
|
2014-08-03 16:22:08 +09:00
|
|
|
return
|
|
|
|
|
|
2010-05-29 18:14:42 +02:00
|
|
|
def write_doc(self, docname, doctree):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (unicode, nodes.Node) -> None
|
2011-10-03 14:08:33 +02:00
|
|
|
catalog = self.catalogs[find_catalog(docname,
|
|
|
|
|
self.config.gettext_compact)]
|
2010-08-15 12:53:00 +02:00
|
|
|
|
|
|
|
|
for node, msg in extract_messages(doctree):
|
2011-07-06 08:25:25 +02:00
|
|
|
catalog.add(msg, node)
|
2010-08-15 12:53:00 +02:00
|
|
|
|
2015-02-21 23:48:18 +09:00
|
|
|
if 'index' in self.env.config.gettext_additional_targets:
|
2014-09-28 21:19:54 +09:00
|
|
|
# Extract translatable messages from index entries.
|
|
|
|
|
for node, entries in traverse_translatable_index(doctree):
|
2016-02-13 22:30:06 +09:00
|
|
|
for typ, msg, tid, main, key_ in entries:
|
2014-09-28 21:19:54 +09:00
|
|
|
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)
|
2013-01-05 23:38:21 +09:00
|
|
|
|
2010-08-21 19:34:05 +02:00
|
|
|
|
2013-09-16 08:28:47 +02:00
|
|
|
# determine tzoffset once to remain unaffected by DST change during build
|
2013-06-15 12:59:49 +09:00
|
|
|
timestamp = time()
|
2013-09-16 08:28:47 +02:00
|
|
|
tzdelta = datetime.fromtimestamp(timestamp) - \
|
|
|
|
|
datetime.utcfromtimestamp(timestamp)
|
2016-05-03 09:53:54 +02:00
|
|
|
# set timestamp from SOURCE_DATE_EPOCH if set
|
|
|
|
|
# see https://reproducible-builds.org/specs/source-date-epoch/
|
2016-04-28 23:36:28 +02:00
|
|
|
source_date_epoch = getenv('SOURCE_DATE_EPOCH')
|
|
|
|
|
if source_date_epoch is not None:
|
|
|
|
|
timestamp = float(source_date_epoch)
|
2016-06-19 16:40:42 +03:00
|
|
|
tzdelta = timedelta(0)
|
2015-03-08 16:24:24 +01:00
|
|
|
|
2016-05-02 10:05:25 +02:00
|
|
|
|
2013-06-13 12:54:05 +09:00
|
|
|
class LocalTimeZone(tzinfo):
|
2013-05-28 11:30:57 +00:00
|
|
|
|
2013-06-13 12:54:05 +09:00
|
|
|
def __init__(self, *args, **kw):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (Any, Any) -> None
|
|
|
|
|
super(LocalTimeZone, self).__init__(*args, **kw) # type: ignore
|
2013-06-13 12:54:05 +09:00
|
|
|
self.tzdelta = tzdelta
|
2013-05-28 11:30:57 +00:00
|
|
|
|
|
|
|
|
def utcoffset(self, dt):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (datetime) -> timedelta
|
2013-06-13 12:54:05 +09:00
|
|
|
return self.tzdelta
|
2013-05-28 11:30:57 +00:00
|
|
|
|
|
|
|
|
def dst(self, dt):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (datetime) -> timedelta
|
2013-06-13 12:54:05 +09:00
|
|
|
return timedelta(0)
|
2013-05-28 11:30:57 +00:00
|
|
|
|
2016-11-16 10:58:41 +09:00
|
|
|
|
2013-06-13 12:54:05 +09:00
|
|
|
ltz = LocalTimeZone()
|
2013-05-28 11:30:57 +00:00
|
|
|
|
|
|
|
|
|
2017-03-02 14:57:39 +09:00
|
|
|
def should_write(filepath, new_content):
|
2018-01-22 22:05:38 +09:00
|
|
|
# type: (unicode, unicode) -> bool
|
2017-03-02 14:57:39 +09:00
|
|
|
if not path.exists(filepath):
|
|
|
|
|
return True
|
2017-04-27 00:17:07 +09:00
|
|
|
try:
|
|
|
|
|
with open(filepath, 'r', encoding='utf-8') as oldpot: # type: ignore
|
|
|
|
|
old_content = oldpot.read()
|
|
|
|
|
old_header_index = old_content.index('"POT-Creation-Date:')
|
|
|
|
|
new_header_index = new_content.index('"POT-Creation-Date:')
|
|
|
|
|
old_body_index = old_content.index('"PO-Revision-Date:')
|
|
|
|
|
new_body_index = new_content.index('"PO-Revision-Date:')
|
|
|
|
|
return ((old_content[:old_header_index] != new_content[:new_header_index]) or
|
|
|
|
|
(new_content[new_body_index:] != old_content[old_body_index:]))
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
2017-03-02 14:57:39 +09:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2010-08-21 19:34:05 +02:00
|
|
|
class MessageCatalogBuilder(I18nBuilder):
|
2010-08-15 12:13:04 +02:00
|
|
|
"""
|
|
|
|
|
Builds gettext-style message catalogs (.pot files).
|
|
|
|
|
"""
|
|
|
|
|
name = 'gettext'
|
2018-02-25 22:16:09 +09:00
|
|
|
epilog = __('The message catalogs are in %(outdir)s.')
|
2010-05-29 18:14:42 +02:00
|
|
|
|
2013-03-10 22:07:31 +09:00
|
|
|
def init(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> None
|
2013-03-10 22:07:31 +09:00
|
|
|
I18nBuilder.init(self)
|
|
|
|
|
self.create_template_bridge()
|
|
|
|
|
self.templates.init(self)
|
|
|
|
|
|
|
|
|
|
def _collect_templates(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> Set[unicode]
|
2013-03-10 22:07:31 +09:00
|
|
|
template_files = set()
|
|
|
|
|
for template_path in self.config.templates_path:
|
|
|
|
|
tmpl_abs_path = path.join(self.app.srcdir, template_path)
|
|
|
|
|
for dirpath, dirs, files in walk(tmpl_abs_path):
|
|
|
|
|
for fn in files:
|
|
|
|
|
if fn.endswith('.html'):
|
2015-10-10 16:44:46 +03:00
|
|
|
filename = canon_path(path.join(dirpath, fn))
|
2013-03-10 22:07:31 +09:00
|
|
|
template_files.add(filename)
|
|
|
|
|
return template_files
|
|
|
|
|
|
|
|
|
|
def _extract_from_template(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> None
|
2018-02-17 14:15:27 +09:00
|
|
|
files = list(self._collect_templates())
|
|
|
|
|
files.sort()
|
2018-02-25 22:16:09 +09:00
|
|
|
logger.info(bold(__('building [%s]: ') % self.name), nonl=1)
|
|
|
|
|
logger.info(__('targets for %d template files'), len(files))
|
2013-03-10 22:07:31 +09:00
|
|
|
|
|
|
|
|
extract_translations = self.templates.environment.extract_translations
|
|
|
|
|
|
2018-02-25 22:16:09 +09:00
|
|
|
for template in status_iterator(files, __('reading templates... '), "purple", # type: ignore # NOQA
|
2017-01-05 13:41:17 +09:00
|
|
|
len(files), self.app.verbosity):
|
2016-11-08 17:47:52 +09:00
|
|
|
with open(template, 'r', encoding='utf-8') as f: # type: ignore
|
2013-11-17 08:37:28 +00:00
|
|
|
context = f.read()
|
2013-03-10 22:07:31 +09:00
|
|
|
for line, meth, msg in extract_translations(context):
|
|
|
|
|
origin = MsgOrigin(template, line)
|
2013-04-13 21:16:22 +09:00
|
|
|
self.catalogs['sphinx'].add(msg, origin)
|
2013-03-10 22:07:31 +09:00
|
|
|
|
|
|
|
|
def build(self, docnames, summary=None, method='update'):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: (Iterable[unicode], unicode, unicode) -> None
|
2013-03-10 22:07:31 +09:00
|
|
|
self._extract_from_template()
|
|
|
|
|
I18nBuilder.build(self, docnames, summary, method)
|
|
|
|
|
|
2010-05-29 18:14:42 +02:00
|
|
|
def finish(self):
|
2016-11-08 17:47:52 +09:00
|
|
|
# type: () -> None
|
2010-08-21 19:34:05 +02:00
|
|
|
I18nBuilder.finish(self)
|
2010-06-26 12:25:53 +02:00
|
|
|
data = dict(
|
|
|
|
|
version = self.config.version,
|
|
|
|
|
copyright = self.config.copyright,
|
|
|
|
|
project = self.config.project,
|
2017-03-03 12:19:09 +09:00
|
|
|
ctime = datetime.fromtimestamp(
|
2013-06-15 12:59:49 +09:00
|
|
|
timestamp, ltz).strftime('%Y-%m-%d %H:%M%z'),
|
2010-06-26 12:25:53 +02:00
|
|
|
)
|
2017-01-21 21:14:09 +09:00
|
|
|
for textdomain, catalog in status_iterator(iteritems(self.catalogs), # type: ignore
|
2018-02-25 22:16:09 +09:00
|
|
|
__("writing message catalogs... "),
|
2017-01-05 13:41:17 +09:00
|
|
|
"darkgreen", len(self.catalogs),
|
|
|
|
|
self.app.verbosity,
|
|
|
|
|
lambda textdomain__: textdomain__[0]):
|
2011-10-03 13:20:53 +02:00
|
|
|
# noop if config.gettext_compact is set
|
|
|
|
|
ensuredir(path.join(self.outdir, path.dirname(textdomain)))
|
|
|
|
|
|
|
|
|
|
pofn = path.join(self.outdir, textdomain + '.pot')
|
2017-03-03 10:43:22 +01:00
|
|
|
output = StringIO()
|
2017-03-03 11:11:49 +01:00
|
|
|
output.write(POHEADER % data) # type: ignore
|
2017-03-03 10:43:22 +01:00
|
|
|
|
|
|
|
|
for message in catalog.messages:
|
|
|
|
|
positions = catalog.metadata[message]
|
|
|
|
|
|
|
|
|
|
if self.config.gettext_location:
|
|
|
|
|
# generate "#: file1:line1\n#: file2:line2 ..."
|
2017-03-03 11:11:49 +01:00
|
|
|
output.write("#: %s\n" % "\n#: ".join( # type: ignore
|
2018-04-01 17:46:44 +09:00
|
|
|
"%s:%s" % (canon_path(relpath(source, self.outdir)), line)
|
2017-03-03 10:43:22 +01:00
|
|
|
for source, line, _ in positions))
|
|
|
|
|
if self.config.gettext_uuid:
|
|
|
|
|
# generate "# uuid1\n# uuid2\n ..."
|
2017-03-03 11:11:49 +01:00
|
|
|
output.write("# %s\n" % "\n# ".join( # type: ignore
|
2017-03-03 10:43:22 +01:00
|
|
|
uid for _, _, uid in positions))
|
|
|
|
|
|
|
|
|
|
# message contains *one* line of text ready for translation
|
|
|
|
|
message = message.replace('\\', r'\\'). \
|
|
|
|
|
replace('"', r'\"'). \
|
|
|
|
|
replace('\n', '\\n"\n"')
|
2017-03-03 11:11:49 +01:00
|
|
|
output.write('msgid "%s"\nmsgstr ""\n\n' % message) # type: ignore
|
2017-03-03 10:43:22 +01:00
|
|
|
|
|
|
|
|
content = output.getvalue()
|
|
|
|
|
|
|
|
|
|
if should_write(pofn, content):
|
2017-03-03 11:11:49 +01:00
|
|
|
with open(pofn, 'w', encoding='utf-8') as pofile: # type: ignore
|
2017-03-03 10:43:22 +01:00
|
|
|
pofile.write(content)
|
2016-07-04 10:46:27 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup(app):
|
2016-12-15 19:22:40 +09:00
|
|
|
# type: (Sphinx) -> Dict[unicode, Any]
|
2016-07-04 10:46:27 +09:00
|
|
|
app.add_builder(MessageCatalogBuilder)
|
|
|
|
|
|
|
|
|
|
app.add_config_value('gettext_compact', True, 'gettext')
|
|
|
|
|
app.add_config_value('gettext_location', True, 'gettext')
|
|
|
|
|
app.add_config_value('gettext_uuid', False, 'gettext')
|
|
|
|
|
app.add_config_value('gettext_auto_build', True, 'env')
|
|
|
|
|
app.add_config_value('gettext_additional_targets', [], 'env')
|
2016-12-12 21:22:16 +09:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'version': 'builtin',
|
|
|
|
|
'parallel_read_safe': True,
|
|
|
|
|
'parallel_write_safe': True,
|
|
|
|
|
}
|