gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1108 satır
92KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="locale — Internationalization services" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/locale.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/locale.py The locale module opens access to the POSIX locale database and functionality. The POSIX locale mechanism allows programmers to deal with certain cultural issues in an ap..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/locale.py The locale module opens access to the POSIX locale database and functionality. The POSIX locale mechanism allows programmers to deal with certain cultural issues in an ap..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>locale — Internationalization services &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Program Frameworks" href="frameworks.html" />
  33. <link rel="prev" title="gettext — Multilingual internationalization services" href="gettext.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/locale.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a><ul>
  86. <li><a class="reference internal" href="#background-details-hints-tips-and-caveats">Background, details, hints, tips and caveats</a></li>
  87. <li><a class="reference internal" href="#for-extension-writers-and-programs-that-embed-python">For extension writers and programs that embed Python</a></li>
  88. <li><a class="reference internal" href="#access-to-message-catalogs">Access to message catalogs</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="gettext.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="frameworks.html"
  101. title="next chapter">Program Frameworks</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/locale.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="frameworks.html" title="Program Frameworks"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="gettext.html" title="gettext — Multilingual internationalization services"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="i18n.html" accesskey="U">Internationalization</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-locale">
  171. <span id="locale-internationalization-services"></span><h1><a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> — Internationalization services<a class="headerlink" href="#module-locale" title="Link to this heading">¶</a></h1>
  172. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/locale.py">Lib/locale.py</a></p>
  173. <hr class="docutils" />
  174. <p>The <a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module opens access to the POSIX locale database and
  175. functionality. The POSIX locale mechanism allows programmers to deal with
  176. certain cultural issues in an application, without requiring the programmer to
  177. know all the specifics of each country where the software is executed.</p>
  178. <p id="index-0">The <a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module is implemented on top of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_locale</span></code> module,
  179. which in turn uses an ANSI C locale implementation if available.</p>
  180. <p>The <a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module defines the following exception and functions:</p>
  181. <dl class="py exception">
  182. <dt class="sig sig-object py" id="locale.Error">
  183. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#locale.Error" title="Link to this definition">¶</a></dt>
  184. <dd><p>Exception raised when the locale passed to <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> is not
  185. recognized.</p>
  186. </dd></dl>
  187. <dl class="py function">
  188. <dt class="sig sig-object py" id="locale.setlocale">
  189. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">setlocale</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locale</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="#locale.setlocale" title="Link to this definition">¶</a></dt>
  190. <dd><p>If <em>locale</em> is given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> modifies the locale
  191. setting for the <em>category</em>. The available categories are listed in the data
  192. description below. <em>locale</em> may be a string, or an iterable of two strings
  193. (language code and encoding). If it’s an iterable, it’s converted to a locale
  194. name using the locale aliasing engine. An empty string specifies the user’s
  195. default settings. If the modification of the locale fails, the exception
  196. <a class="reference internal" href="#locale.Error" title="locale.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> is raised. If successful, the new locale setting is returned.</p>
  197. <p>If <em>locale</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, the current setting for <em>category</em> is
  198. returned.</p>
  199. <p><a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> is not thread-safe on most systems. Applications typically
  200. start with a call of</p>
  201. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">locale</span>
  202. <span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
  203. </pre></div>
  204. </div>
  205. <p>This sets the locale for all categories to the user’s default setting (typically
  206. specified in the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code> environment variable). If the locale is not
  207. changed thereafter, using multithreading should not cause problems.</p>
  208. </dd></dl>
  209. <dl class="py function">
  210. <dt class="sig sig-object py" id="locale.localeconv">
  211. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">localeconv</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#locale.localeconv" title="Link to this definition">¶</a></dt>
  212. <dd><p>Returns the database of the local conventions as a dictionary. This dictionary
  213. has the following strings as keys:</p>
  214. <table class="docutils align-default">
  215. <thead>
  216. <tr class="row-odd"><th class="head"><p>Category</p></th>
  217. <th class="head"><p>Key</p></th>
  218. <th class="head"><p>Meaning</p></th>
  219. </tr>
  220. </thead>
  221. <tbody>
  222. <tr class="row-even"><td><p><a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a></p></td>
  223. <td><p><code class="docutils literal notranslate"><span class="pre">'decimal_point'</span></code></p></td>
  224. <td><p>Decimal point character.</p></td>
  225. </tr>
  226. <tr class="row-odd"><td></td>
  227. <td><p><code class="docutils literal notranslate"><span class="pre">'grouping'</span></code></p></td>
  228. <td><p>Sequence of numbers specifying
  229. which relative positions the
  230. <code class="docutils literal notranslate"><span class="pre">'thousands_sep'</span></code> is
  231. expected. If the sequence is
  232. terminated with
  233. <a class="reference internal" href="#locale.CHAR_MAX" title="locale.CHAR_MAX"><code class="xref py py-const docutils literal notranslate"><span class="pre">CHAR_MAX</span></code></a>, no further
  234. grouping is performed. If the
  235. sequence terminates with a
  236. <code class="docutils literal notranslate"><span class="pre">0</span></code>, the last group size is
  237. repeatedly used.</p></td>
  238. </tr>
  239. <tr class="row-even"><td></td>
  240. <td><p><code class="docutils literal notranslate"><span class="pre">'thousands_sep'</span></code></p></td>
  241. <td><p>Character used between groups.</p></td>
  242. </tr>
  243. <tr class="row-odd"><td><p><a class="reference internal" href="#locale.LC_MONETARY" title="locale.LC_MONETARY"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_MONETARY</span></code></a></p></td>
  244. <td><p><code class="docutils literal notranslate"><span class="pre">'int_curr_symbol'</span></code></p></td>
  245. <td><p>International currency symbol.</p></td>
  246. </tr>
  247. <tr class="row-even"><td></td>
  248. <td><p><code class="docutils literal notranslate"><span class="pre">'currency_symbol'</span></code></p></td>
  249. <td><p>Local currency symbol.</p></td>
  250. </tr>
  251. <tr class="row-odd"><td></td>
  252. <td><p><code class="docutils literal notranslate"><span class="pre">'p_cs_precedes/n_cs_precedes'</span></code></p></td>
  253. <td><p>Whether the currency symbol
  254. precedes the value (for
  255. positive resp. negative
  256. values).</p></td>
  257. </tr>
  258. <tr class="row-even"><td></td>
  259. <td><p><code class="docutils literal notranslate"><span class="pre">'p_sep_by_space/n_sep_by_space'</span></code></p></td>
  260. <td><p>Whether the currency symbol is
  261. separated from the value by a
  262. space (for positive resp.
  263. negative values).</p></td>
  264. </tr>
  265. <tr class="row-odd"><td></td>
  266. <td><p><code class="docutils literal notranslate"><span class="pre">'mon_decimal_point'</span></code></p></td>
  267. <td><p>Decimal point used for
  268. monetary values.</p></td>
  269. </tr>
  270. <tr class="row-even"><td></td>
  271. <td><p><code class="docutils literal notranslate"><span class="pre">'frac_digits'</span></code></p></td>
  272. <td><p>Number of fractional digits
  273. used in local formatting of
  274. monetary values.</p></td>
  275. </tr>
  276. <tr class="row-odd"><td></td>
  277. <td><p><code class="docutils literal notranslate"><span class="pre">'int_frac_digits'</span></code></p></td>
  278. <td><p>Number of fractional digits
  279. used in international
  280. formatting of monetary values.</p></td>
  281. </tr>
  282. <tr class="row-even"><td></td>
  283. <td><p><code class="docutils literal notranslate"><span class="pre">'mon_thousands_sep'</span></code></p></td>
  284. <td><p>Group separator used for
  285. monetary values.</p></td>
  286. </tr>
  287. <tr class="row-odd"><td></td>
  288. <td><p><code class="docutils literal notranslate"><span class="pre">'mon_grouping'</span></code></p></td>
  289. <td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">'grouping'</span></code>,
  290. used for monetary values.</p></td>
  291. </tr>
  292. <tr class="row-even"><td></td>
  293. <td><p><code class="docutils literal notranslate"><span class="pre">'positive_sign'</span></code></p></td>
  294. <td><p>Symbol used to annotate a
  295. positive monetary value.</p></td>
  296. </tr>
  297. <tr class="row-odd"><td></td>
  298. <td><p><code class="docutils literal notranslate"><span class="pre">'negative_sign'</span></code></p></td>
  299. <td><p>Symbol used to annotate a
  300. negative monetary value.</p></td>
  301. </tr>
  302. <tr class="row-even"><td></td>
  303. <td><p><code class="docutils literal notranslate"><span class="pre">'p_sign_posn/n_sign_posn'</span></code></p></td>
  304. <td><p>The position of the sign (for
  305. positive resp. negative
  306. values), see below.</p></td>
  307. </tr>
  308. </tbody>
  309. </table>
  310. <p>All numeric values can be set to <a class="reference internal" href="#locale.CHAR_MAX" title="locale.CHAR_MAX"><code class="xref py py-const docutils literal notranslate"><span class="pre">CHAR_MAX</span></code></a> to indicate that there is no
  311. value specified in this locale.</p>
  312. <p>The possible values for <code class="docutils literal notranslate"><span class="pre">'p_sign_posn'</span></code> and <code class="docutils literal notranslate"><span class="pre">'n_sign_posn'</span></code> are given below.</p>
  313. <table class="docutils align-default">
  314. <thead>
  315. <tr class="row-odd"><th class="head"><p>Value</p></th>
  316. <th class="head"><p>Explanation</p></th>
  317. </tr>
  318. </thead>
  319. <tbody>
  320. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">0</span></code></p></td>
  321. <td><p>Currency and value are surrounded by
  322. parentheses.</p></td>
  323. </tr>
  324. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">1</span></code></p></td>
  325. <td><p>The sign should precede the value and
  326. currency symbol.</p></td>
  327. </tr>
  328. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">2</span></code></p></td>
  329. <td><p>The sign should follow the value and
  330. currency symbol.</p></td>
  331. </tr>
  332. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">3</span></code></p></td>
  333. <td><p>The sign should immediately precede the
  334. value.</p></td>
  335. </tr>
  336. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">4</span></code></p></td>
  337. <td><p>The sign should immediately follow the
  338. value.</p></td>
  339. </tr>
  340. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">CHAR_MAX</span></code></p></td>
  341. <td><p>Nothing is specified in this locale.</p></td>
  342. </tr>
  343. </tbody>
  344. </table>
  345. <p>The function temporarily sets the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> locale to the <code class="docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code>
  346. locale or the <code class="docutils literal notranslate"><span class="pre">LC_MONETARY</span></code> locale if locales are different and numeric or
  347. monetary strings are non-ASCII. This temporary change affects other threads.</p>
  348. <div class="versionchanged">
  349. <p><span class="versionmodified changed">Changed in version 3.7: </span>The function now temporarily sets the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> locale to the
  350. <code class="docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code> locale in some cases.</p>
  351. </div>
  352. </dd></dl>
  353. <dl class="py function">
  354. <dt class="sig sig-object py" id="locale.nl_langinfo">
  355. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">nl_langinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">option</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.nl_langinfo" title="Link to this definition">¶</a></dt>
  356. <dd><p>Return some locale-specific information as a string. This function is not
  357. available on all systems, and the set of possible options might also vary
  358. across platforms. The possible argument values are numbers, for which
  359. symbolic constants are available in the locale module.</p>
  360. <p>The <a class="reference internal" href="#locale.nl_langinfo" title="locale.nl_langinfo"><code class="xref py py-func docutils literal notranslate"><span class="pre">nl_langinfo()</span></code></a> function accepts one of the following keys. Most
  361. descriptions are taken from the corresponding description in the GNU C
  362. library.</p>
  363. <dl class="py data">
  364. <dt class="sig sig-object py" id="locale.CODESET">
  365. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">CODESET</span></span><a class="headerlink" href="#locale.CODESET" title="Link to this definition">¶</a></dt>
  366. <dd><p>Get a string with the name of the character encoding used in the
  367. selected locale.</p>
  368. </dd></dl>
  369. <dl class="py data">
  370. <dt class="sig sig-object py" id="locale.D_T_FMT">
  371. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">D_T_FMT</span></span><a class="headerlink" href="#locale.D_T_FMT" title="Link to this definition">¶</a></dt>
  372. <dd><p>Get a string that can be used as a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to
  373. represent date and time in a locale-specific way.</p>
  374. </dd></dl>
  375. <dl class="py data">
  376. <dt class="sig sig-object py" id="locale.D_FMT">
  377. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">D_FMT</span></span><a class="headerlink" href="#locale.D_FMT" title="Link to this definition">¶</a></dt>
  378. <dd><p>Get a string that can be used as a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to
  379. represent a date in a locale-specific way.</p>
  380. </dd></dl>
  381. <dl class="py data">
  382. <dt class="sig sig-object py" id="locale.T_FMT">
  383. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">T_FMT</span></span><a class="headerlink" href="#locale.T_FMT" title="Link to this definition">¶</a></dt>
  384. <dd><p>Get a string that can be used as a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to
  385. represent a time in a locale-specific way.</p>
  386. </dd></dl>
  387. <dl class="py data">
  388. <dt class="sig sig-object py" id="locale.T_FMT_AMPM">
  389. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">T_FMT_AMPM</span></span><a class="headerlink" href="#locale.T_FMT_AMPM" title="Link to this definition">¶</a></dt>
  390. <dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent time in the am/pm
  391. format.</p>
  392. </dd></dl>
  393. <dl class="py data">
  394. <dt class="sig sig-object py" id="locale.DAY_1">
  395. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_1</span></span><a class="headerlink" href="#locale.DAY_1" title="Link to this definition">¶</a></dt>
  396. <dt class="sig sig-object py" id="locale.DAY_2">
  397. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_2</span></span><a class="headerlink" href="#locale.DAY_2" title="Link to this definition">¶</a></dt>
  398. <dt class="sig sig-object py" id="locale.DAY_3">
  399. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_3</span></span><a class="headerlink" href="#locale.DAY_3" title="Link to this definition">¶</a></dt>
  400. <dt class="sig sig-object py" id="locale.DAY_4">
  401. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_4</span></span><a class="headerlink" href="#locale.DAY_4" title="Link to this definition">¶</a></dt>
  402. <dt class="sig sig-object py" id="locale.DAY_5">
  403. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_5</span></span><a class="headerlink" href="#locale.DAY_5" title="Link to this definition">¶</a></dt>
  404. <dt class="sig sig-object py" id="locale.DAY_6">
  405. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_6</span></span><a class="headerlink" href="#locale.DAY_6" title="Link to this definition">¶</a></dt>
  406. <dt class="sig sig-object py" id="locale.DAY_7">
  407. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_7</span></span><a class="headerlink" href="#locale.DAY_7" title="Link to this definition">¶</a></dt>
  408. <dd><p>Get the name of the n-th day of the week.</p>
  409. <div class="admonition note">
  410. <p class="admonition-title">Note</p>
  411. <p>This follows the US convention of <a class="reference internal" href="#locale.DAY_1" title="locale.DAY_1"><code class="xref py py-const docutils literal notranslate"><span class="pre">DAY_1</span></code></a> being Sunday, not the
  412. international convention (ISO 8601) that Monday is the first day of the
  413. week.</p>
  414. </div>
  415. </dd></dl>
  416. <dl class="py data">
  417. <dt class="sig sig-object py" id="locale.ABDAY_1">
  418. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_1</span></span><a class="headerlink" href="#locale.ABDAY_1" title="Link to this definition">¶</a></dt>
  419. <dt class="sig sig-object py" id="locale.ABDAY_2">
  420. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_2</span></span><a class="headerlink" href="#locale.ABDAY_2" title="Link to this definition">¶</a></dt>
  421. <dt class="sig sig-object py" id="locale.ABDAY_3">
  422. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_3</span></span><a class="headerlink" href="#locale.ABDAY_3" title="Link to this definition">¶</a></dt>
  423. <dt class="sig sig-object py" id="locale.ABDAY_4">
  424. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_4</span></span><a class="headerlink" href="#locale.ABDAY_4" title="Link to this definition">¶</a></dt>
  425. <dt class="sig sig-object py" id="locale.ABDAY_5">
  426. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_5</span></span><a class="headerlink" href="#locale.ABDAY_5" title="Link to this definition">¶</a></dt>
  427. <dt class="sig sig-object py" id="locale.ABDAY_6">
  428. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_6</span></span><a class="headerlink" href="#locale.ABDAY_6" title="Link to this definition">¶</a></dt>
  429. <dt class="sig sig-object py" id="locale.ABDAY_7">
  430. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_7</span></span><a class="headerlink" href="#locale.ABDAY_7" title="Link to this definition">¶</a></dt>
  431. <dd><p>Get the abbreviated name of the n-th day of the week.</p>
  432. </dd></dl>
  433. <dl class="py data">
  434. <dt class="sig sig-object py" id="locale.MON_1">
  435. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_1</span></span><a class="headerlink" href="#locale.MON_1" title="Link to this definition">¶</a></dt>
  436. <dt class="sig sig-object py" id="locale.MON_2">
  437. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_2</span></span><a class="headerlink" href="#locale.MON_2" title="Link to this definition">¶</a></dt>
  438. <dt class="sig sig-object py" id="locale.MON_3">
  439. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_3</span></span><a class="headerlink" href="#locale.MON_3" title="Link to this definition">¶</a></dt>
  440. <dt class="sig sig-object py" id="locale.MON_4">
  441. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_4</span></span><a class="headerlink" href="#locale.MON_4" title="Link to this definition">¶</a></dt>
  442. <dt class="sig sig-object py" id="locale.MON_5">
  443. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_5</span></span><a class="headerlink" href="#locale.MON_5" title="Link to this definition">¶</a></dt>
  444. <dt class="sig sig-object py" id="locale.MON_6">
  445. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_6</span></span><a class="headerlink" href="#locale.MON_6" title="Link to this definition">¶</a></dt>
  446. <dt class="sig sig-object py" id="locale.MON_7">
  447. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_7</span></span><a class="headerlink" href="#locale.MON_7" title="Link to this definition">¶</a></dt>
  448. <dt class="sig sig-object py" id="locale.MON_8">
  449. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_8</span></span><a class="headerlink" href="#locale.MON_8" title="Link to this definition">¶</a></dt>
  450. <dt class="sig sig-object py" id="locale.MON_9">
  451. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_9</span></span><a class="headerlink" href="#locale.MON_9" title="Link to this definition">¶</a></dt>
  452. <dt class="sig sig-object py" id="locale.MON_10">
  453. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_10</span></span><a class="headerlink" href="#locale.MON_10" title="Link to this definition">¶</a></dt>
  454. <dt class="sig sig-object py" id="locale.MON_11">
  455. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_11</span></span><a class="headerlink" href="#locale.MON_11" title="Link to this definition">¶</a></dt>
  456. <dt class="sig sig-object py" id="locale.MON_12">
  457. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_12</span></span><a class="headerlink" href="#locale.MON_12" title="Link to this definition">¶</a></dt>
  458. <dd><p>Get the name of the n-th month.</p>
  459. </dd></dl>
  460. <dl class="py data">
  461. <dt class="sig sig-object py" id="locale.ABMON_1">
  462. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_1</span></span><a class="headerlink" href="#locale.ABMON_1" title="Link to this definition">¶</a></dt>
  463. <dt class="sig sig-object py" id="locale.ABMON_2">
  464. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_2</span></span><a class="headerlink" href="#locale.ABMON_2" title="Link to this definition">¶</a></dt>
  465. <dt class="sig sig-object py" id="locale.ABMON_3">
  466. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_3</span></span><a class="headerlink" href="#locale.ABMON_3" title="Link to this definition">¶</a></dt>
  467. <dt class="sig sig-object py" id="locale.ABMON_4">
  468. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_4</span></span><a class="headerlink" href="#locale.ABMON_4" title="Link to this definition">¶</a></dt>
  469. <dt class="sig sig-object py" id="locale.ABMON_5">
  470. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_5</span></span><a class="headerlink" href="#locale.ABMON_5" title="Link to this definition">¶</a></dt>
  471. <dt class="sig sig-object py" id="locale.ABMON_6">
  472. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_6</span></span><a class="headerlink" href="#locale.ABMON_6" title="Link to this definition">¶</a></dt>
  473. <dt class="sig sig-object py" id="locale.ABMON_7">
  474. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_7</span></span><a class="headerlink" href="#locale.ABMON_7" title="Link to this definition">¶</a></dt>
  475. <dt class="sig sig-object py" id="locale.ABMON_8">
  476. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_8</span></span><a class="headerlink" href="#locale.ABMON_8" title="Link to this definition">¶</a></dt>
  477. <dt class="sig sig-object py" id="locale.ABMON_9">
  478. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_9</span></span><a class="headerlink" href="#locale.ABMON_9" title="Link to this definition">¶</a></dt>
  479. <dt class="sig sig-object py" id="locale.ABMON_10">
  480. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_10</span></span><a class="headerlink" href="#locale.ABMON_10" title="Link to this definition">¶</a></dt>
  481. <dt class="sig sig-object py" id="locale.ABMON_11">
  482. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_11</span></span><a class="headerlink" href="#locale.ABMON_11" title="Link to this definition">¶</a></dt>
  483. <dt class="sig sig-object py" id="locale.ABMON_12">
  484. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_12</span></span><a class="headerlink" href="#locale.ABMON_12" title="Link to this definition">¶</a></dt>
  485. <dd><p>Get the abbreviated name of the n-th month.</p>
  486. </dd></dl>
  487. <dl class="py data">
  488. <dt class="sig sig-object py" id="locale.RADIXCHAR">
  489. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">RADIXCHAR</span></span><a class="headerlink" href="#locale.RADIXCHAR" title="Link to this definition">¶</a></dt>
  490. <dd><p>Get the radix character (decimal dot, decimal comma, etc.).</p>
  491. </dd></dl>
  492. <dl class="py data">
  493. <dt class="sig sig-object py" id="locale.THOUSEP">
  494. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">THOUSEP</span></span><a class="headerlink" href="#locale.THOUSEP" title="Link to this definition">¶</a></dt>
  495. <dd><p>Get the separator character for thousands (groups of three digits).</p>
  496. </dd></dl>
  497. <dl class="py data">
  498. <dt class="sig sig-object py" id="locale.YESEXPR">
  499. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">YESEXPR</span></span><a class="headerlink" href="#locale.YESEXPR" title="Link to this definition">¶</a></dt>
  500. <dd><p>Get a regular expression that can be used with the regex function to
  501. recognize a positive response to a yes/no question.</p>
  502. </dd></dl>
  503. <dl class="py data">
  504. <dt class="sig sig-object py" id="locale.NOEXPR">
  505. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">NOEXPR</span></span><a class="headerlink" href="#locale.NOEXPR" title="Link to this definition">¶</a></dt>
  506. <dd><p>Get a regular expression that can be used with the <code class="docutils literal notranslate"><span class="pre">regex(3)</span></code> function to
  507. recognize a negative response to a yes/no question.</p>
  508. <div class="admonition note">
  509. <p class="admonition-title">Note</p>
  510. <p>The regular expressions for <a class="reference internal" href="#locale.YESEXPR" title="locale.YESEXPR"><code class="xref py py-const docutils literal notranslate"><span class="pre">YESEXPR</span></code></a> and
  511. <a class="reference internal" href="#locale.NOEXPR" title="locale.NOEXPR"><code class="xref py py-const docutils literal notranslate"><span class="pre">NOEXPR</span></code></a> use syntax suitable for the
  512. <code class="docutils literal notranslate"><span class="pre">regex</span></code> function from the C library, which might
  513. differ from the syntax used in <a class="reference internal" href="re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a>.</p>
  514. </div>
  515. </dd></dl>
  516. <dl class="py data">
  517. <dt class="sig sig-object py" id="locale.CRNCYSTR">
  518. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">CRNCYSTR</span></span><a class="headerlink" href="#locale.CRNCYSTR" title="Link to this definition">¶</a></dt>
  519. <dd><p>Get the currency symbol, preceded by “-” if the symbol should appear before
  520. the value, “+” if the symbol should appear after the value, or “.” if the
  521. symbol should replace the radix character.</p>
  522. </dd></dl>
  523. <dl class="py data">
  524. <dt class="sig sig-object py" id="locale.ERA">
  525. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA</span></span><a class="headerlink" href="#locale.ERA" title="Link to this definition">¶</a></dt>
  526. <dd><p>Get a string that represents the era used in the current locale.</p>
  527. <p>Most locales do not define this value. An example of a locale which does
  528. define this value is the Japanese one. In Japan, the traditional
  529. representation of dates includes the name of the era corresponding to the
  530. then-emperor’s reign.</p>
  531. <p>Normally it should not be necessary to use this value directly. Specifying
  532. the <code class="docutils literal notranslate"><span class="pre">E</span></code> modifier in their format strings causes the <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>
  533. function to use this information. The format of the returned string is not
  534. specified, and therefore you should not assume knowledge of it on different
  535. systems.</p>
  536. </dd></dl>
  537. <dl class="py data">
  538. <dt class="sig sig-object py" id="locale.ERA_D_T_FMT">
  539. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA_D_T_FMT</span></span><a class="headerlink" href="#locale.ERA_D_T_FMT" title="Link to this definition">¶</a></dt>
  540. <dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent date and time in a
  541. locale-specific era-based way.</p>
  542. </dd></dl>
  543. <dl class="py data">
  544. <dt class="sig sig-object py" id="locale.ERA_D_FMT">
  545. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA_D_FMT</span></span><a class="headerlink" href="#locale.ERA_D_FMT" title="Link to this definition">¶</a></dt>
  546. <dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent a date in a
  547. locale-specific era-based way.</p>
  548. </dd></dl>
  549. <dl class="py data">
  550. <dt class="sig sig-object py" id="locale.ERA_T_FMT">
  551. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA_T_FMT</span></span><a class="headerlink" href="#locale.ERA_T_FMT" title="Link to this definition">¶</a></dt>
  552. <dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent a time in a
  553. locale-specific era-based way.</p>
  554. </dd></dl>
  555. <dl class="py data">
  556. <dt class="sig sig-object py" id="locale.ALT_DIGITS">
  557. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ALT_DIGITS</span></span><a class="headerlink" href="#locale.ALT_DIGITS" title="Link to this definition">¶</a></dt>
  558. <dd><p>Get a representation of up to 100 values used to represent the values
  559. 0 to 99.</p>
  560. </dd></dl>
  561. </dd></dl>
  562. <dl class="py function">
  563. <dt class="sig sig-object py" id="locale.getdefaultlocale">
  564. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getdefaultlocale</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">envvars</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#locale.getdefaultlocale" title="Link to this definition">¶</a></dt>
  565. <dd><p>Tries to determine the default locale settings and returns them as a tuple of
  566. the form <code class="docutils literal notranslate"><span class="pre">(language</span> <span class="pre">code,</span> <span class="pre">encoding)</span></code>.</p>
  567. <p>According to POSIX, a program which has not called <code class="docutils literal notranslate"><span class="pre">setlocale(LC_ALL,</span> <span class="pre">'')</span></code>
  568. runs using the portable <code class="docutils literal notranslate"><span class="pre">'C'</span></code> locale. Calling <code class="docutils literal notranslate"><span class="pre">setlocale(LC_ALL,</span> <span class="pre">'')</span></code> lets
  569. it use the default locale as defined by the <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code> variable. Since we
  570. do not want to interfere with the current locale setting we thus emulate the
  571. behavior in the way described above.</p>
  572. <p>To maintain compatibility with other platforms, not only the <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code>
  573. variable is tested, but a list of variables given as envvars parameter. The
  574. first found to be defined will be used. <em>envvars</em> defaults to the search
  575. path used in GNU gettext; it must always contain the variable name
  576. <code class="docutils literal notranslate"><span class="pre">'LANG'</span></code>. The GNU gettext search path contains <code class="docutils literal notranslate"><span class="pre">'LC_ALL'</span></code>,
  577. <code class="docutils literal notranslate"><span class="pre">'LC_CTYPE'</span></code>, <code class="docutils literal notranslate"><span class="pre">'LANG'</span></code> and <code class="docutils literal notranslate"><span class="pre">'LANGUAGE'</span></code>, in that order.</p>
  578. <p>Except for the code <code class="docutils literal notranslate"><span class="pre">'C'</span></code>, the language code corresponds to <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1766.html"><strong>RFC 1766</strong></a>.
  579. <em>language code</em> and <em>encoding</em> may be <code class="docutils literal notranslate"><span class="pre">None</span></code> if their values cannot be
  580. determined.</p>
  581. <div class="deprecated-removed">
  582. <p><span class="versionmodified">Deprecated since version 3.11, will be removed in version 3.15.</span></p>
  583. </div>
  584. </dd></dl>
  585. <dl class="py function">
  586. <dt class="sig sig-object py" id="locale.getlocale">
  587. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getlocale</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">LC_CTYPE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.getlocale" title="Link to this definition">¶</a></dt>
  588. <dd><p>Returns the current setting for the given locale category as sequence containing
  589. <em>language code</em>, <em>encoding</em>. <em>category</em> may be one of the <code class="xref py py-const docutils literal notranslate"><span class="pre">LC_*</span></code> values
  590. except <a class="reference internal" href="#locale.LC_ALL" title="locale.LC_ALL"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_ALL</span></code></a>. It defaults to <a class="reference internal" href="#locale.LC_CTYPE" title="locale.LC_CTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_CTYPE</span></code></a>.</p>
  591. <p>Except for the code <code class="docutils literal notranslate"><span class="pre">'C'</span></code>, the language code corresponds to <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1766.html"><strong>RFC 1766</strong></a>.
  592. <em>language code</em> and <em>encoding</em> may be <code class="docutils literal notranslate"><span class="pre">None</span></code> if their values cannot be
  593. determined.</p>
  594. </dd></dl>
  595. <dl class="py function">
  596. <dt class="sig sig-object py" id="locale.getpreferredencoding">
  597. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getpreferredencoding</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">do_setlocale</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.getpreferredencoding" title="Link to this definition">¶</a></dt>
  598. <dd><p>Return the <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a> used for text data, according to user
  599. preferences. User preferences are expressed differently on different
  600. systems, and might not be available programmatically on some systems, so
  601. this function only returns a guess.</p>
  602. <p>On some systems, it is necessary to invoke <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> to obtain the
  603. user preferences, so this function is not thread-safe. If invoking setlocale
  604. is not necessary or desired, <em>do_setlocale</em> should be set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  605. <p>On Android or if the <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled, always
  606. return <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code>, the <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a> and the <em>do_setlocale</em>
  607. argument are ignored.</p>
  608. <p>The <a class="reference internal" href="../c-api/init_config.html#c-preinit"><span class="std std-ref">Python preinitialization</span></a> configures the LC_CTYPE
  609. locale. See also the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>
  610. <div class="versionchanged">
  611. <p><span class="versionmodified changed">Changed in version 3.7: </span>The function now always returns <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code> on Android or if the
  612. <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled.</p>
  613. </div>
  614. </dd></dl>
  615. <dl class="py function">
  616. <dt class="sig sig-object py" id="locale.getencoding">
  617. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getencoding</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#locale.getencoding" title="Link to this definition">¶</a></dt>
  618. <dd><p>Get the current <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a>:</p>
  619. <ul class="simple">
  620. <li><p>On Android and VxWorks, return <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code>.</p></li>
  621. <li><p>On Unix, return the encoding of the current <a class="reference internal" href="#locale.LC_CTYPE" title="locale.LC_CTYPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">LC_CTYPE</span></code></a> locale.
  622. Return <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code> if <code class="docutils literal notranslate"><span class="pre">nl_langinfo(CODESET)</span></code> returns an empty string:
  623. for example, if the current LC_CTYPE locale is not supported.</p></li>
  624. <li><p>On Windows, return the ANSI code page.</p></li>
  625. </ul>
  626. <p>The <a class="reference internal" href="../c-api/init_config.html#c-preinit"><span class="std std-ref">Python preinitialization</span></a> configures the LC_CTYPE
  627. locale. See also the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>
  628. <p>This function is similar to
  629. <a class="reference internal" href="#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpreferredencoding(False)</span></code></a> except this
  630. function ignores the <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a>.</p>
  631. <div class="versionadded">
  632. <p><span class="versionmodified added">New in version 3.11.</span></p>
  633. </div>
  634. </dd></dl>
  635. <dl class="py function">
  636. <dt class="sig sig-object py" id="locale.normalize">
  637. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">normalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">localename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.normalize" title="Link to this definition">¶</a></dt>
  638. <dd><p>Returns a normalized locale code for the given locale name. The returned locale
  639. code is formatted for use with <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a>. If normalization fails, the
  640. original name is returned unchanged.</p>
  641. <p>If the given encoding is not known, the function defaults to the default
  642. encoding for the locale code just like <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a>.</p>
  643. </dd></dl>
  644. <dl class="py function">
  645. <dt class="sig sig-object py" id="locale.resetlocale">
  646. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">resetlocale</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">LC_ALL</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.resetlocale" title="Link to this definition">¶</a></dt>
  647. <dd><p>Sets the locale for <em>category</em> to the default setting.</p>
  648. <p>The default setting is determined by calling <a class="reference internal" href="#locale.getdefaultlocale" title="locale.getdefaultlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">getdefaultlocale()</span></code></a>.
  649. <em>category</em> defaults to <a class="reference internal" href="#locale.LC_ALL" title="locale.LC_ALL"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_ALL</span></code></a>.</p>
  650. <div class="deprecated-removed">
  651. <p><span class="versionmodified">Deprecated since version 3.11, will be removed in version 3.13.</span></p>
  652. </div>
  653. </dd></dl>
  654. <dl class="py function">
  655. <dt class="sig sig-object py" id="locale.strcoll">
  656. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">strcoll</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">string2</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.strcoll" title="Link to this definition">¶</a></dt>
  657. <dd><p>Compares two strings according to the current <a class="reference internal" href="#locale.LC_COLLATE" title="locale.LC_COLLATE"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_COLLATE</span></code></a> setting. As
  658. any other compare function, returns a negative, or a positive value, or <code class="docutils literal notranslate"><span class="pre">0</span></code>,
  659. depending on whether <em>string1</em> collates before or after <em>string2</em> or is equal to
  660. it.</p>
  661. </dd></dl>
  662. <dl class="py function">
  663. <dt class="sig sig-object py" id="locale.strxfrm">
  664. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">strxfrm</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.strxfrm" title="Link to this definition">¶</a></dt>
  665. <dd><p>Transforms a string to one that can be used in locale-aware
  666. comparisons. For example, <code class="docutils literal notranslate"><span class="pre">strxfrm(s1)</span> <span class="pre">&lt;</span> <span class="pre">strxfrm(s2)</span></code> is
  667. equivalent to <code class="docutils literal notranslate"><span class="pre">strcoll(s1,</span> <span class="pre">s2)</span> <span class="pre">&lt;</span> <span class="pre">0</span></code>. This function can be used
  668. when the same string is compared repeatedly, e.g. when collating a
  669. sequence of strings.</p>
  670. </dd></dl>
  671. <dl class="py function">
  672. <dt class="sig sig-object py" id="locale.format_string">
  673. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">format_string</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">val</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grouping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">monetary</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="#locale.format_string" title="Link to this definition">¶</a></dt>
  674. <dd><p>Formats a number <em>val</em> according to the current <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> setting.
  675. The format follows the conventions of the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator. For floating point
  676. values, the decimal point is modified if appropriate. If <em>grouping</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>,
  677. also takes the grouping into account.</p>
  678. <p>If <em>monetary</em> is true, the conversion uses monetary thousands separator and
  679. grouping strings.</p>
  680. <p>Processes formatting specifiers as in <code class="docutils literal notranslate"><span class="pre">format</span> <span class="pre">%</span> <span class="pre">val</span></code>, but takes the current
  681. locale settings into account.</p>
  682. <div class="versionchanged">
  683. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>monetary</em> keyword parameter was added.</p>
  684. </div>
  685. </dd></dl>
  686. <dl class="py function">
  687. <dt class="sig sig-object py" id="locale.currency">
  688. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">currency</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">val</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grouping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">international</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="#locale.currency" title="Link to this definition">¶</a></dt>
  689. <dd><p>Formats a number <em>val</em> according to the current <a class="reference internal" href="#locale.LC_MONETARY" title="locale.LC_MONETARY"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_MONETARY</span></code></a> settings.</p>
  690. <p>The returned string includes the currency symbol if <em>symbol</em> is true, which is
  691. the default. If <em>grouping</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (which is not the default), grouping is done
  692. with the value. If <em>international</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (which is not the default), the
  693. international currency symbol is used.</p>
  694. <div class="admonition note">
  695. <p class="admonition-title">Note</p>
  696. <p>This function will not work with the ‘C’ locale, so you have to set a
  697. locale via <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> first.</p>
  698. </div>
  699. </dd></dl>
  700. <dl class="py function">
  701. <dt class="sig sig-object py" id="locale.str">
  702. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">str</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">float</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.str" title="Link to this definition">¶</a></dt>
  703. <dd><p>Formats a floating point number using the same format as the built-in function
  704. <code class="docutils literal notranslate"><span class="pre">str(float)</span></code>, but takes the decimal point into account.</p>
  705. </dd></dl>
  706. <dl class="py function">
  707. <dt class="sig sig-object py" id="locale.delocalize">
  708. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">delocalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.delocalize" title="Link to this definition">¶</a></dt>
  709. <dd><p>Converts a string into a normalized number string, following the
  710. <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> settings.</p>
  711. <div class="versionadded">
  712. <p><span class="versionmodified added">New in version 3.5.</span></p>
  713. </div>
  714. </dd></dl>
  715. <dl class="py function">
  716. <dt class="sig sig-object py" id="locale.localize">
  717. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">localize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grouping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">monetary</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="#locale.localize" title="Link to this definition">¶</a></dt>
  718. <dd><p>Converts a normalized number string into a formatted string following the
  719. <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> settings.</p>
  720. <div class="versionadded">
  721. <p><span class="versionmodified added">New in version 3.10.</span></p>
  722. </div>
  723. </dd></dl>
  724. <dl class="py function">
  725. <dt class="sig sig-object py" id="locale.atof">
  726. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">atof</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">float</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.atof" title="Link to this definition">¶</a></dt>
  727. <dd><p>Converts a string to a number, following the <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> settings,
  728. by calling <em>func</em> on the result of calling <a class="reference internal" href="#locale.delocalize" title="locale.delocalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">delocalize()</span></code></a> on <em>string</em>.</p>
  729. </dd></dl>
  730. <dl class="py function">
  731. <dt class="sig sig-object py" id="locale.atoi">
  732. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">atoi</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.atoi" title="Link to this definition">¶</a></dt>
  733. <dd><p>Converts a string to an integer, following the <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> conventions.</p>
  734. </dd></dl>
  735. <dl class="py data">
  736. <dt class="sig sig-object py" id="locale.LC_CTYPE">
  737. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_CTYPE</span></span><a class="headerlink" href="#locale.LC_CTYPE" title="Link to this definition">¶</a></dt>
  738. <dd><p>Locale category for the character type functions. Most importantly, this
  739. category defines the text encoding, i.e. how bytes are interpreted as
  740. Unicode codepoints. See <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0538/"><strong>PEP 538</strong></a> and <span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0540/"><strong>PEP 540</strong></a> for how this variable
  741. might be automatically coerced to <code class="docutils literal notranslate"><span class="pre">C.UTF-8</span></code> to avoid issues created by
  742. invalid settings in containers or incompatible settings passed over remote
  743. SSH connections.</p>
  744. <p>Python doesn’t internally use locale-dependent character transformation functions
  745. from <code class="docutils literal notranslate"><span class="pre">ctype.h</span></code>. Instead, an internal <code class="docutils literal notranslate"><span class="pre">pyctype.h</span></code> provides locale-independent
  746. equivalents like <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TOLOWER</span></code>.</p>
  747. </dd></dl>
  748. <dl class="py data">
  749. <dt class="sig sig-object py" id="locale.LC_COLLATE">
  750. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_COLLATE</span></span><a class="headerlink" href="#locale.LC_COLLATE" title="Link to this definition">¶</a></dt>
  751. <dd><p>Locale category for sorting strings. The functions <a class="reference internal" href="#locale.strcoll" title="locale.strcoll"><code class="xref py py-func docutils literal notranslate"><span class="pre">strcoll()</span></code></a> and
  752. <a class="reference internal" href="#locale.strxfrm" title="locale.strxfrm"><code class="xref py py-func docutils literal notranslate"><span class="pre">strxfrm()</span></code></a> of the <a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module are affected.</p>
  753. </dd></dl>
  754. <dl class="py data">
  755. <dt class="sig sig-object py" id="locale.LC_TIME">
  756. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_TIME</span></span><a class="headerlink" href="#locale.LC_TIME" title="Link to this definition">¶</a></dt>
  757. <dd><p>Locale category for the formatting of time. The function <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>
  758. follows these conventions.</p>
  759. </dd></dl>
  760. <dl class="py data">
  761. <dt class="sig sig-object py" id="locale.LC_MONETARY">
  762. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_MONETARY</span></span><a class="headerlink" href="#locale.LC_MONETARY" title="Link to this definition">¶</a></dt>
  763. <dd><p>Locale category for formatting of monetary values. The available options are
  764. available from the <a class="reference internal" href="#locale.localeconv" title="locale.localeconv"><code class="xref py py-func docutils literal notranslate"><span class="pre">localeconv()</span></code></a> function.</p>
  765. </dd></dl>
  766. <dl class="py data">
  767. <dt class="sig sig-object py" id="locale.LC_MESSAGES">
  768. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_MESSAGES</span></span><a class="headerlink" href="#locale.LC_MESSAGES" title="Link to this definition">¶</a></dt>
  769. <dd><p>Locale category for message display. Python currently does not support
  770. application specific locale-aware messages. Messages displayed by the operating
  771. system, like those returned by <a class="reference internal" href="os.html#os.strerror" title="os.strerror"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.strerror()</span></code></a> might be affected by this
  772. category.</p>
  773. <p>This value may not be available on operating systems not conforming to the
  774. POSIX standard, most notably Windows.</p>
  775. </dd></dl>
  776. <dl class="py data">
  777. <dt class="sig sig-object py" id="locale.LC_NUMERIC">
  778. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_NUMERIC</span></span><a class="headerlink" href="#locale.LC_NUMERIC" title="Link to this definition">¶</a></dt>
  779. <dd><p>Locale category for formatting numbers. The functions <a class="reference internal" href="#locale.format_string" title="locale.format_string"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_string()</span></code></a>,
  780. <a class="reference internal" href="#locale.atoi" title="locale.atoi"><code class="xref py py-func docutils literal notranslate"><span class="pre">atoi()</span></code></a>, <a class="reference internal" href="#locale.atof" title="locale.atof"><code class="xref py py-func docutils literal notranslate"><span class="pre">atof()</span></code></a> and <a class="reference internal" href="#locale.str" title="locale.str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the <a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module are
  781. affected by that category. All other numeric formatting operations are not
  782. affected.</p>
  783. </dd></dl>
  784. <dl class="py data">
  785. <dt class="sig sig-object py" id="locale.LC_ALL">
  786. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_ALL</span></span><a class="headerlink" href="#locale.LC_ALL" title="Link to this definition">¶</a></dt>
  787. <dd><p>Combination of all locale settings. If this flag is used when the locale is
  788. changed, setting the locale for all categories is attempted. If that fails for
  789. any category, no category is changed at all. When the locale is retrieved using
  790. this flag, a string indicating the setting for all categories is returned. This
  791. string can be later used to restore the settings.</p>
  792. </dd></dl>
  793. <dl class="py data">
  794. <dt class="sig sig-object py" id="locale.CHAR_MAX">
  795. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">CHAR_MAX</span></span><a class="headerlink" href="#locale.CHAR_MAX" title="Link to this definition">¶</a></dt>
  796. <dd><p>This is a symbolic constant used for different values returned by
  797. <a class="reference internal" href="#locale.localeconv" title="locale.localeconv"><code class="xref py py-func docutils literal notranslate"><span class="pre">localeconv()</span></code></a>.</p>
  798. </dd></dl>
  799. <p>Example:</p>
  800. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">locale</span>
  801. <span class="gp">&gt;&gt;&gt; </span><span class="n">loc</span> <span class="o">=</span> <span class="n">locale</span><span class="o">.</span><span class="n">getlocale</span><span class="p">()</span> <span class="c1"># get current locale</span>
  802. <span class="go"># use German locale; name might vary with platform</span>
  803. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">&#39;de_DE&#39;</span><span class="p">)</span>
  804. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">strcoll</span><span class="p">(</span><span class="s1">&#39;f</span><span class="se">\xe4</span><span class="s1">n&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="c1"># compare a string containing an umlaut</span>
  805. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="c1"># use user&#39;s preferred locale</span>
  806. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">&#39;C&#39;</span><span class="p">)</span> <span class="c1"># use default (C) locale</span>
  807. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="n">loc</span><span class="p">)</span> <span class="c1"># restore saved locale</span>
  808. </pre></div>
  809. </div>
  810. <section id="background-details-hints-tips-and-caveats">
  811. <h2>Background, details, hints, tips and caveats<a class="headerlink" href="#background-details-hints-tips-and-caveats" title="Link to this heading">¶</a></h2>
  812. <p>The C standard defines the locale as a program-wide property that may be
  813. relatively expensive to change. On top of that, some implementations are broken
  814. in such a way that frequent locale changes may cause core dumps. This makes the
  815. locale somewhat painful to use correctly.</p>
  816. <p>Initially, when a program is started, the locale is the <code class="docutils literal notranslate"><span class="pre">C</span></code> locale, no matter
  817. what the user’s preferred locale is. There is one exception: the
  818. <a class="reference internal" href="#locale.LC_CTYPE" title="locale.LC_CTYPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">LC_CTYPE</span></code></a> category is changed at startup to set the current locale
  819. encoding to the user’s preferred locale encoding. The program must explicitly
  820. say that it wants the user’s preferred locale settings for other categories by
  821. calling <code class="docutils literal notranslate"><span class="pre">setlocale(LC_ALL,</span> <span class="pre">'')</span></code>.</p>
  822. <p>It is generally a bad idea to call <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> in some library routine,
  823. since as a side effect it affects the entire program. Saving and restoring it
  824. is almost as bad: it is expensive and affects other threads that happen to run
  825. before the settings have been restored.</p>
  826. <p>If, when coding a module for general use, you need a locale independent version
  827. of an operation that is affected by the locale (such as
  828. certain formats used with <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>), you will have to find a way to
  829. do it without using the standard library routine. Even better is convincing
  830. yourself that using locale settings is okay. Only as a last resort should you
  831. document that your module is not compatible with non-<code class="docutils literal notranslate"><span class="pre">C</span></code> locale settings.</p>
  832. <p>The only way to perform numeric operations according to the locale is to use the
  833. special functions defined by this module: <a class="reference internal" href="#locale.atof" title="locale.atof"><code class="xref py py-func docutils literal notranslate"><span class="pre">atof()</span></code></a>, <a class="reference internal" href="#locale.atoi" title="locale.atoi"><code class="xref py py-func docutils literal notranslate"><span class="pre">atoi()</span></code></a>,
  834. <a class="reference internal" href="#locale.format_string" title="locale.format_string"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_string()</span></code></a>, <a class="reference internal" href="#locale.str" title="locale.str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>.</p>
  835. <p>There is no way to perform case conversions and character classifications
  836. according to the locale. For (Unicode) text strings these are done according
  837. to the character value only, while for byte strings, the conversions and
  838. classifications are done according to the ASCII value of the byte, and bytes
  839. whose high bit is set (i.e., non-ASCII bytes) are never converted or considered
  840. part of a character class such as letter or whitespace.</p>
  841. </section>
  842. <section id="for-extension-writers-and-programs-that-embed-python">
  843. <span id="embedding-locale"></span><h2>For extension writers and programs that embed Python<a class="headerlink" href="#for-extension-writers-and-programs-that-embed-python" title="Link to this heading">¶</a></h2>
  844. <p>Extension modules should never call <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a>, except to find out what
  845. the current locale is. But since the return value can only be used portably to
  846. restore it, that is not very useful (except perhaps to find out whether or not
  847. the locale is <code class="docutils literal notranslate"><span class="pre">C</span></code>).</p>
  848. <p>When Python code uses the <a class="reference internal" href="#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module to change the locale, this also
  849. affects the embedding application. If the embedding application doesn’t want
  850. this to happen, it should remove the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_locale</span></code> extension module (which does
  851. all the work) from the table of built-in modules in the <code class="file docutils literal notranslate"><span class="pre">config.c</span></code> file,
  852. and make sure that the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_locale</span></code> module is not accessible as a shared
  853. library.</p>
  854. </section>
  855. <section id="access-to-message-catalogs">
  856. <span id="locale-gettext"></span><h2>Access to message catalogs<a class="headerlink" href="#access-to-message-catalogs" title="Link to this heading">¶</a></h2>
  857. <dl class="py function">
  858. <dt class="sig sig-object py" id="locale.gettext">
  859. <span class="sig-prename descclassname"><span class="pre">locale.</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">msg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.gettext" title="Link to this definition">¶</a></dt>
  860. <dd></dd></dl>
  861. <dl class="py function">
  862. <dt class="sig sig-object py" id="locale.dgettext">
  863. <span class="sig-prename descclassname"><span class="pre">locale.</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">msg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.dgettext" title="Link to this definition">¶</a></dt>
  864. <dd></dd></dl>
  865. <dl class="py function">
  866. <dt class="sig sig-object py" id="locale.dcgettext">
  867. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">dcgettext</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">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">category</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.dcgettext" title="Link to this definition">¶</a></dt>
  868. <dd></dd></dl>
  869. <dl class="py function">
  870. <dt class="sig sig-object py" id="locale.textdomain">
  871. <span class="sig-prename descclassname"><span class="pre">locale.</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></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.textdomain" title="Link to this definition">¶</a></dt>
  872. <dd></dd></dl>
  873. <dl class="py function">
  874. <dt class="sig sig-object py" id="locale.bindtextdomain">
  875. <span class="sig-prename descclassname"><span class="pre">locale.</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">dir</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.bindtextdomain" title="Link to this definition">¶</a></dt>
  876. <dd></dd></dl>
  877. <dl class="py function">
  878. <dt class="sig sig-object py" id="locale.bind_textdomain_codeset">
  879. <span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">bind_textdomain_codeset</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">codeset</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.bind_textdomain_codeset" title="Link to this definition">¶</a></dt>
  880. <dd></dd></dl>
  881. <p>The locale module exposes the C library’s gettext interface on systems that
  882. provide this interface. It consists of the functions <a class="reference internal" href="gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext()</span></code></a>,
  883. <a class="reference internal" href="#locale.dgettext" title="locale.dgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dgettext()</span></code></a>, <a class="reference internal" href="#locale.dcgettext" title="locale.dcgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dcgettext()</span></code></a>, <a class="reference internal" href="#locale.textdomain" title="locale.textdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">textdomain()</span></code></a>, <a class="reference internal" href="#locale.bindtextdomain" title="locale.bindtextdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">bindtextdomain()</span></code></a>,
  884. and <a class="reference internal" href="#locale.bind_textdomain_codeset" title="locale.bind_textdomain_codeset"><code class="xref py py-func docutils literal notranslate"><span class="pre">bind_textdomain_codeset()</span></code></a>. These are similar to the same functions in
  885. the <a class="reference internal" href="gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> module, but use the C library’s binary format for message
  886. catalogs, and the C library’s search algorithms for locating message catalogs.</p>
  887. <p>Python applications should normally find no need to invoke these functions, and
  888. should use <a class="reference internal" href="gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> instead. A known exception to this rule are
  889. applications that link with additional C libraries which internally invoke
  890. C functions <code class="docutils literal notranslate"><span class="pre">gettext</span></code> or <code class="docutils literal notranslate"><span class="pre">dcgettext</span></code>. For these applications, it may be
  891. necessary to bind the text domain, so that the libraries can properly locate
  892. their message catalogs.</p>
  893. </section>
  894. </section>
  895. <div class="clearer"></div>
  896. </div>
  897. </div>
  898. </div>
  899. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  900. <div class="sphinxsidebarwrapper">
  901. <div>
  902. <h3><a href="../contents.html">Table of Contents</a></h3>
  903. <ul>
  904. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a><ul>
  905. <li><a class="reference internal" href="#background-details-hints-tips-and-caveats">Background, details, hints, tips and caveats</a></li>
  906. <li><a class="reference internal" href="#for-extension-writers-and-programs-that-embed-python">For extension writers and programs that embed Python</a></li>
  907. <li><a class="reference internal" href="#access-to-message-catalogs">Access to message catalogs</a></li>
  908. </ul>
  909. </li>
  910. </ul>
  911. </div>
  912. <div>
  913. <h4>Previous topic</h4>
  914. <p class="topless"><a href="gettext.html"
  915. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a></p>
  916. </div>
  917. <div>
  918. <h4>Next topic</h4>
  919. <p class="topless"><a href="frameworks.html"
  920. title="next chapter">Program Frameworks</a></p>
  921. </div>
  922. <div role="note" aria-label="source link">
  923. <h3>This Page</h3>
  924. <ul class="this-page-menu">
  925. <li><a href="../bugs.html">Report a Bug</a></li>
  926. <li>
  927. <a href="https://github.com/python/cpython/blob/main/Doc/library/locale.rst"
  928. rel="nofollow">Show Source
  929. </a>
  930. </li>
  931. </ul>
  932. </div>
  933. </div>
  934. <div id="sidebarbutton" title="Collapse sidebar">
  935. <span>«</span>
  936. </div>
  937. </div>
  938. <div class="clearer"></div>
  939. </div>
  940. <div class="related" role="navigation" aria-label="related navigation">
  941. <h3>Navigation</h3>
  942. <ul>
  943. <li class="right" style="margin-right: 10px">
  944. <a href="../genindex.html" title="General Index"
  945. >index</a></li>
  946. <li class="right" >
  947. <a href="../py-modindex.html" title="Python Module Index"
  948. >modules</a> |</li>
  949. <li class="right" >
  950. <a href="frameworks.html" title="Program Frameworks"
  951. >next</a> |</li>
  952. <li class="right" >
  953. <a href="gettext.html" title="gettext — Multilingual internationalization services"
  954. >previous</a> |</li>
  955. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  956. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  957. <li class="switchers">
  958. <div class="language_switcher_placeholder"></div>
  959. <div class="version_switcher_placeholder"></div>
  960. </li>
  961. <li>
  962. </li>
  963. <li id="cpython-language-and-version">
  964. <a href="../index.html">3.12.3 Documentation</a> &#187;
  965. </li>
  966. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  967. <li class="nav-item nav-item-2"><a href="i18n.html" >Internationalization</a> &#187;</li>
  968. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a></li>
  969. <li class="right">
  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. Theme
  981. <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. </ul>
  988. </div>
  989. <div class="footer">
  990. &copy;
  991. <a href="../copyright.html">
  992. Copyright
  993. </a>
  994. 2001-2024, Python Software Foundation.
  995. <br />
  996. This page is licensed under the Python Software Foundation License Version 2.
  997. <br />
  998. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  999. <br />
  1000. See <a href="/license.html">History and License</a> for more information.<br />
  1001. <br />
  1002. The Python Software Foundation is a non-profit corporation.
  1003. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1004. <br />
  1005. <br />
  1006. Last updated on Apr 09, 2024 (13:47 UTC).
  1007. <a href="/bugs.html">Found a bug</a>?
  1008. <br />
  1009. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1010. </div>
  1011. </body>
  1012. </html>
上海开阖软件有限公司 沪ICP备12045867号-1