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.

100 lines (99 loc) 4.71 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 __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; }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var injectIntl_1 = require("./injectIntl"); var utils_1 = require("../utils"); var shallowEquals_ = __importStar(require("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(injectIntl_1.Context.Consumer, null, function (intl) { utils_1.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)); exports.default = FormattedMessage;