UNPKG

react-intl

Version:

Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.

92 lines (91 loc) 3.28 kB
import { DEFAULT_INTL_CONFIG, createIntl as createIntl$1, createIntlCache, formatMessage } from "@formatjs/intl"; import { isFormatXMLElementFn } from "intl-messageformat"; import * as React from "react"; import { jsx } from "react/jsx-runtime"; //#region packages/react-intl/utils.tsx const DEFAULT_INTL_CONFIG$1 = { ...DEFAULT_INTL_CONFIG, textComponent: React.Fragment }; /** * Builds an array of {@link React.ReactNode}s with index-based keys, similar to * {@link React.Children.toArray}. However, this function tells React that it * was intentional, so they won't produce a bunch of warnings about it. * * React doesn't recommend doing this because it makes reordering inefficient, * but we mostly need this for message chunks, which don't tend to reorder to * begin with. * */ const toKeyedReactNodeArray = (children) => { return React.Children.toArray(children).map((child, index) => { if (React.isValidElement(child)) return /* @__PURE__ */ jsx(React.Fragment, { children: child }, index); return child; }); }; /** * Takes a `formatXMLElementFn`, and composes it in function, which passes * argument `parts` through, assigning unique key to each part, to prevent * "Each child in a list should have a unique "key"" React error. * @param formatXMLElementFn */ function assignUniqueKeysToParts(formatXMLElementFn) { return function(parts) { return formatXMLElementFn(toKeyedReactNodeArray(parts)); }; } //#endregion //#region packages/react-intl/components/createIntl.ts function assignUniqueKeysToFormatXMLElementFnArgument(values) { if (!values) return values; return Object.keys(values).reduce((acc, k) => { const v = values[k]; acc[k] = isFormatXMLElementFn(v) ? assignUniqueKeysToParts(v) : v; return acc; }, {}); } const formatMessage$1 = (config, formatters, descriptor, rawValues, ...rest) => { const chunks = formatMessage(config, formatters, descriptor, assignUniqueKeysToFormatXMLElementFnArgument(rawValues), ...rest); if (Array.isArray(chunks)) return toKeyedReactNodeArray(chunks); return chunks; }; /** * Create intl object * @param config intl config * @param cache cache for formatter instances to prevent memory leak */ const createIntl = ({ defaultRichTextElements: rawDefaultRichTextElements, ...config }, cache) => { const defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements); const coreIntl = createIntl$1({ ...DEFAULT_INTL_CONFIG$1, ...config, defaultRichTextElements }, cache); const resolvedConfig = { locale: coreIntl.locale, timeZone: coreIntl.timeZone, fallbackOnEmptyString: coreIntl.fallbackOnEmptyString, formats: coreIntl.formats, defaultLocale: coreIntl.defaultLocale, defaultFormats: coreIntl.defaultFormats, messages: coreIntl.messages, onError: coreIntl.onError, defaultRichTextElements }; return { ...coreIntl, formatMessage: formatMessage$1.bind(null, resolvedConfig, coreIntl.formatters), $t: formatMessage$1.bind(null, resolvedConfig, coreIntl.formatters) }; }; //#endregion //#region packages/react-intl/server.ts function defineMessages(msgs) { return msgs; } function defineMessage(msg) { return msg; } //#endregion export { createIntl, createIntlCache, defineMessage, defineMessages }; //# sourceMappingURL=server.js.map