1
<?xml version="1.0" encoding="ascii"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3
"DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>genshi.filters.i18n.Translator</title>
7
<link rel="stylesheet" href="epydoc.css" type="text/css" />
8
<script type="text/javascript" src="epydoc.js"></script>
11
<body bgcolor="white" text="black" link="blue" vlink="#204080"
13
<!-- ==================== NAVIGATION BAR ==================== -->
14
<table class="navbar" border="0" width="100%" cellpadding="0"
15
bgcolor="#a0c0ff" cellspacing="0">
18
<th> <a
19
href="genshi-module.html">Home</a> </th>
22
<th> <a
23
href="module-tree.html">Trees</a> </th>
26
<th> <a
27
href="identifier-index.html">Indices</a> </th>
30
<th> <a
31
href="help.html">Help</a> </th>
33
<!-- Project homepage -->
34
<th class="navbar" align="right" width="100%">
35
<table border="0" cellpadding="0" cellspacing="0">
36
<tr><th class="navbar" align="center"
37
><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
41
<table width="100%" cellpadding="0" cellspacing="0">
44
<span class="breadcrumbs">
45
<a href="genshi-module.html">Package genshi</a> ::
46
<a href="genshi.filters-module.html">Package filters</a> ::
47
<a href="genshi.filters.i18n-module.html">Module i18n</a> ::
52
<table cellpadding="0" cellspacing="0">
53
<!-- hide/show private -->
58
<!-- ==================== CLASS DESCRIPTION ==================== -->
59
<h1 class="epydoc">Class Translator</h1><br /><br />
60
<pre class="base-tree">
63
<strong class="uidshort">Translator</strong>
67
<p>Can extract and translate localizable strings from markup streams and
69
<p>For example, assume the followng template:</p>
70
<pre class="py-doctest">
71
<span class="py-prompt">>>> </span><span class="py-keyword">from</span> genshi.template <span class="py-keyword">import</span> MarkupTemplate
72
<span class="py-prompt">>>></span>
73
<span class="py-prompt">>>> </span>tmpl = MarkupTemplate(<span class="py-string">'''<html xmlns:py="http://genshi.edgewall.org/"></span>
74
<span class="py-more">... </span><span class="py-string"> <head></span>
75
<span class="py-more">... </span><span class="py-string"> <title>Example</title></span>
76
<span class="py-more">... </span><span class="py-string"> </head></span>
77
<span class="py-more">... </span><span class="py-string"> <body></span>
78
<span class="py-more">... </span><span class="py-string"> <h1>Example</h1></span>
79
<span class="py-more">... </span><span class="py-string"> <p>${_("Hello, %(name)s") % dict(name=username)}</p></span>
80
<span class="py-more">... </span><span class="py-string"> </body></span>
81
<span class="py-more">... </span><span class="py-string"></html>'''</span>, filename=<span class="py-string">'example.html'</span>)</pre>
82
<p>For demonstration, we define a dummy <tt class="rst-docutils literal"><span class="pre">gettext</span></tt>-style function with a
83
hard-coded translation table, and pass that to the <a href="genshi.filters.i18n.Translator-class.html" class="link">Translator</a> initializer:</p>
84
<pre class="py-doctest">
85
<span class="py-prompt">>>> </span><span class="py-keyword">def</span> <span class="py-defname">pseudo_gettext</span>(string):
86
<span class="py-more">... </span> return {
87
<span class="py-more">... </span> <span class="py-string">'Example'</span>: <span class="py-string">'Beispiel'</span>,
88
<span class="py-more">... </span> <span class="py-string">'Hello, %(name)s'</span>: <span class="py-string">'Hallo, %(name)s'</span>
89
<span class="py-more">... </span> }[string]
90
<span class="py-prompt">>>></span>
91
<span class="py-prompt">>>> </span>translator = Translator(pseudo_gettext)</pre>
92
<p>Next, the translator needs to be prepended to any already defined filters
94
<pre class="py-doctest">
95
<span class="py-prompt">>>> </span>tmpl.filters.insert(0, translator)</pre>
96
<p>When generating the template output, our hard-coded translations should be
97
applied as expected:</p>
98
<pre class="py-doctest">
99
<span class="py-prompt">>>> </span><span class="py-keyword">print</span> tmpl.generate(username=<span class="py-string">'Hans'</span>, _=pseudo_gettext)
100
<span class="py-output"><html></span>
101
<span class="py-output"> <head></span>
102
<span class="py-output"> <title>Beispiel</title></span>
103
<span class="py-output"> </head></span>
104
<span class="py-output"> <body></span>
105
<span class="py-output"> <h1>Beispiel</h1></span>
106
<span class="py-output"> <p>Hallo, Hans</p></span>
107
<span class="py-output"> </body></span>
108
<span class="py-output"></html></span></pre><br /><br />
110
<!-- ==================== INSTANCE METHODS ==================== -->
111
<a name="section-InstanceMethods"></a>
112
<table class="summary" border="1" cellpadding="3"
113
cellspacing="0" width="100%" bgcolor="white">
114
<tr bgcolor="#70b0f0" class="table-header">
115
<td align="left" colspan="2" class="table-header">
116
<span class="table-header">Instance Methods</span></td>
119
<td width="15%" align="right" valign="top" class="summary">
120
<span class="summary-type"> </span>
121
</td><td class="summary">
122
<table width="100%" cellpadding="0" cellspacing="0" border="0">
124
<td><span class="summary-sig"><a href="genshi.filters.i18n.Translator-class.html#__init__" class="summary-sig-name">__init__</a>(<span class="summary-sig-arg">self</span>,
125
<span class="summary-sig-arg">translate</span>=<span class="summary-sig-default"><function gettext at 0x110a0b0></span>,
126
<span class="summary-sig-arg">ignore_tags</span>=<span class="summary-sig-default"><code class="variable-group">frozenset([</code>QName(u'script')<code class="variable-op">, </code>QName(u'style')<code class="variable-op">, </code>QName(u'http://w<code class="variable-ellipsis">...</code></span>,
127
<span class="summary-sig-arg">include_attrs</span>=<span class="summary-sig-default"><code class="variable-group">frozenset([</code><code class="variable-quote">'</code><code class="variable-string">alt</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">title</code><code class="variable-quote">'</code><code class="variable-group">])</code></span>)</span><br />
128
Initialize the translator.</td>
129
<td align="right" valign="top">
139
<td width="15%" align="right" valign="top" class="summary">
140
<span class="summary-type"> </span>
141
</td><td class="summary">
142
<table width="100%" cellpadding="0" cellspacing="0" border="0">
144
<td><span class="summary-sig"><a href="genshi.filters.i18n.Translator-class.html#__call__" class="summary-sig-name">__call__</a>(<span class="summary-sig-arg">self</span>,
145
<span class="summary-sig-arg">stream</span>,
146
<span class="summary-sig-arg">ctxt</span>=<span class="summary-sig-default">None</span>,
147
<span class="summary-sig-arg">search_text</span>=<span class="summary-sig-default">True</span>)</span><br />
148
Translate any localizable strings in the given stream.</td>
149
<td align="right" valign="top">
159
<td width="15%" align="right" valign="top" class="summary">
160
<span class="summary-type"> </span>
161
</td><td class="summary">
162
<table width="100%" cellpadding="0" cellspacing="0" border="0">
164
<td><span class="summary-sig"><a href="genshi.filters.i18n.Translator-class.html#extract" class="summary-sig-name">extract</a>(<span class="summary-sig-arg">self</span>,
165
<span class="summary-sig-arg">stream</span>,
166
<span class="summary-sig-arg">gettext_functions</span>=<span class="summary-sig-default"><code class="variable-group">(</code><code class="variable-quote">'</code><code class="variable-string">_</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">gettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">ngettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">dgettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">dngettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">ugettex</code><code class="variable-ellipsis">...</code></span>)</span><br />
167
Extract localizable strings from the given template stream.</td>
168
<td align="right" valign="top">
178
<td colspan="2" class="summary">
179
<p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
180
<code>__delattr__</code>,
181
<code>__getattribute__</code>,
182
<code>__hash__</code>,
183
<code>__new__</code>,
184
<code>__reduce__</code>,
185
<code>__reduce_ex__</code>,
186
<code>__repr__</code>,
187
<code>__setattr__</code>,
193
<!-- ==================== CLASS VARIABLES ==================== -->
194
<a name="section-ClassVariables"></a>
195
<table class="summary" border="1" cellpadding="3"
196
cellspacing="0" width="100%" bgcolor="white">
197
<tr bgcolor="#70b0f0" class="table-header">
198
<td align="left" colspan="2" class="table-header">
199
<span class="table-header">Class Variables</span></td>
202
<td width="15%" align="right" valign="top" class="summary">
203
<span class="summary-type"> </span>
204
</td><td class="summary">
205
<a href="genshi.filters.i18n.Translator-class.html#IGNORE_TAGS" class="summary-name">IGNORE_TAGS</a> = <code title="frozenset([QName(u'script'),
207
QName(u'http://www.w3.org/1999/xhtml}script'),
208
QName(u'http://www.w3.org/1999/xhtml}style')])"><code class="variable-group">frozenset([</code>QName(u'script')<code class="variable-op">, </code>QName(u'style')<code class="variable-op">, </code>QN<code class="variable-ellipsis">...</code></code>
212
<td width="15%" align="right" valign="top" class="summary">
213
<span class="summary-type"> </span>
214
</td><td class="summary">
215
<a name="INCLUDE_ATTRS"></a><span class="summary-name">INCLUDE_ATTRS</span> = <code title="frozenset(['alt', 'title'])"><code class="variable-group">frozenset([</code><code class="variable-quote">'</code><code class="variable-string">alt</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">title</code><code class="variable-quote">'</code><code class="variable-group">])</code></code>
219
<td width="15%" align="right" valign="top" class="summary">
220
<span class="summary-type"> </span>
221
</td><td class="summary">
222
<a href="genshi.filters.i18n.Translator-class.html#GETTEXT_FUNCTIONS" class="summary-name">GETTEXT_FUNCTIONS</a> = <code title="('_',
228
'ungettext')"><code class="variable-group">(</code><code class="variable-quote">'</code><code class="variable-string">_</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">gettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">ngettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">dgettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-ellipsis">...</code></code>
232
<!-- ==================== PROPERTIES ==================== -->
233
<a name="section-Properties"></a>
234
<table class="summary" border="1" cellpadding="3"
235
cellspacing="0" width="100%" bgcolor="white">
236
<tr bgcolor="#70b0f0" class="table-header">
237
<td align="left" colspan="2" class="table-header">
238
<span class="table-header">Properties</span></td>
241
<td colspan="2" class="summary">
242
<p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
243
<code>__class__</code>
248
<!-- ==================== METHOD DETAILS ==================== -->
249
<a name="section-MethodDetails"></a>
250
<table class="details" border="1" cellpadding="3"
251
cellspacing="0" width="100%" bgcolor="white">
252
<tr bgcolor="#70b0f0" class="table-header">
253
<td align="left" colspan="2" class="table-header">
254
<span class="table-header">Method Details</span></td>
257
<a name="__init__"></a>
259
<table class="details" border="1" cellpadding="3"
260
cellspacing="0" width="100%" bgcolor="white">
262
<table width="100%" cellpadding="0" cellspacing="0" border="0">
263
<tr valign="top"><td>
264
<h3 class="epydoc"><span class="sig"><span class="sig-name">__init__</span>(<span class="sig-arg">self</span>,
265
<span class="sig-arg">translate</span>=<span class="sig-default"><function gettext at 0x110a0b0></span>,
266
<span class="sig-arg">ignore_tags</span>=<span class="sig-default"><code class="variable-group">frozenset([</code>QName(u'script')<code class="variable-op">, </code>QName(u'style')<code class="variable-op">, </code>QName(u'http://w<code class="variable-ellipsis">...</code></span>,
267
<span class="sig-arg">include_attrs</span>=<span class="sig-default"><code class="variable-group">frozenset([</code><code class="variable-quote">'</code><code class="variable-string">alt</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">title</code><code class="variable-quote">'</code><code class="variable-group">])</code></span>)</span>
268
<br /><em class="fname">(Constructor)</em>
270
</td><td align="right" valign="top"
275
Initialize the translator.
278
<dd><ul class="nomargin-top">
279
<li><strong class="pname"><code>translate</code></strong> - the translation function, for example <tt class="rst-docutils literal"><span class="pre">gettext</span></tt> or
280
<tt class="rst-docutils literal"><span class="pre">ugettext</span></tt>.</li>
281
<li><strong class="pname"><code>ignore_tags</code></strong> - a set of tag names that should not be localized</li>
282
<li><strong class="pname"><code>include_attrs</code></strong> - a set of attribute names should be localized</li>
290
<a name="__call__"></a>
292
<table class="details" border="1" cellpadding="3"
293
cellspacing="0" width="100%" bgcolor="white">
295
<table width="100%" cellpadding="0" cellspacing="0" border="0">
296
<tr valign="top"><td>
297
<h3 class="epydoc"><span class="sig"><span class="sig-name">__call__</span>(<span class="sig-arg">self</span>,
298
<span class="sig-arg">stream</span>,
299
<span class="sig-arg">ctxt</span>=<span class="sig-default">None</span>,
300
<span class="sig-arg">search_text</span>=<span class="sig-default">True</span>)</span>
301
<br /><em class="fname">(Call operator)</em>
303
</td><td align="right" valign="top"
308
<p>Translate any localizable strings in the given stream.</p>
309
<p>This function shouldn't be called directly. Instead, an instance of
310
the <a href="genshi.filters.i18n.Translator-class.html" class="link">Translator</a> class should be registered as a filter with the
311
<a href="genshi.template.base.Template-class.html" class="link">Template</a> or the <a href="genshi.template.loader.TemplateLoader-class.html" class="link">TemplateLoader</a>, or applied as a regular stream
312
filter. If used as a template filter, it should be inserted in front of
313
all the default filters.</p>
316
<dd><ul class="nomargin-top">
317
<li><strong class="pname"><code>stream</code></strong> - the markup event stream</li>
318
<li><strong class="pname"><code>ctxt</code></strong> - the template context (not used)</li>
319
<li><strong class="pname"><code>search_text</code></strong> - whether text nodes should be translated (used
323
<dd>the localized stream</dd>
327
<a name="extract"></a>
329
<table class="details" border="1" cellpadding="3"
330
cellspacing="0" width="100%" bgcolor="white">
332
<table width="100%" cellpadding="0" cellspacing="0" border="0">
333
<tr valign="top"><td>
334
<h3 class="epydoc"><span class="sig"><span class="sig-name">extract</span>(<span class="sig-arg">self</span>,
335
<span class="sig-arg">stream</span>,
336
<span class="sig-arg">gettext_functions</span>=<span class="sig-default"><code class="variable-group">(</code><code class="variable-quote">'</code><code class="variable-string">_</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">gettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">ngettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">dgettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">dngettext</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">ugettex</code><code class="variable-ellipsis">...</code></span>)</span>
338
</td><td align="right" valign="top"
343
<p>Extract localizable strings from the given template stream.</p>
344
<p>For every string found, this function yields a <tt class="rst-docutils literal"><span class="pre">(lineno,</span> <span class="pre">function,</span>
345
<span class="pre">message)</span></tt> tuple, where:</p>
346
<ul class="rst-simple">
347
<li><tt class="rst-docutils literal"><span class="pre">lineno</span></tt> is the number of the line on which the string was found,</li>
348
<li><tt class="rst-docutils literal"><span class="pre">function</span></tt> is the name of the <tt class="rst-docutils literal"><span class="pre">gettext</span></tt> function used (if the
349
string was extracted from embedded Python code), and</li>
350
<li><tt class="rst-docutils literal"><span class="pre">message</span></tt> is the string itself (a <tt class="rst-docutils literal"><span class="pre">unicode</span></tt> object).</li>
352
<pre class="py-doctest">
353
<span class="py-prompt">>>> </span><span class="py-keyword">from</span> genshi.template <span class="py-keyword">import</span> MarkupTemplate
354
<span class="py-prompt">>>></span>
355
<span class="py-prompt">>>> </span>tmpl = MarkupTemplate(<span class="py-string">'''<html xmlns:py="http://genshi.edgewall.org/"></span>
356
<span class="py-more">... </span><span class="py-string"> <head></span>
357
<span class="py-more">... </span><span class="py-string"> <title>Example</title></span>
358
<span class="py-more">... </span><span class="py-string"> </head></span>
359
<span class="py-more">... </span><span class="py-string"> <body></span>
360
<span class="py-more">... </span><span class="py-string"> <h1>Example</h1></span>
361
<span class="py-more">... </span><span class="py-string"> <p>${_("Hello, %(name)s") % dict(name=username)}</p></span>
362
<span class="py-more">... </span><span class="py-string"> </body></span>
363
<span class="py-more">... </span><span class="py-string"></html>'''</span>, filename=<span class="py-string">'example.html'</span>)
364
<span class="py-prompt">>>></span>
365
<span class="py-prompt">>>> </span><span class="py-keyword">for</span> lineno, funcname, message <span class="py-keyword">in</span> Translator().extract(tmpl.stream):
366
<span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"%d, %r, %r"</span> % (lineno, funcname, message)
367
<span class="py-output">3, None, u'Example'</span>
368
<span class="py-output">6, None, u'Example'</span>
369
<span class="py-output">7, '_', u'Hello, %(name)s'</span></pre>
372
<dd><ul class="nomargin-top">
373
<li><strong class="pname"><code>stream</code></strong> - the event stream to extract strings from; can be a
374
regular stream or a template stream</li>
375
<li><strong class="pname"><code>gettext_functions</code></strong> - a sequence of function names that should be
376
treated as gettext-style localization
383
<!-- ==================== CLASS VARIABLE DETAILS ==================== -->
384
<a name="section-ClassVariableDetails"></a>
385
<table class="details" border="1" cellpadding="3"
386
cellspacing="0" width="100%" bgcolor="white">
387
<tr bgcolor="#70b0f0" class="table-header">
388
<td align="left" colspan="2" class="table-header">
389
<span class="table-header">Class Variable Details</span></td>
392
<a name="IGNORE_TAGS"></a>
394
<table class="details" border="1" cellpadding="3"
395
cellspacing="0" width="100%" bgcolor="white">
397
<h3 class="epydoc">IGNORE_TAGS</h3>
403
<dd><table><tr><td><pre class="variable">
404
<code class="variable-group">frozenset([</code>QName(u'script')<code class="variable-op">,</code>
405
QName(u'style')<code class="variable-op">,</code>
406
QName(u'http://www.w3.org/1999/xhtml}script')<code class="variable-op">,</code>
407
QName(u'http://www.w3.org/1999/xhtml}style')<code class="variable-group">])</code>
408
</pre></td></tr></table>
413
<a name="GETTEXT_FUNCTIONS"></a>
415
<table class="details" border="1" cellpadding="3"
416
cellspacing="0" width="100%" bgcolor="white">
418
<h3 class="epydoc">GETTEXT_FUNCTIONS</h3>
424
<dd><table><tr><td><pre class="variable">
425
<code class="variable-group">(</code><code class="variable-quote">'</code><code class="variable-string">_</code><code class="variable-quote">'</code><code class="variable-op">,</code>
426
<code class="variable-quote">'</code><code class="variable-string">gettext</code><code class="variable-quote">'</code><code class="variable-op">,</code>
427
<code class="variable-quote">'</code><code class="variable-string">ngettext</code><code class="variable-quote">'</code><code class="variable-op">,</code>
428
<code class="variable-quote">'</code><code class="variable-string">dgettext</code><code class="variable-quote">'</code><code class="variable-op">,</code>
429
<code class="variable-quote">'</code><code class="variable-string">dngettext</code><code class="variable-quote">'</code><code class="variable-op">,</code>
430
<code class="variable-quote">'</code><code class="variable-string">ugettext</code><code class="variable-quote">'</code><code class="variable-op">,</code>
431
<code class="variable-quote">'</code><code class="variable-string">ungettext</code><code class="variable-quote">'</code><code class="variable-group">)</code>
432
</pre></td></tr></table>
438
<!-- ==================== NAVIGATION BAR ==================== -->
439
<table class="navbar" border="0" width="100%" cellpadding="0"
440
bgcolor="#a0c0ff" cellspacing="0">
443
<th> <a
444
href="genshi-module.html">Home</a> </th>
447
<th> <a
448
href="module-tree.html">Trees</a> </th>
451
<th> <a
452
href="identifier-index.html">Indices</a> </th>
455
<th> <a
456
href="help.html">Help</a> </th>
458
<!-- Project homepage -->
459
<th class="navbar" align="right" width="100%">
460
<table border="0" cellpadding="0" cellspacing="0">
461
<tr><th class="navbar" align="center"
462
><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
466
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
468
<td align="left" class="footer">
469
Generated by Epydoc 3.0beta1 on Mon Apr 16 16:02:13 2007
471
<td align="right" class="footer">
472
<a href="http://epydoc.sourceforge.net">http://epydoc.sourceforge.net</a>
477
<script type="text/javascript">
479
// Private objects are initially displayed (because if
480
// javascript is turned off then we want them to be
481
// visible); but by default, we want to hide them. So hide
482
// them unless we have a cookie that says to show them.