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.

79 lines (78 loc) 3.75 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 __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; }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; import * as React from 'react'; import { Context } from './injectIntl'; import { invariantIntlContext } from '../utils'; import * as shallowEquals_ from 'shallow-equal/objects'; var shallowEquals = shallowEquals_.default || shallowEquals_; var FormattedMessage = /** @class */ (function (_super) { __extends(FormattedMessage, _super); function FormattedMessage() { return _super !== null && _super.apply(this, arguments) || this; } FormattedMessage.prototype.shouldComponentUpdate = function (nextProps) { var _a = this.props, values = _a.values, otherProps = __rest(_a, ["values"]); var nextValues = nextProps.values, nextOtherProps = __rest(nextProps, ["values"]); return (!shallowEquals(nextValues, values) || !shallowEquals(otherProps, nextOtherProps)); }; FormattedMessage.prototype.render = function () { var _this = this; return (React.createElement(Context.Consumer, null, function (intl) { invariantIntlContext(intl); var formatMessage = intl.formatMessage, _a = intl.textComponent, Text = _a === void 0 ? React.Fragment : _a; var _b = _this.props, id = _b.id, description = _b.description, defaultMessage = _b.defaultMessage, values = _b.values, children = _b.children, _c = _b.tagName, Component = _c === void 0 ? Text : _c; var descriptor = { id: id, description: description, defaultMessage: defaultMessage }; var nodes = formatMessage(descriptor, values); if (!Array.isArray(nodes)) { nodes = [nodes]; } if (typeof children === 'function') { return children(nodes); } if (Component) { // Needs to use `createElement()` instead of JSX, otherwise React will // warn about a missing `key` prop with rich-text message formatting. return React.createElement.apply(React, __spreadArrays([Component, null], nodes)); } return nodes; })); }; FormattedMessage.displayName = 'FormattedMessage'; return FormattedMessage; }(React.Component)); export default FormattedMessage;