Files
gods-eye-view/src/ui/views/index.js
T
Sameh Khamis 2c72a038d5 i18n: keep the page's locale in a vue-i18n instance
The application views install one vue-i18n instance that holds the active
locale and the catalogs. src/i18n/index.js stays free of Vue: bindLocale()
keeps its locale in the instance's ref, so t(), tn() and the Intl formatters
read it from there and a view that translates while rendering renders again
when the locale changes. A tag written straight to the ref is normalized.
Message formatting, catalogs and call sites are unchanged, and switching
language still reloads the page.

The instance outlives the views: unmounting one no longer disposes it. The
browser build defines vue-i18n's feature flags and drops its message
compiler, which Core does not use.
2026-11-01 15:25:55 -08:00

68 lines
2.4 KiB
JavaScript

import { createApp } from 'vue';
import { applicationPinia } from '../vueRuntime.js';
import { applicationI18n } from '../i18n.js';
import WelcomeView from './WelcomeView.vue';
import ContextView from './ContextView.vue';
import LayerPanelsView from './LayerPanelsView.vue';
import CommandDockView from './CommandDockView.vue';
import DisplayControlsView from './DisplayControlsView.vue';
import CockpitView from './CockpitView.vue';
import SceneChromeView from './SceneChromeView.vue';
import ProviderSettingsView from './ProviderSettingsView.vue';
import IntelHudView from './IntelHudView.vue';
/**
* Application views by name. A page places each one with a host element,
* `<div class="gev-view" data-gev-view="NAME"></div>` (`display: contents`),
* where the view's markup belongs.
*/
export const APPLICATION_VIEWS = Object.freeze({
'provider-settings': ProviderSettingsView,
'scene-chrome': SceneChromeView,
cockpit: CockpitView,
'display-controls': DisplayControlsView,
'command-dock': CommandDockView,
'layer-panels': LayerPanelsView,
context: ContextView,
welcome: WelcomeView,
'intel-hud': IntelHudView,
});
const mounted = new WeakMap();
/**
* Render every application view into its host. Views belong to the page, as
* their static markup did, so they are unmounted only when a later call omits
* them; omit views before any control code is wired to them. Call before anything
* reads or translates the page; repeated calls are harmless.
* @param {Document} [doc] Page document.
* @param {{omit?: string[]}} [options] Views this page leaves out; their
* hosts are removed, unmounting a view an earlier call rendered.
*/
export function mountApplicationViews(
doc = globalThis.document,
{ omit = [] } = {},
) {
for (const name of omit) {
if (!Object.hasOwn(APPLICATION_VIEWS, name))
throw new Error(`Unknown application view: ${name}`);
}
for (const host of doc?.querySelectorAll?.('[data-gev-view]') ?? []) {
const name = host.getAttribute('data-gev-view');
if (omit.includes(name)) {
mounted.get(host)?.unmount();
mounted.delete(host);
host.remove();
continue;
}
if (mounted.has(host)) continue;
if (!Object.hasOwn(APPLICATION_VIEWS, name))
throw new Error(`Unknown application view: ${name}`);
const app = createApp(APPLICATION_VIEWS[name])
.use(applicationPinia())
.use(applicationI18n());
app.mount(host);
mounted.set(host, app);
}
}