mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-09-03 20:52:55 -05:00
Move translation patching into read_doc for access to runtime settings.
This commit is contained in:
+27
-18
@@ -41,7 +41,7 @@ from sphinx.util.osutil import movefile, SEP, ustrftime
|
||||
from sphinx.util.matching import compile_matchers
|
||||
from sphinx.util.pycompat import all
|
||||
from sphinx.errors import SphinxError, ExtensionError
|
||||
from sphinx.locale import _
|
||||
from sphinx.locale import _, init as init_locale
|
||||
|
||||
|
||||
orig_role_function = roles.role
|
||||
@@ -181,28 +181,12 @@ class CitationReferences(Transform):
|
||||
refnode += nodes.Text('[' + cittext + ']')
|
||||
citnode.parent.replace(citnode, refnode)
|
||||
|
||||
class Locale(Transform):
|
||||
"""
|
||||
Replace translatable nodes with their translated doctree.
|
||||
"""
|
||||
default_priority = 0
|
||||
|
||||
def apply(self):
|
||||
settings, source = self.document.settings, self.document['source']
|
||||
parser = RSTParser()
|
||||
for node, msg in extract_messages(self.document):
|
||||
ctx = node.parent
|
||||
patch = new_document(source, settings)
|
||||
msgstr = "Insert translation **here**."
|
||||
parser.parse(msgstr, patch)
|
||||
ctx.replace(node, patch.children)
|
||||
|
||||
|
||||
class SphinxStandaloneReader(standalone.Reader):
|
||||
"""
|
||||
Add our own transforms.
|
||||
"""
|
||||
transforms = [Locale, CitationReferences, DefaultSubstitutions,
|
||||
transforms = [CitationReferences, DefaultSubstitutions,
|
||||
MoveModuleTargets, HandleCodeBlocks, SortIds]
|
||||
|
||||
def get_transforms(self):
|
||||
@@ -611,6 +595,7 @@ class BuildEnvironment:
|
||||
Parse a file and add/update inventory entries for the doctree.
|
||||
If srcpath is given, read from a different source file.
|
||||
"""
|
||||
section = docname.split(SEP, 1)[0]
|
||||
# remove all inventory entries for that file
|
||||
if app:
|
||||
app.emit('env-purge-doc', self, docname)
|
||||
@@ -675,6 +660,7 @@ class BuildEnvironment:
|
||||
|
||||
# post-processing
|
||||
self.filter_messages(doctree)
|
||||
self.process_translations(doctree, self.get_translation(section))
|
||||
self.process_dependencies(docname, doctree)
|
||||
self.process_images(docname, doctree)
|
||||
self.process_downloads(docname, doctree)
|
||||
@@ -749,6 +735,13 @@ class BuildEnvironment:
|
||||
def note_dependency(self, filename):
|
||||
self.dependencies.setdefault(self.docname, set()).add(filename)
|
||||
|
||||
def get_translation(self, section):
|
||||
translation, has_trans = init_locale(self.config.locale_dirs,
|
||||
self.config.language, section)
|
||||
if not has_trans:
|
||||
return None
|
||||
return translation
|
||||
|
||||
# post-processing of read doctrees
|
||||
|
||||
def filter_messages(self, doctree):
|
||||
@@ -760,6 +753,22 @@ class BuildEnvironment:
|
||||
if node['level'] < filterlevel:
|
||||
node.parent.remove(node)
|
||||
|
||||
def process_translations(self, doctree, translation):
|
||||
"""
|
||||
Replace translatable nodes with their translated doctree.
|
||||
"""
|
||||
if not translation:
|
||||
return
|
||||
settings, source = doctree.settings, doctree['source']
|
||||
parser = RSTParser()
|
||||
for node, msg in extract_messages(doctree):
|
||||
ctx = node.parent
|
||||
patch = new_document(source, settings)
|
||||
msgstr = translation.gettext(msg)
|
||||
parser.parse(msgstr, patch)
|
||||
ctx.replace(node, patch.children)
|
||||
|
||||
|
||||
def process_dependencies(self, docname, doctree):
|
||||
"""
|
||||
Process docutils-generated dependency info.
|
||||
|
||||
Reference in New Issue
Block a user