使用 Sphinx 翻譯文件

Sphinx 是用來建立優美文件的工具。它使用了簡單的 reStructuredText 語法,並以多種格式輸出。如果要找個範例,那麼本文件也是用它來建置的。使用 Sphinx 的非常有用的夥伴是 Read the Docs 服務,它會免費建置並發布您的文件。

我不會聚焦在撰寫文件本身,如果您需要指導的話,只需要按照 Sphinx 網站上的指示即可。一旦準備好文件,將其翻譯是非常容易地,因為 Sphinx 帶有對此的支援,並且在其 Internationalization 中覆蓋得很好。這只不過是一些組態指令並呼叫 sphinx-intl 工具的事。

如果使用 Read the Docs 服務,那麼可以在 Read the Docs 上開始建置翻譯的文件。它們的 Localization and Internationalization 幾乎覆蓋了您需要的每一件事——建立另一個專案,設定其語言以及從主專案上連結它作為翻譯。

Now all you need is translating the documentation content. Sphinx generates PO file for each directory or top level file, what can lead to quite a lot of files to translate (depending on gettext_compact settings). You can import the index.po into Weblate as an initial component and then configure 組件探索 add-on to automatically discover all others.

組件組態

組件名稱

Documentation

檔案遮罩

docs/locales/*/LC_MESSAGES/index.po

新翻譯的模板

docs/locales/index.pot

檔案格式

gettext PO file

翻譯旗標

rst-text

Component discovery configuration

用來比對出翻譯檔的正則表達式

docs/locales/(?P<language>[^/.]*)/LC_MESSAGES/(?P<component>[^/]*)\.po

自訂組件名稱

文件:{{ component|title }}

定義新翻譯的基礎檔

docs/locales/{{ component }}.pot

提示

您希望 Sphinx 產生只是單一的 PO 文件嗎?從 Sphinx 3.3.0 開始可以實現,使用:

gettext_compact = "docs"

使用這種方法可以發現正在翻譯的幾個文件項目: