Debian Codegraph @

pkg/base1/test-locale.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit from "qunit-tests";
4
5 const pig_latin = {
6 "": {
7 language: "pig",
8 "plural-forms": function(n) {
9 const plural = (n != 1);
10 return plural;
11 }
12 },
13 Control: [null, "Ontrolcay"],
14 User: [null, "Useray"],
15 Waiting: [null, "Aitingway"],
16 "$0 disk is missing": [
17 "$0 disk is missing",
18 "$0 isksbay is issingmay",
19 "$0 isksbay are issingmay"
20 ],
21 "key\u0004Control": [null, "OntrolCAY"],
22 "disk-non-rotational\u0004$0 disk is missing": [
23 "disk-non-rotational\u0004$0 disk is missing",
24 "$0 isksBAY is issingMAY",
25 "$0 isksBAY are issingMAY"
26 ]
27 };
28
29 const ru = {
30 "": {
31 language: "ru",
32 "plural-forms":
33 function(n) {
34 const plural = (n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
35 return plural;
36 }
37 },
38 "$0 bit": ["$0 bits", "$0 бит", "$0 бита", "$0 бит"]
39 };
40
41 const zh_CN = {
42 "": {
43 language: "zh_CN",
44 "language-direction": "ltr",
45 "plural-forms": (n) => 0,
46 },
47 "$0 important hit": [null, "$0 次命中(含关键命中)"],
48 };
49
50 QUnit.test("public api", function (assert) {
51 assert.equal(typeof cockpit.locale, "function", "cockpit.locale is a function");
52 });
53
54 QUnit.test("gettext", function (assert) {
55 cockpit.locale(null); /* clear it */
56 cockpit.locale(pig_latin);
57 assert.equal(cockpit.language, "pig", "correct lang");
58 assert.equal(cockpit.gettext("Control"), "Ontrolcay", "returned translation");
59 assert.equal(cockpit.gettext("key", "Control"), "OntrolCAY", "with context");
60 assert.equal(cockpit.gettext("Empty"), "Empty", "english default");
61 assert.equal(cockpit.gettext("verb", "Empty"), "Empty", "english default context");
62 });
63
64 QUnit.test("underscore", function (assert) {
65 cockpit.locale(null); /* clear it */
66 cockpit.locale(pig_latin);
67 const _ = cockpit.gettext;
68 const C_ = _;
69 assert.equal(_("Control"), "Ontrolcay", "returned translation");
70 assert.equal(_("Empty"), "Empty", "english default");
71 assert.equal(C_("key", "Control"), "OntrolCAY", "with context");
72 assert.equal(C_("verb", "Empty"), "Empty", "with context");
73 });
74
75 QUnit.test("ngettext simple", function (assert) {
76 cockpit.locale(null); /* clear it */
77 cockpit.locale(pig_latin);
78 assert.equal(cockpit.ngettext("$0 disk is missing", "$0 disks are missing", 0), "$0 isksbay are issingmay", "zero things");
79 assert.equal(cockpit.ngettext("$0 disk is missing", "$0 disks are missing", 1), "$0 isksbay is issingmay", "one thing");
80 assert.equal(cockpit.ngettext("$0 disk is missing", "$0 disks are missing", 5), "$0 isksbay are issingmay", "multiple things");
81 assert.equal(cockpit.ngettext("disk-non-rotational", "$0 disk is missing", "$0 disks are missing", 0),
82 "$0 isksBAY are issingMAY", "zero things context");
83 assert.equal(cockpit.ngettext("disk-non-rotational", "$0 disk is missing", "$0 disks are missing", 1),
84 "$0 isksBAY is issingMAY", "one thing context");
85 assert.equal(cockpit.ngettext("disk-non-rotational", "$0 disk is missing", "$0 disks are missing", 5),
86 "$0 isksBAY are issingMAY", "multiple things context");
87 assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 1), "$0 byte", "default one");
88 assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 2), "$0 bytes", "default multiple");
89 assert.equal(cockpit.ngettext("memory", "$0 byte", "$0 bytes", 1), "$0 byte", "default one context");
90 assert.equal(cockpit.ngettext("memory", "$0 byte", "$0 bytes", 2), "$0 bytes", "default multiple context");
91 });
92
93 QUnit.test("ngettext complex", function (assert) {
94 cockpit.locale(null); /* clear it */
95 cockpit.locale(ru);
96 assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 0), "$0 бит", "ru, zero things");
97 assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 1), "$0 бит", "ru, one thing");
98 assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 5), "$0 бит", "ru, multiple things");
99 assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 23), "$0 бита", "ru, genitive singular");
100 assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 1), "$0 byte", "ru, default one");
101 assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 2), "$0 bytes", "ru, default multiple");
102
103 cockpit.locale(null); /* clear it */
104 cockpit.locale(zh_CN);
105 [0, 1, 2].forEach(i =>
106 assert.equal(cockpit.ngettext("$0 important hit", "$0 XXX", i), "$0 次命中(含关键命中)", `zh_CN, ${i} things`)
107 );
108 });
109
110 QUnit.test("translate document", function (assert) {
111 cockpit.locale(null);
112 cockpit.locale(pig_latin);
113
114 document.getElementById('translations').innerHTML = "<span translate id='translatable-html'>Control</span>";
115
116 cockpit.translate();
117 const t = document.getElementById('translatable-html');
118 assert.equal(t.innerHTML, "Ontrolcay", "translate element");
119 assert.equal(t.hasAttribute("translate"), false, "translate element attribute removed");
120 });
121
122 QUnit.test("translate elements", function (assert) {
123 cockpit.locale(null);
124 cockpit.locale(pig_latin);
125
126 const div1 = document.createElement('div');
127 div1.innerHTML = "<span translate id='translatable-html'>Control</span>" +
128 "<span translate translate-context='key' id='translatable-context-html'>Control</span>";
129
130 const div2 = document.createElement('div');
131 div2.setAttribute('translate', 'translate');
132 div2.innerHTML = 'User';
133
134 const div3 = document.createElement('div');
135 div3.innerHTML = "<span><i translate>Waiting</i></span>";
136
137 const t = document.getElementById('translations');
138 t.appendChild(div1);
139 t.appendChild(div2);
140 t.appendChild(div3);
141
142 cockpit.translate(div1, div2, div3);
143 const thtml = document.getElementById('translatable-html');
144 assert.equal(thtml.innerHTML, "Ontrolcay", "translate element");
145 assert.equal(thtml.hasAttribute("translate"), false, "translate element attribute removed");
146 const tconhtml = document.getElementById('translatable-context-html');
147 assert.equal(tconhtml.innerHTML, "OntrolCAY", "translate context");
148 assert.equal(tconhtml.hasAttribute("translate"), false, "translate context attribute removed");
149 });
150
151 QUnit.test("translate array", function (assert) {
152 cockpit.locale(null);
153 cockpit.locale(pig_latin);
154
155 const div1 = document.createElement('div');
156 div1.innerHTML = "<span translate id='translatable-html'>Control</span>" +
157 "<span translate translate-context='key' id='translatable-context-html'>Control</span>";
158
159 const div2 = document.createElement('div');
160 div2.setAttribute('translate', 'translate');
161 div2.innerHTML = 'User';
162
163 const div3 = document.createElement('div');
164 div3.innerHTML = "<span><i translate>Waiting</i></span>";
165
166 const t = document.getElementById('translations');
167 t.appendChild(div1);
168 t.appendChild(div2);
169 t.appendChild(div3);
170
171 cockpit.translate(document.querySelector("#translations div"));
172
173 const thtml = document.getElementById('translatable-html');
174 assert.equal(thtml.innerHTML, "Ontrolcay", "translate element");
175 assert.equal(thtml.hasAttribute("translate"), false, "translate element attribute removed");
176 const tconhtml = document.getElementById('translatable-context-html');
177 assert.equal(tconhtml.innerHTML, "OntrolCAY", "translate context");
178 assert.equal(tconhtml.hasAttribute("translate"), false, "translate context attribute removed");
179 });
180
181 QUnit.test("translate glade", function (assert) {
182 cockpit.locale(null);
183 cockpit.locale(pig_latin);
184
185 const div = document.createElement('div');
186 div.innerHTML = "<span translate='yes' id='translatable-glade'>Control</span>" +
187 "<span translate='yes' context='key' id='translatable-glade-context'>Control</span>";
188
189 document.getElementById('translations').appendChild(div);
190
191 cockpit.translate(div);
192
193 const t = document.getElementById('translatable-glade');
194 assert.equal(t.innerHTML, "Ontrolcay", "translatable element");
195 assert.equal(t.hasAttribute("translate"), false, "translate element removed");
196 const tcon = document.getElementById('translatable-glade-context');
197 assert.equal(tcon.innerHTML, "OntrolCAY", "translate context");
198 assert.equal(tcon.hasAttribute("translate"), false, "translate context attribute removed");
199 });
200
201 function init() {
202 /* Area for translate tests to play in */
203 const div = document.createElement('div');
204 div.setAttribute('id', 'translations');
205 div.setAttribute('hidden', 'hidden');
206 document.querySelector("body").appendChild(div);
207
208 /* Ready to go */
209 QUnit.start();
210 }
211
212 if (document.readyState == "loading")
213 document.addEventListener("DOMContentLoaded", init);
214 else
215 init();