extract(self,
stream,
gettext_functions=( ' _ ' , ' gettext ' , ' ngettext ' , ' dgettext ' , ' dngettext ' , ' ugettex ... ,
search_text=True,
comment_stack=None)
|
|
Extract localizable strings from the given template stream.
For every string found, this function yields a (lineno, function,
message, comments) tuple, where:
- lineno is the number of the line on which the string was found,
- function is the name of the gettext function used (if the
string was extracted from embedded Python code), and
- message is the string itself (a unicode object, or a tuple
of unicode objects for functions with multiple string
arguments).
- comments is a list of comments related to the message, extracted
from i18n:comment attributes found in the markup
>>> tmpl = MarkupTemplate('''<html xmlns:py="http://genshi.edgewall.org/">
... <head>
... <title>Example</title>
... </head>
... <body>
... <h1>Example</h1>
... <p>${_("Hello, %(name)s") % dict(name=username)}</p>
... <p>${ngettext("You have %d item", "You have %d items", num)}</p>
... </body>
... </html>''', filename='example.html')
>>> for line, func, msg, comments in Translator().extract(tmpl.stream):
... print('%d, %r, %r' % (line, func, msg))
3, None, u'Example'
6, None, u'Example'
7, '_', u'Hello, %(name)s'
8, 'ngettext', (u'You have %d item', u'You have %d items', None)
- Parameters:
stream - the event stream to extract strings from; can be a
regular stream or a template stream
gettext_functions - a sequence of function names that should be
treated as gettext-style localization
functions
search_text - whether the content of text nodes should be
extracted (used internally)
Notes:
-
Changed in 0.4.1: For a function with multiple string arguments
(such as ngettext), a single item with a tuple of strings is
yielded, instead an item for each string argument.
-
Changed in 0.6: The returned tuples now include a fourth
element, which is a list of comments for the translator.
|