okami.mom/strata

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

Niko1221Merge f-clean-7 (045843071): the app loads screens, languages, Prism and Time-skip on first use; main chunk 3,123,528 -> 755,418 B, start screen 248.5 -> 143.4 ms (p2006 Chrome, median of 10); nothing users see changes (cleanup step 7)1631a9fde

main
2.6 KiB43 linesraw
1// The strings of the tools and images fixes (src/locales/tools-<lang>.json) exist in every language with the same slots and no empty text,
2// do not collide with the main catalog, and translate through tr().
3import test from 'node:test';
4import assert from 'node:assert/strict';
5import { readFile } from 'node:fs/promises';
6import { build } from 'esbuild';
7
8const languages = ['en', 'zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko', 'fr'];
9const catalogs = Object.fromEntries(await Promise.all(languages.map(async l => [l, JSON.parse(await readFile(`src/locales/tools-${l}.json`, 'utf8'))])));
10const main = JSON.parse(await readFile('src/locales/en.json', 'utf8'));
11const tune = JSON.parse(await readFile('src/locales/tune-en.json', 'utf8'));
12const slots = text => [...text.matchAll(/\{\d+\}/g)].map(m => m[0]).sort();
13const compiled = await build({ stdin: { contents: "export * from './src/lib/i18n';", resolveDir: process.cwd() }, bundle: true, write: false, format: 'esm', platform: 'node', loader: { '.json': 'json' } });
14const { tr, setTranslationLanguage, loadCatalog } = await import(`data:text/javascript;base64,${Buffer.from(compiled.outputFiles[0].text).toString('base64')}`);
15for (const l of ['zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko', 'fr']) await loadCatalog(l);  // the non-English catalogs load on first use (src/lib/i18n.ts loadCatalog); load them before the first tr()
16
17test('every language has every tools string with the same slots and no empty text', () => {
18  for (const l of languages) {
19    assert.deepEqual(Object.keys(catalogs[l]), Object.keys(catalogs.en), l);
20    for (const [id, source] of Object.entries(catalogs.en)) {
21      assert.ok(catalogs[l][id].trim(), `${l}:${id} is empty`);
22      assert.deepEqual(slots(catalogs[l][id]), slots(source), `${l}:${id}`);
23    }
24  }
25});
26
27test('the ids and texts do not collide with the main or Tune catalogs, and translate through tr()', () => {
28  const seen = new Set([...Object.values(main), ...Object.values(tune)]);
29  for (const [id, text] of Object.entries(catalogs.en)) {
30    assert.ok(!(id in main) && !(id in tune), id);
31    assert.ok(!seen.has(text), `already in another catalog: ${text}`);
32  }
33  const first = Object.keys(catalogs.en)[0];
34  for (const l of languages) {
35    setTranslationLanguage(l);
36    assert.equal(tr(catalogs.en[first]), catalogs[l][first], l);
37  }
38  setTranslationLanguage('de');
39  const withName = Object.entries(catalogs.en).find(([, t]) => t.includes('{0}'));
40  const shown = tr(withName[1].replace('{0}', 'files, a.b'));
41  assert.equal(shown, catalogs.de[withName[0]].replace('{0}', 'files, a.b'));
42  setTranslationLanguage('en');
43});