Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/i18n/src/index.ts
T
2026-07-08 16:15:37 +02:00

95 lines
3.8 KiB
TypeScript

import type { InitOptions } from "i18next";
export const defaultLocale = "de";
export const supportedLocales = ["de", "en"];
export type SupportedLocale = (typeof supportedLocales)[number];
// Namespace order:
// - `translations` is the legacy single-file namespace that still holds the
// bulk of UI strings. New keys for *new* surfaces should land in a
// dedicated namespace.
// - `common` collects shared UI primitives — buttons, generic labels — and
// ICU-MessageFormat plural patterns (`itemsSelected`, `memberCount`).
// - `settings`/`billing`/`crm`/`auth`/`emailMarketing` are per-area
// namespaces split out of the legacy file. See `docs/contributing/i18n.md`.
export const i18nNamespaces = ["translations", "errors", "mail", "common", "settings", "billing", "crm", "auth", "emailMarketing"] as const;
export type I18nNamespace = (typeof i18nNamespaces)[number];
export const i18nConfig = {
supportedLngs: supportedLocales,
fallbackLng: defaultLocale,
defaultNS: "translations",
ns: [...i18nNamespaces],
react: {
useSuspense: false,
},
} satisfies InitOptions;
export function isSupportedLocale(locale: string | undefined): locale is SupportedLocale {
return Boolean(locale && supportedLocales.includes(locale));
}
// Formality (Anrede) axis — orthogonal to locale. "du" is the base language
// (existing keys), "sie" resolves to the "<locale>-formal" i18next variant
// which holds only the formal overrides and falls back to the base locale via
// the existing `fallbackLng` ("de"). This keeps the change purely additive:
// with the default "du" the effective language is the plain locale, so existing
// behaviour is byte-identical. Toggling a tenant to "sie" switches the
// effective language to "de-formal"/"en-formal"; any key without a formal
// override transparently falls back to the informal text.
export const anredeVariants = ["du", "sie"] as const;
export type Anrede = (typeof anredeVariants)[number];
export const defaultAnrede: Anrede = "du";
/** Formal language variants for every supported locale ("de-formal", "en-formal"). */
export const formalLngs = supportedLocales.map((locale) => `${locale}-formal`);
export function isAnrede(value: string | undefined): value is Anrede {
return Boolean(value && (anredeVariants as readonly string[]).includes(value));
}
/** Maps (locale, anrede) → effective i18next language. "sie" → "<locale>-formal", "du" → locale. */
export function effectiveLng(locale: string, anrede: Anrede = defaultAnrede): string {
return anrede === "sie" ? `${locale}-formal` : locale;
}
const ltrLocales = new Set<string>(["de", "en"]);
const rtlLocales = new Set<string>(["ar", "he", "fa", "ur"]);
export type TextDirection = "ltr" | "rtl";
export function getDirection(locale: string | undefined): TextDirection {
if (!locale) return "ltr";
if (rtlLocales.has(locale)) return "rtl";
if (ltrLocales.has(locale)) return "ltr";
return "ltr";
}
export function isRTL(locale: string | undefined): boolean {
return getDirection(locale) === "rtl";
}
// Foundation extensions (#72 i18n-foundation). Exports are namespaced via
// `./intl` and `./icu` subpaths in `package.json#exports`, but we also
// re-export the helpers here so callers can `import { formatCurrency } from
// "@hivegrid/i18n"` without juggling subpaths.
export {
formatNumber,
formatPercent,
formatCompactNumber,
formatCurrency,
formatDate,
formatRelativeTime,
formatList,
type FormatNumberOptions,
type FormatCurrencyOptions,
type FormatDateOptions,
type FormatRelativeTimeOptions,
type FormatListOptions,
} from "./intl/index.js";
export { formatIcu, createIcuFormatter, type IcuValues, type IcuOptions, type IcuFormatter } from "./icu/index.js";
export { createUseTranslate, isIcuMessage, type Translator, type ReactI18nextLike, type UseTranslateOptions } from "./useTranslate.js";