1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/">
<title>Babel: Locale Display Names</title>
<link rel="stylesheet" href="common/style/edgewall.css" type="text/css">
</head>
<body>
<div class="document" id="locale-display-names">
<div id="navigation">
<span class="projinfo">Babel 0.9.1</span>
<a href="index.html">Documentation Index</a>
</div>
<h1 class="title">Locale Display Names</h1>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#introduction" id="id1" name="id1">1 Introduction</a></li>
<li><a class="reference" href="#the-locale-class" id="id2" name="id2">2 The <tt class="docutils literal"><span class="pre">Locale</span></tt> Class</a></li>
<li><a class="reference" href="#calender-display-names" id="id3" name="id3">3 Calender Display Names</a></li>
</ul>
</div>
<div class="section">
<h1><a id="introduction" name="introduction">1 Introduction</a></h1>
<p>While <a class="reference" href="messages.html">message catalogs</a> allow you to localize any messages
in your application, there are a number of strings that are used in many
applications for which translations are readily available.</p>
<p>Imagine for example you have a list of countries that users can choose from,
and you'd like to display the names of those countries in the language the
user prefers. Instead of translating all those country names yourself in your
application, you can make use of the translations provided by the locale data
included with Babel, which is based on the <a class="reference" href="http://unicode.org/cldr/">Common Locale Data Repository
(CLDR)</a> developed and maintained by the <a class="reference" href="http://unicode.org/">Unicode
Consortium</a>.</p>
</div>
<div class="section">
<h1><a id="the-locale-class" name="the-locale-class">2 The <tt class="docutils literal"><span class="pre">Locale</span></tt> Class</a></h1>
<p>You normally access such locale data through the <a class="reference" href="api/babel.core.Locale-class.html">Locale</a> class provided
by Babel:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="k">from</span> <span class="nn">babel</span> <span class="k">import</span> <span class="n">Locale</span>
<span class="gp">>>> </span><span class="n">locale</span> <span class="o">=</span> <span class="n">Locale</span><span class="p">(</span><span class="s">'en'</span><span class="p">,</span> <span class="s">'US'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">territories</span><span class="p">[</span><span class="s">'US'</span><span class="p">]</span>
<span class="go">u'United States'</span>
<span class="gp">>>> </span><span class="n">locale</span> <span class="o">=</span> <span class="n">Locale</span><span class="p">(</span><span class="s">'es'</span><span class="p">,</span> <span class="s">'MX'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">territories</span><span class="p">[</span><span class="s">'US'</span><span class="p">]</span>
<span class="go">u'Estados Unidos'</span>
</pre></div>
<p>In addition to country/territory names, the locale data also provides access to
names of languages, scripts, variants, time zones, and more. Some of the data
is closely related to number and date formatting.</p>
<p>Most of the corresponding <tt class="docutils literal"><span class="pre">Locale</span></tt> properties return dictionaries, where the
key is a code such as the ISO country and language codes. Consult the API
documentation for references to the relevant specifications.</p>
</div>
<div class="section">
<h1><a id="calender-display-names" name="calender-display-names">3 Calender Display Names</a></h1>
<p>The <a class="reference" href="api/babel.core.Locale-class.html">Locale</a> class provides access to many locale display names related to
calendar display, such as the names of week days or months.</p>
<p>These display names are of course used for date formatting, but can also be
used, for example, to show a list of months to the user in their preferred
language:</p>
<div class="highlight"><pre><span class="gp">>>> </span><span class="n">locale</span> <span class="o">=</span> <span class="n">Locale</span><span class="p">(</span><span class="s">'es'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">month_names</span> <span class="o">=</span> <span class="n">locale</span><span class="o">.</span><span class="n">months</span><span class="p">[</span><span class="s">'format'</span><span class="p">][</span><span class="s">'wide'</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">month_names</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">month_names</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">name</span>
<span class="go">enero</span>
<span class="go">febrero</span>
<span class="go">marzo</span>
<span class="go">abril</span>
<span class="go">mayo</span>
<span class="go">junio</span>
<span class="go">julio</span>
<span class="go">agosto</span>
<span class="go">septiembre</span>
<span class="go">octubre</span>
<span class="go">noviembre</span>
<span class="go">diciembre</span>
</pre></div>
</div>
<div id="footer">
Visit the Babel open source project at
<a href="http://babel.edgewall.org/">http://babel.edgewall.org/</a>
</div>
</div>
</body>
</html>
|