Commit de40cfbe authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #19567 -- Added JavaScriptCatalog and JSONCatalog class-based views

Thanks Cristiano Coelho and Tim Graham for the reviews.
parent 79a09182
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ from django.utils.text import capfirst
from django.utils.translation import ugettext as _, ugettext_lazy
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.views.i18n import JavaScriptCatalog

system_check_errors = []

@@ -316,15 +317,8 @@ class AdminSite(object):
    def i18n_javascript(self, request):
        """
        Displays the i18n JavaScript that the Django admin requires.

        This takes into account the USE_I18N setting. If it's set to False, the
        generated JavaScript will be leaner and faster.
        """
        if settings.USE_I18N:
            from django.views.i18n import javascript_catalog
        else:
            from django.views.i18n import null_javascript_catalog as javascript_catalog
        return javascript_catalog(request, packages=['django.conf', 'django.contrib.admin'])
        return JavaScriptCatalog.as_view(packages=['django.contrib.admin'])(request)

    @never_cache
    def logout(self, request, extra_context=None):
+119 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import importlib
import itertools
import json
import os
import warnings

from django import http
from django.apps import apps
@@ -10,6 +11,7 @@ from django.template import Context, Engine
from django.urls import translate_url
from django.utils import six
from django.utils._os import upath
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import smart_text
from django.utils.formats import get_format, get_format_modules
from django.utils.http import is_safe_url, urlunquote
@@ -17,6 +19,7 @@ from django.utils.translation import (
    LANGUAGE_SESSION_KEY, check_for_language, get_language, to_locale,
)
from django.utils.translation.trans_real import DjangoTranslation
from django.views.generic import View

DEFAULT_PACKAGES = ['django.conf']
LANGUAGE_QUERY_PARAMETER = 'language'
@@ -291,6 +294,10 @@ def javascript_catalog(request, domain='djangojs', packages=None):
    go to the djangojs domain. But this might be needed if you
    deliver your JavaScript source from Django templates.
    """
    warnings.warn(
        "The javascript_catalog() view is deprecated in favor of the "
        "JavaScriptCatalog view.", RemovedInDjango20Warning, stacklevel=2
    )
    locale = _get_locale(request)
    packages = _parse_packages(packages)
    catalog, plural = get_javascript_catalog(locale, domain, packages)
@@ -314,6 +321,10 @@ def json_catalog(request, domain='djangojs', packages=None):
            "plural": '...'  # Expression for plural forms, or null.
        }
    """
    warnings.warn(
        "The json_catalog() view is deprecated in favor of the "
        "JSONCatalog view.", RemovedInDjango20Warning, stacklevel=2
    )
    locale = _get_locale(request)
    packages = _parse_packages(packages)
    catalog, plural = get_javascript_catalog(locale, domain, packages)
@@ -323,3 +334,111 @@ def json_catalog(request, domain='djangojs', packages=None):
        'plural': plural,
    }
    return http.JsonResponse(data)


class JavaScriptCatalog(View):
    """
    Return the selected language catalog as a JavaScript library.

    Receives the list of packages to check for translations in the `packages`
    kwarg either from the extra dictionary passed to the url() function or as a
    plus-sign delimited string from the request. Default is 'django.conf'.

    You can override the gettext domain for this view, but usually you don't
    want to do that as JavaScript messages go to the djangojs domain. This
    might be needed if you deliver your JavaScript source from Django templates.
    """
    domain = 'djangojs'
    packages = None

    def get(self, request, *args, **kwargs):
        locale = get_language()
        domain = kwargs.get('domain', self.domain)
        # If packages are not provided, default to all installed packages, as
        # DjangoTranslation without localedirs harvests them all.
        packages = kwargs.get('packages', '').split('+') or self.packages
        paths = self.get_paths(packages) if packages else None
        self.translation = DjangoTranslation(locale, domain=domain, localedirs=paths)
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

    def get_paths(self, packages):
        allowable_packages = dict((app_config.name, app_config) for app_config in apps.get_app_configs())
        app_configs = [allowable_packages[p] for p in packages if p in allowable_packages]
        # paths of requested packages
        return [os.path.join(app.path, 'locale') for app in app_configs]

    def get_plural(self):
        plural = None
        if '' in self.translation._catalog:
            for line in self.translation._catalog[''].split('\n'):
                if line.startswith('Plural-Forms:'):
                    plural = line.split(':', 1)[1].strip()
        if plural is not None:
            # This should be a compiled function of a typical plural-form:
            # Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
            #               n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
            plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1]
        return plural

    def get_catalog(self):
        pdict = {}
        maxcnts = {}
        catalog = {}
        trans_cat = self.translation._catalog
        trans_fallback_cat = self.translation._fallback._catalog if self.translation._fallback else {}
        for key, value in itertools.chain(six.iteritems(trans_cat), six.iteritems(trans_fallback_cat)):
            if key == '' or key in catalog:
                continue
            if isinstance(key, six.string_types):
                catalog[key] = value
            elif isinstance(key, tuple):
                msgid = key[0]
                cnt = key[1]
                maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
                pdict.setdefault(msgid, {})[cnt] = value
            else:
                raise TypeError(key)
        for k, v in pdict.items():
            catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]
        return catalog

    def get_context_data(self, **kwargs):
        return {
            'catalog': self.get_catalog(),
            'formats': get_formats(),
            'plural': self.get_plural(),
        }

    def render_to_response(self, context, **response_kwargs):
        def indent(s):
            return s.replace('\n', '\n  ')

        template = Engine().from_string(js_catalog_template)
        context['catalog_str'] = indent(
            json.dumps(context['catalog'], sort_keys=True, indent=2)
        ) if context['catalog'] else None
        context['formats_str'] = indent(json.dumps(context['formats'], sort_keys=True, indent=2))

        return http.HttpResponse(template.render(Context(context)), 'text/javascript')


