Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

A Django app that provides helper for generating Javascript Underscore templates to static files with i18n support.

Project description


.. image::
:alt: Build Status

A Django app that provides helper for compiling underscore templates to static
files with i18 support.


This repo an project is forked from ``django-statici18n``
``_ to tackle the problem of compiling of Underscore templates to single static js file.
The original code was generating static js files for translations.

This app is intended to make life easier when you want to work with your Underscore templates and translate them with Django default i18n module (no js ``gettext``). If you are using Underscore templates you can have project directory like::

+- en/
+- fr/
+- underscore/
+- popup.html
+- modals/
+- include/
+- main.html

and your ``popup.html`` can look like::

{% trans "Hello" %} <% username %>

and you want to render it in something like Backbone::

PopupView = Backbone.View.extend({
template: _.template(popup_variable_name),

to do this you need to compile your .html template to be available in js with ``popup_variable_name``. Moreover if you have multiple templates, you can bundle them int single js file and serve it via CDN or nginx ommiting django.

With ``django-static-underscore-i18n`` you can do this by following. Declare dictionary mapping between html files and js variable names::

STATIC_UNDERSCORE_TEMPLATES = {'popup_variable_name': 'underscore/popup.html', ... , }

and run ``python compilejsunderscorei18n`` which will bundle your html templates into one js file for each locale supporting i18 ``{% trans %}`` tags.

.. _javascript_catalog view:
.. _adding an overhead:


1. Use your favorite Python packaging tool to install ``django-staticunderscore-i18n``
from `PyPI`_, e.g.::

pip install django-static-underscore-i18n

2. Add ``'staticunderscorei18n'`` to your ``INSTALLED_APPS`` setting::

# ...

3. Once you have edited your underscore .html templates or `translated`_ and `compiled`_ your messages, use the
``compilejsunderscorei18n`` management command::

python compilejsunderscorei18n

4. Check that the ``django.core.context_processors.i18n`` context processor is added to your
``TEMPLATE_CONTEXT_PROCESSORS`` setting - should have already been set by
Django. This is needed to resolve request.LANGUAGE_CODE variable during compilation phase::

# ...

and you should have ``FileSystemFinder`` and ``AppDirectoriesFinder`` to be available::


5. Edit your template(s) and insert .js files those were compiled. Good practice is to serve static files without django server (you can you nginx for that):

.. code-block:: html+django

<script src="{{ STATIC_URL }}jsunderscorei18n/{{ LANGUAGE_CODE }}/underscore_templates.js"></script>

.. note::

By default, the generated catalogs are stored to ``STATIC_ROOT/jsunderscorei18n``.
You can modify the output path and more options by tweaking
``django-staticunderscorei18n`` settings.

.. _PyPI:
.. _translated:
.. _compiled:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-static-underscore-i18n, version 1.9.1
Filename, size File type Python version Upload date Hashes
Filename, size django_static_underscore_i18n-1.9.1-py2.py3-none-any.whl (9.3 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size django-static-underscore-i18n-1.9.1.tar.gz (6.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page