<!DOCTYPE html>

<html lang="en" data-content_root="../../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>django.views.i18n &#8212; Django 5.2 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=34bb78ad" />
    <link rel="stylesheet" type="text/css" href="../../../_static/default.css?v=bf4d74af" />
    <script src="../../../_static/documentation_options.js?v=6593ad68"></script>
    <script src="../../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(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>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 5.2 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">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-views-i18n">
            
  <h1>Source code for django.views.i18n</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span><span class="w"> </span><span class="nn">json</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">os</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">re</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">pathlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">Path</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">django.apps</span><span class="w"> </span><span class="kn">import</span> <span class="n">apps</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.conf</span><span class="w"> </span><span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.http</span><span class="w"> </span><span class="kn">import</span> <span class="n">HttpResponse</span><span class="p">,</span> <span class="n">HttpResponseRedirect</span><span class="p">,</span> <span class="n">JsonResponse</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">Engine</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.urls</span><span class="w"> </span><span class="kn">import</span> <span class="n">translate_url</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.formats</span><span class="w"> </span><span class="kn">import</span> <span class="n">get_format</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.http</span><span class="w"> </span><span class="kn">import</span> <span class="n">url_has_allowed_host_and_scheme</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.translation</span><span class="w"> </span><span class="kn">import</span> <span class="n">check_for_language</span><span class="p">,</span> <span class="n">get_language</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.translation.trans_real</span><span class="w"> </span><span class="kn">import</span> <span class="n">DjangoTranslation</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.views.generic</span><span class="w"> </span><span class="kn">import</span> <span class="n">View</span>

<span class="n">LANGUAGE_QUERY_PARAMETER</span> <span class="o">=</span> <span class="s2">&quot;language&quot;</span>


<span class="k">def</span><span class="w"> </span><span class="nf">builtin_template_path</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a path to a builtin template.</span>

<span class="sd">    Avoid calling this function at the module level or in a class-definition</span>
<span class="sd">    because __file__ may not exist, e.g. in frozen environments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="s2">&quot;templates&quot;</span> <span class="o">/</span> <span class="n">name</span>


<div class="viewcode-block" id="set_language">
<a class="viewcode-back" href="../../../topics/i18n/translation.html#django.views.i18n.set_language">[docs]</a>
<span class="k">def</span><span class="w"> </span><span class="nf">set_language</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Redirect to a given URL while setting the chosen language in the session</span>
<span class="sd">    (if enabled) and in a cookie. The URL and the language code need to be</span>
<span class="sd">    specified in the request parameters.</span>

