Files
hustoj/web/python-docs/library/gettext.html
2025-03-09 17:10:56 +08:00

804 lines
87 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>gettext --- 多语种国际化服务 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="locale --- 国际化服务" href="locale.html" />
<link rel="prev" title="国际化" href="i18n.html" />
<link rel="canonical" href="https://docs.python.org/3/library/gettext.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> --- 多语种国际化服务</a><ul>
<li><a class="reference internal" href="#gnu-gettext-api">GNU <strong class="program">gettext</strong> API</a></li>
<li><a class="reference internal" href="#class-based-api">基于类的 API</a><ul>
<li><a class="reference internal" href="#the-nulltranslations-class"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code></a></li>
<li><a class="reference internal" href="#the-gnutranslations-class"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a></li>
<li><a class="reference internal" href="#solaris-message-catalog-support">Solaris 消息编目支持</a></li>
<li><a class="reference internal" href="#the-catalog-constructor">编目构造器</a></li>
</ul>
</li>
<li><a class="reference internal" href="#internationalizing-your-programs-and-modules">国际化 (I18N) 你的程序和模块</a><ul>
<li><a class="reference internal" href="#localizing-your-module">本地化你的模块</a></li>
<li><a class="reference internal" href="#localizing-your-application">本地化你的应用程序</a></li>
<li><a class="reference internal" href="#changing-languages-on-the-fly">即时更改语言</a></li>
<li><a class="reference internal" href="#deferred-translations">延迟翻译</a></li>
</ul>
</li>
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="i18n.html"
title="上一章">国际化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="locale.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> --- 国际化服务</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/gettext.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/gettext.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="locale.html" title="locale --- 国际化服务"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="i18n.html" title="国际化"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="i18n.html" accesskey="U">国际化</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-gettext">
<span id="gettext-multilingual-internationalization-services"></span><h1><a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> --- 多语种国际化服务<a class="headerlink" href="#module-gettext" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/gettext.py">Lib/gettext.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块为 Python 模块和应用程序提供国际化 (Internationalization, I18N) 和本地化 (Localization, L10N) 服务。它同时支持 GNU <strong class="program">gettext</strong> 消息编目 API 和更高级的、基于类的 API后者可能更适合于 Python 文件。下方描述的接口允许用户使用一种自然语言编写模块和应用程序消息,并提供翻译后的消息编目,以便在不同的自然语言下运行。</p>
<p>同时还给出一些本地化 Python 模块及应用程序的小技巧。</p>
<section id="gnu-gettext-api">
<h2>GNU <strong class="program">gettext</strong> API<a class="headerlink" href="#gnu-gettext-api" title="永久链接至标题"></a></h2>
<p>模块 <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 定义了下列 API这与 <strong class="program">gettext</strong> API 类似。如果你使用该 API将会对整个应用程序产生全局的影响。如果你的应用程序支持多语种而语言选择取决于用户的语言环境设置这通常正是你所想要的。而如果你正在本地化某个 Python 模块或者你的应用程序需要在运行时切换语言相反你或许想用基于类的API。</p>
<dl class="function">
<dt id="gettext.bindtextdomain">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">bindtextdomain</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">localedir=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.bindtextdomain" title="永久链接至目标"></a></dt>
<dd><p><em>domain</em> 绑定到本地目录 <em>localedir</em>。 更具体地来说,模块 <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 将使用路径 (在 Unix 系统中): <code class="file docutils literal notranslate"><em><span class="pre">localedir</span></em><span class="pre">/</span><em><span class="pre">language</span></em><span class="pre">/LC_MESSAGES/</span><em><span class="pre">domain</span></em><span class="pre">.mo</span></code> 查找二进制 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件,此处对应地查找 <em>language</em> 的位置是环境变量 <span class="target" id="index-20"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANGUAGE</span></code>, <span class="target" id="index-21"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_ALL</span></code>, <span class="target" id="index-22"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_MESSAGES</span></code><span class="target" id="index-23"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code> 中。</p>
<p>如果遗漏了 <em>localedir</em> 或者设置为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,那么将返回当前 <em>domain</em> 所绑定的值 <a class="footnote-reference brackets" href="#id3" id="id1">1</a></p>
</dd></dl>
<dl class="function">
<dt id="gettext.bind_textdomain_codeset">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">bind_textdomain_codeset</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">codeset=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.bind_textdomain_codeset" title="永久链接至目标"></a></dt>
<dd><p><em>domain</em> 绑定到 <em>codeset</em>,修改 <a class="reference internal" href="#gettext.lgettext" title="gettext.lgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgettext()</span></code></a>, <a class="reference internal" href="#gettext.ldgettext" title="gettext.ldgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ldgettext()</span></code></a>, <a class="reference internal" href="#gettext.lngettext" title="gettext.lngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">lngettext()</span></code></a><a class="reference internal" href="#gettext.ldngettext" title="gettext.ldngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ldngettext()</span></code></a> 函数返回的字节串的字符编码。如果省略了 <em>codeset</em>,则返回当前绑定的编码集。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="gettext.textdomain">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">textdomain</code><span class="sig-paren">(</span><em class="sig-param">domain=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.textdomain" title="永久链接至目标"></a></dt>
<dd><p>修改或查询当前的全局域。如果 <em>domain</em><code class="docutils literal notranslate"><span class="pre">None</span></code>,则返回当前的全局域,不为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 则将全局域设置为 <em>domain</em>,并返回它。</p>
</dd></dl>
<span class="target" id="index-4"></span><dl class="function">
<dt id="gettext.gettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">gettext</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.gettext" title="永久链接至目标"></a></dt>
<dd><p>返回 <em>message</em> 的本地化翻译,依据包括当前的全局域、语言和语言环境目录。本函数在本地命名空间中通常有别名 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> (参考下面的示例)。</p>
</dd></dl>
<dl class="function">
<dt id="gettext.dgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">dgettext</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dgettext" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#gettext.gettext" title="gettext.gettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext()</span></code></a> 类似,但在指定的 <em>domain</em> 中查找 message。</p>
</dd></dl>
<dl class="function">
<dt id="gettext.ngettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">ngettext</code><span class="sig-paren">(</span><em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.ngettext" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#gettext.gettext" title="gettext.gettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext()</span></code></a> 类似,但考虑了复数形式。如果找到了翻译,则将 <em>n</em> 代入复数公式,然后返回得出的消息(某些语言具有两种以上的复数形式)。如果未找到翻译,则 <em>n</em> 为 1 时返回 <em>singular</em>,为其他数时返回 <em>plural</em></p>
<p>复数公式取自编目头文件。它是 C 或 Python 表达式,有一个自变量 <em>n</em>,该表达式计算的是所需复数形式在编目中的索引号。关于在 <code class="file docutils literal notranslate"><span class="pre">.po</span></code> 文件中使用的确切语法和各种语言的公式,请参阅 <a class="reference external" href="https://www.gnu.org/software/gettext/manual/gettext.html">GNU gettext 文档</a></p>
</dd></dl>
<dl class="function">
<dt id="gettext.dngettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">dngettext</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dngettext" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#gettext.ngettext" title="gettext.ngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ngettext()</span></code></a> 类似,但在指定的 <em>domain</em> 中查找 message。</p>
</dd></dl>
<dl class="function">
<dt id="gettext.pgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">pgettext</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.pgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gettext.dpgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">dpgettext</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">context</em>, <em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dpgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gettext.npgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">npgettext</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.npgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gettext.dnpgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">dnpgettext</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">context</em>, <em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dnpgettext" title="永久链接至目标"></a></dt>
<dd><p>与前缀中没有 <code class="docutils literal notranslate"><span class="pre">p</span></code> 的相应函数类似(即 <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext()</span></code></a>, <a class="reference internal" href="#gettext.dgettext" title="gettext.dgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dgettext()</span></code></a>, <a class="reference internal" href="#gettext.ngettext" title="gettext.ngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ngettext()</span></code></a>, <a class="reference internal" href="#gettext.dngettext" title="gettext.dngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dngettext()</span></code></a> ),但是仅翻译给定的 message <em>context</em></p>
<div class="versionadded">
<p><span class="versionmodified added">3.8 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="gettext.lgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">lgettext</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.lgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gettext.ldgettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">ldgettext</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.ldgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gettext.lngettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">lngettext</code><span class="sig-paren">(</span><em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.lngettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gettext.ldngettext">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">ldngettext</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.ldngettext" title="永久链接至目标"></a></dt>
<dd><p>与前缀中没有 <code class="docutils literal notranslate"><span class="pre">l</span></code> 的相应函数等效( <a class="reference internal" href="#gettext.gettext" title="gettext.gettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext()</span></code></a>, <a class="reference internal" href="#gettext.dgettext" title="gettext.dgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dgettext()</span></code></a>, <a class="reference internal" href="#gettext.ngettext" title="gettext.ngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ngettext()</span></code></a><a class="reference internal" href="#gettext.dngettext" title="gettext.dngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dngettext()</span></code></a> ),但是如果没有用 <a class="reference internal" href="#gettext.bind_textdomain_codeset" title="gettext.bind_textdomain_codeset"><code class="xref py py-func docutils literal notranslate"><span class="pre">bind_textdomain_codeset()</span></code></a> 显式设置其他编码,则返回的翻译将以首选系统编码来编码字节串。</p>
<div class="admonition warning">
<p class="admonition-title">警告</p>
<p>在 Python 3 中应避免使用这些函数,因为它们返回的是编码后的字节串。最好使用返回 Unicode 字符串的其他方法,因为大多数 Python 应用程序都希望将人类可读的文本作为字符串而不是字节来处理。此外,如果翻译后的字符串存在编码问题,则可能会意外出现与 Unicode 相关的异常。</p>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10.</span></p>
</div>
</dd></dl>
<p>注意GNU <strong class="program">gettext</strong> 还定义了 <code class="xref py py-func docutils literal notranslate"><span class="pre">dcgettext()</span></code> 方法,但它被认为不实用,因此目前没有实现它。</p>
<p>这是该 API 的典型用法示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">gettext</span><span class="o">.</span><span class="n">bindtextdomain</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">,</span> <span class="s1">&#39;/path/to/my/language/directory&#39;</span><span class="p">)</span>
<span class="n">gettext</span><span class="o">.</span><span class="n">textdomain</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">)</span>
<span class="n">_</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">gettext</span>
<span class="c1"># ...</span>
<span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;This is a translatable string.&#39;</span><span class="p">))</span>
</pre></div>
</div>
</section>
<section id="class-based-api">
<h2>基于类的 API<a class="headerlink" href="#class-based-api" title="永久链接至标题"></a></h2>
<p>与 GNU <strong class="program">gettext</strong> API 相比,<a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块的基于类的 API 提供了更多的灵活性和更强的便利性。这是本地化 Python 应用程序和模块的推荐方法。<code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> 定义了一个 <a class="reference internal" href="#gettext.GNUTranslations" title="gettext.GNUTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a> 类,该类实现了 GNU <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 格式文件的解析,并且具有用于返回字符串的方法。本类的实例也可以将自身作为函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 安装到内建命名空间中。</p>
<dl class="function">
<dt id="gettext.find">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">find</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">localedir=None</em>, <em class="sig-param">languages=None</em>, <em class="sig-param">all=False</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.find" title="永久链接至目标"></a></dt>
<dd><p>本函数实现了标准的 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件搜索算法。它接受一个 <em>domain</em>,它与 <a class="reference internal" href="#gettext.textdomain" title="gettext.textdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">textdomain()</span></code></a> 接受的域相同。可选参数 <em>localedir</em><a class="reference internal" href="#gettext.bindtextdomain" title="gettext.bindtextdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">bindtextdomain()</span></code></a> 中的相同。可选参数 <em>languages</em> 是多条字符串的列表,其中每条字符串都是一种语言代码。</p>
<p>如果没有传入 <em>localedir</em>,则使用默认的系统语言环境目录。 <a class="footnote-reference brackets" href="#id4" id="id2">2</a> 如果没有传入 <em>languages</em>,则搜索以下环境变量:<span class="target" id="index-24"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANGUAGE</span></code><span class="target" id="index-25"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_ALL</span></code><span class="target" id="index-26"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_MESSAGES</span></code><span class="target" id="index-27"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code>。从这些变量返回的第一个非空值将用作 <em>languages</em> 变量。环境变量应包含一个语言列表,由冒号分隔,该列表会被按冒号拆分,以产生所需的语言代码字符串列表。</p>
<p><a class="reference internal" href="#gettext.find" title="gettext.find"><code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code></a> 将扩展并规范化 language然后遍历它们搜索由这些组件构建的现有文件:</p>
<p><code class="file docutils literal notranslate"><em><span class="pre">localedir</span></em><span class="pre">/</span><em><span class="pre">language</span></em><span class="pre">/LC_MESSAGES/</span><em><span class="pre">domain</span></em><span class="pre">.mo</span></code></p>
<p><a class="reference internal" href="#gettext.find" title="gettext.find"><code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code></a> 返回找到类似的第一个文件名。如果找不到这样的文件,则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。如果传入了 <em>all</em>,它将返回一个列表,包含所有文件名,并按它们在语言列表或环境变量中出现的顺序排列。</p>
</dd></dl>
<dl class="function">
<dt id="gettext.translation">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">translation</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">localedir=None</em>, <em class="sig-param">languages=None</em>, <em class="sig-param">class_=None</em>, <em class="sig-param">fallback=False</em>, <em class="sig-param">codeset=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.translation" title="永久链接至目标"></a></dt>
<dd><p>根据 <em>domain</em><em>localedir</em><em>languages</em>,返回 <code class="xref py py-class docutils literal notranslate"><span class="pre">*Translations</span></code> 实例,首先应将前述参数传入 <a class="reference internal" href="#gettext.find" title="gettext.find"><code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code></a> 以获取关联的 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件路径的列表。名字与 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件名相同的实例将被缓存。如果传入 <em>class_</em>,它将是实际被实例化的类,否则实例化 <a class="reference internal" href="#gettext.GNUTranslations" title="gettext.GNUTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a>。类的构造函数必须只接受一个 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a> 参数。如果传入 <em>codeset</em>,那么在 <a class="reference internal" href="#gettext.NullTranslations.lgettext" title="gettext.NullTranslations.lgettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lgettext()</span></code></a><a class="reference internal" href="#gettext.NullTranslations.lngettext" title="gettext.NullTranslations.lngettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lngettext()</span></code></a> 方法中,对翻译后的字符串进行编码的字符集将被改变。</p>
<p>如果找到多个文件,后找到的文件将用作先前文件的替补。为了设置替补,将使用 <a class="reference internal" href="copy.html#copy.copy" title="copy.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.copy()</span></code></a> 从缓存中克隆每个 translation 对象。实际的实例数据仍在缓存中共享。</p>
<p>如果 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件未找到,且 <em>fallback</em> 为 false默认值则本函数引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 异常,如果 <em>fallback</em> 为 true则返回一个 <a class="reference internal" href="#gettext.NullTranslations" title="gettext.NullTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code></a> 实例。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>曾经是 <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> 被引发而不是 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a></p>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>codeset</em> 参数。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="gettext.install">
<code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">install</code><span class="sig-paren">(</span><em class="sig-param">domain</em>, <em class="sig-param">localedir=None</em>, <em class="sig-param">codeset=None</em>, <em class="sig-param">names=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.install" title="永久链接至目标"></a></dt>
<dd><p>根据传入 <a class="reference internal" href="#gettext.translation" title="gettext.translation"><code class="xref py py-func docutils literal notranslate"><span class="pre">translation()</span></code></a> 函数的 <em>domain</em><em>localedir</em><em>codeset</em>,在 Python 内建命名空间中安装 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 函数。</p>
<p><em>names</em> 参数的信息请参阅 translation 对象的 <a class="reference internal" href="#gettext.NullTranslations.install" title="gettext.NullTranslations.install"><code class="xref py py-meth docutils literal notranslate"><span class="pre">install()</span></code></a> 方法的描述。</p>
<p>如下所示,通常将字符串包括在 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 函数的调用中,以标记应用程序中待翻译的字符串,就像这样:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;This string will be translated.&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>为了方便,一般将 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 函数安装在 Python 内建命名空间中,以便在应用程序的所有模块中轻松访问它。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10: </span><em>codeset</em> 参数。</p>
</div>
</dd></dl>
<section id="the-nulltranslations-class">
<h3><a class="reference internal" href="#gettext.NullTranslations" title="gettext.NullTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code></a><a class="headerlink" href="#the-nulltranslations-class" title="永久链接至标题"></a></h3>
<p>translation 类实际实现的是,将原始源文件消息字符串转换为已翻译的消息字符串。所有 translation 类使用的基类为 <a class="reference internal" href="#gettext.NullTranslations" title="gettext.NullTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code></a>,它提供了基本的接口,可用于编写自己定制的 translation 类。以下是 <code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code> 的方法:</p>
<dl class="class">
<dt id="gettext.NullTranslations">
<em class="property">class </em><code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">NullTranslations</code><span class="sig-paren">(</span><em class="sig-param">fp=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations" title="永久链接至目标"></a></dt>
<dd><p>接受一个可选参数 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a> <em>fp</em>,该参数会被基类忽略。初始化由派生类设置的 &quot;protected&quot; (受保护的)实例变量 <em>_info</em><em>_charset</em>,与 <em>_fallback</em> 类似,但它是通过 <a class="reference internal" href="#gettext.NullTranslations.add_fallback" title="gettext.NullTranslations.add_fallback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_fallback()</span></code></a> 来设置的。如果 <em>fp</em> 不为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,就会调用 <code class="docutils literal notranslate"><span class="pre">self._parse(fp)</span></code></p>
<dl class="method">
<dt id="gettext.NullTranslations._parse">
<code class="sig-name descname">_parse</code><span class="sig-paren">(</span><em class="sig-param">fp</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations._parse" title="永久链接至目标"></a></dt>
<dd><p>在基类中没有操作,本方法接受文件对象 <em>fp</em>,从该文件读取数据,用来初始化消息编目。如果你手头的消息编目文件的格式不受支持,则应重写本方法来解析你的格式。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.add_fallback">
<code class="sig-name descname">add_fallback</code><span class="sig-paren">(</span><em class="sig-param">fallback</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.add_fallback" title="永久链接至目标"></a></dt>
<dd><p>添加 <em>fallback</em> 为当前 translation 对象的替补对象。如果 translation 对象无法为指定消息提供翻译,则应向替补查询。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.gettext">
<code class="sig-name descname">gettext</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.gettext" title="永久链接至目标"></a></dt>
<dd><p>如果设置了替补,则转发 <code class="xref py py-meth docutils literal notranslate"><span class="pre">gettext()</span></code> 给替补。否则返回 <em>message</em>。在派生类中被重写。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.ngettext">
<code class="sig-name descname">ngettext</code><span class="sig-paren">(</span><em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.ngettext" title="永久链接至目标"></a></dt>
<dd><p>如果设置了替补,则转发 <code class="xref py py-meth docutils literal notranslate"><span class="pre">ngettext()</span></code> 给替补。否则,<em>n</em> 为 1 时返回 <em>singular</em>,为其他时返回 <em>plural</em>。在派生类中被重写。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.pgettext">
<code class="sig-name descname">pgettext</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.pgettext" title="永久链接至目标"></a></dt>
<dd><p>如果设置了替补,则转发 <a class="reference internal" href="#gettext.pgettext" title="gettext.pgettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pgettext()</span></code></a> 给替补。否则返回已翻译的消息。在派生类中被重写。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.8 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.npgettext">
<code class="sig-name descname">npgettext</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.npgettext" title="永久链接至目标"></a></dt>
<dd><p>如果设置了替补,则转发 <a class="reference internal" href="#gettext.npgettext" title="gettext.npgettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">npgettext()</span></code></a> 给替补。否则返回已翻译的消息。在派生类中被重写。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.8 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.lgettext">
<code class="sig-name descname">lgettext</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.lgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.lngettext">
<code class="sig-name descname">lngettext</code><span class="sig-paren">(</span><em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.lngettext" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#gettext.NullTranslations.gettext" title="gettext.NullTranslations.gettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gettext()</span></code></a><a class="reference internal" href="#gettext.NullTranslations.ngettext" title="gettext.NullTranslations.ngettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ngettext()</span></code></a> 等效,但是如果没有用 <a class="reference internal" href="#gettext.NullTranslations.set_output_charset" title="gettext.NullTranslations.set_output_charset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_output_charset()</span></code></a> 显式设置编码,则返回的翻译将以首选系统编码来编码字节串。在派生类中被重写。</p>
<div class="admonition warning">
<p class="admonition-title">警告</p>
<p>应避免在 Python 3 中使用这些方法。请参阅 <a class="reference internal" href="#gettext.lgettext" title="gettext.lgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgettext()</span></code></a> 函数的警告。</p>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.info">
<code class="sig-name descname">info</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.info" title="永久链接至目标"></a></dt>
<dd><p>返回 &quot;protected&quot;(受保护的) <code class="xref py py-attr docutils literal notranslate"><span class="pre">_info</span></code> 变量,它是一个字典,包含在消息编目文件中找到的元数据。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.charset">
<code class="sig-name descname">charset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.charset" title="永久链接至目标"></a></dt>
<dd><p>返回消息编目文件的编码。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.output_charset">
<code class="sig-name descname">output_charset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.output_charset" title="永久链接至目标"></a></dt>
<dd><p>返回由 <a class="reference internal" href="#gettext.NullTranslations.lgettext" title="gettext.NullTranslations.lgettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lgettext()</span></code></a><a class="reference internal" href="#gettext.NullTranslations.lngettext" title="gettext.NullTranslations.lngettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lngettext()</span></code></a> 翻译出的消息的编码。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.set_output_charset">
<code class="sig-name descname">set_output_charset</code><span class="sig-paren">(</span><em class="sig-param">charset</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.set_output_charset" title="永久链接至目标"></a></dt>
<dd><p>更改翻译出的消息的编码。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.NullTranslations.install">
<code class="sig-name descname">install</code><span class="sig-paren">(</span><em class="sig-param">names=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.install" title="永久链接至目标"></a></dt>
<dd><p>本方法将 <a class="reference internal" href="#gettext.NullTranslations.gettext" title="gettext.NullTranslations.gettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gettext()</span></code></a> 安装至内建命名空间,并绑定为 <code class="docutils literal notranslate"><span class="pre">_</span></code></p>
<p>如果传入 <em>names</em> 参数,该参数必须是一个序列,包含除 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 外其他要安装在内建命名空间中的函数的名称。支持的名称有 <code class="docutils literal notranslate"><span class="pre">'gettext'</span></code>, <code class="docutils literal notranslate"><span class="pre">'ngettext'</span></code>, <code class="docutils literal notranslate"><span class="pre">'pgettext'</span></code>, <code class="docutils literal notranslate"><span class="pre">'npgettext'</span></code>, <code class="docutils literal notranslate"><span class="pre">'lgettext'</span></code><code class="docutils literal notranslate"><span class="pre">'lngettext'</span></code></p>
<p>注意,这仅仅是使 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 函数在应用程序中生效的一种方法,尽管也是最方便的方法。由于它会影响整个应用程序全局,特别是内建命名空间,因此已经本地化的模块不应该安装 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code>,而是应该用下列代码使 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 在各自模块中生效:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">translation</span><span class="p">(</span><span class="s1">&#39;mymodule&#39;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="n">_</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">gettext</span>
</pre></div>
</div>
<p>这只将 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 放在其模块的全局命名空间中,所以只影响其模块内的调用。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.8 版更改: </span>添加了 <code class="docutils literal notranslate"><span class="pre">'pgettext'</span></code><code class="docutils literal notranslate"><span class="pre">'npgettext'</span></code></p>
</div>
</dd></dl>
</dd></dl>
</section>
<section id="the-gnutranslations-class">
<h3><a class="reference internal" href="#gettext.GNUTranslations" title="gettext.GNUTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a><a class="headerlink" href="#the-gnutranslations-class" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块提供了一个派生自 <a class="reference internal" href="#gettext.NullTranslations" title="gettext.NullTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code></a> 的其他类:<a class="reference internal" href="#gettext.GNUTranslations" title="gettext.GNUTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a>。该类重写了 <code class="xref py py-meth docutils literal notranslate"><span class="pre">_parse()</span></code>,同时能以大端序和小端序格式读取 GNU <strong class="program">gettext</strong> 格式的 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件。</p>
<p><a class="reference internal" href="#gettext.GNUTranslations" title="gettext.GNUTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a> 从翻译编目中解析出可选的元数据。GNU <strong class="program">gettext</strong> 约定,将元数据包括在空字符串的翻译中。该元数据采用 <span class="target" id="index-28"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> 样式的 <code class="docutils literal notranslate"><span class="pre">key:</span> <span class="pre">value</span></code> 键值对,且应该包含 <code class="docutils literal notranslate"><span class="pre">Project-Id-Version</span></code> 键。如果找到 <code class="docutils literal notranslate"><span class="pre">Content-Type</span></code> 键,那么将用 <code class="docutils literal notranslate"><span class="pre">charset</span></code> 属性去初始化 &quot;protected&quot;(受保护的) <code class="xref py py-attr docutils literal notranslate"><span class="pre">_charset</span></code> 实例变量,而该变量在未找到键的情况下默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。如果指定了字符编码,那么从编目中读取的所有消息 ID 和消息字符串都将使用此编码转换为 Unicode若未指定编码则假定编码为 ASCII。</p>
<p>由于消息 ID 也是作为 Unicode 字符串读取的,因此所有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">*gettext()</span></code> 方法都假定消息 ID 为 Unicode 字符串,而不是字节串。</p>
<p>整个键/值对集合将被放入一个字典,并设置为 &quot;protected&quot;(受保护的) <code class="xref py py-attr docutils literal notranslate"><span class="pre">_info</span></code> 实例变量。</p>
<p>如果 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件的魔法值 (magic number) 无效,或遇到意外的主版本号,或在读取文件时发生其他问题,则实例化 <a class="reference internal" href="#gettext.GNUTranslations" title="gettext.GNUTranslations"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a> 类会引发 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a></p>
<dl class="class">
<dt id="gettext.GNUTranslations">
<em class="property">class </em><code class="sig-prename descclassname">gettext.</code><code class="sig-name descname">GNUTranslations</code><a class="headerlink" href="#gettext.GNUTranslations" title="永久链接至目标"></a></dt>
<dd><p>下列方法是根据基类实现重写的:</p>
<dl class="method">
<dt id="gettext.GNUTranslations.gettext">
<code class="sig-name descname">gettext</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.gettext" title="永久链接至目标"></a></dt>
<dd><p>在编目中查找 <em>message</em> ID并以 Unicode 字符串形式返回相应的消息字符串。如果在编目中没有 <em>message</em> ID 条目,且配置了替补,则查找请求将被转发到替补的 <a class="reference internal" href="#gettext.NullTranslations.gettext" title="gettext.NullTranslations.gettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gettext()</span></code></a> 方法。否则,返回 <em>message</em> ID。</p>
</dd></dl>
<dl class="method">
<dt id="gettext.GNUTranslations.ngettext">
<code class="sig-name descname">ngettext</code><span class="sig-paren">(</span><em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.ngettext" title="永久链接至目标"></a></dt>
<dd><p>查找消息 ID 的复数形式。<em>singular</em> 用作消息 ID用于在编目中查找同时 <em>n</em> 用于确定使用哪种复数形式。返回的消息字符串是 Unicode 字符串。</p>
<p>如果在编目中没有找到消息 ID且配置了替补则查找请求将被转发到替补的 <a class="reference internal" href="#gettext.NullTranslations.ngettext" title="gettext.NullTranslations.ngettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ngettext()</span></code></a> 方法。否则,当 <em>n</em> 为 1 时返回 <em>singular</em>,其他情况返回 <em>plural</em></p>
<p>例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">))</span>
<span class="n">cat</span> <span class="o">=</span> <span class="n">GNUTranslations</span><span class="p">(</span><span class="n">somefile</span><span class="p">)</span>
<span class="n">message</span> <span class="o">=</span> <span class="n">cat</span><span class="o">.</span><span class="n">ngettext</span><span class="p">(</span>
<span class="s1">&#39;There is </span><span class="si">%(num)d</span><span class="s1"> file in this directory&#39;</span><span class="p">,</span>
<span class="s1">&#39;There are </span><span class="si">%(num)d</span><span class="s1"> files in this directory&#39;</span><span class="p">,</span>
<span class="n">n</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;num&#39;</span><span class="p">:</span> <span class="n">n</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.GNUTranslations.pgettext">
<code class="sig-name descname">pgettext</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.pgettext" title="永久链接至目标"></a></dt>
<dd><p>在编目中查找 <em>context</em><em>message</em> ID并以 Unicode 字符串形式返回相应的消息字符串。如果在编目中没有 <em>message</em> ID 和 <em>context</em> 条目,且配置了替补,则查找请求将被转发到替补的 <a class="reference internal" href="#gettext.pgettext" title="gettext.pgettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pgettext()</span></code></a> 方法。否则,返回 <em>message</em> ID。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.8 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.GNUTranslations.npgettext">
<code class="sig-name descname">npgettext</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.npgettext" title="永久链接至目标"></a></dt>
<dd><p>查找消息 ID 的复数形式。<em>singular</em> 用作消息 ID用于在编目中查找同时 <em>n</em> 用于确定使用哪种复数形式。</p>
<p>如果在编目中没有找到 <em>context</em> 对应的消息 ID且配置了替补则查找请求将被转发到替补的 <a class="reference internal" href="#gettext.npgettext" title="gettext.npgettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">npgettext()</span></code></a> 方法。否则,当 <em>n</em> 为 1 时返回 <em>singular</em>,其他情况返回 <em>plural</em></p>
<div class="versionadded">
<p><span class="versionmodified added">3.8 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="gettext.GNUTranslations.lgettext">
<code class="sig-name descname">lgettext</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.lgettext" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="method">
<dt id="gettext.GNUTranslations.lngettext">
<code class="sig-name descname">lngettext</code><span class="sig-paren">(</span><em class="sig-param">singular</em>, <em class="sig-param">plural</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.lngettext" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#gettext.GNUTranslations.gettext" title="gettext.GNUTranslations.gettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gettext()</span></code></a><a class="reference internal" href="#gettext.GNUTranslations.ngettext" title="gettext.GNUTranslations.ngettext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ngettext()</span></code></a> 等效,但是如果没有用 <a class="reference internal" href="#gettext.NullTranslations.set_output_charset" title="gettext.NullTranslations.set_output_charset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_output_charset()</span></code></a> 显式设置编码,则返回的翻译将以首选系统编码来编码字节串。</p>
<div class="admonition warning">
<p class="admonition-title">警告</p>
<p>应避免在 Python 3 中使用这些方法。请参阅 <a class="reference internal" href="#gettext.lgettext" title="gettext.lgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgettext()</span></code></a> 函数的警告。</p>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.8, will be removed in version 3.10.</span></p>
</div>
</dd></dl>
</dd></dl>
</section>
<section id="solaris-message-catalog-support">
<h3>Solaris 消息编目支持<a class="headerlink" href="#solaris-message-catalog-support" title="永久链接至标题"></a></h3>
<p>Solaris 操作系统定义了自己的二进制 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件格式,但由于找不到该格式的文档,因此目前不支持该格式。</p>
</section>
<section id="the-catalog-constructor">
<h3>编目构造器<a class="headerlink" href="#the-catalog-constructor" title="永久链接至标题"></a></h3>
<p id="index-10">GNOME 用的 <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块是 James Henstridge 写的版本,但该版本的 API 略有不同。它文档中的用法是:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">cat</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">Catalog</span><span class="p">(</span><span class="n">domain</span><span class="p">,</span> <span class="n">localedir</span><span class="p">)</span>
<span class="n">_</span> <span class="o">=</span> <span class="n">cat</span><span class="o">.</span><span class="n">gettext</span>
<span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>为了与本模块的旧版本兼容,<code class="xref py py-func docutils literal notranslate"><span class="pre">Catalog()</span></code> 函数是上述 <a class="reference internal" href="#gettext.translation" title="gettext.translation"><code class="xref py py-func docutils literal notranslate"><span class="pre">translation()</span></code></a> 函数的别名。</p>
<p>本模块与 Henstridge 的模块有一个区别:他的编目对象支持通过映射 API 进行访问,但是该特性似乎从未使用过,因此目前不支持该特性。</p>
</section>
</section>
<section id="internationalizing-your-programs-and-modules">
<h2>国际化 (I18N) 你的程序和模块<a class="headerlink" href="#internationalizing-your-programs-and-modules" title="永久链接至标题"></a></h2>
<p>国际化 (I18N) 是指使程序可切换多种语言的操作。本地化 (L10N) 是指程序的适配能力,一旦程序被国际化,就能适配当地的语言和文化习惯。为了向 Python 程序提供不同语言的消息,需要执行以下步骤:</p>
<ol class="arabic simple">
<li><p>准备程序或模块,将可翻译的字符串特别标记起来</p></li>
<li><p>在已标记的文件上运行一套工具,用来生成原始消息编目</p></li>
<li><p>创建消息编目的不同语言的翻译</p></li>
<li><p>使用 <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块,以便正确翻译消息字符串</p></li>
</ol>
<p>为了准备代码以达成 I18N需要巡视文件中的所有字符串。所有要翻译的字符串都应包括在 <code class="docutils literal notranslate"><span class="pre">_('...')</span></code> 内,以此打上标记——也就是调用 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 函数。例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;mylog.txt&#39;</span>
<span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;writing a log message&#39;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
<span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
</pre></div>
</div>
<p>在这个例子中,字符串 <code class="docutils literal notranslate"><span class="pre">'writing</span> <span class="pre">a</span> <span class="pre">log</span> <span class="pre">message'</span></code> 被标记为待翻译,而字符串 <code class="docutils literal notranslate"><span class="pre">'mylog.txt'</span></code><code class="docutils literal notranslate"><span class="pre">'w'</span></code> 没有被标记。</p>
<p>有一些工具可以将待翻译的字符串提取出来。原版的 GNU <strong class="program">gettext</strong> 仅支持 C 或 C++ 源代码,但其扩展版 <strong class="program">xgettext</strong> 可以扫描多种语言的代码(包括 Python来找出标记为可翻译的字符串。<a class="reference external" href="http://babel.pocoo.org/">Babel</a> 是一个 Python 国际化库,其中包含一个 <code class="file docutils literal notranslate"><span class="pre">pybabel</span></code> 脚本用于提取并编译消息编目。François Pinard 写的称为 <strong class="program">xpot</strong> 的程序也能完成类似的工作,可从他的 <a class="reference external" href="https://github.com/pinard/po-utils">po-utils 软件包</a> 中获取。</p>
<p>Python 还包括了这些程序的纯 Python 版本,称为 <strong class="program">pygettext.py</strong><strong class="program">msgfmt.py</strong>,某些 Python 发行版已经安装了它们。<strong class="program">pygettext.py</strong> 类似于 <strong class="program">xgettext</strong>,但只能理解 Python 源代码,无法处理诸如 C 或 C++ 的其他编程语言。<strong class="program">pygettext.py</strong> 支持的命令行界面类似于 <strong class="program">xgettext</strong>,查看其详细用法请运行 <code class="docutils literal notranslate"><span class="pre">pygettext.py</span> <span class="pre">--help</span></code><strong class="program">msgfmt.py</strong> 与 GNU <strong class="program">msgfmt</strong> 是二进制兼容的。有了这两个程序,可以不需要 GNU <strong class="program">gettext</strong> 包来国际化 Python 应用程序。)</p>
<p><strong class="program">xgettext</strong><strong class="program">pygettext</strong> 或类似工具生成的 <code class="file docutils literal notranslate"><span class="pre">.po</span></code> 文件就是消息编目。它们是结构化的人类可读文件,包含源代码中所有被标记的字符串,以及这些字符串的翻译的占位符。</p>
<p>然后把这些 <code class="file docutils literal notranslate"><span class="pre">.po</span></code> 文件的副本交给各个人工译者,他们为所支持的每种自然语言编写翻译。译者以 <code class="file docutils literal notranslate"><span class="pre">&lt;语言名称&gt;.po</span></code> 文件的形式发送回翻译完的某个语言的版本,将该文件用 <strong class="program">msgfmt</strong> 程序编译为机器可读的 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 二进制编目文件。<a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块使用 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件在运行时进行实际的翻译处理。</p>
<p>如何在代码中使用 <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块取决于国际化单个模块还是整个应用程序。接下来的两节将讨论每种情况。</p>
<section id="localizing-your-module">
<h3>本地化你的模块<a class="headerlink" href="#localizing-your-module" title="永久链接至标题"></a></h3>
<p>如果要本地化模块,则切忌进行全局性的更改,如更改内建命名空间。不应使用 GNU <strong class="program">gettext</strong> API而应使用基于类的 API。</p>
<p>假设你的模块叫做 &quot;spam&quot;,并且该模块的各种自然语言翻译 <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> 文件存放于 <code class="file docutils literal notranslate"><span class="pre">/usr/share/locale</span></code>,为 GNU <strong class="program">gettext</strong> 格式。以下内容应放在模块顶部:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">translation</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;/usr/share/locale&#39;</span><span class="p">)</span>
<span class="n">_</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">gettext</span>
</pre></div>
</div>
</section>
<section id="localizing-your-application">
<h3>本地化你的应用程序<a class="headerlink" href="#localizing-your-application" title="永久链接至标题"></a></h3>
<p>如果正在本地化应用程序,可以将 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 函数全局安装到内建命名空间中,通常在应用程序的主文件中安装。这将使某个应用程序的所有文件都能使用 <code class="docutils literal notranslate"><span class="pre">_('...')</span></code>,而不必在每个文件中显式安装它。</p>
<p>最简单的情况,就只需将以下代码添加到应用程序的主程序文件中:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">gettext</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果需要设置语言环境目录,可以将其传递给 <a class="reference internal" href="#gettext.install" title="gettext.install"><code class="xref py py-func docutils literal notranslate"><span class="pre">install()</span></code></a> 函数:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">gettext</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">,</span> <span class="s1">&#39;/usr/share/locale&#39;</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="changing-languages-on-the-fly">
<h3>即时更改语言<a class="headerlink" href="#changing-languages-on-the-fly" title="永久链接至标题"></a></h3>
<p>如果程序需要同时支持多种语言,则可能需要创建多个翻译实例,然后在它们之间进行显式切换,如下所示:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gettext</span>
<span class="n">lang1</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">translation</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">,</span> <span class="n">languages</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;en&#39;</span><span class="p">])</span>
<span class="n">lang2</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">translation</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">,</span> <span class="n">languages</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;fr&#39;</span><span class="p">])</span>
<span class="n">lang3</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">translation</span><span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">,</span> <span class="n">languages</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;de&#39;</span><span class="p">])</span>
<span class="c1"># start by using language1</span>
<span class="n">lang1</span><span class="o">.</span><span class="n">install</span><span class="p">()</span>
<span class="c1"># ... time goes by, user selects language 2</span>
<span class="n">lang2</span><span class="o">.</span><span class="n">install</span><span class="p">()</span>
<span class="c1"># ... more time goes by, user selects language 3</span>
<span class="n">lang3</span><span class="o">.</span><span class="n">install</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="deferred-translations">
<h3>延迟翻译<a class="headerlink" href="#deferred-translations" title="永久链接至标题"></a></h3>
<p>在大多数代码中,字符串会在编写位置进行翻译。但偶尔需要将字符串标记为待翻译,实际翻译却推迟到后面。一个典型的例子是:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">animals</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;mollusk&#39;</span><span class="p">,</span>
<span class="s1">&#39;albatross&#39;</span><span class="p">,</span>
<span class="s1">&#39;rat&#39;</span><span class="p">,</span>
<span class="s1">&#39;penguin&#39;</span><span class="p">,</span>
<span class="s1">&#39;python&#39;</span><span class="p">,</span> <span class="p">]</span>
<span class="c1"># ...</span>
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">animals</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<p>此处希望将 <code class="docutils literal notranslate"><span class="pre">animals</span></code> 列表中的字符串标记为可翻译,但不希望在打印之前对它们进行翻译。</p>
<p>这是处理该情况的一种方式:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">_</span><span class="p">(</span><span class="n">message</span><span class="p">):</span> <span class="k">return</span> <span class="n">message</span>
<span class="n">animals</span> <span class="o">=</span> <span class="p">[</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;mollusk&#39;</span><span class="p">),</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;albatross&#39;</span><span class="p">),</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;rat&#39;</span><span class="p">),</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;penguin&#39;</span><span class="p">),</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;python&#39;</span><span class="p">),</span> <span class="p">]</span>
<span class="k">del</span> <span class="n">_</span>
<span class="c1"># ...</span>
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">animals</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
</pre></div>
</div>
<p>该方法之所以可行,是因为 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 的虚定义只是简单地返回了原本的字符串。并且该虚定义将临时覆盖内建命名空间中 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 的定义(直到 <a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> 命令)。即使先前在本地命名空间中已经有了 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 的定义也请注意。</p>
<p>注意,第二次使用 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 不会认为 &quot;a&quot; 可以传递给 <strong class="program">gettext</strong> 程序去翻译,因为该参数不是字符串文字。</p>
<p>解决该问题的另一种方法是下面这个例子:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">N_</span><span class="p">(</span><span class="n">message</span><span class="p">):</span> <span class="k">return</span> <span class="n">message</span>
<span class="n">animals</span> <span class="o">=</span> <span class="p">[</span><span class="n">N_</span><span class="p">(</span><span class="s1">&#39;mollusk&#39;</span><span class="p">),</span>
<span class="n">N_</span><span class="p">(</span><span class="s1">&#39;albatross&#39;</span><span class="p">),</span>
<span class="n">N_</span><span class="p">(</span><span class="s1">&#39;rat&#39;</span><span class="p">),</span>
<span class="n">N_</span><span class="p">(</span><span class="s1">&#39;penguin&#39;</span><span class="p">),</span>
<span class="n">N_</span><span class="p">(</span><span class="s1">&#39;python&#39;</span><span class="p">),</span> <span class="p">]</span>
<span class="c1"># ...</span>
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">animals</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
</pre></div>
</div>
<p>这种情况下标记可翻译的字符串使用的是函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">N_()</span></code>,该函数不会与 <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> 的任何定义冲突。但是,需要教会消息提取程序去寻找用 <code class="xref py py-func docutils literal notranslate"><span class="pre">N_()</span></code> 标记的可翻译字符串。<strong class="program">xgettext</strong>, <strong class="program">pygettext</strong>, <code class="docutils literal notranslate"><span class="pre">pybabel</span> <span class="pre">extract</span></code><strong class="program">xpot</strong> 都支持此功能,使用 <code class="xref std std-option docutils literal notranslate"><span class="pre">-k</span></code> 命令行开关即可。这里选择 <code class="xref py py-func docutils literal notranslate"><span class="pre">N_()</span></code> 为名称完全是任意的,它也能轻易改为 <code class="xref py py-func docutils literal notranslate"><span class="pre">MarkThisStringForTranslation()</span></code></p>
</section>
</section>
<section id="acknowledgements">
<h2>致谢<a class="headerlink" href="#acknowledgements" title="永久链接至标题"></a></h2>
<p>以下人员为创建此模块贡献了代码、反馈、设计建议、早期实现和宝贵的经验:</p>
<ul class="simple">
<li><p>Peter Funk</p></li>
<li><p>James Henstridge</p></li>
<li><p>Juan David Ibáñez Palomar</p></li>
<li><p>Marc-André Lemburg</p></li>
<li><p>Martin von Löwis</p></li>
<li><p>François Pinard</p></li>
<li><p>Barry Warsaw</p></li>
<li><p>Gustavo Niemeyer</p></li>
</ul>
<p class="rubric">备注</p>
<dl class="footnote brackets">
<dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>不同系统的默认语言环境目录是不同的;比如在 RedHat Linux 上是 <code class="file docutils literal notranslate"><span class="pre">/usr/share/locale</span></code>,在 Solaris 上是 <code class="file docutils literal notranslate"><span class="pre">/usr/lib/locale</span></code><a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块不会支持这些基于不同系统的默认值;而它的默认值为 <code class="file docutils literal notranslate"><em><span class="pre">sys.base_prefix</span></em><span class="pre">/share/locale</span></code> (请参阅 <a class="reference internal" href="sys.html#sys.base_prefix" title="sys.base_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.base_prefix</span></code></a>)。基于上述原因,最好每次都在应用程序的开头使用明确的绝对路径来调用 <a class="reference internal" href="#gettext.bindtextdomain" title="gettext.bindtextdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">bindtextdomain()</span></code></a></p>
</dd>
<dt class="label" id="id4"><span class="brackets"><a class="fn-backref" href="#id2">2</a></span></dt>
<dd><p>参阅上方 <a class="reference internal" href="#gettext.bindtextdomain" title="gettext.bindtextdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">bindtextdomain()</span></code></a> 的脚注。</p>
</dd>
</dl>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> --- 多语种国际化服务</a><ul>
<li><a class="reference internal" href="#gnu-gettext-api">GNU <strong class="program">gettext</strong> API</a></li>
<li><a class="reference internal" href="#class-based-api">基于类的 API</a><ul>
<li><a class="reference internal" href="#the-nulltranslations-class"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code></a></li>
<li><a class="reference internal" href="#the-gnutranslations-class"><code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code></a></li>
<li><a class="reference internal" href="#solaris-message-catalog-support">Solaris 消息编目支持</a></li>
<li><a class="reference internal" href="#the-catalog-constructor">编目构造器</a></li>
</ul>
</li>
<li><a class="reference internal" href="#internationalizing-your-programs-and-modules">国际化 (I18N) 你的程序和模块</a><ul>
<li><a class="reference internal" href="#localizing-your-module">本地化你的模块</a></li>
<li><a class="reference internal" href="#localizing-your-application">本地化你的应用程序</a></li>
<li><a class="reference internal" href="#changing-languages-on-the-fly">即时更改语言</a></li>
<li><a class="reference internal" href="#deferred-translations">延迟翻译</a></li>
</ul>
</li>
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="i18n.html"
title="上一章">国际化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="locale.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> --- 国际化服务</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/gettext.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="locale.html" title="locale --- 国际化服务"
>下一页</a> |</li>
<li class="right" >
<a href="i18n.html" title="国际化"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="i18n.html" >国际化</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>