低级提取接口

可以使用低级提取接口直接从目录或文件中提取。通常这是不需要的,因为命令行工具可以为您做到这一点。

抽取函数

提取函数是命令行工具内部用于提取字符串的函数。

babel.messages.extract.extract_from_dir(dirname: str | os.PathLike[str] | None = None, method_map: Iterable[tuple[str, str]] = [('**.py', 'python')], options_map: SupportsItems[str, dict[str, Any]] | None = None, keywords: Mapping[str, _Keyword] = {'N_': None, '_': None, 'dgettext': (2,), 'dngettext': (2, 3), 'gettext': None, 'ngettext': (1, 2), 'npgettext': ((1, 'c'), 2, 3), 'pgettext': ((1, 'c'), 2), 'ugettext': None, 'ungettext': (1, 2)}, comment_tags: Collection[str] = (), callback: Callable[[str, str, dict[str, Any]], object] | None = None, strip_comment_tags: bool = False, directory_filter: Callable[[str], bool] | None = None) Generator[_FileExtractionResult, None, None]

从给定目录中找到的任何源文件中提取邮件。

此函数生成表单的元组 (filename, lineno, message, comments, context) .

对于每个文件使用哪种解压缩方法由 method_map 参数,该参数将扩展全局模式映射到提取方法名称。例如,以下是默认映射:

>>> method_map = [
...     ('**.py', 'python')
... ]

这基本上是说,目录内任何级别的文件扩展名为“.py”的文件都应该由“python”提取方法处理。与任何映射模式都不匹配的文件将被忽略。请参阅 pathmatch 函数获取有关模式语法的详细信息。

以下扩展映射还将对“Templates”子目录中的任何文件使用“genshi”提取方法:

>>> method_map = [
...     ('**/templates/**.*', 'genshi'),
...     ('**.py', 'python')
... ]

由可选的 options_map 参数增加了这些映射。它使用扩展全局模式作为键,值是将选项名称映射到选项值(两个字符串)的字典。

的球状图案 options_map 不一定需要与方法映射中使用的相同。例如,虽然 templates 应用程序中的文件夹可能是Genshi应用程序,这些文件的选项可能会根据扩展名的不同而有所不同:

>>> options_map = {
...     '**/templates/**.txt': {
...         'template_class': 'genshi.template:TextTemplate',
...         'encoding': 'latin-1'
...     },
...     '**/templates/**.html': {
...         'include_attrs': ''
...     }
... }
参数:
  • dirname -- 要从中提取邮件的目录的路径。如果没有给定,则使用当前工作目录。

  • method_map -- 一览表 (pattern, method) 将提取方法名称映射到扩展全局模式的元组

  • options_map -- 附加选项词典(可选)

  • keywords -- 将关键字(即应识别为翻译函数的函数名称)映射到指定其参数中包含可本地化字符串的元组的字典

  • comment_tags -- 要搜索并包含在结果中的翻译者注释的标记列表

  • callback -- 为从中提取消息的每个文件调用的函数,恰好在执行提取本身之前;按顺序将文件名、提取方法的名称和选项字典作为位置参数传递给该函数

  • strip_comment_tags -- 如果设置为 True 使所有注释标签从收集的注释中删除。

  • directory_filter -- 用于确定是否应将目录递归到其中的回调。接收完整的目录路径;如果目录有效,则应返回True。

看见:

pathmatch

babel.messages.extract.extract_from_file(method: _ExtractionMethod, filename: str | os.PathLike[str], keywords: Mapping[str, _Keyword] = {'N_': None, '_': None, 'dgettext': (2,), 'dngettext': (2, 3), 'gettext': None, 'ngettext': (1, 2), 'npgettext': ((1, 'c'), 2, 3), 'pgettext': ((1, 'c'), 2), 'ugettext': None, 'ungettext': (1, 2)}, comment_tags: Collection[str] = (), options: Mapping[str, Any] | None = None, strip_comment_tags: bool = False) list[_ExtractionResult]

从特定文件提取邮件。

此函数返回以下形式的元组列表 (lineno, message, comments, context) .

