Files
gods-eye-view/src/i18n/messageOverrides.test.mjs
T
Sameh Khamis c19d82d906 feat(i18n): an application uses Core's translator and words Core's messages its own way
The ./i18n entry exports t, tn, msg, translateKnown and the number and date
formatters. setMessageOverrides({ en: {...}, de: {...} }) shows the
application's text for any catalog key: in the active locale, else its
English, else Core's translation.
2026-11-14 17:51:03 -08:00

100 lines
3.2 KiB
JavaScript

import { test, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import {
onLocaleChange,
setLocale,
setMessageOverrides,
t,
tn,
translateKnown,
} from './index.js';
import * as page from './page.js';
afterEach(() => {
setMessageOverrides({});
setLocale('en');
});
test('a host shows its own text for a Core message, in English and each locale', () => {
setMessageOverrides({
en: { 'Download scene JSON': 'Download scene file' },
de: { 'Download scene JSON': 'Szenendatei herunterladen' },
});
assert.equal(t('Download scene JSON'), 'Download scene file');
setLocale('de');
assert.equal(t('Download scene JSON'), 'Szenendatei herunterladen');
// A locale the host did not word shows the host's English, not Core's text.
setLocale('fr');
assert.equal(t('Download scene JSON'), 'Download scene file');
// Messages it does not name stay Core's.
assert.equal(t('Share selected scene'), 'Partager la scène sélectionnée');
});
test('overrides interpolate, take contexts and plurals, and reach translateKnown', () => {
setMessageOverrides({
en: {
'Imported {file}': 'Opened {file}',
'action|CLEAR': 'WIPE',
'{count} cameras': { one: '{count} feed', other: '{count} feeds' },
},
});
assert.equal(t('Imported {file}', { file: 'a.zip' }), 'Opened a.zip');
assert.equal(t('CLEAR', { context: 'action' }), 'WIPE');
assert.equal(t('CLEAR'), 'CLEAR');
assert.equal(tn(1, '{count} camera', '{count} cameras'), '1 feed');
assert.equal(tn(3, '{count} camera', '{count} cameras'), '3 feeds');
assert.equal(translateKnown('Imported {file}'), 'Opened {file}');
// English forms follow English plural rules in any locale.
for (const locale of ['ru', 'zh-CN', 'fr']) {
setLocale(locale);
assert.equal(tn(1, '{count} camera', '{count} cameras'), '1 feed');
assert.equal(tn(21, '{count} camera', '{count} cameras'), '21 feeds');
assert.equal(tn(0, '{count} camera', '{count} cameras'), '0 feeds');
}
});
test('an entry of the wrong kind is ignored, and Core translates', () => {
setLocale('ru');
const core = {
t: t('Share selected scene'),
tn: tn(3, '{count} camera', '{count} cameras'),
};
setMessageOverrides({
en: { 'Share selected scene': {}, '{count} cameras': 'cams' },
});
assert.equal(t('Share selected scene'), core.t);
assert.equal(tn(3, '{count} camera', '{count} cameras'), core.tn);
});
test('each call replaces the overrides, words listeners again, and ignores junk', () => {
let calls = 0;
const stop = onLocaleChange(() => calls++);
setMessageOverrides({
en: { EXPORT: 'SAVE' },
xx: { EXPORT: 'no' },
de: null,
});
assert.equal(t('EXPORT'), 'SAVE');
setMessageOverrides({ en: { IMPORT: 'OPEN' } });
assert.equal(t('EXPORT'), 'EXPORT');
assert.equal(t('IMPORT'), 'OPEN');
// An empty entry counts as none.
setMessageOverrides({ en: { IMPORT: '' } });
assert.equal(t('IMPORT'), 'IMPORT');
assert.equal(calls, 3);
stop();
});
test('the package entry exports the translator and the overrides', () => {
for (const name of [
't',
'tn',
'msg',
'translateKnown',
'setMessageOverrides',
'formatNumber',
])
assert.equal(typeof page[name], 'function', name);
});