Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,93 @@
import { readFileSync } from "node:fs";
import path from "node:path";
import i18next, { type Resource, type ResourceLanguage } from "i18next";
import { i18nConfig, i18nNamespaces, supportedLocales } from "./i18n";
import { serverLocalePath } from "./i18next.server";
/**
* Server-side bootstrap for the *global* i18next instance (the default export
* of `i18next`). A large number of components resolve translations through the
* module-level `t` helper (`import { t } from "i18next"`) rather than the
* `useTranslation()` hook. On the client that global instance is initialised in
* `entry.client.tsx`, but on the server only the per-request scoped instance was
* ever initialised. That left the global `t` returning empty strings during SSR,
* which produced empty `sr-only` labels (axe `button-name`/`link-name`
* violations) and — more damagingly — React hydration mismatches (error #418)
* that aborted hydration and broke client interactivity on authenticated pages.
*
* Initialising the global instance with every locale preloaded synchronously,
* then aligning its language with the request locale immediately before the
* (synchronous) shell render, keeps SSR output identical to the hydrated client
* output. The language is mutated per request; because the non-streaming shell
* render that follows `syncServerGlobalI18nLanguage` is synchronous, the global
* language is correct for that render.
*/
function loadAllResources(): Resource {
const resources: Resource = {};
for (const locale of supportedLocales) {
const bundle: ResourceLanguage = {};
for (const namespace of i18nNamespaces) {
try {
const file = path.join(serverLocalePath, locale, `${namespace}.json`);
bundle[namespace] = JSON.parse(readFileSync(file, "utf8")) as ResourceLanguage[string];
} catch {
// A missing namespace file is non-fatal: i18next will fall back to the
// fallback language / key for that namespace.
bundle[namespace] = {};
}
}
resources[locale] = bundle;
}
return resources;
}
function registerAllBundles(resources: Resource): void {
// Register every locale/namespace bundle. `init({ resources })` only
// guarantees the active language's bundle is added; registering them all
// up-front lets `changeLanguage` resolve any supported locale synchronously.
// This also backfills bundles when another module (e.g. test setup) already
// initialised the global instance with a narrower resource set.
for (const locale of Object.keys(resources)) {
for (const namespace of Object.keys(resources[locale]!)) {
if (!i18next.hasResourceBundle(locale, namespace)) {
i18next.addResourceBundle(locale, namespace, resources[locale]![namespace], true, true);
}
}
}
}
let initPromise: Promise<typeof i18next> | null = null;
export async function ensureServerGlobalI18n(): Promise<typeof i18next> {
if (i18next.isInitialized) {
registerAllBundles(loadAllResources());
return i18next;
}
if (!initPromise) {
const resources = loadAllResources();
initPromise = i18next
.init({
...i18nConfig,
lng: i18nConfig.fallbackLng,
preload: [...supportedLocales],
resources,
// Resources are bundled in memory, so no backend/async loading is needed.
initImmediate: false,
})
.then(() => {
registerAllBundles(resources);
return i18next;
});
}
return initPromise;
}
export async function syncServerGlobalI18nLanguage(language: string): Promise<void> {
await ensureServerGlobalI18n();
if (i18next.language !== language) {
await i18next.changeLanguage(language);
}
}