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.

156 lines (155 loc) 6.37 kB
"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); }; 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createIntl = void 0; var React = __importStar(require("react")); var injectIntl_1 = require("./injectIntl"); var utils_1 = require("../utils"); var intl_1 = require("@formatjs/intl"); var shallowEquals_ = __importStar(require("shallow-equal/objects")); var intl_messageformat_1 = require("intl-messageformat"); 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, wrapRichTextChunksInFragment: config.wrapRichTextChunksInFragment, defaultRichTextElements: config.defaultRichTextElements, }; } function assignUniqueKeysToFormatXMLElementFnArgument(values) { if (!values) { return values; } return Object.keys(values).reduce(function (acc, k) { var v = values[k]; acc[k] = intl_messageformat_1.isFormatXMLElementFn(v) ? utils_1.assignUniqueKeysToParts(v) : v; return acc; }, {}); } var formatMessage = function (config, formatters, descriptor, rawValues) { var values = assignUniqueKeysToFormatXMLElementFnArgument(rawValues); var chunks = intl_1.formatMessage(config, formatters, descriptor, values); if (Array.isArray(chunks)) { return React.Children.toArray(chunks); } return chunks; }; /** * Create intl object * @param config intl config * @param cache cache for formatter instances to prevent memory leak */ exports.createIntl = function (_a, cache) { var rawDefaultRichTextElements = _a.defaultRichTextElements, config = __rest(_a, ["defaultRichTextElements"]); var defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements); var coreIntl = intl_1.createIntl(__assign(__assign({}, utils_1.DEFAULT_INTL_CONFIG), config), cache); return __assign(__assign({}, coreIntl), { formatMessage: formatMessage.bind(null, { locale: coreIntl.locale, timeZone: coreIntl.timeZone, formats: coreIntl.formats, defaultLocale: coreIntl.defaultLocale, defaultFormats: coreIntl.defaultFormats, messages: coreIntl.messages, onError: coreIntl.onError, defaultRichTextElements: defaultRichTextElements, }, coreIntl.formatters) }); }; var IntlProvider = /** @class */ (function (_super) { __extends(IntlProvider, _super); function IntlProvider() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.cache = intl_1.createIntlCache(); _this.state = { cache: _this.cache, intl: exports.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: exports.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;