1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | exports.createIntl = void 0;
|
9 | var tslib_1 = require("tslib");
|
10 | var React = (0, tslib_1.__importStar)(require("react"));
|
11 | var injectIntl_1 = require("./injectIntl");
|
12 | var utils_1 = require("../utils");
|
13 | var intl_1 = require("@formatjs/intl");
|
14 | var intl_messageformat_1 = require("intl-messageformat");
|
15 | function 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 | }
|
30 | function 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 | }
|
42 | var 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 |
|
59 |
|
60 |
|
61 |
|
62 | var 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 | };
|
77 | exports.createIntl = createIntl;
|
78 | var IntlProvider = (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));
|
109 | exports.default = IntlProvider;
|