"use strict"; /* * Copyright 2015, Yahoo Inc. * Copyrights licensed under the New BSD License. * See the accompanying LICENSE file for terms. */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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); }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var injectIntl_1 = require("./injectIntl"); var utils_1 = require("../utils"); var number_1 = require("../formatters/number"); var relativeTime_1 = require("../formatters/relativeTime"); var dateTime_1 = require("../formatters/dateTime"); var plural_1 = require("../formatters/plural"); var message_1 = require("../formatters/message"); var shallowEquals_ = require("shallow-equal/objects"); var list_1 = require("../formatters/list"); var displayName_1 = require("../formatters/displayName"); var error_1 = require("../error"); var shallowEquals = shallowEquals_.default || shallowEquals_; function processIntlConfig(config) { return { locale: config.locale, timeZone: config.timeZone, formats: config.formats, textComponent: config.textComponent, messages: config.messages, defaultLocale: config.defaultLocale, defaultFormats: config.defaultFormats, onError: config.onError, }; } /** * Create intl object * @param config intl config * @param cache cache for formatter instances to prevent memory leak */ function createIntl(config, cache) { var formatters = utils_1.createFormatters(cache); var resolvedConfig = __assign(__assign({}, utils_1.DEFAULT_INTL_CONFIG), config); var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError; if (!locale) { if (onError) { onError(new error_1.ReactIntlError("INVALID_CONFIG" /* INVALID_CONFIG */, "\"locale\" was not configured, using \"" + defaultLocale + "\" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/API.md#intlshape for more details")); } // Since there's no registered locale data for `locale`, this will // fallback to the `defaultLocale` to make sure things can render. // The `messages` are overridden to the `defaultProps` empty object // to maintain referential equality across re-renders. It's assumed // each contains a `defaultMessage` prop. resolvedConfig.locale = resolvedConfig.defaultLocale || 'en'; } else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) { onError(new error_1.ReactIntlError("MISSING_DATA" /* MISSING_DATA */, "Missing locale data for locale: \"" + locale + "\" in Intl.NumberFormat. Using default locale: \"" + defaultLocale + "\" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/Getting-Started.md#runtime-requirements for more details")); } else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length && onError) { onError(new error_1.ReactIntlError("MISSING_DATA" /* MISSING_DATA */, "Missing locale data for locale: \"" + locale + "\" in Intl.DateTimeFormat. Using default locale: \"" + defaultLocale + "\" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/Getting-Started.md#runtime-requirements for more details")); } return __assign(__assign({}, resolvedConfig), { formatters: formatters, formatNumber: number_1.formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: number_1.formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: relativeTime_1.formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: dateTime_1.formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: dateTime_1.formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: dateTime_1.formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: displayName_1.formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) }); } exports.createIntl = createIntl; var IntlProvider = /** @class */ (function (_super) { __extends(IntlProvider, _super); function IntlProvider() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.cache = utils_1.createIntlCache(); _this.state = { cache: _this.cache, intl: createIntl(processIntlConfig(_this.props), _this.cache), prevConfig: processIntlConfig(_this.props), }; return _this; } IntlProvider.getDerivedStateFromProps = function (props, _a) { var prevConfig = _a.prevConfig, cache = _a.cache; var config = processIntlConfig(props); if (!shallowEquals(prevConfig, config)) { return { intl: createIntl(config, cache), prevConfig: config, }; } return null; }; IntlProvider.prototype.render = function () { utils_1.invariantIntlContext(this.state.intl); return React.createElement(injectIntl_1.Provider, { value: this.state.intl }, this.props.children); }; IntlProvider.displayName = 'IntlProvider'; IntlProvider.defaultProps = utils_1.DEFAULT_INTL_CONFIG; return IntlProvider; }(React.PureComponent)); exports.default = IntlProvider;