<span class="sd">    Since this view changes how the user will see the rest of the site, it must</span>
<span class="sd">    only be accessed as a POST request. If called as a GET request, it will</span>
<span class="sd">    redirect to the page in the request (the &#39;next&#39; parameter) without changing</span>
<span class="sd">    any state.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">next_url</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;next&quot;</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;next&quot;</span><span class="p">))</span>
    <span class="k">if</span> <span class="p">(</span>
        <span class="n">next_url</span> <span class="ow">or</span> <span class="n">request</span><span class="o">.</span><span class="n">accepts</span><span class="p">(</span><span class="s2">&quot;text/html&quot;</span><span class="p">)</span>
    <span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">url_has_allowed_host_and_scheme</span><span class="p">(</span>
        <span class="n">url</span><span class="o">=</span><span class="n">next_url</span><span class="p">,</span>
        <span class="n">allowed_hosts</span><span class="o">=</span><span class="p">{</span><span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">()},</span>
        <span class="n">require_https</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">is_secure</span><span class="p">(),</span>
    <span class="p">):</span>
        <span class="n">next_url</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;HTTP_REFERER&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">url_has_allowed_host_and_scheme</span><span class="p">(</span>
            <span class="n">url</span><span class="o">=</span><span class="n">next_url</span><span class="p">,</span>
            <span class="n">allowed_hosts</span><span class="o">=</span><span class="p">{</span><span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">()},</span>
            <span class="n">require_https</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">is_secure</span><span class="p">(),</span>
        <span class="p">):</span>
            <span class="n">next_url</span> <span class="o">=</span> <span class="s2">&quot;/&quot;</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">next_url</span><span class="p">)</span> <span class="k">if</span> <span class="n">next_url</span> <span class="k">else</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="mi">204</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s2">&quot;POST&quot;</span><span class="p">:</span>
        <span class="n">lang_code</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">LANGUAGE_QUERY_PARAMETER</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">lang_code</span> <span class="ow">and</span> <span class="n">check_for_language</span><span class="p">(</span><span class="n">lang_code</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">next_url</span><span class="p">:</span>
                <span class="n">next_trans</span> <span class="o">=</span> <span class="n">translate_url</span><span class="p">(</span><span class="n">next_url</span><span class="p">,</span> <span class="n">lang_code</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">next_trans</span> <span class="o">!=</span> <span class="n">next_url</span><span class="p">:</span>
                    <span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">next_trans</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">lang_code</span><span class="p">,</span>
                <span class="n">max_age</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_AGE</span><span class="p">,</span>
                <span class="n">path</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_PATH</span><span class="p">,</span>
                <span class="n">domain</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_DOMAIN</span><span class="p">,</span>
                <span class="n">secure</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_SECURE</span><span class="p">,</span>
                <span class="n">httponly</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_HTTPONLY</span><span class="p">,</span>
                <span class="n">samesite</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_COOKIE_SAMESITE</span><span class="p">,</span>
            <span class="p">)</span>
    <span class="k">return</span> <span class="n">response</span></div>



<span class="k">def</span><span class="w"> </span><span class="nf">get_formats</span><span class="p">():</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Return all formats strings required for i18n to work.&quot;&quot;&quot;</span>
    <span class="n">FORMAT_SETTINGS</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s2">&quot;DATE_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;DATETIME_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;TIME_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;YEAR_MONTH_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;MONTH_DAY_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;SHORT_DATE_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;SHORT_DATETIME_FORMAT&quot;</span><span class="p">,</span>
        <span class="s2">&quot;FIRST_DAY_OF_WEEK&quot;</span><span class="p">,</span>
        <span class="s2">&quot;DECIMAL_SEPARATOR&quot;</span><span class="p">,</span>
        <span class="s2">&quot;THOUSAND_SEPARATOR&quot;</span><span class="p">,</span>
        <span class="s2">&quot;NUMBER_GROUPING&quot;</span><span class="p">,</span>
        <span class="s2">&quot;DATE_INPUT_FORMATS&quot;</span><span class="p">,</span>
        <span class="s2">&quot;TIME_INPUT_FORMATS&quot;</span><span class="p">,</span>
        <span class="s2">&quot;DATETIME_INPUT_FORMATS&quot;</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="p">{</span><span class="n">attr</span><span class="p">:</span> <span class="n">get_format</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">FORMAT_SETTINGS</span><span class="p">}</span>


<div class="viewcode-block" id="JavaScriptCatalog">
<a class="viewcode-back" href="../../../topics/i18n/translation.html#django.views.i18n.JavaScriptCatalog">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">JavaScriptCatalog</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the selected language catalog as a JavaScript library.</span>

<span class="sd">    Receive the list of packages to check for translations in the `packages`</span>
<span class="sd">    kwarg either from the extra dictionary passed to the path() function or as</span>
<span class="sd">    a plus-sign delimited string from the request. Default is &#39;django.conf&#39;.</span>

<span class="sd">    You can override the gettext domain for this view, but usually you don&#39;t</span>
<span class="sd">    want to do that as JavaScript messages go to the djangojs domain. This</span>
<span class="sd">    might be needed if you deliver your JavaScript source from Django templates.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">domain</span> <span class="o">=</span> <span class="s2">&quot;djangojs&quot;</span>
    <span class="n">packages</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</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">kwargs</span><span class="p">):</span>
        <span class="n">locale</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="n">domain</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;domain&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">)</span>
        <span class="c1"># If packages are not provided, default to all installed packages, as</span>
        <span class="c1"># DjangoTranslation without localedirs harvests them all.</span>
        <span class="n">packages</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;packages&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
        <span class="n">packages</span> <span class="o">=</span> <span class="n">packages</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;+&quot;</span><span class="p">)</span> <span class="k">if</span> <span class="n">packages</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">packages</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_paths</span><span class="p">(</span><span class="n">packages</span><span class="p">)</span> <span class="k">if</span> <span class="n">packages</span> <span class="k">else</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">translation</span> <span class="o">=</span> <span class="n">DjangoTranslation</span><span class="p">(</span><span class="n">locale</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="n">domain</span><span class="p">,</span> <span class="n">localedirs</span><span class="o">=</span><span class="n">paths</span><span class="p">)</span>
        <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_to_response</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_paths</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">packages</span><span class="p">):</span>
        <span class="n">allowable_packages</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">app_config</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">app_config</span> <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">()</span>
        <span class="p">}</span>
        <span class="n">app_configs</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">allowable_packages</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">packages</span> <span class="k">if</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">allowable_packages</span>
        <span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">app_configs</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">packages</span><span class="p">):</span>
            <span class="n">excluded</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">packages</span> <span class="k">if</span> <span class="n">p</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">allowable_packages</span><span class="p">]</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Invalid package(s) provided to JavaScriptCatalog: </span><span class="si">%s</span><span class="s2">&quot;</span>
                <span class="o">%</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">excluded</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="c1"># paths of requested packages</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;locale&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">app</span> <span class="ow">in</span> <span class="n">app_configs</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">_num_plurals</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the number of plurals for this catalog language, or 2 if no</span>
