Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/locale/i18next.server.ts
T

85 lines
3.6 KiB
TypeScript

import { createCookie } from "react-router";
import { loadRequiredWebConfig, resolveSessionCookiePolicy } from "@hivegrid/config";
import { RemixI18Next } from "remix-i18next/server";
import { catalogReadyLocales, defaultLocale, effectiveLng, i18nConfig, i18nNamespaces } from "./i18n";
import { resolveAnrede } from "./anrede.server";
import { getServerI18nResourcesSnapshot, resolveServerLocalePath, serverLocalePath } from "./i18next-resources.server";
const webConfig = loadRequiredWebConfig();
export { resolveServerLocalePath, serverLocalePath };
const isPortalRuntime = webConfig.runtimeSurface === "portal";
export const enabledLocales = webConfig.i18n.enabledLocales;
export const i18nCookie = createCookie(isPortalRuntime ? "__Host-hivegrid_portal_locale" : "_i18n", {
...(isPortalRuntime
? {
httpOnly: true,
path: "/",
sameSite: "lax" as const,
secure: true,
maxAge: 60 * 60 * 24 * 30,
}
: resolveSessionCookiePolicy({
nodeEnv: webConfig.nodeEnv,
routing: webConfig.routing,
maxAge: 60 * 60 * 24 * 30,
})),
secrets: isPortalRuntime
? webConfig.portalSessionSecret
? [webConfig.portalSessionSecret]
: []
: webConfig.sessionSecret
? [webConfig.sessionSecret]
: [],
});
export const remixI18Next = new RemixI18Next({
detection: {
// The cookie used to store user's language preference.
cookie: i18nCookie,
// The supported languages.
supportedLanguages: [...enabledLocales],
// The fallback language.
fallbackLanguage: defaultLocale,
},
// The i18next configuration used when translating
// Server-Side only messages.
i18next: {
...i18nConfig,
// Allow the formal variants ("de-formal", "en-formal") as *translation*
// languages so `getFixedT("<locale>-formal")` resolves the formal bundle
// (and falls back to the base locale for un-overridden keys). Detection
// (`detection.supportedLanguages` above) intentionally stays base-only so
// `getLocale()` — which drives `<html lang>` — never returns a non-BCP47
// "-formal" code.
supportedLngs: [...i18nConfig.supportedLngs, ...catalogReadyLocales.map((locale) => `${locale}-formal`)],
resources: getServerI18nResourcesSnapshot(),
},
// The plugins you want RemixI18next to use for `i18n.getFixedT` inside loaders and actions.
// E.g. The Backend plugin for loading translations from the file system.
plugins: [],
});
export async function getTranslations(request: Request, namespace = "translations", options?: { tenant?: { anrede?: string | null } | null }) {
// Load every namespace so callers can resolve cross-namespace keys like
// `t("auth:errors.invalidEmail")` while `namespace` stays the default for
// un-prefixed keys. See #72 i18n namespace split.
const namespaces: [string, ...string[]] = [namespace, ...i18nNamespaces.filter((ns) => ns !== namespace)];
const locale = await remixI18Next.getLocale(request);
// Resolve the formality axis and translate against the *effective* language.
// With the default "du" (no cookie/param, "du"/unset tenant) `effectiveLng`
// returns the plain locale, so `getFixedT(locale, …)` is equivalent to the
// previous `getFixedT(request, …)` — byte-identical output. "sie" switches to
// "<locale>-formal" and falls back to the base locale for un-overridden keys.
const anrede = await resolveAnrede(request, options?.tenant);
const lng = effectiveLng(locale, anrede);
const t = await remixI18Next.getFixedT(lng, namespaces);
let translations: any = {};
return { t, translations, locale };
}