mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Move compile_catalog_plusjs
to utils/
This commit is contained in:
parent
301c7bdf57
commit
1e443bdcb4
117
setup.py
117
setup.py
@ -1,6 +1,4 @@
|
||||
import os
|
||||
import sys
|
||||
from io import StringIO
|
||||
|
||||
from setuptools import find_packages, setup
|
||||
|
||||
@ -57,120 +55,6 @@ extras_require = {
|
||||
],
|
||||
}
|
||||
|
||||
# Provide a "compile_catalog" command that also creates the translated
|
||||
# JavaScript files if Babel is available.
|
||||
|
||||
cmdclass = {}
|
||||
|
||||
|
||||
class Tee:
|
||||
def __init__(self, stream):
|
||||
self.stream = stream
|
||||
self.buffer = StringIO()
|
||||
|
||||
def write(self, s):
|
||||
self.stream.write(s)
|
||||
self.buffer.write(s)
|
||||
|
||||
def flush(self):
|
||||
self.stream.flush()
|
||||
|
||||
|
||||
try:
|
||||
from json import dump
|
||||
|
||||
from babel.messages.frontend import compile_catalog
|
||||
from babel.messages.pofile import read_po
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
class compile_catalog_plusjs(compile_catalog):
|
||||
"""
|
||||
An extended command that writes all message strings that occur in
|
||||
JavaScript files to a JavaScript file along with the .mo file.
|
||||
|
||||
Unfortunately, babel's setup command isn't built very extensible, so
|
||||
most of the run() code is duplicated here.
|
||||
"""
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
sys.stderr = Tee(sys.stderr)
|
||||
compile_catalog.run(self)
|
||||
finally:
|
||||
if sys.stderr.buffer.getvalue():
|
||||
print("Compiling failed.")
|
||||
sys.exit(1)
|
||||
|
||||
if isinstance(self.domain, list):
|
||||
for domain in self.domain:
|
||||
self._run_domain_js(domain)
|
||||
else:
|
||||
self._run_domain_js(self.domain)
|
||||
|
||||
def _run_domain_js(self, domain):
|
||||
po_files = []
|
||||
js_files = []
|
||||
|
||||
if not self.input_file:
|
||||
if self.locale:
|
||||
po_files.append((self.locale,
|
||||
os.path.join(self.directory, self.locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.po')))
|
||||
js_files.append(os.path.join(self.directory, self.locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.js'))
|
||||
else:
|
||||
for locale in os.listdir(self.directory):
|
||||
po_file = os.path.join(self.directory, locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.po')
|
||||
if os.path.exists(po_file):
|
||||
po_files.append((locale, po_file))
|
||||
js_files.append(os.path.join(self.directory, locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.js'))
|
||||
else:
|
||||
po_files.append((self.locale, self.input_file))
|
||||
if self.output_file:
|
||||
js_files.append(self.output_file)
|
||||
else:
|
||||
js_files.append(os.path.join(self.directory, self.locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.js'))
|
||||
|
||||
for js_file, (locale, po_file) in zip(js_files, po_files):
|
||||
with open(po_file, encoding='utf8') as infile:
|
||||
catalog = read_po(infile, locale)
|
||||
|
||||
if catalog.fuzzy and not self.use_fuzzy:
|
||||
continue
|
||||
|
||||
self.log.info('writing JavaScript strings in catalog %r to %r',
|
||||
po_file, js_file)
|
||||
|
||||
jscatalog = {}
|
||||
for message in catalog:
|
||||
if any(x[0].endswith(('.js', '.js_t', '.html'))
|
||||
for x in message.locations):
|
||||
msgid = message.id
|
||||
if isinstance(msgid, (list, tuple)):
|
||||
msgid = msgid[0]
|
||||
jscatalog[msgid] = message.string
|
||||
|
||||
with open(js_file, 'wt', encoding='utf8') as outfile:
|
||||
outfile.write('Documentation.addTranslations(')
|
||||
dump({
|
||||
'messages': jscatalog,
|
||||
'plural_expr': catalog.plural_expr,
|
||||
'locale': str(catalog.locale)
|
||||
}, outfile, sort_keys=True, indent=4)
|
||||
outfile.write(');')
|
||||
|
||||
cmdclass['compile_catalog'] = compile_catalog_plusjs
|
||||
|
||||
|
||||
setup(
|
||||
name='Sphinx',
|
||||
version=sphinx.__version__,
|
||||
@ -246,5 +130,4 @@ setup(
|
||||
python_requires=">=3.6",
|
||||
install_requires=install_requires,
|
||||
extras_require=extras_require,
|
||||
cmdclass=cmdclass,
|
||||
)
|
||||
|
109
utils/babel_runner.py
Normal file
109
utils/babel_runner.py
Normal file
@ -0,0 +1,109 @@
|
||||
from io import StringIO
|
||||
from json import dump
|
||||
import os
|
||||
import sys
|
||||
|
||||
from babel.messages.frontend import compile_catalog
|
||||
from babel.messages.pofile import read_po
|
||||
|
||||
# Provide a "compile_catalog" command that also creates the translated
|
||||
# JavaScript files if Babel is available.
|
||||
|
||||
|
||||
class Tee:
|
||||
def __init__(self, stream):
|
||||
self.stream = stream
|
||||
self.buffer = StringIO()
|
||||
|
||||
def write(self, s):
|
||||
self.stream.write(s)
|
||||
self.buffer.write(s)
|
||||
|
||||
def flush(self):
|
||||
self.stream.flush()
|
||||
|
||||
|
||||
class compile_catalog_plusjs(compile_catalog):
|
||||
"""
|
||||
An extended command that writes all message strings that occur in
|
||||
JavaScript files to a JavaScript file along with the .mo file.
|
||||
|
||||
Unfortunately, babel's setup command isn't built very extensible, so
|
||||
most of the run() code is duplicated here.
|
||||
"""
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
sys.stderr = Tee(sys.stderr)
|
||||
compile_catalog.run(self)
|
||||
finally:
|
||||
if sys.stderr.buffer.getvalue():
|
||||
print("Compiling failed.")
|
||||
sys.exit(1)
|
||||
|
||||
if isinstance(self.domain, list):
|
||||
for domain in self.domain:
|
||||
self._run_domain_js(domain)
|
||||
else:
|
||||
self._run_domain_js(self.domain)
|
||||
|
||||
def _run_domain_js(self, domain):
|
||||
po_files = []
|
||||
js_files = []
|
||||
|
||||
if not self.input_file:
|
||||
if self.locale:
|
||||
po_files.append((self.locale,
|
||||
os.path.join(self.directory, self.locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.po')))
|
||||
js_files.append(os.path.join(self.directory, self.locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.js'))
|
||||
else:
|
||||
for locale in os.listdir(self.directory):
|
||||
po_file = os.path.join(self.directory, locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.po')
|
||||
if os.path.exists(po_file):
|
||||
po_files.append((locale, po_file))
|
||||
js_files.append(os.path.join(self.directory, locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.js'))
|
||||
else:
|
||||
po_files.append((self.locale, self.input_file))
|
||||
if self.output_file:
|
||||
js_files.append(self.output_file)
|
||||
else:
|
||||
js_files.append(os.path.join(self.directory, self.locale,
|
||||
'LC_MESSAGES',
|
||||
domain + '.js'))
|
||||
|
||||
for js_file, (locale, po_file) in zip(js_files, po_files):
|
||||
with open(po_file, encoding='utf8') as infile:
|
||||
catalog = read_po(infile, locale)
|
||||
|
||||
if catalog.fuzzy and not self.use_fuzzy:
|
||||
continue
|
||||
|
||||
self.log.info('writing JavaScript strings in catalog %r to %r',
|
||||
po_file, js_file)
|
||||
|
||||
jscatalog = {}
|
||||
for message in catalog:
|
||||
if any(x[0].endswith(('.js', '.js_t', '.html'))
|
||||
for x in message.locations):
|
||||
msgid = message.id
|
||||
if isinstance(msgid, (list, tuple)):
|
||||
msgid = msgid[0]
|
||||
jscatalog[msgid] = message.string
|
||||
|
||||
with open(js_file, 'wt', encoding='utf8') as outfile:
|
||||
outfile.write('Documentation.addTranslations(')
|
||||
dump({
|
||||
'messages': jscatalog,
|
||||
'plural_expr': catalog.plural_expr,
|
||||
'locale': str(catalog.locale)
|
||||
}, outfile, sort_keys=True, indent=4)
|
||||
outfile.write(');')
|
||||
|
Loading…
Reference in New Issue
Block a user