Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 964

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Translation &#8212; Django 1.8.19 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.8.19',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../../contents.html" />
    <link rel="up" title="Internationalization and localization" href="index.html" />
    <link rel="next" title="Format localization" href="formatting.html" />
    <link rel="prev" title="Internationalization and localization" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body role="document">

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Internationalization and localization">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="formatting.html" title="Format localization">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-i18n-translation">
            
  <div class="section" id="s-module-django.utils.translation">
<span id="s-translation"></span><span id="module-django.utils.translation"></span><span id="translation"></span><h1>Translation<a class="headerlink" href="#module-django.utils.translation" title="Permalink to this headline">¶</a></h1>
<div class="section" id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>In order to make a Django project translatable, you have to add a minimal
number of hooks to your Python code and templates. These hooks are called
<a class="reference internal" href="index.html#term-translation-string"><span class="xref std std-term">translation strings</span></a>. They tell Django: &#8220;This text
should be translated into the end user&#8217;s language, if a translation for this
text is available in that language.&#8221; It&#8217;s your responsibility to mark
translatable strings; the system can only translate strings it knows about.</p>
<p>Django then provides utilities to extract the translation strings into a
<a class="reference internal" href="index.html#term-message-file"><span class="xref std std-term">message file</span></a>. This file is a convenient way for translators to provide
the equivalent of the translation strings in the target language. Once the
translators have filled in the message file, it must be compiled. This process
relies on the GNU gettext toolset.</p>
<p>Once this is done, Django takes care of translating Web apps on the fly in each
available language, according to users&#8217; language preferences.</p>
<p>Django&#8217;s internationalization hooks are on by default, and that means there&#8217;s a
bit of i18n-related overhead in certain places of the framework. If you don&#8217;t
use internationalization, you should take the two seconds to set
<a class="reference internal" href="../../ref/settings.html#std:setting-USE_I18N"><code class="xref std std-setting docutils literal"><span class="pre">USE_I18N</span> <span class="pre">=</span> <span class="pre">False</span></code></a> in your settings file. Then Django will
make some optimizations so as not to load the internationalization machinery.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">There is also an independent but related <a class="reference internal" href="../../ref/settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal"><span class="pre">USE_L10N</span></code></a> setting that
controls if Django should implement format localization. See
<a class="reference internal" href="formatting.html"><span class="doc">Format localization</span></a> for more details.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Make sure you&#8217;ve activated translation for your project (the fastest way is
to check if <a class="reference internal" href="../../ref/settings.html#std:setting-MIDDLEWARE_CLASSES"><code class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></code></a> includes
<a class="reference internal" href="../../ref/middleware.html#django.middleware.locale.LocaleMiddleware" title="django.middleware.locale.LocaleMiddleware"><code class="xref py py-mod docutils literal"><span class="pre">django.middleware.locale.LocaleMiddleware</span></code></a>). If you haven&#8217;t yet,
see <a class="reference internal" href="#how-django-discovers-language-preference"><span class="std std-ref">How Django discovers language preference</span></a>.</p>
</div>
</div>
<div class="section" id="s-internationalization-in-python-code">
<span id="internationalization-in-python-code"></span><h2>Internationalization: in Python code<a class="headerlink" href="#internationalization-in-python-code" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-standard-translation">
<span id="standard-translation"></span><h3>Standard translation<a class="headerlink" href="#standard-translation" title="Permalink to this headline">¶</a></h3>
<p>Specify a translation string by using the function
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext" title="django.utils.translation.ugettext"><code class="xref py py-func docutils literal"><span class="pre">ugettext()</span></code></a>. It&#8217;s convention to import this
as a shorter alias, <code class="docutils literal"><span class="pre">_</span></code>, to save typing.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Python&#8217;s standard library <code class="docutils literal"><span class="pre">gettext</span></code> module installs <code class="docutils literal"><span class="pre">_()</span></code> into the
global namespace, as an alias for <code class="docutils literal"><span class="pre">gettext()</span></code>. In Django, we have chosen
not to follow this practice, for a couple of reasons:</p>
<ol class="last arabic simple">
<li>For international character set (Unicode) support,
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext" title="django.utils.translation.ugettext"><code class="xref py py-func docutils literal"><span class="pre">ugettext()</span></code></a> is more useful than
<code class="docutils literal"><span class="pre">gettext()</span></code>. Sometimes, you should be using
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext_lazy" title="django.utils.translation.ugettext_lazy"><code class="xref py py-func docutils literal"><span class="pre">ugettext_lazy()</span></code></a> as the default
translation method for a particular file. Without <code class="docutils literal"><span class="pre">_()</span></code> in the
global namespace, the developer has to think about which is the
most appropriate translation function.</li>
<li>The underscore character (<code class="docutils literal"><span class="pre">_</span></code>) is used to represent &#8220;the previous
result&#8221; in Python&#8217;s interactive shell and doctest tests. Installing a
global <code class="docutils literal"><span class="pre">_()</span></code> function causes interference. Explicitly importing
<code class="docutils literal"><span class="pre">ugettext()</span></code> as <code class="docutils literal"><span class="pre">_()</span></code> avoids this problem.</li>
</ol>
</div>
<p>In this example, the text <code class="docutils literal"><span class="pre">&quot;Welcome</span> <span class="pre">to</span> <span class="pre">my</span> <span class="pre">site.&quot;</span></code> is marked as a translation
string:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Welcome to my site.&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
<p>Obviously, you could code this without using the alias. This example is
identical to the previous one:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">ugettext</span><span class="p">(</span><span class="s2">&quot;Welcome to my site.&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
<p>Translation works on computed values. This example is identical to the previous
two:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Welcome&#39;</span><span class="p">,</span> <span class="s1">&#39;to&#39;</span><span class="p">,</span> <span class="s1">&#39;my&#39;</span><span class="p">,</span> <span class="s1">&#39;site.&#39;</span><span class="p">]</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
<p>Translation works on variables. Again, here&#8217;s an identical example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">sentence</span> <span class="o">=</span> <span class="s1">&#39;Welcome to my site.&#39;</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="n">sentence</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
<p>(The caveat with using variables or computed values, as in the previous two
examples, is that Django&#8217;s translation-string-detecting utility,
<a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a>, won&#8217;t be able to find
these strings. More on <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a> later.)</p>
<p>The strings you pass to <code class="docutils literal"><span class="pre">_()</span></code> or <code class="docutils literal"><span class="pre">ugettext()</span></code> can take placeholders,
specified with Python&#8217;s standard named-string interpolation syntax. Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Today is </span><span class="si">%(month)s</span><span class="s1"> </span><span class="si">%(day)s</span><span class="s1">.&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;month&#39;</span><span class="p">:</span> <span class="n">m</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">:</span> <span class="n">d</span><span class="p">}</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
<p>This technique lets language-specific translations reorder the placeholder
text. For example, an English translation may be <code class="docutils literal"><span class="pre">&quot;Today</span> <span class="pre">is</span> <span class="pre">November</span> <span class="pre">26.&quot;</span></code>,
while a Spanish translation may be <code class="docutils literal"><span class="pre">&quot;Hoy</span> <span class="pre">es</span> <span class="pre">26</span> <span class="pre">de</span> <span class="pre">Noviembre.&quot;</span></code> &#8211; with the
month and the day placeholders swapped.</p>
<p>For this reason, you should use named-string interpolation (e.g., <code class="docutils literal"><span class="pre">%(day)s</span></code>)
instead of positional interpolation (e.g., <code class="docutils literal"><span class="pre">%s</span></code> or <code class="docutils literal"><span class="pre">%d</span></code>) whenever you
have more than a single parameter. If you used positional interpolation,
translations wouldn&#8217;t be able to reorder placeholder text.</p>
</div>
<div class="section" id="s-comments-for-translators">
<span id="s-translator-comments"></span><span id="comments-for-translators"></span><span id="translator-comments"></span><h3>Comments for translators<a class="headerlink" href="#comments-for-translators" title="Permalink to this headline">¶</a></h3>
<p>If you would like to give translators hints about a translatable string, you
can add a comment prefixed with the <code class="docutils literal"><span class="pre">Translators</span></code> keyword on the line
preceding the string, e.g.:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># Translators: This message appears on the home page only</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">ugettext</span><span class="p">(</span><span class="s2">&quot;Welcome to my site.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The comment will then appear in the resulting <code class="docutils literal"><span class="pre">.po</span></code> file associated with the
translatable construct located below it and should also be displayed by most
translation tools.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Just for completeness, this is the corresponding fragment of the
resulting <code class="docutils literal"><span class="pre">.po</span></code> file:</p>
<div class="last highlight-po"><div class="highlight"><pre><span></span><span class="c1">#. Translators: This message appears on the home page only</span>
<span class="c1"># path/to/python/file.py:123</span>
<span class="nv">msgid</span> <span class="s">&quot;Welcome to my site.&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>
</pre></div>
</div>
</div>
<p>This also works in templates. See <a class="reference internal" href="#translator-comments-in-templates"><span class="std std-ref">Comments for translators in templates</span></a> for
more details.</p>
</div>
<div class="section" id="s-marking-strings-as-no-op">
<span id="marking-strings-as-no-op"></span><h3>Marking strings as no-op<a class="headerlink" href="#marking-strings-as-no-op" title="Permalink to this headline">¶</a></h3>
<p>Use the function <a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext_noop" title="django.utils.translation.ugettext_noop"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.ugettext_noop()</span></code></a> to mark a
string as a translation string without translating it. The string is later
translated from a variable.</p>
<p>Use this if you have constant strings that should be stored in the source
language because they are exchanged over systems or users &#8211; such as strings
in a database &#8211; but should be translated at the last possible point in time,
such as when the string is presented to the user.</p>
</div>
<div class="section" id="s-pluralization">
<span id="pluralization"></span><h3>Pluralization<a class="headerlink" href="#pluralization" title="Permalink to this headline">¶</a></h3>
<p>Use the function <a class="reference internal" href="../../ref/utils.html#django.utils.translation.ungettext" title="django.utils.translation.ungettext"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.ungettext()</span></code></a> to specify
pluralized messages.</p>
<p><code class="docutils literal"><span class="pre">ungettext</span></code> takes three arguments: the singular translation string, the plural
translation string and the number of objects.</p>
<p>This function is useful when you need your Django application to be localizable
to languages where the number and complexity of <a class="reference external" href="http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms">plural forms</a> is
greater than the two forms used in English (&#8216;object&#8217; for the singular and
&#8216;objects&#8217; for all the cases where <code class="docutils literal"><span class="pre">count</span></code> is different from one, irrespective
of its value.)</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ungettext</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">count</span><span class="p">):</span>
    <span class="n">page</span> <span class="o">=</span> <span class="n">ungettext</span><span class="p">(</span>
        <span class="s1">&#39;there is </span><span class="si">%(count)d</span><span class="s1"> object&#39;</span><span class="p">,</span>
        <span class="s1">&#39;there are </span><span class="si">%(count)d</span><span class="s1"> objects&#39;</span><span class="p">,</span>
    <span class="n">count</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
        <span class="s1">&#39;count&#39;</span><span class="p">:</span> <span class="n">count</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">page</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example the number of objects is passed to the translation
languages as the <code class="docutils literal"><span class="pre">count</span></code> variable.</p>
<p>Note that pluralization is complicated and works differently in each language.
Comparing <code class="docutils literal"><span class="pre">count</span></code> to 1 isn&#8217;t always the correct rule. This code looks
sophisticated, but will produce incorrect results for some languages:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ungettext</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="k">import</span> <span class="n">Report</span>

<span class="n">count</span> <span class="o">=</span> <span class="n">Report</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
<span class="k">if</span> <span class="n">count</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Report</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Report</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span>

<span class="n">text</span> <span class="o">=</span> <span class="n">ungettext</span><span class="p">(</span>
    <span class="s1">&#39;There is </span><span class="si">%(count)d</span><span class="s1"> </span><span class="si">%(name)s</span><span class="s1"> available.&#39;</span><span class="p">,</span>
    <span class="s1">&#39;There are </span><span class="si">%(count)d</span><span class="s1"> </span><span class="si">%(name)s</span><span class="s1"> available.&#39;</span><span class="p">,</span>
    <span class="n">count</span>
<span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
    <span class="s1">&#39;count&#39;</span><span class="p">:</span> <span class="n">count</span><span class="p">,</span>
    <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">name</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Don&#8217;t try to implement your own singular-or-plural logic, it won&#8217;t be correct.
In a case like this, consider something like the following:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">text</span> <span class="o">=</span> <span class="n">ungettext</span><span class="p">(</span>
    <span class="s1">&#39;There is </span><span class="si">%(count)d</span><span class="s1"> </span><span class="si">%(name)s</span><span class="s1"> object available.&#39;</span><span class="p">,</span>
    <span class="s1">&#39;There are </span><span class="si">%(count)d</span><span class="s1"> </span><span class="si">%(name)s</span><span class="s1"> objects available.&#39;</span><span class="p">,</span>
    <span class="n">count</span>
<span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
    <span class="s1">&#39;count&#39;</span><span class="p">:</span> <span class="n">count</span><span class="p">,</span>
    <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">Report</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition note" id="pluralization-var-notes">
<p class="first admonition-title">Note</p>
<p>When using <code class="docutils literal"><span class="pre">ungettext()</span></code>, make sure you use a single name for every
extrapolated variable included in the literal. In the examples above, note
how we used the <code class="docutils literal"><span class="pre">name</span></code> Python variable in both translation strings. This
example, besides being incorrect in some languages as noted above, would
fail:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">text</span> <span class="o">=</span> <span class="n">ungettext</span><span class="p">(</span>
    <span class="s1">&#39;There is </span><span class="si">%(count)d</span><span class="s1"> </span><span class="si">%(name)s</span><span class="s1"> available.&#39;</span><span class="p">,</span>
    <span class="s1">&#39;There are </span><span class="si">%(count)d</span><span class="s1"> </span><span class="si">%(plural_name)s</span><span class="s1"> available.&#39;</span><span class="p">,</span>
    <span class="n">count</span>
<span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
    <span class="s1">&#39;count&#39;</span><span class="p">:</span> <span class="n">Report</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">count</span><span class="p">(),</span>
    <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">Report</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
    <span class="s1">&#39;plural_name&#39;</span><span class="p">:</span> <span class="n">Report</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You would get an error when running <a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span>
<span class="pre">compilemessages</span></code></a>:</p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="nb">format</span> <span class="n">specification</span> <span class="k">for</span> <span class="n">argument</span> <span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="k">as</span> <span class="ow">in</span> <span class="s1">&#39;msgstr[0]&#39;</span><span class="p">,</span> <span class="n">doesn</span><span class="s1">&#39;t exist in &#39;</span><span class="n">msgid</span><span class="s1">&#39;</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Plural form and po files</p>
<p class="last">Django does not support custom plural equations in po files. As all
translation catalogs are merged, only the plural form for the main Django po
file (in <code class="docutils literal"><span class="pre">django/conf/locale/&lt;lang_code&gt;/LC_MESSAGES/django.po</span></code>) is
considered. Plural forms in all other po files are ignored. Therefore, you
should not use different plural equations in your project or application po
files.</p>
</div>
</div>
<div class="section" id="s-contextual-markers">
<span id="s-id1"></span><span id="contextual-markers"></span><span id="id1"></span><h3>Contextual markers<a class="headerlink" href="#contextual-markers" title="Permalink to this headline">¶</a></h3>
<p>Sometimes words have several meanings, such as <code class="docutils literal"><span class="pre">&quot;May&quot;</span></code> in English, which
refers to a month name and to a verb. To enable translators to translate
these words correctly in different contexts, you can use the
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.pgettext" title="django.utils.translation.pgettext"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.pgettext()</span></code></a> function, or the
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.npgettext" title="django.utils.translation.npgettext"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.npgettext()</span></code></a> function if the string needs
pluralization. Both take a context string as the first variable.</p>
<p>In the resulting <code class="docutils literal"><span class="pre">.po</span></code> file, the string will then appear as often as there are
different contextual markers for the same string (the context will appear on the
<code class="docutils literal"><span class="pre">msgctxt</span></code> line), allowing the translator to give a different translation for
each of them.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">pgettext</span>

<span class="n">month</span> <span class="o">=</span> <span class="n">pgettext</span><span class="p">(</span><span class="s2">&quot;month name&quot;</span><span class="p">,</span> <span class="s2">&quot;May&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">pgettext_lazy</span>

<span class="k">class</span> <span class="nc">MyThing</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">help_text</span><span class="o">=</span><span class="n">pgettext_lazy</span><span class="p">(</span>
        <span class="s1">&#39;help text for MyThing model&#39;</span><span class="p">,</span> <span class="s1">&#39;This is the help text&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>will appear in the <code class="docutils literal"><span class="pre">.po</span></code> file as:</p>
<div class="highlight-po"><div class="highlight"><pre><span></span><span class="nv">msgctxt</span> <span class="s">&quot;month name&quot;</span>
<span class="nv">msgid</span> <span class="s">&quot;May&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>
</pre></div>
</div>
<p>Contextual markers are also supported by the <a class="reference internal" href="#std:templatetag-trans"><code class="xref std std-ttag docutils literal"><span class="pre">trans</span></code></a> and
<a class="reference internal" href="#std:templatetag-blocktrans"><code class="xref std std-ttag docutils literal"><span class="pre">blocktrans</span></code></a> template tags.</p>
</div>
<div class="section" id="s-lazy-translation">
<span id="s-lazy-translations"></span><span id="lazy-translation"></span><span id="lazy-translations"></span><h3>Lazy translation<a class="headerlink" href="#lazy-translation" title="Permalink to this headline">¶</a></h3>
<p>Use the lazy versions of translation functions in
<a class="reference internal" href="#module-django.utils.translation" title="django.utils.translation"><code class="xref py py-mod docutils literal"><span class="pre">django.utils.translation</span></code></a> (easily recognizable by the <code class="docutils literal"><span class="pre">lazy</span></code> suffix in
their names) to translate strings lazily &#8211; when the value is accessed rather
than when they&#8217;re called.</p>
<p>These functions store a lazy reference to the string &#8211; not the actual
translation. The translation itself will be done when the string is used in a
string context, such as in template rendering.</p>
<p>This is essential when calls to these functions are located in code paths that
are executed at module load time.</p>
<p>This is something that can easily happen when defining models, forms and
model forms, because Django implements these such that their fields are
actually class-level attributes. For that reason, make sure to use lazy
translations in the following cases:</p>
<div class="section" id="s-model-fields-and-relationships-verbose-name-and-help-text-option-values">
<span id="model-fields-and-relationships-verbose-name-and-help-text-option-values"></span><h4>Model fields and relationships <code class="docutils literal"><span class="pre">verbose_name</span></code> and <code class="docutils literal"><span class="pre">help_text</span></code> option values<a class="headerlink" href="#model-fields-and-relationships-verbose-name-and-help-text-option-values" title="Permalink to this headline">¶</a></h4>
<p>For example, to translate the help text of the <em>name</em> field in the following
model, do the following:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">class</span> <span class="nc">MyThing</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;This is the help text&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>You can mark names of <a class="reference internal" href="../../ref/models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a>,
<a class="reference internal" href="../../ref/models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> or
<a class="reference internal" href="../../ref/models/fields.html#django.db.models.OneToOneField" title="django.db.models.OneToOneField"><code class="xref py py-class docutils literal"><span class="pre">OneToOneField</span></code></a> relationship as translatable by using
their <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.verbose_name" title="django.db.models.Options.verbose_name"><code class="xref py py-attr docutils literal"><span class="pre">verbose_name</span></code></a> options:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyThing</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">kind</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">ThingKind</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;kinds&#39;</span><span class="p">,</span>
                             <span class="n">verbose_name</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;kind&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>Just like you would do in <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.verbose_name" title="django.db.models.Options.verbose_name"><code class="xref py py-attr docutils literal"><span class="pre">verbose_name</span></code></a> you
should provide a lowercase verbose name text for the relation as Django will
automatically titlecase it when required.</p>
</div>
<div class="section" id="s-model-verbose-names-values">
<span id="model-verbose-names-values"></span><h4>Model verbose names values<a class="headerlink" href="#model-verbose-names-values" title="Permalink to this headline">¶</a></h4>
<p>It is recommended to always provide explicit
<a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.verbose_name" title="django.db.models.Options.verbose_name"><code class="xref py py-attr docutils literal"><span class="pre">verbose_name</span></code></a> and
<a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.verbose_name_plural" title="django.db.models.Options.verbose_name_plural"><code class="xref py py-attr docutils literal"><span class="pre">verbose_name_plural</span></code></a> options rather than
relying on the fallback English-centric and somewhat naïve determination of
verbose names Django performs by looking at the model&#8217;s class name:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">class</span> <span class="nc">MyThing</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">),</span> <span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;This is the help text&#39;</span><span class="p">))</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">verbose_name</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;my thing&#39;</span><span class="p">)</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;my things&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-model-methods-short-description-attribute-values">
<span id="model-methods-short-description-attribute-values"></span><h4>Model methods <code class="docutils literal"><span class="pre">short_description</span></code> attribute values<a class="headerlink" href="#model-methods-short-description-attribute-values" title="Permalink to this headline">¶</a></h4>
<p>For model methods, you can provide translations to Django and the admin site
with the <code class="docutils literal"><span class="pre">short_description</span></code> attribute:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">class</span> <span class="nc">MyThing</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">kind</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">ThingKind</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;kinds&#39;</span><span class="p">,</span>
                             <span class="n">verbose_name</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;kind&#39;</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">is_mouse</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">kind</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">MOUSE_TYPE</span>
    <span class="n">is_mouse</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Is it a mouse?&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-working-with-lazy-translation-objects">
<span id="working-with-lazy-translation-objects"></span><h3>Working with lazy translation objects<a class="headerlink" href="#working-with-lazy-translation-objects" title="Permalink to this headline">¶</a></h3>
<p>The result of a <code class="docutils literal"><span class="pre">ugettext_lazy()</span></code> call can be used wherever you would use a
unicode string (an object with type <code class="docutils literal"><span class="pre">unicode</span></code>) in Python. If you try to use
it where a bytestring (a <code class="docutils literal"><span class="pre">str</span></code> object) is expected, things will not work as
expected, since a <code class="docutils literal"><span class="pre">ugettext_lazy()</span></code> object doesn&#8217;t know how to convert
itself to a bytestring. You can&#8217;t use a unicode string inside a bytestring,
either, so this is consistent with normal Python behavior. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># This is fine: putting a unicode proxy into a unicode string.</span>
<span class="s2">&quot;Hello </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s2">&quot;people&quot;</span><span class="p">)</span>

<span class="c1"># This will not work, since you cannot insert a unicode object</span>
<span class="c1"># into a bytestring (nor can you insert our unicode proxy there)</span>
<span class="n">b</span><span class="s2">&quot;Hello </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s2">&quot;people&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you ever see output that looks like <code class="docutils literal"><span class="pre">&quot;hello</span>
<span class="pre">&lt;django.utils.functional...&gt;&quot;</span></code>, you have tried to insert the result of
<code class="docutils literal"><span class="pre">ugettext_lazy()</span></code> into a bytestring. That&#8217;s a bug in your code.</p>
<p>If you don&#8217;t like the long <code class="docutils literal"><span class="pre">ugettext_lazy</span></code> name, you can just alias it as
<code class="docutils literal"><span class="pre">_</span></code> (underscore), like so:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">class</span> <span class="nc">MyThing</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;This is the help text&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>Using <code class="docutils literal"><span class="pre">ugettext_lazy()</span></code> and <code class="docutils literal"><span class="pre">ungettext_lazy()</span></code> to mark strings in models
and utility functions is a common operation. When you&#8217;re working with these
objects elsewhere in your code, you should ensure that you don&#8217;t accidentally
convert them to strings, because they should be converted as late as possible
(so that the correct locale is in effect). This necessitates the use of the
helper function described next.</p>
<div class="section" id="s-lazy-translations-and-plural">
<span id="s-lazy-plural-translations"></span><span id="lazy-translations-and-plural"></span><span id="lazy-plural-translations"></span><h4>Lazy translations and plural<a class="headerlink" href="#lazy-translations-and-plural" title="Permalink to this headline">¶</a></h4>
<p>When using lazy translation for a plural string (<code class="docutils literal"><span class="pre">[u]n[p]gettext_lazy</span></code>), you
generally don&#8217;t know the <code class="docutils literal"><span class="pre">number</span></code> argument at the time of the string
definition. Therefore, you are authorized to pass a key name instead of an
integer as the <code class="docutils literal"><span class="pre">number</span></code> argument. Then <code class="docutils literal"><span class="pre">number</span></code> will be looked up in the
dictionary under that key during string interpolation. Here&#8217;s example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ungettext_lazy</span>

<span class="k">class</span> <span class="nc">MyForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">error_message</span> <span class="o">=</span> <span class="n">ungettext_lazy</span><span class="p">(</span><span class="s2">&quot;You only provided </span><span class="si">%(num)d</span><span class="s2"> argument&quot;</span><span class="p">,</span>
        <span class="s2">&quot;You only provided </span><span class="si">%(num)d</span><span class="s2"> arguments&quot;</span><span class="p">,</span> <span class="s1">&#39;num&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># ...</span>
        <span class="k">if</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_message</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;num&#39;</span><span class="p">:</span> <span class="n">number</span><span class="p">})</span>
</pre></div>
</div>
<p>If the string contains exactly one unnamed placeholder, you can interpolate
directly with the <code class="docutils literal"><span class="pre">number</span></code> argument:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">error_message</span> <span class="o">=</span> <span class="n">ungettext_lazy</span><span class="p">(</span><span class="s2">&quot;You provided </span><span class="si">%d</span><span class="s2"> argument&quot;</span><span class="p">,</span>
        <span class="s2">&quot;You provided </span><span class="si">%d</span><span class="s2"> arguments&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># ...</span>
        <span class="k">if</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_message</span> <span class="o">%</span> <span class="n">number</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-joining-strings-string-concat">
<span id="joining-strings-string-concat"></span><h4>Joining strings: string_concat()<a class="headerlink" href="#joining-strings-string-concat" title="Permalink to this headline">¶</a></h4>
<p>Standard Python string joins (<code class="docutils literal"><span class="pre">''.join([...])</span></code>) will not work on lists
containing lazy translation objects. Instead, you can use
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.string_concat" title="django.utils.translation.string_concat"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.string_concat()</span></code></a>, which creates a lazy object
that concatenates its contents <em>and</em> converts them to strings only when the
result is included in a string. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">string_concat</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span>
<span class="o">...</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;John Lennon&#39;</span><span class="p">)</span>
<span class="n">instrument</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;guitar&#39;</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">string_concat</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;: &#39;</span><span class="p">,</span> <span class="n">instrument</span><span class="p">)</span>
</pre></div>
</div>
<p>In this case, the lazy translations in <code class="docutils literal"><span class="pre">result</span></code> will only be converted to
strings when <code class="docutils literal"><span class="pre">result</span></code> itself is used in a string (usually at template
rendering time).</p>
</div>
<div class="section" id="s-other-uses-of-lazy-in-delayed-translations">
<span id="other-uses-of-lazy-in-delayed-translations"></span><h4>Other uses of lazy in delayed translations<a class="headerlink" href="#other-uses-of-lazy-in-delayed-translations" title="Permalink to this headline">¶</a></h4>
<p>For any other case where you would like to delay the translation, but have to
pass the translatable string as argument to another function, you can wrap
this function inside a lazy call yourself. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>  <span class="c1"># Python 3 compatibility</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="k">import</span> <span class="n">mark_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="n">mark_safe_lazy</span> <span class="o">=</span> <span class="n">lazy</span><span class="p">(</span><span class="n">mark_safe</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>
</pre></div>
</div>
<p>And then later:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">lazy_string</span> <span class="o">=</span> <span class="n">mark_safe_lazy</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;&lt;p&gt;My &lt;strong&gt;string!&lt;/strong&gt;&lt;/p&gt;&quot;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-localized-names-of-languages">
<span id="localized-names-of-languages"></span><h3>Localized names of languages<a class="headerlink" href="#localized-names-of-languages" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.utils.translation.get_language_info">
<code class="descname">get_language_info</code>()<a class="reference internal" href="../../_modules/django/utils/translation.html#get_language_info"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.utils.translation.get_language_info" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <code class="docutils literal"><span class="pre">get_language_info()</span></code> function provides detailed information about
languages:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">get_language_info</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">li</span> <span class="o">=</span> <span class="n">get_language_info</span><span class="p">(</span><span class="s1">&#39;de&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">li</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span> <span class="n">li</span><span class="p">[</span><span class="s1">&#39;name_local&#39;</span><span class="p">],</span> <span class="n">li</span><span class="p">[</span><span class="s1">&#39;bidi&#39;</span><span class="p">])</span>
<span class="go">German Deutsch False</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">name</span></code> and <code class="docutils literal"><span class="pre">name_local</span></code> attributes of the dictionary contain the name of
the language in English and in the language itself, respectively.  The <code class="docutils literal"><span class="pre">bidi</span></code>
attribute is True only for bi-directional languages.</p>
<p>The source of the language information is the <code class="docutils literal"><span class="pre">django.conf.locale</span></code> module.
Similar access to this information is available for template code. See below.</p>
</div>
</div>
<div class="section" id="s-internationalization-in-template-code">
<span id="s-specifying-translation-strings-in-template-code"></span><span id="internationalization-in-template-code"></span><span id="specifying-translation-strings-in-template-code"></span><h2>Internationalization: in template code<a class="headerlink" href="#internationalization-in-template-code" title="Permalink to this headline">¶</a></h2>
<p>Translations in <a class="reference internal" href="../../ref/templates/language.html"><span class="doc">Django templates</span></a> uses two template
tags and a slightly different syntax than in Python code. To give your template
access to these tags, put <code class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">i18n</span> <span class="pre">%}</span></code> toward the top of your template.
As with all template tags, this tag needs to be loaded in all templates which
use translations, even those templates that extend from other templates which
have already loaded the <code class="docutils literal"><span class="pre">i18n</span></code> tag.</p>
<div class="section" id="s-trans-template-tag">
<span id="s-std:templatetag-trans"></span><span id="trans-template-tag"></span><span id="std:templatetag-trans"></span><h3><code class="docutils literal"><span class="pre">trans</span></code> template tag<a class="headerlink" href="#trans-template-tag" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal"><span class="pre">{%</span> <span class="pre">trans</span> <span class="pre">%}</span></code> template tag translates either a constant string
(enclosed in single or double quotes) or variable content:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;This is the title.&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="nv">myvar</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>If the <code class="docutils literal"><span class="pre">noop</span></code> option is present, variable lookup still takes place but the
translation is skipped. This is useful when &#8220;stubbing out&#8221; content that will
require translation in the future:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;myvar&quot;</span> <span class="nv">noop</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Internally, inline translations use an
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext" title="django.utils.translation.ugettext"><code class="xref py py-func docutils literal"><span class="pre">ugettext()</span></code></a> call.</p>
<p>In case a template var (<code class="docutils literal"><span class="pre">myvar</span></code> above) is passed to the tag, the tag will
first resolve such variable to a string at run-time and then look up that
string in the message catalogs.</p>
<p>It&#8217;s not possible to mix a template variable inside a string within <code class="docutils literal"><span class="pre">{%</span> <span class="pre">trans</span>
<span class="pre">%}</span></code>. If your translations require strings with variables (placeholders), use
<a class="reference internal" href="#std:templatetag-blocktrans"><code class="xref std std-ttag docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">%}</span></code></a> instead.</p>
<p>If you&#8217;d like to retrieve a translated string without displaying it, you can
use the following syntax:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;This is the title&quot;</span> <span class="k">as</span> <span class="nv">the_title</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">the_title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">meta</span> <span class="na">name</span><span class="o">=</span><span class="s">&quot;description&quot;</span> <span class="na">content</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">the_title</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>In practice you&#8217;ll use this to get strings that are used in multiple places
or should be used as arguments for other template tags or filters:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;starting point&quot;</span> <span class="k">as</span> <span class="nv">start</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;end point&quot;</span> <span class="k">as</span> <span class="nv">end</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;La Grande Boucle&quot;</span> <span class="k">as</span> <span class="nv">race</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>
  <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/&quot;</span> <span class="na">title</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="cp">%}</span><span class="s">Back to &#39;</span><span class="cp">{{</span> <span class="nv">race</span> <span class="cp">}}</span><span class="s">&#39; homepage</span><span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">race</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">stage</span> <span class="k">in</span> <span class="nv">tour_stages</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">start</span> <span class="nv">end</span> <span class="cp">%}</span>: <span class="cp">{{</span> <span class="nv">stage</span> <span class="cp">}}{%</span> <span class="k">if</span> <span class="nb">forloop</span><span class="nv">.counter</span><span class="o">|</span><span class="nf">divisibleby</span><span class="o">:</span><span class="m">2</span> <span class="cp">%}</span><span class="p">&lt;</span><span class="nt">br</span> <span class="p">/&gt;</span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>, <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">{%</span> <span class="pre">trans</span> <span class="pre">%}</span></code> also supports <a class="reference internal" href="#contextual-markers"><span class="std std-ref">contextual markers</span></a>
using the <code class="docutils literal"><span class="pre">context</span></code> keyword:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;May&quot;</span> <span class="nv">context</span> <span class="s2">&quot;month name&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-blocktrans-template-tag">
<span id="s-std:templatetag-blocktrans"></span><span id="blocktrans-template-tag"></span><span id="std:templatetag-blocktrans"></span><h3><code class="docutils literal"><span class="pre">blocktrans</span></code> template tag<a class="headerlink" href="#blocktrans-template-tag" title="Permalink to this headline">¶</a></h3>
<p>Contrarily to the <a class="reference internal" href="#std:templatetag-trans"><code class="xref std std-ttag docutils literal"><span class="pre">trans</span></code></a> tag, the <code class="docutils literal"><span class="pre">blocktrans</span></code> tag allows you to mark
complex sentences consisting of literals and variable content for translation
by making use of placeholders:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="cp">%}</span>This string will have <span class="cp">{{</span> <span class="nv">value</span> <span class="cp">}}</span> inside.<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>To translate a template expression &#8211; say, accessing object attributes or
using template filters &#8211; you need to bind the expression to a local variable
for use within the translation block. Examples:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="k">with</span> <span class="nv">amount</span><span class="o">=</span><span class="nv">article.price</span> <span class="cp">%}</span>
That will cost $ <span class="cp">{{</span> <span class="nv">amount</span> <span class="cp">}}</span>.
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">blocktrans</span> <span class="k">with</span> <span class="nv">myvar</span><span class="o">=</span><span class="nv">value</span><span class="o">|</span><span class="nf">filter</span> <span class="cp">%}</span>
This will have <span class="cp">{{</span> <span class="nv">myvar</span> <span class="cp">}}</span> inside.
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>You can use multiple expressions inside a single <code class="docutils literal"><span class="pre">blocktrans</span></code> tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="k">with</span> <span class="nv">book_t</span><span class="o">=</span><span class="nv">book</span><span class="o">|</span><span class="nf">title</span> <span class="nv">author_t</span><span class="o">=</span><span class="nv">author</span><span class="o">|</span><span class="nf">title</span> <span class="cp">%}</span>
This is <span class="cp">{{</span> <span class="nv">book_t</span> <span class="cp">}}</span> by <span class="cp">{{</span> <span class="nv">author_t</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The previous more verbose format is still supported:
<code class="docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">with</span> <span class="pre">book|title</span> <span class="pre">as</span> <span class="pre">book_t</span> <span class="pre">and</span> <span class="pre">author|title</span> <span class="pre">as</span> <span class="pre">author_t</span> <span class="pre">%}</span></code></p>
</div>
<p>Other block tags (for example <code class="docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">%}</span></code> or <code class="docutils literal"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></code>) are not allowed
inside a <code class="docutils literal"><span class="pre">blocktrans</span></code> tag.</p>
<p>If resolving one of the block arguments fails, blocktrans will fall back to
the default language by deactivating the currently active language
temporarily with the <a class="reference internal" href="../../ref/utils.html#django.utils.translation.deactivate_all" title="django.utils.translation.deactivate_all"><code class="xref py py-func docutils literal"><span class="pre">deactivate_all()</span></code></a>
function.</p>
<p>This tag also provides for pluralization. To use it:</p>
<ul class="simple">
<li>Designate and bind a counter value with the name <code class="docutils literal"><span class="pre">count</span></code>. This value will
be the one used to select the right plural form.</li>
<li>Specify both the singular and plural forms separating them with the
<code class="docutils literal"><span class="pre">{%</span> <span class="pre">plural</span> <span class="pre">%}</span></code> tag within the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">%}</span></code> and
<code class="docutils literal"><span class="pre">{%</span> <span class="pre">endblocktrans</span> <span class="pre">%}</span></code> tags.</li>
</ul>
<p>An example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="nv">count</span> <span class="nv">counter</span><span class="o">=</span><span class="nv">list</span><span class="o">|</span><span class="nf">length</span> <span class="cp">%}</span>
There is only one <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span> object.
<span class="cp">{%</span> <span class="k">plural</span> <span class="cp">%}</span>
There are <span class="cp">{{</span> <span class="nv">counter</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span> objects.
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>A more complex example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="k">with</span> <span class="nv">amount</span><span class="o">=</span><span class="nv">article.price</span> <span class="nv">count</span> <span class="nv">years</span><span class="o">=</span><span class="nv">i.length</span> <span class="cp">%}</span>
That will cost $ <span class="cp">{{</span> <span class="nv">amount</span> <span class="cp">}}</span> per year.
<span class="cp">{%</span> <span class="k">plural</span> <span class="cp">%}</span>
That will cost $ <span class="cp">{{</span> <span class="nv">amount</span> <span class="cp">}}</span> per <span class="cp">{{</span> <span class="nv">years</span> <span class="cp">}}</span> years.
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>When you use both the pluralization feature and bind values to local variables
in addition to the counter value, keep in mind that the <code class="docutils literal"><span class="pre">blocktrans</span></code>
construct is internally converted to an <code class="docutils literal"><span class="pre">ungettext</span></code> call. This means the
same <a class="reference internal" href="#pluralization-var-notes"><span class="std std-ref">notes regarding ungettext variables</span></a>
apply.</p>
<p>Reverse URL lookups cannot be carried out within the <code class="docutils literal"><span class="pre">blocktrans</span></code> and should
be retrieved (and stored) beforehand:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;path.to.view&#39;</span> <span class="nv">arg</span> <span class="nv">arg2</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">blocktrans</span> <span class="cp">%}</span>
This is a URL: <span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">%}</span></code> also supports <a class="reference internal" href="#contextual-markers"><span class="std std-ref">contextual
markers</span></a> using the <code class="docutils literal"><span class="pre">context</span></code> keyword:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="k">with</span> <span class="nv">name</span><span class="o">=</span><span class="nv">user.username</span> <span class="nv">context</span> <span class="s2">&quot;greeting&quot;</span> <span class="cp">%}</span>Hi <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Another feature <code class="docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">%}</span></code> supports is the <code class="docutils literal"><span class="pre">trimmed</span></code> option. This
option will remove newline characters from the beginning and the end of the
content of the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">%}</span></code> tag, replace any whitespace at the beginning
and end of a line and merge all lines into one using a space character to
separate them. This is quite useful for indenting the content of a <code class="docutils literal"><span class="pre">{%</span>
<span class="pre">blocktrans</span> <span class="pre">%}</span></code> tag without having the indentation characters end up in the
corresponding entry in the PO file, which makes the translation process easier.</p>
<p>For instance, the following <code class="docutils literal"><span class="pre">{%</span> <span class="pre">blocktrans</span> <span class="pre">%}</span></code> tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="nv">trimmed</span> <span class="cp">%}</span>
  First sentence.
  Second paragraph.
