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
|
@@ -14,11 +14,11 @@ import {
|
|
|
14
14
|
CustomFormats,
|
|
15
15
|
} from '../types';
|
|
16
16
|
|
|
17
|
-
import {createError, escape} from '../utils';
|
|
18
17
|
import IntlMessageFormat, {
|
|
19
18
|
FormatXMLElementFn,
|
|
20
19
|
PrimitiveType,
|
|
21
20
|
} from 'intl-messageformat';
|
|
21
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error';
|
|
22
22
|
|
|
23
23
|
function setTimeZoneInOptions(
|
|
24
24
|
opts: Record<string, Intl.DateTimeFormatOptions>,
|
|
@@ -72,8 +72,8 @@ function deepMergeFormatsAndSetTimeZone(
|
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
export const prepareIntlMessageFormatHtmlOutput = (
|
|
76
|
-
chunks: (string |
|
|
75
|
+
export const prepareIntlMessageFormatHtmlOutput = <T>(
|
|
76
|
+
chunks: (string | T)[]
|
|
77
77
|
): React.ReactElement => React.createElement(React.Fragment, null, ...chunks);
|
|
78
78
|
|
|
79
79
|
export function formatMessage(
|
|
@@ -97,7 +97,7 @@ export function formatMessage(
|
|
|
97
97
|
messageDescriptor?: MessageDescriptor,
|
|
98
98
|
values?: Record<string, PrimitiveType>
|
|
99
99
|
): string;
|
|
100
|
-
export function formatMessage(
|
|
100
|
+
export function formatMessage<T>(
|
|
101
101
|
{
|
|
102
102
|
locale,
|
|
103
103
|
formats,
|
|
@@ -118,11 +118,20 @@ export function formatMessage(
|
|
|
118
118
|
>,
|
|
119
119
|
state: Formatters,
|
|
120
120
|
messageDescriptor: MessageDescriptor = {id: ''},
|
|
121
|
-
values:
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
values:
|
|
122
|
+
| Record<
|
|
123
|
+
string,
|
|
124
|
+
| string
|
|
125
|
+
| number
|
|
126
|
+
| boolean
|
|
127
|
+
| Date
|
|
128
|
+
| T
|
|
129
|
+
| FormatXMLElementFn<T>
|
|
130
|
+
| null
|
|
131
|
+
| undefined
|
|
132
|
+
>
|
|
133
|
+
| undefined = {}
|
|
134
|
+
): React.ReactNode {
|
|
126
135
|
const {id, defaultMessage} = messageDescriptor;
|
|
127
136
|
|
|
128
137
|
// `id` is a required field of a Message Descriptor.
|
|
@@ -131,7 +140,7 @@ export function formatMessage(
|
|
|
131
140
|
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
|
|
132
141
|
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
|
|
133
142
|
|
|
134
|
-
let formattedMessageParts:
|
|
143
|
+
let formattedMessageParts: string | T | (string | T)[] = '';
|
|
135
144
|
|
|
136
145
|
if (message) {
|
|
137
146
|
try {
|
|
@@ -139,34 +148,34 @@ export function formatMessage(
|
|
|
139
148
|
formatters: state,
|
|
140
149
|
});
|
|
141
150
|
|
|
142
|
-
formattedMessageParts = formatter.
|
|
151
|
+
formattedMessageParts = formatter.format(values);
|
|
143
152
|
} catch (e) {
|
|
144
153
|
onError(
|
|
145
|
-
|
|
154
|
+
new ReactIntlError(
|
|
155
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
146
156
|
`Error formatting message: "${id}" for locale: "${locale}"` +
|
|
147
157
|
(defaultMessage ? ', using default message as fallback.' : ''),
|
|
148
158
|
e
|
|
149
159
|
)
|
|
150
160
|
);
|
|
151
161
|
}
|
|
152
|
-
} else
|
|
162
|
+
} else if (
|
|
163
|
+
!defaultMessage ||
|
|
164
|
+
(locale && locale.toLowerCase() !== defaultLocale.toLowerCase())
|
|
165
|
+
) {
|
|
153
166
|
// This prevents warnings from littering the console in development
|
|
154
167
|
// when no `messages` are passed into the <IntlProvider> for the
|
|
155
|
-
// default locale
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
(defaultMessage ? ', using default message as fallback.' : '')
|
|
164
|
-
)
|
|
165
|
-
);
|
|
166
|
-
}
|
|
168
|
+
// default locale.
|
|
169
|
+
onError(
|
|
170
|
+
new ReactIntlError(
|
|
171
|
+
ReactIntlErrorCode.MISSING_TRANSLATION,
|
|
172
|
+
`Missing message: "${id}" for locale: "${locale}"` +
|
|
173
|
+
(defaultMessage ? ', using default message as fallback.' : '')
|
|
174
|
+
)
|
|
175
|
+
);
|
|
167
176
|
}
|
|
168
177
|
|
|
169
|
-
if (!formattedMessageParts
|
|
178
|
+
if (!formattedMessageParts && defaultMessage) {
|
|
170
179
|
try {
|
|
171
180
|
const formatter = state.getMessageFormat(
|
|
172
181
|
defaultMessage,
|
|
@@ -174,17 +183,22 @@ export function formatMessage(
|
|
|
174
183
|
defaultFormats
|
|
175
184
|
);
|
|
176
185
|
|
|
177
|
-
formattedMessageParts = formatter.
|
|
186
|
+
formattedMessageParts = formatter.format(values);
|
|
178
187
|
} catch (e) {
|
|
179
188
|
onError(
|
|
180
|
-
|
|
189
|
+
new ReactIntlError(
|
|
190
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
191
|
+
`Error formatting the default message for: "${id}"`,
|
|
192
|
+
e
|
|
193
|
+
)
|
|
181
194
|
);
|
|
182
195
|
}
|
|
183
196
|
}
|
|
184
197
|
|
|
185
|
-
if (!formattedMessageParts
|
|
198
|
+
if (!formattedMessageParts) {
|
|
186
199
|
onError(
|
|
187
|
-
|
|
200
|
+
new ReactIntlError(
|
|
201
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
188
202
|
`Cannot format message: "${id}", ` +
|
|
189
203
|
`using message ${
|
|
190
204
|
message || defaultMessage ? 'source' : 'id'
|
|
@@ -196,41 +210,10 @@ export function formatMessage(
|
|
|
196
210
|
}
|
|
197
211
|
return defaultMessage || String(id);
|
|
198
212
|
}
|
|
199
|
-
if (
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return (formattedMessageParts[0] as string) || defaultMessage || String(id);
|
|
213
|
+
if (Array.isArray(formattedMessageParts)) {
|
|
214
|
+
return prepareIntlMessageFormatHtmlOutput<T>(
|
|
215
|
+
formattedMessageParts as Array<string | T>
|
|
216
|
+
);
|
|
204
217
|
}
|
|
205
|
-
|
|
206
|
-
return prepareIntlMessageFormatHtmlOutput(formattedMessageParts);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export function formatHTMLMessage(
|
|
210
|
-
config: Pick<
|
|
211
|
-
IntlConfig,
|
|
212
|
-
| 'locale'
|
|
213
|
-
| 'formats'
|
|
214
|
-
| 'messages'
|
|
215
|
-
| 'defaultLocale'
|
|
216
|
-
| 'defaultFormats'
|
|
217
|
-
| 'onError'
|
|
218
|
-
>,
|
|
219
|
-
state: Formatters,
|
|
220
|
-
messageDescriptor: MessageDescriptor = {id: ''},
|
|
221
|
-
rawValues: Record<string, PrimitiveType> = {}
|
|
222
|
-
): React.ReactNode {
|
|
223
|
-
// Process all the values before they are used when formatting the ICU
|
|
224
|
-
// Message string. Since the formatted message might be injected via
|
|
225
|
-
// `innerHTML`, all String-based values need to be HTML-escaped.
|
|
226
|
-
const escapedValues = Object.keys(rawValues).reduce(
|
|
227
|
-
(escaped: Record<string, any>, name) => {
|
|
228
|
-
const value = rawValues[name];
|
|
229
|
-
escaped[name] = typeof value === 'string' ? escape(value) : value;
|
|
230
|
-
return escaped;
|
|
231
|
-
},
|
|
232
|
-
{}
|
|
233
|
-
);
|
|
234
|
-
|
|
235
|
-
return formatMessage(config, state, messageDescriptor, escapedValues);
|
|
218
|
+
return formattedMessageParts as string | T;
|
|
236
219
|
}
|
package/src/formatters/number.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {IntlConfig, Formatters, IntlFormatters} from '../types';
|
|
2
|
-
import {getNamedFormat, filterProps
|
|
2
|
+
import {getNamedFormat, filterProps} from '../utils';
|
|
3
3
|
import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
|
|
4
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error';
|
|
4
5
|
|
|
5
6
|
const NUMBER_FORMAT_OPTIONS: Array<keyof UnifiedNumberFormatOptions> = [
|
|
6
7
|
'localeMatcher',
|
|
@@ -55,7 +56,13 @@ export function formatNumber(
|
|
|
55
56
|
try {
|
|
56
57
|
return getFormatter(config, getNumberFormat, options).format(value);
|
|
57
58
|
} catch (e) {
|
|
58
|
-
config.onError(
|
|
59
|
+
config.onError(
|
|
60
|
+
new ReactIntlError(
|
|
61
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
62
|
+
'Error formatting number.',
|
|
63
|
+
e
|
|
64
|
+
)
|
|
65
|
+
);
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
return String(value);
|
|
@@ -70,7 +77,13 @@ export function formatNumberToParts(
|
|
|
70
77
|
try {
|
|
71
78
|
return getFormatter(config, getNumberFormat, options).formatToParts(value);
|
|
72
79
|
} catch (e) {
|
|
73
|
-
config.onError(
|
|
80
|
+
config.onError(
|
|
81
|
+
new ReactIntlError(
|
|
82
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
83
|
+
'Error formatting number.',
|
|
84
|
+
e
|
|
85
|
+
)
|
|
86
|
+
);
|
|
74
87
|
}
|
|
75
88
|
|
|
76
89
|
return [];
|
package/src/formatters/plural.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {IntlConfig, Formatters, IntlFormatters} from '../types';
|
|
2
|
-
import {filterProps
|
|
2
|
+
import {filterProps} from '../utils';
|
|
3
|
+
import {ReactIntlErrorCode, ReactIntlError} from '../error';
|
|
4
|
+
import {FormatError, ErrorCode} from 'intl-messageformat';
|
|
3
5
|
|
|
4
6
|
const PLURAL_FORMAT_OPTIONS: Array<keyof Intl.PluralRulesOptions> = [
|
|
5
7
|
'localeMatcher',
|
|
@@ -14,9 +16,12 @@ export function formatPlural(
|
|
|
14
16
|
): string {
|
|
15
17
|
if (!Intl.PluralRules) {
|
|
16
18
|
onError(
|
|
17
|
-
|
|
19
|
+
new FormatError(
|
|
20
|
+
`Intl.PluralRules is not available in this environment.
|
|
18
21
|
Try polyfilling it using "@formatjs/intl-pluralrules"
|
|
19
|
-
|
|
22
|
+
`,
|
|
23
|
+
ErrorCode.MISSING_INTL_API
|
|
24
|
+
)
|
|
20
25
|
);
|
|
21
26
|
}
|
|
22
27
|
const filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
|
|
@@ -24,7 +29,13 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
|
|
|
24
29
|
try {
|
|
25
30
|
return getPluralRules(locale, filteredOptions).select(value);
|
|
26
31
|
} catch (e) {
|
|
27
|
-
onError(
|
|
32
|
+
onError(
|
|
33
|
+
new ReactIntlError(
|
|
34
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
35
|
+
'Error formatting plural.',
|
|
36
|
+
e
|
|
37
|
+
)
|
|
38
|
+
);
|
|
28
39
|
}
|
|
29
40
|
|
|
30
41
|
return 'other';
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {IntlConfig, IntlFormatters, Formatters} from '../types';
|
|
2
2
|
|
|
3
|
-
import {getNamedFormat, filterProps
|
|
3
|
+
import {getNamedFormat, filterProps} from '../utils';
|
|
4
4
|
import RelativeTimeFormat, {
|
|
5
5
|
IntlRelativeTimeFormatOptions,
|
|
6
6
|
} from '@formatjs/intl-relativetimeformat';
|
|
7
|
+
import {FormatError, ErrorCode} from 'intl-messageformat';
|
|
8
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error';
|
|
7
9
|
|
|
8
10
|
const RELATIVE_TIME_FORMAT_OPTIONS: Array<keyof IntlRelativeTimeFormatOptions> = [
|
|
9
11
|
'numeric',
|
|
@@ -45,9 +47,12 @@ export function formatRelativeTime(
|
|
|
45
47
|
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
|
|
46
48
|
if (!RelativeTimeFormat) {
|
|
47
49
|
config.onError(
|
|
48
|
-
|
|
50
|
+
new FormatError(
|
|
51
|
+
`Intl.RelativeTimeFormat is not available in this environment.
|
|
49
52
|
Try polyfilling it using "@formatjs/intl-relativetimeformat"
|
|
50
|
-
|
|
53
|
+
`,
|
|
54
|
+
ErrorCode.MISSING_INTL_API
|
|
55
|
+
)
|
|
51
56
|
);
|
|
52
57
|
}
|
|
53
58
|
try {
|
|
@@ -56,7 +61,13 @@ Try polyfilling it using "@formatjs/intl-relativetimeformat"
|
|
|
56
61
|
unit
|
|
57
62
|
);
|
|
58
63
|
} catch (e) {
|
|
59
|
-
config.onError(
|
|
64
|
+
config.onError(
|
|
65
|
+
new ReactIntlError(
|
|
66
|
+
ReactIntlErrorCode.FORMAT_ERROR,
|
|
67
|
+
'Error formatting relative time.',
|
|
68
|
+
e
|
|
69
|
+
)
|
|
70
|
+
);
|
|
60
71
|
}
|
|
61
72
|
|
|
62
73
|
return String(value);
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as React from 'react';
|
|
7
7
|
export * from './types';
|
|
8
|
-
export
|
|
8
|
+
export function defineMessages<T, U extends Record<string, T>>(msgs: U): U {
|
|
9
|
+
return msgs;
|
|
10
|
+
}
|
|
9
11
|
import {
|
|
10
12
|
createFormattedComponent,
|
|
11
13
|
createFormattedDateTimePartsComponent,
|
|
@@ -52,5 +54,5 @@ export {FormattedNumberParts} from './components/createFormattedComponent';
|
|
|
52
54
|
export {default as FormattedRelativeTime} from './components/relative';
|
|
53
55
|
export {default as FormattedPlural} from './components/plural';
|
|
54
56
|
export {default as FormattedMessage} from './components/message';
|
|
55
|
-
export {default as FormattedHTMLMessage} from './components/html-message';
|
|
56
57
|
export {createIntlCache} from './utils';
|
|
58
|
+
export {ReactIntlError, ReactIntlErrorCode} from './error'
|
package/src/types.ts
CHANGED
|
@@ -8,6 +8,7 @@ import IntlMessageFormat, {
|
|
|
8
8
|
Formats,
|
|
9
9
|
PrimitiveType,
|
|
10
10
|
FormatXMLElementFn,
|
|
11
|
+
FormatError,
|
|
11
12
|
} from 'intl-messageformat';
|
|
12
13
|
import IntlRelativeTimeFormat, {
|
|
13
14
|
IntlRelativeTimeFormatOptions,
|
|
@@ -16,6 +17,7 @@ import {MessageFormatElement} from 'intl-messageformat-parser';
|
|
|
16
17
|
import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
|
|
17
18
|
import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat';
|
|
18
19
|
import {DisplayNames, DisplayNamesOptions} from '@formatjs/intl-displaynames';
|
|
20
|
+
import { ReactIntlError } from './error';
|
|
19
21
|
|
|
20
22
|
export interface IntlConfig {
|
|
21
23
|
locale: string;
|
|
@@ -25,7 +27,7 @@ export interface IntlConfig {
|
|
|
25
27
|
messages: Record<string, string> | Record<string, MessageFormatElement[]>;
|
|
26
28
|
defaultLocale: string;
|
|
27
29
|
defaultFormats: CustomFormats;
|
|
28
|
-
onError(err:
|
|
30
|
+
onError(err: ReactIntlError | FormatError): void;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
export interface CustomFormats extends Partial<Formats> {
|
|
@@ -64,7 +66,7 @@ export type FormatDisplayNameOptions = Exclude<
|
|
|
64
66
|
'localeMatcher'
|
|
65
67
|
>;
|
|
66
68
|
|
|
67
|
-
export interface IntlFormatters {
|
|
69
|
+
export interface IntlFormatters<T = React.ReactNode> {
|
|
68
70
|
formatDate(
|
|
69
71
|
value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
|
|
70
72
|
opts?: FormatDateOptions
|
|
@@ -106,13 +108,9 @@ export interface IntlFormatters {
|
|
|
106
108
|
descriptor: MessageDescriptor,
|
|
107
109
|
values?: Record<
|
|
108
110
|
string,
|
|
109
|
-
PrimitiveType | React.ReactElement | FormatXMLElementFn
|
|
111
|
+
PrimitiveType | React.ReactElement | FormatXMLElementFn<T>
|
|
110
112
|
>
|
|
111
113
|
): string | React.ReactNodeArray;
|
|
112
|
-
formatHTMLMessage(
|
|
113
|
-
descriptor: MessageDescriptor,
|
|
114
|
-
values?: Record<string, PrimitiveType>
|
|
115
|
-
): React.ReactNode;
|
|
116
114
|
formatList(values: Array<string>, opts?: FormatListOptions): string;
|
|
117
115
|
formatList(
|
|
118
116
|
values: Array<string | React.ReactNode>,
|
package/src/utils.ts
CHANGED
|
@@ -15,23 +15,7 @@ import IntlMessageFormat from 'intl-messageformat';
|
|
|
15
15
|
import memoizeIntlConstructor from 'intl-format-cache';
|
|
16
16
|
import {invariant} from '@formatjs/intl-utils';
|
|
17
17
|
import {IntlRelativeTimeFormatOptions} from '@formatjs/intl-relativetimeformat';
|
|
18
|
-
|
|
19
|
-
const ESCAPED_CHARS: Record<number, string> = {
|
|
20
|
-
38: '&',
|
|
21
|
-
62: '>',
|
|
22
|
-
60: '<',
|
|
23
|
-
34: '"',
|
|
24
|
-
39: ''',
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const UNSAFE_CHARS_REGEX = /[&><"']/g;
|
|
28
|
-
|
|
29
|
-
export function escape(str: string): string {
|
|
30
|
-
return ('' + str).replace(
|
|
31
|
-
UNSAFE_CHARS_REGEX,
|
|
32
|
-
match => ESCAPED_CHARS[match.charCodeAt(0)]
|
|
33
|
-
);
|
|
34
|
-
}
|
|
18
|
+
import { ReactIntlError, ReactIntlErrorCode } from './error';
|
|
35
19
|
|
|
36
20
|
export function filterProps<T extends Record<string, any>, K extends string>(
|
|
37
21
|
props: T,
|
|
@@ -57,12 +41,7 @@ export function invariantIntlContext(intl?: any): asserts intl {
|
|
|
57
41
|
);
|
|
58
42
|
}
|
|
59
43
|
|
|
60
|
-
export function
|
|
61
|
-
const eMsg = exception ? `\n${exception.stack}` : '';
|
|
62
|
-
return `[React Intl] ${message}${eMsg}`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function defaultErrorHandler(error: string): void {
|
|
44
|
+
export function defaultErrorHandler(error: ReactIntlError): void {
|
|
66
45
|
if (process.env.NODE_ENV !== 'production') {
|
|
67
46
|
console.error(error);
|
|
68
47
|
}
|
|
@@ -132,7 +111,7 @@ export function getNamedFormat<T extends keyof CustomFormats>(
|
|
|
132
111
|
formats: CustomFormats,
|
|
133
112
|
type: T,
|
|
134
113
|
name: string,
|
|
135
|
-
onError: (err:
|
|
114
|
+
onError: (err: ReactIntlError) => void
|
|
136
115
|
):
|
|
137
116
|
| Intl.NumberFormatOptions
|
|
138
117
|
| Intl.DateTimeFormatOptions
|
|
@@ -147,5 +126,5 @@ export function getNamedFormat<T extends keyof CustomFormats>(
|
|
|
147
126
|
return format;
|
|
148
127
|
}
|
|
149
128
|
|
|
150
|
-
onError(
|
|
129
|
+
onError(new ReactIntlError(ReactIntlErrorCode.UNSUPPORTED_FORMATTER, `No ${type} format named: ${name}`));
|
|
151
130
|
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { PrimitiveType } from 'intl-messageformat';
|
|
2
|
-
import FormattedMessage from './message';
|
|
3
|
-
declare class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
|
|
4
|
-
static displayName: string;
|
|
5
|
-
static defaultProps: {
|
|
6
|
-
tagName: "span";
|
|
7
|
-
values: {};
|
|
8
|
-
};
|
|
9
|
-
render(): JSX.Element;
|
|
10
|
-
}
|
|
11
|
-
export default FormattedHTMLMessage;
|
|
@@ -1,75 +0,0 @@
|
|
|
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
|
-
*/
|
|
7
|
-
var __extends = (this && this.__extends) || (function () {
|
|
8
|
-
var extendStatics = function (d, b) {
|
|
9
|
-
extendStatics = Object.setPrototypeOf ||
|
|
10
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
11
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
12
|
-
return extendStatics(d, b);
|
|
13
|
-
};
|
|
14
|
-
return function (d, b) {
|
|
15
|
-
extendStatics(d, b);
|
|
16
|
-
function __() { this.constructor = d; }
|
|
17
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
18
|
-
};
|
|
19
|
-
})();
|
|
20
|
-
var __assign = (this && this.__assign) || function () {
|
|
21
|
-
__assign = Object.assign || function(t) {
|
|
22
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
23
|
-
s = arguments[i];
|
|
24
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
25
|
-
t[p] = s[p];
|
|
26
|
-
}
|
|
27
|
-
return t;
|
|
28
|
-
};
|
|
29
|
-
return __assign.apply(this, arguments);
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
var React = require("react");
|
|
33
|
-
var message_1 = require("./message");
|
|
34
|
-
var injectIntl_1 = require("./injectIntl");
|
|
35
|
-
var utils_1 = require("../utils");
|
|
36
|
-
var FormattedHTMLMessage = /** @class */ (function (_super) {
|
|
37
|
-
__extends(FormattedHTMLMessage, _super);
|
|
38
|
-
function FormattedHTMLMessage() {
|
|
39
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
40
|
-
}
|
|
41
|
-
FormattedHTMLMessage.prototype.render = function () {
|
|
42
|
-
var _this = this;
|
|
43
|
-
return (React.createElement(injectIntl_1.Context.Consumer, null, function (intl) {
|
|
44
|
-
if (!_this.props.defaultMessage) {
|
|
45
|
-
utils_1.invariantIntlContext(intl);
|
|
46
|
-
}
|
|
47
|
-
var formatHTMLMessage = intl.formatHTMLMessage, textComponent = intl.textComponent;
|
|
48
|
-
var _a = _this.props, id = _a.id, description = _a.description, defaultMessage = _a.defaultMessage, rawValues = _a.values, children = _a.children;
|
|
49
|
-
var Component = _this.props.tagName;
|
|
50
|
-
// This is bc of TS3.3 doesn't recognize `defaultProps`
|
|
51
|
-
if (!Component) {
|
|
52
|
-
Component = textComponent || 'span';
|
|
53
|
-
}
|
|
54
|
-
var descriptor = { id: id, description: description, defaultMessage: defaultMessage };
|
|
55
|
-
var formattedHTMLMessage = formatHTMLMessage(descriptor, rawValues);
|
|
56
|
-
if (typeof children === 'function') {
|
|
57
|
-
return children(formattedHTMLMessage);
|
|
58
|
-
}
|
|
59
|
-
// Since the message presumably has HTML in it, we need to set
|
|
60
|
-
// `innerHTML` in order for it to be rendered and not escaped by React.
|
|
61
|
-
// To be safe, all string prop values were escaped when formatting the
|
|
62
|
-
// message. It is assumed that the message is not UGC, and came from the
|
|
63
|
-
// developer making it more like a template.
|
|
64
|
-
//
|
|
65
|
-
// Note: There's a perf impact of using this component since there's no
|
|
66
|
-
// way for React to do its virtual DOM diffing.
|
|
67
|
-
var html = { __html: formattedHTMLMessage };
|
|
68
|
-
return React.createElement(Component, { dangerouslySetInnerHTML: html });
|
|
69
|
-
}));
|
|
70
|
-
};
|
|
71
|
-
FormattedHTMLMessage.displayName = 'FormattedHTMLMessage';
|
|
72
|
-
FormattedHTMLMessage.defaultProps = __assign(__assign({}, message_1.default.defaultProps), { tagName: 'span' });
|
|
73
|
-
return FormattedHTMLMessage;
|
|
74
|
-
}(message_1.default));
|
|
75
|
-
exports.default = FormattedHTMLMessage;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { PrimitiveType } from 'intl-messageformat';
|
|
2
|
-
import FormattedMessage from './message';
|
|
3
|
-
declare class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
|
|
4
|
-
static displayName: string;
|
|
5
|
-
static defaultProps: {
|
|
6
|
-
tagName: "span";
|
|
7
|
-
values: {};
|
|
8
|
-
};
|
|
9
|
-
render(): JSX.Element;
|
|
10
|
-
}
|
|
11
|
-
export default FormattedHTMLMessage;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2015, Yahoo Inc.
|
|
3
|
-
* Copyrights licensed under the New BSD License.
|
|
4
|
-
* See the accompanying LICENSE file for terms.
|
|
5
|
-
*/
|
|
6
|
-
import * as React from 'react';
|
|
7
|
-
import FormattedMessage from './message';
|
|
8
|
-
import { Context } from './injectIntl';
|
|
9
|
-
import { invariantIntlContext } from '../utils';
|
|
10
|
-
class FormattedHTMLMessage extends FormattedMessage {
|
|
11
|
-
render() {
|
|
12
|
-
return (React.createElement(Context.Consumer, null, (intl) => {
|
|
13
|
-
if (!this.props.defaultMessage) {
|
|
14
|
-
invariantIntlContext(intl);
|
|
15
|
-
}
|
|
16
|
-
const { formatHTMLMessage, textComponent } = intl;
|
|
17
|
-
const { id, description, defaultMessage, values: rawValues, children, } = this.props;
|
|
18
|
-
let { tagName: Component } = this.props;
|
|
19
|
-
// This is bc of TS3.3 doesn't recognize `defaultProps`
|
|
20
|
-
if (!Component) {
|
|
21
|
-
Component = textComponent || 'span';
|
|
22
|
-
}
|
|
23
|
-
const descriptor = { id, description, defaultMessage };
|
|
24
|
-
const formattedHTMLMessage = formatHTMLMessage(descriptor, rawValues);
|
|
25
|
-
if (typeof children === 'function') {
|
|
26
|
-
return children(formattedHTMLMessage);
|
|
27
|
-
}
|
|
28
|
-
// Since the message presumably has HTML in it, we need to set
|
|
29
|
-
// `innerHTML` in order for it to be rendered and not escaped by React.
|
|
30
|
-
// To be safe, all string prop values were escaped when formatting the
|
|
31
|
-
// message. It is assumed that the message is not UGC, and came from the
|
|
32
|
-
// developer making it more like a template.
|
|
33
|
-
//
|
|
34
|
-
// Note: There's a perf impact of using this component since there's no
|
|
35
|
-
// way for React to do its virtual DOM diffing.
|
|
36
|
-
const html = { __html: formattedHTMLMessage };
|
|
37
|
-
return React.createElement(Component, { dangerouslySetInnerHTML: html });
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
FormattedHTMLMessage.displayName = 'FormattedHTMLMessage';
|
|
42
|
-
FormattedHTMLMessage.defaultProps = Object.assign(Object.assign({}, FormattedMessage.defaultProps), { tagName: 'span' });
|
|
43
|
-
export default FormattedHTMLMessage;
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2015, Yahoo Inc.
|
|
3
|
-
* Copyrights licensed under the New BSD License.
|
|
4
|
-
* See the accompanying LICENSE file for terms.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as React from 'react';
|
|
8
|
-
import {PrimitiveType} from 'intl-messageformat';
|
|
9
|
-
import FormattedMessage from './message';
|
|
10
|
-
import {Context} from './injectIntl';
|
|
11
|
-
import {invariantIntlContext} from '../utils';
|
|
12
|
-
|
|
13
|
-
class FormattedHTMLMessage extends FormattedMessage<
|
|
14
|
-
Record<string, PrimitiveType>
|
|
15
|
-
> {
|
|
16
|
-
static displayName = 'FormattedHTMLMessage';
|
|
17
|
-
static defaultProps = {
|
|
18
|
-
...FormattedMessage.defaultProps,
|
|
19
|
-
tagName: 'span' as 'span',
|
|
20
|
-
};
|
|
21
|
-
render(): JSX.Element {
|
|
22
|
-
return (
|
|
23
|
-
<Context.Consumer>
|
|
24
|
-
{(intl): React.ReactNode => {
|
|
25
|
-
if (!this.props.defaultMessage) {
|
|
26
|
-
invariantIntlContext(intl);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const {formatHTMLMessage, textComponent} = intl;
|
|
30
|
-
const {
|
|
31
|
-
id,
|
|
32
|
-
description,
|
|
33
|
-
defaultMessage,
|
|
34
|
-
values: rawValues,
|
|
35
|
-
children,
|
|
36
|
-
} = this.props;
|
|
37
|
-
|
|
38
|
-
let {tagName: Component} = this.props;
|
|
39
|
-
|
|
40
|
-
// This is bc of TS3.3 doesn't recognize `defaultProps`
|
|
41
|
-
if (!Component) {
|
|
42
|
-
Component = textComponent || 'span';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const descriptor = {id, description, defaultMessage};
|
|
46
|
-
const formattedHTMLMessage = formatHTMLMessage(descriptor, rawValues);
|
|
47
|
-
|
|
48
|
-
if (typeof children === 'function') {
|
|
49
|
-
return children(formattedHTMLMessage);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Since the message presumably has HTML in it, we need to set
|
|
53
|
-
// `innerHTML` in order for it to be rendered and not escaped by React.
|
|
54
|
-
// To be safe, all string prop values were escaped when formatting the
|
|
55
|
-
// message. It is assumed that the message is not UGC, and came from the
|
|
56
|
-
// developer making it more like a template.
|
|
57
|
-
//
|
|
58
|
-
// Note: There's a perf impact of using this component since there's no
|
|
59
|
-
// way for React to do its virtual DOM diffing.
|
|
60
|
-
const html = {__html: formattedHTMLMessage};
|
|
61
|
-
return <Component dangerouslySetInnerHTML={html} />;
|
|
62
|
-
}}
|
|
63
|
-
</Context.Consumer>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export default FormattedHTMLMessage;
|