import { createElement, useContext, ComponentType, ReactElement } from "react"; import { FluentContext } from "./context.js"; import { FluentVariable } from "@fluent/bundle"; export interface WithLocalizationProps { getString( id: string, args?: Record | null, fallback?: string ): string; } export function withLocalization

( Inner: ComponentType

): ComponentType< Omit & Partial > { function WithLocalization( props: Omit & Partial ): ReactElement { const l10n = useContext(FluentContext); if (!l10n) { throw new Error( "withLocalization was used without wrapping it in a " + "." ); } // Re-bind getString to trigger a re-render of Inner. const getString = l10n.getString.bind(l10n); return createElement(Inner, { getString, ...props } as P); } WithLocalization.displayName = `WithLocalization(${displayName(Inner)})`; return WithLocalization; } function displayName

(component: ComponentType

): string { return component.displayName || component.name || "Component"; }