36 lines
1.6 KiB
TypeScript
36 lines
1.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { locale } from '../src/lib/i18n/index.ts';
|
|
import { pluginTranslation } from '../src/lib/plugins/translation.ts';
|
|
|
|
test('Plugin messages use the host language, explicit catalogs, and safe fallbacks', () => {
|
|
const api = pluginTranslation(() => {}, () => {});
|
|
try {
|
|
locale.set('fr');
|
|
assert.equal(api.locale, 'fr');
|
|
assert.equal(api.t('Settings'), 'Paramètres');
|
|
assert.equal(api.t('Column {name}', { name: '<b>User data</b>' }, { fr: { 'Column {name}': 'Colonne {name}' } }), 'Colonne <b>User data</b>');
|
|
assert.equal(api.t('Unknown {name}', { name: 'text' }), 'Unknown text');
|
|
assert.equal(api.t('constructor'), 'constructor');
|
|
assert.equal(api.t('Settings', {}, { fr: Object.create({ Settings: 'Inherited' }) }), 'Paramètres');
|
|
} finally { locale.set('en'); }
|
|
});
|
|
|
|
test('Plugin language subscriptions stop at disposal and reject access after disable', () => {
|
|
const cleanup: (() => void)[] = [], languages: string[] = [];
|
|
let stopped = false;
|
|
const api = pluginTranslation(() => { if (stopped) throw new Error('Disabled'); }, off => cleanup.push(off));
|
|
try {
|
|
api.subscribe(language => languages.push(language));
|
|
locale.set('ja');
|
|
assert.deepEqual(languages, ['en', 'ja']);
|
|
stopped = true;
|
|
for (const off of cleanup) off();
|
|
locale.set('zh-CN');
|
|
assert.deepEqual(languages, ['en', 'ja']);
|
|
assert.throws(() => api.locale, /Disabled/);
|
|
assert.throws(() => api.t('Settings'), /Disabled/);
|
|
assert.throws(() => api.subscribe(() => {}), /Disabled/);
|
|
} finally { locale.set('en'); }
|
|
});
|