import { IntlFormatters, IntlShape, MessageDescriptor } from "@formatjs/intl"; import { type FormatXMLElementFn, type Options as IntlMessageFormatOptions, type PrimitiveType } from "intl-messageformat"; import { ReactNode } from "react"; type BaseIntl = Pick & Omit, "formatMessage">; /** * Gives access to the package's i18n messages for the current locale. * * See also https://formatjs.io/docs/intl */ export type PackageIntl = BaseIntl & PackageIntlExtensions; /** * Trails specific extensions to FormatJS' intl API. */ export interface PackageIntlExtensions { /** * Formats a message with the given descriptor and values. * * This is a method directly implemented by FormatJS see * - [Documentation]() * - {@link IntlShape.formatMessage | Base Implementation} * * The typings here make the signature a little bit stricter: only primitive values * are allowed for formatting. * * If you need rich text (react) formatting, see {@link PackageIntlExtensions.formatRichMessage}. */ formatMessage(this: void, descriptor: MessageDescriptor, values?: Record>, opts?: IntlMessageFormatOptions): string; /** * Similar to `formatMessage()`, but supports _rich text formatting_. * * React nodes are supported as input values, and custom tags can be implemented as react components. * Note that the output is always a single react node. * * *Example with react node as value:* * * ```tsx * function Example() { * const intl = useIntl(); * // Given i18n message "Hello, {name}!" * // replaces 'name' with the given react node: * const message = intl.formatRichMessage({ id: "foo"}, { * name: * }); * return {message}; * } * ``` * * *Example with basic formatting:* * * ```tsx * function Example() { * const intl = useIntl(); * // Given i18n message "Hello, {name}!" * // renders with actual html node: * return {intl.formatRichMessage({ id: "foo"}, { name: "User" })}; * } * ``` * * Note that only a few basic formatting tags are predefined ("b", "strong", "i", "em", "code", "br"). * If you need more advanced tags, you can define your own, see below. * * *Example with custom tag:* * * ```tsx * function Example() { * const intl = useIntl(); * // Given i18n message "Open the door!", * // renders 'foo' using the formatter function below: * const message = intl.formatRichMessage({ id: "foo"}, { * foo: (parts) => {parts} * }); * return {message}; * } * ``` */ formatRichMessage: (descriptor: MessageDescriptor, values?: Record, opts?: IntlMessageFormatOptions) => ReactNode; } /** * Value types supported when rendering rich text messages. * Primitive values (or single react nodes) are used for normal values. * Functions are used to define how tags should be rendered. * * See {@link PackageIntlExtensions.formatRichMessage} */ export type RichTextValue = PrimitiveType | ReactNode | FormatXMLElementFn; /** * Creates a package intl instance with the given locale and messages. * * @internal */ export declare function createPackageIntl(locale: string, messages: Record, options?: { /** Used during testing to suppress console messages. */ suppressNotFoundError?: boolean; /** Also used during testing */ defaultLocale?: string; }): PackageIntl; /** Creates an empty i18n instance, e.g. for tests. */ export declare function createEmptyPackageIntl(locale?: string): PackageIntl; export {};