Files
gods-eye-view/src/sharelink.lang.test.mjs
T
Sameh Khamis b20c80e590 Switch language in place, without a reload
Choosing a language in the picker now applies it at once. The choice is
stored and ?lang is kept in step through replaceState, and the picker keeps
focus.

Code that writes translated text itself re-presents it on a switch:
onLocaleChange() lets an owner listen, removed at teardown, and status
writers hand over a function of the locale (voice cards, scene director,
SDR, draw hints, imagery split, map sources, Bhote Koshi media) so the
words are rebuilt rather than re-sent. The HUD, cockpit, radio, CCTV,
launch, awareness and credit texts follow the same hook, and
applyDocumentTranslations can be applied again without undoing text code
wrote since.

The unsaved-work registry and persistLocaleAndReload existed only to guard
the reload, so they go, with their catalog entries.
2026-11-01 16:47:39 -08:00

71 lines
2.1 KiB
JavaScript

// A copied share link must not carry the sender's ?lang override: on the
// recipient's side ?lang would beat their own stored choice.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { ShareLinkManager } from './sharelink.js';
import { resolveLocale } from './i18n/locale.js';
import { persistLocale } from './i18n/index.js';
function makeManager(href) {
globalThis.window = { location: { href, hash: new URL(href).hash } };
globalThis.history = { replaceState() {} };
const viewer = {
camera: {
changed: { addEventListener() {} },
positionCartographic: { latitude: 0, longitude: 0, height: 1000 },
heading: 0,
pitch: -Math.PI / 2,
roll: 0,
},
};
return new ShareLinkManager(viewer);
}
function installClipboard(writeText) {
Object.defineProperty(globalThis, 'navigator', {
configurable: true,
value: { clipboard: { writeText } },
});
}
test('the copied link does not carry the sender ?lang override', async () => {
let copied = null;
installClipboard(async (url) => {
copied = url;
});
const manager = makeManager('https://gev.example/?lang=ru#v=2&lat=10&lon=20');
assert.equal(await manager.copyLink({ nowMs: 1_000_000 }), true);
const link = new URL(copied);
assert.equal(link.searchParams.get('lang'), null, `copied link: ${copied}`);
const store = new Map([['gev:locale:v1', 'es']]);
const recipient = resolveLocale({
location: { search: link.search },
storage: { getItem: (k) => store.get(k) ?? null },
languages: ['en-US'],
});
assert.equal(recipient, 'es');
});
test('with storage blocked the language picker keeps the choice in ?lang', () => {
let href = 'https://gev.example/#v=2';
const location = {
get href() {
return href;
},
};
const history = {
replaceState: (_s, _t, url) => {
href = url;
},
};
const blocked = {
setItem() {
throw new Error('blocked');
},
};
persistLocale('ru', { location, history, storage: blocked });
// Storage blocked: the choice survives only in ?lang.
assert.equal(new URL(href).searchParams.get('lang'), 'ru');
});