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.

71 lines (70 loc) 3.43 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = exports.Provider = void 0; var React = __importStar(require("react")); var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics")); // Since rollup cannot deal with namespace being a function, // this is to interop with TypeScript since `invariant` // does not export a default // https://github.com/rollup/rollup/issues/1267 var hoistNonReactStatics = hoist_non_react_statics_1.default.default || hoist_non_react_statics_1.default; var utils_1 = require("../utils"); function getDisplayName(Component) { return Component.displayName || Component.name || 'Component'; } // TODO: We should provide initial value here var IntlContext = React.createContext(null); var IntlConsumer = IntlContext.Consumer, IntlProvider = IntlContext.Provider; exports.Provider = IntlProvider; exports.Context = IntlContext; function injectIntl(WrappedComponent, options) { var _a = options || {}, _b = _a.intlPropName, intlPropName = _b === void 0 ? 'intl' : _b, _c = _a.forwardRef, forwardRef = _c === void 0 ? false : _c, _d = _a.enforceContext, enforceContext = _d === void 0 ? true : _d; var WithIntl = function (props) { return (React.createElement(IntlConsumer, null, function (intl) { var _a; if (enforceContext) { utils_1.invariantIntlContext(intl); } var intlProp = (_a = {}, _a[intlPropName] = intl, _a); return (React.createElement(WrappedComponent, __assign({}, props, intlProp, { ref: forwardRef ? props.forwardedRef : null }))); })); }; WithIntl.displayName = "injectIntl(" + getDisplayName(WrappedComponent) + ")"; WithIntl.WrappedComponent = WrappedComponent; if (forwardRef) { return hoistNonReactStatics(React.forwardRef(function (props, ref) { return (React.createElement(WithIntl, __assign({}, props, { forwardedRef: ref }))); }), WrappedComponent); } return hoistNonReactStatics(WithIntl, WrappedComponent); } exports.default = injectIntl;