未记录的事实是狮身人面像不会这样做。事实上,sphinx i18n 功能停止了构建所有特定语言目录的工作流程。这意味着您必须使用自己的脚本设计和包装 sphinx-build 才能进行设置。
要将您的 sphinx reST 文件翻译成另一种语言,您不必自己更新您的“.rst”文件。 Sphinx 已经了解文本块的样子,它可以将标题字符串、标题、双换行符分隔的段落等划分为唯一的“源字符串”(msgid),并将它们放入“.pot”源-语言文件和“.po”目标语言文件。
首先,从“docs/”目录运行make gettext。这告诉 sphinx 解析你的 reST 文件并自动找到一堆要翻译的字符串并给它们一个唯一的msgid。
user@host:~/rtd-github-pages$ cd docs/
user@host:~/rtd-github-pages/docs$ ls
autodoc.rst buildDocs.sh conf.py.orig locales Makefile
_build conf.py index.rst make.bat _static
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ make gettext
Running Sphinx v1.8.4
making output directory...
building [gettext]: targets for 0 template files
building [gettext]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Hello Worldrces... [ 50%] autodoc
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
writing message catalogs... [100%] index
build succeeded.
The message catalogs are in _build/gettext.
user@host:~/rtd-github-pages/docs$
上面的执行应该会创建以下文件
user@host:~/rtd-github-pages/docs$ ls _build/gettext/
autodoc.pot index.pot
user@host:~/rtd-github-pages/docs$
这是来自 '_build/gettext/index.pot' 的 sn-p,在我们的文档主页上显示了两个字符串,我们将把它们从英语翻译成西班牙语。
user@host:~/rtd-github-pages/docs$ grep -m2 -A2 .rst _build/gettext/index.pot
#: ../../index.rst:7
msgid "Welcome to helloWorld's documentation!"
msgstr ""
--
#: ../../index.rst:9
msgid "Contents:"
msgstr ""
user@host:~/rtd-github-pages/docs$
接下来,让我们告诉 sphinx 从上面生成的源语言“.pot”文件中准备一些西班牙语目标语言“.po”文件。
在继续此步骤之前,您需要安装 sphinx-intl 和 python Stemmer 模块。如果您使用的是基于 Debian 的发行版,则可以使用以下命令进行操作。
sudo apt-get install -y sphinx-intl python3-stemmer
执行以下命令来准备我们的西班牙语特定翻译文件。
user@host:~/rtd-github-pages/docs$ sphinx-intl update -p _build/gettext -l es
Create: locales/es/LC_MESSAGES/index.po
Create: locales/es/LC_MESSAGES/autodoc.po
user@host:~/rtd-github-pages/docs$
上述执行创建了两个“.po”文件:每个“.pot”源语言文件一个,它与我们的两个“.rst”文件(index.rst 和 autodoc.rst)中的每一个直接相关.完美。
如果我们 grep 新的西班牙语特定的“docs/locales/es/LC_MESSAGES/index.po”文件,我们会看到它与源“.pot”文件的内容相同。
user@host:~/rtd-github-pages/docs$ grep -m2 -A2 .rst locales/es/LC_MESSAGES/index.po
#: ../../index.rst:7
msgid "Welcome to helloWorld's documentation!"
msgstr ""
--
#: ../../index.rst:9
msgid "Contents:"
msgstr ""
user@host:~/rtd-github-pages/docs$
这些特定于语言的“.po”文件是我们实际进行翻译的地方。如果您是一个大型项目,那么您可能需要使用特殊程序或service 来翻译这些文件。但是,为了清楚起见,我们将直接编辑文件。
user@host:~/rtd-github-pages/docs$ perl -pi -0e "s^(msgid \"Welcome to helloWorld's documentation\!\"\n)msgstr \"\"^\1msgstr \"¡Bienvenido a la documentación de helloWorld\!\"^" locales/es/LC_MESSAGES/index.po
user@host:~/rtd-github-pages/docs$ perl -pi -0e "s^(msgid \"Contents:\"\n)msgstr \"\"^\1msgstr \"Contenidos:\"^" locales/es/LC_MESSAGES/index.po
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ grep -m2 -A2 .rst locales/es/LC_MESSAGES/index.po
#: ../../index.rst:7
msgid "Welcome to helloWorld's documentation!"
msgstr "¡Bienvenido a la documentación de helloWorld!"
--
#: ../../index.rst:9
msgid "Contents:"
msgstr "Contenidos"
user@host:~/rtd-github-pages/docs$
如您所见,上面的执行用原始(英文)语言的对应msgid行的西班牙语翻译填充了msgstr ""的内容。
现在让我们构建两个版本的 html 静态内容:[1] 英文版和 [2] 西班牙文版。
user@host:~/rtd-github-pages/docs$ sphinx-build -b html . _build/html/en -D language='en'
Running Sphinx v1.8.4
loading translations [en]... done
making output directory...
building [mo]: targets for 0 po files that are out of date
building : targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Hello Worldrces... [ 50%] autodoc
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex py-modindex
highlighting module code... [100%] helloWorld
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded.
The HTML pages are in _build/html/en.
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ sphinx-build -b html . _build/html/es -D language='es'
Running Sphinx v1.8.4
loading translations [es]... done
making output directory...
building [mo]: targets for 1 po files that are out of date
writing output... [100%] locales/es/LC_MESSAGES/index.mo
building : targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Hello Worldrces... [ 50%] autodoc
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex py-modindex
highlighting module code... [100%] helloWorld
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in Spanish (code: es) ... done
dumping object inventory... done
build succeeded.
The HTML pages are in _build/html/es.
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ firefox _build/html/en/index.html _build/html/es/index.html &
[1] 12134
user@host:~/rtd-github-pages/docs$
上述执行中的firefox 命令应该会打开带有两个选项卡的浏览器:(1) 英文版和 (2) 西班牙文版。
有关更多信息,请参阅how to setup internationalization in Sphinx 上的这篇文章。