| 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="node328.html" /> |
| 13 | <link rel="prev" href="module-gettext.html" /> |
| 14 | <link rel="parent" href="module-gettext.html" /> |
| 15 | <link rel="next" href="node328.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.28.1 GNU gettext API</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 gettext " |
| 25 | href="module-gettext.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 gettext " |
| 28 | href="module-gettext.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 Class-based API" |
| 31 | href="node328.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="module-gettext.html">6.28 gettext </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-gettext.html">6.28 gettext </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node328.html">6.28.2 Class-based API</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0082810000000000000000"> |
| 56 | 6.28.1 GNU <b class="program">gettext</b> API</A> |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | The <tt class="module">gettext</tt> module defines the following API, which is very |
| 61 | similar to the GNU <b class="program">gettext</b> API. If you use this API you |
| 62 | will affect the translation of your entire application globally. Often |
| 63 | this is what you want if your application is monolingual, with the choice |
| 64 | of language dependent on the locale of your user. If you are |
| 65 | localizing a Python module, or if your application needs to switch |
| 66 | languages on the fly, you probably want to use the class-based API |
| 67 | instead. |
| 68 | |
| 69 | <P> |
| 70 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 71 | <td><nobr><b><tt id='l2h-2412' xml:id='l2h-2412' class="function">bindtextdomain</tt></b>(</nobr></td> |
| 72 | <td><var>domain</var><big>[</big><var>, localedir</var><big>]</big><var></var>)</td></tr></table></dt> |
| 73 | <dd> |
| 74 | Bind the <var>domain</var> to the locale directory |
| 75 | <var>localedir</var>. More concretely, <tt class="module">gettext</tt> will look for |
| 76 | binary <span class="file">.mo</span> files for the given domain using the path (on <span class="Unix">Unix</span>): |
| 77 | <span class="file"><var>localedir</var>/<var>language</var>/LC_MESSAGES/<var>domain</var>.mo</span>, |
| 78 | where <var>languages</var> is searched for in the environment variables |
| 79 | <a class="envvar" id='l2h-2423' xml:id='l2h-2423'>LANGUAGE</a>, <a class="envvar" id='l2h-2424' xml:id='l2h-2424'>LC_ALL</a>, <a class="envvar" id='l2h-2425' xml:id='l2h-2425'>LC_MESSAGES</a>, and |
| 80 | <a class="envvar" id='l2h-2426' xml:id='l2h-2426'>LANG</a> respectively. |
| 81 | |
| 82 | <P> |
| 83 | If <var>localedir</var> is omitted or <code>None</code>, then the current binding |
| 84 | for <var>domain</var> is returned.<A NAME="tex2html42" |
| 85 | HREF="#foot36716"><SUP>6.2</SUP></A></dl> |
| 86 | |
| 87 | <P> |
| 88 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 89 | <td><nobr><b><tt id='l2h-2413' xml:id='l2h-2413' class="function">bind_textdomain_codeset</tt></b>(</nobr></td> |
| 90 | <td><var>domain</var><big>[</big><var>, codeset</var><big>]</big><var></var>)</td></tr></table></dt> |
| 91 | <dd> |
| 92 | Bind the <var>domain</var> to <var>codeset</var>, changing the encoding of |
| 93 | strings returned by the <tt class="function">gettext()</tt> family of functions. |
| 94 | If <var>codeset</var> is omitted, then the current binding is returned. |
| 95 | |
| 96 | <P> |
| 97 | |
| 98 | <span class="versionnote">New in version 2.4.</span> |
| 99 | |
| 100 | </dl> |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 104 | <td><nobr><b><tt id='l2h-2414' xml:id='l2h-2414' class="function">textdomain</tt></b>(</nobr></td> |
| 105 | <td><var></var><big>[</big><var>domain</var><big>]</big><var></var>)</td></tr></table></dt> |
| 106 | <dd> |
| 107 | Change or query the current global domain. If <var>domain</var> is |
| 108 | <code>None</code>, then the current global domain is returned, otherwise the |
| 109 | global domain is set to <var>domain</var>, which is returned. |
| 110 | </dl> |
| 111 | |
| 112 | <P> |
| 113 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 114 | <td><nobr><b><tt id='l2h-2415' xml:id='l2h-2415' class="function">gettext</tt></b>(</nobr></td> |
| 115 | <td><var>message</var>)</td></tr></table></dt> |
| 116 | <dd> |
| 117 | Return the localized translation of <var>message</var>, based on the |
| 118 | current global domain, language, and locale directory. This function |
| 119 | is usually aliased as <tt class="function">_</tt> in the local namespace (see |
| 120 | examples below). |
| 121 | </dl> |
| 122 | |
| 123 | <P> |
| 124 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 125 | <td><nobr><b><tt id='l2h-2416' xml:id='l2h-2416' class="function">lgettext</tt></b>(</nobr></td> |
| 126 | <td><var>message</var>)</td></tr></table></dt> |
| 127 | <dd> |
| 128 | Equivalent to <tt class="function">gettext()</tt>, but the translation is returned |
| 129 | in the preferred system encoding, if no other encoding was explicitly |
| 130 | set with <tt class="function">bind_textdomain_codeset()</tt>. |
| 131 | |
| 132 | <P> |
| 133 | |
| 134 | <span class="versionnote">New in version 2.4.</span> |
| 135 | |
| 136 | </dl> |
| 137 | |
| 138 | <P> |
| 139 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 140 | <td><nobr><b><tt id='l2h-2417' xml:id='l2h-2417' class="function">dgettext</tt></b>(</nobr></td> |
| 141 | <td><var>domain, message</var>)</td></tr></table></dt> |
| 142 | <dd> |
| 143 | Like <tt class="function">gettext()</tt>, but look the message up in the specified |
| 144 | <var>domain</var>. |
| 145 | </dl> |
| 146 | |
| 147 | <P> |
| 148 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 149 | <td><nobr><b><tt id='l2h-2418' xml:id='l2h-2418' class="function">ldgettext</tt></b>(</nobr></td> |
| 150 | <td><var>domain, message</var>)</td></tr></table></dt> |
| 151 | <dd> |
| 152 | Equivalent to <tt class="function">dgettext()</tt>, but the translation is returned |
| 153 | in the preferred system encoding, if no other encoding was explicitly |
| 154 | set with <tt class="function">bind_textdomain_codeset()</tt>. |
| 155 | |
| 156 | <P> |
| 157 | |
| 158 | <span class="versionnote">New in version 2.4.</span> |
| 159 | |
| 160 | </dl> |
| 161 | |
| 162 | <P> |
| 163 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 164 | <td><nobr><b><tt id='l2h-2419' xml:id='l2h-2419' class="function">ngettext</tt></b>(</nobr></td> |
| 165 | <td><var>singular, plural, n</var>)</td></tr></table></dt> |
| 166 | <dd> |
| 167 | |
| 168 | <P> |
| 169 | Like <tt class="function">gettext()</tt>, but consider plural forms. If a translation |
| 170 | is found, apply the plural formula to <var>n</var>, and return the |
| 171 | resulting message (some languages have more than two plural forms). |
| 172 | If no translation is found, return <var>singular</var> if <var>n</var> is 1; |
| 173 | return <var>plural</var> otherwise. |
| 174 | |
| 175 | <P> |
| 176 | The Plural formula is taken from the catalog header. It is a C or |
| 177 | Python expression that has a free variable n; the expression evaluates |
| 178 | to the index of the plural in the catalog. See the GNU gettext |
| 179 | documentation for the precise syntax to be used in .po files, and the |
| 180 | formulas for a variety of languages. |
| 181 | |
| 182 | <P> |
| 183 | |
| 184 | <span class="versionnote">New in version 2.3.</span> |
| 185 | |
| 186 | <P> |
| 187 | </dl> |
| 188 | |
| 189 | <P> |
| 190 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 191 | <td><nobr><b><tt id='l2h-2420' xml:id='l2h-2420' class="function">lngettext</tt></b>(</nobr></td> |
| 192 | <td><var>singular, plural, n</var>)</td></tr></table></dt> |
| 193 | <dd> |
| 194 | Equivalent to <tt class="function">ngettext()</tt>, but the translation is returned |
| 195 | in the preferred system encoding, if no other encoding was explicitly |
| 196 | set with <tt class="function">bind_textdomain_codeset()</tt>. |
| 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-2421' xml:id='l2h-2421' class="function">dngettext</tt></b>(</nobr></td> |
| 207 | <td><var>domain, singular, plural, n</var>)</td></tr></table></dt> |
| 208 | <dd> |
| 209 | Like <tt class="function">ngettext()</tt>, but look the message up in the specified |
| 210 | <var>domain</var>. |
| 211 | |
| 212 | <P> |
| 213 | |
| 214 | <span class="versionnote">New in version 2.3.</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-2422' xml:id='l2h-2422' class="function">ldngettext</tt></b>(</nobr></td> |
| 221 | <td><var>domain, singular, plural, n</var>)</td></tr></table></dt> |
| 222 | <dd> |
| 223 | Equivalent to <tt class="function">dngettext()</tt>, but the translation is returned |
| 224 | in the preferred system encoding, if no other encoding was explicitly |
| 225 | set with <tt class="function">bind_textdomain_codeset()</tt>. |
| 226 | |
| 227 | <P> |
| 228 | |
| 229 | <span class="versionnote">New in version 2.4.</span> |
| 230 | |
| 231 | </dl> |
| 232 | |
| 233 | <P> |
| 234 | Note that GNU <b class="program">gettext</b> also defines a <tt class="function">dcgettext()</tt> |
| 235 | method, but this was deemed not useful and so it is currently |
| 236 | unimplemented. |
| 237 | |
| 238 | <P> |
| 239 | Here's an example of typical usage for this API: |
| 240 | |
| 241 | <P> |
| 242 | <div class="verbatim"><pre> |
| 243 | import gettext |
| 244 | gettext.bindtextdomain('myapplication', '/path/to/my/language/directory') |
| 245 | gettext.textdomain('myapplication') |
| 246 | _ = gettext.gettext |
| 247 | # ... |
| 248 | print _('This is a translatable string.') |
| 249 | </pre></div> |
| 250 | |
| 251 | <P> |
| 252 | <BR><HR><H4>Footnotes</H4> |
| 253 | <DL> |
| 254 | <DT><A NAME="foot36716">... returned.</A><A |
| 255 | HREF="node327.html#tex2html42"><SUP>6.2</SUP></A></DT> |
| 256 | <DD> |
| 257 | The default locale directory is system dependent; for example, |
| 258 | on RedHat Linux it is <span class="file">/usr/share/locale</span>, but on Solaris |
| 259 | it is <span class="file">/usr/lib/locale</span>. The <tt class="module">gettext</tt> module |
| 260 | does not try to support these system dependent defaults; |
| 261 | instead its default is <span class="file"><code>sys.prefix</code>/share/locale</span>. |
| 262 | For this reason, it is always best to call |
| 263 | <tt class="function">bindtextdomain()</tt> with an explicit absolute path at |
| 264 | the start of your application. |
| 265 | |
| 266 | </DD> |
| 267 | </DL> |
| 268 | <DIV CLASS="navigation"> |
| 269 | <div class='online-navigation'> |
| 270 | <p></p><hr /> |
| 271 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 272 | <tr> |
| 273 | <td class='online-navigation'><a rel="prev" title="6.28 gettext " |
| 274 | href="module-gettext.html"><img src='../icons/previous.png' |
| 275 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 276 | <td class='online-navigation'><a rel="parent" title="6.28 gettext " |
| 277 | href="module-gettext.html"><img src='../icons/up.png' |
| 278 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 279 | <td class='online-navigation'><a rel="next" title="6.28.2 Class-based API" |
| 280 | href="node328.html"><img src='../icons/next.png' |
| 281 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 282 | <td align="center" width="100%">Python Library Reference</td> |
| 283 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 284 | href="contents.html"><img src='../icons/contents.png' |
| 285 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 286 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 287 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 288 | <td class='online-navigation'><a rel="index" title="Index" |
| 289 | href="genindex.html"><img src='../icons/index.png' |
| 290 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 291 | </tr></table> |
| 292 | <div class='online-navigation'> |
| 293 | <b class="navlabel">Previous:</b> |
| 294 | <a class="sectref" rel="prev" href="module-gettext.html">6.28 gettext </A> |
| 295 | <b class="navlabel">Up:</b> |
| 296 | <a class="sectref" rel="parent" href="module-gettext.html">6.28 gettext </A> |
| 297 | <b class="navlabel">Next:</b> |
| 298 | <a class="sectref" rel="next" href="node328.html">6.28.2 Class-based API</A> |
| 299 | </div> |
| 300 | </div> |
| 301 | <hr /> |
| 302 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 303 | </DIV> |
| 304 | <!--End of Navigation Panel--> |
| 305 | <ADDRESS> |
| 306 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 307 | </ADDRESS> |
| 308 | </BODY> |
| 309 | </HTML> |