react-intl 2.5.0 → 2.7.2
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/dist/react-intl.js +75 -55
- 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/lib/index.es.js +75 -55
- package/lib/index.js +75 -55
- package/locale-data/am.js +1 -1
- package/locale-data/ar.js +1 -1
- package/locale-data/ars.js +1 -1
- package/locale-data/as.js +1 -1
- package/locale-data/be.js +1 -1
- package/locale-data/bn.js +1 -1
- package/locale-data/br.js +1 -1
- package/locale-data/bs.js +1 -1
- package/locale-data/cs.js +1 -1
- package/locale-data/fa.js +1 -1
- package/locale-data/ff.js +1 -1
- package/locale-data/fr.js +1 -1
- package/locale-data/ga.js +1 -1
- package/locale-data/gd.js +1 -1
- package/locale-data/gu.js +1 -1
- package/locale-data/he.js +1 -1
- package/locale-data/hi.js +1 -1
- package/locale-data/hr.js +1 -1
- package/locale-data/hy.js +1 -1
- package/locale-data/index.js +1 -1
- package/locale-data/iw.js +1 -1
- package/locale-data/ka.js +1 -1
- package/locale-data/kab.js +1 -1
- package/locale-data/kn.js +1 -1
- package/locale-data/lt.js +1 -1
- package/locale-data/lv.js +1 -1
- package/locale-data/mo.js +1 -1
- package/locale-data/mr.js +1 -1
- package/locale-data/mt.js +1 -1
- package/locale-data/ne.js +1 -1
- package/locale-data/pl.js +1 -1
- package/locale-data/prg.js +1 -1
- package/locale-data/ro.js +1 -1
- package/locale-data/ru.js +1 -1
- package/locale-data/sh.js +1 -1
- package/locale-data/shi.js +1 -1
- package/locale-data/sk.js +1 -1
- package/locale-data/sr.js +1 -1
- package/locale-data/tzm.js +1 -1
- package/locale-data/uk.js +1 -1
- package/locale-data/zu.js +1 -1
- package/package.json +1 -1
- package/src/components/message.js +22 -2
- package/src/components/provider.js +16 -8
- package/src/format.js +55 -57
- package/src/inject.js +2 -2
- package/src/types.js +2 -0
- package/src/utils.js +11 -0
package/src/format.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import invariant from 'invariant';
|
|
8
8
|
import IntlRelativeFormat from 'intl-relativeformat';
|
|
9
|
+
import {isValidElement} from 'react';
|
|
9
10
|
|
|
10
11
|
import {
|
|
11
12
|
dateTimeFormatPropTypes,
|
|
@@ -14,7 +15,7 @@ import {
|
|
|
14
15
|
pluralFormatPropTypes,
|
|
15
16
|
} from './types';
|
|
16
17
|
|
|
17
|
-
import {escape, filterProps} from './utils';
|
|
18
|
+
import {createError, defaultErrorHandler, escape, filterProps} from './utils';
|
|
18
19
|
|
|
19
20
|
const DATE_TIME_FORMAT_OPTIONS = Object.keys(dateTimeFormatPropTypes);
|
|
20
21
|
const NUMBER_FORMAT_OPTIONS = Object.keys(numberFormatPropTypes);
|
|
@@ -45,25 +46,24 @@ function updateRelativeFormatThresholds(newThresholds) {
|
|
|
45
46
|
} = newThresholds);
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
function getNamedFormat(formats, type, name) {
|
|
49
|
+
function getNamedFormat(formats, type, name, onError) {
|
|
49
50
|
let format = formats && formats[type] && formats[type][name];
|
|
50
51
|
if (format) {
|
|
51
52
|
return format;
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
console.error(`[React Intl] No ${type} format named: ${name}`);
|
|
56
|
-
}
|
|
55
|
+
onError(createError(`No ${type} format named: ${name}`));
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
export function formatDate(config, state, value, options = {}) {
|
|
60
59
|
const {locale, formats, timeZone} = config;
|
|
61
60
|
const {format} = options;
|
|
62
61
|
|
|
62
|
+
let onError = config.onError || defaultErrorHandler;
|
|
63
63
|
let date = new Date(value);
|
|
64
64
|
let defaults = {
|
|
65
65
|
...(timeZone && {timeZone}),
|
|
66
|
-
...(format && getNamedFormat(formats, 'date', format)),
|
|
66
|
+
...(format && getNamedFormat(formats, 'date', format, onError)),
|
|
67
67
|
};
|
|
68
68
|
let filteredOptions = filterProps(
|
|
69
69
|
options,
|
|
@@ -74,9 +74,7 @@ export function formatDate(config, state, value, options = {}) {
|
|
|
74
74
|
try {
|
|
75
75
|
return state.getDateTimeFormat(locale, filteredOptions).format(date);
|
|
76
76
|
} catch (e) {
|
|
77
|
-
|
|
78
|
-
console.error(`[React Intl] Error formatting date.\n${e}`);
|
|
79
|
-
}
|
|
77
|
+
onError(createError('Error formatting date.', e));
|
|
80
78
|
}
|
|
81
79
|
|
|
82
80
|
return String(date);
|
|
@@ -86,10 +84,11 @@ export function formatTime(config, state, value, options = {}) {
|
|
|
86
84
|
const {locale, formats, timeZone} = config;
|
|
87
85
|
const {format} = options;
|
|
88
86
|
|
|
87
|
+
let onError = config.onError || defaultErrorHandler;
|
|
89
88
|
let date = new Date(value);
|
|
90
89
|
let defaults = {
|
|
91
90
|
...(timeZone && {timeZone}),
|
|
92
|
-
...(format && getNamedFormat(formats, 'time', format)),
|
|
91
|
+
...(format && getNamedFormat(formats, 'time', format, onError)),
|
|
93
92
|
};
|
|
94
93
|
let filteredOptions = filterProps(
|
|
95
94
|
options,
|
|
@@ -109,9 +108,7 @@ export function formatTime(config, state, value, options = {}) {
|
|
|
109
108
|
try {
|
|
110
109
|
return state.getDateTimeFormat(locale, filteredOptions).format(date);
|
|
111
110
|
} catch (e) {
|
|
112
|
-
|
|
113
|
-
console.error(`[React Intl] Error formatting time.\n${e}`);
|
|
114
|
-
}
|
|
111
|
+
onError(createError('Error formatting time.', e));
|
|
115
112
|
}
|
|
116
113
|
|
|
117
114
|
return String(date);
|
|
@@ -121,9 +118,10 @@ export function formatRelative(config, state, value, options = {}) {
|
|
|
121
118
|
const {locale, formats} = config;
|
|
122
119
|
const {format} = options;
|
|
123
120
|
|
|
121
|
+
let onError = config.onError || defaultErrorHandler;
|
|
124
122
|
let date = new Date(value);
|
|
125
123
|
let now = new Date(options.now);
|
|
126
|
-
let defaults = format && getNamedFormat(formats, 'relative', format);
|
|
124
|
+
let defaults = format && getNamedFormat(formats, 'relative', format, onError);
|
|
127
125
|
let filteredOptions = filterProps(options, RELATIVE_FORMAT_OPTIONS, defaults);
|
|
128
126
|
|
|
129
127
|
// Capture the current threshold values, then temporarily override them with
|
|
@@ -136,9 +134,7 @@ export function formatRelative(config, state, value, options = {}) {
|
|
|
136
134
|
now: isFinite(now) ? now : state.now(),
|
|
137
135
|
});
|
|
138
136
|
} catch (e) {
|
|
139
|
-
|
|
140
|
-
console.error(`[React Intl] Error formatting relative time.\n${e}`);
|
|
141
|
-
}
|
|
137
|
+
onError(createError('Error formatting relative time.', e));
|
|
142
138
|
} finally {
|
|
143
139
|
updateRelativeFormatThresholds(oldThresholds);
|
|
144
140
|
}
|
|
@@ -150,15 +146,14 @@ export function formatNumber(config, state, value, options = {}) {
|
|
|
150
146
|
const {locale, formats} = config;
|
|
151
147
|
const {format} = options;
|
|
152
148
|
|
|
153
|
-
let
|
|
149
|
+
let onError = config.onError || defaultErrorHandler;
|
|
150
|
+
let defaults = format && getNamedFormat(formats, 'number', format, onError);
|
|
154
151
|
let filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults);
|
|
155
152
|
|
|
156
153
|
try {
|
|
157
154
|
return state.getNumberFormat(locale, filteredOptions).format(value);
|
|
158
155
|
} catch (e) {
|
|
159
|
-
|
|
160
|
-
console.error(`[React Intl] Error formatting number.\n${e}`);
|
|
161
|
-
}
|
|
156
|
+
onError(createError('Error formatting number.', e));
|
|
162
157
|
}
|
|
163
158
|
|
|
164
159
|
return String(value);
|
|
@@ -168,13 +163,12 @@ export function formatPlural(config, state, value, options = {}) {
|
|
|
168
163
|
const {locale} = config;
|
|
169
164
|
|
|
170
165
|
let filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
|
|
166
|
+
let onError = config.onError || defaultErrorHandler;
|
|
171
167
|
|
|
172
168
|
try {
|
|
173
169
|
return state.getPluralFormat(locale, filteredOptions).format(value);
|
|
174
170
|
} catch (e) {
|
|
175
|
-
|
|
176
|
-
console.error(`[React Intl] Error formatting plural.\n${e}`);
|
|
177
|
-
}
|
|
171
|
+
onError(createError('Error formatting plural.', e));
|
|
178
172
|
}
|
|
179
173
|
|
|
180
174
|
return 'other';
|
|
@@ -190,6 +184,12 @@ export function formatMessage(
|
|
|
190
184
|
|
|
191
185
|
const {id, defaultMessage} = messageDescriptor;
|
|
192
186
|
|
|
187
|
+
// Produce a better error if the user calls `intl.formatMessage(element)`
|
|
188
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
189
|
+
invariant(!isValidElement(config), '[React Intl] Don\'t pass React elements to ' +
|
|
190
|
+
'formatMessage(), pass `.props`.');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
193
|
// `id` is a required field of a Message Descriptor.
|
|
194
194
|
invariant(id, '[React Intl] An `id` must be provided to format a message.');
|
|
195
195
|
|
|
@@ -203,6 +203,7 @@ export function formatMessage(
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
let formattedMessage;
|
|
206
|
+
let onError = config.onError || defaultErrorHandler;
|
|
206
207
|
|
|
207
208
|
if (message) {
|
|
208
209
|
try {
|
|
@@ -210,28 +211,28 @@ export function formatMessage(
|
|
|
210
211
|
|
|
211
212
|
formattedMessage = formatter.format(values);
|
|
212
213
|
} catch (e) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
`
|
|
216
|
-
(defaultMessage ? ', using default message as fallback.' : '')
|
|
217
|
-
|
|
218
|
-
)
|
|
219
|
-
|
|
214
|
+
onError(
|
|
215
|
+
createError(
|
|
216
|
+
`Error formatting message: "${id}" for locale: "${locale}"` +
|
|
217
|
+
(defaultMessage ? ', using default message as fallback.' : ''),
|
|
218
|
+
e
|
|
219
|
+
)
|
|
220
|
+
);
|
|
220
221
|
}
|
|
221
222
|
} else {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
`
|
|
223
|
+
// This prevents warnings from littering the console in development
|
|
224
|
+
// when no `messages` are passed into the <IntlProvider> for the
|
|
225
|
+
// default locale, and a default message is in the source.
|
|
226
|
+
if (
|
|
227
|
+
!defaultMessage ||
|
|
228
|
+
(locale && locale.toLowerCase() !== defaultLocale.toLowerCase())
|
|
229
|
+
) {
|
|
230
|
+
onError(
|
|
231
|
+
createError(
|
|
232
|
+
`Missing message: "${id}" for locale: "${locale}"` +
|
|
232
233
|
(defaultMessage ? ', using default message as fallback.' : '')
|
|
233
|
-
)
|
|
234
|
-
|
|
234
|
+
)
|
|
235
|
+
);
|
|
235
236
|
}
|
|
236
237
|
}
|
|
237
238
|
|
|
@@ -245,24 +246,21 @@ export function formatMessage(
|
|
|
245
246
|
|
|
246
247
|
formattedMessage = formatter.format(values);
|
|
247
248
|
} catch (e) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
`\n${e}`
|
|
252
|
-
);
|
|
253
|
-
}
|
|
249
|
+
onError(
|
|
250
|
+
createError(`Error formatting the default message for: "${id}"`, e)
|
|
251
|
+
);
|
|
254
252
|
}
|
|
255
253
|
}
|
|
256
254
|
|
|
257
255
|
if (!formattedMessage) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
`
|
|
261
|
-
`using message ${
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
)
|
|
265
|
-
|
|
256
|
+
onError(
|
|
257
|
+
createError(
|
|
258
|
+
`Cannot format message: "${id}", ` +
|
|
259
|
+
`using message ${message || defaultMessage
|
|
260
|
+
? 'source'
|
|
261
|
+
: 'id'} as fallback.`
|
|
262
|
+
)
|
|
263
|
+
);
|
|
266
264
|
}
|
|
267
265
|
|
|
268
266
|
return formattedMessage || message || defaultMessage || id;
|
package/src/inject.js
CHANGED
|
@@ -42,7 +42,7 @@ export default function injectIntl(WrappedComponent, options = {}) {
|
|
|
42
42
|
'`injectIntl()`'
|
|
43
43
|
);
|
|
44
44
|
|
|
45
|
-
return this.
|
|
45
|
+
return this._wrappedInstance;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
render() {
|
|
@@ -50,7 +50,7 @@ export default function injectIntl(WrappedComponent, options = {}) {
|
|
|
50
50
|
<WrappedComponent
|
|
51
51
|
{...this.props}
|
|
52
52
|
{...{[intlPropName]: this.context.intl}}
|
|
53
|
-
ref={withRef ?
|
|
53
|
+
ref={withRef ? /* istanbul ignore next */ (ref => this._wrappedInstance = ref) : null}
|
|
54
54
|
/>
|
|
55
55
|
);
|
|
56
56
|
}
|
package/src/types.js
CHANGED
package/src/utils.js
CHANGED
|
@@ -101,3 +101,14 @@ export function shouldIntlComponentUpdate(
|
|
|
101
101
|
)
|
|
102
102
|
);
|
|
103
103
|
}
|
|
104
|
+
|
|
105
|
+
export function createError(message, exception) {
|
|
106
|
+
const eMsg = exception ? `\n${exception}` : '';
|
|
107
|
+
return `[React Intl] ${message}${eMsg}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function defaultErrorHandler(error) {
|
|
111
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
112
|
+
console.error(error);
|
|
113
|
+
}
|
|
114
|
+
}
|