消息和目录¶
此模块提供一个基本接口来保存目录和消息信息。它通常用于修改gettext目录,但并未用于实际使用翻译。
目录¶
- class babel.messages.catalog.Catalog(locale: str | Locale | None = None, domain: str | None = None, header_comment: str | None = '# Translations template for PROJECT.\n# Copyright (C) YEAR ORGANIZATION\n# This file is distributed under the same license as the PROJECT project.\n# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n#', project: str | None = None, version: str | None = None, copyright_holder: str | None = None, msgid_bugs_address: str | None = None, creation_date: datetime | str | None = None, revision_date: datetime | time | float | str | None = None, last_translator: str | None = None, language_team: str | None = None, charset: str | None = None, fuzzy: bool = True)¶
消息目录的表示形式。
- add(id: _MessageID, string: _MessageID | None = None, locations: Iterable[tuple[str, int]] = (), flags: Iterable[str] = (), auto_comments: Iterable[str] = (), user_comments: Iterable[str] = (), previous_id: _MessageID = (), lineno: int | None = None, context: str | None = None) Message ¶
添加或更新具有指定ID的消息。
>>> catalog = Catalog() >>> catalog.add(u'foo') <Message ...> >>> catalog[u'foo'] <Message u'foo' (flags: [])>
此方法简单地构造了一个 Message object with the given arguments and invokes `_ _setitem__`与该对象。
- 参数:
id -- 消息ID,或一个
(singular, plural)
用于可多元化消息的元组string -- 翻译后的消息字符串,或
(singular, plural)
用于可多元化消息的元组locations -- 一系列
(filename, lineno)
元组flags -- 旗帜一组或一系列旗帜
auto_comments -- 一系列自动注释
user_comments -- 一系列用户评论
previous_id -- 上一条消息ID,或
(singular, plural)
用于可多元化消息的元组lineno -- 在PO文件中找到msgid行的行号(如果有
context -- 消息上下文
- check() Iterable[tuple[Message, list[TranslationError]]] ¶
对目录中的翻译运行各种验证检查。
对于每条未通过验证的消息,此方法都会生成一个
(message, errors)
元组,其中message
是 Message 对象和errors
是一系列 TranslationError 物体。- 返回类型:
generator
of(message, errors)
- delete(id: _MessageID, context: str | None = None) None ¶
删除具有指定ID和上下文的消息。
- 参数:
id -- 消息ID
context -- 消息上下文,或
None
在没有上下文的情况下
- get(id: _MessageID, context: str | None = None) Message | None ¶
返回具有指定ID和上下文的消息。
- 参数:
id -- 消息ID
context -- 消息上下文,或
None
在没有上下文的情况下
- property header_comment: str¶
目录的标题注释。
>>> catalog = Catalog(project='Foobar', version='1.0', ... copyright_holder='Foo Company') >>> print(catalog.header_comment) # Translations template for Foobar. # Copyright (C) ... Foo Company # This file is distributed under the same license as the Foobar project. # FIRST AUTHOR <EMAIL@ADDRESS>, .... #
也可以从字符串设置标头。再次检索标题时,任何已知的大写变量都将被替换:
>>> catalog = Catalog(project='Foobar', version='1.0', ... copyright_holder='Foo Company') >>> catalog.header_comment = '''\ ... # The POT for my really cool PROJECT project. ... # Copyright (C) 1990-2003 ORGANIZATION ... # This file is distributed under the same license as the PROJECT ... # project. ... #''' >>> print(catalog.header_comment) # The POT for my really cool Foobar project. # Copyright (C) 1990-2003 Foo Company # This file is distributed under the same license as the Foobar # project. #
- 类型:
unicode
- language_team¶
语言团队的名称和电子邮件地址。
- last_translator¶
最后一位翻译员的姓名和电子邮件地址。
- property mime_headers: list[tuple[str, str]]¶
目录的MIME标头,用于特殊
msgid ""
条目。根据是否设置了区域设置,此属性的行为略有不同,后者表明目录实际上是实际翻译的模板。
下面是这样一个目录模板的输出示例:
>>> from babel.dates import UTC >>> from datetime import datetime >>> created = datetime(1990, 4, 1, 15, 30, tzinfo=UTC) >>> catalog = Catalog(project='Foobar', version='1.0', ... creation_date=created) >>> for name, value in catalog.mime_headers: ... print('%s: %s' % (name, value)) Project-Id-Version: Foobar 1.0 Report-Msgid-Bugs-To: EMAIL@ADDRESS POT-Creation-Date: 1990-04-01 15:30+0000 PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE Last-Translator: FULL NAME <EMAIL@ADDRESS> Language-Team: LANGUAGE <LL@li.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Generated-By: Babel ...
下面是设置区域设置时的输出示例:
>>> revised = datetime(1990, 8, 3, 12, 0, tzinfo=UTC) >>> catalog = Catalog(locale='de_DE', project='Foobar', version='1.0', ... creation_date=created, revision_date=revised, ... last_translator='John Doe <jd@example.com>', ... language_team='de_DE <de@example.com>') >>> for name, value in catalog.mime_headers: ... print('%s: %s' % (name, value)) Project-Id-Version: Foobar 1.0 Report-Msgid-Bugs-To: EMAIL@ADDRESS POT-Creation-Date: 1990-04-01 15:30+0000 PO-Revision-Date: 1990-08-03 12:00+0000 Last-Translator: John Doe <jd@example.com> Language: de_DE Language-Team: de_DE <de@example.com> Plural-Forms: nplurals=2; plural=(n != 1); MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Generated-By: Babel ...
- 类型:
list
- property num_plurals: int¶
目录或区域设置使用的复数数。
>>> Catalog(locale='en').num_plurals 2 >>> Catalog(locale='ga').num_plurals 5
- 类型:
int
- property plural_expr: str¶
目录或区域设置使用的复数表达式。
>>> Catalog(locale='en').plural_expr '(n != 1)' >>> Catalog(locale='ga').plural_expr '(n==1 ? 0 : n==2 ? 1 : n>=3 && n<=6 ? 2 : n>=7 && n<=10 ? 3 : 4)' >>> Catalog(locale='ding').plural_expr # unknown locale '(n != 1)'
- 类型:
str
- property plural_forms: str¶
返回区域设置的复数形式声明。
>>> Catalog(locale='en').plural_forms 'nplurals=2; plural=(n != 1);' >>> Catalog(locale='pt_BR').plural_forms 'nplurals=2; plural=(n > 1);'
- 类型:
str
- update(template: Catalog, no_fuzzy_matching: bool = False, update_header_comment: bool = False, keep_user_comments: bool = True, update_creation_date: bool = True) None ¶
根据给定的模板目录更新目录。
>>> from babel.messages import Catalog >>> template = Catalog() >>> template.add('green', locations=[('main.py', 99)]) <Message ...> >>> template.add('blue', locations=[('main.py', 100)]) <Message ...> >>> template.add(('salad', 'salads'), locations=[('util.py', 42)]) <Message ...> >>> catalog = Catalog(locale='de_DE') >>> catalog.add('blue', u'blau', locations=[('main.py', 98)]) <Message ...> >>> catalog.add('head', u'Kopf', locations=[('util.py', 33)]) <Message ...> >>> catalog.add(('salad', 'salads'), (u'Salat', u'Salate'), ... locations=[('util.py', 38)]) <Message ...>
>>> catalog.update(template) >>> len(catalog) 3
>>> msg1 = catalog['green'] >>> msg1.string >>> msg1.locations [('main.py', 99)]
>>> msg2 = catalog['blue'] >>> msg2.string u'blau' >>> msg2.locations [('main.py', 100)]
>>> msg3 = catalog['salad'] >>> msg3.string (u'Salat', u'Salate') >>> msg3.locations [('util.py', 42)]
目录中但不在模板中的消息将从主集合中移除,但仍可以通过 obsolete 成员:
>>> 'head' in catalog False >>> list(catalog.obsolete.values()) [<Message 'head' (flags: [])>]
- 参数:
template -- 参考目录,通常从POT文件中读取
no_fuzzy_matching -- 是否使用消息ID的模糊匹配
信息¶
- class babel.messages.catalog.Message(id: _MessageID, string: _MessageID | None = '', locations: Iterable[tuple[str, int]] = (), flags: Iterable[str] = (), auto_comments: Iterable[str] = (), user_comments: Iterable[str] = (), previous_id: _MessageID = (), lineno: int | None = None, context: str | None = None)¶
目录中单个消息的表示形式。
- check(catalog: Catalog | None = None) list[TranslationError] ¶
对邮件运行各种验证检查。只有在提供目录的情况下才会执行某些验证。此方法返回 TranslationError 物体。
- 返回类型:
iterator
- 参数:
catalog -- 传递给检查器的目录实例
- 看见:
Catalog.check 获取对目录中的所有邮件执行检查的方法。
- property fuzzy: bool¶
翻译是否模糊。
>>> Message('foo').fuzzy False >>> msg = Message('foo', 'foo', flags=['fuzzy']) >>> msg.fuzzy True >>> msg <Message 'foo' (flags: ['fuzzy'])>
- 类型:
bool
例外情况¶
- exception babel.messages.catalog.TranslationError¶
遇到无效的消息转换时由转换检查器引发的异常。