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.
43 lines (42 loc) • 1.77 kB
JavaScript
import * as hoistNonReactStaticsNs from "hoist-non-react-statics";
import * as React from "react";
import "../types.js";
import { invariantIntlContext } from "../utils.js";
import { jsx as _jsx } from "react/jsx-runtime";
const hoistNonReactStatics = hoistNonReactStaticsNs.default ?? hoistNonReactStaticsNs;
function getDisplayName(Component) {
return Component.displayName || Component.name || "Component";
}
// This is primarily dealing with packaging systems where multiple copies of react-intl
// might exist
const IntlContext = typeof window !== "undefined" && !window.__REACT_INTL_BYPASS_GLOBAL_CONTEXT__ ? window.__REACT_INTL_CONTEXT__ || (window.__REACT_INTL_CONTEXT__ = React.createContext(null)) : React.createContext(null);
const { Consumer: IntlConsumer, Provider: IntlProvider } = IntlContext;
export const Provider = IntlProvider;
export const Context = IntlContext;
export default function injectIntl(WrappedComponent, options) {
const { intlPropName = "intl", forwardRef = false, enforceContext = true } = options || {};
const WithIntl = (props) => /* @__PURE__ */ _jsx(IntlConsumer, { children: (intl) => {
if (enforceContext) {
invariantIntlContext(intl);
}
const intlProp = { [intlPropName]: intl };
return /* @__PURE__ */ _jsx(WrappedComponent, {
...props,
...intlProp,
ref: forwardRef ? props.forwardedRef : null
});
} });
WithIntl.displayName = `injectIntl(${getDisplayName(WrappedComponent)})`;
WithIntl.WrappedComponent = WrappedComponent;
if (forwardRef) {
return hoistNonReactStatics(
// @ts-expect-error
React.forwardRef((props, ref) => /* @__PURE__ */ _jsx(WithIntl, {
...props,
forwardedRef: ref
})),
WrappedComponent
);
}
return hoistNonReactStatics(WithIntl, WrappedComponent);
}