BlueGit

projects-timfalken-com meteors

Files

meteors / test / i18n.test.js

i18n.test.js

i18n.test.js 4.7 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const fs = require('node:fs');
6const path = require('node:path');
7const I18n = require('../src/i18n.js');
8const Blocks = require('../src/blocks.js');
9const Planet = require('../src/planet.js');
10
11const LOCALES = ['en', 'nl', 'de', 'fr', 'ja', 'zh', 'ko', 'pl', 'ru', 'es'];
12const catalog = require('../server/lib/i18n.json');
13
14describe('i18n catalog (t1141)', () =>
15{
16 it('lists every supported locale', () =>
17 {
18 assert.deepEqual(catalog.localeIds, LOCALES);
19 for (const loc of LOCALES)
20 { assert.ok(catalog.locales[loc], loc); }
21 assert.equal(I18n.LOCALES.length, 10);
22 assert.equal(I18n.DEFAULT, 'en');
23 });
24
25 it('has the same non-empty keys in every language', () =>
26 {
27 const enKeys = Object.keys(catalog.locales.en).sort();
28 assert.ok(enKeys.length > 100, 'enough strings: ' + enKeys.length);
29 for (const loc of LOCALES)
30 {
31 const keys = Object.keys(catalog.locales[loc]).sort();
32 assert.deepEqual(keys, enKeys, loc + ' keys differ from English');
33 for (const k of keys)
34 {
35 const v = catalog.locales[loc][k];
36 assert.equal(typeof v, 'string', loc + ' ' + k);
37 assert.ok(v.length > 0, 'empty ' + loc + ' ' + k);
38 }
39 }
40 });
41
42 it('keeps English placeholders in every translation', () =>
43 {
44 const ph = /\{(\w+)\}/g;
45 const en = catalog.locales.en;
46 for (const loc of LOCALES)
47 {
48 if (loc === 'en')
49 { continue; }
50 for (const [k, ev] of Object.entries(en))
51 {
52 if (k.endsWith('Html'))
53 { continue; }
54 const enPh = new Set((ev.match(ph) || []).sort());
55 const locPh = new Set((catalog.locales[loc][k].match(ph) || []).sort());
56 assert.deepEqual([...locPh].sort(), [...enPh].sort(), k + ' in ' + loc);
57 }
58 }
59 });
60
61 it('translates every official planet and registry block', () =>
62 {
63 for (const p of Planet.officialPlanets())
64 {
65 for (const loc of LOCALES)
66 {
67 I18n.locale = loc;
68 I18n._catalog = catalog;
69 const n = I18n.planetName(p);
70 const people = I18n.peopleName(p);
71 assert.ok(n.length, 'planet name ' + p.id + ' ' + loc);
72 assert.ok(people.length, 'people ' + p.id + ' ' + loc);
73 }
74 }
75 for (const b of Blocks.all())
76 {
77 for (const loc of LOCALES)
78 {
79 I18n.locale = loc;
80 I18n._catalog = catalog;
81 const n = I18n.blockName(b.id, b.name);
82 assert.ok(n.length, 'block ' + b.id + ' ' + loc);
83 assert.notEqual(n, 'block.' + b.id);
84 }
85 }
86 });
87
88 it('does not rewrite a custom planet’s own name', () =>
89 {
90 I18n.locale = 'nl';
91 I18n._catalog = catalog;
92 const p = { id: 'c_abc', name: 'Aurora', people: 'Aurorans', custom: true };
93 assert.equal(I18n.planetName(p), 'Aurora');
94 assert.equal(I18n.peopleName(p), 'Aurorans');
95 });
96
97 it('interpolates and pluralises', () =>
98 {
99 I18n._catalog = catalog;
100 I18n.locale = 'en';
101 assert.equal(I18n.t('hud.round', { n: 3 }), 'Round 3');
102 assert.equal(I18n.plural('hud.opponent', 1), 'opponent');
103 assert.equal(I18n.plural('hud.opponent', 4), 'opponents');
104 I18n.locale = 'ru';
105 assert.equal(I18n.pluralForm(1, 'ru'), 'one');
106 assert.equal(I18n.pluralForm(2, 'ru'), 'few');
107 assert.equal(I18n.pluralForm(5, 'ru'), 'many');
108 assert.equal(I18n.pluralForm(21, 'ru'), 'one');
109 assert.equal(I18n.pluralForm(11, 'ru'), 'many');
110 I18n.locale = 'pl';
111 assert.equal(I18n.pluralForm(1, 'pl'), 'one');
112 assert.equal(I18n.pluralForm(3, 'pl'), 'few');
113 assert.equal(I18n.pluralForm(15, 'pl'), 'many');
114 I18n.locale = 'en';
115 assert.equal(I18n.place(1), '1st');
116 I18n.locale = 'nl';
117 assert.equal(I18n.place(1), '1e');
118 assert.match(I18n.t('menu.play'), /Spelen/);
119 });
120
121 it('maps known server error messages', () =>
122 {
123 I18n._catalog = catalog;
124 I18n.locale = 'en';
125 assert.equal(I18n.error('Log in om verder te gaan.'), 'Log in to continue.');
126 assert.match(I18n.error('Deze lobby is vol (maximaal 8 spelers).'), /8/);
127 assert.match(I18n.error('Je hebt het maximum van 30 planeten bereikt.'), /30/);
128 assert.equal(I18n.error('Vul een planeetnaam in.'), I18n.t('error.fillField', { field: I18n.t('error.fieldPlanet') }));
129 });
130
131 it('defaults to English and rejects unknown locales', () =>
132 {
133 assert.equal(I18n.known('en'), true);
134 assert.equal(I18n.known('xx'), false);
135 assert.equal(I18n.info('ja').flag, 'jp');
136 assert.equal(I18n.info('zh').htmlLang, 'zh-Hans');
137 assert.equal(I18n.flagUrl('en'), 'images/flags/gb.svg');
138 });
139
140 it('ships a local SVG flag for every language (no emoji)', () =>
141 {
142 const dir = path.join(__dirname, '..', 'build', 'images', 'flags');
143 for (const loc of I18n.LOCALES)
144 {
145 const file = path.join(dir, loc.flag + '.svg');
146 assert.ok(fs.existsSync(file), file);
147 const svg = fs.readFileSync(file, 'utf8');
148 assert.match(svg, /<svg/);
149 assert.doesNotMatch(svg, /[\u{1F1E6}-\u{1F1FF}]/u);
150 }
151 });
152});