Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import { createCookie } from "react-router";
|
||||
|
||||
import { loadRequiredWebConfig, resolveSessionCookiePolicy } from "@hivegrid/config";
|
||||
import { type Anrede, defaultAnrede, isAnrede } from "@hivegrid/i18n";
|
||||
|
||||
const webConfig = loadRequiredWebConfig();
|
||||
|
||||
/**
|
||||
* Query-string override for the formality axis (`?anrede=sie`). Highest
|
||||
* priority so links / previews can force a variant for a single navigation
|
||||
* without mutating any persisted state.
|
||||
*/
|
||||
export const ANREDE_PARAM = "anrede";
|
||||
|
||||
/**
|
||||
* Persisted browser preference for du/Sie. The settings toggle writes this on
|
||||
* change; `resolveAnrede` reads it. Unsigned (the value is non-sensitive — just
|
||||
* "du"/"sie") and validated with `isAnrede`, so a tampered value degrades to the
|
||||
* default rather than selecting an unexpected language.
|
||||
*/
|
||||
export const anredeCookie = createCookie("_anrede", {
|
||||
...resolveSessionCookiePolicy({
|
||||
nodeEnv: webConfig.nodeEnv,
|
||||
routing: webConfig.routing,
|
||||
maxAge: 60 * 60 * 24 * 365,
|
||||
}),
|
||||
});
|
||||
|
||||
/**
|
||||
* Resolves the effective Anrede for a request.
|
||||
*
|
||||
* Priority (first match wins):
|
||||
* 1. explicit `?anrede=` query param
|
||||
* 2. explicit `_anrede` cookie (set by the settings toggle)
|
||||
* 3. the tenant's persisted default (`Tenant.anrede`)
|
||||
* 4. the global default ("du")
|
||||
*
|
||||
* Every source is validated through `isAnrede`, so unknown values fall through
|
||||
* to the next source. With no cookie / param and a "du" (or unset) tenant the
|
||||
* result is `defaultAnrede`, which keeps `effectiveLng` equal to the base
|
||||
* locale — i.e. byte-identical to the pre-toggle behaviour.
|
||||
*/
|
||||
export async function resolveAnrede(request: Request, tenant?: { anrede?: string | null } | null): Promise<Anrede> {
|
||||
const param = new URL(request.url).searchParams.get(ANREDE_PARAM) ?? undefined;
|
||||
if (isAnrede(param)) {
|
||||
return param;
|
||||
}
|
||||
|
||||
const cookieValue = (await anredeCookie.parse(request.headers.get("Cookie"))) as string | null;
|
||||
if (isAnrede(cookieValue ?? undefined)) {
|
||||
return cookieValue as Anrede;
|
||||
}
|
||||
|
||||
const tenantAnrede = tenant?.anrede ?? undefined;
|
||||
if (isAnrede(tenantAnrede)) {
|
||||
return tenantAnrede;
|
||||
}
|
||||
|
||||
return defaultAnrede;
|
||||
}
|
||||
@@ -1,2 +1,8 @@
|
||||
export { defaultLocale, getDirection, i18nConfig, i18nNamespaces, isRTL, isSupportedLocale, supportedLocales } from "@toolctrl/i18n";
|
||||
export type { I18nNamespace, SupportedLocale, TextDirection } from "@toolctrl/i18n";
|
||||
export { defaultLocale, getDirection, i18nConfig, i18nNamespaces, isRTL, isSupportedLocale, supportedLocales } from "@hivegrid/i18n";
|
||||
export type { I18nNamespace, SupportedLocale, TextDirection } from "@hivegrid/i18n";
|
||||
|
||||
// Formality (Anrede) axis — du/Sie. Re-exported here so app-level consumers
|
||||
// import from the local locale module consistently. Definitions live in the
|
||||
// shared (upstream-mergeable) @hivegrid/i18n package.
|
||||
export { anredeVariants, defaultAnrede, effectiveLng, formalLngs, isAnrede } from "@hivegrid/i18n";
|
||||
export type { Anrede } from "@hivegrid/i18n";
|
||||
+15
-4
@@ -3,9 +3,15 @@ import path from "node:path";
|
||||
|
||||
import i18next, { type Resource, type ResourceLanguage } from "i18next";
|
||||
|
||||
import { i18nConfig, i18nNamespaces, supportedLocales } from "./i18n";
|
||||
import { formalLngs, i18nConfig, i18nNamespaces, supportedLocales } from "./i18n";
|
||||
import { serverLocalePath } from "./i18next.server";
|
||||
|
||||
// The global instance must be able to render *and* `changeLanguage` into the
|
||||
// formal variants ("de-formal", "en-formal") in addition to the base locales.
|
||||
// Their bundles are loaded best-effort (missing files are non-fatal) and fall
|
||||
// back to the base locale for un-overridden keys.
|
||||
const globalLngs = [...supportedLocales, ...formalLngs];
|
||||
|
||||
/**
|
||||
* Server-side bootstrap for the *global* i18next instance (the default export
|
||||
* of `i18next`). A large number of components resolve translations through the
|
||||
@@ -27,7 +33,7 @@ import { serverLocalePath } from "./i18next.server";
|
||||
|
||||
function loadAllResources(): Resource {
|
||||
const resources: Resource = {};
|
||||
for (const locale of supportedLocales) {
|
||||
for (const locale of globalLngs) {
|
||||
const bundle: ResourceLanguage = {};
|
||||
for (const namespace of i18nNamespaces) {
|
||||
try {
|
||||
@@ -35,7 +41,8 @@ function loadAllResources(): Resource {
|
||||
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.
|
||||
// fallback language / key for that namespace. This is also the path the
|
||||
// formal variants take until their override files exist on disk.
|
||||
bundle[namespace] = {};
|
||||
}
|
||||
}
|
||||
@@ -71,8 +78,12 @@ export async function ensureServerGlobalI18n(): Promise<typeof i18next> {
|
||||
initPromise = i18next
|
||||
.init({
|
||||
...i18nConfig,
|
||||
// Accept the formal variants as valid languages so a later
|
||||
// `changeLanguage("<locale>-formal")` is honoured instead of being
|
||||
// dropped back to the detection fallback.
|
||||
supportedLngs: globalLngs,
|
||||
lng: i18nConfig.fallbackLng,
|
||||
preload: [...supportedLocales],
|
||||
preload: [...globalLngs],
|
||||
resources,
|
||||
// Resources are bundled in memory, so no backend/async loading is needed.
|
||||
initImmediate: false,
|
||||
|
||||
@@ -19,8 +19,8 @@ const testWebConfig: TestWebConfig = {
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("@toolctrl/config", async () => {
|
||||
const actual = await vi.importActual<typeof import("@toolctrl/config")>("@toolctrl/config");
|
||||
vi.doMock("@hivegrid/config", async () => {
|
||||
const actual = await vi.importActual<typeof import("@hivegrid/config")>("@hivegrid/config");
|
||||
return {
|
||||
...actual,
|
||||
loadRequiredWebConfig: () => testWebConfig,
|
||||
|
||||
@@ -3,9 +3,10 @@ import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createCookie } from "react-router";
|
||||
|
||||
import { loadRequiredWebConfig, resolveSessionCookiePolicy } from "@toolctrl/config";
|
||||
import { loadRequiredWebConfig, resolveSessionCookiePolicy } from "@hivegrid/config";
|
||||
import { RemixI18Next } from "remix-i18next/server";
|
||||
import { i18nConfig, i18nNamespaces } from "./i18n";
|
||||
import { effectiveLng, formalLngs, i18nConfig, i18nNamespaces } from "./i18n";
|
||||
import { resolveAnrede } from "./anrede.server";
|
||||
|
||||
import Backend from "i18next-fs-backend";
|
||||
|
||||
@@ -57,6 +58,13 @@ export const remixI18Next = new RemixI18Next({
|
||||
// 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, ...formalLngs],
|
||||
backend: {
|
||||
localePath: serverLocalePath,
|
||||
loadPath: `${serverLocalePath}/{{lng}}/{{ns}}.json`,
|
||||
@@ -68,13 +76,20 @@ export const remixI18Next = new RemixI18Next({
|
||||
plugins: [Backend],
|
||||
});
|
||||
|
||||
export async function getTranslations(request: Request, namespace = "translations") {
|
||||
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 t = await remixI18Next.getFixedT(request, namespaces);
|
||||
let translations: any = {};
|
||||
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 };
|
||||
}
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
import { defaultLocale, isSupportedLocale } from "@toolctrl/i18n";
|
||||
import { defaultLocale, isSupportedLocale } from "@hivegrid/i18n";
|
||||
import { remixI18Next } from "./i18next.server";
|
||||
|
||||
/**
|
||||
* Mail-side translator function. Kept locally now that the legacy
|
||||
* `@toolctrl/mail` template-string renderer is retired; the React-Email
|
||||
* `@hivegrid/mail` template-string renderer is retired; the React-Email
|
||||
* templates do not consume a translator directly, but this signature is
|
||||
* still exposed for downstream callers that audit/log under translator
|
||||
* context.
|
||||
|
||||
Reference in new issue
Block a user