'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const I18n = require('../src/i18n.js'); const Blocks = require('../src/blocks.js'); const Planet = require('../src/planet.js'); const LOCALES = ['en', 'nl', 'de', 'fr', 'ja', 'zh', 'ko', 'pl', 'ru', 'es']; const catalog = require('../server/lib/i18n.json'); describe('i18n catalog (t1141)', () => { it('lists every supported locale', () => { assert.deepEqual(catalog.localeIds, LOCALES); for (const loc of LOCALES) { assert.ok(catalog.locales[loc], loc); } assert.equal(I18n.LOCALES.length, 10); assert.equal(I18n.DEFAULT, 'en'); }); it('has the same non-empty keys in every language', () => { const enKeys = Object.keys(catalog.locales.en).sort(); assert.ok(enKeys.length > 100, 'enough strings: ' + enKeys.length); for (const loc of LOCALES) { const keys = Object.keys(catalog.locales[loc]).sort(); assert.deepEqual(keys, enKeys, loc + ' keys differ from English'); for (const k of keys) { const v = catalog.locales[loc][k]; assert.equal(typeof v, 'string', loc + ' ' + k); assert.ok(v.length > 0, 'empty ' + loc + ' ' + k); } } }); it('keeps English placeholders in every translation', () => { const ph = /\{(\w+)\}/g; const en = catalog.locales.en; for (const loc of LOCALES) { if (loc === 'en') { continue; } for (const [k, ev] of Object.entries(en)) { if (k.endsWith('Html')) { continue; } const enPh = new Set((ev.match(ph) || []).sort()); const locPh = new Set((catalog.locales[loc][k].match(ph) || []).sort()); assert.deepEqual([...locPh].sort(), [...enPh].sort(), k + ' in ' + loc); } } }); it('translates every official planet and registry block', () => { for (const p of Planet.officialPlanets()) { for (const loc of LOCALES) { I18n.locale = loc; I18n._catalog = catalog; const n = I18n.planetName(p); const people = I18n.peopleName(p); assert.ok(n.length, 'planet name ' + p.id + ' ' + loc); assert.ok(people.length, 'people ' + p.id + ' ' + loc); } } for (const b of Blocks.all()) { for (const loc of LOCALES) { I18n.locale = loc; I18n._catalog = catalog; const n = I18n.blockName(b.id, b.name); assert.ok(n.length, 'block ' + b.id + ' ' + loc); assert.notEqual(n, 'block.' + b.id); } } }); it('does not rewrite a custom planet’s own name', () => { I18n.locale = 'nl'; I18n._catalog = catalog; const p = { id: 'c_abc', name: 'Aurora', people: 'Aurorans', custom: true }; assert.equal(I18n.planetName(p), 'Aurora'); assert.equal(I18n.peopleName(p), 'Aurorans'); }); it('interpolates and pluralises', () => { I18n._catalog = catalog; I18n.locale = 'en'; assert.equal(I18n.t('hud.round', { n: 3 }), 'Round 3'); assert.equal(I18n.plural('hud.opponent', 1), 'opponent'); assert.equal(I18n.plural('hud.opponent', 4), 'opponents'); I18n.locale = 'ru'; assert.equal(I18n.pluralForm(1, 'ru'), 'one'); assert.equal(I18n.pluralForm(2, 'ru'), 'few'); assert.equal(I18n.pluralForm(5, 'ru'), 'many'); assert.equal(I18n.pluralForm(21, 'ru'), 'one'); assert.equal(I18n.pluralForm(11, 'ru'), 'many'); I18n.locale = 'pl'; assert.equal(I18n.pluralForm(1, 'pl'), 'one'); assert.equal(I18n.pluralForm(3, 'pl'), 'few'); assert.equal(I18n.pluralForm(15, 'pl'), 'many'); I18n.locale = 'en'; assert.equal(I18n.place(1), '1st'); I18n.locale = 'nl'; assert.equal(I18n.place(1), '1e'); assert.match(I18n.t('menu.play'), /Spelen/); }); it('maps known server error messages', () => { I18n._catalog = catalog; I18n.locale = 'en'; assert.equal(I18n.error('Log in om verder te gaan.'), 'Log in to continue.'); assert.match(I18n.error('Deze lobby is vol (maximaal 8 spelers).'), /8/); assert.match(I18n.error('Je hebt het maximum van 30 planeten bereikt.'), /30/); assert.equal(I18n.error('Vul een planeetnaam in.'), I18n.t('error.fillField', { field: I18n.t('error.fieldPlanet') })); }); it('defaults to English and rejects unknown locales', () => { assert.equal(I18n.known('en'), true); assert.equal(I18n.known('xx'), false); assert.equal(I18n.info('ja').flag, 'jp'); assert.equal(I18n.info('zh').htmlLang, 'zh-Hans'); assert.equal(I18n.flagUrl('en'), 'images/flags/gb.svg'); }); it('ships a local SVG flag for every language (no emoji)', () => { const dir = path.join(__dirname, '..', 'build', 'images', 'flags'); for (const loc of I18n.LOCALES) { const file = path.join(dir, loc.flag + '.svg'); assert.ok(fs.existsSync(file), file); const svg = fs.readFileSync(file, 'utf8'); assert.match(svg, / { it('falls back to English when the active locale is missing a key', () => { I18n._catalog = { locales: { en: { 'menu.leaderboard': 'Leaderboard' }, nl: {} } }; I18n.locale = 'nl'; assert.equal(I18n.t('menu.leaderboard'), 'Leaderboard'); assert.equal(I18n.t('menu.missing', null, 'Fallback'), 'Fallback'); }); it('never shows a raw key when applying labels', () => { I18n._catalog = { locales: { en: { 'menu.play': 'Play' }, nl: {} } }; I18n.locale = 'nl'; function el(attrs, text) { const a = Object.assign({}, attrs); return { getAttribute: (k) => (k in a ? a[k] : null), setAttribute: (k, v) => { a[k] = v; }, textContent: text || '', innerHTML: text || '', _attrs: a }; } const label = el({ 'data-i18n': 'menu.leaderboard' }, 'Leaderboard'); const play = el({ 'data-i18n': 'menu.play' }, 'Play'); const root = { querySelectorAll: (sel) => sel === '[data-i18n]' ? [label, play] : [] }; I18n.apply(root); assert.equal(label.textContent, 'Leaderboard'); assert.notEqual(label.textContent, 'menu.leaderboard'); assert.equal(play.textContent, 'Play'); }); }); describe('auth locale default (t1250)', () => { it('maps BCP 47 claims, including zh-Hans, to Meteors ids', () => { assert.equal(I18n.normalize('zh-Hans'), 'zh'); assert.equal(I18n.normalize('zh-hans'), 'zh'); assert.equal(I18n.normalize('nl'), 'nl'); assert.equal(I18n.normalize('en-US'), 'en'); assert.equal(I18n.normalize('xx'), null); assert.equal(I18n.normalize(''), null); }); it('lets an explicit in-game choice win over the auth locale', () => { const mem = {}; global.localStorage = { getItem: (k) => (k in mem ? mem[k] : null), setItem: (k, v) => { mem[k] = String(v); }, removeItem: (k) => { delete mem[k]; } }; I18n._catalog = catalog; I18n.markExplicit(false); I18n.locale = 'en'; I18n.applyPreferred({ authLocale: 'nl', explicit: false }); assert.equal(I18n.locale, 'nl'); assert.equal(I18n.hasExplicitChoice(), false); assert.equal(mem[I18n.STORAGE_KEY], undefined); I18n.setLocale('de'); assert.equal(I18n.hasExplicitChoice(), true); I18n.applyPreferred({ authLocale: 'fr', locale: 'de', explicit: true }); assert.equal(I18n.locale, 'de'); delete global.localStorage; }); it('leaves a guest on the stored locale', () => { const mem = { 'meteors-locale': 'es' }; global.localStorage = { getItem: (k) => (k in mem ? mem[k] : null), setItem: (k, v) => { mem[k] = String(v); }, removeItem: (k) => { delete mem[k]; } }; I18n._catalog = catalog; I18n.markExplicit(false); I18n.locale = I18n.readStored(); assert.equal(I18n.locale, 'es'); // applyPreferred is only called when logged in; a guest keeps this. delete global.localStorage; }); });