class documentation

class Catalog(object):

View In Hierarchy

Representation of a message catalog.
Method __iter__ Iterates through all the entries in the catalog, in the order they were added, yielding a Message object for every entry.
Method add Add or update the message with the specified ID.
Method check Run various validation checks on the translations in the catalog.
Method delete Delete the message with the specified ID and context.
Method get Return the message with the specified ID and context.
Method update Update the catalog based on the given template catalog.
Class Variable mime​_headers Undocumented
Instance Variable header​_comment Undocumented
Instance Variable language​_team Name and email address of the language team.
Instance Variable last​_translator Name and email address of the last translator.
Method __contains__ Return whether the catalog has a message with the specified ID.
Method __delitem__ Delete the message with the specified ID.
Method __getitem__ Return the message with the specified ID.
Method __init__ Initialize the catalog object.
Method __len__ The number of messages in the catalog.
Method __repr__ Undocumented
Method __setitem__ Add or update the message with the specified ID.
Method ​_get​_header​_comment Undocumented
Method ​_get​_locale Undocumented
Method ​_get​_locale​_identifier Undocumented
Method ​_get​_mime​_headers Undocumented
Method ​_key​_for The key for a message is just the singular ID even for pluralizable messages, but is a (msgid, msgctxt) tuple for context-specific messages.
Method ​_set​_header​_comment Undocumented
Method ​_set​_locale Undocumented
Method ​_set​_mime​_headers Undocumented
Class Variable locale​_identifier Undocumented
Instance Variable ​_header​_comment Undocumented
Instance Variable ​_locale Undocumented
Instance Variable ​_locale​_identifier Undocumented
Instance Variable ​_messages Undocumented
Instance Variable ​_num​_plurals Undocumented
Instance Variable ​_plural​_expr Undocumented
Instance Variable charset Undocumented
Instance Variable copyright​_holder Undocumented
Instance Variable creation​_date Undocumented
Instance Variable domain Undocumented
Instance Variable fuzzy Undocumented
Instance Variable locale Undocumented
Instance Variable msgid​_bugs​_address Undocumented
Instance Variable obsolete Undocumented
Instance Variable project Undocumented
Instance Variable revision​_date Undocumented
Instance Variable version Undocumented
Property num​_plurals The number of plurals used by the catalog or locale.
Property plural​_expr The plural expression used by the catalog or locale.
Property plural​_forms Return the plural forms declaration for the locale.
def __iter__(self):
Iterates through all the entries in the catalog, in the order they were added, yielding a Message object for every entry.
Returns
iteratorUndocumented
def add(self, id, string=None, locations=(), flags=(), auto_comments=(), user_comments=(), previous_id=(), lineno=None, context=None):

Add or update the message with the specified ID.

>>> catalog = Catalog()
>>> catalog.add(u'foo')
<Message ...>
>>> catalog[u'foo']
<Message u'foo' (flags: [])>

This method simply constructs a Message object with the given arguments and invokes __setitem__ with that object.

Parameters
idthe message ID, or a (singular, plural) tuple for pluralizable messages
stringthe translated message string, or a (singular, plural) tuple for pluralizable messages
locationsa sequence of (filename, lineno) tuples
flagsa set or sequence of flags
auto​_commentsa sequence of automatic comments
user​_commentsa sequence of user comments
previous​_idthe previous message ID, or a (singular, plural) tuple for pluralizable messages
linenothe line number on which the msgid line was found in the PO file, if any
contextthe message context
def check(self):

Run various validation checks on the translations in the catalog.

For every message which fails validation, this method yield a (message, errors) tuple, where message is the Message object and errors is a sequence of TranslationError objects.

Returns
iteratorUndocumented
def delete(self, id, context=None):
Delete the message with the specified ID and context.
Parameters
idthe message ID
contextthe message context, or None for no context
def get(self, id, context=None):
Return the message with the specified ID and context.
Parameters
idthe message ID
contextthe message context, or None for no context
def update(self, template, no_fuzzy_matching=False, update_header_comment=False, keep_user_comments=True):

Update the catalog based on the given template catalog.

>>> 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)]

