📄 node144.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>6.23.1 GNU gettext API</title>
<META NAME="description" CONTENT="6.23.1 GNU gettext API">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" HREF="node145.html" tppabs="http://www.python.org/doc/current/lib/node145.html">
<LINK REL="previous" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">
<LINK REL="up" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">
<LINK REL="next" HREF="node145.html" tppabs="http://www.python.org/doc/current/lib/node145.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node145.html" tppabs="http://www.python.org/doc/current/lib/node145.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">6.23 gettext </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">6.23 gettext </A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node145.html" tppabs="http://www.python.org/doc/current/lib/node145.html">6.23.2 Class-based API</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION0082310000000000000000">
6.23.1 GNU <b class="program">gettext</b> API</A>
</H2>
<P>
The <tt class="module">gettext</tt> module defines the following API, which is very
similar to the GNU <b class="program">gettext</b> API. If you use this API you
will affect the translation of your entire application globally. Often
this is what you want if your application is monolingual, with the choice
of language dependent on the locale of your user. If you are
localizing a Python module, or if your application needs to switch
languages on the fly, you probably want to use the class-based API
instead.
<P>
<dl><dt><b><a name='l2h-1516'><tt class='function'>bindtextdomain</tt></a></b> (<var>domain</var><big>[</big><var>, localedir</var><big>]</big>)
<dd>
Bind the <var>domain</var> to the locale directory
<var>localedir</var>. More concretely, <tt class="module">gettext</tt> will look for
binary <span class="file">.mo</span> files for the given domain using the path (on Unix):
<span class="file"><var>localedir</var>/<var>language</var>/LC_MESSAGES/<var>domain</var>.mo</span>,
where <var>languages</var> is searched for in the environment variables
<a class="envvar" name='l2h-1520'>$LANGUAGE</a>, <a class="envvar" name='l2h-1521'>$LC_ALL</a>, <a class="envvar" name='l2h-1522'>$LC_MESSAGES</a>, and
<a class="envvar" name='l2h-1523'>$LANG</a> respectively.
<P>
If <var>localedir</var> is omitted or <code>None</code>, then the current binding
for <var>domain</var> is returned.<A NAME="tex2html15"
HREF="#foot17315"><SUP>6.1</SUP></A></dl>
<P>
<dl><dt><b><a name='l2h-1517'><tt class='function'>textdomain</tt></a></b> (<big>[</big><var>domain</var><big>]</big>)
<dd>
Change or query the current global domain. If <var>domain</var> is
<code>None</code>, then the current global domain is returned, otherwise the
global domain is set to <var>domain</var>, which is returned.
</dl>
<P>
<dl><dt><b><a name='l2h-1518'><tt class='function'>gettext</tt></a></b> (<var>message</var>)
<dd>
Return the localized translation of <var>message</var>, based on the
current global domain, language, and locale directory. This function
is usually aliased as <tt class="function">_</tt> in the local namespace (see
examples below).
</dl>
<P>
<dl><dt><b><a name='l2h-1519'><tt class='function'>dgettext</tt></a></b> (<var>domain, message</var>)
<dd>
Like <tt class="function">gettext()</tt>, but look the message up in the specified
<var>domain</var>.
</dl>
<P>
Note that GNU <b class="program">gettext</b> also defines a <tt class="function">dcgettext()</tt>
method, but this was deemed not useful and so it is currently
unimplemented.
<P>
Here's an example of typical usage for this API:
<P>
<dl><dd><pre class="verbatim">
import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print _('This is a translatable string.')
</pre></dl>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot17315">... returned.</A><A NAME="foot17315"
HREF="node144.html#tex2html15" tppabs="http://www.python.org/doc/current/lib/node144.html#tex2html15"><SUP>6.1</SUP></A>
<DD>
The default locale directory is system dependent; e.g. on
RedHat Linux it is <span class="file">/usr/share/locale</span>, but on Solaris it
is <span class="file">/usr/lib/locale</span>. The <tt class="module">gettext</tt> module does
not try to support these system dependent defaults; instead
its default is <span class="file"><code>sys.prefix</code>/share/locale</span>. For
this reason, it is always best to call
<tt class="function">bindtextdomain()</tt> with an explicit absolute path at
the start of your application.
</DL>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node145.html" tppabs="http://www.python.org/doc/current/lib/node145.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">6.23 gettext </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">6.23 gettext </A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node145.html" tppabs="http://www.python.org/doc/current/lib/node145.html">6.23.2 Class-based API</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -