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.

134 lines (133 loc) 5.43 kB
/* * 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 __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; }; import * as React from 'react'; import { Provider } from './injectIntl'; import { DEFAULT_INTL_CONFIG, invariantIntlContext, assignUniqueKeysToParts, } from '../utils'; import { formatMessage as coreFormatMessage, createIntl as coreCreateIntl, createIntlCache, } from '@formatjs/intl'; import * as shallowEquals_ from 'shallow-equal/objects'; import { isFormatXMLElementFn, } from '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] = isFormatXMLElementFn(v) ? assignUniqueKeysToParts(v) : v; return acc; }, {}); } var formatMessage = function (config, formatters, descriptor, rawValues) { var values = assignUniqueKeysToFormatXMLElementFnArgument(rawValues); var chunks = coreFormatMessage(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 */ export var createIntl = function (_a, cache) { var rawDefaultRichTextElements = _a.defaultRichTextElements, config = __rest(_a, ["defaultRichTextElements"]); var defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements); var coreIntl = coreCreateIntl(__assign(__assign({}, 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 = 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 () { invariantIntlContext(this.state.intl); return React.createElement(Provider, { value: this.state.intl }, this.props.children); }; IntlProvider.displayName = 'IntlProvider'; IntlProvider.defaultProps = DEFAULT_INTL_CONFIG; return IntlProvider; }(React.PureComponent)); export default IntlProvider;