django-statici18n¶
Overview¶
When dealing with internationalization in JavaScript code, Django provides the JSONCatalog view which sends out a JavaScript code library with functions that mimic the gettext interface, plus an array of translation strings.
At first glance, it works well and everything is fine. But, because JSONCatalog view is generating JavaScript catalog dynamically on each and every request, it’s adding an overhead that can be an issue with site growth.
That’s what django-statici18n
is for:
Collecting JavaScript catalogs from each of your Django apps (and any other place you specify) into a single location that can easily be served in production.
The main website for django-statici18n
is
github.com/zyegfryed/django-statici18n where you can also file tickets.
Supported Django Versions¶
django-statici18n
works with all the Django versions officially
supported by the Django project. At this time of writing, these are the
3.2 (LTS), 4.1, 4.2 series.
Installation¶
Use your favorite Python packaging tool to install
django-statici18n
from PyPI, e.g.:pip install django-statici18n
Add
'statici18n'
to yourINSTALLED_APPS
setting:INSTALLED_APPS = [ # ... 'statici18n', ]
Once you have translated and compiled your messages, use the
compilejsi18n
management command:python manage.py compilejsi18n
Add the django.core.context_processors.i18n context processor to the
context_processors
section for your backend in theTEMPLATES
setting - it should have already been set by Django:TEMPLATES = [ { # ... 'OPTIONS': { 'context_processors': { # ... 'django.template.context_processors.i18n', }, }, }, ]
Edit your template(s) and replace the dynamically generated script by the statically generated one:
<script src="{{ STATIC_URL }}jsi18n/{{ LANGUAGE_CODE }}/djangojs.js"></script>
Note
By default, the generated catalogs are stored to STATIC_ROOT/jsi18n
.
You can modify the output path and more options by tweaking
django-statici18n
settings.
(Optional)
The following step assumes you’re using django.contrib.staticfiles.
Edit your template(s) and use the provided template tag:
{% load statici18n %} <script src="{% statici18n LANGUAGE_CODE %}"></script>
Or inline the JavaScript directly in your template:
{% load statici18n %} <script>{% inlinei18n LANGUAGE_CODE %}</script>
Documentation¶
- Management commands
- Template tags
- Settings
- Troubleshooting
- FAQ
- Changelog
- v2.5.0 (2024 Apr 20)
- v2.4.0 (2023 Jul 29)
- v2.3.1 (2022 Aug 14)
- v2.3.0 (2022 Aug 12)
- v2.2.0 (2021 Dec 27)
- v2.1.3 (2021 Dec 27)
- v2.1.2 (2021 Dec 26)
- v2.1.1 (2021 Nov 2)
- v2.1.0 (2021 May 17)
- v2.0.1 (2020 Oct 18)
- v2.0.0 (2020 Sep 18)
- v1.9.0 (2020 Jan 11)
- v1.8.3 (2019 Mar 03)
- v1.8.2 (2018 Jun 29)
- v1.8.1 (2018 Jun 29)
- v1.8.0 (2018 May 31)
- v1.7.0 (2018 Feb 11)
- v1.6.1 (2018 Jan 20)
- v1.6.0 (2018 Jan 12)
- v1.5.0 (2017 Dec 08)
- v1.4.0 (2017 Jun 22)
- v1.3.0 (2017 Jan 19)
- v1.2.1 (2016 Aug 20)
- v1.2.0 (2016 Aug 20)
- v1.1.5 (2015 Aug 7)
- v1.1.4 (2015 Aug 7)
- v1.1.3 (2015 Apr 19)
- v1.1.2 (2015 Mar 18)
- v1.1.1 (2014 Nov 17)
- v1.1 (2014 Jan 12)
- v1.0.1 (2013 Nov 20)
- v1.0.0 (2013 Nov 18)
- v0.4.5 (2013 Jun 13)
- v0.4.4 (2013 Jun 12)
- v0.4.3 (2013 Jun 10)
- v0.4.2 (2013 Feb 04)
- v0.4.1 (2012 Oct 17)
- v0.4.0 (2012 Apr 04)
- v0.3.1 (2012 Apr 03)
- v0.3.0 (2012 Apr 03)
- v0.2.0 (2012 Apr 02)
- v0.1.0 (2012 Apr 02)