UNPKG

4.46 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2015, Yahoo Inc.
4 * Copyrights licensed under the New BSD License.
5 * See the accompanying LICENSE file for terms.
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.createIntl = void 0;
9var tslib_1 = require("tslib");
10var React = (0, tslib_1.__importStar)(require("react"));
11var injectIntl_1 = require("./injectIntl");
12var utils_1 = require("../utils");
13var intl_1 = require("@formatjs/intl");
14var intl_messageformat_1 = require("intl-messageformat");
15function processIntlConfig(config) {
16 return {
17 locale: config.locale,
18 timeZone: config.timeZone,
19 fallbackOnEmptyString: config.fallbackOnEmptyString,
20 formats: config.formats,
21 textComponent: config.textComponent,
22 messages: config.messages,
23 defaultLocale: config.defaultLocale,
24 defaultFormats: config.defaultFormats,
25 onError: config.onError,
26 wrapRichTextChunksInFragment: config.wrapRichTextChunksInFragment,
27 defaultRichTextElements: config.defaultRichTextElements,
28 };
29}
30function assignUniqueKeysToFormatXMLElementFnArgument(values) {
31 if (!values) {
32 return values;
33 }
34 return Object.keys(values).reduce(function (acc, k) {
35 var v = values[k];
36 acc[k] = (0, intl_messageformat_1.isFormatXMLElementFn)(v)
37 ? (0, utils_1.assignUniqueKeysToParts)(v)
38 : v;
39 return acc;
40 }, {});
41}
42var formatMessage = function (config, formatters, descriptor, rawValues) {
43 var rest = [];
44 for (var _i = 4; _i < arguments.length; _i++) {
45 rest[_i - 4] = arguments[_i];
46 }
47 var values = assignUniqueKeysToFormatXMLElementFnArgument(rawValues);
48 var chunks = intl_1.formatMessage.apply(void 0, (0, tslib_1.__spreadArray)([config,
49 formatters,
50 descriptor,
51 values], rest, false));
52 if (Array.isArray(chunks)) {
53 return React.Children.toArray(chunks);
54 }
55 return chunks;
56};
57/**
58 * Create intl object
59 * @param config intl config
60 * @param cache cache for formatter instances to prevent memory leak
61 */
62var createIntl = function (_a, cache) {
63 var rawDefaultRichTextElements = _a.defaultRichTextElements, config = (0, tslib_1.__rest)(_a, ["defaultRichTextElements"]);
64 var defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements);
65 var coreIntl = (0, intl_1.createIntl)((0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, utils_1.DEFAULT_INTL_CONFIG), config), { defaultRichTextElements: defaultRichTextElements }), cache);
66 return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, coreIntl), { formatMessage: formatMessage.bind(null, {
67 locale: coreIntl.locale,
68 timeZone: coreIntl.timeZone,
69 formats: coreIntl.formats,
70 defaultLocale: coreIntl.defaultLocale,
71 defaultFormats: coreIntl.defaultFormats,
72 messages: coreIntl.messages,
73 onError: coreIntl.onError,
74 defaultRichTextElements: defaultRichTextElements,
75 }, coreIntl.formatters) });
76};
77exports.createIntl = createIntl;
78var IntlProvider = /** @class */ (function (_super) {
79 (0, tslib_1.__extends)(IntlProvider, _super);
80 function IntlProvider() {
81 var _this = _super !== null && _super.apply(this, arguments) || this;
82 _this.cache = (0, intl_1.createIntlCache)();
83 _this.state = {
84 cache: _this.cache,
85 intl: (0, exports.createIntl)(processIntlConfig(_this.props), _this.cache),
86 prevConfig: processIntlConfig(_this.props),
87 };
88 return _this;
89 }
90 IntlProvider.getDerivedStateFromProps = function (props, _a) {
91 var prevConfig = _a.prevConfig, cache = _a.cache;
92 var config = processIntlConfig(props);
93 if (!(0, utils_1.shallowEqual)(prevConfig, config)) {
94 return {
95 intl: (0, exports.createIntl)(config, cache),
96 prevConfig: config,
97 };
98 }
99 return null;
100 };
101 IntlProvider.prototype.render = function () {
102 (0, utils_1.invariantIntlContext)(this.state.intl);
103 return React.createElement(injectIntl_1.Provider, { value: this.state.intl }, this.props.children);
104 };
105 IntlProvider.displayName = 'IntlProvider';
106 IntlProvider.defaultProps = utils_1.DEFAULT_INTL_CONFIG;
107 return IntlProvider;
108}(React.PureComponent));
109exports.default = IntlProvider;