Messages that are in the catalog but not in the template are removed from the main collection, but can still be accessed via the obsolete member:

>>> 'head' in catalog
False
>>> list(catalog.obsolete.values())
[<Message 'head' (flags: [])>]
Parameters
templatethe reference catalog, usually read from a POT file
no​_fuzzy​_matchingwhether to use fuzzy matching of message IDs
update​_header​_commentUndocumented
keep​_user​_commentsUndocumented
mime_headers =

Undocumented

header_comment =

Undocumented

language_team =
Name and email address of the language team.
last_translator =
Name and email address of the last translator.
def __contains__(self, id):
Return whether the catalog has a message with the specified ID.
def __delitem__(self, id):
Delete the message with the specified ID.
def __getitem__(self, id):
Return the message with the specified ID.
Parameters
idthe message ID
def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER, project=None, version=None, copyright_holder=None, msgid_bugs_address=None, creation_date=None, revision_date=None, last_translator=None, language_team=None, charset=None, fuzzy=True):
Initialize the catalog object.
Parameters
localethe locale identifier or Locale object, or None if the catalog is not bound to a locale (which basically means it's a template)
domainthe message domain
header​_commentthe header comment as string, or None for the default header
projectthe project's name
versionthe project's version
copyright​_holderthe copyright holder of the catalog
msgid​_bugs​_addressthe email address or URL to submit bug reports to
creation​_datethe date the catalog was created
revision​_datethe date the catalog was revised
last​_translatorthe name and email of the last translator
language​_teamthe name and email of the language team
charsetthe encoding to use in the output (defaults to utf-8)
fuzzythe fuzzy bit on the catalog header
def __len__(self):

The number of messages in the catalog.

This does not include the special msgid "" entry.

def __repr__(self):

Undocumented

def __setitem__(self, id, message):

Add or update the message with the specified ID.

>>> catalog = Catalog()
>>> catalog[u'foo'] = Message(u'foo')
>>> catalog[u'foo']
<Message u'foo' (flags: [])>

If a message with that ID is already in the catalog, it is updated to include the locations and flags of the new message.

>>> catalog = Catalog()
>>> catalog[u'foo'] = Message(u'foo', locations=[('main.py', 1)])
>>> catalog[u'foo'].locations
[('main.py', 1)]
>>> catalog[u'foo'] = Message(u'foo', locations=[('utils.py', 5)])
>>> catalog[u'foo'].locations
[('main.py', 1), ('utils.py', 5)]
Parameters
idthe message ID
messagethe Message object
def _get_header_comment(self):

Undocumented

def _get_locale(self):

Undocumented

def _get_locale_identifier(self):

Undocumented

def _get_mime_headers(self):

Undocumented

def _key_for(self, id, context=None):
The key for a message is just the singular ID even for pluralizable messages, but is a (msgid, msgctxt) tuple for context-specific messages.
def _set_header_comment(self, string):

Undocumented

def _set_locale(self, locale):

Undocumented

def _set_mime_headers(self, headers):

Undocumented

locale_identifier =

Undocumented

_header_comment =

Undocumented

_locale =

Undocumented

_locale_identifier =

Undocumented

_messages =

Undocumented

_num_plurals =

Undocumented

_plural_expr =

Undocumented

charset =

Undocumented

copyright_holder =

Undocumented

creation_date =

Undocumented

domain =

Undocumented

fuzzy =

Undocumented

locale =

Undocumented

msgid_bugs_address =

Undocumented

obsolete =

Undocumented

project =

Undocumented

revision_date =

Undocumented

version =

Undocumented

@property
num_plurals =

The number of plurals used by the catalog or locale.

>>> Catalog(locale='en').num_plurals
2
>>> Catalog(locale='ga').num_plurals
5
@property
plural_expr =

The plural expression used by the catalog or locale.

>>> 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)'
@property
plural_forms =

Return the plural forms declaration for the locale.

>>> Catalog(locale='en').plural_forms
'nplurals=2; plural=(n != 1)'
>>> Catalog(locale='pt_BR').plural_forms
'nplurals=2; plural=(n > 1)'