diff options
author | Adam Turner <9087854+AA-Turner@users.noreply.github.com> | 2022-04-16 19:35:20 +0100 |
---|---|---|
committer | Adam Turner <9087854+AA-Turner@users.noreply.github.com> | 2022-04-16 20:01:03 +0100 |
commit | 1e443bdcb4fafd77c5ca3b91def00857900dceef (patch) | |
tree | 221038e8892a7b7669ddd9f92169891e2b2aea5c /utils | |
parent | 301c7bdf57eee47426c9ad4d96392bff623ee6c3 (diff) | |
download | sphinx-git-1e443bdcb4fafd77c5ca3b91def00857900dceef.tar.gz |
Move `compile_catalog_plusjs` to `utils/`
Diffstat (limited to 'utils')
-rw-r--r-- | utils/babel_runner.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/utils/babel_runner.py b/utils/babel_runner.py new file mode 100644 index 000000000..7cabeb47b --- /dev/null +++ b/utils/babel_runner.py @@ -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(');') + |