| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const fs = require('node:fs'); |
| 6 | const path = require('node:path'); |
| 7 | const I18n = require('../src/i18n.js'); |
| 8 | const Blocks = require('../src/blocks.js'); |
| 9 | const Planet = require('../src/planet.js'); |
| 10 | |
| 11 | const LOCALES = ['en', 'nl', 'de', 'fr', 'ja', 'zh', 'ko', 'pl', 'ru', 'es']; |
| 12 | const catalog = require('../server/lib/i18n.json'); |
| 13 | |
| 14 | describe('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 | }); |
| 153 | |
| 154 | describe('i18n fallback (t1256)', () => |
| 155 | { |
| 156 | it('falls back to English when the active locale is missing a key', () => |
| 157 | { |
| 158 | I18n._catalog = { locales: { en: { 'menu.leaderboard': 'Leaderboard' }, nl: {} } }; |
| 159 | I18n.locale = 'nl'; |
| 160 | assert.equal(I18n.t('menu.leaderboard'), 'Leaderboard'); |
| 161 | assert.equal(I18n.t('menu.missing', null, 'Fallback'), 'Fallback'); |
| 162 | }); |
| 163 | |
| 164 | it('never shows a raw key when applying labels', () => |
| 165 | { |
| 166 | I18n._catalog = { locales: { en: { 'menu.play': 'Play' }, nl: {} } }; |
| 167 | I18n.locale = 'nl'; |
| 168 | |
| 169 | function el(attrs, text) |
| 170 | { |
| 171 | const a = Object.assign({}, attrs); |
| 172 | return { |
| 173 | getAttribute: (k) => (k in a ? a[k] : null), |
| 174 | setAttribute: (k, v) => { a[k] = v; }, |
| 175 | textContent: text || '', |
| 176 | innerHTML: text || '', |
| 177 | _attrs: a |
| 178 | }; |
| 179 | } |
| 180 | const label = el({ 'data-i18n': 'menu.leaderboard' }, 'Leaderboard'); |
| 181 | const play = el({ 'data-i18n': 'menu.play' }, 'Play'); |
| 182 | const root = { |
| 183 | querySelectorAll: (sel) => sel === '[data-i18n]' ? [label, play] : [] |
| 184 | }; |
| 185 | I18n.apply(root); |
| 186 | assert.equal(label.textContent, 'Leaderboard'); |
| 187 | assert.notEqual(label.textContent, 'menu.leaderboard'); |
| 188 | assert.equal(play.textContent, 'Play'); |
| 189 | }); |
| 190 | }); |
| 191 | |
| 192 | describe('auth locale default (t1250)', () => |
| 193 | { |
| 194 | it('maps BCP 47 claims, including zh-Hans, to Meteors ids', () => |
| 195 | { |
| 196 | assert.equal(I18n.normalize('zh-Hans'), 'zh'); |
| 197 | assert.equal(I18n.normalize('zh-hans'), 'zh'); |
| 198 | assert.equal(I18n.normalize('nl'), 'nl'); |
| 199 | assert.equal(I18n.normalize('en-US'), 'en'); |
| 200 | assert.equal(I18n.normalize('xx'), null); |
| 201 | assert.equal(I18n.normalize(''), null); |
| 202 | }); |
| 203 | |
| 204 | it('lets an explicit in-game choice win over the auth locale', () => |
| 205 | { |
| 206 | const mem = {}; |
| 207 | global.localStorage = { |
| 208 | getItem: (k) => (k in mem ? mem[k] : null), |
| 209 | setItem: (k, v) => { mem[k] = String(v); }, |
| 210 | removeItem: (k) => { delete mem[k]; } |
| 211 | }; |
| 212 | I18n._catalog = catalog; |
| 213 | I18n.markExplicit(false); |
| 214 | I18n.locale = 'en'; |
| 215 | I18n.applyPreferred({ authLocale: 'nl', explicit: false }); |
| 216 | assert.equal(I18n.locale, 'nl'); |
| 217 | assert.equal(I18n.hasExplicitChoice(), false); |
| 218 | assert.equal(mem[I18n.STORAGE_KEY], undefined); |
| 219 | |
| 220 | I18n.setLocale('de'); |
| 221 | assert.equal(I18n.hasExplicitChoice(), true); |
| 222 | I18n.applyPreferred({ authLocale: 'fr', locale: 'de', explicit: true }); |
| 223 | assert.equal(I18n.locale, 'de'); |
| 224 | delete global.localStorage; |
| 225 | }); |
| 226 | |
| 227 | it('leaves a guest on the stored locale', () => |
| 228 | { |
| 229 | const mem = { 'meteors-locale': 'es' }; |
| 230 | global.localStorage = { |
| 231 | getItem: (k) => (k in mem ? mem[k] : null), |
| 232 | setItem: (k, v) => { mem[k] = String(v); }, |
| 233 | removeItem: (k) => { delete mem[k]; } |
| 234 | }; |
| 235 | I18n._catalog = catalog; |
| 236 | I18n.markExplicit(false); |
| 237 | I18n.locale = I18n.readStored(); |
| 238 | assert.equal(I18n.locale, 'es'); |
| 239 | // applyPreferred is only called when logged in; a guest keeps this. |
| 240 | delete global.localStorage; |
| 241 | }); |
| 242 | }); |
projects-timfalken-com meteors