Adding translations to JavaScript poses some problems:
JavaScript code doesn't have access to a
gettext
implementation.JavaScript code doesn't have access to
.po
or.mo
files; they need to be delivered by the server.The translation catalogs for JavaScript should be kept as small as possible.
Django provides an integrated solution for these problems: It passes the translations into JavaScript, so you can call gettext
, and so on, from within JavaScript.
The main solution to these problems is the django.views.i18n.javascript_catalog()
view, which sends out a JavaScript code library with functions that mimic the gettext
interface, plus an array of translation strings.
Those translation strings are taken from applications or Django core, according to what you specify in either the info_dict
or the URL. Paths listed in LOCALE_PATHS
are also included.
You hook it up like this:
from django.views.i18n import javascript_catalog ...