参数:
  • filename -- 要从中提取消息的文件的路径

  • method -- 指定提取方法的字符串(例如“ Python ”)

  • keywords -- 将关键字(即应识别为翻译函数的函数名称)映射到指定其参数中包含可本地化字符串的元组的字典

  • comment_tags -- 要搜索并包含在结果中的翻译器标记列表

  • strip_comment_tags -- 如果设置为 True 使所有注释标签从收集的注释中删除。

  • options -- 附加选项词典(可选)

返回:

表单的元组列表 (lineno, message, comments, context)

返回类型:

list[tuple[int, str|tuple[str], list[str], str|None]

babel.messages.extract.extract(method: _ExtractionMethod, fileobj: _FileObj, keywords: Mapping[str, _Keyword] = {'N_': None, '_': None, 'dgettext': (2,), 'dngettext': (2, 3), 'gettext': None, 'ngettext': (1, 2), 'npgettext': ((1, 'c'), 2, 3), 'pgettext': ((1, 'c'), 2), 'ugettext': None, 'ungettext': (1, 2)}, comment_tags: Collection[str] = (), options: Mapping[str, Any] | None = None, strip_comment_tags: bool = False) Generator[_ExtractionResult, None, None]

使用指定的提取方法从给定的类似文件的对象中提取消息。

此函数返回表单的元组 (lineno, message, comments, context) .

属性的值将实际提取分派给插件。 method 参数。

>>> source = b'''# foo module
... def run(argv):
...    print(_('Hello, world!'))
... '''
>>> from io import BytesIO
>>> for message in extract('python', BytesIO(source)):
...     print(message)
(3, u'Hello, world!', [], None)
参数:
  • method -- 提取方法(可调用)或指定提取方法的字符串(例如,“python”);如果这是一个简单的名称,将按入口点查找提取函数;如果它是对函数的显式引用(格式为 package.module:funcnamepackage.module.funcname ),则会导入并使用相应的功能

  • fileobj -- 应从中提取消息的类似文件的对象

  • keywords -- 将关键字(即应识别为翻译函数的函数名称)映射到指定其参数中包含可本地化字符串的元组的字典

  • comment_tags -- 要搜索并包含在结果中的翻译器标记列表

  • options -- 附加选项词典(可选)

  • strip_comment_tags -- 如果设置为 True 使所有注释标签从收集的注释中删除。

抛出:

ValueError -- 如果未注册提取方法

返回:

表单的元组的可迭代 (lineno, message, comments, context)

返回类型:

Iterable[tuple[int, str|tuple[str], list[str], str|None]

语言分析

语言解析函数用于从源文件中提取字符串。提取函数会自动使用这些函数,但有时注册包装器函数会很有用,然后就可以调用这些低级函数。

可以通过setuptools入口点系统注册新功能。

babel.messages.extract.extract_python(fileobj: IO[bytes], keywords: Mapping[str, _Keyword], comment_tags: Collection[str], options: _PyOptions) Generator[_ExtractionResult, None, None]

从Python源代码中提取消息。

它返回一个迭代器,该迭代器生成以下形式的元组 (lineno, funcname, message, comments) .

参数:
  • fileobj -- 应从中提取消息的可查找的类似文件的对象

  • keywords -- 应识别为翻译函数的关键字(即函数名称)列表

  • comment_tags -- 要搜索并包含在结果中的翻译器标记列表

  • options -- 附加选项词典(可选)

返回类型:

iterator

babel.messages.extract.extract_javascript(fileobj: _FileObj, keywords: Mapping[str, _Keyword], comment_tags: Collection[str], options: _JSOptions, lineno: int = 1) Generator[_ExtractionResult, None, None]

从JavaScript源代码中提取消息。

参数:
  • fileobj -- 应从中提取消息的可查找的类似文件的对象

  • keywords -- 应识别为翻译函数的关键字(即函数名称)列表

  • comment_tags -- 要搜索并包含在结果中的翻译器标记列表

  • options -- 附加选项词典(可选)支持的选项包括: * jsx -- set to false to disable JSX/E4X support. * template_string --如果 True ,支持getText (key )* parse_template_string --如果 True 将解析javascript模板字符串的内容。

  • lineno -- 行号偏移量(用于解析嵌入的片段)

babel.messages.extract.extract_nothing(fileobj: _FileObj, keywords: Mapping[str, _Keyword], comment_tags: Collection[str], options: Mapping[str, Any]) list[_ExtractionResult]

不实际提取任何内容,而只是返回一个空列表的伪提取器。