parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="gettext — Multilingual internationalization services" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/gettext.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/gettext.py The gettext module provides internationalization (I18N) and localization (L10N) services for your Python modules and applications. It supports both the GNU gettext messa..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_gettext_215ce8c6.png" />15<meta property="og:image:alt" content="Source code: Lib/gettext.py The gettext module provides internationalization (I18N) and localization (L10N) services for your Python modules and applications. It supports both the GNU gettext messa..." />16<meta name="description" content="Source code: Lib/gettext.py The gettext module provides internationalization (I18N) and localization (L10N) services for your Python modules and applications. It supports both the GNU gettext messa..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>gettext — Multilingual internationalization services — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="locale — Internationalization services" href="locale.html" />43 <link rel="prev" title="Internationalization" href="i18n.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/gettext.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" 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"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a><ul>108<li><a class="reference internal" href="#gnu-gettext-api">GNU <strong class="program">gettext</strong> API</a></li>109<li><a class="reference internal" href="#class-based-api">Class-based API</a><ul>110<li><a class="reference internal" href="#the-nulltranslations-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code> class</a></li>111<li><a class="reference internal" href="#the-gnutranslations-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code> class</a></li>112<li><a class="reference internal" href="#solaris-message-catalog-support">Solaris message catalog support</a></li>113<li><a class="reference internal" href="#the-catalog-constructor">The Catalog constructor</a></li>114</ul>115</li>116<li><a class="reference internal" href="#internationalizing-your-programs-and-modules">Internationalizing your programs and modules</a><ul>117<li><a class="reference internal" href="#localizing-your-module">Localizing your module</a></li>118<li><a class="reference internal" href="#localizing-your-application">Localizing your application</a></li>119<li><a class="reference internal" href="#changing-languages-on-the-fly">Changing languages on the fly</a></li>120<li><a class="reference internal" href="#deferred-translations">Deferred translations</a></li>121</ul>122</li>123<li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>124</ul>125</li>126</ul>127 128 </div>129 <div>130 <h4>Previous topic</h4>131 <p class="topless"><a href="i18n.html"132 title="previous chapter">Internationalization</a></p>133 </div>134 <div>135 <h4>Next topic</h4>136 <p class="topless"><a href="locale.html"137 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a></p>138 </div>139 <script>140 document.addEventListener('DOMContentLoaded', () => {141 const title = document.querySelector('meta[property="og:title"]').content;142 const elements = document.querySelectorAll('.improvepage');143 const pageurl = window.location.href.split('?')[0];144 elements.forEach(element => {145 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));146 url.searchParams.set('pagetitle', title);147 url.searchParams.set('pageurl', pageurl);148 url.searchParams.set('pagesource', "library/gettext.rst");149 element.href = url.toString();150 });151 });152 </script>153 <div role="note" aria-label="source link">154 <h3>This page</h3>155 <ul class="this-page-menu">156 <li><a href="../bugs.html">Report a bug</a></li>157 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>158 <li>159 <a href="https://github.com/python/cpython/blob/main/Doc/library/gettext.rst?plain=1"160 rel="nofollow">Show source161 </a>162 </li>163 164 </ul>165 </div>166 </nav>167 </div>168</div>169 170 171 <div class="related" role="navigation" aria-label="Related">172 <h3>Navigation</h3>173 <ul>174 <li class="right" style="margin-right: 10px">175 <a href="../genindex.html" title="General Index"176 accesskey="I">index</a></li>177 <li class="right" >178 <a href="../py-modindex.html" title="Python Module Index"179 >modules</a> |</li>180 <li class="right" >181 <a href="locale.html" title="locale — Internationalization services"182 accesskey="N">next</a> |</li>183 <li class="right" >184 <a href="i18n.html" title="Internationalization"185 accesskey="P">previous</a> |</li>186 187 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>188 <li><a href="https://www.python.org/">Python</a> »</li>189 <li class="switchers">190 <div class="language_switcher_placeholder"></div>191 <div class="version_switcher_placeholder"></div>192 </li>193 <li>194 195 </li>196 <li id="cpython-language-and-version">197 <a href="../index.html">3.15.0a6 Documentation</a> »198 </li>199 200 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>201 <li class="nav-item nav-item-2"><a href="i18n.html" accesskey="U">Internationalization</a> »</li>202 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a></li>203 <li class="right">204 205 206 <div class="inline-search" role="search">207 <form class="inline-search" action="../search.html" method="get">208 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">209 <input type="submit" value="Go">210 </form>211 </div>212 |213 </li>214 <li class="right">215<label class="theme-selector-label">216 Theme217 <select class="theme-selector" oninput="activateTheme(this.value)">218 <option value="auto" selected>Auto</option>219 <option value="light">Light</option>220 <option value="dark">Dark</option>221 </select>222</label> |</li>223 224 </ul>225 </div> 226 227 <div class="document">228 <div class="documentwrapper">229 <div class="bodywrapper">230 <div class="body" role="main">231 232 <section id="module-gettext">233<span id="gettext-multilingual-internationalization-services"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services<a class="headerlink" href="#module-gettext" title="Link to this heading">¶</a></h1>234<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/gettext.py">Lib/gettext.py</a></p>235<hr class="docutils" />236<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module provides internationalization (I18N) and localization237(L10N) services for your Python modules and applications. It supports both the238GNU <strong class="program">gettext</strong> message catalog API and a higher level, class-based API that may239be more appropriate for Python files. The interface described below allows you240to write your module and application messages in one natural language, and241provide a catalog of translated messages for running under different natural242languages.</p>243<p>Some hints on localizing your Python modules and applications are also given.</p>244<section id="gnu-gettext-api">245<h2>GNU <strong class="program">gettext</strong> API<a class="headerlink" href="#gnu-gettext-api" title="Link to this heading">¶</a></h2>246<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module defines the following API, which is very similar to247the GNU <strong class="program">gettext</strong> API. If you use this API you will affect the248translation of your entire application globally. Often this is what you want if249your application is monolingual, with the choice of language dependent on the250locale of your user. If you are localizing a Python module, or if your251application needs to switch languages on the fly, you probably want to use the252class-based API instead.</p>253<dl class="py function">254<dt class="sig sig-object py" id="gettext.bindtextdomain">255<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">bindtextdomain</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">localedir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.bindtextdomain" title="Link to this definition">¶</a></dt>256<dd><p>Bind the <em>domain</em> to the locale directory <em>localedir</em>. More concretely,257<code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> will look for binary <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> files for the given domain using258the path (on 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>, where259<em>language</em> is searched for in the environment variables <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANGUAGE</span></code>,260<span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_ALL</span></code>, <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_MESSAGES</span></code>, and <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code> respectively.</p>261<p>If <em>localedir</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the current binding for <em>domain</em> is262returned. <a class="footnote-reference brackets" href="#id3" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>263</dd></dl>264 265<dl class="py function">266<dt class="sig sig-object py" id="gettext.textdomain">267<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">textdomain</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.textdomain" title="Link to this definition">¶</a></dt>268<dd><p>Change or query the current global domain. If <em>domain</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the269current global domain is returned, otherwise the global domain is set to270<em>domain</em>, which is returned.</p>271</dd></dl>272 273<dl class="py function" id="index-4">274<dt class="sig sig-object py" id="gettext.gettext">275<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">gettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.gettext" title="Link to this definition">¶</a></dt>276<dd><p>Return the localized translation of <em>message</em>, based on the current global277domain, language, and locale directory. This function is usually aliased as278<code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> in the local namespace (see examples below).</p>279</dd></dl>280 281<dl class="py function">282<dt class="sig sig-object py" id="gettext.dgettext">283<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">dgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dgettext" title="Link to this definition">¶</a></dt>284<dd><p>Like <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>, but look the message up in the specified <em>domain</em>.</p>285</dd></dl>286 287<dl class="py function">288<dt class="sig sig-object py" id="gettext.ngettext">289<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">ngettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.ngettext" title="Link to this definition">¶</a></dt>290<dd><p>Like <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>, but consider plural forms. If a translation is found,291apply the plural formula to <em>n</em>, and return the resulting message (some292languages have more than two plural forms). If no translation is found, return293<em>singular</em> if <em>n</em> is 1; return <em>plural</em> otherwise.</p>294<p>The Plural formula is taken from the catalog header. It is a C or Python295expression that has a free variable <em>n</em>; the expression evaluates to the index296of the plural in the catalog. See297<a class="reference external" href="https://www.gnu.org/software/gettext/manual/gettext.html">the GNU gettext documentation</a>298for the precise syntax to be used in <code class="file docutils literal notranslate"><span class="pre">.po</span></code> files and the299formulas for a variety of languages.</p>300</dd></dl>301 302<dl class="py function">303<dt class="sig sig-object py" id="gettext.dngettext">304<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">dngettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dngettext" title="Link to this definition">¶</a></dt>305<dd><p>Like <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>, but look the message up in the specified <em>domain</em>.</p>306</dd></dl>307 308<dl class="py function">309<dt class="sig sig-object py" id="gettext.pgettext">310<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">pgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.pgettext" title="Link to this definition">¶</a></dt>311<dd></dd></dl>312 313<dl class="py function">314<dt class="sig sig-object py" id="gettext.dpgettext">315<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">dpgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dpgettext" title="Link to this definition">¶</a></dt>316<dd></dd></dl>317 318<dl class="py function">319<dt class="sig sig-object py" id="gettext.npgettext">320<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">npgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.npgettext" title="Link to this definition">¶</a></dt>321<dd></dd></dl>322 323<dl class="py function">324<dt class="sig sig-object py" id="gettext.dnpgettext">325<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">dnpgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.dnpgettext" title="Link to this definition">¶</a></dt>326<dd><p>Similar to the corresponding functions without the <code class="docutils literal notranslate"><span class="pre">p</span></code> in the prefix (that327is, <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>),328but the translation is restricted to the given message <em>context</em>.</p>329<div class="versionadded">330<p><span class="versionmodified added">Added in version 3.8.</span></p>331</div>332</dd></dl>333 334<p>Note that GNU <strong class="program">gettext</strong> also defines a <code class="xref py py-func docutils literal notranslate"><span class="pre">dcgettext()</span></code> method, but335this was deemed not useful and so it is currently unimplemented.</p>336<p>Here’s an example of typical usage for this API:</p>337<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>338<span class="n">gettext</span><span class="o">.</span><span class="n">bindtextdomain</span><span class="p">(</span><span class="s1">'myapplication'</span><span class="p">,</span> <span class="s1">'/path/to/my/language/directory'</span><span class="p">)</span>339<span class="n">gettext</span><span class="o">.</span><span class="n">textdomain</span><span class="p">(</span><span class="s1">'myapplication'</span><span class="p">)</span>340<span class="n">_</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">gettext</span>341<span class="c1"># ...</span>342<span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'This is a translatable string.'</span><span class="p">))</span>343</pre></div>344</div>345</section>346<section id="class-based-api">347<h2>Class-based API<a class="headerlink" href="#class-based-api" title="Link to this heading">¶</a></h2>348<p>The class-based API of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module gives you more flexibility and349greater convenience than the GNU <strong class="program">gettext</strong> API. It is the recommended350way of localizing your Python applications and modules. <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> defines351a <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> class which implements the parsing of GNU <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> format352files, and has methods for returning strings. Instances of this class can also353install themselves in the built-in namespace as the function <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code>.</p>354<dl class="py function">355<dt class="sig sig-object py" id="gettext.find">356<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">localedir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">languages</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.find" title="Link to this definition">¶</a></dt>357<dd><p>This function implements the standard <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> file search algorithm. It358takes a <em>domain</em>, identical to what <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> takes. Optional359<em>localedir</em> is as in <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>. Optional <em>languages</em> is a list of360strings, where each string is a language code.</p>361<p>If <em>localedir</em> is not given, then the default system locale directory is used.362<a class="footnote-reference brackets" href="#id4" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a> If <em>languages</em> is not given, then the following environment variables are363searched: <span class="target" id="index-5"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANGUAGE</span></code>, <span class="target" id="index-6"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_ALL</span></code>, <span class="target" id="index-7"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LC_MESSAGES</span></code>, and364<span class="target" id="index-8"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code>. The first one returning a non-empty value is used for the365<em>languages</em> variable. The environment variables should contain a colon separated366list of languages, which will be split on the colon to produce the expected list367of language code strings.</p>368<p><code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code> then expands and normalizes the languages, and then iterates369through them, searching for an existing file built of these components:</p>370<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>371<p>The first such file name that exists is returned by <code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code>. If no such372file is found, then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. If <em>all</em> is given, it returns a list373of all file names, in the order in which they appear in the languages list or374the environment variables.</p>375</dd></dl>376 377<dl class="py function">378<dt class="sig sig-object py" id="gettext.translation">379<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">translation</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">localedir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">languages</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">class_</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fallback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.translation" title="Link to this definition">¶</a></dt>380<dd><p>Return a <code class="docutils literal notranslate"><span class="pre">*Translations</span></code> instance based on the <em>domain</em>, <em>localedir</em>,381and <em>languages</em>, which are first passed to <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> to get a list of the382associated <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> file paths. Instances with identical <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> file383names are cached. The actual class instantiated is <em>class_</em> if384provided, otherwise <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>. The class’s constructor must385take a single <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> argument.</p>386<p>If multiple files are found, later files are used as fallbacks for earlier ones.387To allow setting the fallback, <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> is used to clone each388translation object from the cache; the actual instance data is still shared with389the cache.</p>390<p>If no <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> file is found, this function raises <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> if391<em>fallback</em> is false (which is the default), and returns a392<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> instance if <em>fallback</em> is true.</p>393<div class="versionchanged">394<p><span class="versionmodified changed">Changed in version 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> used to be raised, it is now an alias of <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>395</div>396<div class="versionchanged">397<p><span class="versionmodified changed">Changed in version 3.11: </span><em>codeset</em> parameter is removed.</p>398</div>399</dd></dl>400 401<dl class="py function">402<dt class="sig sig-object py" id="gettext.install">403<span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">install</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">localedir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">names</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.install" title="Link to this definition">¶</a></dt>404<dd><p>This installs the function <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> in Python’s builtins namespace, based on405<em>domain</em> and <em>localedir</em> which are passed to the function <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>406<p>For the <em>names</em> parameter, please see the description of the translation407object’s <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> method.</p>408<p>As seen below, you usually mark the strings in your application that are409candidates for translation, by wrapping them in a call to the <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code>410function, like this:</p>411<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">'This string will be translated.'</span><span class="p">))</span>412</pre></div>413</div>414<p>For convenience, you want the <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> function to be installed in Python’s415builtins namespace, so it is easily accessible in all modules of your416application.</p>417<div class="versionchanged">418<p><span class="versionmodified changed">Changed in version 3.11: </span><em>names</em> is now a keyword-only parameter.</p>419</div>420</dd></dl>421 422<section id="the-nulltranslations-class">423<h3>The <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> class<a class="headerlink" href="#the-nulltranslations-class" title="Link to this heading">¶</a></h3>424<p>Translation classes are what actually implement the translation of original425source file message strings to translated message strings. The base class used426by all translation classes is <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>; this provides the basic427interface you can use to write your own specialized translation classes. Here428are the methods of <code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code>:</p>429<dl class="py class">430<dt class="sig sig-object py" id="gettext.NullTranslations">431<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">NullTranslations</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations" title="Link to this definition">¶</a></dt>432<dd><p>Takes an optional <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>fp</em>, which is ignored by the base class.433Initializes “protected” instance variables <em>_info</em> and <em>_charset</em> which are set434by derived classes, as well as <em>_fallback</em>, which is set through435<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>. It then calls <code class="docutils literal notranslate"><span class="pre">self._parse(fp)</span></code> if <em>fp</em> is not436<code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>437<dl class="py method">438<dt class="sig sig-object py" id="gettext.NullTranslations._parse">439<span class="sig-name descname"><span class="pre">_parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fp</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations._parse" title="Link to this definition">¶</a></dt>440<dd><p>No-op in the base class, this method takes file object <em>fp</em>, and reads441the data from the file, initializing its message catalog. If you have an442unsupported message catalog file format, you should override this method443to parse your format.</p>444</dd></dl>445 446<dl class="py method">447<dt class="sig sig-object py" id="gettext.NullTranslations.add_fallback">448<span class="sig-name descname"><span class="pre">add_fallback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fallback</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.add_fallback" title="Link to this definition">¶</a></dt>449<dd><p>Add <em>fallback</em> as the fallback object for the current translation object.450A translation object should consult the fallback if it cannot provide a451translation for a given message.</p>452</dd></dl>453 454<dl class="py method">455<dt class="sig sig-object py" id="gettext.NullTranslations.gettext">456<span class="sig-name descname"><span class="pre">gettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.gettext" title="Link to this definition">¶</a></dt>457<dd><p>If a fallback has been set, forward <code class="xref py py-meth docutils literal notranslate"><span class="pre">gettext()</span></code> to the fallback.458Otherwise, return <em>message</em>. Overridden in derived classes.</p>459</dd></dl>460 461<dl class="py method">462<dt class="sig sig-object py" id="gettext.NullTranslations.ngettext">463<span class="sig-name descname"><span class="pre">ngettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.ngettext" title="Link to this definition">¶</a></dt>464<dd><p>If a fallback has been set, forward <code class="xref py py-meth docutils literal notranslate"><span class="pre">ngettext()</span></code> to the fallback.465Otherwise, return <em>singular</em> if <em>n</em> is 1; return <em>plural</em> otherwise.466Overridden in derived classes.</p>467</dd></dl>468 469<dl class="py method">470<dt class="sig sig-object py" id="gettext.NullTranslations.pgettext">471<span class="sig-name descname"><span class="pre">pgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.pgettext" title="Link to this definition">¶</a></dt>472<dd><p>If a fallback has been set, forward <code class="xref py py-meth docutils literal notranslate"><span class="pre">pgettext()</span></code> to the fallback.473Otherwise, return the translated message. Overridden in derived classes.</p>474<div class="versionadded">475<p><span class="versionmodified added">Added in version 3.8.</span></p>476</div>477</dd></dl>478 479<dl class="py method">480<dt class="sig sig-object py" id="gettext.NullTranslations.npgettext">481<span class="sig-name descname"><span class="pre">npgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.npgettext" title="Link to this definition">¶</a></dt>482<dd><p>If a fallback has been set, forward <code class="xref py py-meth docutils literal notranslate"><span class="pre">npgettext()</span></code> to the fallback.483Otherwise, return the translated message. Overridden in derived classes.</p>484<div class="versionadded">485<p><span class="versionmodified added">Added in version 3.8.</span></p>486</div>487</dd></dl>488 489<dl class="py method">490<dt class="sig sig-object py" id="gettext.NullTranslations.info">491<span class="sig-name descname"><span class="pre">info</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.info" title="Link to this definition">¶</a></dt>492<dd><p>Return a dictionary containing493the metadata found in the message catalog file.</p>494</dd></dl>495 496<dl class="py method">497<dt class="sig sig-object py" id="gettext.NullTranslations.charset">498<span class="sig-name descname"><span class="pre">charset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.charset" title="Link to this definition">¶</a></dt>499<dd><p>Return the encoding of the message catalog file.</p>500</dd></dl>501 502<dl class="py method">503<dt class="sig sig-object py" id="gettext.NullTranslations.install">504<span class="sig-name descname"><span class="pre">install</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">names</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.NullTranslations.install" title="Link to this definition">¶</a></dt>505<dd><p>This method installs <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> into the built-in namespace,506binding it to <code class="docutils literal notranslate"><span class="pre">_</span></code>.</p>507<p>If the <em>names</em> parameter is given, it must be a sequence containing the508names of functions you want to install in the builtins namespace in509addition to <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code>. Supported names are <code class="docutils literal notranslate"><span class="pre">'gettext'</span></code>, <code class="docutils literal notranslate"><span class="pre">'ngettext'</span></code>,510<code class="docutils literal notranslate"><span class="pre">'pgettext'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'npgettext'</span></code>.</p>511<p>Note that this is only one way, albeit the most convenient way, to make512the <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> function available to your application. Because it affects513the entire application globally, and specifically the built-in namespace,514localized modules should never install <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code>. Instead, they should use515this code to make <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> available to their module:</p>516<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>517<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">'mymodule'</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>518<span class="n">_</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">gettext</span>519</pre></div>520</div>521<p>This puts <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> only in the module’s global namespace and so only522affects calls within this module.</p>523<div class="versionchanged">524<p><span class="versionmodified changed">Changed in version 3.8: </span>Added <code class="docutils literal notranslate"><span class="pre">'pgettext'</span></code> and <code class="docutils literal notranslate"><span class="pre">'npgettext'</span></code>.</p>525</div>526</dd></dl>527 528</dd></dl>529 530</section>531<section id="the-gnutranslations-class">532<h3>The <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> class<a class="headerlink" href="#the-gnutranslations-class" title="Link to this heading">¶</a></h3>533<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module provides one additional class derived from534<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>. This class overrides535<code class="xref py py-meth docutils literal notranslate"><span class="pre">_parse()</span></code> to enable reading GNU <strong class="program">gettext</strong> format <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> files536in both big-endian and little-endian format.</p>537<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> parses optional metadata out of the translation538catalog. It is convention with GNU <strong class="program">gettext</strong> to include metadata as539the translation for the empty string. This metadata is in <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc822.html"><strong>RFC 822</strong></a>-style540<code class="docutils literal notranslate"><span class="pre">key:</span> <span class="pre">value</span></code> pairs, and should contain the <code class="docutils literal notranslate"><span class="pre">Project-Id-Version</span></code> key. If the541key <code class="docutils literal notranslate"><span class="pre">Content-Type</span></code> is found, then the <code class="docutils literal notranslate"><span class="pre">charset</span></code> property is used to542initialize the “protected” <code class="xref py py-attr docutils literal notranslate"><span class="pre">_charset</span></code> instance variable, defaulting to543<code class="docutils literal notranslate"><span class="pre">None</span></code> if not found. If the charset encoding is specified, then all message544ids and message strings read from the catalog are converted to Unicode using545this encoding, else ASCII is assumed.</p>546<p>Since message ids are read as Unicode strings too, all <code class="docutils literal notranslate"><span class="pre">*gettext()</span></code> methods547will assume message ids as Unicode strings, not byte strings.</p>548<p>The entire set of key/value pairs are placed into a dictionary and set as the549“protected” <code class="xref py py-attr docutils literal notranslate"><span class="pre">_info</span></code> instance variable.</p>550<p>If the <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> file’s magic number is invalid, the major version number is551unexpected, or if other problems occur while reading the file, instantiating a552<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> class can raise <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>553<dl class="py class">554<dt class="sig sig-object py" id="gettext.GNUTranslations">555<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">gettext.</span></span><span class="sig-name descname"><span class="pre">GNUTranslations</span></span><a class="headerlink" href="#gettext.GNUTranslations" title="Link to this definition">¶</a></dt>556<dd><p>The following methods are overridden from the base class implementation:</p>557<dl class="py method">558<dt class="sig sig-object py" id="gettext.GNUTranslations.gettext">559<span class="sig-name descname"><span class="pre">gettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.gettext" title="Link to this definition">¶</a></dt>560<dd><p>Look up the <em>message</em> id in the catalog and return the corresponding message561string, as a Unicode string. If there is no entry in the catalog for the562<em>message</em> id, and a fallback has been set, the look up is forwarded to the563fallback’s <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> method. Otherwise, the564<em>message</em> id is returned.</p>565</dd></dl>566 567<dl class="py method">568<dt class="sig sig-object py" id="gettext.GNUTranslations.ngettext">569<span class="sig-name descname"><span class="pre">ngettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.ngettext" title="Link to this definition">¶</a></dt>570<dd><p>Do a plural-forms lookup of a message id. <em>singular</em> is used as the message id571for purposes of lookup in the catalog, while <em>n</em> is used to determine which572plural form to use. The returned message string is a Unicode string.</p>573<p>If the message id is not found in the catalog, and a fallback is specified,574the request is forwarded to the fallback’s <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>575method. Otherwise, when <em>n</em> is 1 <em>singular</em> is returned, and <em>plural</em> is576returned in all other cases.</p>577<p>Here is an example:</p>578<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">'.'</span><span class="p">))</span>579<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>580<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>581 <span class="s1">'There is </span><span class="si">%(num)d</span><span class="s1"> file in this directory'</span><span class="p">,</span>582 <span class="s1">'There are </span><span class="si">%(num)d</span><span class="s1"> files in this directory'</span><span class="p">,</span>583 <span class="n">n</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">'num'</span><span class="p">:</span> <span class="n">n</span><span class="p">}</span>584</pre></div>585</div>586</dd></dl>587 588<dl class="py method">589<dt class="sig sig-object py" id="gettext.GNUTranslations.pgettext">590<span class="sig-name descname"><span class="pre">pgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.pgettext" title="Link to this definition">¶</a></dt>591<dd><p>Look up the <em>context</em> and <em>message</em> id in the catalog and return the592corresponding message string, as a Unicode string. If there is no593entry in the catalog for the <em>message</em> id and <em>context</em>, and a fallback594has been set, the look up is forwarded to the fallback’s595<code class="xref py py-meth docutils literal notranslate"><span class="pre">pgettext()</span></code> method. Otherwise, the <em>message</em> id is returned.</p>596<div class="versionadded">597<p><span class="versionmodified added">Added in version 3.8.</span></p>598</div>599</dd></dl>600 601<dl class="py method">602<dt class="sig sig-object py" id="gettext.GNUTranslations.npgettext">603<span class="sig-name descname"><span class="pre">npgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">singular</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">plural</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gettext.GNUTranslations.npgettext" title="Link to this definition">¶</a></dt>604<dd><p>Do a plural-forms lookup of a message id. <em>singular</em> is used as the605message id for purposes of lookup in the catalog, while <em>n</em> is used to606determine which plural form to use.</p>607<p>If the message id for <em>context</em> is not found in the catalog, and a608fallback is specified, the request is forwarded to the fallback’s609<code class="xref py py-meth docutils literal notranslate"><span class="pre">npgettext()</span></code> method. Otherwise, when <em>n</em> is 1 <em>singular</em> is610returned, and <em>plural</em> is returned in all other cases.</p>611<div class="versionadded">612<p><span class="versionmodified added">Added in version 3.8.</span></p>613</div>614</dd></dl>615 616</dd></dl>617 618</section>619<section id="solaris-message-catalog-support">620<h3>Solaris message catalog support<a class="headerlink" href="#solaris-message-catalog-support" title="Link to this heading">¶</a></h3>621<p>The Solaris operating system defines its own binary <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> file format, but622since no documentation can be found on this format, it is not supported at this623time.</p>624</section>625<section id="the-catalog-constructor">626<h3>The Catalog constructor<a class="headerlink" href="#the-catalog-constructor" title="Link to this heading">¶</a></h3>627<p id="index-10">GNOME uses a version of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module by James Henstridge, but this628version has a slightly different API. Its documented usage was:</p>629<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>630<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>631<span class="n">_</span> <span class="o">=</span> <span class="n">cat</span><span class="o">.</span><span class="n">gettext</span>632<span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'hello world'</span><span class="p">))</span>633</pre></div>634</div>635<p>For compatibility with this older module, the function <code class="xref py py-func docutils literal notranslate"><span class="pre">Catalog()</span></code> is an636alias for the <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> function described above.</p>637<p>One difference between this module and Henstridge’s: his catalog objects638supported access through a mapping API, but this appears to be unused and so is639not currently supported.</p>640</section>641</section>642<section id="internationalizing-your-programs-and-modules">643<span id="i18n-howto"></span><h2>Internationalizing your programs and modules<a class="headerlink" href="#internationalizing-your-programs-and-modules" title="Link to this heading">¶</a></h2>644<p>Internationalization (I18N) refers to the operation by which a program is made645aware of multiple languages. Localization (L10N) refers to the adaptation of646your program, once internationalized, to the local language and cultural habits.647In order to provide multilingual messages for your Python programs, you need to648take the following steps:</p>649<ol class="arabic simple">650<li><p>prepare your program or module by specially marking translatable strings</p></li>651<li><p>run a suite of tools over your marked files to generate raw messages catalogs</p></li>652<li><p>create language-specific translations of the message catalogs</p></li>653<li><p>use the <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module so that message strings are properly translated</p></li>654</ol>655<p>In order to prepare your code for I18N, you need to look at all the strings in656your files. Any string that needs to be translated should be marked by wrapping657it in <code class="docutils literal notranslate"><span class="pre">_('...')</span></code> — that is, a call to the function <a class="reference internal" href="#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-func docutils literal notranslate"><span class="pre">_</span></code></a>. For example:</p>658<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">'mylog.txt'</span>659<span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">'writing a log message'</span><span class="p">)</span>660<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">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>661 <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>662</pre></div>663</div>664<p>In this example, the string <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> is marked as a candidate665for translation, while the strings <code class="docutils literal notranslate"><span class="pre">'mylog.txt'</span></code> and <code class="docutils literal notranslate"><span class="pre">'w'</span></code> are not.</p>666<p>There are a few tools to extract the strings meant for translation.667The original GNU <strong class="program">gettext</strong> only supported C or C++ source668code but its extended version <strong class="program">xgettext</strong> scans code written669in a number of languages, including Python, to find strings marked as670translatable. <a class="reference external" href="https://babel.pocoo.org/">Babel</a> is a Python671internationalization library that includes a <code class="file docutils literal notranslate"><span class="pre">pybabel</span></code> script to672extract and compile message catalogs. François Pinard’s program673called <strong class="program">xpot</strong> does a similar job and is available as part of674his <a class="reference external" href="https://github.com/pinard/po-utils">po-utils package</a>.</p>675<p>(Python also includes pure-Python versions of these programs, called676<strong class="program">pygettext.py</strong> and <strong class="program">msgfmt.py</strong>; some Python distributions677will install them for you. <strong class="program">pygettext.py</strong> is similar to678<strong class="program">xgettext</strong>, but only understands Python source code and679cannot handle other programming languages such as C or C++.680<strong class="program">pygettext.py</strong> supports a command-line interface similar to681<strong class="program">xgettext</strong>; for details on its use, run <code class="docutils literal notranslate"><span class="pre">pygettext.py</span>682<span class="pre">--help</span></code>. <strong class="program">msgfmt.py</strong> is binary compatible with GNU683<strong class="program">msgfmt</strong>. With these two programs, you may not need the GNU684<strong class="program">gettext</strong> package to internationalize your Python685applications.)</p>686<p><strong class="program">xgettext</strong>, <strong class="program">pygettext</strong>, and similar tools generate687<code class="file docutils literal notranslate"><span class="pre">.po</span></code> files that are message catalogs. They are structured688human-readable files that contain every marked string in the source689code, along with a placeholder for the translated versions of these690strings.</p>691<p>Copies of these <code class="file docutils literal notranslate"><span class="pre">.po</span></code> files are then handed over to the692individual human translators who write translations for every693supported natural language. They send back the completed694language-specific versions as a <code class="file docutils literal notranslate"><span class="pre"><language-name>.po</span></code> file that’s695compiled into a machine-readable <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> binary catalog file using696the <strong class="program">msgfmt</strong> program. The <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> files are used by the697<code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module for the actual translation processing at698run-time.</p>699<p>How you use the <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module in your code depends on whether you are700internationalizing a single module or your entire application. The next two701sections will discuss each case.</p>702<section id="localizing-your-module">703<h3>Localizing your module<a class="headerlink" href="#localizing-your-module" title="Link to this heading">¶</a></h3>704<p>If you are localizing your module, you must take care not to make global705changes, e.g. to the built-in namespace. You should not use the GNU <strong class="program">gettext</strong>706API but instead the class-based API.</p>707<p>Let’s say your module is called “spam” and the module’s various natural language708translation <code class="file docutils literal notranslate"><span class="pre">.mo</span></code> files reside in <code class="file docutils literal notranslate"><span class="pre">/usr/share/locale</span></code> in GNU709<strong class="program">gettext</strong> format. Here’s what you would put at the top of your710module:</p>711<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>712<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">'spam'</span><span class="p">,</span> <span class="s1">'/usr/share/locale'</span><span class="p">)</span>713<span class="n">_</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">gettext</span>714</pre></div>715</div>716</section>717<section id="localizing-your-application">718<h3>Localizing your application<a class="headerlink" href="#localizing-your-application" title="Link to this heading">¶</a></h3>719<p>If you are localizing your application, you can install the <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> function720globally into the built-in namespace, usually in the main driver file of your721application. This will let all your application-specific files just use722<code class="docutils literal notranslate"><span class="pre">_('...')</span></code> without having to explicitly install it in each file.</p>723<p>In the simple case then, you need only add the following bit of code to the main724driver file of your application:</p>725<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>726<span class="n">gettext</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="s1">'myapplication'</span><span class="p">)</span>727</pre></div>728</div>729<p>If you need to set the locale directory, you can pass it into the730<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> function:</p>731<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>732<span class="n">gettext</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="s1">'myapplication'</span><span class="p">,</span> <span class="s1">'/usr/share/locale'</span><span class="p">)</span>733</pre></div>734</div>735</section>736<section id="changing-languages-on-the-fly">737<h3>Changing languages on the fly<a class="headerlink" href="#changing-languages-on-the-fly" title="Link to this heading">¶</a></h3>738<p>If your program needs to support many languages at the same time, you may want739to create multiple translation instances and then switch between them740explicitly, like so:</p>741<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gettext</span>742 743<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">'myapplication'</span><span class="p">,</span> <span class="n">languages</span><span class="o">=</span><span class="p">[</span><span class="s1">'en'</span><span class="p">])</span>744<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">'myapplication'</span><span class="p">,</span> <span class="n">languages</span><span class="o">=</span><span class="p">[</span><span class="s1">'fr'</span><span class="p">])</span>745<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">'myapplication'</span><span class="p">,</span> <span class="n">languages</span><span class="o">=</span><span class="p">[</span><span class="s1">'de'</span><span class="p">])</span>746 747<span class="c1"># start by using language1</span>748<span class="n">lang1</span><span class="o">.</span><span class="n">install</span><span class="p">()</span>749 750<span class="c1"># ... time goes by, user selects language 2</span>751<span class="n">lang2</span><span class="o">.</span><span class="n">install</span><span class="p">()</span>752 753<span class="c1"># ... more time goes by, user selects language 3</span>754<span class="n">lang3</span><span class="o">.</span><span class="n">install</span><span class="p">()</span>755</pre></div>756</div>757</section>758<section id="deferred-translations">759<h3>Deferred translations<a class="headerlink" href="#deferred-translations" title="Link to this heading">¶</a></h3>760<p>In most coding situations, strings are translated where they are coded.761Occasionally however, you need to mark strings for translation, but defer actual762translation until later. A classic example is:</p>763<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">'mollusk'</span><span class="p">,</span>764 <span class="s1">'albatross'</span><span class="p">,</span>765 <span class="s1">'rat'</span><span class="p">,</span>766 <span class="s1">'penguin'</span><span class="p">,</span>767 <span class="s1">'python'</span><span class="p">,</span> <span class="p">]</span>768<span class="c1"># ...</span>769<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">animals</span><span class="p">:</span>770 <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>771</pre></div>772</div>773<p>Here, you want to mark the strings in the <code class="docutils literal notranslate"><span class="pre">animals</span></code> list as being774translatable, but you don’t actually want to translate them until they are775printed.</p>776<p>Here is one way you can handle this situation:</p>777<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </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>778 779<span class="n">animals</span> <span class="o">=</span> <span class="p">[</span><span class="n">_</span><span class="p">(</span><span class="s1">'mollusk'</span><span class="p">),</span>780 <span class="n">_</span><span class="p">(</span><span class="s1">'albatross'</span><span class="p">),</span>781 <span class="n">_</span><span class="p">(</span><span class="s1">'rat'</span><span class="p">),</span>782 <span class="n">_</span><span class="p">(</span><span class="s1">'penguin'</span><span class="p">),</span>783 <span class="n">_</span><span class="p">(</span><span class="s1">'python'</span><span class="p">),</span> <span class="p">]</span>784 785<span class="k">del</span> <span class="n">_</span>786 787<span class="c1"># ...</span>788<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">animals</span><span class="p">:</span>789 <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>790</pre></div>791</div>792<p>This works because the dummy definition of <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> simply returns the string793unchanged. And this dummy definition will temporarily override any definition794of <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> in the built-in namespace (until the <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> command). Take795care, though if you have a previous definition of <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> in the local796namespace.</p>797<p>Note that the second use of <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code> will not identify “a” as being798translatable to the <strong class="program">gettext</strong> program, because the parameter799is not a string literal.</p>800<p>Another way to handle this is with the following example:</p>801<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </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>802 803<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">'mollusk'</span><span class="p">),</span>804 <span class="n">N_</span><span class="p">(</span><span class="s1">'albatross'</span><span class="p">),</span>805 <span class="n">N_</span><span class="p">(</span><span class="s1">'rat'</span><span class="p">),</span>806 <span class="n">N_</span><span class="p">(</span><span class="s1">'penguin'</span><span class="p">),</span>807 <span class="n">N_</span><span class="p">(</span><span class="s1">'python'</span><span class="p">),</span> <span class="p">]</span>808 809<span class="c1"># ...</span>810<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">animals</span><span class="p">:</span>811 <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>812</pre></div>813</div>814<p>In this case, you are marking translatable strings with the function815<code class="xref py py-func docutils literal notranslate"><span class="pre">N_()</span></code>, which won’t conflict with any definition of <code class="xref py py-func docutils literal notranslate"><span class="pre">_()</span></code>.816However, you will need to teach your message extraction program to817look for translatable strings marked with <code class="xref py py-func docutils literal notranslate"><span class="pre">N_()</span></code>. <strong class="program">xgettext</strong>,818<strong class="program">pygettext</strong>, <code class="docutils literal notranslate"><span class="pre">pybabel</span> <span class="pre">extract</span></code>, and <strong class="program">xpot</strong> all819support this through the use of the <code class="xref std std-option docutils literal notranslate"><span class="pre">-k</span></code> command-line switch.820The choice of <code class="xref py py-func docutils literal notranslate"><span class="pre">N_()</span></code> here is totally arbitrary; it could have just821as easily been <code class="xref py py-func docutils literal notranslate"><span class="pre">MarkThisStringForTranslation()</span></code>.</p>822</section>823</section>824<section id="acknowledgements">825<h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Link to this heading">¶</a></h2>826<p>The following people contributed code, feedback, design suggestions, previous827implementations, and valuable experience to the creation of this module:</p>828<ul class="simple">829<li><p>Peter Funk</p></li>830<li><p>James Henstridge</p></li>831<li><p>Juan David Ibáñez Palomar</p></li>832<li><p>Marc-André Lemburg</p></li>833<li><p>Martin von Löwis</p></li>834<li><p>François Pinard</p></li>835<li><p>Barry Warsaw</p></li>836<li><p>Gustavo Niemeyer</p></li>837</ul>838<p class="rubric">Footnotes</p>839<aside class="footnote-list brackets">840<aside class="footnote brackets" id="id3" role="doc-footnote">841<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>842<p>The default locale directory is system dependent; for example, on Red Hat Linux843it is <code class="file docutils literal notranslate"><span class="pre">/usr/share/locale</span></code>, but on Solaris it is <code class="file docutils literal notranslate"><span class="pre">/usr/lib/locale</span></code>.844The <code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> module does not try to support these system dependent845defaults; instead its default is <code class="file docutils literal notranslate"><em><span class="pre">sys.base_prefix</span></em><span class="pre">/share/locale</span></code> (see846<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>). For this reason, it is always best to call847<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> with an explicit absolute path at the start of your848application.</p>849</aside>850<aside class="footnote brackets" id="id4" role="doc-footnote">851<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id2">2</a><span class="fn-bracket">]</span></span>852<p>See the footnote for <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> above.</p>853</aside>854</aside>855</section>856</section>857 858 859 <div class="clearer"></div>860 </div>861 </div>862 </div>863 <div class="sphinxsidebar" role="navigation" aria-label="Main">864 <div class="sphinxsidebarwrapper">865 <div>866 <h3><a href="../contents.html">Table of Contents</a></h3>867 <ul>868<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a><ul>869<li><a class="reference internal" href="#gnu-gettext-api">GNU <strong class="program">gettext</strong> API</a></li>870<li><a class="reference internal" href="#class-based-api">Class-based API</a><ul>871<li><a class="reference internal" href="#the-nulltranslations-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">NullTranslations</span></code> class</a></li>872<li><a class="reference internal" href="#the-gnutranslations-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">GNUTranslations</span></code> class</a></li>873<li><a class="reference internal" href="#solaris-message-catalog-support">Solaris message catalog support</a></li>874<li><a class="reference internal" href="#the-catalog-constructor">The Catalog constructor</a></li>875</ul>876</li>877<li><a class="reference internal" href="#internationalizing-your-programs-and-modules">Internationalizing your programs and modules</a><ul>878<li><a class="reference internal" href="#localizing-your-module">Localizing your module</a></li>879<li><a class="reference internal" href="#localizing-your-application">Localizing your application</a></li>880<li><a class="reference internal" href="#changing-languages-on-the-fly">Changing languages on the fly</a></li>881<li><a class="reference internal" href="#deferred-translations">Deferred translations</a></li>882</ul>883</li>884<li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>885</ul>886</li>887</ul>888 889 </div>890 <div>891 <h4>Previous topic</h4>892 <p class="topless"><a href="i18n.html"893 title="previous chapter">Internationalization</a></p>894 </div>895 <div>896 <h4>Next topic</h4>897 <p class="topless"><a href="locale.html"898 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a></p>899 </div>900 <script>901 document.addEventListener('DOMContentLoaded', () => {902 const title = document.querySelector('meta[property="og:title"]').content;903 const elements = document.querySelectorAll('.improvepage');904 const pageurl = window.location.href.split('?')[0];905 elements.forEach(element => {906 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));907 url.searchParams.set('pagetitle', title);908 url.searchParams.set('pageurl', pageurl);909 url.searchParams.set('pagesource', "library/gettext.rst");910 element.href = url.toString();911 });912 });913 </script>914 <div role="note" aria-label="source link">915 <h3>This page</h3>916 <ul class="this-page-menu">917 <li><a href="../bugs.html">Report a bug</a></li>918 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>919 <li>920 <a href="https://github.com/python/cpython/blob/main/Doc/library/gettext.rst?plain=1"921 rel="nofollow">Show source922 </a>923 </li>924 925 </ul>926 </div>927 </div>928<div id="sidebarbutton" title="Collapse sidebar">929<span>«</span>930</div>931 932 </div>933 <div class="clearer"></div>934 </div> 935 <div class="related" role="navigation" aria-label="Related">936 <h3>Navigation</h3>937 <ul>938 <li class="right" style="margin-right: 10px">939 <a href="../genindex.html" title="General Index"940 >index</a></li>941 <li class="right" >942 <a href="../py-modindex.html" title="Python Module Index"943 >modules</a> |</li>944 <li class="right" >945 <a href="locale.html" title="locale — Internationalization services"946 >next</a> |</li>947 <li class="right" >948 <a href="i18n.html" title="Internationalization"949 >previous</a> |</li>950 951 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>952 <li><a href="https://www.python.org/">Python</a> »</li>953 <li class="switchers">954 <div class="language_switcher_placeholder"></div>955 <div class="version_switcher_placeholder"></div>956 </li>957 <li>958 959 </li>960 <li id="cpython-language-and-version">961 <a href="../index.html">3.15.0a6 Documentation</a> »962 </li>963 964 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>965 <li class="nav-item nav-item-2"><a href="i18n.html" >Internationalization</a> »</li>966 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a></li>967 <li class="right">968 969 970 <div class="inline-search" role="search">971 <form class="inline-search" action="../search.html" method="get">972 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">973 <input type="submit" value="Go">974 </form>975 </div>976 |977 </li>978 <li class="right">979<label class="theme-selector-label">980 Theme981 <select class="theme-selector" oninput="activateTheme(this.value)">982 <option value="auto" selected>Auto</option>983 <option value="light">Light</option>984 <option value="dark">Dark</option>985 </select>986</label> |</li>987 988 </ul>989 </div> 990 <div class="footer">991 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.992 <br>993 This page is licensed under the Python Software Foundation License Version 2.994 <br>995 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.996 <br>997 998 See <a href="/license.html">History and License</a> for more information.<br>999 1000 1001 <br>1002 1003 The Python Software Foundation is a non-profit corporation.1004<a href="https://www.python.org/psf/donations/">Please donate.</a>1005<br>1006 <br>1007 Last updated on Mar 10, 2026 (08:58 UTC).1008 1009 <a href="/bugs.html">Found a bug</a>?1010 1011 <br>1012 1013 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.1014 </div>1015 1016 </body>1017</html>