From 3ebe5e75419d2dd59bdbb5c9b626e9afd33d6945 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Tue, 18 Aug 2026 17:05:58 -0500 Subject: [PATCH 1/3] fix(i18n): translate route titles once locale messages are loaded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Opening a route in a new tab showed the raw i18n key (e.g. "settings.scan-settings") as the tab title. Locale namespaces load as separate chunks and nothing awaited them, so the router guard resolved the title before any messages existed. Route definitions compounded it by calling `t()` at module-eval time, baking the key into `meta.title` and leaving the guard to translate it a second time. Bootstrap now awaits a `localesReady` promise before installing the router, `meta.title` holds the plain key, and a locale watcher re-translates the current title — the stored language is applied when the app mounts, after the first navigation guard has already run. Co-Authored-By: Claude Opus 5 --- frontend/src/locales/index.ts | 61 ++++++++++++++++---------- frontend/src/main.ts | 6 ++- frontend/src/plugins/router.test.ts | 31 +++++++++++++ frontend/src/plugins/router.ts | 68 +++++++++++++++++------------ 4 files changed, 116 insertions(+), 50 deletions(-) create mode 100644 frontend/src/plugins/router.test.ts diff --git a/frontend/src/locales/index.ts b/frontend/src/locales/index.ts index 1c9013f96..d16866ef2 100644 --- a/frontend/src/locales/index.ts +++ b/frontend/src/locales/index.ts @@ -1,32 +1,16 @@ import { createI18n } from "vue-i18n"; -function loadLocales() { - const locales = import.meta.glob("./*/**/*.json"); - const messages: { - [key: string]: { [namespace: string]: Record }; - } = {}; - Object.keys(locales).forEach(async (key) => { - const matched = key.match(/\.\/([A-Za-z0-9-_]+)\/([A-Za-z0-9-_]+)\.json$/i); - if (matched && matched.length > 2) { - const locale = matched[1]; - const namespace = matched[2]; - if (!messages[locale]) { - messages[locale] = {}; - } - const localeModule = (await locales[key]()) as { - default: Record; - }; - messages[locale][namespace] = localeModule.default; - } - }); - return messages; -} +type NamespacedMessages = Record>; + +const localeModules = import.meta.glob<{ default: Record }>( + "./*/**/*.json", +); const i18n = createI18n({ legacy: false, locale: "en_US", fallbackLocale: "en_US", - messages: loadLocales(), + messages: {}, pluralRules: { cs_CZ(choice: number) { if (choice === 0) return 0; @@ -36,4 +20,37 @@ const i18n = createI18n({ }, }); +// Each namespace is its own chunk, so messages only land a tick or two after +// this module evaluates. Bootstrap awaits this before installing the router: +// the navigation guard translates the route title on the very first +// navigation, and without messages it would write the raw key to the tab. +export const localesReady = (async () => { + const messages: Record = {}; + + await Promise.all( + Object.entries(localeModules).map(async ([path, load]) => { + const matched = path.match( + /\.\/([A-Za-z0-9-_]+)\/([A-Za-z0-9-_]+)\.json$/i, + ); + if (!matched) return; + + const [, locale, namespace] = matched; + try { + const localeModule = await load(); + messages[locale] ??= {}; + messages[locale][namespace] = localeModule.default; + } catch (error) { + // A namespace that fails to load (stale chunk after a redeploy) must + // not hold up the app: the rest of the UI still translates, and the + // missing keys fall back to their key names. + console.error(`Error loading locale messages for ${path}: `, error); + } + }), + ); + + Object.entries(messages).forEach(([locale, localeMessages]) => { + i18n.global.setLocaleMessage(locale, localeMessages); + }); +})(); + export default i18n; diff --git a/frontend/src/main.ts b/frontend/src/main.ts index 9fda57c5a..2ec5bd50e 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -1,6 +1,7 @@ import { createApp } from "vue"; import App from "@/RomM.vue"; import "@/console/index.css"; +import { localesReady } from "@/locales"; import { registerPlugins } from "@/plugins"; import router from "@/plugins/router"; import storeAuth from "@/stores/auth"; @@ -61,7 +62,10 @@ async function initializeApp() { // Registrar vuetify + pinia + i18n + emitter registerPlugins(app); - await initializeData(); + // Locale messages gate the router alongside the initial data: the guard + // resolves the route title as soon as the router is installed, and with + // messages still in flight it would set the raw key as the tab title. + await Promise.all([initializeData(), localesReady]); // Route-level lazy imports fail outside vite's preload helper, so stale // chunks during navigation surface here instead of as vite:preloadError. diff --git a/frontend/src/plugins/router.test.ts b/frontend/src/plugins/router.test.ts new file mode 100644 index 000000000..edf74f206 --- /dev/null +++ b/frontend/src/plugins/router.test.ts @@ -0,0 +1,31 @@ +import { createPinia, setActivePinia } from "pinia"; +import { beforeAll, describe, expect, it } from "vitest"; +import i18n, { localesReady } from "@/locales"; +import router from "@/plugins/router"; + +// Titles that are plain strings rather than i18n keys. +const LITERAL_TITLES = new Set(["RomM", "Controller debug"]); + +describe("route titles", () => { + beforeAll(async () => { + setActivePinia(createPinia()); + await localesReady; + }); + + it("stores i18n keys that resolve against the locale messages", () => { + const titles = router + .getRoutes() + .map((route) => route.meta.title) + .filter((title): title is string => typeof title === "string") + .filter((title) => !LITERAL_TITLES.has(title)); + + expect(titles.length).toBeGreaterThan(0); + + for (const title of titles) { + // Route definitions must hold the key, not an eagerly translated + // string: messages load after the route table is built. + expect(title).toMatch(/^[a-z0-9-]+\.[a-z0-9-]+$/); + expect(i18n.global.t(title)).not.toBe(title); + } + }); +}); diff --git a/frontend/src/plugins/router.ts b/frontend/src/plugins/router.ts index 1fe6facee..f378a902d 100644 --- a/frontend/src/plugins/router.ts +++ b/frontend/src/plugins/router.ts @@ -1,8 +1,10 @@ import { storeToRefs } from "pinia"; +import { watch } from "vue"; import { createRouter, createWebHistory, type NavigationGuardWithThis, + type RouteLocationNormalized, } from "vue-router"; import i18n from "@/locales"; import { startViewTransition } from "@/plugins/transition"; @@ -81,7 +83,7 @@ const routes = [ path: "", name: ROUTES.SETUP, meta: { - title: i18n.global.t("login.setup-wizard"), + title: "login.setup-wizard", }, components: { default: () => import("@/views/Auth/Setup.vue"), @@ -101,7 +103,7 @@ const routes = [ path: "", name: ROUTES.LOGIN, meta: { - title: i18n.global.t("login.login"), + title: "login.login", }, components: { default: () => import("@/views/Auth/Login.vue"), @@ -121,7 +123,7 @@ const routes = [ path: "", name: ROUTES.RESET_PASSWORD, meta: { - title: i18n.global.t("login.reset-password"), + title: "login.reset-password", }, components: { default: () => import("@/views/Auth/ResetPassword.vue"), @@ -141,7 +143,7 @@ const routes = [ path: "", name: ROUTES.REGISTER, meta: { - title: i18n.global.t("login.register"), + title: "login.register", }, components: { default: () => import("@/views/Auth/Register.vue"), @@ -168,7 +170,7 @@ const routes = [ path: "", name: ROUTES.HOME, meta: { - title: i18n.global.t("settings.home"), + title: "settings.home", }, components: { default: () => import("@/views/Home.vue"), @@ -179,7 +181,7 @@ const routes = [ path: "search", name: ROUTES.SEARCH, meta: { - title: i18n.global.t("common.search"), + title: "common.search", }, components: { default: () => import("@/views/Gallery/Search.vue"), @@ -294,7 +296,7 @@ const routes = [ path: "scan", name: ROUTES.SCAN, meta: { - title: i18n.global.t("scan.scan"), + title: "scan.scan", bare: true, }, components: { @@ -306,7 +308,7 @@ const routes = [ path: "upload", name: ROUTES.UPLOAD, meta: { - title: i18n.global.t("common.upload-roms", "Upload ROMs"), + title: "common.upload-roms", }, components: { // v1 has no Upload view (the dialog was its only entry @@ -320,7 +322,7 @@ const routes = [ path: "activity", name: ROUTES.ACTIVITY, meta: { - title: i18n.global.t("activity.active-sessions"), + title: "activity.active-sessions", bare: true, }, components: { @@ -343,7 +345,7 @@ const routes = [ path: "user-interface", name: ROUTES.USER_INTERFACE, meta: { - title: i18n.global.t("common.user-interface"), + title: "common.user-interface", bare: true, }, components: { @@ -355,7 +357,7 @@ const routes = [ path: "library-management", name: ROUTES.LIBRARY_MANAGEMENT, meta: { - title: i18n.global.t("common.library-management"), + title: "common.library-management", bare: true, }, components: { @@ -367,7 +369,7 @@ const routes = [ path: "scan-settings", name: ROUTES.SCAN_SETTINGS, meta: { - title: i18n.global.t("settings.scan-settings"), + title: "settings.scan-settings", bare: true, }, components: { @@ -379,7 +381,7 @@ const routes = [ path: "metadata-sources", name: ROUTES.METADATA_SOURCES, meta: { - title: i18n.global.t("scan.metadata-sources"), + title: "scan.metadata-sources", bare: true, }, components: { @@ -391,7 +393,7 @@ const routes = [ path: "client-api-tokens", name: ROUTES.CLIENT_API_TOKENS, meta: { - title: i18n.global.t("settings.client-api-tokens"), + title: "settings.client-api-tokens", bare: true, }, components: { @@ -403,7 +405,7 @@ const routes = [ path: "administration", name: ROUTES.ADMINISTRATION, meta: { - title: i18n.global.t("common.administration"), + title: "common.administration", bare: true, }, components: { @@ -415,7 +417,7 @@ const routes = [ path: "server-stats", name: ROUTES.SERVER_STATS, meta: { - title: i18n.global.t("common.server-stats"), + title: "common.server-stats", bare: true, }, components: { @@ -427,7 +429,7 @@ const routes = [ path: "logs", name: ROUTES.LOGS, meta: { - title: i18n.global.t("common.logs"), + title: "common.logs", bare: true, // The log panel fills the viewport and scrolls internally // instead of growing the document — see SettingsLayout `fill`. @@ -461,7 +463,7 @@ const routes = [ // it redirects this URL home; v2 renders PlatformsIndex.vue. path: "platforms", name: ROUTES.PLATFORMS_INDEX, - meta: { title: i18n.global.t("common.platforms") }, + meta: { title: "common.platforms" }, components: { default: () => import("@/views/Home.vue"), v2: v2For(ROUTES.PLATFORMS_INDEX), @@ -470,7 +472,7 @@ const routes = [ { path: "collections", name: ROUTES.COLLECTIONS_INDEX, - meta: { title: i18n.global.t("common.collections") }, + meta: { title: "common.collections" }, components: { default: () => import("@/views/Home.vue"), v2: v2For(ROUTES.COLLECTIONS_INDEX), @@ -608,6 +610,15 @@ function checkRoutePermissions(route: string, user: User | null): boolean { ); } +// `meta.title` holds an i18n key, translated per navigation rather than when +// the route table is built — messages load asynchronously and aren't there +// yet at module-eval time. +function applyRouteTitle(route: RouteLocationNormalized) { + document.title = route.meta.title + ? i18n.global.t(route.meta.title as string) + : "RomM"; +} + router.beforeEach(async (to, _from, next) => { const heartbeat = storeHeartbeat(); const auth = storeAuth(); @@ -621,9 +632,7 @@ router.beforeEach(async (to, _from, next) => { // allows; the offline notice explains it and the connection layer // re-routes correctly once the backend answers again. if (!heartbeat.connected) { - document.title = to.meta.title - ? i18n.global.t(to.meta.title as string) - : "RomM"; + applyRouteTitle(to); return next(); } @@ -669,11 +678,7 @@ router.beforeEach(async (to, _from, next) => { return next({ name: ROUTES.NOT_FOUND }); } - if (to.meta.title) { - document.title = i18n.global.t(to.meta.title as string); - } else { - document.title = "RomM"; - } + applyRouteTitle(to); next(); } catch (error) { console.error("Navigation guard error:", error); @@ -682,6 +687,15 @@ router.beforeEach(async (to, _from, next) => { } }); +// The stored language is applied when the app mounts, after the first +// navigation has already resolved the title in the default locale. Routes +// whose view owns its title (usePageTitle) carry no `meta.title` and keep it. +watch(i18n.global.locale, () => { + if (router.currentRoute.value.meta.title) { + applyRouteTitle(router.currentRoute.value); + } +}); + router.beforeResolve(async (to, from) => { // Query/hash-only changes (same path — e.g. the v2 GameDetails `?tab=` // param) aren't a real view change. Running a view transition would From d0760f570e44f45f033228d54d9c316a82565632 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Tue, 18 Aug 2026 17:20:25 -0500 Subject: [PATCH 2/3] refactor(i18n): load locale bundles on demand and key every route title Review follow-ups: - Only the fallback and stored languages are awaited at boot; the rest load when switched to, so startup no longer blocks on every locale bundle. The locale watcher awaits the incoming language before re-applying the route title. - `settings.controller-debug` already exists, so the last literal route title becomes a key, and the root route drops its redundant "RomM" title (the guard already falls back to it). Every `meta.title` is now an i18n key, which the test no longer has to exempt. - Drop the em-dash from the new comment per the repo writing rule, and the trailing space in the locale load error. Co-Authored-By: Claude Opus 5 --- frontend/src/locales/index.ts | 107 +++++++++++++++++++--------- frontend/src/plugins/router.test.ts | 6 +- frontend/src/plugins/router.ts | 19 +++-- 3 files changed, 81 insertions(+), 51 deletions(-) diff --git a/frontend/src/locales/index.ts b/frontend/src/locales/index.ts index d16866ef2..50f35a529 100644 --- a/frontend/src/locales/index.ts +++ b/frontend/src/locales/index.ts @@ -1,15 +1,33 @@ +import { watch } from "vue"; import { createI18n } from "vue-i18n"; -type NamespacedMessages = Record>; +type LocaleModule = { default: Record }; -const localeModules = import.meta.glob<{ default: Record }>( - "./*/**/*.json", -); +const FALLBACK_LOCALE = "en_US"; + +// Written by the language selector via `useLocalStorage`, so it holds the +// raw locale name. +const STORED_LOCALE_KEY = "settings.locale"; + +const localeModules = import.meta.glob("./*/**/*.json"); + +const modulesByLocale = new Map< + string, + Map Promise> +>(); +for (const [path, load] of Object.entries(localeModules)) { + const matched = path.match(/\.\/([A-Za-z0-9-_]+)\/([A-Za-z0-9-_]+)\.json$/i); + if (!matched) continue; + + const [, locale, namespace] = matched; + if (!modulesByLocale.has(locale)) modulesByLocale.set(locale, new Map()); + modulesByLocale.get(locale)?.set(namespace, load); +} const i18n = createI18n({ legacy: false, - locale: "en_US", - fallbackLocale: "en_US", + locale: FALLBACK_LOCALE, + fallbackLocale: FALLBACK_LOCALE, messages: {}, pluralRules: { cs_CZ(choice: number) { @@ -20,37 +38,56 @@ const i18n = createI18n({ }, }); -// Each namespace is its own chunk, so messages only land a tick or two after -// this module evaluates. Bootstrap awaits this before installing the router: -// the navigation guard translates the route title on the very first -// navigation, and without messages it would write the raw key to the tab. -export const localesReady = (async () => { - const messages: Record = {}; +const pendingLocales = new Map>(); - await Promise.all( - Object.entries(localeModules).map(async ([path, load]) => { - const matched = path.match( - /\.\/([A-Za-z0-9-_]+)\/([A-Za-z0-9-_]+)\.json$/i, - ); - if (!matched) return; +// Fetches every namespace of a language and registers them as one message +// bundle. Memoized, so repeated calls (a language toggled back and forth) +// reuse the first load. +export function loadLocale(locale: string): Promise { + const pending = pendingLocales.get(locale); + if (pending) return pending; - const [, locale, namespace] = matched; - try { - const localeModule = await load(); - messages[locale] ??= {}; - messages[locale][namespace] = localeModule.default; - } catch (error) { - // A namespace that fails to load (stale chunk after a redeploy) must - // not hold up the app: the rest of the UI still translates, and the - // missing keys fall back to their key names. - console.error(`Error loading locale messages for ${path}: `, error); - } - }), - ); + const namespaces = modulesByLocale.get(locale); + if (!namespaces) return Promise.resolve(); - Object.entries(messages).forEach(([locale, localeMessages]) => { - i18n.global.setLocaleMessage(locale, localeMessages); - }); -})(); + const loading = (async () => { + const messages: Record> = {}; + + await Promise.all( + [...namespaces].map(async ([namespace, load]) => { + try { + messages[namespace] = (await load()).default; + } catch (error) { + // A namespace that fails to load (a stale chunk after a redeploy) + // must not hold up the app: the rest of the UI still translates, + // and the missing keys fall back to their key names. + console.error( + `Error loading ${locale}/${namespace} messages:`, + error, + ); + } + }), + ); + + i18n.global.setLocaleMessage(locale, messages); + })(); + + pendingLocales.set(locale, loading); + return loading; +} + +// Namespaces are separate chunks, so messages only land a tick or two after +// this module evaluates. Bootstrap awaits the two bundles the first paint can +// need before installing the router: the navigation guard translates the +// route title on the very first navigation, and without messages it would +// write the raw key to the tab. Other languages load when switched to. +export const localesReady = Promise.all([ + loadLocale(FALLBACK_LOCALE), + loadLocale(localStorage.getItem(STORED_LOCALE_KEY) ?? FALLBACK_LOCALE), +]); + +watch(i18n.global.locale, (locale) => { + void loadLocale(locale); +}); export default i18n; diff --git a/frontend/src/plugins/router.test.ts b/frontend/src/plugins/router.test.ts index edf74f206..bd6a69406 100644 --- a/frontend/src/plugins/router.test.ts +++ b/frontend/src/plugins/router.test.ts @@ -3,9 +3,6 @@ import { beforeAll, describe, expect, it } from "vitest"; import i18n, { localesReady } from "@/locales"; import router from "@/plugins/router"; -// Titles that are plain strings rather than i18n keys. -const LITERAL_TITLES = new Set(["RomM", "Controller debug"]); - describe("route titles", () => { beforeAll(async () => { setActivePinia(createPinia()); @@ -16,8 +13,7 @@ describe("route titles", () => { const titles = router .getRoutes() .map((route) => route.meta.title) - .filter((title): title is string => typeof title === "string") - .filter((title) => !LITERAL_TITLES.has(title)); + .filter((title): title is string => typeof title === "string"); expect(titles.length).toBeGreaterThan(0); diff --git a/frontend/src/plugins/router.ts b/frontend/src/plugins/router.ts index f378a902d..8ea4d0341 100644 --- a/frontend/src/plugins/router.ts +++ b/frontend/src/plugins/router.ts @@ -6,7 +6,7 @@ import { type NavigationGuardWithThis, type RouteLocationNormalized, } from "vue-router"; -import i18n from "@/locales"; +import i18n, { loadLocale } from "@/locales"; import { startViewTransition } from "@/plugins/transition"; import romApi from "@/services/api/rom"; import storeAuth from "@/stores/auth"; @@ -155,9 +155,6 @@ const routes = [ { path: "/", name: ROUTES.MAIN, - meta: { - title: "RomM", - }, // Named views let v1 and v2 coexist at the same URL. The v2 layout owns // its own so child routes with a `v2` component // render inside the v2 shell. @@ -448,7 +445,7 @@ const routes = [ // settings-adjacent tool rather than a standalone view. path: "controller-debug", name: ROUTES.CONTROLLER_DEBUG, - meta: { title: "Controller debug", bare: true }, + meta: { title: "settings.controller-debug", bare: true }, components: { // v1 has no equivalent; redirect to home if a v1 user // somehow lands here. @@ -611,8 +608,8 @@ function checkRoutePermissions(route: string, user: User | null): boolean { } // `meta.title` holds an i18n key, translated per navigation rather than when -// the route table is built — messages load asynchronously and aren't there -// yet at module-eval time. +// the route table is built. Messages load asynchronously and aren't there yet +// at module-eval time. function applyRouteTitle(route: RouteLocationNormalized) { document.title = route.meta.title ? i18n.global.t(route.meta.title as string) @@ -690,10 +687,10 @@ router.beforeEach(async (to, _from, next) => { // The stored language is applied when the app mounts, after the first // navigation has already resolved the title in the default locale. Routes // whose view owns its title (usePageTitle) carry no `meta.title` and keep it. -watch(i18n.global.locale, () => { - if (router.currentRoute.value.meta.title) { - applyRouteTitle(router.currentRoute.value); - } +watch(i18n.global.locale, async (locale) => { + await loadLocale(locale); + const route = router.currentRoute.value; + if (route.meta.title) applyRouteTitle(route); }); router.beforeResolve(async (to, from) => { From 983caaa543661c2970171edc70f07a67c0fd2bf3 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Tue, 18 Aug 2026 17:32:27 -0500 Subject: [PATCH 3/3] refactor(i18n): request global scope explicitly where the locale is set `useI18n()` already resolves to the global scope while no SFC declares an `` block, but the components that switch the app's language now say so, per the vue-i18n scope guide. An `` block added to either SFC would otherwise flip the write to component-local and silently stop the language switch from reaching the rest of the app. The v1 language selector is left alone: it writes the locale the same way, but v1 is frozen and this is a defensive change, not a bug fix. Co-Authored-By: Claude Opus 5 --- frontend/src/RomM.vue | 4 +++- frontend/src/v2/components/shared/LanguageSelector.vue | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/frontend/src/RomM.vue b/frontend/src/RomM.vue index b5cd6b37b..82f25cee2 100644 --- a/frontend/src/RomM.vue +++ b/frontend/src/RomM.vue @@ -24,7 +24,9 @@ const BackendStatusBanner = defineAsyncComponent( () => import("@/v2/components/AppShell/BackendStatusBanner.vue"), ); -const { locale } = useI18n(); +// Global scope is explicit because this write switches the whole app: +// an block in this SFC would otherwise flip it to component-local. +const { locale } = useI18n({ useScope: "global" }); const languageStore = storeLanguage(); const consoleStore = storeConsole(); const vuetifyTheme = useTheme(); diff --git a/frontend/src/v2/components/shared/LanguageSelector.vue b/frontend/src/v2/components/shared/LanguageSelector.vue index 56911ecd5..3ee23036c 100644 --- a/frontend/src/v2/components/shared/LanguageSelector.vue +++ b/frontend/src/v2/components/shared/LanguageSelector.vue @@ -21,7 +21,9 @@ interface Props { } withDefaults(defineProps(), { prefixLabel: false }); -const { t, locale } = useI18n(); +// Global scope is explicit because this write switches the whole app: +// an block in this SFC would otherwise flip it to component-local. +const { t, locale } = useI18n({ useScope: "global" }); const languageStore = storeLanguage(); const { languages, selectedLanguage } = storeToRefs(languageStore); const { locale: localeStorage } = useUISettings();