class Message(object):
"""Representation of a single message in a catalog."""
- def __init__(self, id, string='', locations=(), flags=(), comments=[]):
+ def __init__(self, id, string='', locations=(), flags=(), comments=()):
"""Create the message object.
:param id: the message ID, or a ``(singular, plural)`` tuple for
``(singular, plural)`` tuple for pluralizable messages
:param locations: a sequence of ``(filenname, lineno)`` tuples
:param flags: a set or sequence of flags
- :param comments: a list of comments for the msgid
+ :param comments: a sequence of translator comments for the message
"""
self.id = id
if not string and self.pluralizable:
self.flags.add('python-format')
else:
self.flags.discard('python-format')
- self.comments = comments
+ self.comments = list(comments)
def __repr__(self):
return '<%s %r>' % (type(self).__name__, self.id)
self._messages = odict()
self.project = project or 'PROJECT' #: the project name
- self.version = version or 'VERSION' #: the project version
+ self.version = version or 'VERSION' #: the project version
self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS'
-
+
if creation_date is None:
creation_date = time.localtime()
elif isinstance(creation_date, datetime):
time.strftime('%Y-%m-%d %H:%M%z', self.revision_date)))
headers.append(('Last-Translator', self.last_translator))
headers.append(('Language-Team', '%s <LL@li.org>' % self.locale))
+ headers.append(('Plural-Forms', self.plural_forms))
headers.append(('MIME-Version', '1.0'))
headers.append(('Content-Type',
'text/plain; charset=%s' % self.charset))
headers.append(('Content-Transfer-Encoding', '8bit'))
- if self.locale is not None:
- headers.append(('Plural-Forms', self.plural_forms))
headers.append(('Generated-By', 'Babel %s' % VERSION))
return headers
headers = property(headers, doc="""\
PO-Revision-Date: 1990-08-03 12:00+0000
Last-Translator: John Doe <jd@example.com>
Language-Team: de_DE <LL@li.org>
+ Plural-Forms: nplurals=2; plural=(n != 1)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
- Plural-Forms: nplurals=2; plural=(n != 1)
Generated-By: Babel ...
:type: `list`
elif self.locale.language in PLURALS:
num = PLURALS[self.locale.language][0]
return num
- num_plurals = property(num_plurals)
+ num_plurals = property(num_plurals, doc="""\
+ The number of plurals used by the locale.
+ """)
def plural_forms(self):
num, expr = ('INTEGER', 'EXPRESSION')
return self._key_for(id) in self._messages
def __len__(self):
+ """The number of messages in the catalog.
+
+ This does not include the special ``msgid ""`` entry.
+ """
return len(self._messages)
def __iter__(self):
assert isinstance(message.string, (list, tuple))
self._messages[key] = message
- def add(self, id, string=None, locations=(), flags=(), comments=[]):
+ def add(self, id, string=None, locations=(), flags=(), comments=()):
"""Add or update the message with the specified ID.
>>> catalog = Catalog()
``(singular, plural)`` tuple for pluralizable messages
:param locations: a sequence of ``(filenname, lineno)`` tuples
:param flags: a set or sequence of flags
- :param comments: a list of comments for the msgid
+ :param comments: a list of translator comments
"""
self[id] = Message(id, string, list(locations), flags, comments)
def extract_from_dir(dirname=os.getcwd(), method_map=DEFAULT_MAPPING,
options_map=None, keywords=DEFAULT_KEYWORDS,
- comments_tags=[], callback=None):
+ comment_tags=(), callback=None):
"""Extract messages from any source files found in the given directory.
This function generates tuples of the form:
that should be recognized as translation functions) to
tuples that specify which of their arguments contain
localizable strings
- :param comments_tags: a list of translator tags to search for and include
- in output
+ :param comment_tags: a list of tags of translator comments to search for
+ and include in the results
:param callback: a function that is called for every file that message are
extracted from, just before the extraction itself is
performed; the function is passed the filename, the name
for lineno, message, comments in \
extract_from_file(method, filepath,
keywords=keywords,
- comments_tags=comments_tags,
+ comment_tags=comment_tags,
options=options):
yield filename, lineno, message, comments
break
def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS,
- comments_tags=[], options=None):
+ comment_tags=(), options=None):
"""Extract messages from a specific file.
This function returns a list of tuples of the form:
that should be recognized as translation functions) to
tuples that specify which of their arguments contain
localizable strings
- :param comments_tags: a list of translator tags to search for and include
- in output
+ :param comment_tags: a list of translator tags to search for and include
+ in the results
:param options: a dictionary of additional options (optional)
:return: the list of extracted messages
:rtype: `list`
"""
fileobj = open(filename, 'U')
try:
- return list(extract(method, fileobj, keywords,
- comments_tags=comments_tags, options=options))
+ return list(extract(method, fileobj, keywords, comment_tags, options))
finally:
fileobj.close()
-def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, comments_tags=[],
+def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, comment_tags=(),
options=None):
"""Extract messages from the given file-like object using the specified
extraction method.
that should be recognized as translation functions) to
tuples that specify which of their arguments contain
localizable strings
- :param comments_tags: a list of translator tags to search for and include
- in output
+ :param comment_tags: a list of translator tags to search for and include
+ in the results
:param options: a dictionary of additional options (optional)
:return: the list of extracted messages
:rtype: `list`
for entry_point in working_set.iter_entry_points(GROUP_NAME, method):
func = entry_point.load(require=True)
- m = []
- for lineno, funcname, messages, comments in \
- func(fileobj,
- keywords.keys(),
- comments_tags=comments_tags,
- options=options or {}):
+ results = func(fileobj, keywords.keys(), comment_tags,
+ options=options or {})
+ for lineno, funcname, messages, comments in results:
if isinstance(messages, (list, tuple)):
msgs = []
for index in keywords[funcname]:
raise ValueError('Unknown extraction method %r' % method)
-def extract_nothing(fileobj, keywords, comments_tags, options):
+def extract_nothing(fileobj, keywords, comment_tags, options):
"""Pseudo extractor that does not actually extract anything, but simply
returns an empty list.
"""
return []
-def extract_genshi(fileobj, keywords, comments_tags, options):
+def extract_genshi(fileobj, keywords, comment_tags, options):
"""Extract messages from Genshi templates.
:param fileobj: the file-like object the messages should be extracted from
:param keywords: a list of keywords (i.e. function names) that should be
recognized as translation functions
- :param comments_tags: a list of translator tags to search for and include
- in output
+ :param comment_tags: a list of translator tags to search for and include
+ in the results
:param options: a dictionary of additional options (optional)
:return: an iterator over ``(lineno, funcname, message, comments)`` tuples
:rtype: ``iterator``
gettext_functions=keywords):
yield lineno, func, message, []
-def extract_python(fileobj, keywords, comments_tags, options):
+def extract_python(fileobj, keywords, comment_tags, options):
"""Extract messages from Python source code.
:param fileobj: the file-like object the messages should be extracted from
:param keywords: a list of keywords (i.e. function names) that should be
recognized as translation functions
- :param comments_tags: a list of translator tags to search for and include
- in output
+ :param comment_tags: a list of translator tags to search for and include
+ in the results
:param options: a dictionary of additional options (optional)
:return: an iterator over ``(lineno, funcname, message, comments)`` tuples
:rtype: ``iterator``
if in_translator_comments is True:
translator_comments.append(value[1:].strip())
continue
- for comments_tag in comments_tags:
- if comments_tag in value:
+ for comment_tags in comment_tags:
+ if comment_tags in value:
if in_translator_comments is not True:
in_translator_comments = True
translator_comments.append(value[1:].strip())