Django Static i18n

2.7.1 · active · verified Thu Apr 16

django-statici18n is a Django application designed to compile i18n JavaScript catalogs into static files. This addresses the performance overhead associated with Django's default dynamic `JSONCatalog` view, which generates JavaScript code on each request. The library is actively maintained and supports all officially supported Django versions, currently including 4.2 LTS, 5.2 LTS, and 6.0 series.

Common errors

Warnings

Install

Imports

Quickstart

To quickly set up `django-statici18n`: 1. Add `statici18n` and `django.contrib.staticfiles` to `INSTALLED_APPS`. 2. Ensure `django.template.context_processors.i18n` is in your `TEMPLATES` context processors. 3. Define `LOCALE_PATHS` in your settings to point to your translation directories. 4. Run `python manage.py makemessages`, then `python manage.py compilemessages` to create and compile your translation files. 5. Execute `python manage.py compilejsi18n` to generate the static JavaScript catalogs. 6. Include the generated script in your templates using `{% load statici18n %}` and `{% statici18n LANGUAGE_CODE %}`.

import os

# settings.py
# ...

INSTALLED_APPS = [
    # ...
    'django.contrib.staticfiles',
    'statici18n',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # ...
                'django.template.context_processors.i18n',
            ],
        },
    },
]

# Define LOCALE_PATHS to ensure Django finds your translation files
LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale'),
]

# Optional: Configure output directory if different from default (STATIC_ROOT/jsi18n)
# STATICI18N_ROOT = os.path.join(BASE_DIR, 'static_i18n_output')
# STATICI18N_OUTPUT_DIR = 'js_catalogs'

# project/templates/base.html (or any template where JS i18n is needed)
# ...
# {% load static i18n %}
# {% load statici18n %}
# <script src="{% statici18n LANGUAGE_CODE %}"></script>
# ...

# After making changes to messages:
# python manage.py makemessages -l en
# python manage.py compilemessages
# python manage.py compilejsi18n
# python manage.py collectstatic

view raw JSON →