okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-web: the served web chat page for 0.2.75d9f47922

main
5.1 KiB92 linesraw
1// The web page's translation mechanism (serve/web/i18n.js), run in Node with a small stub of the DOM and fetch.
2//   node serve/test_web_i18n.js      (serve/test_web_i18n.py runs it too and skips when node is missing)
3"use strict";
4const assert = require("node:assert/strict");
5const path = require("node:path");
6
7// a page element that holds data-i18n marks and keeps its attributes
8class El {
9  constructor(attrs, text = "") { this.attrs = {...attrs}; this.textContent = text; this.dataset = {}; }
10  getAttribute(k) { return k in this.attrs ? this.attrs[k] : null; }
11  setAttribute(k, v) { this.attrs[k] = String(v); }
12}
13const marks = [
14  new El({"data-i18n": "a.text"}, "Rewind to here"),
15  new El({"data-i18n-title": "a.title", title: "Light / dark"}),
16  new El({"data-i18n-label": "a.label", "aria-label": "Send"}),
17  new El({"data-i18n-placeholder": "a.hint", placeholder: "Ask anything…"}),
18];
19globalThis.document = {
20  documentElement: {lang: "en"},
21  querySelectorAll: (sel) => marks.filter((e) => sel.slice(1, -1) in e.attrs),
22};
23const catalogs = {
24  de: {"a.text": "Hierher zurückspulen", "a.title": "Hell / Dunkel", "chat.hidden": {one: "{n} Nachricht ausgeblendet", other: "{n} Nachrichten ausgeblendet"}},
25  ru: {"chat.hidden": {one: "{n} сообщение скрыто", few: "{n} сообщения скрыто", many: "{n} сообщений скрыто", other: "{n} сообщения скрыто"}},
26  ja: {"chat.hidden": {other: "{n} 件のメッセージを非表示"}},
27};
28let fetched = [];
29globalThis.fetch = async (url) => {
30  fetched.push(url);
31  const m = /web\/locales\/([\w-]+)\.json$/.exec(url);
32  if (m && catalogs[m[1]]) return {ok: true, json: async () => catalogs[m[1]]};
33  if (m && m[1] === "zh") return {ok: true, json: async () => ["not", "an", "object"]};
34  if (m && m[1] === "es") throw new Error("offline");
35  return {ok: false, json: async () => { throw new Error("404"); }};
36};
37const i18n = require(path.join(__dirname, "web", "i18n.js"));
38const {tr} = i18n;
39const plural = {one: "{n} message hidden", other: "{n} messages hidden"};
40
41(async () => {
42  // English: the text in the code, with its {placeholders}; a key with no text shows the key rather than nothing
43  assert.equal(tr("x.y", "Hello {name}", {name: "Ada"}), "Hello Ada");
44  assert.equal(tr("x.y", "Hello {name}"), "Hello {name}", "an unknown placeholder stays visible");
45  assert.equal(tr("chat.hidden", plural, {n: 1}), "1 message hidden");
46  assert.equal(tr("chat.hidden", plural, {n: 3}), "3 messages hidden");
47  assert.equal(tr("x.y", undefined), "x.y");
48  assert.equal(globalThis.tr, tr, "tr is global for app.js");
49  i18n.apply();
50  assert.equal(marks[0].textContent, "Rewind to here", "English leaves the marked text alone");
51
52  // a catalog replaces the text; a key it does not have falls back to the English text in the code
53  assert.equal(await i18n.setLanguage("de"), "de");
54  assert.equal(document.documentElement.lang, "de");
55  assert.equal(marks[0].textContent, "Hierher zurückspulen");
56  assert.equal(marks[1].attrs.title, "Hell / Dunkel");
57  assert.equal(marks[2].attrs["aria-label"], "Send", "no German text for this key: English stays");
58  assert.equal(tr("chat.hidden", plural, {n: 1}), "1 Nachricht ausgeblendet");
59  assert.equal(tr("chat.hidden", plural, {n: 2}), "2 Nachrichten ausgeblendet");
60  assert.equal(tr("only.english", "Only English"), "Only English");
61
62  // plural forms of the language: Russian has few and many, Japanese only other
63  await i18n.setLanguage("ru");
64  assert.deepEqual([1, 2, 5, 21, 22].map((n) => tr("chat.hidden", plural, {n})),
65                   ["1 сообщение скрыто", "2 сообщения скрыто", "5 сообщений скрыто", "21 сообщение скрыто", "22 сообщения скрыто"]);
66  await i18n.setLanguage("ja");
67  assert.equal(tr("chat.hidden", plural, {n: 1}), "1 件のメッセージを非表示");
68
69  // switching back restores the English text that was in the page
70  await i18n.setLanguage("de");
71  await i18n.setLanguage("en");
72  assert.equal(marks[0].textContent, "Rewind to here");
73  assert.equal(marks[1].attrs.title, "Light / dark");
74  assert.equal(marks[3].attrs.placeholder, "Ask anything…");
75  assert.equal(document.documentElement.lang, "en");
76
77  // a language that is not offered, a catalog that is not an object, one that cannot be fetched, a missing file:
78  // all stay English, and never throw
79  fetched = [];
80  assert.equal(await i18n.setLanguage("xx"), "en");
81  assert.deepEqual(fetched, [], "an unknown code is not even requested");
82  assert.equal(await i18n.setLanguage("zh"), "en");
83  assert.equal(await i18n.setLanguage("es"), "en");
84  assert.equal(await i18n.setLanguage("fr"), "en", "French is not in LANGUAGES yet");
85  assert.equal(await i18n.setLanguage("ko"), "en", "the file does not exist in this test");
86  assert.equal(tr("chat.hidden", plural, {n: 3}), "3 messages hidden");
87
88  // the list: English first, every entry [code, own name]
89  assert.equal(i18n.LANGUAGES[0][0], "en");
90  for (const [code, name] of i18n.LANGUAGES) assert.ok(/^[a-z]{2,3}(-[A-Z]{2})?$/.test(code) && name, code);
91  console.log("ok web i18n");
92})().catch((e) => { console.error(e); process.exitCode = 1; });