<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>will result in the entry <code class="docutils literal"><span class="pre">&quot;First</span> <span class="pre">sentence.</span> <span class="pre">Second</span> <span class="pre">paragraph.&quot;</span></code> in the PO file,
compared to <code class="docutils literal"><span class="pre">&quot;\n</span>&#160; <span class="pre">First</span> <span class="pre">sentence.\n</span>&#160; <span class="pre">Second</span> <span class="pre">sentence.\n&quot;</span></code>, if the <code class="docutils literal"><span class="pre">trimmed</span></code>
option had not been specified.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>The <code class="docutils literal"><span class="pre">trimmed</span></code> option was added.</p>
</div>
</div>
<div class="section" id="s-string-literals-passed-to-tags-and-filters">
<span id="string-literals-passed-to-tags-and-filters"></span><h3>String literals passed to tags and filters<a class="headerlink" href="#string-literals-passed-to-tags-and-filters" title="Permalink to this headline">¶</a></h3>
<p>You can translate string literals passed as arguments to tags and filters
by using the familiar <code class="docutils literal"><span class="pre">_()</span></code> syntax:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">some_tag</span> <span class="kp">_</span><span class="o">(</span><span class="s2">&quot;Page not found&quot;</span><span class="o">)</span> <span class="nv">value</span><span class="o">|</span><span class="nf">yesno</span><span class="o">:</span><span class="kp">_</span><span class="o">(</span><span class="s2">&quot;yes,no&quot;</span><span class="o">)</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In this case, both the tag and the filter will see the translated string,
so they don&#8217;t need to be aware of translations.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In this example, the translation infrastructure will be passed the string
<code class="docutils literal"><span class="pre">&quot;yes,no&quot;</span></code>, not the individual strings <code class="docutils literal"><span class="pre">&quot;yes&quot;</span></code> and <code class="docutils literal"><span class="pre">&quot;no&quot;</span></code>. The
translated string will need to contain the comma so that the filter
parsing code knows how to split up the arguments. For example, a German
translator might translate the string <code class="docutils literal"><span class="pre">&quot;yes,no&quot;</span></code> as <code class="docutils literal"><span class="pre">&quot;ja,nein&quot;</span></code>
(keeping the comma intact).</p>
</div>
</div>
<div class="section" id="s-comments-for-translators-in-templates">
<span id="s-translator-comments-in-templates"></span><span id="comments-for-translators-in-templates"></span><span id="translator-comments-in-templates"></span><h3>Comments for translators in templates<a class="headerlink" href="#comments-for-translators-in-templates" title="Permalink to this headline">¶</a></h3>
<p>Just like with <a class="reference internal" href="#translator-comments"><span class="std std-ref">Python code</span></a>, these notes for
translators can be specified using comments, either with the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-comment"><code class="xref std std-ttag docutils literal"><span class="pre">comment</span></code></a>
tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">comment</span> <span class="cp">%}</span><span class="c">Translators: View verb</span><span class="cp">{%</span> <span class="k">endcomment</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;View&quot;</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">comment</span> <span class="cp">%}</span><span class="c">Translators: Short intro blurb</span><span class="cp">{%</span> <span class="k">endcomment</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">blocktrans</span> <span class="cp">%}</span>A multiline translatable
literal.<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>or with the <code class="docutils literal"><span class="pre">{#</span></code> ... <code class="docutils literal"><span class="pre">#}</span></code> <a class="reference internal" href="../../ref/templates/language.html#template-comments"><span class="std std-ref">one-line comment constructs</span></a>:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="c">{# Translators: Label of a button that triggers search #}</span>
<span class="p">&lt;</span><span class="nt">button</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;submit&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;Go&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>

<span class="c">{# Translators: This is a text of the base template #}</span>
<span class="cp">{%</span> <span class="k">blocktrans</span> <span class="cp">%}</span>Ambiguous translatable block of text<span class="cp">{%</span> <span class="k">endblocktrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Just for completeness, these are the corresponding fragments of the
resulting <code class="docutils literal"><span class="pre">.po</span></code> file:</p>
<div class="last highlight-po"><div class="highlight"><pre><span></span><span class="c1">#. Translators: View verb</span>
<span class="c1"># path/to/template/file.html:10</span>
<span class="nv">msgid</span> <span class="s">&quot;View&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>

<span class="c1">#. Translators: Short intro blurb</span>
<span class="c1"># path/to/template/file.html:13</span>
<span class="nv">msgid</span> <span class="s">&quot;&quot;</span>
<span class="s">&quot;A multiline translatable&quot;</span>
<span class="s">&quot;literal.&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>

<span class="c1"># ...</span>

<span class="c1">#. Translators: Label of a button that triggers search</span>
<span class="c1"># path/to/template/file.html:100</span>
<span class="nv">msgid</span> <span class="s">&quot;Go&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>

<span class="c1">#. Translators: This is a text of the base template</span>
<span class="c1"># path/to/template/file.html:103</span>
<span class="nv">msgid</span> <span class="s">&quot;Ambiguous translatable block of text&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-switching-language-in-templates">
<span id="s-std:templatetag-language"></span><span id="switching-language-in-templates"></span><span id="std:templatetag-language"></span><h3>Switching language in templates<a class="headerlink" href="#switching-language-in-templates" title="Permalink to this headline">¶</a></h3>
<p>If you want to select a language within a template, you can use the
<code class="docutils literal"><span class="pre">language</span></code> template tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">i18n</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">get_current_language</span> <span class="k">as</span> <span class="nv">LANGUAGE_CODE</span> <span class="cp">%}</span>
<span class="c">&lt;!-- Current language: </span><span class="cp">{{</span> <span class="nv">LANGUAGE_CODE</span> <span class="cp">}}</span><span class="c"> --&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;Welcome to our page&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>

<span class="cp">{%</span> <span class="k">language</span> <span class="s1">&#39;en&#39;</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">get_current_language</span> <span class="k">as</span> <span class="nv">LANGUAGE_CODE</span> <span class="cp">%}</span>
    <span class="c">&lt;!-- Current language: </span><span class="cp">{{</span> <span class="nv">LANGUAGE_CODE</span> <span class="cp">}}</span><span class="c"> --&gt;</span>
    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;Welcome to our page&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endlanguage</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>While the first occurrence of &#8220;Welcome to our page&#8221; uses the current language,
the second will always be in English.</p>
</div>
<div class="section" id="s-other-tags">
<span id="s-template-translation-vars"></span><span id="other-tags"></span><span id="template-translation-vars"></span><h3>Other tags<a class="headerlink" href="#other-tags" title="Permalink to this headline">¶</a></h3>
<p>These tags also require a <code class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">i18n</span> <span class="pre">%}</span></code>.</p>
<div class="section" id="s-get-available-languages">
<span id="s-std:templatetag-get_available_languages"></span><span id="get-available-languages"></span><span id="std:templatetag-get_available_languages"></span><h4><code class="docutils literal"><span class="pre">get_available_languages</span></code><a class="headerlink" href="#get-available-languages" title="Permalink to this headline">¶</a></h4>
<p><code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_available_languages</span> <span class="pre">as</span> <span class="pre">LANGUAGES</span> <span class="pre">%}</span></code> returns a list of tuples in
which the first element is the <a class="reference internal" href="index.html#term-language-code"><span class="xref std std-term">language code</span></a> and the second is the
language name (translated into the currently active locale).</p>
</div>
<div class="section" id="s-get-current-language">
<span id="s-std:templatetag-get_current_language"></span><span id="get-current-language"></span><span id="std:templatetag-get_current_language"></span><h4><code class="docutils literal"><span class="pre">get_current_language</span></code><a class="headerlink" href="#get-current-language" title="Permalink to this headline">¶</a></h4>
<p><code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_current_language</span> <span class="pre">as</span> <span class="pre">LANGUAGE_CODE</span> <span class="pre">%}</span></code> returns the current user&#8217;s
preferred language as a string. Example: <code class="docutils literal"><span class="pre">en-us</span></code>. See
<a class="reference internal" href="#how-django-discovers-language-preference"><span class="std std-ref">How Django discovers language preference</span></a>.</p>
</div>
<div class="section" id="s-get-current-language-bidi">
<span id="s-std:templatetag-get_current_language_bidi"></span><span id="get-current-language-bidi"></span><span id="std:templatetag-get_current_language_bidi"></span><h4><code class="docutils literal"><span class="pre">get_current_language_bidi</span></code><a class="headerlink" href="#get-current-language-bidi" title="Permalink to this headline">¶</a></h4>
<p><code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_current_language_bidi</span> <span class="pre">as</span> <span class="pre">LANGUAGE_BIDI</span> <span class="pre">%}</span></code> returns the current
locale&#8217;s direction. If <code class="docutils literal"><span class="pre">True</span></code>, it&#8217;s a right-to-left language, e.g. Hebrew,
Arabic. If <code class="docutils literal"><span class="pre">False</span></code> it&#8217;s a left-to-right language, e.g. English, French,
German, etc.</p>
<p>If you enable the <code class="docutils literal"><span class="pre">django.template.context_processors.i18n</span></code> context processor
then each <code class="docutils literal"><span class="pre">RequestContext</span></code> will have access to <code class="docutils literal"><span class="pre">LANGUAGES</span></code>,
<code class="docutils literal"><span class="pre">LANGUAGE_CODE</span></code>, and <code class="docutils literal"><span class="pre">LANGUAGE_BIDI</span></code> as defined above.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The <code class="docutils literal"><span class="pre">i18n</span></code> context processor is not enabled by default for new projects.</p>
</div>
</div>
<div class="section" id="s-get-language-info">
<span id="s-std:templatetag-get_language_info"></span><span id="get-language-info"></span><span id="std:templatetag-get_language_info"></span><h4><code class="docutils literal"><span class="pre">get_language_info</span></code><a class="headerlink" href="#get-language-info" title="Permalink to this headline">¶</a></h4>
<p>You can also retrieve information about any of the available languages using
provided template tags and filters. To get information about a single language,
use the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_language_info</span> <span class="pre">%}</span></code> tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">get_language_info</span> <span class="nv">for</span> <span class="nv">LANGUAGE_CODE</span> <span class="k">as</span> <span class="nv">lang</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">get_language_info</span> <span class="nv">for</span> <span class="s2">&quot;pl&quot;</span> <span class="k">as</span> <span class="nv">lang</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>You can then access the information:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span>Language code: <span class="cp">{{</span> <span class="nv">lang.code</span> <span class="cp">}}</span><span class="p">&lt;</span><span class="nt">br</span> <span class="p">/&gt;</span>
Name of language: <span class="cp">{{</span> <span class="nv">lang.name_local</span> <span class="cp">}}</span><span class="p">&lt;</span><span class="nt">br</span> <span class="p">/&gt;</span>
Name in English: <span class="cp">{{</span> <span class="nv">lang.name</span> <span class="cp">}}</span><span class="p">&lt;</span><span class="nt">br</span> <span class="p">/&gt;</span>
Bi-directional: <span class="cp">{{</span> <span class="nv">lang.bidi</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-get-language-info-list">
<span id="s-std:templatetag-get_language_info_list"></span><span id="get-language-info-list"></span><span id="std:templatetag-get_language_info_list"></span><h4><code class="docutils literal"><span class="pre">get_language_info_list</span></code><a class="headerlink" href="#get-language-info-list" title="Permalink to this headline">¶</a></h4>
<p>You can also use the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_language_info_list</span> <span class="pre">%}</span></code> template tag to retrieve
information for a list of languages (e.g. active languages as specified in
<a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGES</span></code></a>). See <a class="reference internal" href="#set-language-redirect-view"><span class="std std-ref">the section about the set_language redirect
view</span></a> for an example of how to display a language
selector using <code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_language_info_list</span> <span class="pre">%}</span></code>.</p>
<p>In addition to <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGES</span></code></a> style nested tuples,
<code class="docutils literal"><span class="pre">{%</span> <span class="pre">get_language_info_list</span> <span class="pre">%}</span></code> supports simple lists of language codes.
If you do this in your view:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">context</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;available_languages&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;en&#39;</span><span class="p">,</span> <span class="s1">&#39;es&#39;</span><span class="p">,</span> <span class="s1">&#39;fr&#39;</span><span class="p">]}</span>
<span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;mytemplate.html&#39;</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
</pre></div>
</div>
<p>you can iterate over those languages in the template:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">get_language_info_list</span> <span class="nv">for</span> <span class="nv">available_languages</span> <span class="k">as</span> <span class="nv">langs</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">lang</span> <span class="k">in</span> <span class="nv">langs</span> <span class="cp">%}</span> ... <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<span class="target" id="std:templatefilter-language_name"></span><span class="target" id="std:templatefilter-language_name_local"></span></div>
<div class="section" id="s-template-filters">
<span id="s-std:templatefilter-language_bidi"></span><span id="template-filters"></span><span id="std:templatefilter-language_bidi"></span><h4>Template filters<a class="headerlink" href="#template-filters" title="Permalink to this headline">¶</a></h4>
<p>There are also simple filters available for convenience:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">{{</span> <span class="pre">LANGUAGE_CODE|language_name</span> <span class="pre">}}</span></code> (&#8220;German&#8221;)</li>
<li><code class="docutils literal"><span class="pre">{{</span> <span class="pre">LANGUAGE_CODE|language_name_local</span> <span class="pre">}}</span></code> (&#8220;Deutsch&#8221;)</li>
<li><code class="docutils literal"><span class="pre">{{</span> <span class="pre">LANGUAGE_CODE|language_bidi</span> <span class="pre">}}</span></code> (False)</li>
</ul>
</div>
</div>
</div>
<div class="section" id="s-internationalization-in-javascript-code">
<span id="internationalization-in-javascript-code"></span><h2>Internationalization: in JavaScript code<a class="headerlink" href="#internationalization-in-javascript-code" title="Permalink to this headline">¶</a></h2>
<p>Adding translations to JavaScript poses some problems:</p>
<ul class="simple">
<li>JavaScript code doesn&#8217;t have access to a <code class="docutils literal"><span class="pre">gettext</span></code> implementation.</li>
<li>JavaScript code doesn&#8217;t have access to <code class="docutils literal"><span class="pre">.po</span></code> or <code class="docutils literal"><span class="pre">.mo</span></code> files; they need to
be delivered by the server.</li>
<li>The translation catalogs for JavaScript should be kept as small as
possible.</li>
</ul>
<p>Django provides an integrated solution for these problems: It passes the
translations into JavaScript, so you can call <code class="docutils literal"><span class="pre">gettext</span></code>, etc., from within
JavaScript.</p>
<div class="section" id="s-module-django.views.i18n">
<span id="s-the-javascript-catalog-view"></span><span id="s-javascript-catalog-view"></span><span id="module-django.views.i18n"></span><span id="the-javascript-catalog-view"></span><span id="javascript-catalog-view"></span><h3>The <code class="docutils literal"><span class="pre">javascript_catalog</span></code> view<a class="headerlink" href="#module-django.views.i18n" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.views.i18n.javascript_catalog">
<code class="descname">javascript_catalog</code>(<em>request</em>, <em>domain='djangojs'</em>, <em>packages=None</em>)<a class="reference internal" href="../../_modules/django/views/i18n.html#javascript_catalog"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.views.i18n.javascript_catalog" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The main solution to these problems is the
<a class="reference internal" href="#django.views.i18n.javascript_catalog" title="django.views.i18n.javascript_catalog"><code class="xref py py-meth docutils literal"><span class="pre">django.views.i18n.javascript_catalog()</span></code></a> view, which sends out a JavaScript
code library with functions that mimic the <code class="docutils literal"><span class="pre">gettext</span></code> interface, plus an array
of translation strings. Those translation strings are taken from applications or
Django core, according to what you specify in either the <code class="docutils literal"><span class="pre">info_dict</span></code> or the
URL. Paths listed in <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> are also included.</p>
<p>You hook it up like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.i18n</span> <span class="kn">import</span> <span class="n">javascript_catalog</span>

<span class="n">js_info_dict</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;packages&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;your.app.package&#39;</span><span class="p">,),</span>
<span class="p">}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^jsi18n/$&#39;</span><span class="p">,</span> <span class="n">javascript_catalog</span><span class="p">,</span> <span class="n">js_info_dict</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;javascript-catalog&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Each string in <code class="docutils literal"><span class="pre">packages</span></code> should be in Python dotted-package syntax (the
same format as the strings in <a class="reference internal" href="../../ref/settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a>) and should refer to a
package that contains a <code class="docutils literal"><span class="pre">locale</span></code> directory. If you specify multiple packages,
all those catalogs are merged into one catalog. This is useful if you have
JavaScript that uses strings from different applications.</p>
<p>The precedence of translations is such that the packages appearing later in the
<code class="docutils literal"><span class="pre">packages</span></code> argument have higher precedence than the ones appearing at the
beginning, this is important in the case of clashing translations for the same
literal.</p>
<p>By default, the view uses the <code class="docutils literal"><span class="pre">djangojs</span></code> gettext domain. This can be
changed by altering the <code class="docutils literal"><span class="pre">domain</span></code> argument.</p>
<p>You can make the view dynamic by putting the packages into the URL pattern:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^jsi18n/(?P&lt;packages&gt;\S+?)/$&#39;</span><span class="p">,</span> <span class="n">javascript_catalog</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;javascript-catalog&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>With this, you specify the packages as a list of package names delimited by &#8216;+&#8217;
signs in the URL. This is especially useful if your pages use code from
different apps and this changes often and you don&#8217;t want to pull in one big
catalog file. As a security measure, these values can only be either
<code class="docutils literal"><span class="pre">django.conf</span></code> or any package from the <a class="reference internal" href="../../ref/settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a> setting.</p>
<p>The JavaScript translations found in the paths listed in the
<a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> setting are also always included. To keep consistency
with the translations lookup order algorithm used for Python and templates, the
directories listed in <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> have the highest precedence with
the ones appearing first having higher precedence than the ones appearing
later.</p>
</div>
<div class="section" id="s-using-the-javascript-translation-catalog">
<span id="using-the-javascript-translation-catalog"></span><h3>Using the JavaScript translation catalog<a class="headerlink" href="#using-the-javascript-translation-catalog" title="Permalink to this headline">¶</a></h3>
<p>To use the catalog, just pull in the dynamically generated script like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;javascript-catalog&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>This uses reverse URL lookup to find the URL of the JavaScript catalog view.
When the catalog is loaded, your JavaScript code can use the following methods:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">gettext</span></code></li>
<li><code class="docutils literal"><span class="pre">ngettext</span></code></li>
<li><code class="docutils literal"><span class="pre">interpolate</span></code></li>
<li><code class="docutils literal"><span class="pre">get_format</span></code></li>
<li><code class="docutils literal"><span class="pre">gettext_noop</span></code></li>
<li><code class="docutils literal"><span class="pre">pgettext</span></code></li>
<li><code class="docutils literal"><span class="pre">npgettext</span></code></li>
<li><code class="docutils literal"><span class="pre">pluralidx</span></code></li>
</ul>
<div class="section" id="s-gettext">
<span id="gettext"></span><h4><code class="docutils literal"><span class="pre">gettext</span></code><a class="headerlink" href="#gettext" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">gettext</span></code> function behaves similarly to the standard <code class="docutils literal"><span class="pre">gettext</span></code>
interface within your Python code:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">gettext</span><span class="p">(</span><span class="s1">&#39;this is to be translated&#39;</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="s-ngettext">
<span id="ngettext"></span><h4><code class="docutils literal"><span class="pre">ngettext</span></code><a class="headerlink" href="#ngettext" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">ngettext</span></code> function provides an interface to pluralize words and
phrases:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">object_count</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1">// or 0, or 2, or 3, ...</span>
<span class="nx">s</span> <span class="o">=</span> <span class="nx">ngettext</span><span class="p">(</span><span class="s1">&#39;literal for the singular case&#39;</span><span class="p">,</span>
        <span class="s1">&#39;literal for the plural case&#39;</span><span class="p">,</span> <span class="nx">object_count</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="s-interpolate">
<span id="interpolate"></span><h4><code class="docutils literal"><span class="pre">interpolate</span></code><a class="headerlink" href="#interpolate" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">interpolate</span></code> function supports dynamically populating a format string.
The interpolation syntax is borrowed from Python, so the <code class="docutils literal"><span class="pre">interpolate</span></code>
function supports both positional and named interpolation:</p>
<ul>
<li><p class="first">Positional interpolation: <code class="docutils literal"><span class="pre">obj</span></code> contains a JavaScript Array object
whose elements values are then sequentially interpolated in their
corresponding <code class="docutils literal"><span class="pre">fmt</span></code> placeholders in the same order they appear.
For example:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nx">fmts</span> <span class="o">=</span> <span class="nx">ngettext</span><span class="p">(</span><span class="s1">&#39;There is %s object. Remaining: %s&#39;</span><span class="p">,</span>
        <span class="s1">&#39;There are %s objects. Remaining: %s&#39;</span><span class="p">,</span> <span class="mi">11</span><span class="p">);</span>
<span class="nx">s</span> <span class="o">=</span> <span class="nx">interpolate</span><span class="p">(</span><span class="nx">fmts</span><span class="p">,</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">20</span><span class="p">]);</span>
<span class="c1">// s is &#39;There are 11 objects. Remaining: 20&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">Named interpolation: This mode is selected by passing the optional
boolean <code class="docutils literal"><span class="pre">named</span></code> parameter as <code class="docutils literal"><span class="pre">true</span></code>. <code class="docutils literal"><span class="pre">obj</span></code> contains a JavaScript
object or associative array. For example:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nx">d</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nx">count</span><span class="o">:</span> <span class="mi">10</span><span class="p">,</span>
    <span class="nx">total</span><span class="o">:</span> <span class="mi">50</span>
<span class="p">};</span>

<span class="nx">fmts</span> <span class="o">=</span> <span class="nx">ngettext</span><span class="p">(</span><span class="s1">&#39;Total: %(total)s, there is %(count)s object&#39;</span><span class="p">,</span>
<span class="s1">&#39;there are %(count)s of a total of %(total)s objects&#39;</span><span class="p">,</span> <span class="nx">d</span><span class="p">.</span><span class="nx">count</span><span class="p">);</span>
<span class="nx">s</span> <span class="o">=</span> <span class="nx">interpolate</span><span class="p">(</span><span class="nx">fmts</span><span class="p">,</span> <span class="nx">d</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
</pre></div>
</div>
</li>
</ul>
<p>You shouldn&#8217;t go over the top with string interpolation, though: this is still
JavaScript, so the code has to make repeated regular-expression substitutions.
This isn&#8217;t as fast as string interpolation in Python, so keep it to those
cases where you really need it (for example, in conjunction with <code class="docutils literal"><span class="pre">ngettext</span></code>
to produce proper pluralizations).</p>
</div>
<div class="section" id="s-get-format">
<span id="get-format"></span><h4><code class="docutils literal"><span class="pre">get_format</span></code><a class="headerlink" href="#get-format" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">get_format</span></code> function has access to the configured i18n formatting
settings and can retrieve the format string for a given setting name:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">get_format</span><span class="p">(</span><span class="s1">&#39;DATE_FORMAT&#39;</span><span class="p">));</span>
<span class="c1">// &#39;N j, Y&#39;</span>
</pre></div>
</div>
<p>It has access to the following settings:</p>
<ul class="simple">
<li><a class="reference internal" href="../../ref/settings.html#std:setting-DATE_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">DATE_FORMAT</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-DATE_INPUT_FORMATS"><code class="xref std std-setting docutils literal"><span class="pre">DATE_INPUT_FORMATS</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-DATETIME_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">DATETIME_FORMAT</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-DATETIME_INPUT_FORMATS"><code class="xref std std-setting docutils literal"><span class="pre">DATETIME_INPUT_FORMATS</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-DECIMAL_SEPARATOR"><code class="xref std std-setting docutils literal"><span class="pre">DECIMAL_SEPARATOR</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-FIRST_DAY_OF_WEEK"><code class="xref std std-setting docutils literal"><span class="pre">FIRST_DAY_OF_WEEK</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-MONTH_DAY_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">MONTH_DAY_FORMAT</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-NUMBER_GROUPING"><code class="xref std std-setting docutils literal"><span class="pre">NUMBER_GROUPING</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-SHORT_DATE_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">SHORT_DATE_FORMAT</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-SHORT_DATETIME_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">SHORT_DATETIME_FORMAT</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-THOUSAND_SEPARATOR"><code class="xref std std-setting docutils literal"><span class="pre">THOUSAND_SEPARATOR</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-TIME_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">TIME_FORMAT</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-TIME_INPUT_FORMATS"><code class="xref std std-setting docutils literal"><span class="pre">TIME_INPUT_FORMATS</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-YEAR_MONTH_FORMAT"><code class="xref std std-setting docutils literal"><span class="pre">YEAR_MONTH_FORMAT</span></code></a></li>
</ul>
<p>This is useful for maintaining formatting consistency with the Python-rendered
values.</p>
</div>
<div class="section" id="s-gettext-noop">
<span id="gettext-noop"></span><h4><code class="docutils literal"><span class="pre">gettext_noop</span></code><a class="headerlink" href="#gettext-noop" title="Permalink to this headline">¶</a></h4>
<p>This emulates the <code class="docutils literal"><span class="pre">gettext</span></code> function but does nothing, returning whatever
is passed to it:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">gettext_noop</span><span class="p">(</span><span class="s1">&#39;this will not be translated&#39;</span><span class="p">));</span>
</pre></div>
</div>
<p>This is useful for stubbing out portions of the code that will need translation
in the future.</p>
</div>
<div class="section" id="s-pgettext">
<span id="pgettext"></span><h4><code class="docutils literal"><span class="pre">pgettext</span></code><a class="headerlink" href="#pgettext" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">pgettext</span></code> function behaves like the Python variant
(<a class="reference internal" href="../../ref/utils.html#django.utils.translation.pgettext" title="django.utils.translation.pgettext"><code class="xref py py-func docutils literal"><span class="pre">pgettext()</span></code></a>), providing a contextually
translated word:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">pgettext</span><span class="p">(</span><span class="s1">&#39;month name&#39;</span><span class="p">,</span> <span class="s1">&#39;May&#39;</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="s-npgettext">
<span id="npgettext"></span><h4><code class="docutils literal"><span class="pre">npgettext</span></code><a class="headerlink" href="#npgettext" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">npgettext</span></code> function also behaves like the Python variant
(<a class="reference internal" href="../../ref/utils.html#django.utils.translation.npgettext" title="django.utils.translation.npgettext"><code class="xref py py-func docutils literal"><span class="pre">npgettext()</span></code></a>), providing a <strong>pluralized</strong>
contextually translated word:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">npgettext</span><span class="p">(</span><span class="s1">&#39;group&#39;</span><span class="p">,</span> <span class="s1">&#39;party&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
<span class="c1">// party</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">npgettext</span><span class="p">(</span><span class="s1">&#39;group&#39;</span><span class="p">,</span> <span class="s1">&#39;party&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">));</span>
<span class="c1">// parties</span>
</pre></div>
</div>
</div>
<div class="section" id="s-pluralidx">
<span id="pluralidx"></span><h4><code class="docutils literal"><span class="pre">pluralidx</span></code><a class="headerlink" href="#pluralidx" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal"><span class="pre">pluralidx</span></code> function works in a similar way to the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatefilter-pluralize"><code class="xref std std-tfilter docutils literal"><span class="pre">pluralize</span></code></a>
template filter, determining if a given <code class="docutils literal"><span class="pre">count</span></code> should use a plural form of
a word or not:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span></span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">pluralidx</span><span class="p">(</span><span class="mi">0</span><span class="p">));</span>
<span class="c1">// true</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">pluralidx</span><span class="p">(</span><span class="mi">1</span><span class="p">));</span>
<span class="c1">// false</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">pluralidx</span><span class="p">(</span><span class="mi">2</span><span class="p">));</span>
<span class="c1">// true</span>
</pre></div>
</div>
<p>In the simplest case, if no custom pluralization is needed, this returns
<code class="docutils literal"><span class="pre">false</span></code> for the integer <code class="docutils literal"><span class="pre">1</span></code> and <code class="docutils literal"><span class="pre">true</span></code> for all other numbers.</p>
<p>However, pluralization is not this simple in all languages. If the language does
not support pluralization, an empty value is provided.</p>
<p>Additionally, if there are complex rules around pluralization, the catalog view
will render a conditional expression. This will evaluate to either a <code class="docutils literal"><span class="pre">true</span></code>
(should pluralize) or <code class="docutils literal"><span class="pre">false</span></code> (should <strong>not</strong> pluralize) value.</p>
</div>
</div>
<div class="section" id="s-note-on-performance">
<span id="note-on-performance"></span><h3>Note on performance<a class="headerlink" href="#note-on-performance" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#django.views.i18n.javascript_catalog" title="django.views.i18n.javascript_catalog"><code class="xref py py-func docutils literal"><span class="pre">javascript_catalog()</span></code></a> view generates the catalog
from <code class="docutils literal"><span class="pre">.mo</span></code> files on every request. Since its output is constant — at least
for a given version of a site — it&#8217;s a good candidate for caching.</p>
<p>Server-side caching will reduce CPU load. It&#8217;s easily implemented with the
<a class="reference internal" href="../cache.html#django.views.decorators.cache.cache_page" title="django.views.decorators.cache.cache_page"><code class="xref py py-func docutils literal"><span class="pre">cache_page()</span></code></a> decorator. To trigger cache
invalidation when your translations change, provide a version-dependent key
prefix, as shown in the example below, or map the view at a version-dependent
URL.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.decorators.cache</span> <span class="kn">import</span> <span class="n">cache_page</span>
<span class="kn">from</span> <span class="nn">django.views.i18n</span> <span class="kn">import</span> <span class="n">javascript_catalog</span>

<span class="c1"># The value returned by get_version() must change when translations change.</span>
<span class="nd">@cache_page</span><span class="p">(</span><span class="mi">86400</span><span class="p">,</span> <span class="n">key_prefix</span><span class="o">=</span><span class="s1">&#39;js18n-</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">get_version</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">cached_javascript_catalog</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="s1">&#39;djangojs&#39;</span><span class="p">,</span> <span class="n">packages</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">javascript_catalog</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">domain</span><span class="p">,</span> <span class="n">packages</span><span class="p">)</span>
</pre></div>
</div>
<p>Client-side caching will save bandwidth and make your site load faster. If
you&#8217;re using ETags (<a class="reference internal" href="../../ref/settings.html#std:setting-USE_ETAGS"><code class="xref std std-setting docutils literal"><span class="pre">USE_ETAGS</span> <span class="pre">=</span> <span class="pre">True</span></code></a>), you&#8217;re already
covered. Otherwise, you can apply <a class="reference internal" href="../conditional-view-processing.html#conditional-decorators"><span class="std std-ref">conditional decorators</span></a>. In the following example, the cache is invalidated
whenever you restart your application server.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.http</span> <span class="kn">import</span> <span class="n">last_modified</span>
<span class="kn">from</span> <span class="nn">django.views.i18n</span> <span class="kn">import</span> <span class="n">javascript_catalog</span>

<span class="n">last_modified_date</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>

<span class="nd">@last_modified</span><span class="p">(</span><span class="k">lambda</span> <span class="n">req</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">:</span> <span class="n">last_modified_date</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">cached_javascript_catalog</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="s1">&#39;djangojs&#39;</span><span class="p">,</span> <span class="n">packages</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">javascript_catalog</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">domain</span><span class="p">,</span> <span class="n">packages</span><span class="p">)</span>
</pre></div>
</div>
<p>You can even pre-generate the JavaScript catalog as part of your deployment
procedure and serve it as a static file. This radical technique is implemented
in <a class="reference external" href="http://django-statici18n.readthedocs.org/en/latest/">django-statici18n</a>.</p>
</div>
</div>
<div class="section" id="s-module-django.conf.urls.i18n">
<span id="s-internationalization-in-url-patterns"></span><span id="s-url-internationalization"></span><span id="module-django.conf.urls.i18n"></span><span id="internationalization-in-url-patterns"></span><span id="url-internationalization"></span><h2>Internationalization: in URL patterns<a class="headerlink" href="#module-django.conf.urls.i18n" title="Permalink to this headline">¶</a></h2>
<p>Django provides two mechanisms to internationalize URL patterns:</p>
<ul class="simple">
<li>Adding the language prefix to the root of the URL patterns to make it
possible for <a class="reference internal" href="../../ref/middleware.html#django.middleware.locale.LocaleMiddleware" title="django.middleware.locale.LocaleMiddleware"><code class="xref py py-class docutils literal"><span class="pre">LocaleMiddleware</span></code></a> to detect
the language to activate from the requested URL.</li>
<li>Making URL patterns themselves translatable via the
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext_lazy" title="django.utils.translation.ugettext_lazy"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.ugettext_lazy()</span></code></a> function.</li>
</ul>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Using either one of these features requires that an active language be set
for each request; in other words, you need to have
<a class="reference internal" href="../../ref/middleware.html#django.middleware.locale.LocaleMiddleware" title="django.middleware.locale.LocaleMiddleware"><code class="xref py py-class docutils literal"><span class="pre">django.middleware.locale.LocaleMiddleware</span></code></a> in your
<a class="reference internal" href="../../ref/settings.html#std:setting-MIDDLEWARE_CLASSES"><code class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></code></a> setting.</p>
</div>
<div class="section" id="s-language-prefix-in-url-patterns">
<span id="language-prefix-in-url-patterns"></span><h3>Language prefix in URL patterns<a class="headerlink" href="#language-prefix-in-url-patterns" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.conf.urls.i18n.i18n_patterns">
<code class="descname">i18n_patterns</code>(<em>prefix</em>, <em>pattern_description</em>, <em>...</em>)<a class="reference internal" href="../../_modules/django/conf/urls/i18n.html#i18n_patterns"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.conf.urls.i18n.i18n_patterns" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span>The <code class="docutils literal"><span class="pre">prefix</span></code> argument to <code class="docutils literal"><span class="pre">i18n_patterns()</span></code> has been deprecated and will
not be supported in Django 1.10. Simply pass a list of
<a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal"><span class="pre">django.conf.urls.url()</span></code></a> instances instead.</p>
</div>
<p>This function can be used in your root URLconf and Django will automatically
prepend the current active language code to all url patterns defined within
<a class="reference internal" href="#django.conf.urls.i18n.i18n_patterns" title="django.conf.urls.i18n.i18n_patterns"><code class="xref py py-func docutils literal"><span class="pre">i18n_patterns()</span></code></a>. Example URL patterns:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">django.conf.urls.i18n</span> <span class="kn">import</span> <span class="n">i18n_patterns</span>

<span class="kn">from</span> <span class="nn">about</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">about_views</span>
<span class="kn">from</span> <span class="nn">news</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">news_views</span>
<span class="kn">from</span> <span class="nn">sitemap.views</span> <span class="kn">import</span> <span class="n">sitemap</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;sitemap-xml&#39;</span><span class="p">),</span>
<span class="p">]</span>

<span class="n">news_patterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^$&#39;</span><span class="p">,</span> <span class="n">news_views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^category/(?P&lt;slug&gt;[\w-]+)/$&#39;</span><span class="p">,</span> <span class="n">news_views</span><span class="o">.</span><span class="n">category</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;category&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^(?P&lt;slug&gt;[\w-]+)/$&#39;</span><span class="p">,</span> <span class="n">news_views</span><span class="o">.</span><span class="n">details</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;detail&#39;</span><span class="p">),</span>
<span class="p">]</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">i18n_patterns</span><span class="p">(</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">about_views</span><span class="o">.</span><span class="n">main</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;about&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^news/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="n">news_patterns</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="s1">&#39;news&#39;</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>After defining these URL patterns, Django will automatically add the
language prefix to the URL patterns that were added by the <code class="docutils literal"><span class="pre">i18n_patterns</span></code>
function. Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">activate</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">activate</span><span class="p">(</span><span class="s1">&#39;en&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;sitemap-xml&#39;</span><span class="p">)</span>
<span class="go">&#39;/sitemap.xml&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;news:index&#39;</span><span class="p">)</span>
<span class="go">&#39;/en/news/&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">activate</span><span class="p">(</span><span class="s1">&#39;nl&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;news:detail&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;slug&#39;</span><span class="p">:</span> <span class="s1">&#39;news-slug&#39;</span><span class="p">})</span>
<span class="go">&#39;/nl/news/news-slug/&#39;</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last"><a class="reference internal" href="#django.conf.urls.i18n.i18n_patterns" title="django.conf.urls.i18n.i18n_patterns"><code class="xref py py-func docutils literal"><span class="pre">i18n_patterns()</span></code></a> is only allowed in your root
URLconf. Using it within an included URLconf will throw an
<a class="reference internal" href="../../ref/exceptions.html#django.core.exceptions.ImproperlyConfigured" title="django.core.exceptions.ImproperlyConfigured"><code class="xref py py-exc docutils literal"><span class="pre">ImproperlyConfigured</span></code></a> exception.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Ensure that you don&#8217;t have non-prefixed URL patterns that might collide
with an automatically-added language prefix.</p>
</div>
</div>
<div class="section" id="s-translating-url-patterns">
<span id="translating-url-patterns"></span><h3>Translating URL patterns<a class="headerlink" href="#translating-url-patterns" title="Permalink to this headline">¶</a></h3>
<p>URL patterns can also be marked translatable using the
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext_lazy" title="django.utils.translation.ugettext_lazy"><code class="xref py py-func docutils literal"><span class="pre">ugettext_lazy()</span></code></a> function. Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="kn">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">django.conf.urls.i18n</span> <span class="kn">import</span> <span class="n">i18n_patterns</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="kn">from</span> <span class="nn">about</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">about_views</span>
<span class="kn">from</span> <span class="nn">news</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">news_views</span>
<span class="kn">from</span> <span class="nn">sitemaps.views</span> <span class="kn">import</span> <span class="n">sitemap</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^sitemap\.xml$&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;sitemap-xml&#39;</span><span class="p">),</span>
<span class="p">]</span>

<span class="n">news_patterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^$&#39;</span><span class="p">,</span> <span class="n">news_views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">r&#39;^category/(?P&lt;slug&gt;[\w-]+)/$&#39;</span><span class="p">),</span> <span class="n">news_views</span><span class="o">.</span><span class="n">category</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;category&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^(?P&lt;slug&gt;[\w-]+)/$&#39;</span><span class="p">,</span> <span class="n">news_views</span><span class="o">.</span><span class="n">details</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;detail&#39;</span><span class="p">),</span>
<span class="p">]</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">i18n_patterns</span><span class="p">(</span>
    <span class="n">url</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">r&#39;^about/$&#39;</span><span class="p">),</span> <span class="n">about_views</span><span class="o">.</span><span class="n">main</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;about&#39;</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">r&#39;^news/&#39;</span><span class="p">),</span> <span class="n">include</span><span class="p">(</span><span class="n">news_patterns</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="s1">&#39;news&#39;</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>After you&#8217;ve created the translations, the
<a class="reference internal" href="../../ref/urlresolvers.html#django.core.urlresolvers.reverse" title="django.core.urlresolvers.reverse"><code class="xref py py-func docutils literal"><span class="pre">reverse()</span></code></a> function will return the URL in the
active language. Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">activate</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">activate</span><span class="p">(</span><span class="s1">&#39;en&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;news:category&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;slug&#39;</span><span class="p">:</span> <span class="s1">&#39;recent&#39;</span><span class="p">})</span>
<span class="go">&#39;/en/news/category/recent/&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">activate</span><span class="p">(</span><span class="s1">&#39;nl&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;news:category&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;slug&#39;</span><span class="p">:</span> <span class="s1">&#39;recent&#39;</span><span class="p">})</span>
<span class="go">&#39;/nl/nieuws/categorie/recent/&#39;</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">In most cases, it&#8217;s best to use translated URLs only within a
language-code-prefixed block of patterns (using
<a class="reference internal" href="#django.conf.urls.i18n.i18n_patterns" title="django.conf.urls.i18n.i18n_patterns"><code class="xref py py-func docutils literal"><span class="pre">i18n_patterns()</span></code></a>), to avoid the possibility
that a carelessly translated URL causes a collision with a non-translated
URL pattern.</p>
</div>
</div>
<div class="section" id="s-reversing-in-templates">
<span id="s-id2"></span><span id="reversing-in-templates"></span><span id="id2"></span><h3>Reversing in templates<a class="headerlink" href="#reversing-in-templates" title="Permalink to this headline">¶</a></h3>
<p>If localized URLs get reversed in templates they always use the current
language. To link to a URL in another language use the <a class="reference internal" href="#std:templatetag-language"><code class="xref std std-ttag docutils literal"><span class="pre">language</span></code></a>
template tag. It enables the given language in the enclosed template section:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">i18n</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">get_available_languages</span> <span class="k">as</span> <span class="nv">languages</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">trans</span> <span class="s2">&quot;View this category in:&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">lang_code</span><span class="o">,</span> <span class="nv">lang_name</span> <span class="k">in</span> <span class="nv">languages</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">language</span> <span class="nv">lang_code</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;category&#39;</span> <span class="nv">slug</span><span class="o">=</span><span class="nv">category.slug</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">lang_name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endlanguage</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#std:templatetag-language"><code class="xref std std-ttag docutils literal"><span class="pre">language</span></code></a> tag expects the language code as the only argument.</p>
</div>
</div>
<div class="section" id="s-localization-how-to-create-language-files">
<span id="s-how-to-create-language-files"></span><span id="localization-how-to-create-language-files"></span><span id="how-to-create-language-files"></span><h2>Localization: how to create language files<a class="headerlink" href="#localization-how-to-create-language-files" title="Permalink to this headline">¶</a></h2>
<p>Once the string literals of an application have been tagged for later
translation, the translation themselves need to be written (or obtained). Here&#8217;s
how that works.</p>
<div class="section" id="s-message-files">
<span id="message-files"></span><h3>Message files<a class="headerlink" href="#message-files" title="Permalink to this headline">¶</a></h3>
<p>The first step is to create a <a class="reference internal" href="index.html#term-message-file"><span class="xref std std-term">message file</span></a> for a new language. A message
file is a plain-text file, representing a single language, that contains all
available translation strings and how they should be represented in the given
language. Message files have a <code class="docutils literal"><span class="pre">.po</span></code> file extension.</p>
<p>Django comes with a tool, <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a>, that automates the creation and upkeep of these files.</p>
<div class="admonition-gettext-utilities admonition">
<p class="first admonition-title">Gettext utilities</p>
<p>The <code class="docutils literal"><span class="pre">makemessages</span></code> command (and <code class="docutils literal"><span class="pre">compilemessages</span></code> discussed later) use
commands from the GNU gettext toolset: <code class="docutils literal"><span class="pre">xgettext</span></code>, <code class="docutils literal"><span class="pre">msgfmt</span></code>,
<code class="docutils literal"><span class="pre">msgmerge</span></code> and <code class="docutils literal"><span class="pre">msguniq</span></code>.</p>
<p class="last">The minimum version of the <code class="docutils literal"><span class="pre">gettext</span></code> utilities supported is 0.15.</p>
</div>
<p>To create or update a message file, run this command:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">makemessages</span> <span class="o">-</span><span class="n">l</span> <span class="n">de</span>
</pre></div>
</div>
<p>...where <code class="docutils literal"><span class="pre">de</span></code> is the <a class="reference internal" href="index.html#term-locale-name"><span class="xref std std-term">locale name</span></a> for the message file you want to
create. For example, <code class="docutils literal"><span class="pre">pt_BR</span></code> for Brazilian Portuguese, <code class="docutils literal"><span class="pre">de_AT</span></code> for Austrian
German or <code class="docutils literal"><span class="pre">id</span></code> for Indonesian.</p>
<p>The script should be run from one of two places:</p>
<ul class="simple">
<li>The root directory of your Django project (the one that contains
<code class="docutils literal"><span class="pre">manage.py</span></code>).</li>
<li>The root directory of one of your Django apps.</li>
</ul>
<p>The script runs over your project source tree or your application source tree
and pulls out all strings marked for translation (see
<a class="reference internal" href="#how-django-discovers-translations"><span class="std std-ref">How Django discovers translations</span></a> and be sure <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a>
is configured correctly). It creates (or updates) a message file in the
directory <code class="docutils literal"><span class="pre">locale/LANG/LC_MESSAGES</span></code>. In the <code class="docutils literal"><span class="pre">de</span></code> example, the file will be
<code class="docutils literal"><span class="pre">locale/de/LC_MESSAGES/django.po</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>When you run <code class="docutils literal"><span class="pre">makemessages</span></code> from the root directory of your project, the
extracted strings will be automatically distributed to the proper message
files. That is, a string extracted from a file of an app containing a
<code class="docutils literal"><span class="pre">locale</span></code> directory will go in a message file under that directory.
A string extracted from a file of an app without any <code class="docutils literal"><span class="pre">locale</span></code> directory
will either go in a message file under the directory listed first in
<a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> or will generate an error if <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a>
is empty.</p>
</div>
<p>By default <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a> examines every
file that has the <code class="docutils literal"><span class="pre">.html</span></code> or <code class="docutils literal"><span class="pre">.txt</span></code> file extension. In case you want to
override that default, use the <code class="docutils literal"><span class="pre">--extension</span></code> or <code class="docutils literal"><span class="pre">-e</span></code> option to specify the
file extensions to examine:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">makemessages</span> <span class="o">-</span><span class="n">l</span> <span class="n">de</span> <span class="o">-</span><span class="n">e</span> <span class="n">txt</span>
</pre></div>
</div>
<p>Separate multiple extensions with commas and/or use <code class="docutils literal"><span class="pre">-e</span></code> or <code class="docutils literal"><span class="pre">--extension</span></code>
multiple times:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">makemessages</span> <span class="o">-</span><span class="n">l</span> <span class="n">de</span> <span class="o">-</span><span class="n">e</span> <span class="n">html</span><span class="p">,</span><span class="n">txt</span> <span class="o">-</span><span class="n">e</span> <span class="n">xml</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">When <a class="reference internal" href="#creating-message-files-from-js-code"><span class="std std-ref">creating message files from JavaScript source code</span></a> you need to use the special
&#8216;djangojs&#8217; domain, <strong>not</strong> <code class="docutils literal"><span class="pre">-e</span> <span class="pre">js</span></code>.</p>
</div>
<div class="admonition-using-jinja2-templates admonition">
<p class="first admonition-title">Using Jinja2 templates?</p>
<p><a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a> doesn&#8217;t understand the syntax of Jinja2 templates.
To extract strings from a project containing Jinja2 templates, use <a class="reference external" href="http://babel.pocoo.org/docs/messages/#message-extraction">Message
Extracting</a> from <a class="reference external" href="http://babel.pocoo.org/">Babel</a> instead.</p>
<p>Here&#8217;s an example <code class="docutils literal"><span class="pre">babel.cfg</span></code> configuration file:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Extraction from Python source files</span>
<span class="p">[</span><span class="n">python</span><span class="p">:</span> <span class="o">**.</span><span class="n">py</span><span class="p">]</span>

<span class="c1"># Extraction from Jinja2 templates</span>
<span class="p">[</span><span class="n">jinja2</span><span class="p">:</span> <span class="o">**.</span><span class="n">jinja</span><span class="p">]</span>
<span class="n">extensions</span> <span class="o">=</span> <span class="n">jinja2</span><span class="o">.</span><span class="n">ext</span><span class="o">.</span><span class="n">with_</span>
</pre></div>
</div>
<p>Make sure you list all extensions you&#8217;re using! Otherwise Babel won&#8217;t
recognize the tags defined by these extensions and will ignore Jinja2
templates containing them entirely.</p>
<p class="last">Babel provides similar features to <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a>, can replace it
in general, and doesn&#8217;t depend on <code class="docutils literal"><span class="pre">gettext</span></code>. For more information, read
its documentation about <a class="reference external" href="http://babel.pocoo.org/docs/messages/">working with message catalogs</a>.</p>
</div>
<div class="admonition-no-gettext admonition">
<p class="first admonition-title">No gettext?</p>
<p class="last">If you don&#8217;t have the <code class="docutils literal"><span class="pre">gettext</span></code> utilities installed,
<a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a> will create empty files. If that&#8217;s the case, either
install the <code class="docutils literal"><span class="pre">gettext</span></code> utilities or just copy the English message file
(<code class="docutils literal"><span class="pre">locale/en/LC_MESSAGES/django.po</span></code>) if available and use it as a starting
point; it&#8217;s just an empty translation file.</p>
</div>
<div class="admonition-working-on-windows admonition">
<p class="first admonition-title">Working on Windows?</p>
<p class="last">If you&#8217;re using Windows and need to install the GNU gettext utilities so
<a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a> works, see <a class="reference internal" href="#gettext-on-windows"><span class="std std-ref">gettext on Windows</span></a> for more
information.</p>
</div>
<p>The format of <code class="docutils literal"><span class="pre">.po</span></code> files is straightforward. Each <code class="docutils literal"><span class="pre">.po</span></code> file contains a
small bit of metadata, such as the translation maintainer&#8217;s contact
information, but the bulk of the file is a list of <strong>messages</strong> &#8211; simple
mappings between translation strings and the actual translated text for the
particular language.</p>
<p>For example, if your Django app contained a translation string for the text
<code class="docutils literal"><span class="pre">&quot;Welcome</span> <span class="pre">to</span> <span class="pre">my</span> <span class="pre">site.&quot;</span></code>, like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;Welcome to my site.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>...then <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a> will have created
a <code class="docutils literal"><span class="pre">.po</span></code> file containing the following snippet &#8211; a message:</p>
<div class="highlight-po"><div class="highlight"><pre><span></span><span class="kd">#: path/to/python/module.py:23</span>
<span class="nv">msgid</span> <span class="s">&quot;Welcome to my site.&quot;</span>
<span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>
</pre></div>
</div>
<p>A quick explanation:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">msgid</span></code> is the translation string, which appears in the source. Don&#8217;t
change it.</li>
<li><code class="docutils literal"><span class="pre">msgstr</span></code> is where you put the language-specific translation. It starts
out empty, so it&#8217;s your responsibility to change it. Make sure you keep
the quotes around your translation.</li>
<li>As a convenience, each message includes, in the form of a comment line
prefixed with <code class="docutils literal"><span class="pre">#</span></code> and located above the <code class="docutils literal"><span class="pre">msgid</span></code> line, the filename and
line number from which the translation string was gleaned.</li>
</ul>
<p>Long messages are a special case. There, the first string directly after the
<code class="docutils literal"><span class="pre">msgstr</span></code> (or <code class="docutils literal"><span class="pre">msgid</span></code>) is an empty string. Then the content itself will be
written over the next few lines as one string per line. Those strings are
directly concatenated. Don&#8217;t forget trailing spaces within the strings;
otherwise, they&#8217;ll be tacked together without whitespace!</p>
<div class="admonition-mind-your-charset admonition">
<p class="first admonition-title">Mind your charset</p>
<p class="last">Due to the way the <code class="docutils literal"><span class="pre">gettext</span></code> tools work internally and because we want to
allow non-ASCII source strings in Django&#8217;s core and your applications, you
<strong>must</strong> use UTF-8 as the encoding for your PO files (the default when PO
files are created).  This means that everybody will be using the same
encoding, which is important when Django processes the PO files.</p>
</div>
<p>To reexamine all source code and templates for new translation strings and
update all message files for <strong>all</strong> languages, run this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">makemessages</span> <span class="o">-</span><span class="n">a</span>
</pre></div>
</div>
</div>
<div class="section" id="s-compiling-message-files">
<span id="compiling-message-files"></span><h3>Compiling message files<a class="headerlink" href="#compiling-message-files" title="Permalink to this headline">¶</a></h3>
<p>After you create your message file &#8211; and each time you make changes to it &#8211;
you&#8217;ll need to compile it into a more efficient form, for use by <code class="docutils literal"><span class="pre">gettext</span></code>. Do
this with the <a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">compilemessages</span></code></a>
utility.</p>
<p>This tool runs over all available <code class="docutils literal"><span class="pre">.po</span></code> files and creates <code class="docutils literal"><span class="pre">.mo</span></code> files, which
are binary files optimized for use by <code class="docutils literal"><span class="pre">gettext</span></code>. In the same directory from
which you ran <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a>, run
<a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">compilemessages</span></code></a> like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">compilemessages</span>
</pre></div>
</div>
<p>That&#8217;s it. Your translations are ready for use.</p>
<div class="admonition-working-on-windows admonition">
<p class="first admonition-title">Working on Windows?</p>
<p class="last">If you&#8217;re using Windows and need to install the GNU gettext utilities so
<a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">compilemessages</span></code></a> works see
<a class="reference internal" href="#gettext-on-windows"><span class="std std-ref">gettext on Windows</span></a> for more information.</p>
</div>
<div class="admonition-po-files-encoding-and-bom-usage admonition">
<p class="first admonition-title">.po files: Encoding and BOM usage.</p>
<p class="last">Django only supports <code class="docutils literal"><span class="pre">.po</span></code> files encoded in UTF-8 and without any BOM
(Byte Order Mark) so if your text editor adds such marks to the beginning of
files by default then you will need to reconfigure it.</p>
</div>
</div>
<div class="section" id="s-creating-message-files-from-javascript-source-code">
<span id="s-creating-message-files-from-js-code"></span><span id="creating-message-files-from-javascript-source-code"></span><span id="creating-message-files-from-js-code"></span><h3>Creating message files from JavaScript source code<a class="headerlink" href="#creating-message-files-from-javascript-source-code" title="Permalink to this headline">¶</a></h3>
<p>You create and update the message files the same way as the other Django message
files &#8211; with the <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a> tool.
The only difference is you need to explicitly specify what in gettext parlance
is known as a domain in this case the <code class="docutils literal"><span class="pre">djangojs</span></code> domain, by providing a <code class="docutils literal"><span class="pre">-d</span>
<span class="pre">djangojs</span></code> parameter, like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">makemessages</span> <span class="o">-</span><span class="n">d</span> <span class="n">djangojs</span> <span class="o">-</span><span class="n">l</span> <span class="n">de</span>
</pre></div>
</div>
<p>This would create or update the message file for JavaScript for German. After
updating message files, just run <a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">compilemessages</span></code></a> the same way as you do with normal Django message files.</p>
</div>
<div class="section" id="s-gettext-on-windows">
<span id="s-id3"></span><span id="gettext-on-windows"></span><span id="id3"></span><h3><code class="docutils literal"><span class="pre">gettext</span></code> on Windows<a class="headerlink" href="#gettext-on-windows" title="Permalink to this headline">¶</a></h3>
<p>This is only needed for people who either want to extract message IDs or compile
message files (<code class="docutils literal"><span class="pre">.po</span></code>). Translation work itself just involves editing existing
files of this type, but if you want to create your own message files, or want to
test or compile a changed message file, you will need the <code class="docutils literal"><span class="pre">gettext</span></code> utilities:</p>
<ul>
<li><p class="first">Download the following zip files from the GNOME servers
<a class="reference external" href="https://download.gnome.org/binaries/win32/dependencies/">https://download.gnome.org/binaries/win32/dependencies/</a></p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">gettext-runtime-X.zip</span></code></li>
<li><code class="docutils literal"><span class="pre">gettext-tools-X.zip</span></code></li>
</ul>
<p><code class="docutils literal"><span class="pre">X</span></code> is the version number, we are requiring <code class="docutils literal"><span class="pre">0.15</span></code> or higher.</p>
</li>
<li><p class="first">Extract the contents of the <code class="docutils literal"><span class="pre">bin\</span></code> directories in both files to the
same folder on your system (i.e. <code class="docutils literal"><span class="pre">C:\Program</span> <span class="pre">Files\gettext-utils</span></code>)</p>
</li>
<li><p class="first">Update the system PATH:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">Control</span> <span class="pre">Panel</span> <span class="pre">&gt;</span> <span class="pre">System</span> <span class="pre">&gt;</span> <span class="pre">Advanced</span> <span class="pre">&gt;</span> <span class="pre">Environment</span> <span class="pre">Variables</span></code>.</li>
<li>In the <code class="docutils literal"><span class="pre">System</span> <span class="pre">variables</span></code> list, click <code class="docutils literal"><span class="pre">Path</span></code>, click <code class="docutils literal"><span class="pre">Edit</span></code>.</li>
<li>Add <code class="docutils literal"><span class="pre">;C:\Program</span> <span class="pre">Files\gettext-utils\bin</span></code> at the end of the
<code class="docutils literal"><span class="pre">Variable</span> <span class="pre">value</span></code> field.</li>
</ul>
</li>
</ul>
<p>You may also use <code class="docutils literal"><span class="pre">gettext</span></code> binaries you have obtained elsewhere, so long as
the <code class="docutils literal"><span class="pre">xgettext</span> <span class="pre">--version</span></code> command works properly. Do not attempt to use Django
translation utilities with a <code class="docutils literal"><span class="pre">gettext</span></code> package if the command <code class="docutils literal"><span class="pre">xgettext</span>
<span class="pre">--version</span></code> entered at a Windows command prompt causes a popup window saying
&#8220;xgettext.exe has generated errors and will be closed by Windows&#8221;.</p>
</div>
<div class="section" id="s-customizing-the-makemessages-command">
<span id="s-customizing-makemessages"></span><span id="customizing-the-makemessages-command"></span><span id="customizing-makemessages"></span><h3>Customizing the <code class="docutils literal"><span class="pre">makemessages</span></code> command<a class="headerlink" href="#customizing-the-makemessages-command" title="Permalink to this headline">¶</a></h3>
<p>If you want to pass additional parameters to <code class="docutils literal"><span class="pre">xgettext</span></code>, you need to create a
custom <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a> command and override its <code class="docutils literal"><span class="pre">xgettext_options</span></code>
attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.management.commands</span> <span class="kn">import</span> <span class="n">makemessages</span>

<span class="k">class</span> <span class="nc">Command</span><span class="p">(</span><span class="n">makemessages</span><span class="o">.</span><span class="n">Command</span><span class="p">):</span>
    <span class="n">xgettext_options</span> <span class="o">=</span> <span class="n">makemessages</span><span class="o">.</span><span class="n">Command</span><span class="o">.</span><span class="n">xgettext_options</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;--keyword=mytrans&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>If you need more flexibility, you could also add a new argument to your custom
<a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">makemessages</span></code></a> command:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.management.commands</span> <span class="kn">import</span> <span class="n">makemessages</span>

<span class="k">class</span> <span class="nc">Command</span><span class="p">(</span><span class="n">makemessages</span><span class="o">.</span><span class="n">Command</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Command</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_arguments</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--extra-keyword&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;xgettext_keywords&#39;</span><span class="p">,</span>
                            <span class="n">action</span><span class="o">=</span><span class="s1">&#39;append&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="n">xgettext_keywords</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;xgettext_keywords&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">xgettext_keywords</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">xgettext_options</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">makemessages</span><span class="o">.</span><span class="n">Command</span><span class="o">.</span><span class="n">xgettext_options</span><span class="p">[:]</span> <span class="o">+</span>
                <span class="p">[</span><span class="s1">&#39;--keyword=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">kwd</span> <span class="k">for</span> <span class="n">kwd</span> <span class="ow">in</span> <span class="n">xgettext_keywords</span><span class="p">]</span>
            <span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Command</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">handle</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-miscellaneous">
<span id="miscellaneous"></span><h2>Miscellaneous<a class="headerlink" href="#miscellaneous" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-the-set-language-redirect-view">
<span id="s-set-language-redirect-view"></span><span id="the-set-language-redirect-view"></span><span id="set-language-redirect-view"></span><h3>The <code class="docutils literal"><span class="pre">set_language</span></code> redirect view<a class="headerlink" href="#the-set-language-redirect-view" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="django.views.i18n.set_language">
<code class="descname">set_language</code>(<em>request</em>)<a class="reference internal" href="../../_modules/django/views/i18n.html#set_language"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.views.i18n.set_language" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>As a convenience, Django comes with a view, <a class="reference internal" href="#django.views.i18n.set_language" title="django.views.i18n.set_language"><code class="xref py py-func docutils literal"><span class="pre">django.views.i18n.set_language()</span></code></a>,
that sets a user&#8217;s language preference and redirects to a given URL or, by default,
back to the previous page.</p>
<p>Activate this view by adding the following line to your URLconf:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">url</span><span class="p">(</span><span class="s1">r&#39;^i18n/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s1">&#39;django.conf.urls.i18n&#39;</span><span class="p">)),</span>
</pre></div>
</div>
<p>(Note that this example makes the view available at <code class="docutils literal"><span class="pre">/i18n/setlang/</span></code>.)</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Make sure that you don&#8217;t include the above URL within
<a class="reference internal" href="#django.conf.urls.i18n.i18n_patterns" title="django.conf.urls.i18n.i18n_patterns"><code class="xref py py-func docutils literal"><span class="pre">i18n_patterns()</span></code></a> - it needs to be
language-independent itself to work correctly.</p>
</div>
<p>The view expects to be called via the <code class="docutils literal"><span class="pre">POST</span></code> method, with a <code class="docutils literal"><span class="pre">language</span></code>
parameter set in request. If session support is enabled, the view
saves the language choice in the user&#8217;s session. Otherwise, it saves the
language choice in a cookie that is by default named <code class="docutils literal"><span class="pre">django_language</span></code>.
(The name can be changed through the <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_NAME"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_NAME</span></code></a> setting.)</p>
<p>After setting the language choice, Django redirects the user, following this
algorithm:</p>
<ul class="simple">
<li>Django looks for a <code class="docutils literal"><span class="pre">next</span></code> parameter in the <code class="docutils literal"><span class="pre">POST</span></code> data.</li>
<li>If that doesn&#8217;t exist, or is empty, Django tries the URL in the
<code class="docutils literal"><span class="pre">Referrer</span></code> header.</li>
<li>If that&#8217;s empty &#8211; say, if a user&#8217;s browser suppresses that header &#8211;
then the user will be redirected to <code class="docutils literal"><span class="pre">/</span></code> (the site root) as a fallback.</li>
</ul>
<p>Here&#8217;s example HTML template code:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">i18n</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">form</span> <span class="na">action</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;set_language&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">method</span><span class="o">=</span><span class="s">&quot;post&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">csrf_token</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">name</span><span class="o">=</span><span class="s">&quot;next&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;hidden&quot;</span> <span class="na">value</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">redirect_to</span> <span class="cp">}}</span><span class="s">&quot;</span> <span class="p">/&gt;</span>
    <span class="p">&lt;</span><span class="nt">select</span> <span class="na">name</span><span class="o">=</span><span class="s">&quot;language&quot;</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">get_current_language</span> <span class="k">as</span> <span class="nv">LANGUAGE_CODE</span> <span class="cp">%}</span>
        <span class="cp">{%</span> <span class="k">get_available_languages</span> <span class="k">as</span> <span class="nv">LANGUAGES</span> <span class="cp">%}</span>
        <span class="cp">{%</span> <span class="k">get_language_info_list</span> <span class="nv">for</span> <span class="nv">LANGUAGES</span> <span class="k">as</span> <span class="nv">languages</span> <span class="cp">%}</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">language</span> <span class="k">in</span> <span class="nv">languages</span> <span class="cp">%}</span>
            <span class="p">&lt;</span><span class="nt">option</span> <span class="na">value</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">language.code</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">language.code</span> <span class="o">==</span> <span class="nv">LANGUAGE_CODE</span> <span class="cp">%}</span> <span class="na">selected</span><span class="o">=</span><span class="s">&quot;selected&quot;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="p">&gt;</span>
                <span class="cp">{{</span> <span class="nv">language.name_local</span> <span class="cp">}}</span> (<span class="cp">{{</span> <span class="nv">language.code</span> <span class="cp">}}</span>)
            <span class="p">&lt;/</span><span class="nt">option</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="p">&lt;/</span><span class="nt">select</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;submit&quot;</span> <span class="na">value</span><span class="o">=</span><span class="s">&quot;Go&quot;</span> <span class="p">/&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>In this example, Django looks up the URL of the page to which the user will be
redirected in the <code class="docutils literal"><span class="pre">redirect_to</span></code> context variable.</p>
</div>
<div class="section" id="s-explicitly-setting-the-active-language">
<span id="explicitly-setting-the-active-language"></span><h3>Explicitly setting the active language<a class="headerlink" href="#explicitly-setting-the-active-language" title="Permalink to this headline">¶</a></h3>
<p>You may want to set the active language for the current session explicitly. Perhaps
a user&#8217;s language preference is retrieved from another system, for example.
You&#8217;ve already been introduced to <a class="reference internal" href="../../ref/utils.html#django.utils.translation.activate" title="django.utils.translation.activate"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.activate()</span></code></a>. That
applies to the current thread only. To persist the language for the entire
session, also modify <a class="reference internal" href="../../ref/utils.html#django.utils.translation.LANGUAGE_SESSION_KEY" title="django.utils.translation.LANGUAGE_SESSION_KEY"><code class="xref py py-data docutils literal"><span class="pre">LANGUAGE_SESSION_KEY</span></code></a>
in the session:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">translation</span>
<span class="n">user_language</span> <span class="o">=</span> <span class="s1">&#39;fr&#39;</span>
<span class="n">translation</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">user_language</span><span class="p">)</span>
<span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="n">translation</span><span class="o">.</span><span class="n">LANGUAGE_SESSION_KEY</span><span class="p">]</span> <span class="o">=</span> <span class="n">user_language</span>
</pre></div>
</div>
<p>You would typically want to use both: <a class="reference internal" href="../../ref/utils.html#django.utils.translation.activate" title="django.utils.translation.activate"><code class="xref py py-func docutils literal"><span class="pre">django.utils.translation.activate()</span></code></a>
will change the language for this thread, and modifying the session makes this
preference persist in future requests.</p>
<p>If you are not using sessions, the language will persist in a cookie, whose name
is configured in <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_NAME"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_NAME</span></code></a>. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">translation</span>
<span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">http</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="n">user_language</span> <span class="o">=</span> <span class="s1">&#39;fr&#39;</span>
<span class="n">translation</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">user_language</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">HttpResponse</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_NAME</span><span class="p">,</span> <span class="n">user_language</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-using-translations-outside-views-and-templates">
<span id="using-translations-outside-views-and-templates"></span><h3>Using translations outside views and templates<a class="headerlink" href="#using-translations-outside-views-and-templates" title="Permalink to this headline">¶</a></h3>
<p>While Django provides a rich set of i18n tools for use in views and templates,
it does not restrict the usage to Django-specific code. The Django translation
mechanisms can be used to translate arbitrary texts to any language that is
supported by Django (as long as an appropriate translation catalog exists, of
course). You can load a translation catalog, activate it and translate text to
language of your choice, but remember to switch back to original language, as
activating a translation catalog is done on per-thread basis and such change
will affect code running in the same thread.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">translation</span>

<span class="k">def</span> <span class="nf">welcome_translated</span><span class="p">(</span><span class="n">language</span><span class="p">):</span>
    <span class="n">cur_language</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">translation</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">language</span><span class="p">)</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">ugettext</span><span class="p">(</span><span class="s1">&#39;welcome&#39;</span><span class="p">)</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">translation</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">cur_language</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">text</span>
</pre></div>
</div>
<p>Calling this function with the value &#8216;de&#8217; will give you <code class="docutils literal"><span class="pre">&quot;Willkommen&quot;</span></code>,
regardless of <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_CODE</span></code></a> and language set by middleware.</p>
<p>Functions of particular interest are <code class="docutils literal"><span class="pre">django.utils.translation.get_language()</span></code>
which returns the language used in the current thread,
<code class="docutils literal"><span class="pre">django.utils.translation.activate()</span></code> which activates a translation catalog
for the current thread, and <code class="docutils literal"><span class="pre">django.utils.translation.check_for_language()</span></code>
which checks if the given language is supported by Django.</p>
<p>To help write more concise code, there is also a context manager
<code class="docutils literal"><span class="pre">django.utils.translation.override()</span></code> that stores the current language on
enter and restores it on exit. With it, the above example becomes:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">translation</span>

<span class="k">def</span> <span class="nf">welcome_translated</span><span class="p">(</span><span class="n">language</span><span class="p">):</span>
    <span class="k">with</span> <span class="n">translation</span><span class="o">.</span><span class="n">override</span><span class="p">(</span><span class="n">language</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">translation</span><span class="o">.</span><span class="n">ugettext</span><span class="p">(</span><span class="s1">&#39;welcome&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-language-cookie">
<span id="language-cookie"></span><h3>Language cookie<a class="headerlink" href="#language-cookie" title="Permalink to this headline">¶</a></h3>
<p>A number of settings can be used to adjust language cookie options:</p>
<ul class="simple">
<li><a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_NAME"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_NAME</span></code></a></li>
</ul>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<ul class="simple">
<li><a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_AGE"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_AGE</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_DOMAIN"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_DOMAIN</span></code></a></li>
<li><a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_PATH"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_PATH</span></code></a></li>
</ul>
</div>
</div>
<div class="section" id="s-implementation-notes">
<span id="implementation-notes"></span><h2>Implementation notes<a class="headerlink" href="#implementation-notes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-specialties-of-django-translation">
<span id="s-specialties-of-django-i18n"></span><span id="specialties-of-django-translation"></span><span id="specialties-of-django-i18n"></span><h3>Specialties of Django translation<a class="headerlink" href="#specialties-of-django-translation" title="Permalink to this headline">¶</a></h3>
<p>Django&#8217;s translation machinery uses the standard <code class="docutils literal"><span class="pre">gettext</span></code> module that comes
with Python. If you know <code class="docutils literal"><span class="pre">gettext</span></code>, you might note these specialties in the
way Django does translation:</p>
<ul class="simple">
<li>The string domain is <code class="docutils literal"><span class="pre">django</span></code> or <code class="docutils literal"><span class="pre">djangojs</span></code>. This string domain is
used to differentiate between different programs that store their data
in a common message-file library (usually <code class="docutils literal"><span class="pre">/usr/share/locale/</span></code>). The
<code class="docutils literal"><span class="pre">django</span></code> domain is used for Python and template translation strings
and is loaded into the global translation catalogs. The <code class="docutils literal"><span class="pre">djangojs</span></code>
domain is only used for JavaScript translation catalogs to make sure
that those are as small as possible.</li>
<li>Django doesn&#8217;t use <code class="docutils literal"><span class="pre">xgettext</span></code> alone. It uses Python wrappers around
<code class="docutils literal"><span class="pre">xgettext</span></code> and <code class="docutils literal"><span class="pre">msgfmt</span></code>. This is mostly for convenience.</li>
</ul>
</div>
<div class="section" id="s-how-django-discovers-language-preference">
<span id="s-id4"></span><span id="how-django-discovers-language-preference"></span><span id="id4"></span><h3>How Django discovers language preference<a class="headerlink" href="#how-django-discovers-language-preference" title="Permalink to this headline">¶</a></h3>
<p>Once you&#8217;ve prepared your translations &#8211; or, if you just want to use the
translations that come with Django &#8211; you&#8217;ll just need to activate translation
for your app.</p>
<p>Behind the scenes, Django has a very flexible model of deciding which language
should be used &#8211; installation-wide, for a particular user, or both.</p>
<p>To set an installation-wide language preference, set <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_CODE</span></code></a>.
Django uses this language as the default translation &#8211; the final attempt if no
better matching translation is found through one of the methods employed by the
locale middleware (see below).</p>
<p>If all you want is to run Django with your native language all you need to do
is set <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_CODE</span></code></a> and make sure the corresponding <a class="reference internal" href="index.html#term-message-file"><span class="xref std std-term">message
files</span></a> and their compiled versions (<code class="docutils literal"><span class="pre">.mo</span></code>) exist.</p>
<p>If you want to let each individual user specify which language they
prefer, then you also need to use the <code class="docutils literal"><span class="pre">LocaleMiddleware</span></code>.
<code class="docutils literal"><span class="pre">LocaleMiddleware</span></code> enables language selection based on data from the request.
It customizes content for each user.</p>
<p>To use <code class="docutils literal"><span class="pre">LocaleMiddleware</span></code>, add <code class="docutils literal"><span class="pre">'django.middleware.locale.LocaleMiddleware'</span></code>
to your <a class="reference internal" href="../../ref/settings.html#std:setting-MIDDLEWARE_CLASSES"><code class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></code></a> setting. Because middleware order
matters, you should follow these guidelines:</p>
<ul class="simple">
<li>Make sure it&#8217;s one of the first middlewares installed.</li>
<li>It should come after <code class="docutils literal"><span class="pre">SessionMiddleware</span></code>, because <code class="docutils literal"><span class="pre">LocaleMiddleware</span></code>
makes use of session data. And it should come before <code class="docutils literal"><span class="pre">CommonMiddleware</span></code>
because <code class="docutils literal"><span class="pre">CommonMiddleware</span></code> needs an activated language in order
to resolve the requested URL.</li>
<li>If you use <code class="docutils literal"><span class="pre">CacheMiddleware</span></code>, put <code class="docutils literal"><span class="pre">LocaleMiddleware</span></code> after it.</li>
</ul>
<p>For example, your <a class="reference internal" href="../../ref/settings.html#std:setting-MIDDLEWARE_CLASSES"><code class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></code></a> might look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">MIDDLEWARE_CLASSES</span> <span class="o">=</span> <span class="p">(</span>
   <span class="s1">&#39;django.contrib.sessions.middleware.SessionMiddleware&#39;</span><span class="p">,</span>
   <span class="s1">&#39;django.middleware.locale.LocaleMiddleware&#39;</span><span class="p">,</span>
   <span class="s1">&#39;django.middleware.common.CommonMiddleware&#39;</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>(For more on middleware, see the <a class="reference internal" href="../http/middleware.html"><span class="doc">middleware documentation</span></a>.)</p>
<p><code class="docutils literal"><span class="pre">LocaleMiddleware</span></code> tries to determine the user&#8217;s language preference by
following this algorithm:</p>
<ul>
<li><p class="first">First, it looks for the language prefix in the requested URL.  This is
only performed when you are using the <code class="docutils literal"><span class="pre">i18n_patterns</span></code> function in your
root URLconf. See <a class="reference internal" href="#url-internationalization"><span class="std std-ref">Internationalization: in URL patterns</span></a> for more information
about the language prefix and how to internationalize URL patterns.</p>
</li>
<li><p class="first">Failing that, it looks for the <a class="reference internal" href="../../ref/utils.html#django.utils.translation.LANGUAGE_SESSION_KEY" title="django.utils.translation.LANGUAGE_SESSION_KEY"><code class="xref py py-data docutils literal"><span class="pre">LANGUAGE_SESSION_KEY</span></code></a>
key in the current user&#8217;s session.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>In previous versions, the key was named <code class="docutils literal"><span class="pre">django_language</span></code>, and the
<code class="docutils literal"><span class="pre">LANGUAGE_SESSION_KEY</span></code> constant did not exist.</p>
</div>
</li>
<li><p class="first">Failing that, it looks for a cookie.</p>
<p>The name of the cookie used is set by the <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_COOKIE_NAME"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_COOKIE_NAME</span></code></a>
setting. (The default name is <code class="docutils literal"><span class="pre">django_language</span></code>.)</p>
</li>
<li><p class="first">Failing that, it looks at the <code class="docutils literal"><span class="pre">Accept-Language</span></code> HTTP header. This
header is sent by your browser and tells the server which language(s) you
prefer, in order by priority. Django tries each language in the header
until it finds one with available translations.</p>
</li>
<li><p class="first">Failing that, it uses the global <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_CODE</span></code></a> setting.</p>
</li>
</ul>
<p id="locale-middleware-notes">Notes:</p>
<ul>
<li><p class="first">In each of these places, the language preference is expected to be in the
standard <a class="reference internal" href="index.html#term-language-code"><span class="xref std std-term">language format</span></a>, as a string. For example,
Brazilian Portuguese is <code class="docutils literal"><span class="pre">pt-br</span></code>.</p>
</li>
<li><p class="first">If a base language is available but the sublanguage specified is not,
Django uses the base language. For example, if a user specifies <code class="docutils literal"><span class="pre">de-at</span></code>
(Austrian German) but Django only has <code class="docutils literal"><span class="pre">de</span></code> available, Django uses
<code class="docutils literal"><span class="pre">de</span></code>.</p>
</li>
<li><p class="first">Only languages listed in the <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGES</span></code></a> setting can be selected.
If you want to restrict the language selection to a subset of provided
languages (because your application doesn&#8217;t provide all those languages),
set <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGES</span></code></a> to a list of languages. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">LANGUAGES</span> <span class="o">=</span> <span class="p">(</span>
  <span class="p">(</span><span class="s1">&#39;de&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;German&#39;</span><span class="p">)),</span>
  <span class="p">(</span><span class="s1">&#39;en&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;English&#39;</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This example restricts languages that are available for automatic
selection to German and English (and any sublanguage, like de-ch or
en-us).</p>
</li>
<li><p class="first">If you define a custom <a class="reference internal" href="../../ref/settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal"><span class="pre">LANGUAGES</span></code></a> setting, as explained in the
previous bullet, you can mark the language names as translation strings
&#8211; but use <a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext_lazy" title="django.utils.translation.ugettext_lazy"><code class="xref py py-func docutils literal"><span class="pre">ugettext_lazy()</span></code></a> instead of
<a class="reference internal" href="../../ref/utils.html#django.utils.translation.ugettext" title="django.utils.translation.ugettext"><code class="xref py py-func docutils literal"><span class="pre">ugettext()</span></code></a> to avoid a circular import.</p>
<p>Here&#8217;s a sample settings file:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="n">LANGUAGES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s1">&#39;de&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;German&#39;</span><span class="p">)),</span>
    <span class="p">(</span><span class="s1">&#39;en&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;English&#39;</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p>Once <code class="docutils literal"><span class="pre">LocaleMiddleware</span></code> determines the user&#8217;s preference, it makes this
preference available as <code class="docutils literal"><span class="pre">request.LANGUAGE_CODE</span></code> for each
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></code></a>. Feel free to read this value in your view
code. Here&#8217;s a simple example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">count</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">LANGUAGE_CODE</span> <span class="o">==</span> <span class="s1">&#39;de-at&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s2">&quot;You prefer to read Austrian German.&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s2">&quot;You prefer to read another language.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that, with static (middleware-less) translation, the language is in
<code class="docutils literal"><span class="pre">settings.LANGUAGE_CODE</span></code>, while with dynamic (middleware) translation, it&#8217;s
in <code class="docutils literal"><span class="pre">request.LANGUAGE_CODE</span></code>.</p>
</div>
<div class="section" id="s-how-django-discovers-translations">
<span id="s-id5"></span><span id="how-django-discovers-translations"></span><span id="id5"></span><h3>How Django discovers translations<a class="headerlink" href="#how-django-discovers-translations" title="Permalink to this headline">¶</a></h3>
<p>At runtime, Django builds an in-memory unified catalog of literals-translations.
To achieve this it looks for translations by following this algorithm regarding
the order in which it examines the different file paths to load the compiled
<a class="reference internal" href="index.html#term-message-file"><span class="xref std std-term">message files</span></a> (<code class="docutils literal"><span class="pre">.mo</span></code>) and the precedence of multiple
translations for the same literal:</p>
<ol class="arabic simple">
<li>The directories listed in <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> have the highest
precedence, with the ones appearing first having higher precedence than
the ones appearing later.</li>
<li>Then, it looks for and uses if it exists a <code class="docutils literal"><span class="pre">locale</span></code> directory in each
of the installed apps listed in <a class="reference internal" href="../../ref/settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a>.  The ones
appearing first have higher precedence than the ones appearing later.</li>
<li>Finally, the Django-provided base translation in <code class="docutils literal"><span class="pre">django/conf/locale</span></code>
is used as a fallback.</li>
</ol>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The translations for literals included in JavaScript assets are looked up
following a similar but not identical algorithm. See the
<a class="reference internal" href="#javascript-catalog-view"><span class="std std-ref">javascript_catalog view documentation</span></a> for
more details.</p>
</div>
<p>In all cases the name of the directory containing the translation is expected to
be named using <a class="reference internal" href="index.html#term-locale-name"><span class="xref std std-term">locale name</span></a> notation. E.g. <code class="docutils literal"><span class="pre">de</span></code>, <code class="docutils literal"><span class="pre">pt_BR</span></code>, <code class="docutils literal"><span class="pre">es_AR</span></code>,
etc.</p>
<p>This way, you can write applications that include their own translations, and
you can override base translations in your project. Or, you can just build
a big project out of several apps and put all translations into one big common
message file specific to the project you are composing. The choice is yours.</p>
<p>All message file repositories are structured the same way. They are:</p>
<ul class="simple">
<li>All paths listed in <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> in your settings file are
searched for <code class="docutils literal"><span class="pre">&lt;language&gt;/LC_MESSAGES/django.(po|mo)</span></code></li>
<li><code class="docutils literal"><span class="pre">$APPPATH/locale/&lt;language&gt;/LC_MESSAGES/django.(po|mo)</span></code></li>
<li><code class="docutils literal"><span class="pre">$PYTHONPATH/django/conf/locale/&lt;language&gt;/LC_MESSAGES/django.(po|mo)</span></code></li>
</ul>
<p>To create message files, you use the <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">makemessages</span></code></a>
tool. And you use <a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">compilemessages</span></code></a>
to produce the binary <code class="docutils literal"><span class="pre">.mo</span></code> files that are used by <code class="docutils literal"><span class="pre">gettext</span></code>.</p>
<p>You can also run <a class="reference internal" href="../../ref/django-admin.html#django-admin-compilemessages"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">compilemessages</span>
<span class="pre">--settings=path.to.settings</span></code></a> to make the compiler process all
the directories in your <a class="reference internal" href="../../ref/settings.html#std:setting-LOCALE_PATHS"><code class="xref std std-setting docutils literal"><span class="pre">LOCALE_PATHS</span></code></a> setting.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Translation</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#internationalization-in-python-code">Internationalization: in Python code</a><ul>
<li><a class="reference internal" href="#standard-translation">Standard translation</a></li>
<li><a class="reference internal" href="#comments-for-translators">Comments for translators</a></li>
<li><a class="reference internal" href="#marking-strings-as-no-op">Marking strings as no-op</a></li>
<li><a class="reference internal" href="#pluralization">Pluralization</a></li>
<li><a class="reference internal" href="#contextual-markers">Contextual markers</a></li>
<li><a class="reference internal" href="#lazy-translation">Lazy translation</a><ul>
<li><a class="reference internal" href="#model-fields-and-relationships-verbose-name-and-help-text-option-values">Model fields and relationships <code class="docutils literal"><span class="pre">verbose_name</span></code> and <code class="docutils literal"><span class="pre">help_text</span></code> option values</a></li>
<li><a class="reference internal" href="#model-verbose-names-values">Model verbose names values</a></li>
<li><a class="reference internal" href="#model-methods-short-description-attribute-values">Model methods <code class="docutils literal"><span class="pre">short_description</span></code> attribute values</a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-lazy-translation-objects">Working with lazy translation objects</a><ul>
<li><a class="reference internal" href="#lazy-translations-and-plural">Lazy translations and plural</a></li>
<li><a class="reference internal" href="#joining-strings-string-concat">Joining strings: string_concat()</a></li>
<li><a class="reference internal" href="#other-uses-of-lazy-in-delayed-translations">Other uses of lazy in delayed translations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#localized-names-of-languages">Localized names of languages</a></li>
</ul>
</li>
<li><a class="reference internal" href="#internationalization-in-template-code">Internationalization: in template code</a><ul>
<li><a class="reference internal" href="#trans-template-tag"><code class="docutils literal"><span class="pre">trans</span></code> template tag</a></li>
<li><a class="reference internal" href="#blocktrans-template-tag"><code class="docutils literal"><span class="pre">blocktrans</span></code> template tag</a></li>
<li><a class="reference internal" href="#string-literals-passed-to-tags-and-filters">String literals passed to tags and filters</a></li>
<li><a class="reference internal" href="#comments-for-translators-in-templates">Comments for translators in templates</a></li>
<li><a class="reference internal" href="#switching-language-in-templates">Switching language in templates</a></li>
<li><a class="reference internal" href="#other-tags">Other tags</a><ul>
<li><a class="reference internal" href="#get-available-languages"><code class="docutils literal"><span class="pre">get_available_languages</span></code></a></li>
<li><a class="reference internal" href="#get-current-language"><code class="docutils literal"><span class="pre">get_current_language</span></code></a></li>
<li><a class="reference internal" href="#get-current-language-bidi"><code class="docutils literal"><span class="pre">get_current_language_bidi</span></code></a></li>
<li><a class="reference internal" href="#get-language-info"><code class="docutils literal"><span class="pre">get_language_info</span></code></a></li>
<li><a class="reference internal" href="#get-language-info-list"><code class="docutils literal"><span class="pre">get_language_info_list</span></code></a></li>
<li><a class="reference internal" href="#template-filters">Template filters</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#internationalization-in-javascript-code">Internationalization: in JavaScript code</a><ul>
<li><a class="reference internal" href="#module-django.views.i18n">The <code class="docutils literal"><span class="pre">javascript_catalog</span></code> view</a></li>
<li><a class="reference internal" href="#using-the-javascript-translation-catalog">Using the JavaScript translation catalog</a><ul>
<li><a class="reference internal" href="#gettext"><code class="docutils literal"><span class="pre">gettext</span></code></a></li>
<li><a class="reference internal" href="#ngettext"><code class="docutils literal"><span class="pre">ngettext</span></code></a></li>
<li><a class="reference internal" href="#interpolate"><code class="docutils literal"><span class="pre">interpolate</span></code></a></li>
<li><a class="reference internal" href="#get-format"><code class="docutils literal"><span class="pre">get_format</span></code></a></li>
<li><a class="reference internal" href="#gettext-noop"><code class="docutils literal"><span class="pre">gettext_noop</span></code></a></li>
<li><a class="reference internal" href="#pgettext"><code class="docutils literal"><span class="pre">pgettext</span></code></a></li>
<li><a class="reference internal" href="#npgettext"><code class="docutils literal"><span class="pre">npgettext</span></code></a></li>
<li><a class="reference internal" href="#pluralidx"><code class="docutils literal"><span class="pre">pluralidx</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#note-on-performance">Note on performance</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-django.conf.urls.i18n">Internationalization: in URL patterns</a><ul>
<li><a class="reference internal" href="#language-prefix-in-url-patterns">Language prefix in URL patterns</a></li>
<li><a class="reference internal" href="#translating-url-patterns">Translating URL patterns</a></li>
<li><a class="reference internal" href="#reversing-in-templates">Reversing in templates</a></li>
</ul>
</li>
<li><a class="reference internal" href="#localization-how-to-create-language-files">Localization: how to create language files</a><ul>
<li><a class="reference internal" href="#message-files">Message files</a></li>
<li><a class="reference internal" href="#compiling-message-files">Compiling message files</a></li>
<li><a class="reference internal" href="#creating-message-files-from-javascript-source-code">Creating message files from JavaScript source code</a></li>
<li><a class="reference internal" href="#gettext-on-windows"><code class="docutils literal"><span class="pre">gettext</span></code> on Windows</a></li>
<li><a class="reference internal" href="#customizing-the-makemessages-command">Customizing the <code class="docutils literal"><span class="pre">makemessages</span></code> command</a></li>
</ul>
</li>
<li><a class="reference internal" href="#miscellaneous">Miscellaneous</a><ul>
<li><a class="reference internal" href="#the-set-language-redirect-view">The <code class="docutils literal"><span class="pre">set_language</span></code> redirect view</a></li>
<li><a class="reference internal" href="#explicitly-setting-the-active-language">Explicitly setting the active language</a></li>
<li><a class="reference internal" href="#using-translations-outside-views-and-templates">Using translations outside views and templates</a></li>
<li><a class="reference internal" href="#language-cookie">Language cookie</a></li>
</ul>
</li>
<li><a class="reference internal" href="#implementation-notes">Implementation notes</a><ul>
<li><a class="reference internal" href="#specialties-of-django-translation">Specialties of Django translation</a></li>
<li><a class="reference internal" href="#how-django-discovers-language-preference">How Django discovers language preference</a></li>
<li><a class="reference internal" href="#how-django-discovers-translations">How Django discovers translations</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Internationalization and localization</a></li>
    
    
      <li>Next: <a href="formatting.html">Format localization</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.8.19 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Internationalization and localization</a>
        
        <ul><li>Translation</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/i18n/translation.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Internationalization and localization">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="formatting.html" title="Format localization">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>