Files
gods-eye-view/src/ui/vueRuntime.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

50 lines
1.4 KiB
JavaScript

import { createApp } from 'vue';
import { createPinia } from 'pinia';
import { applicationI18n } from './i18n.js';
let pagePinia = null;
/**
* The page's Pinia instance, shared by the application views and the shell,
* so a view and a shell control read the same stores.
* @returns {import('pinia').Pinia}
*/
export function applicationPinia() {
pagePinia ??= createPinia();
return pagePinia;
}
/**
* Create a Vue runtime: one Pinia instance shared by every view it mounts,
* the page's vue-i18n instance, and the mounts themselves for teardown.
* @param {{pinia?: import('pinia').Pinia}} [options]
* @returns {{pinia: import('pinia').Pinia, mount: Function, destroy: Function}}
*/
export function createUiRuntime({ pinia = createPinia() } = {}) {
const apps = new Set();
return {
pinia,
/**
* Mount a root component into a host element.
* @param {object} component Root component.
* @param {Element} host Element the view renders into.
* @param {object} [props] Root props.
* @returns {Function} Unmount this view.
*/
mount(component, host, props = {}) {
const app = createApp(component, props);
app.use(pinia).use(applicationI18n());
app.mount(host);
apps.add(app);
return () => {
if (!apps.delete(app)) return;
app.unmount();
};
},
destroy() {
for (const app of apps) app.unmount();
apps.clear();
},
};
}