Zoxume
Zoxume

Reputation: 223

Build translated Sphinx docs in separate directories

I work on a documentation that will be published in different languages. It is one of the reasons I use Sphinx.

I know how generate the translated version but with the setting described in the documentation, the resulting files replaces the ones that were generated before. Thus, when generating multiple translation, I have to move the files to another directory before doing anything else. It would be more practical (and easier to deploy) to generate the translations in separate directories.

Is there a way to tell Sphinx or the makefile that when I run

make -e SPHINXOPTS="-D language='(lang)'" (format)

the files have to be generated in /build/(format)/(lang) ?

For now, only the HTML build is used (and I doubt that something else will be used) so a specific solution would be accepted if it is not possible to do it globally.

Sphinx version is 1.4.6.

Upvotes: 4

Views: 822

Answers (1)

Zoxume
Zoxume

Reputation: 223

I found a working solution by replacing the Makefile by a custom Python script (build.py).

Using sys.argv, I emulate the make target behaviour. I added several options for the language. Using the subprocess module, precisely its call() function, I am able to run commands with a set of options. The script is based on a function that generates the command to be executed by subprocess.call():

def build_command(target, build_dir, lang=None):
    lang_opt = []
    if lang:
        lang_opt = ["-D", "language='" + lang + "'"]
        build_dir += "/" + lang
    else:
        build_dir += "/default"

    return ["sphinx-build", "-b", target, "-aE"] + lang_opt + ["source", "build/" + build_dir]

It is the lang parameter that allows me to separate each language, independently of the target. Later in the code, I just run

subprocess.call(build_command(target, target, lang))

To build the documentation in the desired language with the specified target (usually, target = "html"). It can also emulate make gettext:

subprocess.call(build_command("gettext", "locale"))

And so on...

A better solution may exist, but at least this one will do the job.

Upvotes: 4

Related Questions