Slide 11
Slide 11 text
enable django i18n handle js
No access to gettext implementation. So, Django passes the translations to JS through
JavaScriptCatalog view: generates libraries to mimic gettext interface.
1. Hook up this special view in URLs
from django.views.i18n import JavaScriptCatalog # New in Django 1.10
urlpatterns = [
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),]
2. Using JS catalog
document.write(gettext('this is to be translated'));
document.write(get_format('DATE_FORMAT'));
document.write(pgettext('month name', 'Feb'));
document.write(npgettext('group', 'member', 2));// members
3. A candidate for caching
from django.views.decorators.cache import cache_page
urlpatterns = [
url(r'^jsi18n/$', cache_page(86400, key_prefix='js18n-%s' %
get_version())(JavaScriptCatalog.as_view()), name='javascript-catalog'),]
Gettext Methods:
● gettext
● ngettext
● interpolate
● get_format
DATE_FORMAT
DATETIME_FORMAT
DECIMAL_SEPARATOR
FIRST_DAY_OF_WEEK
NUMBER_GROUPING
THOUSAND_SEPARATOR
TIME_FORMAT
YEAR_MONTH_FORMAT
● gettext_noop
● pgettext
● npgettext
● pluralidx