Trails Packages
    Preparing search index...

    Interface PackageIntlExtensions

    Trails specific extensions to FormatJS' intl API.

    interface PackageIntlExtensions {
        formatRichMessage: (
            descriptor: MessageDescriptor,
            values?: Record<string, RichTextValue>,
            opts?: Options,
        ) => ReactNode;
        formatMessage(
            this: void,
            descriptor: MessageDescriptor,
            values?: Record<
                string,
                PrimitiveType
                | FormatXMLElementFn<string, string>,
            >,
            opts?: Options,
        ): string;
    }
    Index

    Properties

    formatRichMessage: (
        descriptor: MessageDescriptor,
        values?: Record<string, RichTextValue>,
        opts?: Options,
    ) => ReactNode

    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:

    function Example() {
    const intl = useIntl();
    // Given i18n message "Hello, {name}!"
    // replaces 'name' with the given react node:
    const message = intl.formatRichMessage({ id: "foo"}, {
    name: <FancyUserName />
    });
    return <Box>{message}</Box>;
    }

    Example with basic formatting:

    function Example() {
    const intl = useIntl();
    // Given i18n message "Hello, <strong>{name}</strong>!"
    // renders with actual <strong> html node:
    return <Box>{intl.formatRichMessage({ id: "foo"}, { name: "User" })}</Box>;
    }

    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:

    function Example() {
    const intl = useIntl();
    // Given i18n message "Open <foo>the door</foo>!",
    // renders 'foo' using the formatter function below:
    const message = intl.formatRichMessage({ id: "foo"}, {
    foo: (parts) => <FancyTag>{parts}</FancyTag>
    });
    return <Box>{message}</Box>;
    }

    Methods

    • Formats a message with the given descriptor and values.

      This is a method directly implemented by FormatJS see

      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 PackageIntlExtensions.formatRichMessage.

      Parameters

      • this: void
      • descriptor: MessageDescriptor
      • Optionalvalues: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>
      • Optionalopts: Options

      Returns string