【问题标题】:How to translate comments in code blocks using Sphinx如何使用 Sphinx 翻译代码块中的注释
【发布时间】:2020-04-16 10:37:38
【问题描述】:

我使用Sphinx 为一个项目编写文档。我有代码示例(定义为code-blocks,包括字面意思和内联类型)。在这些代码示例中有cmets,但是当我制作文档翻译时,它们没有被提取到.po文件中,显然没有被翻译。

如何翻译代码示例中的 cmets?

我发现了有关 Sphinx 修改的其他问题。答案建议修改conf.py(制作一些钩子),创建roles 或extensions。我以前从未这样做过,我不知道从哪里开始,什么解决方案会更好。这个问题有现成的解决方案吗?

UPD。这些是我想在我的文档中展示的代码示例:

git clone https://github.com/ynikitenko/lena
# most of requirements are for development only
pip install -r lena/requirements.txt

(这里我想翻译评论)。一个更困难(也许不需要)的例子是:

class End(object):
    """Stop sequence here."""

    def run(self, flow):
        """Exhaust all preceding flow and stop iteration
        (yield nothing to the following flow).
        """
        for val in flow:
            pass
        return
        # otherwise it won't be a generator
        yield "unreachable"

这些示例使用指令格式化

.. code-block:: 

【问题讨论】:

  • 你能用一个例子澄清你的意思吗?喜欢 cmets 长什么样,想看什么样的代码示例。
  • @zkytony 当然。例如,这是一个内联注释lena.readthedocs.io/en/latest/#from-pypi 我想翻译“# if you plan to render LaTeX templates”。更大的代码示例可能在这里,带有完整的文档字符串(但这种情况可能不太重要):lena.readthedocs.io/en/latest/tutorial/answers.html#ex-1
  • @zkytony 我在问题中添加了这个,谢谢你的提问。

标签: internationalization python-sphinx


【解决方案1】:

我写信给官方sphinx-users google 群组,这是来自 Documatt 的 Matt 的the answer

这是不可能的。 Sphinx 必须理解每种语言的 cmets。

如果您想翻译代码块中的 cmets(以及 :: 之后的文字块),您必须将它们全部翻译。将 gettext_additional_targets = ["literal-block"] 添加到您的 conf.py 并重新运行 POT/PO 更新。

代码行保留在代码的“翻译”中,但现在问题已经为我解决了。

【讨论】:

  • 也许我应该补充一点,我摆脱了原始问题中显示的 cmets。应该避免它们,并且大部分代码不必翻译。但有时它很有用。
猜你喜欢
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2019-07-03
相关资源
最近更新 更多