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
3.8 KiB61 linesraw
1import { test } from 'node:test';
2import assert from 'node:assert/strict';
3import { readFile } from 'node:fs/promises';
4import { build } from 'esbuild';
5
6// French (#3970): the catalogs are complete (the generic language tests cover keys and placeholders); these tests cover
7// what is specific to French: spacing before ? ! : ;, apostrophes, short labels staying short, and the picker entry.
8const names = [['en', 'fr'], ['fix-en', 'fix-fr'], ['tune-en', 'tune-fr'], ['tools-en', 'tools-fr']];
9const read = async name => JSON.parse(await readFile(`src/locales/${name}.json`, 'utf8'));
10const compiled = await build({ stdin: { contents: "export * from './src/lib/i18n';", resolveDir: process.cwd() }, bundle: true, write: false, format: 'esm', platform: 'node', loader: { '.json': 'json' } });
11const { tr, setTranslationLanguage, localeTag, currentLanguage, LANGUAGES, validLanguage, loadCatalog } = await import(`data:text/javascript;base64,${Buffer.from(compiled.outputFiles[0].text).toString('base64')}`);
12for (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()
13
14test('French is in the language picker under its own name', () => {
15  const entry = LANGUAGES.find(item => item.value === 'fr');
16  assert.equal(entry?.label, 'Français');
17  assert.ok(validLanguage('fr'));
18  assert.equal(LANGUAGES.length, 9);
19  setTranslationLanguage('fr');
20  assert.equal(currentLanguage(), 'fr');
21  assert.equal(localeTag(), 'fr');
22  assert.equal(tr('Settings'), 'Paramètres');
23  assert.equal(tr('Cancel'), 'Annuler');
24  assert.equal(tr('Open {0}', ['Modèles']).includes('{'), false);
25  assert.equal(tr('unrecognized user content Ω'), 'unrecognized user content Ω');
26  setTranslationLanguage('en');
27});
28
29test('French text keeps the French typography: a no-break space before ? ! : ; and a typographic apostrophe', async () => {
30  const bad = [];
31  for (const [englishName, frenchName] of names) {
32    const english = await read(englishName), french = await read(frenchName);
33    for (const [id, text] of Object.entries(french)) {
34      if (text === english[id]) continue; // kept as is: names, file names, class names
35      if (/[A-Za-zÀ-ÿ\)\]\d%] [?!;:]/.test(text) && !/https?:|\d:\d/.test(text)) bad.push(`${id}: ordinary space before punctuation: ${text}`);
36      if (/[A-Za-zÀ-ÿ\)\]\d%][?!;]/.test(text)) bad.push(`${id}: no space before ? ! or ;: ${text}`);
37      if (/[A-Za-zÀ-ÿ]'[A-Za-zÀ-ÿ{]/.test(text)) bad.push(`${id}: straight apostrophe: ${text}`);
38    }
39  }
40  assert.deepEqual(bad, []);
41});
42
43test('short labels stay short in French (buttons, tabs, chips)', async () => {
44  const english = await read('en'), french = await read('fr');
45  const long = [];
46  for (const [id, text] of Object.entries(english)) {
47    if (text.length > 10 || french[id] === text || /\{\d+\}/.test(text)) continue;
48    // a label of up to 10 letters may take up to 3 times the room, and never more than 36 characters
49    if (french[id].length > Math.max(18, text.length * 3.2) || french[id].length > 36) long.push(`${id}: ${text} -> ${french[id]} (${french[id].length})`);
50  }
51  assert.deepEqual(long, []);
52});
53
54test('no French string is still English by accident: at most 5% of the main catalog is left unchanged', async () => {
55  const english = await read('en'), french = await read('fr');
56  const same = Object.keys(english).filter(id => english[id] === french[id]);
57  assert.ok(same.length < Object.keys(english).length * 0.05, `${same.length} unchanged`);
58  // every unchanged string has no real words in it, or is a name that is spelled the same in French
59  const words = same.filter(id => english[id].split(/\s+/).length >= 4 && /\s(the|your|you|is|are|this|that|with|from|and)\s/i.test(english[id]) && !/^[-/\\]/.test(english[id]));
60  assert.deepEqual(words.map(id => `${id}: ${english[id]}`), []);
61});