Home | Trees | Indices | Help |
|
---|
|
object --+ | Translator
Can extract and translate localizable strings from markup streams and templates.
For example, assume the followng template:
>>> from genshi.template import MarkupTemplate >>> >>> 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> ... </body> ... </html>''', filename='example.html')
For demonstration, we define a dummy gettext-style function with a hard-coded translation table, and pass that to the Translator initializer:
>>> def pseudo_gettext(string): ... return { ... 'Example': 'Beispiel', ... 'Hello, %(name)s': 'Hallo, %(name)s' ... }[string] >>> >>> translator = Translator(pseudo_gettext)
Next, the translator needs to be prepended to any already defined filters on the template:
>>> tmpl.filters.insert(0, translator)
When generating the template output, our hard-coded translations should be applied as expected:
>>> print tmpl.generate(username='Hans', _=pseudo_gettext) <html> <head> <title>Beispiel</title> </head> <body> <h1>Beispiel</h1> <p>Hallo, Hans</p> </body> </html>
Instance Methods | |||
|
|||
|
|||
|
|||
Inherited from |
Class Variables | |
IGNORE_TAGS =
|
|
INCLUDE_ATTRS =
|
|
GETTEXT_FUNCTIONS =
|
Properties | |
Inherited from |
Method Details |
|
Translate any localizable strings in the given stream. This function shouldn't be called directly. Instead, an instance of the Translator class should be registered as a filter with the Template or the TemplateLoader, or applied as a regular stream filter. If used as a template filter, it should be inserted in front of all the default filters.
|
Extract localizable strings from the given template stream. For every string found, this function yields a (lineno, function, message) tuple, where:
>>> from genshi.template import MarkupTemplate >>> >>> 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> ... </body> ... </html>''', filename='example.html') >>> >>> for lineno, funcname, message in Translator().extract(tmpl.stream): ... print "%d, %r, %r" % (lineno, funcname, message) 3, None, u'Example' 6, None, u'Example' 7, '_', u'Hello, %(name)s'
|
Class Variable Details |
IGNORE_TAGS
|
GETTEXT_FUNCTIONS
|
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0beta1 on Mon Apr 16 16:02:13 2007 | http://epydoc.sourceforge.net |