class JSONCatalog(JavaScriptCatalog):
    """
    Return the selected language catalog as a JSON object.

    Receives the same parameters as JavaScriptCatalog and returns a response
    with a JSON object of the following format:

        {
            "catalog": {
                # Translations catalog
            },
            "formats": {
                # Language formats for date, time, etc.
            },
            "plural": '...'  # Expression for plural forms, or null.
        }
    """
    def render_to_response(self, context, **response_kwargs):
        return http.JsonResponse(context)
+2 −0
Original line number Diff line number Diff line
@@ -156,6 +156,8 @@ details on these changes.
  ``Field.contribute_to_class()`` and ``virtual`` in
  ``Model._meta.add_field()`` will be removed.

* The ``javascript_catalog()`` and ``json_catalog()`` views will be removed.

.. _deprecation-removed-in-1.10:

1.10
+12 −0
Original line number Diff line number Diff line
@@ -277,6 +277,14 @@ Internationalization
  Content) for AJAX requests when there is no ``next`` parameter in ``POST`` or
  ``GET``.

* The :class:`~django.views.i18n.JavaScriptCatalog` and
  :class:`~django.views.i18n.JSONCatalog` class-based views supersede the
  deprecated ``javascript_catalog()`` and ``json_catalog()`` function-based
  views. The new views are almost equivalent to the old ones except that by
  default the new views collect all JavaScript strings in the ``djangojs``
  translation domain from all installed apps rather than only the JavaScript
  strings from :setting:`LOCALE_PATHS`.

Management Commands
~~~~~~~~~~~~~~~~~~~

@@ -929,6 +937,10 @@ Miscellaneous
  ``Model._meta.add_field()`` are deprecated in favor of ``private_only``
  and ``private``, respectively.

* The ``javascript_catalog()`` and ``json_catalog()`` views are deprecated in
  favor of class-based views :class:`~django.views.i18n.JavaScriptCatalog`
  and :class:`~django.views.i18n.JSONCatalog`.

.. _removed-features-1.10:

Features removed in 1.10
+114 −13
Original line number Diff line number Diff line
@@ -959,15 +959,76 @@ Django provides an integrated solution for these problems: It passes the
translations into JavaScript, so you can call ``gettext``, etc., from within
JavaScript.

The main solution to these problems is the following ``JavaScriptCatalog`` view,
which generates a JavaScript code library with functions that mimic the
``gettext`` interface, plus an array of translation strings.

.. _javascript_catalog-view:

The ``javascript_catalog`` view
-------------------------------
The ``JavaScriptCatalog`` view
------------------------------

.. module:: django.views.i18n

.. versionadded:: 1.10

.. class:: JavaScriptCatalog

    A view that produces a JavaScript code library with functions that mimic
    the ``gettext`` interface, plus an array of translation strings.

    **Attributes**

    .. attribute:: domain

        Translation domain containing strings to add in the view output.
        Defaults to ``'djangojs'``.

    .. attribute:: packages

        A list of :attr:`application names <django.apps.AppConfig.name>` among
        installed applications. Those apps should contain a ``locale``
        directory. All those catalogs plus all catalogs found in
        :setting:`LOCALE_PATHS` (which are always included) are merged into one
        catalog. Defaults to ``None``, which means that all available
        translations from all :setting:`INSTALLED_APPS` are provided in the
        JavaScript output.

    **Example with default values**::

        from django.views.i18n import JavaScriptCatalog

        urlpatterns = [
            url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
        ]

    **Example with custom packages**::

        urlpatterns = [
            url(r'^jsi18n/myapp/$',
                JavaScriptCatalog.as_view(packages=['your.app.label']),
                name='javascript-catalog'),
        ]

