node145.html

来自「一本很好的python的说明书,适合对python感兴趣的人」· HTML 代码 · 共 175 行

HTML
175
字号
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>6.23.2 Class-based API</title>
<META NAME="description" CONTENT="6.23.2 Class-based 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="node150.html" tppabs="http://www.python.org/doc/current/lib/node150.html">
<LINK REL="previous" HREF="node144.html" tppabs="http://www.python.org/doc/current/lib/node144.html">
<LINK REL="up" href="module-gettext.html" tppabs="http://www.python.org/doc/current/lib/module-gettext.html">
<LINK REL="next" HREF="node146.html" tppabs="http://www.python.org/doc/current/lib/node146.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node144.html" tppabs="http://www.python.org/doc/current/lib/node144.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="node146.html" tppabs="http://www.python.org/doc/current/lib/node146.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="node144.html" tppabs="http://www.python.org/doc/current/lib/node144.html">6.23.1 GNU gettext API</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="node146.html" tppabs="http://www.python.org/doc/current/lib/node146.html">6.23.2.1 The NullTranslations class</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0082320000000000000000">
6.23.2 Class-based API</A>
</H2>

<P>
The class-based API of the <tt class="module">gettext</tt> module gives you more
flexibility and greater convenience than the GNU <b class="program">gettext</b>
API.  It is the recommended way of localizing your Python applications and
modules.  <tt class="module">gettext</tt> defines a ``translations'' class which
implements the parsing of GNU <span class="file">.mo</span> format files, and has methods
for returning either standard 8-bit strings or Unicode strings.
Translations instances can also install themselves in the built-in
namespace as the function <tt class="function">_()</tt>.

<P>
<dl><dt><b><a name='l2h-1524'><tt class='function'>find</tt></a></b> (<var>domain</var><big>[</big><var>, localedir</var><big>[</big><var>, languages</var><big>]</big><big>]</big>)
<dd>
This function implements the standard <span class="file">.mo</span> file search
algorithm.  It takes a <var>domain</var>, identical to what
<tt class="function">textdomain()</tt> takes, and optionally a <var>localedir</var> (as in
<tt class="function">bindtextdomain()</tt>), and a list of languages.  All arguments
are strings.

<P>
If <var>localedir</var> is not given, then the default system locale
directory is used.<A NAME="tex2html16"
  HREF="#foot17305"><SUP>6.2</SUP></A>  If <var>languages</var> is not given,
then the following environment variables are searched: <a class="envvar" name='l2h-1527'>$LANGUAGE</a>,
<a class="envvar" name='l2h-1528'>$LC_ALL</a>, <a class="envvar" name='l2h-1529'>$LC_MESSAGES</a>, and <a class="envvar" name='l2h-1530'>$LANG</a>.  The first one
returning a non-empty value is used for the <var>languages</var> variable.
The environment variables can contain a colon separated list of
languages, which will be split.

<P>
<tt class="function">find()</tt> then expands and normalizes the languages, and then
iterates through them, searching for an existing file built of these
components:

<P>
<span class="file"><var>localedir</var>/<var>language</var>/LC_MESSAGES/<var>domain</var>.mo</span>

<P>
The first such file name that exists is returned by <tt class="function">find()</tt>.
If no such file is found, then <code>None</code> is returned.
</dl>

<P>
<dl><dt><b><a name='l2h-1525'><tt class='function'>translation</tt></a></b> (<var>domain</var><big>[</big><var>, localedir</var><big>[</big><var>,
                              languages</var><big>[</big><var>, class_</var><big>]</big><big>]</big><big>]</big>)
<dd>
Return a <tt class="class">Translations</tt> instance based on the <var>domain</var>,
<var>localedir</var>, and <var>languages</var>, which are first passed to
<tt class="function">find()</tt> to get the
associated <span class="file">.mo</span> file path.  Instances with
identical <span class="file">.mo</span> file names are cached.  The actual class instantiated
is either <var>class_</var> if provided, otherwise
<tt class="class">GNUTranslations</tt>.  The class's constructor must take a single
file object argument.  If no <span class="file">.mo</span> file is found, this
function raises <tt class="exception">IOError</tt>.
</dl>

<P>
<dl><dt><b><a name='l2h-1526'><tt class='function'>install</tt></a></b> (<var>domain</var><big>[</big><var>, localedir</var><big>[</big><var>, unicode</var><big>]</big><big>]</big>)
<dd>
This installs the function <tt class="function">_</tt> in Python's builtin namespace,
based on <var>domain</var>, and <var>localedir</var> which are passed to the
function <tt class="function">translation()</tt>.  The <var>unicode</var> flag is passed to
the resulting translation object's <tt class="method">install</tt> method.

<P>
As seen below, you usually mark the strings in your application that are
candidates for translation, by wrapping them in a call to the function
<tt class="function">_()</tt>, e.g.

<P>
<dl><dd><pre class="verbatim">
print _('This string will be translated.')
</pre></dl>

<P>
For convenience, you want the <tt class="function">_()</tt> function to be installed in
Python's builtin namespace, so it is easily accessible in all modules
of your application.  
</dl>

<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot17305">... used.</A><A NAME="foot17305"
 HREF="node145.html#tex2html16" tppabs="http://www.python.org/doc/current/lib/node145.html#tex2html16"><SUP>6.2</SUP></A>
<DD>See the footnote for
<tt class="function">bindtextdomain()</tt> above.

</DL>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>

<UL>
<LI><A NAME="tex2html2666"
  HREF="node146.html" tppabs="http://www.python.org/doc/current/lib/node146.html">6.23.2.1 The <tt class="class">NullTranslations</tt> class</A>
<LI><A NAME="tex2html2667"
  HREF="node147.html" tppabs="http://www.python.org/doc/current/lib/node147.html">6.23.2.2 The <tt class="class">GNUTranslations</tt> class</A>
<LI><A NAME="tex2html2668"
  HREF="node148.html" tppabs="http://www.python.org/doc/current/lib/node148.html">6.23.2.3 Solaris message catalog support</A>
<LI><A NAME="tex2html2669"
  HREF="node149.html" tppabs="http://www.python.org/doc/current/lib/node149.html">6.23.2.4 The Catalog constructor</A>
</UL>
<!--End of Table of Child-Links-->

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node144.html" tppabs="http://www.python.org/doc/current/lib/node144.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="node146.html" tppabs="http://www.python.org/doc/current/lib/node146.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="node144.html" tppabs="http://www.python.org/doc/current/lib/node144.html">6.23.1 GNU gettext API</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="node146.html" tppabs="http://www.python.org/doc/current/lib/node146.html">6.23.2.1 The NullTranslations class</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 + =
减小字号Ctrl + -
显示快捷键?