但愿在readthedocs上建立支持多语言的文档,效果相似:
html
经过语言选项,能够切到到不一样的语言版本;实现这个目标包含两个主要步骤:python
- 在本地对文档进行翻译
- 在
readthedocs.org
上配置翻译
本文假设您已经对sphinx
文档生成工具和readthedocs.org
文档托管网站有所了解,本文主要专一于多语言的配置上。git
在本地对文档进行翻译
翻译以前须要安装一些软件包:github
- sphinx: 文档生成工具
- sphinx_intl: 多语言工具
- recommonmark: sphinx支持markdown的插件
- sphinx_rtd_theme: sphinx的readthedocs主题插件
安装命令:web
pip install sphinx sphinx_intl recommonmark sphinx_rtd_theme
咱们如今有一个项目了,而且其文档是英文的,而且英文文档已经部署到readthedocs网站上了;以deeptables为例,其.readthedocs.yml
文件内容为:bash
version: 2 sphinx: configuration: docs/source/conf.py formats: all python: version: 3.6 install: - requirements: docs/requirements.txt
docs/source/conf.py
的内容为:markdown
import os, sys sys.path.insert(0, os.path.abspath('..')) project = 'DeepTables' copyright = '2020, Zetyun.com' author = 'Zetyun.com' # The full version, including alpha/beta/rc tags release = '0.1.1' extensions = ['recommonmark', 'sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.viewcode' # 'sphinx.ext.autodoc', # 'sphinx.ext.mathjax', # 'sphinx.ext.ifconfig', # 'sphinx.ext.viewcode', # 'sphinx.ext.githubpages', ] exclude_patterns = [] #html_theme = 'alabaster' html_theme = 'sphinx_rtd_theme' pygments_style = 'sphinx' templates_path = ['_templates'] source_suffix = ['.rst', '.md'] master_doc = 'index' html_static_path = ['_static'] language = 'en' # ['en', 'zh_CN'] # # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'deeptables', 'DeepTables Documentation', [author], 1) ] texinfo_documents = [ (master_doc, 'DeepTables', 'DeepTables Documentation', author, 'DeepTables', 'One line description of project.', 'Miscellaneous'), ]
源码精简后的目录的结构:工具
├── deeptables │ ├── __init__.py ├── docs │ ├── Makefile │ ├── build │ ├── make.bat │ ├── requirements.txt │ └── source │ ├── conf.py │ ├── index.rst ├── examples ├── requirements.txt ├── setup.cfg ├── setup.py └── tests
文档访问地址:
http://deeptables.readthedocs.io/
其中docs目录是其文档目录。
开始操做:
网站
- 建立一个新项目
deeptables-docs-zh_CN
,并把原来项目的docs复制过来
➜ mkdir deeptables-docs-zh_CN ➜ cp -r deeptables/docs deeptables-docs-zh_CN ➜ cp deeptables/.readthedocs.yml deeptables-docs-zh_CN
- 配置新项目中的
conf.py
language = 'zh_CN' # 设置新项目的语言与中文 locale_dirs = ['locale/'] # 设置本地化数据目录
- 而后在source目录执行命令生成pot文件
➜ cd source ➜ sphinx-build -b gettext . locale 正在运行 Sphinx v3.0.0 正在加载翻译 [zh_CN]... 完成 建立输出目录... 完成 ...
若是报错找不到项目里的模块,能够把本身的项目加入到PYTHONPATH中:ui
export PYTHONPATH=/path/to/module
正常状况下会生成locale目录,里面有不少pot文件:
➜ tree locale locale ├── deeptables.datasets.pot ├── deeptables.eda.pot ├── deeptables.ensemble.pot ├── deeptables.fe.pot
而后生成咱们须要编辑的po文件:
➜ sphinx-intl update -p locale -l zh_CN Create: locale/zh_CN/LC_MESSAGES/model_config.po Create: locale/zh_CN/LC_MESSAGES/deeptables.preprocessing.po Create: locale/zh_CN/LC_MESSAGES/faq.po
打开index.po文件进行翻译:
# SOME DESCRIPTIVE TITLE. # Copyright (C) 2020, Zetyun.com # This file is distributed under the same license as the DeepTables package. # FIRST AUTHOR <EMAIL@ADDRESS>, 2020. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: DeepTables \n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2020-04-13 17:23+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.8.0\n" #: ../index.rst:62 msgid "Quick-Start" msgstr "快速开始"
其中msgid是咱们要翻译的内容,msgstr是翻译结果,好比把Quick-Start
翻译成快速开始
:
#: ../index.rst:62 msgid "Quick-Start" msgstr "快速开始"
其中po,pot,mo文件的关系如图:
为了给咱们生成能够人工编写的po文件,须要先生成pot文件,pot文件能够从rst/markdown文件生成。
生成的po文件能够格式化成mo文件,最终再结合最开始rst/markdown文件生成翻译后的html。
因此readthedocs进行构建生成html时仅须要rst/md和po文件,其余的文件咱们能够经过gitignore忽略上传。
- 构建中文文档
在docs目录执行make html:
➜ make clean ➜ make html 正在运行 Sphinx v3.0.0 正在加载翻译 [zh_CN]... 完成 建立输出目录... 完成 WARNING: html_static_path 指向的 '_static' 不存在 构建 [mo]: 1 个 po 文件的目标文件已过时 写入输出... [100%] locale/zh_CN/LC_MESSAGES/index.mo ... 复制静态文件... ... 完成 copying extra files... 完成 dumping search index in Chinese (code: zh)... 完成 dumping object inventory... 完成 build 成功, 111 warnings
而后咱们在生成的html目录查看启动web服务查看效果:
(deeptables) ➜ docs cd build/html (deeptables) ➜ html python3 -m http.server 8000
访问http://localhost:8000/quick_start.html查看效果。
以上步骤能够参考sphinx官方文档:
http://www.sphinx-doc.org/en/master/usage/advanced/intl.html
- 将
deeptables-docs-zh_CN
放到git中维护,方便后面发布到rtd网站上。
在readthedocs.org
上配置翻译
通过上一章的配置,咱们应该拥有两个不一样语言的项目文档,接着咱们把这两个文档整合到一个域名上。
readthedocs.org
支持多语言的方法是配置多个项目。
须要先在rtd在建立一个项目deeptables-docs-zh_CN
,rtd的项目列表以下:
而后配置新项目的的语言为中文:
其git地址等其余信息请妥善配置。
此处请注意,在conf.py中配置lnguage=zh_CN
没有用的,须要在页面上进行配置。接着能够构建一下项目,验证文档是不是中文的了:
ttps://deeptables.readthedocs.io/zh_CN/latest/
如图:
接着就能够配置咱们的主文档项目关联这个翻译文档了。
在主文档deeptables项目的设置中找到翻译选项,而后把项目加入deeptables-docs-zh_CN
:
最后返回主文档https://deeptables.readthedocs.io/zh_CN/latest/就能够选择语言了。