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.
This commit is contained in:
Sameh Khamis committed 2026-11-01 16:47:39 -08:00
1 parent ba20bc13d8
commit b20c80e590
87 files changed
+1194 -777

No files matched your search

+26 -42
View File
@@ -42,11 +42,10 @@ import {
import { runCctvLayerEnableTransition } from '../cctvFocusPolicy.js';
import {
describeUnsavedWork,
getLocale,
msg,
persistLocaleAndReload,
registerUnsavedWork,
onLocaleChange,
persistLocale,
setLocale,
t,
} from '../i18n/index.js';
import { applyLocaleChoice } from './localePicker.js';
@@ -721,63 +720,50 @@ export class StyleManager extends ShellFacade {
/**
* Publishes the language picker, which ProviderSettingsView renders. A
* choice reloads the page; _confirmLocaleReload asks first when that would
* lose work.
* choice switches the page's language in place and is kept for the next
* visit; the Display panel's label column is sized again for it.
* @returns {void}
*/
_initLocaleSelector() {
// Work this shell can see that a reload drops. Other modules register
// their own (registerUnsavedWork in src/i18n/index.js).
this._unsavedWorkChecks = [
registerUnsavedWork(() =>
this.services?.cctvLayer
?.getUIState?.()
?.cameras?.some((camera) => camera.calDirty)
? msg('unsaved CCTV calibration')
: null,
),
registerUnsavedWork(() =>
this.cockpitView?.active ? msg('the open cockpit') : null,
),
];
this._lifetime.own(
onLocaleChange(() => nextTick(() => this._sizeDisplayLabelColumn())),
);
// A host that does not initialize Core's locale shows no picker.
if (this._localePickerEnabled === false) return;
this._publishLocalePicker();
}
/**
* @param {{focus?: boolean}} [options] Focus the picker's button, as after
* a choice: the picker is drawn again for the new language.
*/
_publishLocalePicker({ focus = false } = {}) {
useLocaleStore(applicationPinia()).publishPicker({
current: getLocale(),
onSelect: (locale) => this._chooseLocale(locale),
focus,
});
}
/**
* Switch to `locale`: nothing for the active language, otherwise store it
* and reload once _confirmLocaleReload agrees.
* and apply it to the page.
* @param {string} locale
* @returns {'unchanged'|'declined'|'switched'}
* @returns {'unchanged'|'switched'}
*/
_chooseLocale(locale) {
return applyLocaleChoice(locale, {
current: getLocale(),
confirmSwitch: () => this._confirmLocaleReload(),
switchLocale: (next) => persistLocaleAndReload(next),
switchLocale: (next) => {
setLocale(persistLocale(next));
// Focus once, as the redrawn picker mounts; a later remount leaves
// focus where it is.
this._publishLocalePicker({ focus: true });
void nextTick(() => this._publishLocalePicker());
},
});
}
/**
* Switching language reloads the page. While that would lose work the page
* cannot restore, ask first.
* @returns {boolean} true to switch.
*/
_confirmLocaleReload() {
const lost = describeUnsavedWork();
if (!lost.length || typeof globalThis.confirm !== 'function') return true;
return globalThis.confirm(
t(
'Switching language reloads the page. This will be lost: {items}. Switch anyway?',
{ items: lost.join(', ') },
),
);
}
/**
* Sizes one Display-panel label column to the longest label in the active
* language, so slider tracks line up. Counts characters (labels are
@@ -1801,8 +1787,6 @@ export class StyleManager extends ShellFacade {
this._shareState.destroy();
this._locationNavigation.destroy();
this._lifetime.destroy();
for (const unregister of this._unsavedWorkChecks?.splice(0) ?? [])
unregister();
useLocaleStore(applicationPinia()).publishPicker(null);
this._recording.destroy();
this._mobileShell?.destroy();