@xndrjs/i18n-react 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -323,15 +323,10 @@ function toThrownError(error) {
323
323
  }
324
324
  return new Error("i18n namespace load failed");
325
325
  }
326
- function renderDisplayEntry(entry, options) {
326
+ var noopT = () => "";
327
+ function gateValueFromEntry(entry) {
327
328
  if (entry.status === "ready") {
328
- return options.children({ t: entry.t, locale: entry.locale });
329
- }
330
- if (entry.status === "error" && entry.t === null) {
331
- if (options.renderError) {
332
- return options.renderError({ error: entry.error, retry: entry.retry });
333
- }
334
- throw toThrownError(entry.error);
329
+ return { t: entry.t, locale: entry.locale };
335
330
  }
336
331
  if (entry.t !== null && entry.display) {
337
332
  const value = {
@@ -345,6 +340,19 @@ function renderDisplayEntry(entry, options) {
345
340
  value.error = entry.error;
346
341
  value.retry = entry.retry;
347
342
  }
343
+ return value;
344
+ }
345
+ return null;
346
+ }
347
+ function renderDisplayEntry(entry, options) {
348
+ if (entry.status === "error" && entry.t === null) {
349
+ if (options.renderError) {
350
+ return options.renderError({ error: entry.error, retry: entry.retry });
351
+ }
352
+ throw toThrownError(entry.error);
353
+ }
354
+ const value = gateValueFromEntry(entry);
355
+ if (value !== null) {
348
356
  return options.children(value);
349
357
  }
350
358
  return options.fallback ?? null;
@@ -352,10 +360,10 @@ function renderDisplayEntry(entry, options) {
352
360
  function createI18nLoadGate(options) {
353
361
  const { useLoadArgs } = options;
354
362
  const keepPrevious = options.keepPreviousOnPartitionChange !== false;
355
- function useGateEntry(namespaces) {
363
+ function useGateLoad(namespaces) {
356
364
  const resolved = resolveNamespaces(namespaces);
357
365
  const args = useLoadArgs();
358
- return useNamespaceLoad({
366
+ const entry = useNamespaceLoad({
359
367
  coordinator: args.coordinator,
360
368
  engineRef: args.engineRef,
361
369
  partition: args.partition,
@@ -365,9 +373,10 @@ function createI18nLoadGate(options) {
365
373
  ...args.tryResolveSync !== void 0 ? { tryResolveSync: () => args.tryResolveSync(resolved) } : {},
366
374
  keepPrevious
367
375
  });
376
+ return { entry, locale: args.locale };
368
377
  }
369
378
  function I18n({ namespaces, fallback, renderError, children }) {
370
- const entry = useGateEntry(namespaces);
379
+ const { entry } = useGateLoad(namespaces);
371
380
  return renderDisplayEntry(entry, {
372
381
  ...fallback !== void 0 ? { fallback } : {},
373
382
  ...renderError !== void 0 ? { renderError } : {},
@@ -377,16 +386,23 @@ function createI18nLoadGate(options) {
377
386
  function withI18n(hocOptions, render) {
378
387
  const { fallback, renderError } = hocOptions;
379
388
  const Outer = forwardRef(function I18nHocOuter(props, ref) {
380
- const entry = useGateEntry(hocOptions.namespaces);
381
- const needsFallback = entry.status === "pending" && entry.t === null;
382
- const resolvedFallback = !needsFallback || fallback === void 0 ? void 0 : typeof fallback === "function" ? fallback(props) : fallback;
383
- return renderDisplayEntry(entry, {
384
- ...resolvedFallback !== void 0 ? { fallback: resolvedFallback } : {},
385
- ...renderError !== void 0 ? {
386
- renderError: ({ error, retry }) => renderError({ error, retry, props })
387
- } : {},
388
- children: (value) => render(props, value, ref)
389
- });
389
+ const { entry, locale } = useGateLoad(hocOptions.namespaces);
390
+ const displayValue = gateValueFromEntry(entry);
391
+ const value = displayValue ?? { t: noopT, locale };
392
+ const rendered = render(props, value, ref);
393
+ if (entry.status === "error" && entry.t === null) {
394
+ if (renderError) {
395
+ return renderError({ error: entry.error, retry: entry.retry, props });
396
+ }
397
+ throw toThrownError(entry.error);
398
+ }
399
+ if (displayValue !== null) {
400
+ return rendered;
401
+ }
402
+ if (fallback === void 0) {
403
+ return null;
404
+ }
405
+ return typeof fallback === "function" ? fallback(props) : fallback;
390
406
  });
391
407
  const displayName = render.name || "Component";
392
408
  Outer.displayName = `withI18n(${displayName})`;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/create-scoped-t.ts","../src/create-load-coordinator.ts","../src/namespace-load-gate.tsx","../src/root-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;AAAA,MACP,GAAI,SAAA,KAAc,MAAA,GAAY,EAAE,SAAA,KAAc,EAAC;AAAA,MAC/C,GAAI,SAAA,KAAc,MAAA,GAAY,EAAE,SAAA,KAAc;AAAC,KAChD,CAAA;AACD,IAAA,QAAA,CAAS,OAAA,GAAU;AAAA,MACjB,MAAA;AAAA,MACA,aAAa,qBAAA,EAAuC;AAAA,MACpD;AAAA,KACF;AAAA,EACF,CAAA,MAAO;AACL,IAAA,QAAA,CAAS,QAAQ,MAAA,GAAS,MAAA;AAAA,EAC5B;AAEA,EAAA,2BAAQ,eAAA,CAAgB,QAAA,EAAhB,EAAyB,KAAA,EAAO,QAAA,CAAS,SAAU,QAAA,EAAS,CAAA;AACtE","file":"index.js","sourcesContent":["/**\n * Builds the scoped `t` function used by providers and load gates.\n *\n * Takes a loaded `@xndrjs/i18n` scope and returns a capability-safe translator:\n * namespaces outside the provider list throw at runtime. Locale is always bound.\n */\nimport type { CreateScopedTOptions, ScopedScopeLike, ScopedTranslateFn } from \"./types.js\";\n\nfunction assertNamespace(namespace: string, namespaces: readonly string[]): void {\n if (!namespaces.includes(namespace)) {\n throw new Error(\n `Namespace \"${namespace}\" is not loaded by this provider. Allowed: [${namespaces.join(\", \")}]`\n );\n }\n}\n\nfunction resolveActiveScope(scope: ScopedScopeLike, locale: string): ScopedScopeLike {\n if (typeof scope.forLocale === \"function\" && scope.locale !== locale) {\n return scope.forLocale(locale);\n }\n return scope;\n}\n\n/**\n * Wraps a loaded scope `t` with namespace guards and locale binding.\n */\nexport function createScopedT(\n scope: ScopedScopeLike,\n options: CreateScopedTOptions\n): ScopedTranslateFn {\n const activeScope = resolveActiveScope(scope, options.locale);\n const { namespaces } = options;\n\n return ((namespace: string, key: string, ...rest: unknown[]) => {\n assertNamespace(namespace, namespaces);\n return activeScope.t(namespace, key, ...rest);\n }) as ScopedTranslateFn;\n}\n\n/**\n * Curries a multi-namespace context `t(ns, key, …)` into a namespace-bound `t(key, …)`.\n * Used when composing gate values without introducing a separate hooks API.\n */\nexport function bindNamespaceTranslate(\n t: ScopedTranslateFn,\n namespace: string\n): (key: string, params?: Record<string, unknown>) => string {\n return (key: string, params?: Record<string, unknown>) =>\n params === undefined ? t(namespace, key) : t(namespace, key, params);\n}\n","/**\n * Load deduplication for lazy i18n namespace gates / HOCs.\n *\n * Supports multiple concurrent keys so parallel per-namespace gates can share\n * one coordinator. Each `(engine, partition, namespaces)` entry keeps its own\n * promise and status. Display state (keep-previous / pendingLocale / stable `t`)\n * lives here so gate components stay pure observers of `useSyncExternalStore`.\n */\nimport { createScopedT } from \"./create-scoped-t.js\";\nimport type {\n GetDisplayEntryOptions,\n LoadCoordinator,\n LoadCoordinatorEntry,\n LoadCoordinatorKey,\n LoadCoordinatorRequest,\n LoadDisplayEntry,\n ScopedScopeLike,\n ScopedTranslateFn,\n} from \"./types.js\";\n\nconst engineIds = new WeakMap<object, number>();\nlet nextEngineId = 1;\n\nfunction engineId(ref: unknown): number {\n if (ref === null || (typeof ref !== \"object\" && typeof ref !== \"function\")) {\n return 0;\n }\n const obj = ref as object;\n let id = engineIds.get(obj);\n if (id === undefined) {\n id = nextEngineId++;\n engineIds.set(obj, id);\n }\n return id;\n}\n\nfunction cacheKey(\n engineRef: unknown,\n partition: string | undefined,\n namespaces: readonly string[]\n): string {\n return `${engineId(engineRef)}\\0${partition ?? \"\"}\\0${namespaces.join(\"\\0\")}`;\n}\n\n/** Gate identity for keep-previous (namespaces only — partition/locale can change). */\nfunction gateId(engineRef: unknown, namespaces: readonly string[]): string {\n return `${engineId(engineRef)}\\0${namespaces.join(\"\\0\")}`;\n}\n\ninterface CacheEntry<Scope> {\n status: \"pending\" | \"resolved\" | \"error\";\n promise: Promise<Scope>;\n resolvedScope: Scope | null;\n error: unknown;\n requestId: number;\n /** Stable LoadCoordinatorEntry snapshot for getEntry(). */\n entrySnapshot: LoadCoordinatorEntry<Scope>;\n}\n\ninterface DisplayCache {\n lastReady: { scope: ScopedScopeLike; locale: string } | null;\n /** Stable display entry object until inputs change. */\n displaySnapshot: LoadDisplayEntry<ScopedScopeLike> | null;\n /** Identity key for the last displaySnapshot. */\n displayKey: string | null;\n boundT: ScopedTranslateFn | null;\n boundTKey: string | null;\n}\n\n/**\n * Dedupes in-flight loads by `(engineRef, partition, namespaces)` and exposes\n * display snapshots + retry for manual pending/resolved/error rendering.\n */\nexport function createLoadCoordinator<Scope = ScopedScopeLike>(): LoadCoordinator<Scope> {\n let revision = 0;\n let nextRequestId = 0;\n const entries = new Map<string, CacheEntry<Scope>>();\n const displayByGate = new Map<string, DisplayCache>();\n const listeners = new Set<() => void>();\n let lastKey: string | null = null;\n\n function notify(): void {\n for (const listener of listeners) {\n listener();\n }\n }\n\n function bumpAndNotify(): void {\n revision += 1;\n // Invalidate display snapshots that depend on entry status.\n for (const display of displayByGate.values()) {\n display.displaySnapshot = null;\n display.displayKey = null;\n }\n notify();\n }\n\n function markResolvedSync(key: string, syncScope: Scope): void {\n const entrySnapshot: LoadCoordinatorEntry<Scope> = {\n status: \"resolved\",\n scope: syncScope,\n };\n entries.set(key, {\n status: \"resolved\",\n promise: Promise.resolve(syncScope),\n resolvedScope: syncScope,\n error: null,\n requestId: ++nextRequestId,\n entrySnapshot,\n });\n }\n\n /**\n * SSR / getServerSnapshot: hydrate from peek/state only — never kick `load()`.\n */\n function ensureSync(input: LoadCoordinatorRequest<Scope>): void {\n const key = cacheKey(input.engineRef, input.partition, input.namespaces);\n lastKey = key;\n\n if (entries.has(key)) {\n return;\n }\n\n if (input.tryResolveSync === undefined) {\n return;\n }\n\n const syncScope = input.tryResolveSync();\n if (syncScope !== null) {\n markResolvedSync(key, syncScope);\n }\n }\n\n function ensure(input: LoadCoordinatorRequest<Scope>): void {\n const key = cacheKey(input.engineRef, input.partition, input.namespaces);\n lastKey = key;\n\n const existing = entries.get(key);\n if (existing !== undefined) {\n return;\n }\n\n if (input.tryResolveSync !== undefined) {\n const syncScope = input.tryResolveSync();\n if (syncScope !== null) {\n markResolvedSync(key, syncScope);\n return;\n }\n }\n\n const requestId = ++nextRequestId;\n const promise = input.load().then(\n (scope) => {\n const entry = entries.get(key);\n if (entry === undefined || entry.requestId !== requestId) {\n return scope;\n }\n entry.status = \"resolved\";\n entry.resolvedScope = scope;\n entry.error = null;\n entry.entrySnapshot = { status: \"resolved\", scope };\n bumpAndNotify();\n return scope;\n },\n (error: unknown) => {\n const entry = entries.get(key);\n if (entry !== undefined && entry.requestId === requestId) {\n entry.status = \"error\";\n entry.error = error;\n entry.resolvedScope = null;\n entry.entrySnapshot = { status: \"error\", error };\n bumpAndNotify();\n }\n throw error;\n }\n );\n // Avoid unhandled rejection when only gate subscribers attach (no getPromise).\n void promise.catch((error: unknown) => {\n console.error(\"[i18n-react] namespace load failed:\", error);\n });\n\n entries.set(key, {\n status: \"pending\",\n promise,\n resolvedScope: null,\n error: null,\n requestId,\n entrySnapshot: { status: \"pending\" },\n });\n }\n\n function getEntry(keyInput: LoadCoordinatorKey): LoadCoordinatorEntry<Scope> {\n const key = cacheKey(keyInput.engineRef, keyInput.partition, keyInput.namespaces);\n const entry = entries.get(key);\n if (entry === undefined) {\n return { status: \"pending\" };\n }\n return entry.entrySnapshot;\n }\n\n function getOrCreateDisplay(engineRef: unknown, namespaces: readonly string[]): DisplayCache {\n const id = gateId(engineRef, namespaces);\n let display = displayByGate.get(id);\n if (display === undefined) {\n display = {\n lastReady: null,\n displaySnapshot: null,\n displayKey: null,\n boundT: null,\n boundTKey: null,\n };\n displayByGate.set(id, display);\n }\n return display;\n }\n\n function bindT(\n display: DisplayCache,\n scope: ScopedScopeLike,\n locale: string,\n namespaces: readonly string[],\n scopeToken: string\n ): ScopedTranslateFn {\n const tKey = `${scopeToken}\\0${locale}\\0${namespaces.join(\"\\0\")}`;\n if (display.boundT !== null && display.boundTKey === tKey) {\n return display.boundT;\n }\n const t = createScopedT(scope, { namespaces, locale });\n display.boundT = t;\n display.boundTKey = tKey;\n return t;\n }\n\n function getDisplayEntry(\n keyInput: LoadCoordinatorKey,\n options: GetDisplayEntryOptions\n ): LoadDisplayEntry<Scope> {\n const key = cacheKey(keyInput.engineRef, keyInput.partition, keyInput.namespaces);\n const entry = entries.get(key);\n const display = getOrCreateDisplay(keyInput.engineRef, options.namespaces);\n const keepPrevious = options.keepPrevious !== false;\n\n const retry = () => {\n retryKey(keyInput);\n };\n\n let result: LoadDisplayEntry<Scope>;\n\n if (entry === undefined || entry.status === \"pending\") {\n const kept = keepPrevious ? display.lastReady : null;\n result = {\n status: \"pending\",\n display: kept as { scope: Scope & ScopedScopeLike; locale: string } | null,\n pendingLocale: kept ? options.locale : undefined,\n t: kept\n ? bindT(display, kept.scope, kept.locale, options.namespaces, `kept:${kept.locale}`)\n : null,\n } as LoadDisplayEntry<Scope>;\n } else if (entry.status === \"resolved\") {\n const scope = entry.resolvedScope as Scope & ScopedScopeLike;\n display.lastReady = { scope, locale: options.locale };\n result = {\n status: \"ready\",\n scope,\n locale: options.locale,\n t: bindT(display, scope, options.locale, options.namespaces, `ready:${key}`),\n } as LoadDisplayEntry<Scope>;\n } else {\n const kept = keepPrevious ? display.lastReady : null;\n result = {\n status: \"error\",\n error: entry.error,\n display: kept as { scope: Scope & ScopedScopeLike; locale: string } | null,\n pendingLocale: kept ? options.locale : undefined,\n t: kept\n ? bindT(display, kept.scope, kept.locale, options.namespaces, `kept:${kept.locale}`)\n : null,\n retry,\n } as LoadDisplayEntry<Scope>;\n }\n\n const displayKey = `${key}\\0${result.status}\\0${options.locale}\\0${String(!!result.t)}`;\n if (display.displaySnapshot !== null && display.displayKey === displayKey) {\n return display.displaySnapshot as LoadDisplayEntry<Scope>;\n }\n display.displaySnapshot = result as LoadDisplayEntry<ScopedScopeLike>;\n display.displayKey = displayKey;\n return result;\n }\n\n function retryKey(keyInput: LoadCoordinatorKey): void {\n const key = cacheKey(keyInput.engineRef, keyInput.partition, keyInput.namespaces);\n if (!entries.has(key)) {\n return;\n }\n entries.delete(key);\n bumpAndNotify();\n }\n\n function subscribe(listener: () => void): () => void {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n }\n\n function getPromise(): Promise<Scope> {\n if (lastKey === null) {\n throw new Error(\"createLoadCoordinator: call ensure()/request() before getPromise()\");\n }\n const entry = entries.get(lastKey);\n if (entry === undefined) {\n throw new Error(\"createLoadCoordinator: call ensure()/request() before getPromise()\");\n }\n return entry.promise;\n }\n\n function getResolvedScope(): Scope | null {\n if (lastKey === null) {\n return null;\n }\n const entry = entries.get(lastKey);\n return entry?.status === \"resolved\" ? entry.resolvedScope : null;\n }\n\n return {\n get revision() {\n return revision;\n },\n request: ensure,\n ensure,\n ensureSync,\n getEntry,\n getDisplayEntry,\n retry: retryKey,\n subscribe,\n getPromise,\n getResolvedScope,\n };\n}\n","/**\n * Manual (non-Suspense) i18n load gate: ensure + subscribe via the load\n * coordinator, then render fallback / keep-previous / error UI / throw.\n */\nimport {\n forwardRef,\n useSyncExternalStore,\n type ForwardedRef,\n type ForwardRefExoticComponent,\n type ReactNode,\n type RefAttributes,\n} from \"react\";\nimport type {\n LoadCoordinator,\n LoadCoordinatorRequest,\n LoadDisplayEntry,\n LoadPartition,\n ScopedScopeLike,\n ScopedTranslateFn,\n} from \"./types.js\";\n\n/** Inputs for {@link useNamespaceLoad}. */\nexport interface UseNamespaceLoadInput<Scope> {\n coordinator: LoadCoordinator<Scope>;\n engineRef: unknown;\n partition: LoadPartition;\n namespaces: readonly string[];\n locale: string;\n load: () => Promise<Scope>;\n tryResolveSync?: () => Scope | null;\n keepPrevious?: boolean;\n}\n\n/**\n * Starts (or reuses) a coordinator load and re-renders when that entry settles.\n * Client getSnapshot may kick `load()`; SSR getServerSnapshot only peeks sync\n * (hydrated `state`) and never starts a client fetch during prerender.\n */\nexport function useNamespaceLoad<Scope>(\n input: UseNamespaceLoadInput<Scope>\n): LoadDisplayEntry<Scope> {\n const {\n coordinator,\n engineRef,\n partition,\n namespaces,\n locale,\n load,\n tryResolveSync,\n keepPrevious,\n } = input;\n\n const request: LoadCoordinatorRequest<Scope> = {\n engineRef,\n partition,\n namespaces,\n load,\n ...(tryResolveSync !== undefined ? { tryResolveSync } : {}),\n };\n\n const displayOptions = {\n locale,\n namespaces,\n ...(keepPrevious !== undefined ? { keepPrevious } : {}),\n };\n\n return useSyncExternalStore(\n coordinator.subscribe,\n () => {\n coordinator.ensure(request);\n return coordinator.getDisplayEntry({ engineRef, partition, namespaces }, displayOptions);\n },\n () => {\n coordinator.ensureSync(request);\n return coordinator.getDisplayEntry({ engineRef, partition, namespaces }, displayOptions);\n }\n );\n}\n\n/** Scoped `{ t, locale }` injected into gate children / HOC when ready (or kept). */\nexport interface I18nLoadGateValue {\n t: ScopedTranslateFn;\n locale: string;\n /** Set during keep-then-switch while a new partition is loading. */\n pendingLocale?: string;\n error?: unknown;\n retry?: () => void;\n}\n\n/** Load wiring from React context (coordinator, engine, partition, load). */\nexport interface I18nLoadArgs {\n coordinator: LoadCoordinator<ScopedScopeLike>;\n engineRef: unknown;\n partition: LoadPartition;\n locale: string;\n load: (namespaces: readonly string[]) => Promise<ScopedScopeLike>;\n tryResolveSync?: (namespaces: readonly string[]) => ScopedScopeLike | null;\n}\n\nexport interface CreateI18nLoadGateOptions {\n /**\n * When true (default), keep last resolved `{ t, locale }` while a new\n * partition is pending instead of showing fallback.\n */\n keepPreviousOnPartitionChange?: boolean;\n /** Hook that resolves coordinator + load inputs from React context. */\n useLoadArgs: () => I18nLoadArgs;\n}\n\nexport interface I18nGateProps {\n namespaces: readonly string[];\n /** Shown only while the load is pending (not on error). */\n fallback?: ReactNode;\n /**\n * When set, called for error with no keep-previous display.\n * Otherwise the load error is thrown for a React error boundary.\n */\n renderError?: (args: { error: unknown; retry: () => void }) => ReactNode;\n children: (value: I18nLoadGateValue) => ReactNode;\n}\n\n/** Pending UI for `withI18n` — static node or derived from own props. */\nexport type WithI18nFallback<P extends object> = ReactNode | ((props: P) => ReactNode);\n\nexport interface I18nHocOptions<P extends object = object> {\n namespaces: readonly string[];\n fallback?: WithI18nFallback<P>;\n renderError?: (args: { error: unknown; retry: () => void; props: P }) => ReactNode;\n}\n\n/** Render fn for `withI18n`: own props, i18n bag, optional forwarded ref. */\nexport type WithI18nRender<P extends object, R = never> = (\n props: P,\n i18n: I18nLoadGateValue,\n ref?: ForwardedRef<R>\n) => ReactNode;\n\nfunction resolveNamespaces(namespaces: readonly string[] | undefined): readonly string[] {\n if (namespaces === undefined || namespaces.length === 0) {\n throw new Error(\"withI18n / I18n: requires a non-empty namespaces list\");\n }\n return namespaces;\n}\n\nfunction toThrownError(error: unknown): Error {\n if (error instanceof Error) {\n return error;\n }\n if (typeof error === \"string\") {\n return new Error(error);\n }\n return new Error(\"i18n namespace load failed\");\n}\n\nfunction renderDisplayEntry(\n entry: LoadDisplayEntry<ScopedScopeLike>,\n options: {\n fallback?: ReactNode;\n renderError?: (args: { error: unknown; retry: () => void }) => ReactNode;\n children: (value: I18nLoadGateValue) => ReactNode;\n }\n): ReactNode {\n if (entry.status === \"ready\") {\n return options.children({ t: entry.t, locale: entry.locale });\n }\n\n if (entry.status === \"error\" && entry.t === null) {\n if (options.renderError) {\n return options.renderError({ error: entry.error, retry: entry.retry! });\n }\n throw toThrownError(entry.error);\n }\n\n if (entry.t !== null && entry.display) {\n const value: I18nLoadGateValue = {\n t: entry.t,\n locale: entry.display.locale,\n };\n if (entry.pendingLocale !== undefined) {\n value.pendingLocale = entry.pendingLocale;\n }\n if (entry.status === \"error\") {\n value.error = entry.error;\n value.retry = entry.retry;\n }\n return options.children(value);\n }\n\n return options.fallback ?? null;\n}\n\n/**\n * Factory for generic `I18n` gate + `withI18n` HOC over the same Outer.\n * Codegen emits typed wrappers; tests use this directly.\n */\nexport function createI18nLoadGate(options: CreateI18nLoadGateOptions): {\n I18n: (props: I18nGateProps) => ReactNode;\n withI18n: <P extends object = object, R = never>(\n hocOptions: I18nHocOptions<P>,\n render: WithI18nRender<P, R>\n ) => ForwardRefExoticComponent<P & RefAttributes<R>>;\n} {\n const { useLoadArgs } = options;\n const keepPrevious = options.keepPreviousOnPartitionChange !== false;\n\n function useGateEntry(namespaces: readonly string[]): LoadDisplayEntry<ScopedScopeLike> {\n const resolved = resolveNamespaces(namespaces);\n const args = useLoadArgs();\n return useNamespaceLoad({\n coordinator: args.coordinator,\n engineRef: args.engineRef,\n partition: args.partition,\n namespaces: resolved,\n locale: args.locale,\n load: () => args.load(resolved),\n ...(args.tryResolveSync !== undefined\n ? { tryResolveSync: () => args.tryResolveSync!(resolved) }\n : {}),\n keepPrevious,\n });\n }\n\n function I18n({ namespaces, fallback, renderError, children }: I18nGateProps): ReactNode {\n const entry = useGateEntry(namespaces);\n return renderDisplayEntry(entry, {\n ...(fallback !== undefined ? { fallback } : {}),\n ...(renderError !== undefined ? { renderError } : {}),\n children,\n });\n }\n\n function withI18n<P extends object = object, R = never>(\n hocOptions: I18nHocOptions<P>,\n render: WithI18nRender<P, R>\n ): ForwardRefExoticComponent<P & RefAttributes<R>> {\n const { fallback, renderError } = hocOptions;\n const Outer = forwardRef<R, P>(function I18nHocOuter(props, ref) {\n const entry = useGateEntry(hocOptions.namespaces);\n\n const needsFallback = entry.status === \"pending\" && entry.t === null;\n const resolvedFallback =\n !needsFallback || fallback === undefined\n ? undefined\n : typeof fallback === \"function\"\n ? fallback(props as P)\n : fallback;\n\n return renderDisplayEntry(entry, {\n ...(resolvedFallback !== undefined ? { fallback: resolvedFallback } : {}),\n ...(renderError !== undefined\n ? {\n renderError: ({ error, retry }) => renderError({ error, retry, props: props as P }),\n }\n : {}),\n children: (value) => render(props as P, value, ref),\n });\n });\n\n const displayName = render.name || \"Component\";\n Outer.displayName = `withI18n(${displayName})`;\n return Outer as ForwardRefExoticComponent<P & RefAttributes<R>>;\n }\n\n return { I18n, withI18n };\n}\n","/**\n * Holds the shared i18n root: handle + coordinator + active locale.\n *\n * Generated {@link useI18nRoot} casts the handle to the project's typed factory return.\n */\nimport { createContext, useContext } from \"react\";\nimport type { I18nRootContextValue } from \"./types.js\";\n\nconst I18nRootContext = createContext<I18nRootContextValue | null>(null);\n\n/** Resolves the shared root from an ancestor {@link I18nRootProvider}. */\nexport function useI18nRootContext(): I18nRootContextValue {\n const value = useContext(I18nRootContext);\n if (value === null) {\n throw new Error(\"useI18nRoot must be used within I18nRoot / I18nRootProvider\");\n }\n return value;\n}\n\n/** Internal — provider wiring. */\nexport { I18nRootContext };\n","/**\n * Single provider that shares handle + coordinator + locale across a lazy subtree.\n */\nimport { useRef, type ReactNode } from \"react\";\nimport type { FetchArtifact, I18nCreateInput, OnMissingTranslation } from \"@xndrjs/i18n\";\nimport { createLoadCoordinator } from \"./create-load-coordinator.js\";\nimport { I18nRootContext } from \"./root-context.js\";\nimport type {\n CreateI18nFactory,\n I18nHandleLike,\n I18nRootContextValue,\n ScopedScopeLike,\n} from \"./types.js\";\n\nexport interface I18nRootProviderProps {\n createI18n: CreateI18nFactory;\n locale: string;\n /**\n * Cold-start / eager dictionary seed (defaults to `{}` when neither prop is set).\n * Mutually exclusive with {@link state}.\n */\n dictionary?: Record<string, unknown>;\n /** Full create/hydrate input from `handle.serialize()` — mutually exclusive with {@link dictionary}. */\n state?: I18nCreateInput | undefined;\n onMissing?: OnMissingTranslation;\n /** Custom JSON loader for `loaderStrategy: \"fetch\"` projects (ignored by import loaders). */\n fetchImpl?: FetchArtifact;\n children: ReactNode;\n}\n\n/** Creates and shares one handle + load coordinator for descendant gates. */\nexport function I18nRootProvider({\n createI18n,\n locale,\n dictionary,\n state,\n onMissing,\n fetchImpl,\n children,\n}: I18nRootProviderProps): ReactNode {\n if (state !== undefined && dictionary !== undefined) {\n throw new Error(\"I18nRootProvider: pass either `state` or `dictionary`, not both.\");\n }\n\n const valueRef = useRef<I18nRootContextValue | null>(null);\n\n if (valueRef.current === null) {\n const seed: I18nCreateInput =\n state ?? ({ dictionary: dictionary ?? {} } satisfies I18nCreateInput);\n const handle = createI18n({\n state: seed,\n ...(onMissing !== undefined ? { onMissing } : {}),\n ...(fetchImpl !== undefined ? { fetchImpl } : {}),\n });\n valueRef.current = {\n handle: handle as I18nHandleLike,\n coordinator: createLoadCoordinator<ScopedScopeLike>(),\n locale,\n };\n } else {\n valueRef.current.locale = locale;\n }\n\n return <I18nRootContext.Provider value={valueRef.current}>{children}</I18nRootContext.Provider>;\n}\n"]}
1
+ {"version":3,"sources":["../src/create-scoped-t.ts","../src/create-load-coordinator.ts","../src/namespace-load-gate.tsx","../src/root-context.tsx","../src/root-provider.tsx"],"names":[],"mappings":";;;;AAQA,SAAS,eAAA,CAAgB,WAAmB,UAAA,EAAqC;AAC/E,EAAA,IAAI,CAAC,UAAA,CAAW,QAAA,CAAS,SAAS,CAAA,EAAG;AACnC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,cAAc,SAAS,CAAA,4CAAA,EAA+C,UAAA,CAAW,IAAA,CAAK,IAAI,CAAC,CAAA,CAAA;AAAA,KAC7F;AAAA,EACF;AACF;AAEA,SAAS,kBAAA,CAAmB,OAAwB,MAAA,EAAiC;AACnF,EAAA,IAAI,OAAO,KAAA,CAAM,SAAA,KAAc,UAAA,IAAc,KAAA,CAAM,WAAW,MAAA,EAAQ;AACpE,IAAA,OAAO,KAAA,CAAM,UAAU,MAAM,CAAA;AAAA,EAC/B;AACA,EAAA,OAAO,KAAA;AACT;AAKO,SAAS,aAAA,CACd,OACA,OAAA,EACmB;AACnB,EAAA,MAAM,WAAA,GAAc,kBAAA,CAAmB,KAAA,EAAO,OAAA,CAAQ,MAAM,CAAA;AAC5D,EAAA,MAAM,EAAE,YAAW,GAAI,OAAA;AAEvB,EAAA,QAAQ,CAAC,SAAA,EAAmB,GAAA,EAAA,GAAgB,IAAA,KAAoB;AAC9D,IAAA,eAAA,CAAgB,WAAW,UAAU,CAAA;AACrC,IAAA,OAAO,WAAA,CAAY,CAAA,CAAE,SAAA,EAAW,GAAA,EAAK,GAAG,IAAI,CAAA;AAAA,EAC9C,CAAA;AACF;AAMO,SAAS,sBAAA,CACd,GACA,SAAA,EAC2D;AAC3D,EAAA,OAAO,CAAC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Q,WAAW,eAAe,CAAA;AACxC,EAAA,IAAI,UAAU,IAAA,EAAM;AAClB,IAAA,MAAM,IAAI,MAAM,6DAA6D,CAAA;AAAA,EAC/E;AACA,EAAA,OAAO,KAAA;AACT;ACcO,SAAS,gBAAA,CAAiB;AAAA,EAC/B,UAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,KAAA;AAAA,EACA,SAAA;AAAA,EACA,SAAA;AAAA,EACA;AACF,CAAA,EAAqC;AACnC,EAAA,IAAI,KAAA,KAAU,MAAA,IAAa,UAAA,KAAe,MAAA,EAAW;AACnD,IAAA,MAAM,IAAI,MAAM,kEAAkE,CAAA;AAAA,EACpF;AAEA,EAAA,MAAM,QAAA,GAAW,OAAoC,IAAI,CAAA;AAEzD,EAAA,IAAI,QAAA,CAAS,YAAY,IAAA,EAAM;AAC7B,IAAA,MAAM,OACJ,KAAA,IAAU,EAAE,UAAA,EAAY,UAAA,IAAc,EAAC,EAAE;AAC3C,IAAA,MAAM,SAAS,UAAA,CAAW;AAAA,MACxB,KAAA,EAAO,IAAA;AAAA,MACP,GAAI,SAAA,KAAc,MAAA,GAAY,EAAE,SAAA,KAAc,EAAC;AAAA,MAC/C,GAAI,SAAA,KAAc,MAAA,GAAY,EAAE,SAAA,KAAc;AAAC,KAChD,CAAA;AACD,IAAA,QAAA,CAAS,OAAA,GAAU;AAAA,MACjB,MAAA;AAAA,MACA,aAAa,qBAAA,EAAuC;AAAA,MACpD;AAAA,KACF;AAAA,EACF,CAAA,MAAO;AACL,IAAA,QAAA,CAAS,QAAQ,MAAA,GAAS,MAAA;AAAA,EAC5B;AAEA,EAAA,2BAAQ,eAAA,CAAgB,QAAA,EAAhB,EAAyB,KAAA,EAAO,QAAA,CAAS,SAAU,QAAA,EAAS,CAAA;AACtE","file":"index.js","sourcesContent":["/**\n * Builds the scoped `t` function used by providers and load gates.\n *\n * Takes a loaded `@xndrjs/i18n` scope and returns a capability-safe translator:\n * namespaces outside the provider list throw at runtime. Locale is always bound.\n */\nimport type { CreateScopedTOptions, ScopedScopeLike, ScopedTranslateFn } from \"./types.js\";\n\nfunction assertNamespace(namespace: string, namespaces: readonly string[]): void {\n if (!namespaces.includes(namespace)) {\n throw new Error(\n `Namespace \"${namespace}\" is not loaded by this provider. Allowed: [${namespaces.join(\", \")}]`\n );\n }\n}\n\nfunction resolveActiveScope(scope: ScopedScopeLike, locale: string): ScopedScopeLike {\n if (typeof scope.forLocale === \"function\" && scope.locale !== locale) {\n return scope.forLocale(locale);\n }\n return scope;\n}\n\n/**\n * Wraps a loaded scope `t` with namespace guards and locale binding.\n */\nexport function createScopedT(\n scope: ScopedScopeLike,\n options: CreateScopedTOptions\n): ScopedTranslateFn {\n const activeScope = resolveActiveScope(scope, options.locale);\n const { namespaces } = options;\n\n return ((namespace: string, key: string, ...rest: unknown[]) => {\n assertNamespace(namespace, namespaces);\n return activeScope.t(namespace, key, ...rest);\n }) as ScopedTranslateFn;\n}\n\n/**\n * Curries a multi-namespace context `t(ns, key, …)` into a namespace-bound `t(key, …)`.\n * Used when composing gate values without introducing a separate hooks API.\n */\nexport function bindNamespaceTranslate(\n t: ScopedTranslateFn,\n namespace: string\n): (key: string, params?: Record<string, unknown>) => string {\n return (key: string, params?: Record<string, unknown>) =>\n params === undefined ? t(namespace, key) : t(namespace, key, params);\n}\n","/**\n * Load deduplication for lazy i18n namespace gates / HOCs.\n *\n * Supports multiple concurrent keys so parallel per-namespace gates can share\n * one coordinator. Each `(engine, partition, namespaces)` entry keeps its own\n * promise and status. Display state (keep-previous / pendingLocale / stable `t`)\n * lives here so gate components stay pure observers of `useSyncExternalStore`.\n */\nimport { createScopedT } from \"./create-scoped-t.js\";\nimport type {\n GetDisplayEntryOptions,\n LoadCoordinator,\n LoadCoordinatorEntry,\n LoadCoordinatorKey,\n LoadCoordinatorRequest,\n LoadDisplayEntry,\n ScopedScopeLike,\n ScopedTranslateFn,\n} from \"./types.js\";\n\nconst engineIds = new WeakMap<object, number>();\nlet nextEngineId = 1;\n\nfunction engineId(ref: unknown): number {\n if (ref === null || (typeof ref !== \"object\" && typeof ref !== \"function\")) {\n return 0;\n }\n const obj = ref as object;\n let id = engineIds.get(obj);\n if (id === undefined) {\n id = nextEngineId++;\n engineIds.set(obj, id);\n }\n return id;\n}\n\nfunction cacheKey(\n engineRef: unknown,\n partition: string | undefined,\n namespaces: readonly string[]\n): string {\n return `${engineId(engineRef)}\\0${partition ?? \"\"}\\0${namespaces.join(\"\\0\")}`;\n}\n\n/** Gate identity for keep-previous (namespaces only — partition/locale can change). */\nfunction gateId(engineRef: unknown, namespaces: readonly string[]): string {\n return `${engineId(engineRef)}\\0${namespaces.join(\"\\0\")}`;\n}\n\ninterface CacheEntry<Scope> {\n status: \"pending\" | \"resolved\" | \"error\";\n promise: Promise<Scope>;\n resolvedScope: Scope | null;\n error: unknown;\n requestId: number;\n /** Stable LoadCoordinatorEntry snapshot for getEntry(). */\n entrySnapshot: LoadCoordinatorEntry<Scope>;\n}\n\ninterface DisplayCache {\n lastReady: { scope: ScopedScopeLike; locale: string } | null;\n /** Stable display entry object until inputs change. */\n displaySnapshot: LoadDisplayEntry<ScopedScopeLike> | null;\n /** Identity key for the last displaySnapshot. */\n displayKey: string | null;\n boundT: ScopedTranslateFn | null;\n boundTKey: string | null;\n}\n\n/**\n * Dedupes in-flight loads by `(engineRef, partition, namespaces)` and exposes\n * display snapshots + retry for manual pending/resolved/error rendering.\n */\nexport function createLoadCoordinator<Scope = ScopedScopeLike>(): LoadCoordinator<Scope> {\n let revision = 0;\n let nextRequestId = 0;\n const entries = new Map<string, CacheEntry<Scope>>();\n const displayByGate = new Map<string, DisplayCache>();\n const listeners = new Set<() => void>();\n let lastKey: string | null = null;\n\n function notify(): void {\n for (const listener of listeners) {\n listener();\n }\n }\n\n function bumpAndNotify(): void {\n revision += 1;\n // Invalidate display snapshots that depend on entry status.\n for (const display of displayByGate.values()) {\n display.displaySnapshot = null;\n display.displayKey = null;\n }\n notify();\n }\n\n function markResolvedSync(key: string, syncScope: Scope): void {\n const entrySnapshot: LoadCoordinatorEntry<Scope> = {\n status: \"resolved\",\n scope: syncScope,\n };\n entries.set(key, {\n status: \"resolved\",\n promise: Promise.resolve(syncScope),\n resolvedScope: syncScope,\n error: null,\n requestId: ++nextRequestId,\n entrySnapshot,\n });\n }\n\n /**\n * SSR / getServerSnapshot: hydrate from peek/state only — never kick `load()`.\n */\n function ensureSync(input: LoadCoordinatorRequest<Scope>): void {\n const key = cacheKey(input.engineRef, input.partition, input.namespaces);\n lastKey = key;\n\n if (entries.has(key)) {\n return;\n }\n\n if (input.tryResolveSync === undefined) {\n return;\n }\n\n const syncScope = input.tryResolveSync();\n if (syncScope !== null) {\n markResolvedSync(key, syncScope);\n }\n }\n\n function ensure(input: LoadCoordinatorRequest<Scope>): void {\n const key = cacheKey(input.engineRef, input.partition, input.namespaces);\n lastKey = key;\n\n const existing = entries.get(key);\n if (existing !== undefined) {\n return;\n }\n\n if (input.tryResolveSync !== undefined) {\n const syncScope = input.tryResolveSync();\n if (syncScope !== null) {\n markResolvedSync(key, syncScope);\n return;\n }\n }\n\n const requestId = ++nextRequestId;\n const promise = input.load().then(\n (scope) => {\n const entry = entries.get(key);\n if (entry === undefined || entry.requestId !== requestId) {\n return scope;\n }\n entry.status = \"resolved\";\n entry.resolvedScope = scope;\n entry.error = null;\n entry.entrySnapshot = { status: \"resolved\", scope };\n bumpAndNotify();\n return scope;\n },\n (error: unknown) => {\n const entry = entries.get(key);\n if (entry !== undefined && entry.requestId === requestId) {\n entry.status = \"error\";\n entry.error = error;\n entry.resolvedScope = null;\n entry.entrySnapshot = { status: \"error\", error };\n bumpAndNotify();\n }\n throw error;\n }\n );\n // Avoid unhandled rejection when only gate subscribers attach (no getPromise).\n void promise.catch((error: unknown) => {\n console.error(\"[i18n-react] namespace load failed:\", error);\n });\n\n entries.set(key, {\n status: \"pending\",\n promise,\n resolvedScope: null,\n error: null,\n requestId,\n entrySnapshot: { status: \"pending\" },\n });\n }\n\n function getEntry(keyInput: LoadCoordinatorKey): LoadCoordinatorEntry<Scope> {\n const key = cacheKey(keyInput.engineRef, keyInput.partition, keyInput.namespaces);\n const entry = entries.get(key);\n if (entry === undefined) {\n return { status: \"pending\" };\n }\n return entry.entrySnapshot;\n }\n\n function getOrCreateDisplay(engineRef: unknown, namespaces: readonly string[]): DisplayCache {\n const id = gateId(engineRef, namespaces);\n let display = displayByGate.get(id);\n if (display === undefined) {\n display = {\n lastReady: null,\n displaySnapshot: null,\n displayKey: null,\n boundT: null,\n boundTKey: null,\n };\n displayByGate.set(id, display);\n }\n return display;\n }\n\n function bindT(\n display: DisplayCache,\n scope: ScopedScopeLike,\n locale: string,\n namespaces: readonly string[],\n scopeToken: string\n ): ScopedTranslateFn {\n const tKey = `${scopeToken}\\0${locale}\\0${namespaces.join(\"\\0\")}`;\n if (display.boundT !== null && display.boundTKey === tKey) {\n return display.boundT;\n }\n const t = createScopedT(scope, { namespaces, locale });\n display.boundT = t;\n display.boundTKey = tKey;\n return t;\n }\n\n function getDisplayEntry(\n keyInput: LoadCoordinatorKey,\n options: GetDisplayEntryOptions\n ): LoadDisplayEntry<Scope> {\n const key = cacheKey(keyInput.engineRef, keyInput.partition, keyInput.namespaces);\n const entry = entries.get(key);\n const display = getOrCreateDisplay(keyInput.engineRef, options.namespaces);\n const keepPrevious = options.keepPrevious !== false;\n\n const retry = () => {\n retryKey(keyInput);\n };\n\n let result: LoadDisplayEntry<Scope>;\n\n if (entry === undefined || entry.status === \"pending\") {\n const kept = keepPrevious ? display.lastReady : null;\n result = {\n status: \"pending\",\n display: kept as { scope: Scope & ScopedScopeLike; locale: string } | null,\n pendingLocale: kept ? options.locale : undefined,\n t: kept\n ? bindT(display, kept.scope, kept.locale, options.namespaces, `kept:${kept.locale}`)\n : null,\n } as LoadDisplayEntry<Scope>;\n } else if (entry.status === \"resolved\") {\n const scope = entry.resolvedScope as Scope & ScopedScopeLike;\n display.lastReady = { scope, locale: options.locale };\n result = {\n status: \"ready\",\n scope,\n locale: options.locale,\n t: bindT(display, scope, options.locale, options.namespaces, `ready:${key}`),\n } as LoadDisplayEntry<Scope>;\n } else {\n const kept = keepPrevious ? display.lastReady : null;\n result = {\n status: \"error\",\n error: entry.error,\n display: kept as { scope: Scope & ScopedScopeLike; locale: string } | null,\n pendingLocale: kept ? options.locale : undefined,\n t: kept\n ? bindT(display, kept.scope, kept.locale, options.namespaces, `kept:${kept.locale}`)\n : null,\n retry,\n } as LoadDisplayEntry<Scope>;\n }\n\n const displayKey = `${key}\\0${result.status}\\0${options.locale}\\0${String(!!result.t)}`;\n if (display.displaySnapshot !== null && display.displayKey === displayKey) {\n return display.displaySnapshot as LoadDisplayEntry<Scope>;\n }\n display.displaySnapshot = result as LoadDisplayEntry<ScopedScopeLike>;\n display.displayKey = displayKey;\n return result;\n }\n\n function retryKey(keyInput: LoadCoordinatorKey): void {\n const key = cacheKey(keyInput.engineRef, keyInput.partition, keyInput.namespaces);\n if (!entries.has(key)) {\n return;\n }\n entries.delete(key);\n bumpAndNotify();\n }\n\n function subscribe(listener: () => void): () => void {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n }\n\n function getPromise(): Promise<Scope> {\n if (lastKey === null) {\n throw new Error(\"createLoadCoordinator: call ensure()/request() before getPromise()\");\n }\n const entry = entries.get(lastKey);\n if (entry === undefined) {\n throw new Error(\"createLoadCoordinator: call ensure()/request() before getPromise()\");\n }\n return entry.promise;\n }\n\n function getResolvedScope(): Scope | null {\n if (lastKey === null) {\n return null;\n }\n const entry = entries.get(lastKey);\n return entry?.status === \"resolved\" ? entry.resolvedScope : null;\n }\n\n return {\n get revision() {\n return revision;\n },\n request: ensure,\n ensure,\n ensureSync,\n getEntry,\n getDisplayEntry,\n retry: retryKey,\n subscribe,\n getPromise,\n getResolvedScope,\n };\n}\n","/**\n * Manual (non-Suspense) i18n load gate: ensure + subscribe via the load\n * coordinator, then render fallback / keep-previous / error UI / throw.\n */\nimport {\n forwardRef,\n useSyncExternalStore,\n type ForwardedRef,\n type ForwardRefExoticComponent,\n type ReactNode,\n type RefAttributes,\n} from \"react\";\nimport type {\n LoadCoordinator,\n LoadCoordinatorRequest,\n LoadDisplayEntry,\n LoadPartition,\n ScopedScopeLike,\n ScopedTranslateFn,\n} from \"./types.js\";\n\n/** Inputs for {@link useNamespaceLoad}. */\nexport interface UseNamespaceLoadInput<Scope> {\n coordinator: LoadCoordinator<Scope>;\n engineRef: unknown;\n partition: LoadPartition;\n namespaces: readonly string[];\n locale: string;\n load: () => Promise<Scope>;\n tryResolveSync?: () => Scope | null;\n keepPrevious?: boolean;\n}\n\n/**\n * Starts (or reuses) a coordinator load and re-renders when that entry settles.\n * Client getSnapshot may kick `load()`; SSR getServerSnapshot only peeks sync\n * (hydrated `state`) and never starts a client fetch during prerender.\n */\nexport function useNamespaceLoad<Scope>(\n input: UseNamespaceLoadInput<Scope>\n): LoadDisplayEntry<Scope> {\n const {\n coordinator,\n engineRef,\n partition,\n namespaces,\n locale,\n load,\n tryResolveSync,\n keepPrevious,\n } = input;\n\n const request: LoadCoordinatorRequest<Scope> = {\n engineRef,\n partition,\n namespaces,\n load,\n ...(tryResolveSync !== undefined ? { tryResolveSync } : {}),\n };\n\n const displayOptions = {\n locale,\n namespaces,\n ...(keepPrevious !== undefined ? { keepPrevious } : {}),\n };\n\n return useSyncExternalStore(\n coordinator.subscribe,\n () => {\n coordinator.ensure(request);\n return coordinator.getDisplayEntry({ engineRef, partition, namespaces }, displayOptions);\n },\n () => {\n coordinator.ensureSync(request);\n return coordinator.getDisplayEntry({ engineRef, partition, namespaces }, displayOptions);\n }\n );\n}\n\n/** Scoped `{ t, locale }` injected into gate children / HOC when ready (or kept). */\nexport interface I18nLoadGateValue {\n t: ScopedTranslateFn;\n locale: string;\n /** Set during keep-then-switch while a new partition is loading. */\n pendingLocale?: string;\n error?: unknown;\n retry?: () => void;\n}\n\n/** Load wiring from React context (coordinator, engine, partition, load). */\nexport interface I18nLoadArgs {\n coordinator: LoadCoordinator<ScopedScopeLike>;\n engineRef: unknown;\n partition: LoadPartition;\n locale: string;\n load: (namespaces: readonly string[]) => Promise<ScopedScopeLike>;\n tryResolveSync?: (namespaces: readonly string[]) => ScopedScopeLike | null;\n}\n\nexport interface CreateI18nLoadGateOptions {\n /**\n * When true (default), keep last resolved `{ t, locale }` while a new\n * partition is pending instead of showing fallback.\n */\n keepPreviousOnPartitionChange?: boolean;\n /** Hook that resolves coordinator + load inputs from React context. */\n useLoadArgs: () => I18nLoadArgs;\n}\n\nexport interface I18nGateProps {\n namespaces: readonly string[];\n /** Shown only while the load is pending (not on error). */\n fallback?: ReactNode;\n /**\n * When set, called for error with no keep-previous display.\n * Otherwise the load error is thrown for a React error boundary.\n */\n renderError?: (args: { error: unknown; retry: () => void }) => ReactNode;\n children: (value: I18nLoadGateValue) => ReactNode;\n}\n\n/** Pending UI for `withI18n` — static node or derived from own props. */\nexport type WithI18nFallback<P extends object> = ReactNode | ((props: P) => ReactNode);\n\nexport interface I18nHocOptions<P extends object = object> {\n namespaces: readonly string[];\n fallback?: WithI18nFallback<P>;\n renderError?: (args: { error: unknown; retry: () => void; props: P }) => ReactNode;\n}\n\n/** Render fn for `withI18n`: own props, i18n bag, optional forwarded ref. */\nexport type WithI18nRender<P extends object, R = never> = (\n props: P,\n i18n: I18nLoadGateValue,\n ref?: ForwardedRef<R>\n) => ReactNode;\n\nfunction resolveNamespaces(namespaces: readonly string[] | undefined): readonly string[] {\n if (namespaces === undefined || namespaces.length === 0) {\n throw new Error(\"withI18n / I18n: requires a non-empty namespaces list\");\n }\n return namespaces;\n}\n\nfunction toThrownError(error: unknown): Error {\n if (error instanceof Error) {\n return error;\n }\n if (typeof error === \"string\") {\n return new Error(error);\n }\n return new Error(\"i18n namespace load failed\");\n}\n\nconst noopT: ScopedTranslateFn = () => \"\";\n\n/** Gate value for display, or `null` when only fallback / error UI should show. */\nfunction gateValueFromEntry(entry: LoadDisplayEntry<ScopedScopeLike>): I18nLoadGateValue | null {\n if (entry.status === \"ready\") {\n return { t: entry.t, locale: entry.locale };\n }\n\n if (entry.t !== null && entry.display) {\n const value: I18nLoadGateValue = {\n t: entry.t,\n locale: entry.display.locale,\n };\n if (entry.pendingLocale !== undefined) {\n value.pendingLocale = entry.pendingLocale;\n }\n if (entry.status === \"error\") {\n value.error = entry.error;\n value.retry = entry.retry;\n }\n return value;\n }\n\n return null;\n}\n\nfunction renderDisplayEntry(\n entry: LoadDisplayEntry<ScopedScopeLike>,\n options: {\n fallback?: ReactNode;\n renderError?: (args: { error: unknown; retry: () => void }) => ReactNode;\n children: (value: I18nLoadGateValue) => ReactNode;\n }\n): ReactNode {\n if (entry.status === \"error\" && entry.t === null) {\n if (options.renderError) {\n return options.renderError({ error: entry.error, retry: entry.retry! });\n }\n throw toThrownError(entry.error);\n }\n\n const value = gateValueFromEntry(entry);\n if (value !== null) {\n return options.children(value);\n }\n\n return options.fallback ?? null;\n}\n\n/**\n * Factory for generic `I18n` gate + `withI18n` HOC over the same Outer.\n * Codegen emits typed wrappers; tests use this directly.\n */\nexport function createI18nLoadGate(options: CreateI18nLoadGateOptions): {\n I18n: (props: I18nGateProps) => ReactNode;\n withI18n: <P extends object = object, R = never>(\n hocOptions: I18nHocOptions<P>,\n render: WithI18nRender<P, R>\n ) => ForwardRefExoticComponent<P & RefAttributes<R>>;\n} {\n const { useLoadArgs } = options;\n const keepPrevious = options.keepPreviousOnPartitionChange !== false;\n\n function useGateLoad(namespaces: readonly string[]): {\n entry: LoadDisplayEntry<ScopedScopeLike>;\n locale: string;\n } {\n const resolved = resolveNamespaces(namespaces);\n const args = useLoadArgs();\n const entry = useNamespaceLoad({\n coordinator: args.coordinator,\n engineRef: args.engineRef,\n partition: args.partition,\n namespaces: resolved,\n locale: args.locale,\n load: () => args.load(resolved),\n ...(args.tryResolveSync !== undefined\n ? { tryResolveSync: () => args.tryResolveSync!(resolved) }\n : {}),\n keepPrevious,\n });\n return { entry, locale: args.locale };\n }\n\n function I18n({ namespaces, fallback, renderError, children }: I18nGateProps): ReactNode {\n const { entry } = useGateLoad(namespaces);\n return renderDisplayEntry(entry, {\n ...(fallback !== undefined ? { fallback } : {}),\n ...(renderError !== undefined ? { renderError } : {}),\n children,\n });\n }\n\n function withI18n<P extends object = object, R = never>(\n hocOptions: I18nHocOptions<P>,\n render: WithI18nRender<P, R>\n ): ForwardRefExoticComponent<P & RefAttributes<R>> {\n const { fallback, renderError } = hocOptions;\n const Outer = forwardRef<R, P>(function I18nHocOuter(props, ref) {\n const { entry, locale } = useGateLoad(hocOptions.namespaces);\n\n // Always invoke `render` so hooks inside it are Outer hooks and stay\n // unconditional across pending → ready (e.g. I18nRoot without hydrated state).\n const displayValue = gateValueFromEntry(entry);\n const value = displayValue ?? ({ t: noopT, locale } satisfies I18nLoadGateValue);\n const rendered = render(props as P, value, ref);\n\n if (entry.status === \"error\" && entry.t === null) {\n if (renderError) {\n return renderError({ error: entry.error, retry: entry.retry!, props: props as P });\n }\n throw toThrownError(entry.error);\n }\n\n if (displayValue !== null) {\n return rendered;\n }\n\n if (fallback === undefined) {\n return null;\n }\n return typeof fallback === \"function\" ? fallback(props as P) : fallback;\n });\n\n const displayName = render.name || \"Component\";\n Outer.displayName = `withI18n(${displayName})`;\n return Outer as ForwardRefExoticComponent<P & RefAttributes<R>>;\n }\n\n return { I18n, withI18n };\n}\n","/**\n * Holds the shared i18n root: handle + coordinator + active locale.\n *\n * Generated {@link useI18nRoot} casts the handle to the project's typed factory return.\n */\nimport { createContext, useContext } from \"react\";\nimport type { I18nRootContextValue } from \"./types.js\";\n\nconst I18nRootContext = createContext<I18nRootContextValue | null>(null);\n\n/** Resolves the shared root from an ancestor {@link I18nRootProvider}. */\nexport function useI18nRootContext(): I18nRootContextValue {\n const value = useContext(I18nRootContext);\n if (value === null) {\n throw new Error(\"useI18nRoot must be used within I18nRoot / I18nRootProvider\");\n }\n return value;\n}\n\n/** Internal — provider wiring. */\nexport { I18nRootContext };\n","/**\n * Single provider that shares handle + coordinator + locale across a lazy subtree.\n */\nimport { useRef, type ReactNode } from \"react\";\nimport type { FetchArtifact, I18nCreateInput, OnMissingTranslation } from \"@xndrjs/i18n\";\nimport { createLoadCoordinator } from \"./create-load-coordinator.js\";\nimport { I18nRootContext } from \"./root-context.js\";\nimport type {\n CreateI18nFactory,\n I18nHandleLike,\n I18nRootContextValue,\n ScopedScopeLike,\n} from \"./types.js\";\n\nexport interface I18nRootProviderProps {\n createI18n: CreateI18nFactory;\n locale: string;\n /**\n * Cold-start / eager dictionary seed (defaults to `{}` when neither prop is set).\n * Mutually exclusive with {@link state}.\n */\n dictionary?: Record<string, unknown>;\n /** Full create/hydrate input from `handle.serialize()` — mutually exclusive with {@link dictionary}. */\n state?: I18nCreateInput | undefined;\n onMissing?: OnMissingTranslation;\n /** Custom JSON loader for `loaderStrategy: \"fetch\"` projects (ignored by import loaders). */\n fetchImpl?: FetchArtifact;\n children: ReactNode;\n}\n\n/** Creates and shares one handle + load coordinator for descendant gates. */\nexport function I18nRootProvider({\n createI18n,\n locale,\n dictionary,\n state,\n onMissing,\n fetchImpl,\n children,\n}: I18nRootProviderProps): ReactNode {\n if (state !== undefined && dictionary !== undefined) {\n throw new Error(\"I18nRootProvider: pass either `state` or `dictionary`, not both.\");\n }\n\n const valueRef = useRef<I18nRootContextValue | null>(null);\n\n if (valueRef.current === null) {\n const seed: I18nCreateInput =\n state ?? ({ dictionary: dictionary ?? {} } satisfies I18nCreateInput);\n const handle = createI18n({\n state: seed,\n ...(onMissing !== undefined ? { onMissing } : {}),\n ...(fetchImpl !== undefined ? { fetchImpl } : {}),\n });\n valueRef.current = {\n handle: handle as I18nHandleLike,\n coordinator: createLoadCoordinator<ScopedScopeLike>(),\n locale,\n };\n } else {\n valueRef.current.locale = locale;\n }\n\n return <I18nRootContext.Provider value={valueRef.current}>{children}</I18nRootContext.Provider>;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xndrjs/i18n-react",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "React runtime primitives and codegen for @xndrjs/i18n client bindings.",
5
5
  "private": false,
6
6
  "license": "MIT",
@@ -449,6 +449,38 @@ describe("createI18nLoadGate", () => {
449
449
  expect(screen.getByTestId("child").textContent).toBe("x:Ciao");
450
450
  });
451
451
 
452
+ it("HOC render may use hooks across pending → ready (no hydrated state)", async () => {
453
+ const pending = deferred<ScopedScopeLike>();
454
+ const { withI18n } = createGateFixture({ load: () => pending.promise });
455
+
456
+ const Child = withI18n<{ label: string }>(
457
+ {
458
+ namespaces: ["default"],
459
+ fallback: <span data-testid="fallback">loading</span>,
460
+ },
461
+ function Child({ label }, { t }) {
462
+ const [n] = useState(0);
463
+ return (
464
+ <span data-testid="child">
465
+ {label}:{t("default", "greeting")}:{n}
466
+ </span>
467
+ );
468
+ }
469
+ );
470
+
471
+ render(<Child label="x" />);
472
+ expect(screen.getByTestId("fallback").textContent).toBe("loading");
473
+
474
+ const handle = createTestHandle();
475
+ const scope = await handle.load({ namespaces: ["default"], locale: "it" });
476
+ await act(async () => {
477
+ pending.resolve(asScopedScope(scope));
478
+ await pending.promise;
479
+ });
480
+
481
+ expect(screen.getByTestId("child").textContent).toBe("x:Ciao:0");
482
+ });
483
+
452
484
  it("dedupes loads across two gates sharing one coordinator", async () => {
453
485
  const handle = createTestHandle();
454
486
  const coordinator = createLoadCoordinator<ScopedScopeLike>();
@@ -152,23 +152,12 @@ function toThrownError(error: unknown): Error {
152
152
  return new Error("i18n namespace load failed");
153
153
  }
154
154
 
155
- function renderDisplayEntry(
156
- entry: LoadDisplayEntry<ScopedScopeLike>,
157
- options: {
158
- fallback?: ReactNode;
159
- renderError?: (args: { error: unknown; retry: () => void }) => ReactNode;
160
- children: (value: I18nLoadGateValue) => ReactNode;
161
- }
162
- ): ReactNode {
163
- if (entry.status === "ready") {
164
- return options.children({ t: entry.t, locale: entry.locale });
165
- }
155
+ const noopT: ScopedTranslateFn = () => "";
166
156
 
167
- if (entry.status === "error" && entry.t === null) {
168
- if (options.renderError) {
169
- return options.renderError({ error: entry.error, retry: entry.retry! });
170
- }
171
- throw toThrownError(entry.error);
157
+ /** Gate value for display, or `null` when only fallback / error UI should show. */
158
+ function gateValueFromEntry(entry: LoadDisplayEntry<ScopedScopeLike>): I18nLoadGateValue | null {
159
+ if (entry.status === "ready") {
160
+ return { t: entry.t, locale: entry.locale };
172
161
  }
173
162
 
174
163
  if (entry.t !== null && entry.display) {
@@ -183,6 +172,29 @@ function renderDisplayEntry(
183
172
  value.error = entry.error;
184
173
  value.retry = entry.retry;
185
174
  }
175
+ return value;
176
+ }
177
+
178
+ return null;
179
+ }
180
+
181
+ function renderDisplayEntry(
182
+ entry: LoadDisplayEntry<ScopedScopeLike>,
183
+ options: {
184
+ fallback?: ReactNode;
185
+ renderError?: (args: { error: unknown; retry: () => void }) => ReactNode;
186
+ children: (value: I18nLoadGateValue) => ReactNode;
187
+ }
188
+ ): ReactNode {
189
+ if (entry.status === "error" && entry.t === null) {
190
+ if (options.renderError) {
191
+ return options.renderError({ error: entry.error, retry: entry.retry! });
192
+ }
193
+ throw toThrownError(entry.error);
194
+ }
195
+
196
+ const value = gateValueFromEntry(entry);
197
+ if (value !== null) {
186
198
  return options.children(value);
187
199
  }
188
200
 
@@ -203,10 +215,13 @@ export function createI18nLoadGate(options: CreateI18nLoadGateOptions): {
203
215
  const { useLoadArgs } = options;
204
216
  const keepPrevious = options.keepPreviousOnPartitionChange !== false;
205
217
 
206
- function useGateEntry(namespaces: readonly string[]): LoadDisplayEntry<ScopedScopeLike> {
218
+ function useGateLoad(namespaces: readonly string[]): {
219
+ entry: LoadDisplayEntry<ScopedScopeLike>;
220
+ locale: string;
221
+ } {
207
222
  const resolved = resolveNamespaces(namespaces);
208
223
  const args = useLoadArgs();
209
- return useNamespaceLoad({
224
+ const entry = useNamespaceLoad({
210
225
  coordinator: args.coordinator,
211
226
  engineRef: args.engineRef,
212
227
  partition: args.partition,
@@ -218,10 +233,11 @@ export function createI18nLoadGate(options: CreateI18nLoadGateOptions): {
218
233
  : {}),
219
234
  keepPrevious,
220
235
  });
236
+ return { entry, locale: args.locale };
221
237
  }
222
238
 
223
239
  function I18n({ namespaces, fallback, renderError, children }: I18nGateProps): ReactNode {
224
- const entry = useGateEntry(namespaces);
240
+ const { entry } = useGateLoad(namespaces);
225
241
  return renderDisplayEntry(entry, {
226
242
  ...(fallback !== undefined ? { fallback } : {}),
227
243
  ...(renderError !== undefined ? { renderError } : {}),
@@ -235,25 +251,29 @@ export function createI18nLoadGate(options: CreateI18nLoadGateOptions): {
235
251
  ): ForwardRefExoticComponent<P & RefAttributes<R>> {
236
252
  const { fallback, renderError } = hocOptions;
237
253
  const Outer = forwardRef<R, P>(function I18nHocOuter(props, ref) {
238
- const entry = useGateEntry(hocOptions.namespaces);
254
+ const { entry, locale } = useGateLoad(hocOptions.namespaces);
255
+
256
+ // Always invoke `render` so hooks inside it are Outer hooks and stay
257
+ // unconditional across pending → ready (e.g. I18nRoot without hydrated state).
258
+ const displayValue = gateValueFromEntry(entry);
259
+ const value = displayValue ?? ({ t: noopT, locale } satisfies I18nLoadGateValue);
260
+ const rendered = render(props as P, value, ref);
261
+
262
+ if (entry.status === "error" && entry.t === null) {
263
+ if (renderError) {
264
+ return renderError({ error: entry.error, retry: entry.retry!, props: props as P });
265
+ }
266
+ throw toThrownError(entry.error);
267
+ }
239
268
 
240
- const needsFallback = entry.status === "pending" && entry.t === null;
241
- const resolvedFallback =
242
- !needsFallback || fallback === undefined
243
- ? undefined
244
- : typeof fallback === "function"
245
- ? fallback(props as P)
246
- : fallback;
269
+ if (displayValue !== null) {
270
+ return rendered;
271
+ }
247
272
 
248
- return renderDisplayEntry(entry, {
249
- ...(resolvedFallback !== undefined ? { fallback: resolvedFallback } : {}),
250
- ...(renderError !== undefined
251
- ? {
252
- renderError: ({ error, retry }) => renderError({ error, retry, props: props as P }),
253
- }
254
- : {}),
255
- children: (value) => render(props as P, value, ref),
256
- });
273
+ if (fallback === undefined) {
274
+ return null;
275
+ }
276
+ return typeof fallback === "function" ? fallback(props as P) : fallback;
257
277
  });
258
278
 
259
279
  const displayName = render.name || "Component";
@@ -1,4 +1,4 @@
1
- import { act } from "react";
1
+ import { act, useState } from "react";
2
2
  import { createI18nHandle, IcuTranslationProviderMulti } from "@xndrjs/i18n";
3
3
  import { render, screen } from "@testing-library/react";
4
4
  import { describe, expect, it } from "vitest";
@@ -85,4 +85,35 @@ describe("runtime primitives", () => {
85
85
  )
86
86
  ).toThrow(/pass either `state` or `dictionary`/);
87
87
  });
88
+
89
+ it("withI18n allows hooks in render when I18nRoot has no hydrated state", async () => {
90
+ const { withI18n } = createDefaultLoadGate();
91
+
92
+ const Child = withI18n<{ label: string }>(
93
+ {
94
+ namespaces: ["default"],
95
+ fallback: <span data-testid="fallback">loading</span>,
96
+ },
97
+ function Child({ label }, { t }) {
98
+ const [n] = useState(0);
99
+ return (
100
+ <span data-testid="child">
101
+ {label}:{t("default", "greeting")}:{n}
102
+ </span>
103
+ );
104
+ }
105
+ );
106
+
107
+ await act(async () => {
108
+ render(
109
+ <I18nRootProvider createI18n={createTestI18n} locale="it">
110
+ <Child label="x" />
111
+ </I18nRootProvider>
112
+ );
113
+ });
114
+
115
+ expect(await screen.findByTestId("child")).toBeTruthy();
116
+ expect(screen.getByTestId("child").textContent).toBe("x:Ciao:0");
117
+ expect(screen.queryByTestId("fallback")).toBeNull();
118
+ });
88
119
  });