python - setup.py 中 Babel 的 message_extractors 配置 : Third parameter in tuple

标签 python gettext python-babel

我想开始使用 Babel和我的 Python 项目。我有一个 setup.py,根据文档,其中包含以下内容:

setup(
    ...
    message_extractors = {'my_project': [('**.py', 'lingua_python', None),
    ]},
    ...

一切正常。现在我想知道第三个参数(设置为 None )意味着什么以及我可以传入什么(只是为了了解它)。我看了Babel documentation但找不到任何东西,但我在 Mako documentation 上看到了这个对于巴别塔:

# Extraction from Python source files

[python: myproj/**.py]

# Extraction from Mako templates

[mako: myproj/templates/**.html]
input_encoding = utf-8

我想知道:这个 input_encoding 选项似乎可以传递到那里。由于我在所有 Python、我的模板等中使用 Unicode 和 UTF-8。我想这可能是一个要指定的有效选项(也许这是一个隐含的假设,我不知道)。

所以现在我正在寻找一些记录这种行为的东西,但即使是我找到的 Babel 文档的一部分也没有说明第三个参数是什么。

请通过分享一些详细解释这一点的有用链接来启发我。预先感谢您。

更新:我发现最后一个参数似乎是选项的dict,可能传递给扩展(例如mako )。当我将最后一个参数从 None 设置为 {'input_encoding': 'utf-8'} 时,mako.exceptions.CompileException 提示 编码'ascii'的Unicode解码操作失败消失。因此,虽然这似乎是答案,但我仍在寻找有关此问题的文档。

最佳答案

第三个参数是选项映射。

这可以包含将传递给提取器方法的任意关键字参数。这完全取决于提取器方法它将接受哪些关键字参数。

提取器在调用时将这些选项作为最后一个参数传递,通常在方法签名中称为optionsMako Babel plugin例如,支持 encodinginput_encoding。前者似乎是后者参数的遗留拼写,仅在未指定 input_encoding 时使用。

因此,每个 Babel 插件都需要单独记录支持的选项,Babel 项目没有设置限制,也不会知道每个插件支持的具体细节。

Mako 提取器的具体信息记录在 Common Framework Integrations section 中:

The Mako extractor supports an optional input_encoding parameter specifying the encoding of the templates (identical to Template/TemplateLookup‘s input_encoding parameter).

关于python - setup.py 中 Babel 的 message_extractors 配置 : Third parameter in tuple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15257933/

相关文章:

python - 使用报纸模块的 Ubuntu 脚本导入错误

python - 使用带过滤器的 Pandas 写入 csv

delphi - 翻译 const delphi 7 gnugettext

html - 如何防止使用 gettext() 翻译注释

variables - 我可以在 .po 文件中包含变量吗?

python - 如何为 flask 应用程序设置空白默认语言环境?

python - 如何根据条件从另一个数据帧更新数据帧值

python - 是否可以在 python 中加载和显示大(8296 x 14740 像素)图像?

python - Flask-Babel 不会翻译 Web 项目中的任何内容

python - Babel date_format 返回 2017 年的 2016 年日期