The precedence of translations is such that the packages appearing later in the
``packages`` argument have higher precedence than the ones appearing at the
beginning. This is important in the case of clashing translations for the same
literal.

If you use more than one ``JavaScriptCatalog`` view on a site and some of them
define the same strings, the strings in the catalog that was loaded last take
precedence.

The ``javascript_catalog`` view
-------------------------------

.. function:: javascript_catalog(request, domain='djangojs', packages=None)

.. deprecated:: 1.10

    ``javascript_catalog()`` is deprecated in favor of
    :class:`JavaScriptCatalog` and will be removed in Django 2.0.

The main solution to these problems is the
:meth:`django.views.i18n.javascript_catalog` view, which sends out a JavaScript
code library with functions that mimic the ``gettext`` interface, plus an array
@@ -1209,6 +1270,37 @@ will render a conditional expression. This will evaluate to either a ``true``

.. highlight:: python

The ``JSONCatalog`` view
------------------------

.. versionadded:: 1.10

.. class:: JSONCatalog

    In order to use another client-side library to handle translations, you may
    want to take advantage of the ``JSONCatalog`` view. It's similar to
    :class:`~django.views.i18n.JavaScriptCatalog` but returns a JSON response.

    See the documentation for :class:`~django.views.i18n.JavaScriptCatalog`
    to learn about possible values and use of the ``domain`` and ``packages``
    attributes.

    The response format is as follows:

    .. code-block:: text

        {
            "catalog": {
                # Translations catalog
            },
            "formats": {
                # Language formats for date, time, etc.
            },
            "plural": "..."  # Expression for plural forms, or null.
        }

    .. JSON doesn't allow comments so highlighting as JSON won't work here.

The ``json_catalog`` view
-------------------------

@@ -1216,6 +1308,11 @@ The ``json_catalog`` view

.. function:: json_catalog(request, domain='djangojs', packages=None)

.. deprecated:: 1.10

    ``json_catalog()`` is deprecated in favor of :class:`JSONCatalog` and will
    be removed in Django 2.0.

In order to use another client-side library to handle translations, you may
want to take advantage of the ``json_catalog()`` view. It's similar to
:meth:`~django.views.i18n.javascript_catalog` but returns a JSON response.
@@ -1260,9 +1357,9 @@ The response format is as follows:
Note on performance
-------------------

The :func:`~django.views.i18n.javascript_catalog` view generates the catalog
from ``.mo`` files on every request. Since its output is constant at least
for a given version of a site it's a good candidate for caching.
The various JavaScript/JSON i18n views generate the catalog from ``.mo`` files
on every request. Since its output is constant, at least for a given version
of a site, it's a good candidate for caching.

Server-side caching will reduce CPU load. It's easily implemented with the
:func:`~django.views.decorators.cache.cache_page` decorator. To trigger cache
@@ -1271,12 +1368,14 @@ prefix, as shown in the example below, or map the view at a version-dependent
URL::

    from django.views.decorators.cache import cache_page
    from django.views.i18n import javascript_catalog
    from django.views.i18n import JavaScriptCatalog

    # The value returned by get_version() must change when translations change.
    @cache_page(86400, key_prefix='js18n-%s' % get_version())
    def cached_javascript_catalog(request, domain='djangojs', packages=None):
        return javascript_catalog(request, domain, packages)
    urlpatterns = [
        url(r'^jsi18n/$',
            cache_page(86400, key_prefix='js18n-%s' % get_version())(JavaScriptCatalog.as_view()),
            name='javascript-catalog'),
    ]

Client-side caching will save bandwidth and make your site load faster. If
you're using ETags (:setting:`USE_ETAGS = True <USE_ETAGS>`), you're already
@@ -1286,13 +1385,15 @@ whenever you restart your application server::

    from django.utils import timezone
    from django.views.decorators.http import last_modified
    from django.views.i18n import javascript_catalog
    from django.views.i18n import JavaScriptCatalog

    last_modified_date = timezone.now()

    @last_modified(lambda req, **kw: last_modified_date)
    def cached_javascript_catalog(request, domain='djangojs', packages=None):
        return javascript_catalog(request, domain, packages)
    urlpatterns = [
        url(r'^jsi18n/$',
            last_modified(lambda req, **kw: last_modified_date)(JavaScriptCatalog.as_view()),
            name='javascript-catalog'),
    ]

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
Loading