| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node330.html" /> |
| 13 | <link rel="prev" href="node328.html" /> |
| 14 | <link rel="parent" href="node328.html" /> |
| 15 | <link rel="next" href="node330.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.28.2.1 The NullTranslations class</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="6.28.2 Class-based API" |
| 25 | href="node328.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="6.28.2 Class-based API" |
| 28 | href="node328.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="6.28.2.2 The GNUTranslations class" |
| 31 | href="node330.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="node328.html">6.28.2 Class-based API</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="node328.html">6.28.2 Class-based API</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node330.html">6.28.2.2 The GNUTranslations class</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION0082821000000000000000"> |
| 56 | 6.28.2.1 The <tt class="class">NullTranslations</tt> class</A> |
| 57 | </H3> |
| 58 | Translation classes are what actually implement the translation of |
| 59 | original source file message strings to translated message strings. |
| 60 | The base class used by all translation classes is |
| 61 | <tt class="class">NullTranslations</tt>; this provides the basic interface you can use |
| 62 | to write your own specialized translation classes. Here are the |
| 63 | methods of <tt class="class">NullTranslations</tt>: |
| 64 | |
| 65 | <P> |
| 66 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 67 | <td><nobr><b><tt id='l2h-2434' xml:id='l2h-2434' class="method">__init__</tt></b>(</nobr></td> |
| 68 | <td><var></var><big>[</big><var>fp</var><big>]</big><var></var>)</td></tr></table></dt> |
| 69 | <dd> |
| 70 | Takes an optional file object <var>fp</var>, which is ignored by the base |
| 71 | class. Initializes ``protected'' instance variables <var>_info</var> and |
| 72 | <var>_charset</var> which are set by derived classes, as well as <var>_fallback</var>, |
| 73 | which is set through <tt class="method">add_fallback</tt>. It then calls |
| 74 | <code>self._parse(fp)</code> if <var>fp</var> is not <code>None</code>. |
| 75 | </dl> |
| 76 | |
| 77 | <P> |
| 78 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 79 | <td><nobr><b><tt id='l2h-2435' xml:id='l2h-2435' class="method">_parse</tt></b>(</nobr></td> |
| 80 | <td><var>fp</var>)</td></tr></table></dt> |
| 81 | <dd> |
| 82 | No-op'd in the base class, this method takes file object <var>fp</var>, and |
| 83 | reads the data from the file, initializing its message catalog. If |
| 84 | you have an unsupported message catalog file format, you should |
| 85 | override this method to parse your format. |
| 86 | </dl> |
| 87 | |
| 88 | <P> |
| 89 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 90 | <td><nobr><b><tt id='l2h-2436' xml:id='l2h-2436' class="method">add_fallback</tt></b>(</nobr></td> |
| 91 | <td><var>fallback</var>)</td></tr></table></dt> |
| 92 | <dd> |
| 93 | Add <var>fallback</var> as the fallback object for the current translation |
| 94 | object. A translation object should consult the fallback if it cannot |
| 95 | provide a translation for a given message. |
| 96 | </dl> |
| 97 | |
| 98 | <P> |
| 99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 100 | <td><nobr><b><tt id='l2h-2437' xml:id='l2h-2437' class="method">gettext</tt></b>(</nobr></td> |
| 101 | <td><var>message</var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | If a fallback has been set, forward <tt class="method">gettext()</tt> to the fallback. |
| 104 | Otherwise, return the translated message. Overridden in derived classes. |
| 105 | </dl> |
| 106 | |
| 107 | <P> |
| 108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 109 | <td><nobr><b><tt id='l2h-2438' xml:id='l2h-2438' class="method">lgettext</tt></b>(</nobr></td> |
| 110 | <td><var>message</var>)</td></tr></table></dt> |
| 111 | <dd> |
| 112 | If a fallback has been set, forward <tt class="method">lgettext()</tt> to the fallback. |
| 113 | Otherwise, return the translated message. Overridden in derived classes. |
| 114 | |
| 115 | <P> |
| 116 | |
| 117 | <span class="versionnote">New in version 2.4.</span> |
| 118 | |
| 119 | </dl> |
| 120 | |
| 121 | <P> |
| 122 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 123 | <td><nobr><b><tt id='l2h-2439' xml:id='l2h-2439' class="method">ugettext</tt></b>(</nobr></td> |
| 124 | <td><var>message</var>)</td></tr></table></dt> |
| 125 | <dd> |
| 126 | If a fallback has been set, forward <tt class="method">ugettext()</tt> to the fallback. |
| 127 | Otherwise, return the translated message as a Unicode string. |
| 128 | Overridden in derived classes. |
| 129 | </dl> |
| 130 | |
| 131 | <P> |
| 132 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 133 | <td><nobr><b><tt id='l2h-2440' xml:id='l2h-2440' class="method">ngettext</tt></b>(</nobr></td> |
| 134 | <td><var>singular, plural, n</var>)</td></tr></table></dt> |
| 135 | <dd> |
| 136 | If a fallback has been set, forward <tt class="method">ngettext()</tt> to the fallback. |
| 137 | Otherwise, return the translated message. Overridden in derived classes. |
| 138 | |
| 139 | <P> |
| 140 | |
| 141 | <span class="versionnote">New in version 2.3.</span> |
| 142 | |
| 143 | </dl> |
| 144 | |
| 145 | <P> |
| 146 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 147 | <td><nobr><b><tt id='l2h-2441' xml:id='l2h-2441' class="method">lngettext</tt></b>(</nobr></td> |
| 148 | <td><var>singular, plural, n</var>)</td></tr></table></dt> |
| 149 | <dd> |
| 150 | If a fallback has been set, forward <tt class="method">ngettext()</tt> to the fallback. |
| 151 | Otherwise, return the translated message. Overridden in derived classes. |
| 152 | |
| 153 | <P> |
| 154 | |
| 155 | <span class="versionnote">New in version 2.4.</span> |
| 156 | |
| 157 | </dl> |
| 158 | |
| 159 | <P> |
| 160 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 161 | <td><nobr><b><tt id='l2h-2442' xml:id='l2h-2442' class="method">ungettext</tt></b>(</nobr></td> |
| 162 | <td><var>singular, plural, n</var>)</td></tr></table></dt> |
| 163 | <dd> |
| 164 | If a fallback has been set, forward <tt class="method">ungettext()</tt> to the fallback. |
| 165 | Otherwise, return the translated message as a Unicode string. |
| 166 | Overridden in derived classes. |
| 167 | |
| 168 | <P> |
| 169 | |
| 170 | <span class="versionnote">New in version 2.3.</span> |
| 171 | |
| 172 | </dl> |
| 173 | |
| 174 | <P> |
| 175 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 176 | <td><nobr><b><tt id='l2h-2443' xml:id='l2h-2443' class="method">info</tt></b>(</nobr></td> |
| 177 | <td><var></var>)</td></tr></table></dt> |
| 178 | <dd> |
| 179 | Return the ``protected'' <tt class="member">_info</tt> variable. |
| 180 | </dl> |
| 181 | |
| 182 | <P> |
| 183 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 184 | <td><nobr><b><tt id='l2h-2444' xml:id='l2h-2444' class="method">charset</tt></b>(</nobr></td> |
| 185 | <td><var></var>)</td></tr></table></dt> |
| 186 | <dd> |
| 187 | Return the ``protected'' <tt class="member">_charset</tt> variable. |
| 188 | </dl> |
| 189 | |
| 190 | <P> |
| 191 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 192 | <td><nobr><b><tt id='l2h-2445' xml:id='l2h-2445' class="method">output_charset</tt></b>(</nobr></td> |
| 193 | <td><var></var>)</td></tr></table></dt> |
| 194 | <dd> |
| 195 | Return the ``protected'' <tt class="member">_output_charset</tt> variable, which |
| 196 | defines the encoding used to return translated messages. |
| 197 | |
| 198 | <P> |
| 199 | |
| 200 | <span class="versionnote">New in version 2.4.</span> |
| 201 | |
| 202 | </dl> |
| 203 | |
| 204 | <P> |
| 205 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 206 | <td><nobr><b><tt id='l2h-2446' xml:id='l2h-2446' class="method">set_output_charset</tt></b>(</nobr></td> |
| 207 | <td><var>charset</var>)</td></tr></table></dt> |
| 208 | <dd> |
| 209 | Change the ``protected'' <tt class="member">_output_charset</tt> variable, which |
| 210 | defines the encoding used to return translated messages. |
| 211 | |
| 212 | <P> |
| 213 | |
| 214 | <span class="versionnote">New in version 2.4.</span> |
| 215 | |
| 216 | </dl> |
| 217 | |
| 218 | <P> |
| 219 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 220 | <td><nobr><b><tt id='l2h-2447' xml:id='l2h-2447' class="method">install</tt></b>(</nobr></td> |
| 221 | <td><var></var><big>[</big><var>unicode</var><big>]</big><var></var>)</td></tr></table></dt> |
| 222 | <dd> |
| 223 | If the <var>unicode</var> flag is false, this method installs |
| 224 | <tt class="method">self.gettext()</tt> into the built-in namespace, binding it to |
| 225 | "<tt class="samp">_</tt>". If <var>unicode</var> is true, it binds <tt class="method">self.ugettext()</tt> |
| 226 | instead. By default, <var>unicode</var> is false. |
| 227 | |
| 228 | <P> |
| 229 | Note that this is only one way, albeit the most convenient way, to |
| 230 | make the <tt class="function">_</tt> function available to your application. Because it |
| 231 | affects the entire application globally, and specifically the built-in |
| 232 | namespace, localized modules should never install <tt class="function">_</tt>. |
| 233 | Instead, they should use this code to make <tt class="function">_</tt> available to |
| 234 | their module: |
| 235 | |
| 236 | <P> |
| 237 | <div class="verbatim"><pre> |
| 238 | import gettext |
| 239 | t = gettext.translation('mymodule', ...) |
| 240 | _ = t.gettext |
| 241 | </pre></div> |
| 242 | |
| 243 | <P> |
| 244 | This puts <tt class="function">_</tt> only in the module's global namespace and so |
| 245 | only affects calls within this module. |
| 246 | </dl> |
| 247 | |
| 248 | <P> |
| 249 | |
| 250 | <DIV CLASS="navigation"> |
| 251 | <div class='online-navigation'> |
| 252 | <p></p><hr /> |
| 253 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 254 | <tr> |
| 255 | <td class='online-navigation'><a rel="prev" title="6.28.2 Class-based API" |
| 256 | href="node328.html"><img src='../icons/previous.png' |
| 257 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 258 | <td class='online-navigation'><a rel="parent" title="6.28.2 Class-based API" |
| 259 | href="node328.html"><img src='../icons/up.png' |
| 260 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 261 | <td class='online-navigation'><a rel="next" title="6.28.2.2 The GNUTranslations class" |
| 262 | href="node330.html"><img src='../icons/next.png' |
| 263 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 264 | <td align="center" width="100%">Python Library Reference</td> |
| 265 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 266 | href="contents.html"><img src='../icons/contents.png' |
| 267 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 268 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 269 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 270 | <td class='online-navigation'><a rel="index" title="Index" |
| 271 | href="genindex.html"><img src='../icons/index.png' |
| 272 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 273 | </tr></table> |
| 274 | <div class='online-navigation'> |
| 275 | <b class="navlabel">Previous:</b> |
| 276 | <a class="sectref" rel="prev" href="node328.html">6.28.2 Class-based API</A> |
| 277 | <b class="navlabel">Up:</b> |
| 278 | <a class="sectref" rel="parent" href="node328.html">6.28.2 Class-based API</A> |
| 279 | <b class="navlabel">Next:</b> |
| 280 | <a class="sectref" rel="next" href="node330.html">6.28.2.2 The GNUTranslations class</A> |
| 281 | </div> |
| 282 | </div> |
| 283 | <hr /> |
| 284 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 285 | </DIV> |
| 286 | <!--End of Navigation Panel--> |
| 287 | <ADDRESS> |
| 288 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 289 | </ADDRESS> |
| 290 | </BODY> |
| 291 | </HTML> |