Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/i18n/src/index.ts
T

118 lines
4.4 KiB
TypeScript

import type { InitOptions } from "i18next";
import { catalogReadyLocales, fallbackLng, supportedLocales } from "./locales.js";
export {
EnabledLocalesConfigError,
catalogReadyLocales,
defaultEnabledLocales,
defaultLocale,
fallbackLng,
formalLngs,
isSupportedLocale,
localeReleaseStages,
normalizeLocaleCandidate,
parseEnabledLocales,
resolveEnabledLocale,
supportedLocales,
type EnabledLocalesIssue,
type EnabledLocalesIssueCode,
type FormalLocale,
type ResolveEnabledLocaleInput,
type SupportedLocale,
} from "./locales.js";
// 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: catalogReadyLocales,
fallbackLng,
defaultNS: "translations",
ns: [...i18nNamespaces],
interpolation: {
// React escapes every text node it renders, so i18next must not escape
// interpolated values a second time. With i18next's default
// (`escapeValue: true`) any value containing HTML-relevant characters is
// turned into entities that React then renders literally — a time zone
// interpolates as `Europe/Berlin` and a URL as `https://…`.
// Values that legitimately reach `dangerouslySetInnerHTML` are sanitized
// explicitly at those call sites (see `app/utils/html-sanitizer.ts`).
escapeValue: false,
},
react: {
useSuspense: false,
},
} satisfies InitOptions;
// Formality (Anrede) axis — orthogonal to locale. "du" is the base language
// (existing keys), "sie" resolves to the "<locale>-formal" i18next variant.
// Sparse overrides fall back through the matching informal locale before
// German. 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 "<locale>-formal"; any key without a formal override
// transparently falls back to the matching informal text.
export const anredeVariants = ["du", "sie"] as const;
export type Anrede = (typeof anredeVariants)[number];
export const defaultAnrede: Anrede = "du";
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>(supportedLocales);
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,
formatCurrencyAmount,
formatDate,
formatRelativeTime,
formatList,
type FormatNumberOptions,
type CurrencyAmount,
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";