use-intl 2.7.6 → 2.8.0
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/{AbstractIntlMessages.d.ts → core/AbstractIntlMessages.d.ts} +0 -0
- package/dist/{DateTimeFormatOptions.d.ts → core/DateTimeFormatOptions.d.ts} +0 -0
- package/dist/{Formats.d.ts → core/Formats.d.ts} +0 -0
- package/dist/{IntlProvider.d.ts → core/IntlConfiguration.d.ts} +15 -16
- package/dist/{IntlError.d.ts → core/IntlError.d.ts} +0 -0
- package/dist/{IntlMessages.d.ts → core/IntlMessages.d.ts} +0 -0
- package/dist/{NumberFormatOptions.d.ts → core/NumberFormatOptions.d.ts} +0 -0
- package/dist/{TranslationValues.d.ts → core/TranslationValues.d.ts} +2 -2
- package/dist/{convertFormatsToIntlMessageFormat.d.ts → core/convertFormatsToIntlMessageFormat.d.ts} +0 -0
- package/dist/core/createBaseTranslator.d.ts +26 -0
- package/dist/{useIntl.d.ts → core/createIntl.d.ts} +11 -1
- package/dist/core/createTranslator.d.ts +48 -0
- package/dist/core/createTranslatorImpl.d.ts +14 -0
- package/dist/core/defaults.d.ts +11 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/resolveNamespace.d.ts +5 -0
- package/dist/{use-intl.esm8.js → core/use-intl.esm.js} +2 -2
- package/dist/core/use-intl.esm.js.map +1 -0
- package/dist/core/use-intl.esm2.js +39 -0
- package/dist/core/use-intl.esm2.js.map +1 -0
- package/dist/{use-intl.esm4.js → core/use-intl.esm3.js} +12 -12
- package/dist/core/use-intl.esm3.js.map +1 -0
- package/dist/core/use-intl.esm4.js +59 -0
- package/dist/core/use-intl.esm4.js.map +1 -0
- package/dist/core/use-intl.esm5.js +15 -0
- package/dist/core/use-intl.esm5.js.map +1 -0
- package/dist/{use-intl.esm9.js → core/use-intl.esm6.js} +2 -2
- package/dist/core/use-intl.esm6.js.map +1 -0
- package/dist/core/use-intl.esm7.js +213 -0
- package/dist/core/use-intl.esm7.js.map +1 -0
- package/dist/core/use-intl.esm8.js +10 -0
- package/dist/core/use-intl.esm8.js.map +1 -0
- package/dist/{use-intl.esm13.js → core/use-intl.esm9.js} +2 -2
- package/dist/core/use-intl.esm9.js.map +1 -0
- package/dist/{utils → core/utils}/MessageKeys.d.ts +0 -0
- package/dist/{utils → core/utils}/NamespaceKeys.d.ts +0 -0
- package/dist/{utils → core/utils}/NestedKeyOf.d.ts +0 -0
- package/dist/{utils → core/utils}/NestedValueOf.d.ts +0 -0
- package/dist/{validateMessages.d.ts → core/validateMessages.d.ts} +0 -0
- package/dist/index.d.ts +2 -12
- package/dist/{IntlContext.d.ts → react/IntlContext.d.ts} +4 -4
- package/dist/react/IntlProvider.d.ts +16 -0
- package/dist/react/index.d.ts +6 -0
- package/dist/react/use-intl.esm.js +36 -0
- package/dist/react/use-intl.esm.js.map +1 -0
- package/dist/{use-intl.esm3.js → react/use-intl.esm2.js} +4 -4
- package/dist/react/use-intl.esm2.js.map +1 -0
- package/dist/react/use-intl.esm3.js +25 -0
- package/dist/react/use-intl.esm3.js.map +1 -0
- package/dist/{use-intl.esm5.js → react/use-intl.esm4.js} +2 -2
- package/dist/react/use-intl.esm4.js.map +1 -0
- package/dist/{use-intl.esm7.js → react/use-intl.esm5.js} +2 -2
- package/dist/react/use-intl.esm5.js.map +1 -0
- package/dist/{use-intl.esm6.js → react/use-intl.esm6.js} +1 -1
- package/dist/react/use-intl.esm6.js.map +1 -0
- package/dist/{use-intl.esm10.js → react/use-intl.esm7.js} +1 -1
- package/dist/react/use-intl.esm7.js.map +1 -0
- package/dist/{use-intl.esm11.js → react/use-intl.esm8.js} +2 -2
- package/dist/react/use-intl.esm8.js.map +1 -0
- package/dist/react/use-intl.esm9.js +44 -0
- package/dist/react/use-intl.esm9.js.map +1 -0
- package/dist/react/useIntl.d.ts +5 -0
- package/dist/{useIntlContext.d.ts → react/useIntlContext.d.ts} +0 -0
- package/dist/{useLocale.d.ts → react/useLocale.d.ts} +0 -0
- package/dist/{useNow.d.ts → react/useNow.d.ts} +0 -0
- package/dist/{useTimeZone.d.ts → react/useTimeZone.d.ts} +0 -0
- package/dist/{useTranslations.d.ts → react/useTranslations.d.ts} +6 -6
- package/dist/react/useTranslationsImpl.d.ts +8 -0
- package/dist/use-intl.cjs.development.js +381 -229
- package/dist/use-intl.cjs.development.js.map +1 -1
- package/dist/use-intl.cjs.production.min.js +1 -1
- package/dist/use-intl.cjs.production.min.js.map +1 -1
- package/dist/use-intl.esm.js +9 -7
- package/dist/use-intl.esm.js.map +1 -1
- package/package.json +3 -2
- package/src/{AbstractIntlMessages.tsx → core/AbstractIntlMessages.tsx} +0 -0
- package/src/{DateTimeFormatOptions.tsx → core/DateTimeFormatOptions.tsx} +0 -0
- package/src/{Formats.tsx → core/Formats.tsx} +0 -0
- package/src/{IntlProvider.tsx → core/IntlConfiguration.tsx} +16 -53
- package/src/{IntlError.tsx → core/IntlError.tsx} +0 -0
- package/src/{IntlMessages.tsx → core/IntlMessages.tsx} +0 -0
- package/src/{NumberFormatOptions.tsx → core/NumberFormatOptions.tsx} +0 -0
- package/src/core/TranslationValues.tsx +22 -0
- package/src/{convertFormatsToIntlMessageFormat.tsx → core/convertFormatsToIntlMessageFormat.tsx} +0 -0
- package/src/core/createBaseTranslator.tsx +316 -0
- package/src/{useIntl.tsx → core/createIntl.tsx} +17 -4
- package/src/core/createTranslator.tsx +112 -0
- package/src/core/createTranslatorImpl.tsx +85 -0
- package/src/core/defaults.tsx +18 -0
- package/src/core/index.tsx +11 -0
- package/src/core/resolveNamespace.tsx +12 -0
- package/src/{utils → core/utils}/MessageKeys.tsx +0 -0
- package/src/{utils → core/utils}/NamespaceKeys.tsx +0 -0
- package/src/{utils → core/utils}/NestedKeyOf.tsx +0 -0
- package/src/{utils → core/utils}/NestedValueOf.tsx +0 -0
- package/src/{validateMessages.tsx → core/validateMessages.tsx} +0 -0
- package/src/index.tsx +2 -15
- package/src/{IntlContext.tsx → react/IntlContext.tsx} +4 -4
- package/src/react/IntlProvider.tsx +43 -0
- package/src/react/index.tsx +6 -0
- package/src/react/useIntl.tsx +19 -0
- package/src/{useIntlContext.tsx → react/useIntlContext.tsx} +0 -0
- package/src/{useLocale.tsx → react/useLocale.tsx} +0 -0
- package/src/{useNow.tsx → react/useNow.tsx} +0 -0
- package/src/{useTimeZone.tsx → react/useTimeZone.tsx} +0 -0
- package/src/{useTranslations.tsx → react/useTranslations.tsx} +9 -7
- package/src/react/useTranslationsImpl.tsx +64 -0
- package/dist/use-intl.esm10.js.map +0 -1
- package/dist/use-intl.esm11.js.map +0 -1
- package/dist/use-intl.esm12.js +0 -213
- package/dist/use-intl.esm12.js.map +0 -1
- package/dist/use-intl.esm13.js.map +0 -1
- package/dist/use-intl.esm2.js +0 -48
- package/dist/use-intl.esm2.js.map +0 -1
- package/dist/use-intl.esm3.js.map +0 -1
- package/dist/use-intl.esm4.js.map +0 -1
- package/dist/use-intl.esm5.js.map +0 -1
- package/dist/use-intl.esm6.js.map +0 -1
- package/dist/use-intl.esm7.js.map +0 -1
- package/dist/use-intl.esm8.js.map +0 -1
- package/dist/use-intl.esm9.js.map +0 -1
- package/dist/useTranslationsImpl.d.ts +0 -12
- package/src/TranslationValues.tsx +0 -13
- package/src/useTranslationsImpl.tsx +0 -328
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var IntlMessageFormat = require('intl-messageformat');
|
|
5
6
|
var React = require('react');
|
|
6
|
-
var intlMessageformat = require('intl-messageformat');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
9
|
|
|
10
|
+
var IntlMessageFormat__default = /*#__PURE__*/_interopDefaultLegacy(IntlMessageFormat);
|
|
10
11
|
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
11
12
|
|
|
12
13
|
function _extends() {
|
|
@@ -133,8 +134,6 @@ function _objectWithoutPropertiesLoose(source, excluded) {
|
|
|
133
134
|
return target;
|
|
134
135
|
}
|
|
135
136
|
|
|
136
|
-
var IntlContext = /*#__PURE__*/React.createContext(undefined);
|
|
137
|
-
|
|
138
137
|
exports.IntlErrorCode = void 0;
|
|
139
138
|
|
|
140
139
|
(function (IntlErrorCode) {
|
|
@@ -173,85 +172,6 @@ var IntlError = /*#__PURE__*/function (_Error) {
|
|
|
173
172
|
return IntlError;
|
|
174
173
|
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
175
174
|
|
|
176
|
-
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
177
|
-
Object.entries(messages).forEach(function (_ref) {
|
|
178
|
-
var key = _ref[0],
|
|
179
|
-
messageOrMessages = _ref[1];
|
|
180
|
-
|
|
181
|
-
if (key.includes('.')) {
|
|
182
|
-
var keyLabel = key;
|
|
183
|
-
if (parentPath) keyLabel += " (at " + parentPath + ")";
|
|
184
|
-
invalidKeyLabels.push(keyLabel);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (messageOrMessages != null && typeof messageOrMessages === 'object') {
|
|
188
|
-
validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
|
|
189
|
-
return part != null;
|
|
190
|
-
}).join('.'));
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function validateMessages(messages, onError) {
|
|
196
|
-
var invalidKeyLabels = [];
|
|
197
|
-
validateMessagesSegment(messages, invalidKeyLabels);
|
|
198
|
-
|
|
199
|
-
if (invalidKeyLabels.length > 0) {
|
|
200
|
-
onError(new IntlError(exports.IntlErrorCode.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 " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ')));
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
var _excluded = ["children", "onError", "getMessageFallback", "messages"];
|
|
205
|
-
|
|
206
|
-
function defaultGetMessageFallback(_ref) {
|
|
207
|
-
var key = _ref.key,
|
|
208
|
-
namespace = _ref.namespace;
|
|
209
|
-
return [namespace, key].filter(function (part) {
|
|
210
|
-
return part != null;
|
|
211
|
-
}).join('.');
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function defaultOnError(error) {
|
|
215
|
-
console.error(error);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function IntlProvider(_ref2) {
|
|
219
|
-
var children = _ref2.children,
|
|
220
|
-
_ref2$onError = _ref2.onError,
|
|
221
|
-
onError = _ref2$onError === void 0 ? defaultOnError : _ref2$onError,
|
|
222
|
-
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
223
|
-
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
224
|
-
messages = _ref2.messages,
|
|
225
|
-
contextValues = _objectWithoutPropertiesLoose(_ref2, _excluded);
|
|
226
|
-
|
|
227
|
-
{
|
|
228
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
229
|
-
React.useEffect(function () {
|
|
230
|
-
if (messages) {
|
|
231
|
-
validateMessages(messages, onError);
|
|
232
|
-
}
|
|
233
|
-
}, [messages, onError]);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return React__default["default"].createElement(IntlContext.Provider, {
|
|
237
|
-
value: _extends({}, contextValues, {
|
|
238
|
-
messages: messages,
|
|
239
|
-
onError: onError,
|
|
240
|
-
getMessageFallback: getMessageFallback
|
|
241
|
-
})
|
|
242
|
-
}, children);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function useIntlContext() {
|
|
246
|
-
var context = React.useContext(IntlContext);
|
|
247
|
-
|
|
248
|
-
if (!context) {
|
|
249
|
-
throw new Error('No intl context found. Have you configured the provider?' );
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return context;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
175
|
function setTimeZoneInFormats(formats, timeZone) {
|
|
256
176
|
if (!formats) return formats; // The only way to set a time zone with `intl-messageformat` is to merge it into the formats
|
|
257
177
|
// https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15
|
|
@@ -282,17 +202,30 @@ function convertFormatsToIntlMessageFormat(formats, timeZone) {
|
|
|
282
202
|
});
|
|
283
203
|
}
|
|
284
204
|
|
|
285
|
-
|
|
205
|
+
/**
|
|
206
|
+
* Contains defaults that are used for all entry points into the core.
|
|
207
|
+
* See also `InitializedIntlConfiguration`.
|
|
208
|
+
*/
|
|
209
|
+
function defaultGetMessageFallback(props) {
|
|
210
|
+
return [props.namespace, props.key].filter(function (part) {
|
|
211
|
+
return part != null;
|
|
212
|
+
}).join('.');
|
|
213
|
+
}
|
|
214
|
+
function defaultOnError(error) {
|
|
215
|
+
console.error(error);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function resolvePath(messages, key, namespace) {
|
|
286
219
|
if (!messages) {
|
|
287
220
|
throw new Error("No messages available at `" + namespace + "`." );
|
|
288
221
|
}
|
|
289
222
|
|
|
290
223
|
var message = messages;
|
|
291
|
-
|
|
224
|
+
key.split('.').forEach(function (part) {
|
|
292
225
|
var next = message[part];
|
|
293
226
|
|
|
294
227
|
if (part == null || next == null) {
|
|
295
|
-
throw new Error("Could not resolve `" +
|
|
228
|
+
throw new Error("Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." );
|
|
296
229
|
}
|
|
297
230
|
|
|
298
231
|
message = next;
|
|
@@ -310,8 +243,8 @@ function prepareTranslationValues(values) {
|
|
|
310
243
|
var transformed;
|
|
311
244
|
|
|
312
245
|
if (typeof value === 'function') {
|
|
313
|
-
transformed = function transformed(
|
|
314
|
-
var result = value(
|
|
246
|
+
transformed = function transformed(chunks) {
|
|
247
|
+
var result = value(chunks);
|
|
315
248
|
return React.isValidElement(result) ? React.cloneElement(result, {
|
|
316
249
|
key: key + index++
|
|
317
250
|
}) : result;
|
|
@@ -325,167 +258,228 @@ function prepareTranslationValues(values) {
|
|
|
325
258
|
return transformedValues;
|
|
326
259
|
}
|
|
327
260
|
|
|
328
|
-
function
|
|
329
|
-
var
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
locale = _useIntlContext.locale,
|
|
334
|
-
onError = _useIntlContext.onError,
|
|
335
|
-
timeZone = _useIntlContext.timeZone; // The `namespacePrefix` is part of the type system.
|
|
336
|
-
// See the comment in the hook invocation.
|
|
261
|
+
function getMessagesOrError(_ref) {
|
|
262
|
+
var messages = _ref.messages,
|
|
263
|
+
namespace = _ref.namespace,
|
|
264
|
+
_ref$onError = _ref.onError,
|
|
265
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
|
|
337
266
|
|
|
267
|
+
try {
|
|
268
|
+
if (!messages) {
|
|
269
|
+
throw new Error("development" !== "production" ? "No messages were configured on the provider." : undefined);
|
|
270
|
+
}
|
|
338
271
|
|
|
339
|
-
|
|
340
|
-
namespace = namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
341
|
-
var cachedFormatsByLocaleRef = React.useRef({});
|
|
342
|
-
var messagesOrError = React.useMemo(function () {
|
|
343
|
-
try {
|
|
344
|
-
if (!allMessages) {
|
|
345
|
-
throw new Error("development" !== "production" ? "No messages were configured on the provider." : undefined);
|
|
346
|
-
}
|
|
272
|
+
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
347
273
|
|
|
348
|
-
|
|
274
|
+
if (!retrievedMessages) {
|
|
275
|
+
throw new Error("development" !== "production" ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
276
|
+
}
|
|
349
277
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
278
|
+
return retrievedMessages;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
var intlError = new IntlError(exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
281
|
+
onError(intlError);
|
|
282
|
+
return intlError;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function createBaseTranslator(_ref2) {
|
|
286
|
+
var cachedFormatsByLocale = _ref2.cachedFormatsByLocale,
|
|
287
|
+
defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
288
|
+
globalFormats = _ref2.formats,
|
|
289
|
+
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
290
|
+
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
291
|
+
locale = _ref2.locale,
|
|
292
|
+
messagesOrError = _ref2.messagesOrError,
|
|
293
|
+
namespace = _ref2.namespace,
|
|
294
|
+
onError = _ref2.onError,
|
|
295
|
+
timeZone = _ref2.timeZone;
|
|
296
|
+
|
|
297
|
+
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
298
|
+
var error = new IntlError(code, message);
|
|
299
|
+
onError(error);
|
|
300
|
+
return getMessageFallback({
|
|
301
|
+
error: error,
|
|
302
|
+
key: key,
|
|
303
|
+
namespace: namespace
|
|
304
|
+
});
|
|
305
|
+
}
|
|
353
306
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
307
|
+
function translateBaseFn(
|
|
308
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
309
|
+
key,
|
|
310
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
311
|
+
values,
|
|
312
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
313
|
+
formats) {
|
|
314
|
+
var _cachedFormatsByLocal;
|
|
315
|
+
|
|
316
|
+
if (messagesOrError instanceof IntlError) {
|
|
317
|
+
// We have already warned about this during render
|
|
365
318
|
return getMessageFallback({
|
|
366
|
-
error:
|
|
319
|
+
error: messagesOrError,
|
|
367
320
|
key: key,
|
|
368
321
|
namespace: namespace
|
|
369
322
|
});
|
|
370
323
|
}
|
|
371
324
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
378
|
-
formats) {
|
|
379
|
-
var _cachedFormatsByLocal;
|
|
380
|
-
|
|
381
|
-
var cachedFormatsByLocale = cachedFormatsByLocaleRef.current;
|
|
382
|
-
|
|
383
|
-
if (messagesOrError instanceof IntlError) {
|
|
384
|
-
// We have already warned about this during render
|
|
385
|
-
return getMessageFallback({
|
|
386
|
-
error: messagesOrError,
|
|
387
|
-
key: key,
|
|
388
|
-
namespace: namespace
|
|
389
|
-
});
|
|
390
|
-
}
|
|
325
|
+
var messages = messagesOrError;
|
|
326
|
+
var cacheKey = [namespace, key].filter(function (part) {
|
|
327
|
+
return part != null;
|
|
328
|
+
}).join('.');
|
|
329
|
+
var messageFormat;
|
|
391
330
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
var messageFormat;
|
|
397
|
-
|
|
398
|
-
if ((_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
399
|
-
messageFormat = cachedFormatsByLocale[locale][cacheKey];
|
|
400
|
-
} else {
|
|
401
|
-
var message;
|
|
402
|
-
|
|
403
|
-
try {
|
|
404
|
-
message = resolvePath(messages, key, namespace);
|
|
405
|
-
} catch (error) {
|
|
406
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
407
|
-
}
|
|
331
|
+
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
332
|
+
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
333
|
+
} else {
|
|
334
|
+
var message;
|
|
408
335
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
336
|
+
try {
|
|
337
|
+
message = resolvePath(messages, key, namespace);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
340
|
+
}
|
|
412
341
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
342
|
+
if (typeof message === 'object') {
|
|
343
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INSUFFICIENT_PATH, "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." );
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
try {
|
|
347
|
+
messageFormat = new IntlMessageFormat__default["default"](message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
348
|
+
} catch (error) {
|
|
349
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
350
|
+
}
|
|
418
351
|
|
|
352
|
+
if (cachedFormatsByLocale) {
|
|
419
353
|
if (!cachedFormatsByLocale[locale]) {
|
|
420
354
|
cachedFormatsByLocale[locale] = {};
|
|
421
355
|
}
|
|
422
356
|
|
|
423
357
|
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
424
358
|
}
|
|
359
|
+
}
|
|
425
360
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
361
|
+
try {
|
|
362
|
+
var formattedMessage = messageFormat.format( // @ts-ignore `intl-messageformat` expects a different format
|
|
363
|
+
// for rich text elements since a recent minor update. This
|
|
364
|
+
// needs to be evaluated in detail, possibly also in regards
|
|
365
|
+
// to be able to format to parts.
|
|
366
|
+
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
432
367
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
368
|
+
if (formattedMessage == null) {
|
|
369
|
+
throw new Error("development" !== "production" ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
370
|
+
} // Limit the function signature to return strings or React elements
|
|
436
371
|
|
|
437
372
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
373
|
+
return React.isValidElement(formattedMessage) || // Arrays of React elements
|
|
374
|
+
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
443
377
|
}
|
|
378
|
+
}
|
|
444
379
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
380
|
+
function translateFn(
|
|
381
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
382
|
+
key,
|
|
383
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
384
|
+
values,
|
|
385
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
386
|
+
formats) {
|
|
387
|
+
var result = translateBaseFn(key, values, formats);
|
|
388
|
+
|
|
389
|
+
if (typeof result !== 'string') {
|
|
390
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, "The message `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') + " didn't resolve to a string. If you want to format rich text, use `t.rich` instead." );
|
|
391
|
+
}
|
|
457
392
|
|
|
458
|
-
|
|
393
|
+
return result;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
translateFn.rich = translateBaseFn;
|
|
397
|
+
|
|
398
|
+
translateFn.raw = function (
|
|
399
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
400
|
+
key) {
|
|
401
|
+
if (messagesOrError instanceof IntlError) {
|
|
402
|
+
// We have already warned about this during render
|
|
403
|
+
return getMessageFallback({
|
|
404
|
+
error: messagesOrError,
|
|
405
|
+
key: key,
|
|
406
|
+
namespace: namespace
|
|
407
|
+
});
|
|
459
408
|
}
|
|
460
409
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
translateFn.raw = function (
|
|
464
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
465
|
-
key) {
|
|
466
|
-
if (messagesOrError instanceof IntlError) {
|
|
467
|
-
// We have already warned about this during render
|
|
468
|
-
return getMessageFallback({
|
|
469
|
-
error: messagesOrError,
|
|
470
|
-
key: key,
|
|
471
|
-
namespace: namespace
|
|
472
|
-
});
|
|
473
|
-
}
|
|
410
|
+
var messages = messagesOrError;
|
|
474
411
|
|
|
475
|
-
|
|
412
|
+
try {
|
|
413
|
+
return resolvePath(messages, key, namespace);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
476
418
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
} catch (error) {
|
|
480
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
481
|
-
}
|
|
482
|
-
};
|
|
419
|
+
return translateFn;
|
|
420
|
+
}
|
|
483
421
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
422
|
+
/**
|
|
423
|
+
* For the strictly typed messages to work we have to wrap the namespace into
|
|
424
|
+
* a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
|
|
425
|
+
*/
|
|
426
|
+
function resolveNamespace(namespace, namespacePrefix) {
|
|
427
|
+
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
487
428
|
}
|
|
488
429
|
|
|
430
|
+
var _excluded$2 = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
431
|
+
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
432
|
+
var getMessageFallback = _ref.getMessageFallback,
|
|
433
|
+
messages = _ref.messages,
|
|
434
|
+
namespace = _ref.namespace,
|
|
435
|
+
onError = _ref.onError,
|
|
436
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
|
|
437
|
+
|
|
438
|
+
// The `namespacePrefix` is part of the type system.
|
|
439
|
+
// See the comment in the function invocation.
|
|
440
|
+
messages = messages[namespacePrefix];
|
|
441
|
+
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
442
|
+
var translator = createBaseTranslator(_extends({}, rest, {
|
|
443
|
+
onError: onError,
|
|
444
|
+
getMessageFallback: getMessageFallback,
|
|
445
|
+
messagesOrError: getMessagesOrError({
|
|
446
|
+
messages: messages,
|
|
447
|
+
namespace: namespace,
|
|
448
|
+
onError: onError
|
|
449
|
+
})
|
|
450
|
+
}));
|
|
451
|
+
var originalRich = translator.rich;
|
|
452
|
+
|
|
453
|
+
function base() {
|
|
454
|
+
return translator.apply(void 0, arguments);
|
|
455
|
+
} // Augment `t.rich` to return plain strings
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
base.rich = function (key,
|
|
459
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
460
|
+
values, formats) {
|
|
461
|
+
// `chunks` is returned as a string when no React element
|
|
462
|
+
// is used, therefore it's safe to cast this type.
|
|
463
|
+
var result = originalRich(key, values, formats); // When only string chunks are provided to the parser, only strings should be returned here.
|
|
464
|
+
|
|
465
|
+
if (typeof result !== 'string') {
|
|
466
|
+
var error = new IntlError(exports.IntlErrorCode.FORMATTING_ERROR, "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" );
|
|
467
|
+
onError(error);
|
|
468
|
+
return getMessageFallback({
|
|
469
|
+
error: error,
|
|
470
|
+
key: key,
|
|
471
|
+
namespace: namespace
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
return result;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
base.raw = translator.raw;
|
|
479
|
+
return base;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
var _excluded$1 = ["onError", "getMessageFallback", "messages", "namespace"];
|
|
489
483
|
/**
|
|
490
484
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
491
485
|
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
@@ -495,16 +489,27 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
495
489
|
* (e.g. `namespace.Component`).
|
|
496
490
|
*/
|
|
497
491
|
|
|
498
|
-
function
|
|
499
|
-
var
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
492
|
+
function createTranslator(_ref) {
|
|
493
|
+
var _ref$onError = _ref.onError,
|
|
494
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
495
|
+
_ref$getMessageFallba = _ref.getMessageFallback,
|
|
496
|
+
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
497
|
+
messages = _ref.messages,
|
|
498
|
+
namespace = _ref.namespace,
|
|
499
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
503
500
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
501
|
+
// We have to wrap the actual function so the type inference for the optional
|
|
502
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
503
|
+
// The prefix ("!") is arbitrary.
|
|
504
|
+
return createTranslatorImpl(_extends({}, rest, {
|
|
505
|
+
onError: onError,
|
|
506
|
+
getMessageFallback: getMessageFallback,
|
|
507
|
+
messages: {
|
|
508
|
+
'!': messages
|
|
509
|
+
},
|
|
510
|
+
// @ts-ignore
|
|
511
|
+
namespace: namespace ? "!." + namespace : '!'
|
|
512
|
+
}), '!');
|
|
508
513
|
}
|
|
509
514
|
|
|
510
515
|
var MINUTE = 60;
|
|
@@ -549,13 +554,13 @@ function getRelativeTimeFormatConfig(seconds) {
|
|
|
549
554
|
};
|
|
550
555
|
}
|
|
551
556
|
|
|
552
|
-
function
|
|
553
|
-
var
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
onError =
|
|
558
|
-
timeZone =
|
|
557
|
+
function createIntl(_ref) {
|
|
558
|
+
var formats = _ref.formats,
|
|
559
|
+
locale = _ref.locale,
|
|
560
|
+
globalNow = _ref.now,
|
|
561
|
+
_ref$onError = _ref.onError,
|
|
562
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
563
|
+
timeZone = _ref.timeZone;
|
|
559
564
|
|
|
560
565
|
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
|
|
561
566
|
var options;
|
|
@@ -656,6 +661,151 @@ function useIntl() {
|
|
|
656
661
|
};
|
|
657
662
|
}
|
|
658
663
|
|
|
664
|
+
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
665
|
+
Object.entries(messages).forEach(function (_ref) {
|
|
666
|
+
var key = _ref[0],
|
|
667
|
+
messageOrMessages = _ref[1];
|
|
668
|
+
|
|
669
|
+
if (key.includes('.')) {
|
|
670
|
+
var keyLabel = key;
|
|
671
|
+
if (parentPath) keyLabel += " (at " + parentPath + ")";
|
|
672
|
+
invalidKeyLabels.push(keyLabel);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
if (messageOrMessages != null && typeof messageOrMessages === 'object') {
|
|
676
|
+
validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
|
|
677
|
+
return part != null;
|
|
678
|
+
}).join('.'));
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function validateMessages(messages, onError) {
|
|
684
|
+
var invalidKeyLabels = [];
|
|
685
|
+
validateMessagesSegment(messages, invalidKeyLabels);
|
|
686
|
+
|
|
687
|
+
if (invalidKeyLabels.length > 0) {
|
|
688
|
+
onError(new IntlError(exports.IntlErrorCode.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 " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ')));
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
var IntlContext = /*#__PURE__*/React.createContext(undefined);
|
|
693
|
+
|
|
694
|
+
var _excluded = ["children", "onError", "getMessageFallback", "messages"];
|
|
695
|
+
function IntlProvider(_ref) {
|
|
696
|
+
var children = _ref.children,
|
|
697
|
+
_ref$onError = _ref.onError,
|
|
698
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
699
|
+
_ref$getMessageFallba = _ref.getMessageFallback,
|
|
700
|
+
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
701
|
+
messages = _ref.messages,
|
|
702
|
+
contextValues = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
703
|
+
|
|
704
|
+
{
|
|
705
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
706
|
+
React.useEffect(function () {
|
|
707
|
+
if (messages) {
|
|
708
|
+
validateMessages(messages, onError);
|
|
709
|
+
}
|
|
710
|
+
}, [messages, onError]);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
return React__default["default"].createElement(IntlContext.Provider, {
|
|
714
|
+
value: _extends({}, contextValues, {
|
|
715
|
+
messages: messages,
|
|
716
|
+
onError: onError,
|
|
717
|
+
getMessageFallback: getMessageFallback
|
|
718
|
+
})
|
|
719
|
+
}, children);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function useIntlContext() {
|
|
723
|
+
var context = React.useContext(IntlContext);
|
|
724
|
+
|
|
725
|
+
if (!context) {
|
|
726
|
+
throw new Error('No intl context found. Have you configured the provider?' );
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
return context;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
733
|
+
var _useIntlContext = useIntlContext(),
|
|
734
|
+
defaultTranslationValues = _useIntlContext.defaultTranslationValues,
|
|
735
|
+
globalFormats = _useIntlContext.formats,
|
|
736
|
+
getMessageFallback = _useIntlContext.getMessageFallback,
|
|
737
|
+
locale = _useIntlContext.locale,
|
|
738
|
+
onError = _useIntlContext.onError,
|
|
739
|
+
timeZone = _useIntlContext.timeZone; // The `namespacePrefix` is part of the type system.
|
|
740
|
+
// See the comment in the hook invocation.
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
allMessages = allMessages[namespacePrefix];
|
|
744
|
+
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
745
|
+
var cachedFormatsByLocaleRef = React.useRef({});
|
|
746
|
+
var messagesOrError = React.useMemo(function () {
|
|
747
|
+
return getMessagesOrError({
|
|
748
|
+
messages: allMessages,
|
|
749
|
+
namespace: namespace,
|
|
750
|
+
onError: onError
|
|
751
|
+
});
|
|
752
|
+
}, [allMessages, namespace, onError]);
|
|
753
|
+
var translate = React.useMemo(function () {
|
|
754
|
+
return createBaseTranslator({
|
|
755
|
+
cachedFormatsByLocale: cachedFormatsByLocaleRef.current,
|
|
756
|
+
getMessageFallback: getMessageFallback,
|
|
757
|
+
messagesOrError: messagesOrError,
|
|
758
|
+
defaultTranslationValues: defaultTranslationValues,
|
|
759
|
+
namespace: namespace,
|
|
760
|
+
onError: onError,
|
|
761
|
+
formats: globalFormats,
|
|
762
|
+
locale: locale,
|
|
763
|
+
timeZone: timeZone
|
|
764
|
+
});
|
|
765
|
+
}, [getMessageFallback, messagesOrError, defaultTranslationValues, namespace, onError, globalFormats, locale, timeZone]);
|
|
766
|
+
return translate;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
771
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
772
|
+
*
|
|
773
|
+
* If no namespace is provided, all available messages are returned.
|
|
774
|
+
* The namespace can also indicate nesting by using a dot
|
|
775
|
+
* (e.g. `namespace.Component`).
|
|
776
|
+
*/
|
|
777
|
+
|
|
778
|
+
function useTranslations(namespace) {
|
|
779
|
+
var context = useIntlContext();
|
|
780
|
+
var messages = context.messages; // We have to wrap the actual hook so the type inference for the optional
|
|
781
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
782
|
+
// The prefix ("!") is arbitrary.
|
|
783
|
+
|
|
784
|
+
return useTranslationsImpl({
|
|
785
|
+
'!': messages
|
|
786
|
+
}, // @ts-ignore
|
|
787
|
+
namespace ? "!." + namespace : '!', '!');
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
function useIntl() {
|
|
791
|
+
var _useIntlContext = useIntlContext(),
|
|
792
|
+
formats = _useIntlContext.formats,
|
|
793
|
+
locale = _useIntlContext.locale,
|
|
794
|
+
globalNow = _useIntlContext.now,
|
|
795
|
+
onError = _useIntlContext.onError,
|
|
796
|
+
timeZone = _useIntlContext.timeZone;
|
|
797
|
+
|
|
798
|
+
return React.useMemo(function () {
|
|
799
|
+
return createIntl({
|
|
800
|
+
formats: formats,
|
|
801
|
+
locale: locale,
|
|
802
|
+
now: globalNow,
|
|
803
|
+
onError: onError,
|
|
804
|
+
timeZone: timeZone
|
|
805
|
+
});
|
|
806
|
+
}, [formats, globalNow, locale, onError, timeZone]);
|
|
807
|
+
}
|
|
808
|
+
|
|
659
809
|
function useLocale() {
|
|
660
810
|
return useIntlContext().locale;
|
|
661
811
|
}
|
|
@@ -711,6 +861,8 @@ function useTimeZone() {
|
|
|
711
861
|
|
|
712
862
|
exports.IntlError = IntlError;
|
|
713
863
|
exports.IntlProvider = IntlProvider;
|
|
864
|
+
exports.createIntl = createIntl;
|
|
865
|
+
exports.createTranslator = createTranslator;
|
|
714
866
|
exports.useIntl = useIntl;
|
|
715
867
|
exports.useLocale = useLocale;
|
|
716
868
|
exports.useNow = useNow;
|