react-intl 3.12.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/dist/components/message.d.ts +1 -1
- package/dist/components/message.js +2 -2
- package/dist/components/plural.d.ts +1 -1
- package/dist/components/provider.js +13 -8
- package/dist/error.d.ts +11 -0
- package/dist/error.js +28 -0
- package/dist/formatters/dateTime.js +5 -4
- package/dist/formatters/displayName.js +4 -2
- package/dist/formatters/list.js +4 -2
- package/dist/formatters/message.d.ts +1 -2
- package/dist/formatters/message.js +17 -34
- package/dist/formatters/number.js +3 -2
- package/dist/formatters/plural.js +4 -2
- package/dist/formatters/relativeTime.js +4 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -4
- package/dist/react-intl.api.md +29 -22
- package/dist/react-intl.d.ts +77 -36
- package/dist/react-intl.js +6267 -5860
- package/dist/react-intl.js.map +1 -1
- package/dist/react-intl.min.js +1 -1
- package/dist/react-intl.min.js.map +1 -1
- package/dist/types.d.ts +5 -5
- package/dist/utils.d.ts +3 -4
- package/dist/utils.js +2 -18
- package/lib/components/message.d.ts +1 -1
- package/lib/components/message.js +2 -2
- package/lib/components/plural.d.ts +1 -1
- package/lib/components/provider.js +15 -10
- package/lib/error.d.ts +11 -0
- package/lib/error.js +9 -0
- package/lib/formatters/dateTime.js +6 -5
- package/lib/formatters/displayName.js +6 -4
- package/lib/formatters/list.js +6 -4
- package/lib/formatters/message.d.ts +1 -2
- package/lib/formatters/message.js +17 -31
- package/lib/formatters/number.js +4 -3
- package/lib/formatters/plural.js +6 -4
- package/lib/formatters/relativeTime.js +6 -4
- package/lib/index.d.ts +2 -2
- package/lib/index.js +4 -2
- package/lib/react-intl.d.ts +21 -17
- package/lib/tsdoc-metadata.json +1 -1
- package/lib/types.d.ts +5 -5
- package/lib/utils.d.ts +3 -4
- package/lib/utils.js +2 -16
- package/package.json +45 -47
- package/src/components/message.tsx +6 -5
- package/src/components/provider.tsx +25 -14
- package/src/error.ts +18 -0
- package/src/formatters/dateTime.ts +30 -5
- package/src/formatters/displayName.ts +15 -4
- package/src/formatters/list.ts +15 -4
- package/src/formatters/message.ts +49 -66
- package/src/formatters/number.ts +16 -3
- package/src/formatters/plural.ts +15 -4
- package/src/formatters/relativeTime.ts +15 -4
- package/src/index.ts +4 -2
- package/src/types.ts +5 -7
- package/src/utils.ts +4 -25
- package/dist/components/html-message.d.ts +0 -11
- package/dist/components/html-message.js +0 -75
- package/lib/components/html-message.d.ts +0 -11
- package/lib/components/html-message.js +0 -43
- package/src/components/html-message.tsx +0 -68
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,53 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [4.1.1](https://github.com/formatjs/react-intl/compare/v4.1.0...v4.1.1) (2020-03-06)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* dont warn if locale is the same as defaultLocale ([c8b178b](https://github.com/formatjs/react-intl/commit/c8b178b1e825b3e1abc2417c3ec60d92ab1911f6)), closes [#1594](https://github.com/formatjs/react-intl/issues/1594)
|
|
11
|
+
|
|
12
|
+
## [4.1.0](https://github.com/formatjs/react-intl/compare/v4.0.0...v4.1.0) (2020-03-05)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* Introduce ReactIntlErrorCode so we can distinguish and log things differently ([5b2b034](https://github.com/formatjs/react-intl/commit/5b2b034b0afd03479943cf4d1162be532deb95b8))
|
|
18
|
+
|
|
19
|
+
## [4.0.0](https://github.com/formatjs/react-intl/compare/v3.12.1...v4.0.0) (2020-03-05)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### ⚠ BREAKING CHANGES
|
|
23
|
+
|
|
24
|
+
* This release contains subtle changes to the way we handle embedded HTML Message:
|
|
25
|
+
|
|
26
|
+
1. All tags specified must have corresponding values and will throw
|
|
27
|
+
error if it's missing, e.g:
|
|
28
|
+
```
|
|
29
|
+
new IntlMessageFormat("a<b>strong</b>").format({ b: (...chunks) => <strong>chunks</strong> })
|
|
30
|
+
```
|
|
31
|
+
2. We don't allow formatting self-closing tags because we already use ICU `{placeholder}` syntax for that.
|
|
32
|
+
3. XML/HTML tags are escaped using apostrophe just like other ICU constructs.
|
|
33
|
+
4. Remove dependency on DOMParser and restrictions on void element like `<link>`. This effectively means you don't need to polyfill DOMParser in Node anymore.
|
|
34
|
+
5. `FormattedHTMLMessage` & `intl.formatHTMLMessage` have been removed since `FormattedMessage` now fully supports embedded HTML tag.
|
|
35
|
+
|
|
36
|
+
Why are we doing those changes?
|
|
37
|
+
- `FormattedHTMLMessage` & `intl.formatHTMLMessage` were originally created when React was fairly new. These components helped ease migration over from raw HTML to JSX. Given that current popularity of React right now and the fact that `FormattedMessage` allow rendering embedded HTML tag, this is no longer needed.
|
|
38
|
+
- Initially during the 1st iteration of embedded HTML support, we allow any tag that doesn’t have a corresponding formatter to be rendered as raw HTML. We’ve received feedbacks internally that allowing embedded HTML tag to be rendered as-is without sanitization is a XSS security risk. Therefore, in order to allow raw HTML tag you have to opt-in by escaping them using apostrophe. We will update our linter to check for this as well.
|
|
39
|
+
|
|
40
|
+
### Features
|
|
41
|
+
|
|
42
|
+
* Upgrade intl-messageformat & intl-messageformat-parser, remove FormattedHTMLMessage ([fae69e9](https://github.com/formatjs/react-intl/commit/fae69e9da2cfd395eeb5d7333a637f6627d94ade))
|
|
43
|
+
|
|
44
|
+
### [3.12.1](https://github.com/formatjs/react-intl/compare/v3.12.0...v3.12.1) (2020-03-04)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
### Bug Fixes
|
|
48
|
+
|
|
49
|
+
* clarify err messages ([b207d80](https://github.com/formatjs/react-intl/commit/b207d805a9e5609983ac1b2ca8f9708030e12337))
|
|
50
|
+
* inline defineMessages macro, fix [#1592](https://github.com/formatjs/react-intl/issues/1592) ([5ae8349](https://github.com/formatjs/react-intl/commit/5ae8349dc2ee08b54a54a2a521b8a9c052960d04))
|
|
51
|
+
|
|
5
52
|
## [3.12.0](https://github.com/formatjs/react-intl/compare/v3.11.0...v3.12.0) (2020-02-04)
|
|
6
53
|
|
|
7
54
|
|
|
@@ -6,7 +6,7 @@ export interface Props<V extends Record<string, any> = Record<string, React.Reac
|
|
|
6
6
|
tagName?: React.ElementType<any>;
|
|
7
7
|
children?(...nodes: React.ReactNodeArray): React.ReactNode;
|
|
8
8
|
}
|
|
9
|
-
declare class FormattedMessage<V extends Record<string, any> = Record<string, PrimitiveType | React.ReactElement | FormatXMLElementFn
|
|
9
|
+
declare class FormattedMessage<T = React.ReactNode, V extends Record<string, any> = Record<string, PrimitiveType | React.ReactElement | FormatXMLElementFn<T>>> extends React.Component<Props<V>> {
|
|
10
10
|
static displayName: string;
|
|
11
11
|
static defaultProps: {
|
|
12
12
|
values: {};
|
|
@@ -53,12 +53,12 @@ var message_1 = require("../formatters/message");
|
|
|
53
53
|
var utils_1 = require("../utils");
|
|
54
54
|
var shallowEquals_ = require("shallow-equal/objects");
|
|
55
55
|
var shallowEquals = shallowEquals_.default || shallowEquals_;
|
|
56
|
-
|
|
56
|
+
function defaultFormatMessage(descriptor, values) {
|
|
57
57
|
if (process.env.NODE_ENV !== 'production') {
|
|
58
58
|
console.error('[React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry. Using default message as fallback.');
|
|
59
59
|
}
|
|
60
60
|
return message_1.formatMessage(__assign(__assign({}, utils_1.DEFAULT_INTL_CONFIG), { locale: 'en' }), utils_1.createFormatters(), descriptor, values);
|
|
61
|
-
}
|
|
61
|
+
}
|
|
62
62
|
var FormattedMessage = /** @class */ (function (_super) {
|
|
63
63
|
__extends(FormattedMessage, _super);
|
|
64
64
|
function FormattedMessage() {
|
|
@@ -11,7 +11,7 @@ interface Props extends FormatPluralOptions {
|
|
|
11
11
|
many?: React.ReactNode;
|
|
12
12
|
children?(value: React.ReactNode): React.ReactElement | null;
|
|
13
13
|
}
|
|
14
|
-
declare const _default: React.ForwardRefExoticComponent<Pick<Props, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "
|
|
14
|
+
declare const _default: React.ForwardRefExoticComponent<Pick<Props, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "localeMatcher" | "format" | "type" | "value"> & {
|
|
15
15
|
forwardedRef?: ((instance: any) => void) | React.RefObject<any> | null | undefined;
|
|
16
16
|
} & React.RefAttributes<any>> & {
|
|
17
17
|
WrappedComponent: React.ComponentType<Props>;
|
|
@@ -32,7 +32,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
32
32
|
var React = require("react");
|
|
33
33
|
var injectIntl_1 = require("./injectIntl");
|
|
34
34
|
var utils_1 = require("../utils");
|
|
35
|
-
var intl_locales_supported_1 = require("intl-locales-supported");
|
|
36
35
|
var number_1 = require("../formatters/number");
|
|
37
36
|
var relativeTime_1 = require("../formatters/relativeTime");
|
|
38
37
|
var dateTime_1 = require("../formatters/dateTime");
|
|
@@ -41,6 +40,7 @@ var message_1 = require("../formatters/message");
|
|
|
41
40
|
var shallowEquals_ = require("shallow-equal/objects");
|
|
42
41
|
var list_1 = require("../formatters/list");
|
|
43
42
|
var displayName_1 = require("../formatters/displayName");
|
|
43
|
+
var error_1 = require("../error");
|
|
44
44
|
var shallowEquals = shallowEquals_.default || shallowEquals_;
|
|
45
45
|
function processIntlConfig(config) {
|
|
46
46
|
return {
|
|
@@ -62,12 +62,10 @@ function processIntlConfig(config) {
|
|
|
62
62
|
function createIntl(config, cache) {
|
|
63
63
|
var formatters = utils_1.createFormatters(cache);
|
|
64
64
|
var resolvedConfig = __assign(__assign({}, utils_1.DEFAULT_INTL_CONFIG), config);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
onError(utils_1.createError("Missing locale data for locale: \"" + locale + "\". " +
|
|
70
|
-
("Using default locale: \"" + defaultLocale + "\" as fallback.")));
|
|
65
|
+
var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
|
|
66
|
+
if (!locale) {
|
|
67
|
+
if (onError) {
|
|
68
|
+
onError(new error_1.ReactIntlError("INVALID_CONFIG" /* INVALID_CONFIG */, "\"locale\" was not configured, using \"" + defaultLocale + "\" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/API.md#intlshape for more details"));
|
|
71
69
|
}
|
|
72
70
|
// Since there's no registered locale data for `locale`, this will
|
|
73
71
|
// fallback to the `defaultLocale` to make sure things can render.
|
|
@@ -76,7 +74,14 @@ function createIntl(config, cache) {
|
|
|
76
74
|
// each <FormattedMessage> contains a `defaultMessage` prop.
|
|
77
75
|
resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
|
|
78
76
|
}
|
|
79
|
-
|
|
77
|
+
else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
|
|
78
|
+
onError(new error_1.ReactIntlError("MISSING_DATA" /* MISSING_DATA */, "Missing locale data for locale: \"" + locale + "\" in Intl.NumberFormat. Using default locale: \"" + defaultLocale + "\" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/Getting-Started.md#runtime-requirements for more details"));
|
|
79
|
+
}
|
|
80
|
+
else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
|
|
81
|
+
onError) {
|
|
82
|
+
onError(new error_1.ReactIntlError("MISSING_DATA" /* MISSING_DATA */, "Missing locale data for locale: \"" + locale + "\" in Intl.DateTimeFormat. Using default locale: \"" + defaultLocale + "\" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/Getting-Started.md#runtime-requirements for more details"));
|
|
83
|
+
}
|
|
84
|
+
return __assign(__assign({}, resolvedConfig), { formatters: formatters, formatNumber: number_1.formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: number_1.formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: relativeTime_1.formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: dateTime_1.formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: dateTime_1.formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: dateTime_1.formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: displayName_1.formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
|
|
80
85
|
}
|
|
81
86
|
exports.createIntl = createIntl;
|
|
82
87
|
var IntlProvider = /** @class */ (function (_super) {
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const enum ReactIntlErrorCode {
|
|
2
|
+
FORMAT_ERROR = "FORMAT_ERROR",
|
|
3
|
+
UNSUPPORTED_FORMATTER = "UNSUPPORTED_FORMATTER",
|
|
4
|
+
INVALID_CONFIG = "INVALID_CONFIG",
|
|
5
|
+
MISSING_DATA = "MISSING_DATA",
|
|
6
|
+
MISSING_TRANSLATION = "MISSING_TRANSLATION"
|
|
7
|
+
}
|
|
8
|
+
export declare class ReactIntlError extends Error {
|
|
9
|
+
code: ReactIntlErrorCode;
|
|
10
|
+
constructor(code: ReactIntlErrorCode, message: string, exception?: Error);
|
|
11
|
+
}
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
extendStatics(d, b);
|
|
11
|
+
function __() { this.constructor = d; }
|
|
12
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
var ReactIntlError = /** @class */ (function (_super) {
|
|
17
|
+
__extends(ReactIntlError, _super);
|
|
18
|
+
function ReactIntlError(code, message, exception) {
|
|
19
|
+
var _this = _super.call(this, "[React Intl Error " + code + "] " + message + " " + (exception ? "\n" + exception.stack : '')) || this;
|
|
20
|
+
_this.code = code;
|
|
21
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
22
|
+
Error.captureStackTrace(_this, ReactIntlError);
|
|
23
|
+
}
|
|
24
|
+
return _this;
|
|
25
|
+
}
|
|
26
|
+
return ReactIntlError;
|
|
27
|
+
}(Error));
|
|
28
|
+
exports.ReactIntlError = ReactIntlError;
|
|
@@ -17,6 +17,7 @@ var __assign = (this && this.__assign) || function () {
|
|
|
17
17
|
};
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
19
|
var utils_1 = require("../utils");
|
|
20
|
+
var error_1 = require("../error");
|
|
20
21
|
var DATE_TIME_FORMAT_OPTIONS = [
|
|
21
22
|
'localeMatcher',
|
|
22
23
|
'formatMatcher',
|
|
@@ -55,7 +56,7 @@ function formatDate(config, getDateTimeFormat, value, options) {
|
|
|
55
56
|
return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
|
|
56
57
|
}
|
|
57
58
|
catch (e) {
|
|
58
|
-
config.onError(
|
|
59
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting date.', e));
|
|
59
60
|
}
|
|
60
61
|
return String(date);
|
|
61
62
|
}
|
|
@@ -67,7 +68,7 @@ function formatTime(config, getDateTimeFormat, value, options) {
|
|
|
67
68
|
return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
|
|
68
69
|
}
|
|
69
70
|
catch (e) {
|
|
70
|
-
config.onError(
|
|
71
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting time.', e));
|
|
71
72
|
}
|
|
72
73
|
return String(date);
|
|
73
74
|
}
|
|
@@ -79,7 +80,7 @@ function formatDateToParts(config, getDateTimeFormat, value, options) {
|
|
|
79
80
|
return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date);
|
|
80
81
|
}
|
|
81
82
|
catch (e) {
|
|
82
|
-
config.onError(
|
|
83
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting date.', e));
|
|
83
84
|
}
|
|
84
85
|
return [];
|
|
85
86
|
}
|
|
@@ -91,7 +92,7 @@ function formatTimeToParts(config, getDateTimeFormat, value, options) {
|
|
|
91
92
|
return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date);
|
|
92
93
|
}
|
|
93
94
|
catch (e) {
|
|
94
|
-
config.onError(
|
|
95
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting time.', e));
|
|
95
96
|
}
|
|
96
97
|
return [];
|
|
97
98
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var utils_1 = require("../utils");
|
|
4
|
+
var intl_messageformat_1 = require("intl-messageformat");
|
|
5
|
+
var error_1 = require("../error");
|
|
4
6
|
var DISPLAY_NAMES_OPTONS = [
|
|
5
7
|
'localeMatcher',
|
|
6
8
|
'style',
|
|
@@ -12,14 +14,14 @@ function formatDisplayName(_a, getDisplayNames, value, options) {
|
|
|
12
14
|
if (options === void 0) { options = {}; }
|
|
13
15
|
var DisplayNames = Intl.DisplayNames;
|
|
14
16
|
if (!DisplayNames) {
|
|
15
|
-
onError(
|
|
17
|
+
onError(new intl_messageformat_1.FormatError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n", 2 /* MISSING_INTL_API */));
|
|
16
18
|
}
|
|
17
19
|
var filteredOptions = utils_1.filterProps(options, DISPLAY_NAMES_OPTONS);
|
|
18
20
|
try {
|
|
19
21
|
return getDisplayNames(locale, filteredOptions).of(value);
|
|
20
22
|
}
|
|
21
23
|
catch (e) {
|
|
22
|
-
onError(
|
|
24
|
+
onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting display name.', e));
|
|
23
25
|
}
|
|
24
26
|
}
|
|
25
27
|
exports.formatDisplayName = formatDisplayName;
|
package/dist/formatters/list.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var utils_1 = require("../utils");
|
|
4
|
+
var intl_messageformat_1 = require("intl-messageformat");
|
|
5
|
+
var error_1 = require("../error");
|
|
4
6
|
var LIST_FORMAT_OPTIONS = [
|
|
5
7
|
'localeMatcher',
|
|
6
8
|
'type',
|
|
@@ -15,7 +17,7 @@ function formatList(_a, getListFormat, values, options) {
|
|
|
15
17
|
if (options === void 0) { options = {}; }
|
|
16
18
|
var ListFormat = Intl.ListFormat;
|
|
17
19
|
if (!ListFormat) {
|
|
18
|
-
onError(
|
|
20
|
+
onError(new intl_messageformat_1.FormatError("Intl.ListFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-listformat\"\n", 2 /* MISSING_INTL_API */));
|
|
19
21
|
}
|
|
20
22
|
var filteredOptions = utils_1.filterProps(options, LIST_FORMAT_OPTIONS);
|
|
21
23
|
try {
|
|
@@ -47,7 +49,7 @@ function formatList(_a, getListFormat, values, options) {
|
|
|
47
49
|
}, []);
|
|
48
50
|
}
|
|
49
51
|
catch (e) {
|
|
50
|
-
onError(
|
|
52
|
+
onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting list.', e));
|
|
51
53
|
}
|
|
52
54
|
return values;
|
|
53
55
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { Formatters, IntlConfig, MessageDescriptor } from '../types';
|
|
3
3
|
import { PrimitiveType } from 'intl-messageformat';
|
|
4
|
-
export declare const prepareIntlMessageFormatHtmlOutput: (chunks: (string |
|
|
4
|
+
export declare const prepareIntlMessageFormatHtmlOutput: <T>(chunks: (string | T)[]) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>;
|
|
5
5
|
export declare function formatMessage({ locale, formats, messages, defaultLocale, defaultFormats, onError, }: Pick<IntlConfig, 'locale' | 'formats' | 'messages' | 'defaultLocale' | 'defaultFormats' | 'onError'>, state: Formatters, messageDescriptor?: MessageDescriptor, values?: Record<string, PrimitiveType>): string;
|
|
6
|
-
export declare function formatHTMLMessage(config: Pick<IntlConfig, 'locale' | 'formats' | 'messages' | 'defaultLocale' | 'defaultFormats' | 'onError'>, state: Formatters, messageDescriptor?: MessageDescriptor, rawValues?: Record<string, PrimitiveType>): React.ReactNode;
|
|
@@ -25,8 +25,8 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
var React = require("react");
|
|
27
27
|
var intl_utils_1 = require("@formatjs/intl-utils");
|
|
28
|
-
var utils_1 = require("../utils");
|
|
29
28
|
var intl_messageformat_1 = require("intl-messageformat");
|
|
29
|
+
var error_1 = require("../error");
|
|
30
30
|
function setTimeZoneInOptions(opts, timeZone) {
|
|
31
31
|
return Object.keys(opts).reduce(function (all, k) {
|
|
32
32
|
all[k] = __assign({ timeZone: timeZone }, opts[k]);
|
|
@@ -58,64 +58,47 @@ function formatMessage(_a, state, messageDescriptor, values) {
|
|
|
58
58
|
var message = messages && messages[String(id)];
|
|
59
59
|
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
|
|
60
60
|
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
|
|
61
|
-
var formattedMessageParts =
|
|
61
|
+
var formattedMessageParts = '';
|
|
62
62
|
if (message) {
|
|
63
63
|
try {
|
|
64
64
|
var formatter = state.getMessageFormat(message, locale, formats, {
|
|
65
65
|
formatters: state,
|
|
66
66
|
});
|
|
67
|
-
formattedMessageParts = formatter.
|
|
67
|
+
formattedMessageParts = formatter.format(values);
|
|
68
68
|
}
|
|
69
69
|
catch (e) {
|
|
70
|
-
onError(
|
|
70
|
+
onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, "Error formatting message: \"" + id + "\" for locale: \"" + locale + "\"" +
|
|
71
71
|
(defaultMessage ? ', using default message as fallback.' : ''), e));
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
else
|
|
74
|
+
else if (!defaultMessage ||
|
|
75
|
+
(locale && locale.toLowerCase() !== defaultLocale.toLowerCase())) {
|
|
75
76
|
// This prevents warnings from littering the console in development
|
|
76
77
|
// when no `messages` are passed into the <IntlProvider> for the
|
|
77
|
-
// default locale
|
|
78
|
-
|
|
79
|
-
(
|
|
80
|
-
onError(utils_1.createError("Missing message: \"" + id + "\" for locale: \"" + locale + "\"" +
|
|
81
|
-
(defaultMessage ? ', using default message as fallback.' : '')));
|
|
82
|
-
}
|
|
78
|
+
// default locale.
|
|
79
|
+
onError(new error_1.ReactIntlError("MISSING_TRANSLATION" /* MISSING_TRANSLATION */, "Missing message: \"" + id + "\" for locale: \"" + locale + "\"" +
|
|
80
|
+
(defaultMessage ? ', using default message as fallback.' : '')));
|
|
83
81
|
}
|
|
84
|
-
if (!formattedMessageParts
|
|
82
|
+
if (!formattedMessageParts && defaultMessage) {
|
|
85
83
|
try {
|
|
86
84
|
var formatter = state.getMessageFormat(defaultMessage, defaultLocale, defaultFormats);
|
|
87
|
-
formattedMessageParts = formatter.
|
|
85
|
+
formattedMessageParts = formatter.format(values);
|
|
88
86
|
}
|
|
89
87
|
catch (e) {
|
|
90
|
-
onError(
|
|
88
|
+
onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, "Error formatting the default message for: \"" + id + "\"", e));
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
|
-
if (!formattedMessageParts
|
|
94
|
-
onError(
|
|
91
|
+
if (!formattedMessageParts) {
|
|
92
|
+
onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, "Cannot format message: \"" + id + "\", " +
|
|
95
93
|
("using message " + (message || defaultMessage ? 'source' : 'id') + " as fallback.")));
|
|
96
94
|
if (typeof message === 'string') {
|
|
97
95
|
return message || defaultMessage || String(id);
|
|
98
96
|
}
|
|
99
97
|
return defaultMessage || String(id);
|
|
100
98
|
}
|
|
101
|
-
if (formattedMessageParts
|
|
102
|
-
|
|
103
|
-
return formattedMessageParts[0] || defaultMessage || String(id);
|
|
99
|
+
if (Array.isArray(formattedMessageParts)) {
|
|
100
|
+
return exports.prepareIntlMessageFormatHtmlOutput(formattedMessageParts);
|
|
104
101
|
}
|
|
105
|
-
return
|
|
102
|
+
return formattedMessageParts;
|
|
106
103
|
}
|
|
107
104
|
exports.formatMessage = formatMessage;
|
|
108
|
-
function formatHTMLMessage(config, state, messageDescriptor, rawValues) {
|
|
109
|
-
if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
|
|
110
|
-
if (rawValues === void 0) { rawValues = {}; }
|
|
111
|
-
// Process all the values before they are used when formatting the ICU
|
|
112
|
-
// Message string. Since the formatted message might be injected via
|
|
113
|
-
// `innerHTML`, all String-based values need to be HTML-escaped.
|
|
114
|
-
var escapedValues = Object.keys(rawValues).reduce(function (escaped, name) {
|
|
115
|
-
var value = rawValues[name];
|
|
116
|
-
escaped[name] = typeof value === 'string' ? utils_1.escape(value) : value;
|
|
117
|
-
return escaped;
|
|
118
|
-
}, {});
|
|
119
|
-
return formatMessage(config, state, messageDescriptor, escapedValues);
|
|
120
|
-
}
|
|
121
|
-
exports.formatHTMLMessage = formatHTMLMessage;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var utils_1 = require("../utils");
|
|
4
|
+
var error_1 = require("../error");
|
|
4
5
|
var NUMBER_FORMAT_OPTIONS = [
|
|
5
6
|
'localeMatcher',
|
|
6
7
|
'style',
|
|
@@ -40,7 +41,7 @@ function formatNumber(config, getNumberFormat, value, options) {
|
|
|
40
41
|
return getFormatter(config, getNumberFormat, options).format(value);
|
|
41
42
|
}
|
|
42
43
|
catch (e) {
|
|
43
|
-
config.onError(
|
|
44
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting number.', e));
|
|
44
45
|
}
|
|
45
46
|
return String(value);
|
|
46
47
|
}
|
|
@@ -51,7 +52,7 @@ function formatNumberToParts(config, getNumberFormat, value, options) {
|
|
|
51
52
|
return getFormatter(config, getNumberFormat, options).formatToParts(value);
|
|
52
53
|
}
|
|
53
54
|
catch (e) {
|
|
54
|
-
config.onError(
|
|
55
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting number.', e));
|
|
55
56
|
}
|
|
56
57
|
return [];
|
|
57
58
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var utils_1 = require("../utils");
|
|
4
|
+
var error_1 = require("../error");
|
|
5
|
+
var intl_messageformat_1 = require("intl-messageformat");
|
|
4
6
|
var PLURAL_FORMAT_OPTIONS = [
|
|
5
7
|
'localeMatcher',
|
|
6
8
|
'type',
|
|
@@ -9,14 +11,14 @@ function formatPlural(_a, getPluralRules, value, options) {
|
|
|
9
11
|
var locale = _a.locale, onError = _a.onError;
|
|
10
12
|
if (options === void 0) { options = {}; }
|
|
11
13
|
if (!Intl.PluralRules) {
|
|
12
|
-
onError(
|
|
14
|
+
onError(new intl_messageformat_1.FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", 2 /* MISSING_INTL_API */));
|
|
13
15
|
}
|
|
14
16
|
var filteredOptions = utils_1.filterProps(options, PLURAL_FORMAT_OPTIONS);
|
|
15
17
|
try {
|
|
16
18
|
return getPluralRules(locale, filteredOptions).select(value);
|
|
17
19
|
}
|
|
18
20
|
catch (e) {
|
|
19
|
-
onError(
|
|
21
|
+
onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting plural.', e));
|
|
20
22
|
}
|
|
21
23
|
return 'other';
|
|
22
24
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var utils_1 = require("../utils");
|
|
4
|
+
var intl_messageformat_1 = require("intl-messageformat");
|
|
5
|
+
var error_1 = require("../error");
|
|
4
6
|
var RELATIVE_TIME_FORMAT_OPTIONS = [
|
|
5
7
|
'numeric',
|
|
6
8
|
'style',
|
|
@@ -20,13 +22,13 @@ function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options)
|
|
|
20
22
|
}
|
|
21
23
|
var RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
22
24
|
if (!RelativeTimeFormat) {
|
|
23
|
-
config.onError(
|
|
25
|
+
config.onError(new intl_messageformat_1.FormatError("Intl.RelativeTimeFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-relativetimeformat\"\n", 2 /* MISSING_INTL_API */));
|
|
24
26
|
}
|
|
25
27
|
try {
|
|
26
28
|
return getFormatter(config, getRelativeTimeFormat, options).format(value, unit);
|
|
27
29
|
}
|
|
28
30
|
catch (e) {
|
|
29
|
-
config.onError(
|
|
31
|
+
config.onError(new error_1.ReactIntlError("FORMAT_ERROR" /* FORMAT_ERROR */, 'Error formatting relative time.', e));
|
|
30
32
|
}
|
|
31
33
|
return String(value);
|
|
32
34
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
export * from './types';
|
|
3
|
-
export
|
|
3
|
+
export declare function defineMessages<T, U extends Record<string, T>>(msgs: U): U;
|
|
4
4
|
import { CustomFormatConfig } from './types';
|
|
5
5
|
import { UnifiedNumberFormatOptions } from '@formatjs/intl-unified-numberformat';
|
|
6
6
|
import { IntlListFormatOptions } from '@formatjs/intl-listformat';
|
|
@@ -35,5 +35,5 @@ export { FormattedNumberParts } from './components/createFormattedComponent';
|
|
|
35
35
|
export { default as FormattedRelativeTime } from './components/relative';
|
|
36
36
|
export { default as FormattedPlural } from './components/plural';
|
|
37
37
|
export { default as FormattedMessage } from './components/message';
|
|
38
|
-
export { default as FormattedHTMLMessage } from './components/html-message';
|
|
39
38
|
export { createIntlCache } from './utils';
|
|
39
|
+
export { ReactIntlError, ReactIntlErrorCode } from './error';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
function defineMessages(msgs) {
|
|
4
|
+
return msgs;
|
|
5
|
+
}
|
|
6
|
+
exports.defineMessages = defineMessages;
|
|
5
7
|
var createFormattedComponent_1 = require("./components/createFormattedComponent");
|
|
6
8
|
var injectIntl_1 = require("./components/injectIntl");
|
|
7
9
|
exports.injectIntl = injectIntl_1.default;
|
|
@@ -28,7 +30,7 @@ var plural_1 = require("./components/plural");
|
|
|
28
30
|
exports.FormattedPlural = plural_1.default;
|
|
29
31
|
var message_1 = require("./components/message");
|
|
30
32
|
exports.FormattedMessage = message_1.default;
|
|
31
|
-
var html_message_1 = require("./components/html-message");
|
|
32
|
-
exports.FormattedHTMLMessage = html_message_1.default;
|
|
33
33
|
var utils_1 = require("./utils");
|
|
34
34
|
exports.createIntlCache = utils_1.createIntlCache;
|
|
35
|
+
var error_1 = require("./error");
|
|
36
|
+
exports.ReactIntlError = error_1.ReactIntlError;
|
package/dist/react-intl.api.md
CHANGED
|
@@ -74,31 +74,17 @@ export const FormattedDisplayName: React.FC<DisplayNamesOptions_2 & {
|
|
|
74
74
|
value: string | number | object;
|
|
75
75
|
}>;
|
|
76
76
|
|
|
77
|
-
// Warning: (ae-forgotten-export) The symbol "PrimitiveType" needs to be exported by the entry point index.d.ts
|
|
78
|
-
//
|
|
79
|
-
// @public (undocumented)
|
|
80
|
-
export class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
|
|
81
|
-
// (undocumented)
|
|
82
|
-
static defaultProps: {
|
|
83
|
-
tagName: "span";
|
|
84
|
-
values: {};
|
|
85
|
-
};
|
|
86
|
-
// (undocumented)
|
|
87
|
-
static displayName: string;
|
|
88
|
-
// (undocumented)
|
|
89
|
-
render(): JSX.Element;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
77
|
// @public (undocumented)
|
|
93
78
|
export const FormattedList: React.FC<IntlListFormatOptions & {
|
|
94
79
|
value: React.ReactNode[];
|
|
95
80
|
}>;
|
|
96
81
|
|
|
82
|
+
// Warning: (ae-forgotten-export) The symbol "PrimitiveType" needs to be exported by the entry point index.d.ts
|
|
97
83
|
// Warning: (ae-forgotten-export) The symbol "FormatXMLElementFn" needs to be exported by the entry point index.d.ts
|
|
98
84
|
// Warning: (ae-forgotten-export) The symbol "Props" needs to be exported by the entry point index.d.ts
|
|
99
85
|
//
|
|
100
86
|
// @public (undocumented)
|
|
101
|
-
export class FormattedMessage<V extends Record<string, any> = Record<string, PrimitiveType | React.ReactElement | FormatXMLElementFn
|
|
87
|
+
export class FormattedMessage<T = React.ReactNode, V extends Record<string, any> = Record<string, PrimitiveType | React.ReactElement | FormatXMLElementFn<T>>> extends React.Component<Props_3<V>> {
|
|
102
88
|
// (undocumented)
|
|
103
89
|
static defaultProps: {
|
|
104
90
|
values: {};
|
|
@@ -127,7 +113,7 @@ export const FormattedNumberParts: React.FC<Formatter['formatNumber'] & {
|
|
|
127
113
|
// Warning: (ae-forgotten-export) The symbol "Props" needs to be exported by the entry point index.d.ts
|
|
128
114
|
//
|
|
129
115
|
// @public (undocumented)
|
|
130
|
-
export const FormattedPlural: React.ForwardRefExoticComponent<Pick<Props_2, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "
|
|
116
|
+
export const FormattedPlural: React.ForwardRefExoticComponent<Pick<Props_2, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "localeMatcher" | "format" | "type" | "value"> & {
|
|
131
117
|
forwardedRef?: ((instance: any) => void) | React.RefObject<any> | null | undefined;
|
|
132
118
|
} & React.RefAttributes<any>> & {
|
|
133
119
|
WrappedComponent: React.ComponentType<Props_2>;
|
|
@@ -240,8 +226,10 @@ export interface IntlConfig {
|
|
|
240
226
|
//
|
|
241
227
|
// (undocumented)
|
|
242
228
|
messages: Record<string, string> | Record<string, MessageFormatElement[]>;
|
|
229
|
+
// Warning: (ae-forgotten-export) The symbol "FormatError" needs to be exported by the entry point index.d.ts
|
|
230
|
+
//
|
|
243
231
|
// (undocumented)
|
|
244
|
-
onError(err:
|
|
232
|
+
onError(err: ReactIntlError | FormatError): void;
|
|
245
233
|
// (undocumented)
|
|
246
234
|
textComponent?: React.ComponentType | keyof React.ReactHTML;
|
|
247
235
|
// (undocumented)
|
|
@@ -252,7 +240,7 @@ export interface IntlConfig {
|
|
|
252
240
|
export const IntlContext: React.Context<IntlShape>;
|
|
253
241
|
|
|
254
242
|
// @public (undocumented)
|
|
255
|
-
export interface IntlFormatters {
|
|
243
|
+
export interface IntlFormatters<T = React.ReactNode> {
|
|
256
244
|
// (undocumented)
|
|
257
245
|
formatDate(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
258
246
|
// (undocumented)
|
|
@@ -260,15 +248,13 @@ export interface IntlFormatters {
|
|
|
260
248
|
// (undocumented)
|
|
261
249
|
formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts?: FormatDisplayNameOptions): string | undefined;
|
|
262
250
|
// (undocumented)
|
|
263
|
-
formatHTMLMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): React.ReactNode;
|
|
264
|
-
// (undocumented)
|
|
265
251
|
formatList(values: Array<string>, opts?: FormatListOptions): string;
|
|
266
252
|
// (undocumented)
|
|
267
253
|
formatList(values: Array<string | React.ReactNode>, opts?: FormatListOptions): React.ReactNode;
|
|
268
254
|
// (undocumented)
|
|
269
255
|
formatMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): string;
|
|
270
256
|
// (undocumented)
|
|
271
|
-
formatMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | React.ReactElement | FormatXMLElementFn
|
|
257
|
+
formatMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | React.ReactElement | FormatXMLElementFn<T>>): string | React.ReactNodeArray;
|
|
272
258
|
// (undocumented)
|
|
273
259
|
formatNumber(value: Parameters<Intl.NumberFormat['format']>[0], opts?: FormatNumberOptions): string;
|
|
274
260
|
// (undocumented)
|
|
@@ -321,6 +307,27 @@ export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
|
|
|
321
307
|
// @public (undocumented)
|
|
322
308
|
export const RawIntlProvider: React.Provider<IntlShape>;
|
|
323
309
|
|
|
310
|
+
// @public (undocumented)
|
|
311
|
+
export class ReactIntlError extends Error {
|
|
312
|
+
constructor(code: ReactIntlErrorCode, message: string, exception?: Error);
|
|
313
|
+
// (undocumented)
|
|
314
|
+
code: ReactIntlErrorCode;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// @public (undocumented)
|
|
318
|
+
export const enum ReactIntlErrorCode {
|
|
319
|
+
// (undocumented)
|
|
320
|
+
FORMAT_ERROR = "FORMAT_ERROR",
|
|
321
|
+
// (undocumented)
|
|
322
|
+
INVALID_CONFIG = "INVALID_CONFIG",
|
|
323
|
+
// (undocumented)
|
|
324
|
+
MISSING_DATA = "MISSING_DATA",
|
|
325
|
+
// (undocumented)
|
|
326
|
+
MISSING_TRANSLATION = "MISSING_TRANSLATION",
|
|
327
|
+
// (undocumented)
|
|
328
|
+
UNSUPPORTED_FORMATTER = "UNSUPPORTED_FORMATTER"
|
|
329
|
+
}
|
|
330
|
+
|
|
324
331
|
// @public (undocumented)
|
|
325
332
|
export function useIntl(): IntlShape;
|
|
326
333
|
|