use-intl 3.16.0 → 3.17.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/dist/development/_IntlProvider.js +10 -4
- package/dist/development/core.js +6 -6
- package/dist/development/{createFormatter-ZTUVkKlh.js → createFormatter-D6te1ReQ.js} +45 -58
- package/dist/development/index.js +4 -2
- package/dist/development/{initializeConfig-BnOiVylt.js → initializeConfig-DOn-8mVr.js} +76 -0
- package/dist/development/react.js +10 -7
- package/dist/esm/_IntlProvider.js +1 -1
- package/dist/esm/core.js +1 -1
- package/dist/esm/createFormatter-C4S7xHL_.js +1 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/initializeConfig-BB2Ue6Z9.js +1 -0
- package/dist/esm/react.js +1 -1
- package/dist/production/_IntlProvider.js +1 -1
- package/dist/production/core.js +1 -1
- package/dist/production/createFormatter-DdXLo8F4.js +1 -0
- package/dist/production/index.js +1 -1
- package/dist/production/initializeConfig-BLiF0aUh.js +1 -0
- package/dist/production/react.js +1 -1
- package/dist/types/src/core/createBaseTranslator.d.ts +5 -5
- package/dist/types/src/core/createFormatter.d.ts +5 -2
- package/dist/types/src/core/createTranslator.d.ts +3 -3
- package/dist/types/src/core/createTranslatorImpl.d.ts +6 -7
- package/dist/types/src/core/formatters.d.ts +20 -0
- package/dist/types/src/core/index.d.ts +1 -0
- package/dist/types/src/core/initializeConfig.d.ts +1 -1
- package/dist/types/src/react/IntlContext.d.ts +6 -12
- package/dist/types/src/react/index.test.d.ts +1 -0
- package/dist/types/src/react/useIntlContext.d.ts +2 -10
- package/dist/types/src/react/useTranslationsImpl.d.ts +3 -4
- package/package.json +4 -3
- package/dist/esm/createFormatter-D7emvAET.js +0 -1
- package/dist/esm/initializeConfig-CeCQBa_L.js +0 -1
- package/dist/production/createFormatter-DY1vgx2z.js +0 -1
- package/dist/production/initializeConfig-iVzvEYey.js +0 -1
- package/dist/types/src/core/MessageFormatCache.d.ts +0 -5
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
|
-
var initializeConfig = require('./initializeConfig-
|
|
6
|
+
var initializeConfig = require('./initializeConfig-DOn-8mVr.js');
|
|
7
7
|
var IntlContext = require('./IntlContext-BKfsnzBx.js');
|
|
8
|
+
require('@formatjs/fast-memoize');
|
|
9
|
+
require('intl-messageformat');
|
|
8
10
|
|
|
9
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
12
|
|
|
@@ -22,7 +24,11 @@ function IntlProvider(_ref) {
|
|
|
22
24
|
onError,
|
|
23
25
|
timeZone
|
|
24
26
|
} = _ref;
|
|
25
|
-
|
|
27
|
+
// The formatter cache is released when the locale changes. For
|
|
28
|
+
// long-running apps with a persistent `IntlProvider` at the root,
|
|
29
|
+
// this can reduce the memory footprint (e.g. in React Native).
|
|
30
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
31
|
+
const formatters = React.useMemo(() => initializeConfig.createFormatters(), [locale]);
|
|
26
32
|
|
|
27
33
|
// Memoizing this value helps to avoid triggering a re-render of all
|
|
28
34
|
// context consumers in case the configuration didn't change. However,
|
|
@@ -43,8 +49,8 @@ function IntlProvider(_ref) {
|
|
|
43
49
|
onError,
|
|
44
50
|
timeZone
|
|
45
51
|
}),
|
|
46
|
-
|
|
47
|
-
}), [defaultTranslationValues, formats, getMessageFallback, locale,
|
|
52
|
+
formatters
|
|
53
|
+
}), [defaultTranslationValues, formats, formatters, getMessageFallback, locale, messages, now, onError, timeZone]);
|
|
48
54
|
return /*#__PURE__*/React__default.default.createElement(IntlContext.IntlContext.Provider, {
|
|
49
55
|
value: value
|
|
50
56
|
}, children);
|
package/dist/development/core.js
CHANGED
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var initializeConfig = require('./initializeConfig-
|
|
6
|
-
var createFormatter = require('./createFormatter-
|
|
5
|
+
var initializeConfig = require('./initializeConfig-DOn-8mVr.js');
|
|
6
|
+
var createFormatter = require('./createFormatter-D6te1ReQ.js');
|
|
7
|
+
require('@formatjs/fast-memoize');
|
|
7
8
|
require('intl-messageformat');
|
|
8
9
|
require('react');
|
|
9
10
|
|
|
10
11
|
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
11
12
|
let {
|
|
12
|
-
getMessageFallback,
|
|
13
13
|
messages,
|
|
14
14
|
namespace,
|
|
15
|
-
onError,
|
|
16
15
|
...rest
|
|
17
16
|
} = _ref;
|
|
18
17
|
// The `namespacePrefix` is part of the type system.
|
|
@@ -21,8 +20,6 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
21
20
|
namespace = createFormatter.resolveNamespace(namespace, namespacePrefix);
|
|
22
21
|
return createFormatter.createBaseTranslator({
|
|
23
22
|
...rest,
|
|
24
|
-
onError,
|
|
25
|
-
getMessageFallback,
|
|
26
23
|
messages,
|
|
27
24
|
namespace
|
|
28
25
|
});
|
|
@@ -38,6 +35,7 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
38
35
|
*/
|
|
39
36
|
function createTranslator(_ref) {
|
|
40
37
|
let {
|
|
38
|
+
_formatters = initializeConfig.createFormatters(),
|
|
41
39
|
getMessageFallback = initializeConfig.defaultGetMessageFallback,
|
|
42
40
|
messages,
|
|
43
41
|
namespace,
|
|
@@ -50,6 +48,7 @@ function createTranslator(_ref) {
|
|
|
50
48
|
return createTranslatorImpl({
|
|
51
49
|
...rest,
|
|
52
50
|
onError,
|
|
51
|
+
formatters: _formatters,
|
|
53
52
|
getMessageFallback,
|
|
54
53
|
// @ts-expect-error `messages` is allowed to be `undefined` here and will be handled internally
|
|
55
54
|
messages: {
|
|
@@ -61,6 +60,7 @@ function createTranslator(_ref) {
|
|
|
61
60
|
|
|
62
61
|
exports.IntlError = initializeConfig.IntlError;
|
|
63
62
|
exports.IntlErrorCode = initializeConfig.IntlErrorCode;
|
|
63
|
+
exports._createFormatters = initializeConfig.createFormatters;
|
|
64
64
|
exports.initializeConfig = initializeConfig.initializeConfig;
|
|
65
65
|
exports.createFormatter = createFormatter.createFormatter;
|
|
66
66
|
exports.createTranslator = createTranslator;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var IntlMessageFormat = require('intl-messageformat');
|
|
4
3
|
var React = require('react');
|
|
5
|
-
var initializeConfig = require('./initializeConfig-
|
|
4
|
+
var initializeConfig = require('./initializeConfig-DOn-8mVr.js');
|
|
5
|
+
var IntlMessageFormat = require('intl-messageformat');
|
|
6
6
|
|
|
7
7
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
8
|
|
|
@@ -53,6 +53,7 @@ function convertFormatsToIntlMessageFormat(formats, timeZone) {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// eslint-disable-next-line import/no-named-as-default -- False positive
|
|
56
|
+
|
|
56
57
|
function resolvePath(locale, messages, key, namespace) {
|
|
57
58
|
const fullKey = initializeConfig.joinPath(namespace, key);
|
|
58
59
|
if (!messages) {
|
|
@@ -132,9 +133,9 @@ function createBaseTranslatorImpl(_ref) {
|
|
|
132
133
|
let {
|
|
133
134
|
defaultTranslationValues,
|
|
134
135
|
formats: globalFormats,
|
|
136
|
+
formatters,
|
|
135
137
|
getMessageFallback = initializeConfig.defaultGetMessageFallback,
|
|
136
138
|
locale,
|
|
137
|
-
messageFormatCache,
|
|
138
139
|
messagesOrError,
|
|
139
140
|
namespace,
|
|
140
141
|
onError,
|
|
@@ -168,58 +169,45 @@ function createBaseTranslatorImpl(_ref) {
|
|
|
168
169
|
} catch (error) {
|
|
169
170
|
return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
170
171
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
} else {
|
|
184
|
-
code = initializeConfig.IntlErrorCode.INSUFFICIENT_PATH;
|
|
185
|
-
{
|
|
186
|
-
errorMessage = "Message at `".concat(initializeConfig.joinPath(namespace, key), "` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages");
|
|
187
|
-
}
|
|
172
|
+
if (typeof message === 'object') {
|
|
173
|
+
let code, errorMessage;
|
|
174
|
+
if (Array.isArray(message)) {
|
|
175
|
+
code = initializeConfig.IntlErrorCode.INVALID_MESSAGE;
|
|
176
|
+
{
|
|
177
|
+
errorMessage = "Message at `".concat(initializeConfig.joinPath(namespace, key), "` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages");
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
code = initializeConfig.IntlErrorCode.INSUFFICIENT_PATH;
|
|
181
|
+
{
|
|
182
|
+
errorMessage = "Message at `".concat(initializeConfig.joinPath(namespace, key), "` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages");
|
|
188
183
|
}
|
|
189
|
-
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
190
184
|
}
|
|
185
|
+
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
186
|
+
}
|
|
187
|
+
let messageFormat;
|
|
191
188
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return new Intl.DateTimeFormat(locales, {
|
|
209
|
-
timeZone,
|
|
210
|
-
...options
|
|
211
|
-
});
|
|
212
|
-
},
|
|
213
|
-
getPluralRules(locales, options) {
|
|
214
|
-
return new Intl.PluralRules(locales, options);
|
|
215
|
-
}
|
|
189
|
+
// Hot path that avoids creating an `IntlMessageFormat` instance
|
|
190
|
+
const plainMessage = getPlainMessage(message, values);
|
|
191
|
+
if (plainMessage) return plainMessage;
|
|
192
|
+
try {
|
|
193
|
+
messageFormat = formatters.getMessageFormat(message, locale, convertFormatsToIntlMessageFormat({
|
|
194
|
+
...globalFormats,
|
|
195
|
+
...formats
|
|
196
|
+
}, timeZone), {
|
|
197
|
+
formatters: {
|
|
198
|
+
...formatters,
|
|
199
|
+
getDateTimeFormat(locales, options) {
|
|
200
|
+
// Workaround for https://github.com/formatjs/formatjs/issues/4279
|
|
201
|
+
return formatters.getDateTimeFormat(locales, {
|
|
202
|
+
timeZone,
|
|
203
|
+
...options
|
|
204
|
+
});
|
|
216
205
|
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
messageFormatCache === null || messageFormatCache === void 0 || messageFormatCache.set(cacheKey, messageFormat);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
} catch (error) {
|
|
209
|
+
const thrownError = error;
|
|
210
|
+
return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.INVALID_MESSAGE, thrownError.message + ('originalMessage' in thrownError ? " (".concat(thrownError.originalMessage, ")") : '') );
|
|
223
211
|
}
|
|
224
212
|
try {
|
|
225
213
|
const formattedMessage = messageFormat.format(
|
|
@@ -354,6 +342,7 @@ function calculateRelativeTimeValue(seconds, unit) {
|
|
|
354
342
|
}
|
|
355
343
|
function createFormatter(_ref) {
|
|
356
344
|
let {
|
|
345
|
+
_formatters: formatters = initializeConfig.createFormatters(),
|
|
357
346
|
formats,
|
|
358
347
|
locale,
|
|
359
348
|
now: globalNow,
|
|
@@ -410,7 +399,7 @@ function createFormatter(_ref) {
|
|
|
410
399
|
formatOrOptions) {
|
|
411
400
|
return getFormattedValue(formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.dateTime, options => {
|
|
412
401
|
options = applyTimeZone(options);
|
|
413
|
-
return
|
|
402
|
+
return formatters.getDateTimeFormat(locale, options).format(value);
|
|
414
403
|
}, () => String(value));
|
|
415
404
|
}
|
|
416
405
|
function dateTimeRange( /** If a number is supplied, this is interpreted as a UTC timestamp. */
|
|
@@ -421,13 +410,11 @@ function createFormatter(_ref) {
|
|
|
421
410
|
formatOrOptions) {
|
|
422
411
|
return getFormattedValue(formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.dateTime, options => {
|
|
423
412
|
options = applyTimeZone(options);
|
|
424
|
-
return
|
|
413
|
+
return formatters.getDateTimeFormat(locale, options).formatRange(start, end);
|
|
425
414
|
}, () => [dateTime(start), dateTime(end)].join(' – '));
|
|
426
415
|
}
|
|
427
416
|
function number(value, formatOrOptions) {
|
|
428
|
-
return getFormattedValue(formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.number, options =>
|
|
429
|
-
// @ts-expect-error -- TS is currently lacking support for ECMA-402 10.0 (`useGrouping: 'auto'`, see https://github.com/microsoft/TypeScript/issues/56269)
|
|
430
|
-
options).format(value), () => String(value));
|
|
417
|
+
return getFormattedValue(formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.number, options => formatters.getNumberFormat(locale, options).format(value), () => String(value));
|
|
431
418
|
}
|
|
432
419
|
function getGlobalNow() {
|
|
433
420
|
if (globalNow) {
|
|
@@ -475,7 +462,7 @@ function createFormatter(_ref) {
|
|
|
475
462
|
// is the formatting of times <1 second as "now".
|
|
476
463
|
opts.numeric = unit === 'second' ? 'auto' : 'always';
|
|
477
464
|
const value = calculateRelativeTimeValue(seconds, unit);
|
|
478
|
-
return
|
|
465
|
+
return formatters.getRelativeTimeFormat(locale, opts).format(value, unit);
|
|
479
466
|
} catch (error) {
|
|
480
467
|
onError(new initializeConfig.IntlError(initializeConfig.IntlErrorCode.FORMATTING_ERROR, error.message));
|
|
481
468
|
return String(date);
|
|
@@ -503,7 +490,7 @@ function createFormatter(_ref) {
|
|
|
503
490
|
return getFormattedValue(formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.list,
|
|
504
491
|
// @ts-expect-error -- `richValues.size` is used to determine the return type, but TypeScript can't infer the meaning of this correctly
|
|
505
492
|
options => {
|
|
506
|
-
const result =
|
|
493
|
+
const result = formatters.getListFormat(locale, options).formatToParts(serializedValue).map(part => part.type === 'literal' ? part.value : richValues.get(part.value) || part.value);
|
|
507
494
|
if (richValues.size > 0) {
|
|
508
495
|
return result;
|
|
509
496
|
} else {
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var initializeConfig = require('./initializeConfig-
|
|
5
|
+
var initializeConfig = require('./initializeConfig-DOn-8mVr.js');
|
|
6
6
|
var core = require('./core.js');
|
|
7
|
-
var createFormatter = require('./createFormatter-
|
|
7
|
+
var createFormatter = require('./createFormatter-D6te1ReQ.js');
|
|
8
8
|
var _IntlProvider = require('./_IntlProvider.js');
|
|
9
9
|
var react = require('./react.js');
|
|
10
10
|
var _useLocale = require('./_useLocale-0Rl9uR82.js');
|
|
11
|
+
require('@formatjs/fast-memoize');
|
|
11
12
|
require('intl-messageformat');
|
|
12
13
|
require('react');
|
|
13
14
|
require('./IntlContext-BKfsnzBx.js');
|
|
@@ -16,6 +17,7 @@ require('./IntlContext-BKfsnzBx.js');
|
|
|
16
17
|
|
|
17
18
|
exports.IntlError = initializeConfig.IntlError;
|
|
18
19
|
exports.IntlErrorCode = initializeConfig.IntlErrorCode;
|
|
20
|
+
exports._createFormatters = initializeConfig.createFormatters;
|
|
19
21
|
exports.initializeConfig = initializeConfig.initializeConfig;
|
|
20
22
|
exports.createTranslator = core.createTranslator;
|
|
21
23
|
exports.createFormatter = createFormatter.createFormatter;
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var fastMemoize = require('@formatjs/fast-memoize');
|
|
4
|
+
var IntlMessageFormat = require('intl-messageformat');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var IntlMessageFormat__default = /*#__PURE__*/_interopDefault(IntlMessageFormat);
|
|
9
|
+
|
|
3
10
|
function _defineProperty(e, r, t) {
|
|
4
11
|
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
5
12
|
value: t,
|
|
@@ -68,6 +75,74 @@ function defaultOnError(error) {
|
|
|
68
75
|
console.error(error);
|
|
69
76
|
}
|
|
70
77
|
|
|
78
|
+
function createIntlCache() {
|
|
79
|
+
return {
|
|
80
|
+
dateTime: {},
|
|
81
|
+
number: {},
|
|
82
|
+
message: {},
|
|
83
|
+
relativeTime: {},
|
|
84
|
+
pluralRules: {},
|
|
85
|
+
list: {},
|
|
86
|
+
displayNames: {}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function createMemoCache(store) {
|
|
90
|
+
return {
|
|
91
|
+
create() {
|
|
92
|
+
return {
|
|
93
|
+
get(key) {
|
|
94
|
+
return store[key];
|
|
95
|
+
},
|
|
96
|
+
set(key, value) {
|
|
97
|
+
store[key] = value;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function memoFn(fn, cache) {
|
|
104
|
+
return fastMemoize.memoize(fn, {
|
|
105
|
+
cache: createMemoCache(cache),
|
|
106
|
+
strategy: fastMemoize.strategies.variadic
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function memoConstructor(ConstructorFn, cache) {
|
|
110
|
+
return memoFn(function () {
|
|
111
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
112
|
+
args[_key] = arguments[_key];
|
|
113
|
+
}
|
|
114
|
+
return new ConstructorFn(...args);
|
|
115
|
+
}, cache);
|
|
116
|
+
}
|
|
117
|
+
function createFormatters() {
|
|
118
|
+
const cache = createIntlCache();
|
|
119
|
+
const getDateTimeFormat = memoConstructor(Intl.DateTimeFormat, cache.dateTime);
|
|
120
|
+
const getNumberFormat = memoConstructor(Intl.NumberFormat, cache.number);
|
|
121
|
+
const getPluralRules = memoConstructor(Intl.PluralRules, cache.pluralRules);
|
|
122
|
+
const getRelativeTimeFormat = memoConstructor(Intl.RelativeTimeFormat, cache.relativeTime);
|
|
123
|
+
const getListFormat = memoConstructor(Intl.ListFormat, cache.list);
|
|
124
|
+
const getDisplayNames = memoConstructor(Intl.DisplayNames, cache.displayNames);
|
|
125
|
+
const getMessageFormat = memoFn(function () {
|
|
126
|
+
return new IntlMessageFormat__default.default(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1], arguments.length <= 2 ? undefined : arguments[2], {
|
|
127
|
+
formatters: {
|
|
128
|
+
getNumberFormat,
|
|
129
|
+
getDateTimeFormat,
|
|
130
|
+
getPluralRules
|
|
131
|
+
},
|
|
132
|
+
...(arguments.length <= 3 ? undefined : arguments[3])
|
|
133
|
+
});
|
|
134
|
+
}, cache.message);
|
|
135
|
+
return {
|
|
136
|
+
getDateTimeFormat,
|
|
137
|
+
getNumberFormat,
|
|
138
|
+
getPluralRules,
|
|
139
|
+
getMessageFormat,
|
|
140
|
+
getRelativeTimeFormat,
|
|
141
|
+
getListFormat,
|
|
142
|
+
getDisplayNames
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
71
146
|
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
72
147
|
Object.entries(messages).forEach(_ref => {
|
|
73
148
|
let [key, messageOrMessages] = _ref;
|
|
@@ -116,6 +191,7 @@ function initializeConfig(_ref) {
|
|
|
116
191
|
|
|
117
192
|
exports.IntlError = IntlError;
|
|
118
193
|
exports.IntlErrorCode = IntlErrorCode;
|
|
194
|
+
exports.createFormatters = createFormatters;
|
|
119
195
|
exports.defaultGetMessageFallback = defaultGetMessageFallback;
|
|
120
196
|
exports.defaultOnError = defaultOnError;
|
|
121
197
|
exports.initializeConfig = initializeConfig;
|
|
@@ -5,10 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var _IntlProvider = require('./_IntlProvider.js');
|
|
6
6
|
var _useLocale = require('./_useLocale-0Rl9uR82.js');
|
|
7
7
|
var React = require('react');
|
|
8
|
-
var createFormatter = require('./createFormatter-
|
|
9
|
-
var initializeConfig = require('./initializeConfig-
|
|
8
|
+
var createFormatter = require('./createFormatter-D6te1ReQ.js');
|
|
9
|
+
var initializeConfig = require('./initializeConfig-DOn-8mVr.js');
|
|
10
10
|
require('./IntlContext-BKfsnzBx.js');
|
|
11
11
|
require('intl-messageformat');
|
|
12
|
+
require('@formatjs/fast-memoize');
|
|
12
13
|
|
|
13
14
|
let hasWarnedForMissingTimezone = false;
|
|
14
15
|
const isServer = typeof window === 'undefined';
|
|
@@ -16,9 +17,9 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
16
17
|
const {
|
|
17
18
|
defaultTranslationValues,
|
|
18
19
|
formats: globalFormats,
|
|
20
|
+
formatters,
|
|
19
21
|
getMessageFallback,
|
|
20
22
|
locale,
|
|
21
|
-
messageFormatCache,
|
|
22
23
|
onError,
|
|
23
24
|
timeZone
|
|
24
25
|
} = _useLocale.useIntlContext();
|
|
@@ -32,7 +33,7 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
32
33
|
onError(new initializeConfig.IntlError(initializeConfig.IntlErrorCode.ENVIRONMENT_FALLBACK, "There is no `timeZone` configured, this can lead to markup mismatches caused by environment differences. Consider adding a global default: https://next-intl-docs.vercel.app/docs/configuration#time-zone" ));
|
|
33
34
|
}
|
|
34
35
|
const translate = React.useMemo(() => createFormatter.createBaseTranslator({
|
|
35
|
-
|
|
36
|
+
formatters,
|
|
36
37
|
getMessageFallback,
|
|
37
38
|
messages: allMessages,
|
|
38
39
|
defaultTranslationValues,
|
|
@@ -41,7 +42,7 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
41
42
|
formats: globalFormats,
|
|
42
43
|
locale,
|
|
43
44
|
timeZone
|
|
44
|
-
}), [
|
|
45
|
+
}), [formatters, getMessageFallback, allMessages, defaultTranslationValues, namespace, onError, globalFormats, locale, timeZone]);
|
|
45
46
|
return translate;
|
|
46
47
|
}
|
|
47
48
|
|
|
@@ -122,6 +123,7 @@ function useMessages() {
|
|
|
122
123
|
function useFormatter() {
|
|
123
124
|
const {
|
|
124
125
|
formats,
|
|
126
|
+
formatters,
|
|
125
127
|
locale,
|
|
126
128
|
now: globalNow,
|
|
127
129
|
onError,
|
|
@@ -132,8 +134,9 @@ function useFormatter() {
|
|
|
132
134
|
locale,
|
|
133
135
|
now: globalNow,
|
|
134
136
|
onError,
|
|
135
|
-
timeZone
|
|
136
|
-
|
|
137
|
+
timeZone,
|
|
138
|
+
_formatters: formatters
|
|
139
|
+
}), [formats, formatters, globalNow, locale, onError, timeZone]);
|
|
137
140
|
}
|
|
138
141
|
|
|
139
142
|
exports.IntlProvider = _IntlProvider.IntlProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import e,{
|
|
1
|
+
import e,{useMemo as t}from"react";import{c as a,i as o}from"./initializeConfig-BB2Ue6Z9.js";import{I as r}from"./IntlContext-DoS4CDM3.js";import"@formatjs/fast-memoize";import"intl-messageformat";function s(s){let{children:l,defaultTranslationValues:m,formats:n,getMessageFallback:i,locale:f,messages:c,now:g,onError:u,timeZone:p}=s;const d=t((()=>a()),[f]),j=t((()=>({...o({locale:f,defaultTranslationValues:m,formats:n,getMessageFallback:i,messages:c,now:g,onError:u,timeZone:p}),formatters:d})),[m,n,d,i,f,c,g,u,p]);return e.createElement(r.Provider,{value:j},l)}export{s as IntlProvider};
|
package/dist/esm/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{c as e,b as r,d as t}from"./initializeConfig-BB2Ue6Z9.js";export{I as IntlError,a as IntlErrorCode,i as initializeConfig}from"./initializeConfig-BB2Ue6Z9.js";import{r as s,a as o}from"./createFormatter-C4S7xHL_.js";export{c as createFormatter}from"./createFormatter-C4S7xHL_.js";import"@formatjs/fast-memoize";import"intl-messageformat";import"react";function m(a){let{_formatters:m=e(),getMessageFallback:i=r,messages:n,namespace:c,onError:f=t,...g}=a;return function(e,a){let{messages:r,namespace:t,...m}=e;return r=r[a],t=s(t,a),o({...m,messages:r,namespace:t})}({...g,onError:f,formatters:m,getMessageFallback:i,messages:{"!":n},namespace:c?"!.".concat(c):"!"},"!")}export{e as _createFormatters,m as createTranslator};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isValidElement as e,cloneElement as t}from"react";import{I as n,a as r,j as o,d as a,b as s,c as i}from"./initializeConfig-BB2Ue6Z9.js";import c from"intl-messageformat";function u(e,t){return e?Object.keys(e).reduce(((n,r)=>(n[r]={timeZone:t,...e[r]},n)),{}):e}function m(e,t,n,r){const a=o(r,n);if(!t)throw new Error("No messages available at `".concat(r,"`."));let s=t;return n.split(".").forEach((t=>{const n=s[t];if(null==t||null==n)throw new Error("Could not resolve `".concat(a,"` in messages for locale `").concat(e,"`."));s=n})),s}function l(i){const l=function(e,t,o){let s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:a;try{if(!t)throw new Error("No messages were configured on the provider.");const n=o?m(e,t,o):t;if(!n)throw new Error("No messages for namespace `".concat(o,"` found."));return n}catch(e){const t=new n(r.MISSING_MESSAGE,e.message);return s(t),t}}(i.locale,i.messages,i.namespace,i.onError);return function(a){let{defaultTranslationValues:i,formats:l,formatters:f,getMessageFallback:g=s,locale:d,messagesOrError:p,namespace:y,onError:h,timeZone:v}=a;function w(e,t,r){const o=new n(t,r);return h(o),g({error:o,key:e,namespace:y})}function E(a,s,h){if(p instanceof n)return g({error:p,key:a,namespace:y});const E=p;let T,S;try{T=m(d,E,a,y)}catch(e){return w(a,r.MISSING_MESSAGE,e.message)}if("object"==typeof T){let e,t;return Array.isArray(T)?(e=r.INVALID_MESSAGE,t="Message at `".concat(o(y,a),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(e=r.INSUFFICIENT_PATH,t="Message at `".concat(o(y,a),"` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages")),w(a,e,t)}const b=function(e,t){if(t)return;const n=e.replace(/'([{}])/gi,"$1");return/<|{/.test(n)?void 0:n}(T,s);if(b)return b;try{S=f.getMessageFormat(T,d,function(e,t){const n=t?{...e,dateTime:u(e.dateTime,t)}:e,r=c.formats.date,o=t?u(r,t):r,a=c.formats.time,s=t?u(a,t):a;return{...n,date:{...o,...null==n?void 0:n.dateTime},time:{...s,...null==n?void 0:n.dateTime}}}({...l,...h},v),{formatters:{...f,getDateTimeFormat:(e,t)=>f.getDateTimeFormat(e,{timeZone:v,...t})}})}catch(e){const t=e;return w(a,r.INVALID_MESSAGE,t.message+("originalMessage"in t?" (".concat(t.originalMessage,")"):""))}try{const n=S.format(function(n){if(0===Object.keys(n).length)return;const r={};return Object.keys(n).forEach((o=>{let a=0;const s=n[o];let i;i="function"==typeof s?n=>{const r=s(n);return e(r)?t(r,{key:o+a++}):r}:s,r[o]=i})),r}({...i,...s}));if(null==n)throw new Error("Unable to format `".concat(a,"` in ").concat(y?"namespace `".concat(y,"`"):"messages"));return e(n)||Array.isArray(n)||"string"==typeof n?n:String(n)}catch(e){return w(a,r.FORMATTING_ERROR,e.message)}}function T(e,t,n){const o=E(e,t,n);return"string"!=typeof o?w(e,r.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(y?"namespace `".concat(y,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):o}return T.rich=E,T.markup=(e,t,o)=>{const a=E(e,t,o);if("string"!=typeof a){const t=new n(r.FORMATTING_ERROR,"`t.markup` only accepts functions for formatting that receive and return strings.\n\nE.g. t.markup('markup', {b: (chunks) => `<b>${chunks}</b>`})");return h(t),g({error:t,key:e,namespace:y})}return a},T.raw=e=>{if(p instanceof n)return g({error:p,key:e,namespace:y});const t=p;try{return m(d,t,e,y)}catch(t){return w(e,r.MISSING_MESSAGE,t.message)}},T}({...i,messagesOrError:l})}function f(e,t){return e===t?void 0:e.slice((t+".").length)}const g=60,d=60*g,p=24*d,y=7*p,h=p*(365/12),v=3*h,w=365*p,E={second:1,seconds:1,minute:g,minutes:g,hour:d,hours:d,day:p,days:p,week:y,weeks:y,month:h,months:h,quarter:v,quarters:v,year:w,years:w};function T(e){let{_formatters:t=i(),formats:o,locale:s,now:c,onError:u=a,timeZone:m}=e;function l(e){var t;return null!==(t=e)&&void 0!==t&&t.timeZone||(m?e={...e,timeZone:m}:u(new n(r.ENVIRONMENT_FALLBACK,"The `timeZone` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone"))),e}function f(e,t,o,a){let s;try{s=function(e,t){let o;if("string"==typeof t){const a=t;if(o=null==e?void 0:e[a],!o){const e=new n(r.MISSING_FORMAT,"Format `".concat(a,"` is not available. You can configure it on the provider or provide custom options."));throw u(e),e}}else o=t;return o}(t,e)}catch(e){return a()}try{return o(s)}catch(e){return u(new n(r.FORMATTING_ERROR,e.message)),a()}}function v(e,n){return f(n,null==o?void 0:o.dateTime,(n=>(n=l(n),t.getDateTimeFormat(s,n).format(e))),(()=>String(e)))}function T(){return c||(u(new n(r.ENVIRONMENT_FALLBACK,"The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now")),new Date)}return{dateTime:v,number:function(e,n){return f(n,null==o?void 0:o.number,(n=>t.getNumberFormat(s,n).format(e)),(()=>String(e)))},relativeTime:function(e,o){try{let n,r;const a={};o instanceof Date||"number"==typeof o?n=new Date(o):o&&(n=null!=o.now?new Date(o.now):T(),r=o.unit,a.style=o.style,a.numberingSystem=o.numberingSystem),n||(n=T());const i=(new Date(e).getTime()-n.getTime())/1e3;r||(r=function(e){const t=Math.abs(e);return t<g?"second":t<d?"minute":t<p?"hour":t<y?"day":t<h?"week":t<w?"month":"year"}(i)),a.numeric="second"===r?"auto":"always";const c=function(e,t){return Math.round(e/E[t])}(i,r);return t.getRelativeTimeFormat(s,a).format(c,r)}catch(t){return u(new n(r.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,n){const r=[],a=new Map;let i=0;for(const t of e){let e;"object"==typeof t?(e=String(i),a.set(e,t)):e=String(t),r.push(e),i++}return f(n,null==o?void 0:o.list,(e=>{const n=t.getListFormat(s,e).formatToParts(r).map((e=>"literal"===e.type?e.value:a.get(e.value)||e.value));return a.size>0?n:n.join("")}),(()=>String(e)))},dateTimeRange:function(e,n,r){return f(r,null==o?void 0:o.dateTime,(r=>(r=l(r),t.getDateTimeFormat(s,r).formatRange(e,n))),(()=>[v(e),v(n)].join(" – ")))}}}export{l as a,T as c,f as r};
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{I as IntlError,a as IntlErrorCode,i as initializeConfig}from"./initializeConfig-
|
|
1
|
+
export{I as IntlError,a as IntlErrorCode,c as _createFormatters,i as initializeConfig}from"./initializeConfig-BB2Ue6Z9.js";export{createTranslator}from"./core.js";export{c as createFormatter}from"./createFormatter-C4S7xHL_.js";export{IntlProvider}from"./_IntlProvider.js";export{useFormatter,useMessages,useNow,useTimeZone,useTranslations}from"./react.js";export{u as useLocale}from"./_useLocale-v-ZT5JoE.js";import"@formatjs/fast-memoize";import"intl-messageformat";import"react";import"./IntlContext-DoS4CDM3.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{memoize as e,strategies as t}from"@formatjs/fast-memoize";import n from"intl-messageformat";function r(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}let o=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class a extends Error{constructor(e,t){let n=e;t&&(n+=": "+t),super(n),r(this,"code",void 0),r(this,"originalMessage",void 0),this.code=e,t&&(this.originalMessage=t)}}function i(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return t.filter(Boolean).join(".")}function s(e){return i(e.namespace,e.key)}function l(e){console.error(e)}function u(n,r){return e(n,{cache:(o=r,{create:()=>({get:e=>o[e],set(e,t){o[e]=t}})}),strategy:t.variadic});var o}function c(e,t){return u((function(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];return new e(...n)}),t)}function m(){const e={dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}},t=c(Intl.DateTimeFormat,e.dateTime),r=c(Intl.NumberFormat,e.number),o=c(Intl.PluralRules,e.pluralRules),a=c(Intl.RelativeTimeFormat,e.relativeTime),i=c(Intl.ListFormat,e.list),s=c(Intl.DisplayNames,e.displayNames),l=u((function(){return new n(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1],arguments.length<=2?void 0:arguments[2],{formatters:{getNumberFormat:r,getDateTimeFormat:t,getPluralRules:o},...arguments.length<=3?void 0:arguments[3]})}),e.message);return{getDateTimeFormat:t,getNumberFormat:r,getPluralRules:o,getMessageFormat:l,getRelativeTimeFormat:a,getListFormat:i,getDisplayNames:s}}function g(e,t,n){Object.entries(e).forEach((e=>{let[r,o]=e;if(r.includes(".")){let e=r;n&&(e+=" (at ".concat(n,")")),t.push(e)}null!=o&&"object"==typeof o&&g(o,t,i(n,r))}))}function f(e,t){const n=[];g(e,n),n.length>0&&t(new a(o.INVALID_KEY,'Namespace keys can not contain the character "." as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid '.concat(1===n.length?"key":"keys",": ").concat(n.join(", "),'\n\nIf you\'re migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from "lodash";\n\nconst input = {\n "one.one": "1.1",\n "one.two": "1.2",\n "two.one.one": "2.1.1"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// "one": {\n// "one": "1.1",\n// "two": "1.2"\n// },\n// "two": {\n// "one": {\n// "one": "2.1.1"\n// }\n// }\n// }\n')))}function I(e){let{getMessageFallback:t,messages:n,onError:r,...o}=e;const a=r||l,i=t||s;return n&&f(n,a),{...o,messages:n,onError:a,getMessageFallback:i}}export{a as I,o as a,s as b,m as c,l as d,I as i,i as j};
|
package/dist/esm/react.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{IntlProvider}from"./_IntlProvider.js";import{a as e}from"./_useLocale-v-ZT5JoE.js";export{u as useLocale}from"./_useLocale-v-ZT5JoE.js";import{useMemo as
|
|
1
|
+
export{IntlProvider}from"./_IntlProvider.js";import{a as e}from"./_useLocale-v-ZT5JoE.js";export{u as useLocale}from"./_useLocale-v-ZT5JoE.js";import{useMemo as t,useState as o,useEffect as r}from"react";import{r as n,a,c as s}from"./createFormatter-C4S7xHL_.js";import{I as i,a as m}from"./initializeConfig-BB2Ue6Z9.js";import"./IntlContext-DoS4CDM3.js";import"intl-messageformat";import"@formatjs/fast-memoize";let c=!1;const l="undefined"==typeof window;function f(o){return function(o,r,s){const{defaultTranslationValues:f,formats:u,formatters:d,getMessageFallback:p,locale:g,onError:v,timeZone:w}=e();return o=o[s],r=n(r,s),w||c||!l||(c=!0,v(new i(m.ENVIRONMENT_FALLBACK,"There is no `timeZone` configured, this can lead to markup mismatches caused by environment differences. Consider adding a global default: https://next-intl-docs.vercel.app/docs/configuration#time-zone"))),t((()=>a({formatters:d,getMessageFallback:p,messages:o,defaultTranslationValues:f,namespace:r,onError:v,formats:u,locale:g,timeZone:w})),[d,p,o,f,r,v,u,g,w])}({"!":e().messages},o?"!.".concat(o):"!","!")}function d(){return new Date}function p(t){const n=null==t?void 0:t.updateInterval,{now:a}=e(),[s,i]=o(a||d());return r((()=>{if(!n)return;const e=setInterval((()=>{i(d())}),n);return()=>{clearInterval(e)}}),[a,n]),null==n&&a?a:s}function g(){return e().timeZone}function v(){const t=e();if(!t.messages)throw new Error("No messages found. Have you configured them correctly? See https://next-intl-docs.vercel.app/docs/configuration#messages");return t.messages}function w(){const{formats:o,formatters:r,locale:n,now:a,onError:i,timeZone:m}=e();return t((()=>s({formats:o,locale:n,now:a,onError:i,timeZone:m,_formatters:r})),[o,r,a,n,i,m])}export{w as useFormatter,v as useMessages,p as useNow,g as useTimeZone,f as useTranslations};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("./initializeConfig-BLiF0aUh.js"),t=require("./IntlContext-DcFt0tgW.js");function a(e){return e&&e.__esModule?e:{default:e}}require("@formatjs/fast-memoize"),require("intl-messageformat");var o=a(e);exports.IntlProvider=function(a){let{children:s,defaultTranslationValues:n,formats:l,getMessageFallback:i,locale:u,messages:m,now:f,onError:c,timeZone:d}=a;const g=e.useMemo((()=>r.createFormatters()),[u]),v=e.useMemo((()=>({...r.initializeConfig({locale:u,defaultTranslationValues:n,formats:l,getMessageFallback:i,messages:m,now:f,onError:c,timeZone:d}),formatters:g})),[n,l,g,i,u,m,f,c,d]);return o.default.createElement(t.IntlContext.Provider,{value:v},s)};
|
package/dist/production/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-DdXLo8F4.js"),r=require("./initializeConfig-BLiF0aUh.js");require("react"),require("intl-messageformat"),require("@formatjs/fast-memoize"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createFormatter=e.createFormatter,exports._createFormatters=r.createFormatters,exports.initializeConfig=r.initializeConfig,exports.createTranslator=function(t){let{_formatters:a=r.createFormatters(),getMessageFallback:s=r.defaultGetMessageFallback,messages:o,namespace:n,onError:i=r.defaultOnError,...c}=t;return function(r,t){let{messages:a,namespace:s,...o}=r;return a=a[t],s=e.resolveNamespace(s,t),e.createBaseTranslator({...o,messages:a,namespace:s})}({...c,onError:i,formatters:a,getMessageFallback:s,messages:{"!":o},namespace:n?"!.".concat(n):"!"},"!")};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("react"),t=require("intl-messageformat"),r=require("./initializeConfig-BLiF0aUh.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(t);function i(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}let a=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class s extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),i(this,"code",void 0),i(this,"originalMessage",void 0),this.code=e,t&&(this.originalMessage=t)}}function u(e,t){return e?Object.keys(e).reduce(((r,n)=>(r[n]={timeZone:t,...e[n]},r)),{}):e}function c(e,t,n,o){const i=r.joinPath(o,n);if(!t)throw new Error(i);let a=t;return n.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(i+" (".concat(e,")"));a=r})),a}const l=60,m=60*l,f=24*m,E=7*f,d=f*(365/12),g=3*d,I=365*f,S={second:1,seconds:1,minute:l,minutes:l,hour:m,hours:m,day:f,days:f,week:E,weeks:E,month:d,months:d,quarter:g,quarters:g,year:I,years:I};exports.IntlError=s,exports.IntlErrorCode=a,exports.createBaseTranslator=function(t){const n=function(e,t,n){let o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:r.defaultOnError;try{if(!t)throw new Error(void 0);const r=n?c(e,t,n):t;if(!r)throw new Error(n);return r}catch(e){const t=new s(a.MISSING_MESSAGE,e.message);return o(t),t}}(t.locale,t.messages,t.namespace,t.onError);return function(t){let{defaultTranslationValues:n,formats:i,formatters:l,getMessageFallback:m=r.defaultGetMessageFallback,locale:f,messagesOrError:E,namespace:d,onError:g,timeZone:I}=t;function S(e,t,r){const n=new s(t,r);return g(n),m({error:n,key:e,namespace:d})}function y(t,r,g){if(E instanceof s)return m({error:E,key:t,namespace:d});const y=E;let T,N;try{T=c(f,y,t,d)}catch(e){return S(t,a.MISSING_MESSAGE,e.message)}if("object"==typeof T){let e,r;return e=Array.isArray(T)?a.INVALID_MESSAGE:a.INSUFFICIENT_PATH,S(t,e,r)}const A=function(e,t){if(t)return;const r=e.replace(/'([{}])/gi,"$1");return/<|{/.test(r)?void 0:r}(T,r);if(A)return A;try{N=l.getMessageFormat(T,f,function(e,t){const r=t?{...e,dateTime:u(e.dateTime,t)}:e,n=o.default.formats.date,i=t?u(n,t):n,a=o.default.formats.time,s=t?u(a,t):a;return{...r,date:{...i,...null==r?void 0:r.dateTime},time:{...s,...null==r?void 0:r.dateTime}}}({...i,...g},I),{formatters:{...l,getDateTimeFormat:(e,t)=>l.getDateTimeFormat(e,{timeZone:I,...t})}})}catch(e){const r=e;return S(t,a.INVALID_MESSAGE,r.message)}try{const t=N.format(function(t){if(0===Object.keys(t).length)return;const r={};return Object.keys(t).forEach((n=>{let o=0;const i=t[n];let a;a="function"==typeof i?t=>{const r=i(t);return e.isValidElement(r)?e.cloneElement(r,{key:n+o++}):r}:i,r[n]=a})),r}({...n,...r}));if(null==t)throw new Error(void 0);return e.isValidElement(t)||Array.isArray(t)||"string"==typeof t?t:String(t)}catch(e){return S(t,a.FORMATTING_ERROR,e.message)}}function T(e,t,r){const n=y(e,t,r);return"string"!=typeof n?S(e,a.INVALID_MESSAGE,void 0):n}return T.rich=y,T.markup=(e,t,r)=>{const n=y(e,t,r);if("string"!=typeof n){const t=new s(a.FORMATTING_ERROR,void 0);return g(t),m({error:t,key:e,namespace:d})}return n},T.raw=e=>{if(E instanceof s)return m({error:E,key:e,namespace:d});const t=E;try{return c(f,t,e,d)}catch(t){return S(e,a.MISSING_MESSAGE,t.message)}},T}({...t,messagesOrError:n})},exports.createFormatter=function(e){let{_formatters:t=r.createFormatters(),formats:n,locale:o,now:i,onError:u=r.defaultOnError,timeZone:c}=e;function g(e){var t;return null!==(t=e)&&void 0!==t&&t.timeZone||(c?e={...e,timeZone:c}:u(new s(a.ENVIRONMENT_FALLBACK,void 0))),e}function y(e,t,r,n){let o;try{o=function(e,t){let r;if("string"==typeof t){if(r=null==e?void 0:e[t],!r){const e=new s(a.MISSING_FORMAT,void 0);throw u(e),e}}else r=t;return r}(t,e)}catch(e){return n()}try{return r(o)}catch(e){return u(new s(a.FORMATTING_ERROR,e.message)),n()}}function T(e,r){return y(r,null==n?void 0:n.dateTime,(r=>(r=g(r),t.getDateTimeFormat(o,r).format(e))),(()=>String(e)))}function N(){return i||(u(new s(a.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:T,number:function(e,r){return y(r,null==n?void 0:n.number,(r=>t.getNumberFormat(o,r).format(e)),(()=>String(e)))},relativeTime:function(e,r){try{let n,i;const a={};r instanceof Date||"number"==typeof r?n=new Date(r):r&&(n=null!=r.now?new Date(r.now):N(),i=r.unit,a.style=r.style,a.numberingSystem=r.numberingSystem),n||(n=N());const s=(new Date(e).getTime()-n.getTime())/1e3;i||(i=function(e){const t=Math.abs(e);return t<l?"second":t<m?"minute":t<f?"hour":t<E?"day":t<d?"week":t<I?"month":"year"}(s)),a.numeric="second"===i?"auto":"always";const u=function(e,t){return Math.round(e/S[t])}(s,i);return t.getRelativeTimeFormat(o,a).format(u,i)}catch(t){return u(new s(a.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){const i=[],a=new Map;let s=0;for(const t of e){let e;"object"==typeof t?(e=String(s),a.set(e,t)):e=String(t),i.push(e),s++}return y(r,null==n?void 0:n.list,(e=>{const r=t.getListFormat(o,e).formatToParts(i).map((e=>"literal"===e.type?e.value:a.get(e.value)||e.value));return a.size>0?r:r.join("")}),(()=>String(e)))},dateTimeRange:function(e,r,i){return y(i,null==n?void 0:n.dateTime,(n=>(n=g(n),t.getDateTimeFormat(o,n).formatRange(e,r))),(()=>[T(e),T(r)].join(" – ")))}}},exports.resolveNamespace=function(e,t){return e===t?void 0:e.slice((t+".").length)};
|
package/dist/production/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-DdXLo8F4.js"),r=require("./core.js"),t=require("./initializeConfig-BLiF0aUh.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),a=require("./_useLocale-CpTrqBDt.js");require("react"),require("intl-messageformat"),require("@formatjs/fast-memoize"),require("./IntlContext-DcFt0tgW.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createFormatter=e.createFormatter,exports.createTranslator=r.createTranslator,exports._createFormatters=t.createFormatters,exports.initializeConfig=t.initializeConfig,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=a.useLocale;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("@formatjs/fast-memoize");function t(e){return e&&e.__esModule?e:{default:e}}var r=t(require("intl-messageformat"));function a(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t.filter(Boolean).join(".")}function n(e){return a(e.namespace,e.key)}function s(e){console.error(e)}function l(t,r){return e.memoize(t,{cache:(a=r,{create:()=>({get:e=>a[e],set(e,t){a[e]=t}})}),strategy:e.strategies.variadic});var a}function o(e,t){return l((function(){for(var t=arguments.length,r=new Array(t),a=0;a<t;a++)r[a]=arguments[a];return new e(...r)}),t)}exports.createFormatters=function(){const e={dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}},t=o(Intl.DateTimeFormat,e.dateTime),a=o(Intl.NumberFormat,e.number),n=o(Intl.PluralRules,e.pluralRules),s=o(Intl.RelativeTimeFormat,e.relativeTime),i=o(Intl.ListFormat,e.list),m=o(Intl.DisplayNames,e.displayNames),u=l((function(){return new r.default(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1],arguments.length<=2?void 0:arguments[2],{formatters:{getNumberFormat:a,getDateTimeFormat:t,getPluralRules:n},...arguments.length<=3?void 0:arguments[3]})}),e.message);return{getDateTimeFormat:t,getNumberFormat:a,getPluralRules:n,getMessageFormat:u,getRelativeTimeFormat:s,getListFormat:i,getDisplayNames:m}},exports.defaultGetMessageFallback=n,exports.defaultOnError=s,exports.initializeConfig=function(e){let{getMessageFallback:t,messages:r,onError:a,...l}=e;return{...l,messages:r,onError:a||s,getMessageFallback:t||n}},exports.joinPath=a;
|
package/dist/production/react.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-CpTrqBDt.js"),r=require("react"),o=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-CpTrqBDt.js"),r=require("react"),o=require("./createFormatter-DdXLo8F4.js");require("./initializeConfig-BLiF0aUh.js"),require("@formatjs/fast-memoize"),require("intl-messageformat"),require("./IntlContext-DcFt0tgW.js");let n=!1;const s="undefined"==typeof window;function a(){return new Date}exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,formatters:n,locale:s,now:a,onError:u,timeZone:l}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:s,now:a,onError:u,timeZone:l,_formatters:n})),[e,n,a,s,u,l])},exports.useMessages=function(){const e=t.useIntlContext();if(!e.messages)throw new Error(void 0);return e.messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:n}=t.useIntlContext(),[s,u]=r.useState(n||a());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{u(a())}),o);return()=>{clearInterval(e)}}),[n,o]),null==o&&n?n:s},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,a,u){const{defaultTranslationValues:l,formats:i,formatters:c,getMessageFallback:m,locale:f,onError:I,timeZone:d}=t.useIntlContext();return e=e[u],a=o.resolveNamespace(a,u),d||n||!s||(n=!0,I(new o.IntlError(o.IntlErrorCode.ENVIRONMENT_FALLBACK,void 0))),r.useMemo((()=>o.createBaseTranslator({formatters:c,getMessageFallback:m,messages:e,defaultTranslationValues:l,namespace:a,onError:I,formats:i,locale:f,timeZone:d})),[c,m,e,l,a,I,i,f,d])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
|
|
@@ -3,20 +3,20 @@ import AbstractIntlMessages from './AbstractIntlMessages';
|
|
|
3
3
|
import Formats from './Formats';
|
|
4
4
|
import { InitializedIntlConfig } from './IntlConfig';
|
|
5
5
|
import IntlError from './IntlError';
|
|
6
|
-
import MessageFormatCache from './MessageFormatCache';
|
|
7
6
|
import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from './TranslationValues';
|
|
7
|
+
import { Formatters } from './formatters';
|
|
8
8
|
import MessageKeys from './utils/MessageKeys';
|
|
9
9
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
10
10
|
import NestedValueOf from './utils/NestedValueOf';
|
|
11
11
|
export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
|
|
12
|
-
|
|
12
|
+
formatters: Formatters;
|
|
13
13
|
defaultTranslationValues?: RichTranslationValues;
|
|
14
14
|
namespace?: string;
|
|
15
15
|
messagesOrError: Messages | IntlError;
|
|
16
16
|
};
|
|
17
17
|
export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(config: Omit<CreateBaseTranslatorProps<Messages>, 'messagesOrError'>): {
|
|
18
|
-
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues
|
|
19
|
-
rich: (key: string, values?: RichTranslationValues
|
|
20
|
-
markup(key: string, values: MarkupTranslationValues, formats?: Partial<Formats> |
|
|
18
|
+
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues, formats?: Partial<Formats>): string;
|
|
19
|
+
rich: (key: string, values?: RichTranslationValues, formats?: Partial<Formats>) => string | ReactElement | ReactNodeArray;
|
|
20
|
+
markup(key: Parameters<(key: string, values?: RichTranslationValues, formats?: Partial<Formats>) => string | ReactElement | ReactNodeArray>[0], values: MarkupTranslationValues, formats?: Parameters<(key: string, values?: RichTranslationValues, formats?: Partial<Formats>) => string | ReactElement | ReactNodeArray>[2]): string;
|
|
21
21
|
raw(key: string): any;
|
|
22
22
|
};
|
|
@@ -5,17 +5,20 @@ import IntlError from './IntlError';
|
|
|
5
5
|
import NumberFormatOptions from './NumberFormatOptions';
|
|
6
6
|
import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
|
|
7
7
|
import TimeZone from './TimeZone';
|
|
8
|
+
import { Formatters } from './formatters';
|
|
8
9
|
type Props = {
|
|
9
10
|
locale: string;
|
|
10
11
|
timeZone?: TimeZone;
|
|
11
12
|
onError?(error: IntlError): void;
|
|
12
13
|
formats?: Partial<Formats>;
|
|
13
14
|
now?: Date;
|
|
15
|
+
/** @private */
|
|
16
|
+
_formatters?: Formatters;
|
|
14
17
|
};
|
|
15
|
-
export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
|
|
18
|
+
export default function createFormatter({ _formatters: formatters, formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
|
|
16
19
|
dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
|
|
17
20
|
number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
|
|
18
|
-
relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions[
|
|
21
|
+
relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions["now"] | RelativeTimeFormatOptions) => string;
|
|
19
22
|
list: <Value extends string | ReactElement<any, string | import("react").JSXElementConstructor<any>>>(value: Iterable<Value>, formatOrOptions?: string | Intl.ListFormatOptions) => Value extends string ? string : Iterable<ReactElement>;
|
|
20
23
|
dateTimeRange: (start: Date | number, end: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
|
|
21
24
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ReactElement, ReactNodeArray } from 'react';
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlConfig from './IntlConfig';
|
|
4
|
-
import MessageFormatCache from './MessageFormatCache';
|
|
5
4
|
import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from './TranslationValues';
|
|
5
|
+
import { Formatters } from './formatters';
|
|
6
6
|
import MessageKeys from './utils/MessageKeys';
|
|
7
7
|
import NamespaceKeys from './utils/NamespaceKeys';
|
|
8
8
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
@@ -15,11 +15,11 @@ import NestedValueOf from './utils/NestedValueOf';
|
|
|
15
15
|
* The namespace can also indicate nesting by using a dot
|
|
16
16
|
* (e.g. `namespace.Component`).
|
|
17
17
|
*/
|
|
18
|
-
export default function createTranslator<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>({ getMessageFallback, messages, namespace, onError, ...rest }: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {
|
|
18
|
+
export default function createTranslator<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>({ _formatters, getMessageFallback, messages, namespace, onError, ...rest }: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {
|
|
19
19
|
messages?: IntlConfig<IntlMessages>['messages'];
|
|
20
20
|
namespace?: NestedKey;
|
|
21
21
|
/** @private */
|
|
22
|
-
|
|
22
|
+
_formatters?: Formatters;
|
|
23
23
|
}): {
|
|
24
24
|
<TargetKey extends MessageKeys<NestedValueOf<{
|
|
25
25
|
'!': IntlMessages;
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
import AbstractIntlMessages from './AbstractIntlMessages';
|
|
3
2
|
import { InitializedIntlConfig } from './IntlConfig';
|
|
4
|
-
import
|
|
3
|
+
import { Formatters } from './formatters';
|
|
5
4
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
6
5
|
export type CreateTranslatorImplProps<Messages> = Omit<InitializedIntlConfig, 'messages'> & {
|
|
7
6
|
namespace: string;
|
|
8
7
|
messages: Messages;
|
|
9
|
-
|
|
8
|
+
formatters: Formatters;
|
|
10
9
|
};
|
|
11
|
-
export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({
|
|
12
|
-
<TargetKey extends import(".").MessageKeys<import(".").NestedValueOf<Messages, NestedKey>, NestedKeyOf<import(".").NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: import("./TranslationValues").default
|
|
13
|
-
rich: (key: string, values?: import("./TranslationValues").RichTranslationValues
|
|
14
|
-
markup(key: string, values: import("./TranslationValues").MarkupTranslationValues, formats?: Partial<import("./Formats").default> |
|
|
10
|
+
export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ messages, namespace, ...rest }: CreateTranslatorImplProps<Messages>, namespacePrefix: string): {
|
|
11
|
+
<TargetKey extends import(".").MessageKeys<import(".").NestedValueOf<Messages, NestedKey>, NestedKeyOf<import(".").NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: import("./TranslationValues").default, formats?: Partial<import("./Formats").default>): string;
|
|
12
|
+
rich: (key: string, values?: import("./TranslationValues").RichTranslationValues, formats?: Partial<import("./Formats").default>) => string | import("react").ReactElement | import("react").ReactNodeArray;
|
|
13
|
+
markup(key: Parameters<(key: string, values?: import("./TranslationValues").RichTranslationValues, formats?: Partial<import("./Formats").default>) => string | import("react").ReactElement | import("react").ReactNodeArray>[0], values: import("./TranslationValues").MarkupTranslationValues, formats?: Parameters<(key: string, values?: import("./TranslationValues").RichTranslationValues, formats?: Partial<import("./Formats").default>) => string | import("react").ReactElement | import("react").ReactNodeArray>[2]): string;
|
|
15
14
|
raw(key: string): any;
|
|
16
15
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import IntlMessageFormat from 'intl-messageformat';
|
|
2
|
+
export declare function createIntlCache(): {
|
|
3
|
+
dateTime: Record<string, Intl.DateTimeFormat>;
|
|
4
|
+
number: Record<string, Intl.NumberFormat>;
|
|
5
|
+
message: Record<string, IntlMessageFormat>;
|
|
6
|
+
relativeTime: Record<string, Intl.RelativeTimeFormat>;
|
|
7
|
+
pluralRules: Record<string, Intl.PluralRules>;
|
|
8
|
+
list: Record<string, Intl.ListFormat>;
|
|
9
|
+
displayNames: Record<string, Intl.DisplayNames>;
|
|
10
|
+
};
|
|
11
|
+
export declare function createFormatters(): {
|
|
12
|
+
getDateTimeFormat(...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
|
|
13
|
+
getNumberFormat(...args: ConstructorParameters<typeof Intl.NumberFormat>): Intl.NumberFormat;
|
|
14
|
+
getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
15
|
+
getMessageFormat(...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
|
|
16
|
+
getRelativeTimeFormat(...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
|
|
17
|
+
getListFormat(...args: ConstructorParameters<typeof Intl.ListFormat>): Intl.ListFormat;
|
|
18
|
+
getDisplayNames(...args: ConstructorParameters<typeof Intl.DisplayNames>): Intl.DisplayNames;
|
|
19
|
+
};
|
|
20
|
+
export type Formatters = ReturnType<typeof createFormatters>;
|
|
@@ -12,3 +12,4 @@ export type { default as MessageKeys } from './utils/MessageKeys';
|
|
|
12
12
|
export type { default as NamespaceKeys } from './utils/NamespaceKeys';
|
|
13
13
|
export type { default as NestedKeyOf } from './utils/NestedKeyOf';
|
|
14
14
|
export type { default as NestedValueOf } from './utils/NestedValueOf';
|
|
15
|
+
export { createFormatters as _createFormatters } from './formatters';
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
key: string;
|
|
8
|
-
namespace?: string | undefined;
|
|
9
|
-
}) => string;
|
|
10
|
-
} & {
|
|
11
|
-
messageFormatCache?: MessageFormatCache | undefined;
|
|
12
|
-
}) | undefined>;
|
|
1
|
+
import type { InitializedIntlConfig } from '../core/IntlConfig';
|
|
2
|
+
import type { Formatters } from '../core/formatters';
|
|
3
|
+
export type IntlContextValue = InitializedIntlConfig & {
|
|
4
|
+
formatters: Formatters;
|
|
5
|
+
};
|
|
6
|
+
declare const IntlContext: import("react").Context<IntlContextValue | undefined>;
|
|
13
7
|
export default IntlContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
getMessageFallback: (info: {
|
|
4
|
-
error: import("..").IntlError;
|
|
5
|
-
key: string;
|
|
6
|
-
namespace?: string | undefined;
|
|
7
|
-
}) => string;
|
|
8
|
-
} & {
|
|
9
|
-
messageFormatCache?: import("../core/MessageFormatCache").default | undefined;
|
|
10
|
-
};
|
|
1
|
+
import { IntlContextValue } from './IntlContext';
|
|
2
|
+
export default function useIntlContext(): IntlContextValue;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
import AbstractIntlMessages from '../core/AbstractIntlMessages';
|
|
3
2
|
import NestedKeyOf from '../core/utils/NestedKeyOf';
|
|
4
3
|
export default function useTranslationsImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string): {
|
|
5
|
-
<TargetKey extends unknown>(key: TargetKey, values?: import("../core").TranslationValues
|
|
6
|
-
rich: (key: string, values?: import("../core").RichTranslationValues
|
|
7
|
-
markup(key: string, values: import("../core").MarkupTranslationValues, formats?: Partial<import("../core").Formats> |
|
|
4
|
+
<TargetKey extends unknown>(key: TargetKey, values?: import("../core").TranslationValues, formats?: Partial<import("../core").Formats>): string;
|
|
5
|
+
rich: (key: string, values?: import("../core").RichTranslationValues, formats?: Partial<import("../core").Formats>) => string | import("react").ReactElement | import("react").ReactNodeArray;
|
|
6
|
+
markup(key: Parameters<(key: string, values?: import("../core").RichTranslationValues, formats?: Partial<import("../core").Formats>) => string | import("react").ReactElement | import("react").ReactNodeArray>[0], values: import("../core").MarkupTranslationValues, formats?: Parameters<(key: string, values?: import("../core").RichTranslationValues, formats?: Partial<import("../core").Formats>) => string | import("react").ReactElement | import("react").ReactNodeArray>[2]): string;
|
|
8
7
|
raw(key: string): any;
|
|
9
8
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-intl",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.17.1",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "Jan Amann <jan@amann.work>",
|
|
6
6
|
"description": "Internationalization (i18n) for React",
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"formatting"
|
|
64
64
|
],
|
|
65
65
|
"dependencies": {
|
|
66
|
+
"@formatjs/fast-memoize": "^2.2.0",
|
|
66
67
|
"intl-messageformat": "^10.5.14"
|
|
67
68
|
},
|
|
68
69
|
"peerDependencies": {
|
|
@@ -71,8 +72,8 @@
|
|
|
71
72
|
"size-limit": [
|
|
72
73
|
{
|
|
73
74
|
"path": "dist/production/index.js",
|
|
74
|
-
"limit": "15.
|
|
75
|
+
"limit": "15.545 kB"
|
|
75
76
|
}
|
|
76
77
|
],
|
|
77
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "1635c438061d18f89c0b6818213892051e19fa65"
|
|
78
79
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import e from"intl-messageformat";import{isValidElement as t,cloneElement as n}from"react";import{I as r,a as o,j as a,d as s,b as i}from"./initializeConfig-CeCQBa_L.js";function c(e,t){return e?Object.keys(e).reduce(((n,r)=>(n[r]={timeZone:t,...e[r]},n)),{}):e}function u(e,t,n,r){const o=a(r,n);if(!t)throw new Error("No messages available at `".concat(r,"`."));let s=t;return n.split(".").forEach((t=>{const n=s[t];if(null==t||null==n)throw new Error("Could not resolve `".concat(o,"` in messages for locale `").concat(e,"`."));s=n})),s}function l(l){const m=function(e,t,n){let a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:s;try{if(!t)throw new Error("No messages were configured on the provider.");const r=n?u(e,t,n):t;if(!r)throw new Error("No messages for namespace `".concat(n,"` found."));return r}catch(e){const t=new r(o.MISSING_MESSAGE,e.message);return a(t),t}}(l.locale,l.messages,l.namespace,l.onError);return function(s){let{defaultTranslationValues:l,formats:m,getMessageFallback:f=i,locale:g,messageFormatCache:d,messagesOrError:p,namespace:h,onError:y,timeZone:w}=s;function v(e,t,n){const o=new r(t,n);return y(o),f({error:o,key:e,namespace:h})}function E(s,i,y){if(p instanceof r)return f({error:p,key:s,namespace:h});const E=p;let T;try{T=u(g,E,s,h)}catch(e){return v(s,o.MISSING_MESSAGE,e.message)}const I=a(g,h,s,String(T));let S;if(null!=d&&d.has(I))S=d.get(I);else{if("object"==typeof T){let e,t;return Array.isArray(T)?(e=o.INVALID_MESSAGE,t="Message at `".concat(a(h,s),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(e=o.INSUFFICIENT_PATH,t="Message at `".concat(a(h,s),"` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages")),v(s,e,t)}const t=function(e,t){if(t)return;const n=e.replace(/'([{}])/gi,"$1");return/<|{/.test(n)?void 0:n}(T,i);if(t)return t;try{S=new e(T,g,function(t,n){const r=n?{...t,dateTime:c(t.dateTime,n)}:t,o=e.formats.date,a=n?c(o,n):o,s=e.formats.time,i=n?c(s,n):s;return{...r,date:{...a,...null==r?void 0:r.dateTime},time:{...i,...null==r?void 0:r.dateTime}}}({...m,...y},w),{formatters:{getNumberFormat:(e,t)=>new Intl.NumberFormat(e,t),getDateTimeFormat:(e,t)=>new Intl.DateTimeFormat(e,{timeZone:w,...t}),getPluralRules:(e,t)=>new Intl.PluralRules(e,t)}})}catch(e){const t=e;return v(s,o.INVALID_MESSAGE,t.message+("originalMessage"in t?" (".concat(t.originalMessage,")"):""))}null==d||d.set(I,S)}try{const e=S.format(function(e){if(0===Object.keys(e).length)return;const r={};return Object.keys(e).forEach((o=>{let a=0;const s=e[o];let i;i="function"==typeof s?e=>{const r=s(e);return t(r)?n(r,{key:o+a++}):r}:s,r[o]=i})),r}({...l,...i}));if(null==e)throw new Error("Unable to format `".concat(s,"` in ").concat(h?"namespace `".concat(h,"`"):"messages"));return t(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(e){return v(s,o.FORMATTING_ERROR,e.message)}}function T(e,t,n){const r=E(e,t,n);return"string"!=typeof r?v(e,o.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(h?"namespace `".concat(h,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):r}return T.rich=E,T.markup=(e,t,n)=>{const a=E(e,t,n);if("string"!=typeof a){const t=new r(o.FORMATTING_ERROR,"`t.markup` only accepts functions for formatting that receive and return strings.\n\nE.g. t.markup('markup', {b: (chunks) => `<b>${chunks}</b>`})");return y(t),f({error:t,key:e,namespace:h})}return a},T.raw=e=>{if(p instanceof r)return f({error:p,key:e,namespace:h});const t=p;try{return u(g,t,e,h)}catch(t){return v(e,o.MISSING_MESSAGE,t.message)}},T}({...l,messagesOrError:m})}function m(e,t){return e===t?void 0:e.slice((t+".").length)}const f=60,g=60*f,d=24*g,p=7*d,h=d*(365/12),y=3*h,w=365*d,v={second:1,seconds:1,minute:f,minutes:f,hour:g,hours:g,day:d,days:d,week:p,weeks:p,month:h,months:h,quarter:y,quarters:y,year:w,years:w};function E(e){let{formats:t,locale:n,now:a,onError:i=s,timeZone:c}=e;function u(e){var t;return null!==(t=e)&&void 0!==t&&t.timeZone||(c?e={...e,timeZone:c}:i(new r(o.ENVIRONMENT_FALLBACK,"The `timeZone` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone"))),e}function l(e,t,n,a){let s;try{s=function(e,t){let n;if("string"==typeof t){const a=t;if(n=null==e?void 0:e[a],!n){const e=new r(o.MISSING_FORMAT,"Format `".concat(a,"` is not available. You can configure it on the provider or provide custom options."));throw i(e),e}}else n=t;return n}(t,e)}catch(e){return a()}try{return n(s)}catch(e){return i(new r(o.FORMATTING_ERROR,e.message)),a()}}function m(e,r){return l(r,null==t?void 0:t.dateTime,(t=>(t=u(t),new Intl.DateTimeFormat(n,t).format(e))),(()=>String(e)))}function y(){return a||(i(new r(o.ENVIRONMENT_FALLBACK,"The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now")),new Date)}return{dateTime:m,number:function(e,r){return l(r,null==t?void 0:t.number,(t=>new Intl.NumberFormat(n,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let r,o;const a={};t instanceof Date||"number"==typeof t?r=new Date(t):t&&(r=null!=t.now?new Date(t.now):y(),o=t.unit,a.style=t.style,a.numberingSystem=t.numberingSystem),r||(r=y());const s=(new Date(e).getTime()-r.getTime())/1e3;o||(o=function(e){const t=Math.abs(e);return t<f?"second":t<g?"minute":t<d?"hour":t<p?"day":t<h?"week":t<w?"month":"year"}(s)),a.numeric="second"===o?"auto":"always";const i=function(e,t){return Math.round(e/v[t])}(s,o);return new Intl.RelativeTimeFormat(n,a).format(i,o)}catch(t){return i(new r(o.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){const o=[],a=new Map;let s=0;for(const t of e){let e;"object"==typeof t?(e=String(s),a.set(e,t)):e=String(t),o.push(e),s++}return l(r,null==t?void 0:t.list,(e=>{const t=new Intl.ListFormat(n,e).formatToParts(o).map((e=>"literal"===e.type?e.value:a.get(e.value)||e.value));return a.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,r,o){return l(o,null==t?void 0:t.dateTime,(t=>(t=u(t),new Intl.DateTimeFormat(n,t).formatRange(e,r))),(()=>[m(e),m(r)].join(" – ")))}}}export{l as a,E as c,m as r};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function e(e,n,t){return(n=function(e){var n=function(e,n){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var o=t.call(e,n||"default");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(e)}(e,"string");return"symbol"==typeof n?n:n+""}(n))in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}let n=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class t extends Error{constructor(n,t){let o=n;t&&(o+=": "+t),super(o),e(this,"code",void 0),e(this,"originalMessage",void 0),this.code=n,t&&(this.originalMessage=t)}}function o(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return n.filter(Boolean).join(".")}function r(e){return o(e.namespace,e.key)}function a(e){console.error(e)}function s(e,n,t){Object.entries(e).forEach((e=>{let[r,a]=e;if(r.includes(".")){let e=r;t&&(e+=" (at ".concat(t,")")),n.push(e)}null!=a&&"object"==typeof a&&s(a,n,o(t,r))}))}function i(e,o){const r=[];s(e,r),r.length>0&&o(new t(n.INVALID_KEY,'Namespace keys can not contain the character "." as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid '.concat(1===r.length?"key":"keys",": ").concat(r.join(", "),'\n\nIf you\'re migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from "lodash";\n\nconst input = {\n "one.one": "1.1",\n "one.two": "1.2",\n "two.one.one": "2.1.1"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// "one": {\n// "one": "1.1",\n// "two": "1.2"\n// },\n// "two": {\n// "one": {\n// "one": "2.1.1"\n// }\n// }\n// }\n')))}function c(e){let{getMessageFallback:n,messages:t,onError:o,...s}=e;const c=o||a,u=n||r;return t&&i(t,c),{...s,messages:t,onError:c,getMessageFallback:u}}export{t as I,n as a,r as b,a as d,c as i,o as j};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var e=require("intl-messageformat"),t=require("react"),r=require("./initializeConfig-iVzvEYey.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function i(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}let a=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class s extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),i(this,"code",void 0),i(this,"originalMessage",void 0),this.code=e,t&&(this.originalMessage=t)}}function u(e,t){return e?Object.keys(e).reduce(((r,n)=>(r[n]={timeZone:t,...e[n]},r)),{}):e}function c(e,t,n,o){const i=r.joinPath(o,n);if(!t)throw new Error(i);let a=t;return n.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(i+" (".concat(e,")"));a=r})),a}const l=60,m=60*l,f=24*m,E=7*f,I=f*(365/12),d=3*I,g=365*f,S={second:1,seconds:1,minute:l,minutes:l,hour:m,hours:m,day:f,days:f,week:E,weeks:E,month:I,months:I,quarter:d,quarters:d,year:g,years:g};exports.IntlError=s,exports.IntlErrorCode=a,exports.createBaseTranslator=function(e){const n=function(e,t,n){let o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:r.defaultOnError;try{if(!t)throw new Error(void 0);const r=n?c(e,t,n):t;if(!r)throw new Error(n);return r}catch(e){const t=new s(a.MISSING_MESSAGE,e.message);return o(t),t}}(e.locale,e.messages,e.namespace,e.onError);return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:l=r.defaultGetMessageFallback,locale:m,messageFormatCache:f,messagesOrError:E,namespace:I,onError:d,timeZone:g}=e;function S(e,t,r){const n=new s(t,r);return d(n),l({error:n,key:e,namespace:I})}function y(e,d,y){if(E instanceof s)return l({error:E,key:e,namespace:I});const T=E;let w;try{w=c(m,T,e,I)}catch(t){return S(e,a.MISSING_MESSAGE,t.message)}const N=r.joinPath(m,I,e,String(w));let A;if(null!=f&&f.has(N))A=f.get(N);else{if("object"==typeof w){let t,r;return t=Array.isArray(w)?a.INVALID_MESSAGE:a.INSUFFICIENT_PATH,S(e,t,r)}const t=function(e,t){if(t)return;const r=e.replace(/'([{}])/gi,"$1");return/<|{/.test(r)?void 0:r}(w,d);if(t)return t;try{A=new o.default(w,m,function(e,t){const r=t?{...e,dateTime:u(e.dateTime,t)}:e,n=o.default.formats.date,i=t?u(n,t):n,a=o.default.formats.time,s=t?u(a,t):a;return{...r,date:{...i,...null==r?void 0:r.dateTime},time:{...s,...null==r?void 0:r.dateTime}}}({...i,...y},g),{formatters:{getNumberFormat:(e,t)=>new Intl.NumberFormat(e,t),getDateTimeFormat:(e,t)=>new Intl.DateTimeFormat(e,{timeZone:g,...t}),getPluralRules:(e,t)=>new Intl.PluralRules(e,t)}})}catch(t){const r=t;return S(e,a.INVALID_MESSAGE,r.message)}null==f||f.set(N,A)}try{const e=A.format(function(e){if(0===Object.keys(e).length)return;const r={};return Object.keys(e).forEach((n=>{let o=0;const i=e[n];let a;a="function"==typeof i?e=>{const r=i(e);return t.isValidElement(r)?t.cloneElement(r,{key:n+o++}):r}:i,r[n]=a})),r}({...n,...d}));if(null==e)throw new Error(void 0);return t.isValidElement(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(t){return S(e,a.FORMATTING_ERROR,t.message)}}function T(e,t,r){const n=y(e,t,r);return"string"!=typeof n?S(e,a.INVALID_MESSAGE,void 0):n}return T.rich=y,T.markup=(e,t,r)=>{const n=y(e,t,r);if("string"!=typeof n){const t=new s(a.FORMATTING_ERROR,void 0);return d(t),l({error:t,key:e,namespace:I})}return n},T.raw=e=>{if(E instanceof s)return l({error:E,key:e,namespace:I});const t=E;try{return c(m,t,e,I)}catch(t){return S(e,a.MISSING_MESSAGE,t.message)}},T}({...e,messagesOrError:n})},exports.createFormatter=function(e){let{formats:t,locale:n,now:o,onError:i=r.defaultOnError,timeZone:u}=e;function c(e){var t;return null!==(t=e)&&void 0!==t&&t.timeZone||(u?e={...e,timeZone:u}:i(new s(a.ENVIRONMENT_FALLBACK,void 0))),e}function d(e,t,r,n){let o;try{o=function(e,t){let r;if("string"==typeof t){if(r=null==e?void 0:e[t],!r){const e=new s(a.MISSING_FORMAT,void 0);throw i(e),e}}else r=t;return r}(t,e)}catch(e){return n()}try{return r(o)}catch(e){return i(new s(a.FORMATTING_ERROR,e.message)),n()}}function y(e,r){return d(r,null==t?void 0:t.dateTime,(t=>(t=c(t),new Intl.DateTimeFormat(n,t).format(e))),(()=>String(e)))}function T(){return o||(i(new s(a.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:y,number:function(e,r){return d(r,null==t?void 0:t.number,(t=>new Intl.NumberFormat(n,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let r,o;const i={};t instanceof Date||"number"==typeof t?r=new Date(t):t&&(r=null!=t.now?new Date(t.now):T(),o=t.unit,i.style=t.style,i.numberingSystem=t.numberingSystem),r||(r=T());const a=(new Date(e).getTime()-r.getTime())/1e3;o||(o=function(e){const t=Math.abs(e);return t<l?"second":t<m?"minute":t<f?"hour":t<E?"day":t<I?"week":t<g?"month":"year"}(a)),i.numeric="second"===o?"auto":"always";const s=function(e,t){return Math.round(e/S[t])}(a,o);return new Intl.RelativeTimeFormat(n,i).format(s,o)}catch(t){return i(new s(a.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){const o=[],i=new Map;let a=0;for(const t of e){let e;"object"==typeof t?(e=String(a),i.set(e,t)):e=String(t),o.push(e),a++}return d(r,null==t?void 0:t.list,(e=>{const t=new Intl.ListFormat(n,e).formatToParts(o).map((e=>"literal"===e.type?e.value:i.get(e.value)||e.value));return i.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,r,o){return d(o,null==t?void 0:t.dateTime,(t=>(t=c(t),new Intl.DateTimeFormat(n,t).formatRange(e,r))),(()=>[y(e),y(r)].join(" – ")))}}},exports.resolveNamespace=function(e,t){return e===t?void 0:e.slice((t+".").length)};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";function e(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return r.filter(Boolean).join(".")}function r(r){return e(r.namespace,r.key)}function n(e){console.error(e)}exports.defaultGetMessageFallback=r,exports.defaultOnError=n,exports.initializeConfig=function(e){let{getMessageFallback:t,messages:o,onError:a,...s}=e;return{...s,messages:o,onError:a||n,getMessageFallback:t||r}},exports.joinPath=e;
|