Debian Codegraph @

django/views/templates/i18n_catalog.js

raw
1 {% autoescape off %}
2 'use strict';
3 {
4 const globals = this;
5 const django = globals.django || (globals.django = {});
6
7 {% if plural %}
8 django.pluralidx = function(n) {
9 const v = {{ plural }};
10 if (typeof v === 'boolean') {
11 return v ? 1 : 0;
12 } else {
13 return v;
14 }
15 };
16 {% else %}
17 django.pluralidx = function(count) { return (count == 1) ? 0 : 1; };
18 {% endif %}
19
20 /* gettext library */
21
22 django.catalog = django.catalog || {};
23 {% if catalog_str %}
24 const newcatalog = {{ catalog_str }};
25 for (const key in newcatalog) {
26 django.catalog[key] = newcatalog[key];
27 }
28 {% endif %}
29
30 if (!django.jsi18n_initialized) {
31 django.gettext = function(msgid) {
32 const value = django.catalog[msgid];
33 if (typeof value === 'undefined') {
34 return msgid;
35 } else {
36 return (typeof value === 'string') ? value : value[0];
37 }
38 };
39
40 django.ngettext = function(singular, plural, count) {
41 const value = django.catalog[singular];
42 if (typeof value === 'undefined') {
43 return (count == 1) ? singular : plural;
44 } else {
45 return value.constructor === Array ? value[django.pluralidx(count)] : value;
46 }
47 };
48
49 django.gettext_noop = function(msgid) { return msgid; };
50
51 django.pgettext = function(context, msgid) {
52 let value = django.gettext(context + '\x04' + msgid);
53 if (value.includes('\x04')) {
54 value = msgid;
55 }
56 return value;
57 };
58
59 django.npgettext = function(context, singular, plural, count) {
60 let value = django.ngettext(context + '\x04' + singular, context + '\x04' + plural, count);
61 if (value.includes('\x04')) {
62 value = django.ngettext(singular, plural, count);
63 }
64 return value;
65 };
66
67 django.interpolate = function(fmt, obj, named) {
68 if (named) {
69 return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
70 } else {
71 return fmt.replace(/%s/g, function(match){return String(obj.shift())});
72 }
73 };
74
75
76 /* formatting library */
77
78 django.formats = {{ formats_str }};
79
80 django.get_format = function(format_type) {
81 const value = django.formats[format_type];
82 if (typeof value === 'undefined') {
83 return format_type;
84 } else {
85 return value;
86 }
87 };
88
89 /* add to global namespace */
90 globals.pluralidx = django.pluralidx;
91 globals.gettext = django.gettext;
92 globals.ngettext = django.ngettext;
93 globals.gettext_noop = django.gettext_noop;
94 globals.pgettext = django.pgettext;
95 globals.npgettext = django.npgettext;
96 globals.interpolate = django.interpolate;
97 globals.get_format = django.get_format;
98
99 django.jsi18n_initialized = true;
100 }
101 };
102 {% endautoescape %}