<span class="sd">        plural string is available.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;nplurals=\s*(\d+)&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_plural_string</span> <span class="ow">or</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">match</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">return</span> <span class="mi">2</span>

    <span class="nd">@property</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">_plural_string</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the plural string (including nplurals) for this catalog language,</span>
<span class="sd">        or None if no plural string is available.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s2">&quot;&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">translation</span><span class="o">.</span><span class="n">_catalog</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">translation</span><span class="o">.</span><span class="n">_catalog</span><span class="p">[</span><span class="s2">&quot;&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;Plural-Forms:&quot;</span><span class="p">):</span>
                    <span class="k">return</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_plural</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">plural</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_plural_string</span>
        <span class="k">if</span> <span class="n">plural</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># This should be a compiled function of a typical plural-form:</span>
            <span class="c1"># Plural-Forms: nplurals=3; plural=n%10==1 &amp;&amp; n%100!=11 ? 0 :</span>
            <span class="c1">#               n%10&gt;=2 &amp;&amp; n%10&lt;=4 &amp;&amp; (n%100&lt;10 || n%100&gt;=20) ? 1 : 2;</span>
            <span class="n">plural</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">el</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">plural</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">el</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;plural=&quot;</span><span class="p">)</span>
            <span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;=&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">plural</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_catalog</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">pdict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">catalog</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">translation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">translation</span>
        <span class="n">seen_keys</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">translation</span><span class="o">.</span><span class="n">_catalog</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s2">&quot;&quot;</span> <span class="ow">or</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">seen_keys</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="n">catalog</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
                <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                    <span class="n">msgid</span><span class="p">,</span> <span class="n">cnt</span> <span class="o">=</span> <span class="n">key</span>
                    <span class="n">pdict</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">msgid</span><span class="p">,</span> <span class="p">{})[</span><span class="n">cnt</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                <span class="n">seen_keys</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">translation</span><span class="o">.</span><span class="n">_fallback</span><span class="p">:</span>
                <span class="n">translation</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">_fallback</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="n">num_plurals</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_num_plurals</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">pdict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">catalog</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">v</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_plurals</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">catalog</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;catalog&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_catalog</span><span class="p">(),</span>
            <span class="s2">&quot;formats&quot;</span><span class="p">:</span> <span class="n">get_formats</span><span class="p">(),</span>
            <span class="s2">&quot;plural&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_plural</span><span class="p">(),</span>
        <span class="p">}</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">render_to_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="o">**</span><span class="n">response_kwargs</span><span class="p">):</span>
        <span class="k">def</span><span class="w"> </span><span class="nf">indent</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">  &quot;</span><span class="p">)</span>

        <span class="k">with</span> <span class="n">builtin_template_path</span><span class="p">(</span><span class="s2">&quot;i18n_catalog.js&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
            <span class="n">template</span> <span class="o">=</span> <span class="n">Engine</span><span class="p">()</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">fh</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="n">context</span><span class="p">[</span><span class="s2">&quot;catalog_str&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">indent</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">context</span><span class="p">[</span><span class="s2">&quot;catalog&quot;</span><span class="p">],</span> <span class="n">sort_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">context</span><span class="p">[</span><span class="s2">&quot;catalog&quot;</span><span class="p">]</span>
            <span class="k">else</span> <span class="kc">None</span>
        <span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s2">&quot;formats_str&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">indent</span><span class="p">(</span>
            <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">context</span><span class="p">[</span><span class="s2">&quot;formats&quot;</span><span class="p">],</span> <span class="n">sort_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">2</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">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">(</span><span class="n">context</span><span class="p">)),</span> <span class="s1">&#39;text/javascript; charset=&quot;utf-8&quot;&#39;</span>
        <span class="p">)</span></div>



<div class="viewcode-block" id="JSONCatalog">
<a class="viewcode-back" href="../../../topics/i18n/translation.html#django.views.i18n.JSONCatalog">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">JSONCatalog</span><span class="p">(</span><span class="n">JavaScriptCatalog</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the selected language catalog as a JSON object.</span>

<span class="sd">    Receive the same parameters as JavaScriptCatalog and return a response</span>
<span class="sd">    with a JSON object of the following format:</span>

<span class="sd">        {</span>
<span class="sd">            &quot;catalog&quot;: {</span>
<span class="sd">                # Translations catalog</span>
<span class="sd">            },</span>
<span class="sd">            &quot;formats&quot;: {</span>
<span class="sd">                # Language formats for date, time, etc.</span>
<span class="sd">            },</span>
<span class="sd">            &quot;plural&quot;: &#39;...&#39;  # Expression for plural forms, or null.</span>
<span class="sd">        }</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">render_to_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="o">**</span><span class="n">response_kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">JsonResponse</span><span class="p">(</span><span class="n">context</span><span class="p">)</span></div>

</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Oct 01, 2025</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

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