use-intl 3.0.0-beta.3 → 3.0.0-rc.6

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.
Files changed (40) hide show
  1. package/dist/development/_IntlProvider.js +1 -1
  2. package/dist/development/core.js +6 -32
  3. package/dist/development/{createIntl-dfba4462.js → createFormatter-4848f6c8.js} +90 -58
  4. package/dist/development/index.js +3 -6
  5. package/dist/development/{initializeConfig-cde42612.js → initializeConfig-0a0b87a7.js} +1 -0
  6. package/dist/development/react.js +16 -31
  7. package/dist/esm/_IntlProvider.js +1 -1
  8. package/dist/esm/core.js +1 -1
  9. package/dist/esm/createFormatter-c871a8ce.js +1 -0
  10. package/dist/esm/index.js +1 -1
  11. package/dist/esm/initializeConfig-29e7ba4c.js +1 -0
  12. package/dist/esm/react.js +1 -1
  13. package/dist/production/core.js +1 -1
  14. package/dist/production/createFormatter-7a8c6f36.js +1 -0
  15. package/dist/production/index.js +1 -1
  16. package/dist/production/react.js +1 -1
  17. package/dist/types/src/core/IntlConfig.d.ts +1 -1
  18. package/dist/types/src/core/IntlError.d.ts +1 -0
  19. package/dist/types/src/core/RelativeTimeFormatOptions.d.ts +5 -0
  20. package/dist/types/src/core/TranslationValues.d.ts +1 -1
  21. package/dist/types/src/core/createBaseTranslator.d.ts +2 -1
  22. package/dist/types/src/core/createFormatter.d.ts +3 -2
  23. package/dist/types/src/core/createTranslator.d.ts +16 -3
  24. package/dist/types/src/core/createTranslatorImpl.d.ts +7 -4
  25. package/dist/types/src/core/index.d.ts +1 -3
  26. package/dist/types/src/react/IntlProvider.d.ts +2 -2
  27. package/dist/types/src/react/index.d.ts +0 -1
  28. package/dist/types/src/react/useFormatter.d.ts +1 -1
  29. package/dist/types/src/react/useMessages.d.ts +2 -1
  30. package/dist/types/src/react/useTranslations.d.ts +10 -1
  31. package/dist/types/src/react/useTranslationsImpl.d.ts +2 -1
  32. package/package.json +5 -4
  33. package/dist/esm/createIntl-164a7766.js +0 -1
  34. package/dist/esm/initializeConfig-c316f266.js +0 -1
  35. package/dist/production/createIntl-e408cd5c.js +0 -1
  36. package/dist/types/src/core/createIntl.d.ts +0 -7
  37. package/dist/types/src/react/getInitializedConfig.d.ts +0 -1
  38. package/dist/types/src/react/useIntl.d.ts +0 -6
  39. package/dist/types/test/core/createIntl.test.d.ts +0 -1
  40. package/dist/types/test/react/useIntl.test.d.ts +0 -1
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var React = require('react');
6
- var initializeConfig = require('./initializeConfig-cde42612.js');
6
+ var initializeConfig = require('./initializeConfig-0a0b87a7.js');
7
7
  var IntlContext = require('./IntlContext-b5cc6be8.js');
8
8
 
9
9
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var initializeConfig = require('./initializeConfig-cde42612.js');
6
- var createIntl = require('./createIntl-dfba4462.js');
5
+ var initializeConfig = require('./initializeConfig-0a0b87a7.js');
6
+ var createFormatter = require('./createFormatter-4848f6c8.js');
7
7
  require('intl-messageformat');
8
8
  require('react');
9
9
 
@@ -18,39 +18,14 @@ function createTranslatorImpl(_ref, namespacePrefix) {
18
18
  // The `namespacePrefix` is part of the type system.
19
19
  // See the comment in the function invocation.
20
20
  messages = messages[namespacePrefix];
21
- namespace = createIntl.resolveNamespace(namespace, namespacePrefix);
22
- const translator = createIntl.createBaseTranslator({
21
+ namespace = createFormatter.resolveNamespace(namespace, namespacePrefix);
22
+ return createFormatter.createBaseTranslator({
23
23
  ...rest,
24
24
  onError,
25
25
  getMessageFallback,
26
26
  messages,
27
27
  namespace
28
28
  });
29
- const originalRich = translator.rich;
30
- function base() {
31
- return translator(...arguments);
32
- }
33
-
34
- // Augment `t.rich` to return plain strings
35
- base.rich = (key, values, formats) => {
36
- // `chunks` is returned as a string when no React element
37
- // is used, therefore it's safe to cast this type.
38
- const result = originalRich(key, values, formats);
39
-
40
- // When only string chunks are provided to the parser, only strings should be returned here.
41
- if (typeof result !== 'string') {
42
- const error = new initializeConfig.IntlError(initializeConfig.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>`})" );
43
- onError(error);
44
- return getMessageFallback({
45
- error,
46
- key,
47
- namespace
48
- });
49
- }
50
- return result;
51
- };
52
- base.raw = translator.raw;
53
- return base;
54
29
  }
55
30
 
56
31
  /**
@@ -76,6 +51,7 @@ function createTranslator(_ref) {
76
51
  ...rest,
77
52
  onError,
78
53
  getMessageFallback,
54
+ // @ts-expect-error `messages` is allowed to be `undefined` here and will be handled internally
79
55
  messages: {
80
56
  '!': messages
81
57
  },
@@ -86,7 +62,5 @@ function createTranslator(_ref) {
86
62
  exports.IntlError = initializeConfig.IntlError;
87
63
  exports.IntlErrorCode = initializeConfig.IntlErrorCode;
88
64
  exports.initializeConfig = initializeConfig.initializeConfig;
89
- exports.createBaseTranslator = createIntl.createBaseTranslator;
90
- exports.createFormatter = createIntl.createFormatter;
91
- exports.createIntl = createIntl.createIntl;
65
+ exports.createFormatter = createFormatter.createFormatter;
92
66
  exports.createTranslator = createTranslator;
@@ -2,7 +2,7 @@
2
2
 
3
3
  var IntlMessageFormat = require('intl-messageformat');
4
4
  var React = require('react');
5
- var initializeConfig = require('./initializeConfig-cde42612.js');
5
+ var initializeConfig = require('./initializeConfig-0a0b87a7.js');
6
6
 
7
7
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
8
 
@@ -245,6 +245,29 @@ function createBaseTranslatorImpl(_ref2) {
245
245
  return result;
246
246
  }
247
247
  translateFn.rich = translateBaseFn;
248
+
249
+ // Augment `translateBaseFn` to return plain strings
250
+ translateFn.markup = (key, values, formats) => {
251
+ const result = translateBaseFn(key,
252
+ // @ts-expect-error -- `MarkupTranslationValues` is practically a sub type
253
+ // of `RichTranslationValues` but TypeScript isn't smart enough here.
254
+ values, formats);
255
+
256
+ // When only string chunks are provided to the parser, only
257
+ // strings should be returned here. Note that we need a runtime
258
+ // check for this since rich text values could be accidentally
259
+ // inherited from `defaultTranslationValues`.
260
+ if (typeof result !== 'string') {
261
+ const error = new initializeConfig.IntlError(initializeConfig.IntlErrorCode.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>`})" );
262
+ onError(error);
263
+ return getMessageFallback({
264
+ error,
265
+ key,
266
+ namespace
267
+ });
268
+ }
269
+ return result;
270
+ };
248
271
  translateFn.raw = key => {
249
272
  if (messagesOrError instanceof initializeConfig.IntlError) {
250
273
  // We have already warned about this during render
@@ -272,45 +295,53 @@ function resolveNamespace(namespace, namespacePrefix) {
272
295
  return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
273
296
  }
274
297
 
275
- const MINUTE = 60;
298
+ const SECOND = 1;
299
+ const MINUTE = SECOND * 60;
276
300
  const HOUR = MINUTE * 60;
277
301
  const DAY = HOUR * 24;
278
302
  const WEEK = DAY * 7;
279
303
  const MONTH = DAY * (365 / 12); // Approximation
304
+ const QUARTER = MONTH * 3;
280
305
  const YEAR = DAY * 365;
281
- function getRelativeTimeFormatConfig(seconds) {
306
+ const UNIT_SECONDS = {
307
+ second: SECOND,
308
+ seconds: SECOND,
309
+ minute: MINUTE,
310
+ minutes: MINUTE,
311
+ hour: HOUR,
312
+ hours: HOUR,
313
+ day: DAY,
314
+ days: DAY,
315
+ week: WEEK,
316
+ weeks: WEEK,
317
+ month: MONTH,
318
+ months: MONTH,
319
+ quarter: QUARTER,
320
+ quarters: QUARTER,
321
+ year: YEAR,
322
+ years: YEAR
323
+ };
324
+ function resolveRelativeTimeUnit(seconds) {
282
325
  const absValue = Math.abs(seconds);
283
- let value, unit;
284
-
285
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
286
- // will include fractions like '2.1 hours ago'.
287
-
288
326
  if (absValue < MINUTE) {
289
- unit = 'second';
290
- value = Math.round(seconds);
327
+ return 'second';
291
328
  } else if (absValue < HOUR) {
292
- unit = 'minute';
293
- value = Math.round(seconds / MINUTE);
329
+ return 'minute';
294
330
  } else if (absValue < DAY) {
295
- unit = 'hour';
296
- value = Math.round(seconds / HOUR);
331
+ return 'hour';
297
332
  } else if (absValue < WEEK) {
298
- unit = 'day';
299
- value = Math.round(seconds / DAY);
333
+ return 'day';
300
334
  } else if (absValue < MONTH) {
301
- unit = 'week';
302
- value = Math.round(seconds / WEEK);
335
+ return 'week';
303
336
  } else if (absValue < YEAR) {
304
- unit = 'month';
305
- value = Math.round(seconds / MONTH);
306
- } else {
307
- unit = 'year';
308
- value = Math.round(seconds / YEAR);
337
+ return 'month';
309
338
  }
310
- return {
311
- value,
312
- unit
313
- };
339
+ return 'year';
340
+ }
341
+ function calculateRelativeTimeValue(seconds, unit) {
342
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
343
+ // will include fractions like '2.1 hours ago'.
344
+ return Math.round(seconds / UNIT_SECONDS[unit]);
314
345
  }
315
346
  function createFormatter(_ref) {
316
347
  let {
@@ -318,7 +349,7 @@ function createFormatter(_ref) {
318
349
  locale,
319
350
  now: globalNow,
320
351
  onError = initializeConfig.defaultOnError,
321
- timeZone
352
+ timeZone: globalTimeZone
322
353
  } = _ref;
323
354
  function resolveFormatOrOptions(typeFormats, formatOrOptions) {
324
355
  let options;
@@ -356,11 +387,15 @@ function createFormatter(_ref) {
356
387
  formatOrOptions) {
357
388
  return getFormattedValue(value, formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.dateTime, options => {
358
389
  var _options;
359
- if (timeZone && !((_options = options) !== null && _options !== void 0 && _options.timeZone)) {
360
- options = {
361
- ...options,
362
- timeZone
363
- };
390
+ if (!((_options = options) !== null && _options !== void 0 && _options.timeZone)) {
391
+ if (globalTimeZone) {
392
+ options = {
393
+ ...options,
394
+ timeZone: globalTimeZone
395
+ };
396
+ } else {
397
+ onError(new initializeConfig.IntlError(initializeConfig.IntlErrorCode.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" ));
398
+ }
364
399
  }
365
400
  return new Intl.DateTimeFormat(locale, options).format(value);
366
401
  });
@@ -368,24 +403,32 @@ function createFormatter(_ref) {
368
403
  function number(value, formatOrOptions) {
369
404
  return getFormattedValue(value, formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.number, options => new Intl.NumberFormat(locale, options).format(value));
370
405
  }
406
+ function getGlobalNow() {
407
+ if (globalNow) {
408
+ return globalNow;
409
+ } else {
410
+ onError(new initializeConfig.IntlError(initializeConfig.IntlErrorCode.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" ));
411
+ return new Date();
412
+ }
413
+ }
414
+ function extractNowDate(nowOrOptions) {
415
+ if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
416
+ return new Date(nowOrOptions);
417
+ }
418
+ if ((nowOrOptions === null || nowOrOptions === void 0 ? void 0 : nowOrOptions.now) !== undefined) {
419
+ return new Date(nowOrOptions.now);
420
+ }
421
+ return getGlobalNow();
422
+ }
371
423
  function relativeTime( /** The date time that needs to be formatted. */
372
424
  date, /** The reference point in time to which `date` will be formatted in relation to. */
373
- now) {
425
+ nowOrOptions) {
374
426
  try {
375
- if (!now) {
376
- if (globalNow) {
377
- now = globalNow;
378
- } else {
379
- throw new Error("The `now` parameter wasn't provided and there was no global fallback configured on the provider." );
380
- }
381
- }
382
- const dateDate = date instanceof Date ? date : new Date(date);
383
- const nowDate = now instanceof Date ? now : new Date(now);
427
+ const dateDate = new Date(date);
428
+ const nowDate = extractNowDate(nowOrOptions);
384
429
  const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
385
- const {
386
- unit,
387
- value
388
- } = getRelativeTimeFormatConfig(seconds);
430
+ const unit = typeof nowOrOptions === 'number' || nowOrOptions instanceof Date || (nowOrOptions === null || nowOrOptions === void 0 ? void 0 : nowOrOptions.unit) === undefined ? resolveRelativeTimeUnit(seconds) : nowOrOptions.unit;
431
+ const value = calculateRelativeTimeValue(seconds, unit);
389
432
  return new Intl.RelativeTimeFormat(locale, {
390
433
  numeric: 'auto'
391
434
  }).format(value, unit);
@@ -405,17 +448,6 @@ function createFormatter(_ref) {
405
448
  };
406
449
  }
407
450
 
408
- /** @deprecated Switch to `createFormatter` */
409
- function createIntl() {
410
- const formatter = createFormatter(...arguments);
411
- return {
412
- formatDateTime: formatter.dateTime,
413
- formatNumber: formatter.number,
414
- formatRelativeTime: formatter.relativeTime
415
- };
416
- }
417
-
418
451
  exports.createBaseTranslator = createBaseTranslator;
419
452
  exports.createFormatter = createFormatter;
420
- exports.createIntl = createIntl;
421
453
  exports.resolveNamespace = resolveNamespace;
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var initializeConfig = require('./initializeConfig-cde42612.js');
5
+ var initializeConfig = require('./initializeConfig-0a0b87a7.js');
6
6
  var core = require('./core.js');
7
- var createIntl = require('./createIntl-dfba4462.js');
7
+ var createFormatter = require('./createFormatter-4848f6c8.js');
8
8
  var _IntlProvider = require('./_IntlProvider.js');
9
9
  var react = require('./react.js');
10
10
  var _useLocale = require('./_useLocale-321e619f.js');
@@ -18,12 +18,9 @@ exports.IntlError = initializeConfig.IntlError;
18
18
  exports.IntlErrorCode = initializeConfig.IntlErrorCode;
19
19
  exports.initializeConfig = initializeConfig.initializeConfig;
20
20
  exports.createTranslator = core.createTranslator;
21
- exports.createBaseTranslator = createIntl.createBaseTranslator;
22
- exports.createFormatter = createIntl.createFormatter;
23
- exports.createIntl = createIntl.createIntl;
21
+ exports.createFormatter = createFormatter.createFormatter;
24
22
  exports.IntlProvider = _IntlProvider.IntlProvider;
25
23
  exports.useFormatter = react.useFormatter;
26
- exports.useIntl = react.useIntl;
27
24
  exports.useMessages = react.useMessages;
28
25
  exports.useNow = react.useNow;
29
26
  exports.useTimeZone = react.useTimeZone;
@@ -32,6 +32,7 @@ function _toPropertyKey(arg) {
32
32
  let IntlErrorCode = /*#__PURE__*/function (IntlErrorCode) {
33
33
  IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
34
34
  IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
35
+ IntlErrorCode["ENVIRONMENT_FALLBACK"] = "ENVIRONMENT_FALLBACK";
35
36
  IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
36
37
  IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
37
38
  IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
@@ -5,11 +5,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _IntlProvider = require('./_IntlProvider.js');
6
6
  var _useLocale = require('./_useLocale-321e619f.js');
7
7
  var React = require('react');
8
- var createIntl = require('./createIntl-dfba4462.js');
9
- require('./initializeConfig-cde42612.js');
8
+ var createFormatter = require('./createFormatter-4848f6c8.js');
9
+ var initializeConfig = require('./initializeConfig-0a0b87a7.js');
10
10
  require('./IntlContext-b5cc6be8.js');
11
11
  require('intl-messageformat');
12
12
 
13
+ let hasWarnedForMissingTimezone = false;
14
+ const isServer = typeof window === 'undefined';
13
15
  function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
14
16
  const {
15
17
  defaultTranslationValues,
@@ -24,8 +26,12 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
24
26
  // The `namespacePrefix` is part of the type system.
25
27
  // See the comment in the hook invocation.
26
28
  allMessages = allMessages[namespacePrefix];
27
- namespace = createIntl.resolveNamespace(namespace, namespacePrefix);
28
- const translate = React.useMemo(() => createIntl.createBaseTranslator({
29
+ namespace = createFormatter.resolveNamespace(namespace, namespacePrefix);
30
+ if (!timeZone && !hasWarnedForMissingTimezone && isServer) {
31
+ hasWarnedForMissingTimezone = true;
32
+ 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
+ const translate = React.useMemo(() => createFormatter.createBaseTranslator({
29
35
  messageFormatCache,
30
36
  getMessageFallback,
31
37
  messages: allMessages,
@@ -106,7 +112,11 @@ function useTimeZone() {
106
112
  }
107
113
 
108
114
  function useMessages() {
109
- return _useLocale.useIntlContext().messages;
115
+ const context = _useLocale.useIntlContext();
116
+ if (!context.messages) {
117
+ throw new Error('No messages found. Have you configured them correctly? See https://next-intl-docs.vercel.app/docs/configuration#messages' );
118
+ }
119
+ return context.messages;
110
120
  }
111
121
 
112
122
  function useFormatter() {
@@ -117,31 +127,7 @@ function useFormatter() {
117
127
  onError,
118
128
  timeZone
119
129
  } = _useLocale.useIntlContext();
120
- return React.useMemo(() => createIntl.createFormatter({
121
- formats,
122
- locale,
123
- now: globalNow,
124
- onError,
125
- timeZone
126
- }), [formats, globalNow, locale, onError, timeZone]);
127
- }
128
-
129
- let hasWarned = false;
130
-
131
- /** @deprecated Switch to `useFormatter` instead. */
132
- function useIntl() {
133
- const {
134
- formats,
135
- locale,
136
- now: globalNow,
137
- onError,
138
- timeZone
139
- } = _useLocale.useIntlContext();
140
- if (!hasWarned) {
141
- hasWarned = true;
142
- console.warn('`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.');
143
- }
144
- return React.useMemo(() => createIntl.createIntl({
130
+ return React.useMemo(() => createFormatter.createFormatter({
145
131
  formats,
146
132
  locale,
147
133
  now: globalNow,
@@ -153,7 +139,6 @@ function useIntl() {
153
139
  exports.IntlProvider = _IntlProvider.IntlProvider;
154
140
  exports.useLocale = _useLocale.useLocale;
155
141
  exports.useFormatter = useFormatter;
156
- exports.useIntl = useIntl;
157
142
  exports.useMessages = useMessages;
158
143
  exports.useNow = useNow;
159
144
  exports.useTimeZone = useTimeZone;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-c316f266.js"),r=require("./IntlContext-381f3ce4.js");function i(e){return e&&e.__esModule?e:{default:e}}var n=i(e);exports.IntlProvider=function(i){let{children:a,...o}=i;const[u]=e.useState((()=>new Map));return n.default.createElement(r.IntlContext.Provider,{value:{...t.initializeConfig(o),messageFormatCache:u}},a)};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-29e7ba4c.js"),r=require("./IntlContext-381f3ce4.js");function i(e){return e&&e.__esModule?e:{default:e}}var n=i(e);exports.IntlProvider=function(i){let{children:a,...o}=i;const[u]=e.useState((()=>new Map));return n.default.createElement(r.IntlContext.Provider,{value:{...t.initializeConfig(o),messageFormatCache:u}},a)};
package/dist/esm/core.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-c316f266.js"),r=require("./createIntl-164a7766.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createBaseTranslator=r.createBaseTranslator,exports.createFormatter=r.createFormatter,exports.createIntl=r.createIntl,exports.createTranslator=function(t){let{getMessageFallback:a=e.defaultGetMessageFallback,messages:s,namespace:n,onError:o=e.defaultOnError,...c}=t;return function(t,a){let{getMessageFallback:s,messages:n,namespace:o,onError:c,...i}=t;n=n[a],o=r.resolveNamespace(o,a);const l=r.createBaseTranslator({...i,onError:c,getMessageFallback:s,messages:n,namespace:o}),g=l.rich;function u(){return l(...arguments)}return u.rich=(r,t,a)=>{const n=g(r,t,a);if("string"!=typeof n){const t=new e.IntlError(e.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>`})");return c(t),s({error:t,key:r,namespace:o})}return n},u.raw=l.raw,u}({...c,onError:o,getMessageFallback:a,messages:{"!":s},namespace:n?"!.".concat(n):"!"},"!")};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-29e7ba4c.js"),r=require("./createFormatter-c871a8ce.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createFormatter=r.createFormatter,exports.createTranslator=function(a){let{getMessageFallback:t=e.defaultGetMessageFallback,messages:s,namespace:o,onError:n=e.defaultOnError,...l}=a;return function(e,a){let{getMessageFallback:t,messages:s,namespace:o,onError:n,...l}=e;return s=s[a],o=r.resolveNamespace(o,a),r.createBaseTranslator({...l,onError:n,getMessageFallback:t,messages:s,namespace:o})}({...l,onError:n,getMessageFallback:t,messages:{"!":s},namespace:o?"!.".concat(o):"!"},"!")};
@@ -0,0 +1 @@
1
+ "use strict";var e=require("intl-messageformat"),r=require("react"),t=require("./initializeConfig-29e7ba4c.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function a(e,r){return e?Object.keys(e).reduce(((t,n)=>(t[n]={timeZone:r,...e[n]},t)),{}):e}function s(e,r,t){if(!e)throw new Error("No messages available at `".concat(t,"`."));let n=e;return r.split(".").forEach((e=>{const o=n[e];if(null==e||null==o)throw new Error("Could not resolve `".concat(r,"` in ").concat(t?"`".concat(t,"`"):"messages","."));n=o})),n}const i=60,c=60*i,u=24*c,l=7*u,m=u*(365/12),d=3*m,f=365*u,g={second:1,seconds:1,minute:i,minutes:i,hour:c,hours:c,day:u,days:u,week:l,weeks:l,month:m,months:m,quarter:d,quarters:d,year:f,years:f};exports.createBaseTranslator=function(e){const n=function(e){let{messages:r,namespace:n,onError:o=t.defaultOnError}=e;try{if(!r)throw new Error("No messages were configured on the provider.");const e=n?s(r,n):r;if(!e)throw new Error("No messages for namespace `".concat(n,"` found."));return e}catch(e){const r=new t.IntlError(t.IntlErrorCode.MISSING_MESSAGE,e.message);return o(r),r}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:c=t.defaultGetMessageFallback,locale:u,messageFormatCache:l,messagesOrError:m,namespace:d,onError:f,timeZone:g}=e;function E(e,r,n){const o=new t.IntlError(r,n);return f(o),c({error:o,key:e,namespace:d})}function p(e,f,p){if(m instanceof t.IntlError)return c({error:m,key:e,namespace:d});const I=m;let h;try{h=s(I,e,d)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}function y(e){return e.filter((e=>null!=e)).join(".")}const w=y([u,d,e,String(h)]);let v;if(null!=l&&l.has(w))v=l.get(w);else{if("object"==typeof h){let r,n;return Array.isArray(h)?(r=t.IntlErrorCode.INVALID_MESSAGE,n="Message at `".concat(y([d,e]),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(r=t.IntlErrorCode.INSUFFICIENT_PATH,n="Message at `".concat(y([d,e]),"` 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")),E(e,r,n)}const r=function(e,r){if(r)return;const t=e.replace(/'([{}])/gi,"$1");return/<|{/.test(t)?void 0:t}(h,f);if(r)return r;try{v=new o.default(h,u,function(e,r){const t=r?{...e,dateTime:a(e.dateTime,r)}:e,n=o.default.formats.date,s=r?a(n,r):n,i=o.default.formats.time,c=r?a(i,r):i;return{...t,date:{...s,...null==t?void 0:t.dateTime},time:{...c,...null==t?void 0:t.dateTime}}}({...i,...p},g))}catch(r){return E(e,t.IntlErrorCode.INVALID_MESSAGE,r.message)}null==l||l.set(w,v)}try{const t=v.format(function(e){if(0===Object.keys(e).length)return;const t={};return Object.keys(e).forEach((n=>{let o=0;const a=e[n];let s;s="function"==typeof a?e=>{const t=a(e);return r.isValidElement(t)?r.cloneElement(t,{key:n+o++}):t}:a,t[n]=s})),t}({...n,...f}));if(null==t)throw new Error("Unable to format `".concat(e,"` in ").concat(d?"namespace `".concat(d,"`"):"messages"));return r.isValidElement(t)||Array.isArray(t)||"string"==typeof t?t:String(t)}catch(r){return E(e,t.IntlErrorCode.FORMATTING_ERROR,r.message)}}function I(e,r,n){const o=p(e,r,n);return"string"!=typeof o?E(e,t.IntlErrorCode.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(d?"namespace `".concat(d,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):o}return I.rich=p,I.markup=(e,r,n)=>{const o=p(e,r,n);if("string"!=typeof o){const r=new t.IntlError(t.IntlErrorCode.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 f(r),c({error:r,key:e,namespace:d})}return o},I.raw=e=>{if(m instanceof t.IntlError)return c({error:m,key:e,namespace:d});const r=m;try{return s(r,e,d)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}},I}({...e,messagesOrError:n})},exports.createFormatter=function(e){let{formats:r,locale:n,now:o,onError:a=t.defaultOnError,timeZone:s}=e;function d(e,r,n,o){let s;try{s=function(e,r){let n;if("string"==typeof r){const o=r;if(n=null==e?void 0:e[o],!n){const e=new t.IntlError(t.IntlErrorCode.MISSING_FORMAT,"Format `".concat(o,"` is not available. You can configure it on the provider or provide custom options."));throw a(e),e}}else n=r;return n}(n,r)}catch(r){return String(e)}try{return o(s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}}function E(e){return e instanceof Date||"number"==typeof e?new Date(e):void 0!==(null==e?void 0:e.now)?new Date(e.now):o||(a(new t.IntlError(t.IntlErrorCode.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:function(e,o){return d(e,o,null==r?void 0:r.dateTime,(r=>{var o;return null!==(o=r)&&void 0!==o&&o.timeZone||(s?r={...r,timeZone:s}:a(new t.IntlError(t.IntlErrorCode.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"))),new Intl.DateTimeFormat(n,r).format(e)}))},number:function(e,t){return d(e,t,null==r?void 0:r.number,(r=>new Intl.NumberFormat(n,r).format(e)))},relativeTime:function(e,r){try{const t=new Date(e),o=E(r),a=(t.getTime()-o.getTime())/1e3,s="number"==typeof r||r instanceof Date||void 0===(null==r?void 0:r.unit)?function(e){const r=Math.abs(e);return r<i?"second":r<c?"minute":r<u?"hour":r<l?"day":r<m?"week":r<f?"month":"year"}(a):r.unit,d=function(e,r){return Math.round(e/g[r])}(a,s);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(d,s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}},list:function(e,t){return d(e,t,null==r?void 0:r.list,(r=>new Intl.ListFormat(n,r).format(e)))}}},exports.resolveNamespace=function(e,r){return e===r?void 0:e.slice((r+".").length)};
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-c316f266.js"),r=require("./core.js"),t=require("./createIntl-164a7766.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),a=require("./_useLocale-89b32eb9.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createTranslator=r.createTranslator,exports.createBaseTranslator=t.createBaseTranslator,exports.createFormatter=t.createFormatter,exports.createIntl=t.createIntl,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useIntl=o.useIntl,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=a.useLocale;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-29e7ba4c.js"),r=require("./core.js"),t=require("./createFormatter-c871a8ce.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),i=require("./_useLocale-89b32eb9.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createTranslator=r.createTranslator,exports.createFormatter=t.createFormatter,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=i.useLocale;
@@ -0,0 +1 @@
1
+ "use strict";function e(e,n,t){return(n=function(e){var n=function(e,n){if("object"!=typeof e||null===e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,n||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(e)}(e,"string");return"symbol"==typeof n?n:String(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 r=n;t&&(r+=": "+t),super(r),e(this,"code",void 0),e(this,"originalMessage",void 0),this.code=n,t&&(this.originalMessage=t)}}function r(e){return[e.namespace,e.key].filter((e=>null!=e)).join(".")}function o(e){console.error(e)}function i(e,n,t){Object.entries(e).forEach((e=>{let[r,o]=e;if(r.includes(".")){let e=r;t&&(e+=" (at ".concat(t,")")),n.push(e)}null!=o&&"object"==typeof o&&i(o,n,[t,r].filter((e=>null!=e)).join("."))}))}function s(e,r){const o=[];i(e,o),o.length>0&&r(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===o.length?"key":"keys",": ").concat(o.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')))}exports.IntlError=t,exports.IntlErrorCode=n,exports.defaultGetMessageFallback=r,exports.defaultOnError=o,exports.initializeConfig=function(e){let{getMessageFallback:n,messages:t,onError:i,...a}=e;const c=i||o,l=n||r;return t&&s(t,c),{...a,messages:t,onError:c,getMessageFallback:l}};
package/dist/esm/react.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-89b32eb9.js"),r=require("react"),o=require("./createIntl-164a7766.js");function n(){return new Date}require("./initializeConfig-c316f266.js"),require("./IntlContext-381f3ce4.js"),require("intl-messageformat");let s=!1;exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:l}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:l})),[e,s,n,a,l])},exports.useIntl=function(){const{formats:e,locale:n,now:a,onError:l,timeZone:u}=t.useIntlContext();return s||(s=!0,console.warn("`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.")),r.useMemo((()=>o.createIntl({formats:e,locale:n,now:a,onError:l,timeZone:u})),[e,a,n,l,u])},exports.useMessages=function(){return t.useIntlContext().messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:s}=t.useIntlContext(),[a,l]=r.useState(s||n());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{l(n())}),o);return()=>{clearInterval(e)}}),[s,o]),a},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,n,s){const{defaultTranslationValues:a,formats:l,getMessageFallback:u,locale:i,messageFormatCache:c,onError:m,timeZone:f}=t.useIntlContext();return e=e[s],n=o.resolveNamespace(n,s),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:c,getMessageFallback:u,messages:e,defaultTranslationValues:a,namespace:n,onError:m,formats:l,locale:i,timeZone:f})),[c,u,e,n,m,a,l,i,f])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-89b32eb9.js"),r=require("react"),o=require("./createFormatter-c871a8ce.js"),n=require("./initializeConfig-29e7ba4c.js");require("./IntlContext-381f3ce4.js"),require("intl-messageformat");let s=!1;const a="undefined"==typeof window;function u(){return new Date}exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useMessages=function(){const e=t.useIntlContext();if(!e.messages)throw new Error("No messages found. Have you configured them correctly? See https://next-intl-docs.vercel.app/docs/configuration#messages");return e.messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:n}=t.useIntlContext(),[s,a]=r.useState(n||u());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{a(u())}),o);return()=>{clearInterval(e)}}),[n,o]),s},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,u,i){const{defaultTranslationValues:l,formats:c,getMessageFallback:m,locale:f,messageFormatCache:d,onError:g,timeZone:p}=t.useIntlContext();return e=e[i],u=o.resolveNamespace(u,i),p||s||!a||(s=!0,g(new n.IntlError(n.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"))),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:d,getMessageFallback:m,messages:e,defaultTranslationValues:l,namespace:u,onError:g,formats:c,locale:f,timeZone:p})),[d,m,e,u,g,l,c,f,p])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createIntl-e408cd5c.js"),r=require("./initializeConfig-984a566d.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createBaseTranslator=e.createBaseTranslator,exports.createFormatter=e.createFormatter,exports.createIntl=e.createIntl,exports.initializeConfig=r.initializeConfig,exports.createTranslator=function(t){let{getMessageFallback:a=r.defaultGetMessageFallback,messages:s,namespace:o,onError:n=r.defaultOnError,...l}=t;return function(r,t){let{getMessageFallback:a,messages:s,namespace:o,onError:n,...l}=r;s=s[t],o=e.resolveNamespace(o,t);const c=e.createBaseTranslator({...l,onError:n,getMessageFallback:a,messages:s,namespace:o}),i=c.rich;function g(){return c(...arguments)}return g.rich=(r,t,s)=>{const l=i(r,t,s);if("string"!=typeof l){const t=new e.IntlError(e.IntlErrorCode.FORMATTING_ERROR,void 0);return n(t),a({error:t,key:r,namespace:o})}return l},g.raw=c.raw,g}({...l,onError:n,getMessageFallback:a,messages:{"!":s},namespace:o?"!.".concat(o):"!"},"!")};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-7a8c6f36.js"),r=require("./initializeConfig-984a566d.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createFormatter=e.createFormatter,exports.initializeConfig=r.initializeConfig,exports.createTranslator=function(a){let{getMessageFallback:t=r.defaultGetMessageFallback,messages:s,namespace:o,onError:n=r.defaultOnError,...l}=a;return function(r,a){let{getMessageFallback:t,messages:s,namespace:o,onError:n,...l}=r;return s=s[a],o=e.resolveNamespace(o,a),e.createBaseTranslator({...l,onError:n,getMessageFallback:t,messages:s,namespace:o})}({...l,onError:n,getMessageFallback:t,messages:{"!":s},namespace:o?"!.".concat(o):"!"},"!")};
@@ -0,0 +1 @@
1
+ "use strict";var e=require("intl-messageformat"),t=require("react"),r=require("./initializeConfig-984a566d.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||null===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:String(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,r){if(!e)throw new Error(void 0);let n=e;return t.split(".").forEach((e=>{const t=n[e];if(null==e||null==t)throw new Error(void 0);n=t})),n}const l=60,f=60*l,m=24*f,E=7*m,d=m*(365/12),I=3*d,S=365*m,g={second:1,seconds:1,minute:l,minutes:l,hour:f,hours:f,day:m,days:m,week:E,weeks:E,month:d,months:d,quarter:I,quarters:I,year:S,years:S};exports.IntlError=s,exports.IntlErrorCode=a,exports.createBaseTranslator=function(e){const n=function(e){let{messages:t,namespace:n,onError:o=r.defaultOnError}=e;try{if(!t)throw new Error(void 0);const e=n?c(t,n):t;if(!e)throw new Error(void 0);return e}catch(e){const t=new s(a.MISSING_MESSAGE,e.message);return o(t),t}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:l=r.defaultGetMessageFallback,locale:f,messageFormatCache:m,messagesOrError:E,namespace:d,onError:I,timeZone:S}=e;function g(e,t,r){const n=new s(t,r);return I(n),l({error:n,key:e,namespace:d})}function N(e,r,I){if(E instanceof s)return l({error:E,key:e,namespace:d});const N=E;let y;try{y=c(N,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}function A(e){return e.filter((e=>null!=e)).join(".")}const v=A([f,d,e,String(y)]);let T;if(null!=m&&m.has(v))T=m.get(v);else{if("object"==typeof y){let t,r;return t=Array.isArray(y)?a.INVALID_MESSAGE:a.INSUFFICIENT_PATH,g(e,t,r)}const t=function(e,t){if(t)return;const r=e.replace(/'([{}])/gi,"$1");return/<|{/.test(r)?void 0:r}(y,r);if(t)return t;try{T=new o.default(y,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,...I},S))}catch(t){return g(e,a.INVALID_MESSAGE,t.message)}null==m||m.set(v,T)}try{const e=T.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,...r}));if(null==e)throw new Error(void 0);return t.isValidElement(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(t){return g(e,a.FORMATTING_ERROR,t.message)}}function y(e,t,r){const n=N(e,t,r);return"string"!=typeof n?g(e,a.INVALID_MESSAGE,void 0):n}return y.rich=N,y.markup=(e,t,r)=>{const n=N(e,t,r);if("string"!=typeof n){const t=new s(a.FORMATTING_ERROR,void 0);return I(t),l({error:t,key:e,namespace:d})}return n},y.raw=e=>{if(E instanceof s)return l({error:E,key:e,namespace:d});const t=E;try{return c(t,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}},y}({...e,messagesOrError:n})},exports.createFormatter=function(e){let{formats:t,locale:n,now:o,onError:i=r.defaultOnError,timeZone:u}=e;function c(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}(r,t)}catch(t){return String(e)}try{return n(o)}catch(t){return i(new s(a.FORMATTING_ERROR,t.message)),String(e)}}function I(e){return e instanceof Date||"number"==typeof e?new Date(e):void 0!==(null==e?void 0:e.now)?new Date(e.now):o||(i(new s(a.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:function(e,r){return c(e,r,null==t?void 0:t.dateTime,(t=>{var r;return null!==(r=t)&&void 0!==r&&r.timeZone||(u?t={...t,timeZone:u}:i(new s(a.ENVIRONMENT_FALLBACK,void 0))),new Intl.DateTimeFormat(n,t).format(e)}))},number:function(e,r){return c(e,r,null==t?void 0:t.number,(t=>new Intl.NumberFormat(n,t).format(e)))},relativeTime:function(e,t){try{const r=new Date(e),o=I(t),i=(r.getTime()-o.getTime())/1e3,a="number"==typeof t||t instanceof Date||void 0===(null==t?void 0:t.unit)?function(e){const t=Math.abs(e);return t<l?"second":t<f?"minute":t<m?"hour":t<E?"day":t<d?"week":t<S?"month":"year"}(i):t.unit,s=function(e,t){return Math.round(e/g[t])}(i,a);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(s,a)}catch(t){return i(new s(a.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){return c(e,r,null==t?void 0:t.list,(t=>new Intl.ListFormat(n,t).format(e)))}}},exports.resolveNamespace=function(e,t){return e===t?void 0:e.slice((t+".").length)};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createIntl-e408cd5c.js"),r=require("./core.js"),t=require("./initializeConfig-984a566d.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),a=require("./_useLocale-8e23751a.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createBaseTranslator=e.createBaseTranslator,exports.createFormatter=e.createFormatter,exports.createIntl=e.createIntl,exports.createTranslator=r.createTranslator,exports.initializeConfig=t.initializeConfig,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useIntl=o.useIntl,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=a.useLocale;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-7a8c6f36.js"),r=require("./core.js"),t=require("./initializeConfig-984a566d.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),i=require("./_useLocale-8e23751a.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createFormatter=e.createFormatter,exports.createTranslator=r.createTranslator,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=i.useLocale;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-8e23751a.js"),r=require("react"),o=require("./createIntl-e408cd5c.js");function n(){return new Date}require("./initializeConfig-984a566d.js"),require("./IntlContext-381f3ce4.js"),require("intl-messageformat"),exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useIntl=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createIntl({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useMessages=function(){return t.useIntlContext().messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:s}=t.useIntlContext(),[a,u]=r.useState(s||n());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{u(n())}),o);return()=>{clearInterval(e)}}),[s,o]),a},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,n,s){const{defaultTranslationValues:a,formats:u,getMessageFallback:l,locale:c,messageFormatCache:i,onError:m,timeZone:f}=t.useIntlContext();return e=e[s],n=o.resolveNamespace(n,s),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:i,getMessageFallback:l,messages:e,defaultTranslationValues:a,namespace:n,onError:m,formats:u,locale:c,timeZone:f})),[i,l,e,n,m,a,u,c,f])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-8e23751a.js"),r=require("react"),o=require("./createFormatter-7a8c6f36.js");require("./initializeConfig-984a566d.js"),require("./IntlContext-381f3ce4.js"),require("intl-messageformat");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,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},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]),s},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,a,u){const{defaultTranslationValues:l,formats:i,getMessageFallback:c,locale:m,messageFormatCache: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({messageFormatCache:f,getMessageFallback:c,messages:e,defaultTranslationValues:l,namespace:a,onError:I,formats:i,locale:m,timeZone:d})),[f,c,e,a,I,l,i,m,d])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
@@ -29,7 +29,7 @@ type IntlConfig<Messages = AbstractIntlMessages> = {
29
29
  /**
30
30
  * Providing this value will have two effects:
31
31
  * 1. It will be used as the default for the `now` argument of
32
- * `useIntl().formatRelativeTime` if no explicit value is provided.
32
+ * `useFormatter().formatRelativeTime` if no explicit value is provided.
33
33
  * 2. It will be returned as a static value from the `useNow` hook. Note
34
34
  * however that when `updateInterval` is configured on the `useNow` hook,
35
35
  * the global `now` value will only be used for the initial render, but
@@ -1,6 +1,7 @@
1
1
  export declare enum IntlErrorCode {
2
2
  MISSING_MESSAGE = "MISSING_MESSAGE",
3
3
  MISSING_FORMAT = "MISSING_FORMAT",
4
+ ENVIRONMENT_FALLBACK = "ENVIRONMENT_FALLBACK",
4
5
  INSUFFICIENT_PATH = "INSUFFICIENT_PATH",
5
6
  INVALID_MESSAGE = "INVALID_MESSAGE",
6
7
  INVALID_KEY = "INVALID_KEY",
@@ -0,0 +1,5 @@
1
+ type RelativeTimeFormatOptions = {
2
+ now?: number | Date;
3
+ unit?: Intl.RelativeTimeFormatUnit;
4
+ };
5
+ export default RelativeTimeFormatOptions;
@@ -2,5 +2,5 @@ import { ReactNode } from 'react';
2
2
  export type TranslationValue = string | number | boolean | Date | null | undefined;
3
3
  type TranslationValues = Record<string, TranslationValue>;
4
4
  export type RichTranslationValues = Record<string, TranslationValue | ((chunks: ReactNode) => ReactNode)>;
5
- export type RichTranslationValuesPlain = Record<string, TranslationValue | ((chunks: string) => string)>;
5
+ export type MarkupTranslationValues = Record<string, TranslationValue | ((chunks: string) => string)>;
6
6
  export default TranslationValues;
@@ -4,7 +4,7 @@ import Formats from './Formats';
4
4
  import { InitializedIntlConfig } from './IntlConfig';
5
5
  import IntlError from './IntlError';
6
6
  import MessageFormatCache from './MessageFormatCache';
7
- import TranslationValues, { RichTranslationValues } from './TranslationValues';
7
+ import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from './TranslationValues';
8
8
  import MessageKeys from './utils/MessageKeys';
9
9
  import NestedKeyOf from './utils/NestedKeyOf';
10
10
  import NestedValueOf from './utils/NestedValueOf';
@@ -17,5 +17,6 @@ export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
17
17
  export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(config: Omit<CreateBaseTranslatorProps<Messages>, 'messagesOrError'>): {
18
18
  <TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues | undefined, formats?: Partial<Formats> | undefined): string;
19
19
  rich: (key: string, values?: RichTranslationValues | undefined, formats?: Partial<Formats> | undefined) => string | ReactElement<any, string | import("react").JSXElementConstructor<any>> | ReactNodeArray;
20
+ markup(key: string, values: MarkupTranslationValues, formats?: Partial<Formats> | undefined): string;
20
21
  raw(key: string): any;
21
22
  };
@@ -2,6 +2,7 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
2
2
  import Formats from './Formats';
3
3
  import IntlError from './IntlError';
4
4
  import NumberFormatOptions from './NumberFormatOptions';
5
+ import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
5
6
  import TimeZone from './TimeZone';
6
7
  type Props = {
7
8
  locale: string;
@@ -10,10 +11,10 @@ type Props = {
10
11
  formats?: Partial<Formats>;
11
12
  now?: Date;
12
13
  };
13
- export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone }: Props): {
14
+ export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
14
15
  dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
15
16
  number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
16
- relativeTime: (date: number | Date, now?: number | Date) => string;
17
+ relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions) => string;
17
18
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions) => string;
18
19
  };
19
20
  export {};
@@ -1,6 +1,8 @@
1
+ import { ReactElement, ReactNodeArray } from 'react';
1
2
  import Formats from './Formats';
2
3
  import IntlConfig from './IntlConfig';
3
- import TranslationValues, { RichTranslationValuesPlain } from './TranslationValues';
4
+ import MessageFormatCache from './MessageFormatCache';
5
+ import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from './TranslationValues';
4
6
  import MessageKeys from './utils/MessageKeys';
5
7
  import NamespaceKeys from './utils/NamespaceKeys';
6
8
  import NestedKeyOf from './utils/NestedKeyOf';
@@ -14,8 +16,10 @@ import NestedValueOf from './utils/NestedValueOf';
14
16
  * (e.g. `namespace.Component`).
15
17
  */
16
18
  export default function createTranslator<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>({ getMessageFallback, messages, namespace, onError, ...rest }: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {
17
- messages: NonNullable<IntlConfig<IntlMessages>['messages']>;
19
+ messages: IntlConfig<IntlMessages>['messages'];
18
20
  namespace?: NestedKey;
21
+ /** @private */
22
+ messageFormatCache?: MessageFormatCache;
19
23
  }): {
20
24
  <TargetKey extends MessageKeys<NestedValueOf<{
21
25
  '!': IntlMessages;
@@ -34,7 +38,16 @@ export default function createTranslator<NestedKey extends NamespaceKeys<IntlMes
34
38
  '!': IntlMessages;
35
39
  }, [
36
40
  NestedKey
37
- ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValuesPlain, formats?: Partial<Formats>): string;
41
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValues, formats?: Partial<Formats>): string | ReactElement | ReactNodeArray;
42
+ markup<TargetKey extends MessageKeys<NestedValueOf<{
43
+ '!': IntlMessages;
44
+ }, [
45
+ NestedKey
46
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
47
+ '!': IntlMessages;
48
+ }, [
49
+ NestedKey
50
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: MarkupTranslationValues, formats?: Partial<Formats>): string;
38
51
  raw<TargetKey extends MessageKeys<NestedValueOf<{
39
52
  '!': IntlMessages;
40
53
  }, [
@@ -1,13 +1,16 @@
1
+ /// <reference types="react" />
1
2
  import AbstractIntlMessages from './AbstractIntlMessages';
2
3
  import { InitializedIntlConfig } from './IntlConfig';
3
- import { RichTranslationValuesPlain } from './TranslationValues';
4
+ import MessageFormatCache from './MessageFormatCache';
4
5
  import NestedKeyOf from './utils/NestedKeyOf';
5
6
  export type CreateTranslatorImplProps<Messages> = Omit<InitializedIntlConfig, 'messages'> & {
6
7
  namespace: string;
7
8
  messages: Messages;
9
+ messageFormatCache?: MessageFormatCache;
8
10
  };
9
11
  export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ getMessageFallback, messages, namespace, onError, ...rest }: CreateTranslatorImplProps<Messages>, namespacePrefix: string): {
10
- (key: import("./utils/MessageKeys").default<import("./utils/NestedValueOf").default<Messages, NestedKey>, NestedKeyOf<import("./utils/NestedValueOf").default<Messages, NestedKey>>>, values?: import("./TranslationValues").default | undefined, formats?: Partial<import("./Formats").default> | undefined): string;
11
- rich(key: string, values: RichTranslationValuesPlain, formats?: Partial<import("./Formats").default> | undefined): string;
12
- raw: (key: string) => any;
12
+ <TargetKey extends import("./utils/MessageKeys").default<import("./utils/NestedValueOf").default<Messages, NestedKey>, NestedKeyOf<import("./utils/NestedValueOf").default<Messages, NestedKey>>>>(key: TargetKey, values?: import("./TranslationValues").default | undefined, formats?: Partial<import("./Formats").default> | undefined): string;
13
+ rich: (key: string, values?: import("./TranslationValues").RichTranslationValues | undefined, formats?: Partial<import("./Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
14
+ markup(key: string, values: import("./TranslationValues").MarkupTranslationValues, formats?: Partial<import("./Formats").default> | undefined): string;
15
+ raw(key: string): any;
13
16
  };
@@ -1,16 +1,14 @@
1
1
  export type { default as AbstractIntlMessages } from './AbstractIntlMessages';
2
- export type { default as TranslationValues, RichTranslationValues, RichTranslationValuesPlain } from './TranslationValues';
2
+ export type { default as TranslationValues, RichTranslationValues, MarkupTranslationValues } from './TranslationValues';
3
3
  export type { default as Formats } from './Formats';
4
4
  export type { default as IntlConfig } from './IntlConfig';
5
5
  export type { default as DateTimeFormatOptions } from './DateTimeFormatOptions';
6
6
  export type { default as NumberFormatOptions } from './NumberFormatOptions';
7
7
  export { default as IntlError, IntlErrorCode } from './IntlError';
8
8
  export { default as createTranslator } from './createTranslator';
9
- export { default as createBaseTranslator } from './createBaseTranslator';
10
9
  export { default as createFormatter } from './createFormatter';
11
10
  export { default as initializeConfig } from './initializeConfig';
12
11
  export type { default as MessageKeys } from './utils/MessageKeys';
13
12
  export type { default as NamespaceKeys } from './utils/NamespaceKeys';
14
13
  export type { default as NestedKeyOf } from './utils/NestedKeyOf';
15
14
  export type { default as NestedValueOf } from './utils/NestedValueOf';
16
- export { default as createIntl } from './createIntl';
@@ -1,7 +1,7 @@
1
- import { ReactNode } from 'react';
1
+ import React, { ReactNode } from 'react';
2
2
  import IntlConfig from '../core/IntlConfig';
3
3
  type Props = IntlConfig & {
4
4
  children: ReactNode;
5
5
  };
6
- export default function IntlProvider({ children, ...config }: Props): JSX.Element;
6
+ export default function IntlProvider({ children, ...config }: Props): React.JSX.Element;
7
7
  export {};
@@ -5,4 +5,3 @@ export { default as useNow } from './useNow';
5
5
  export { default as useTimeZone } from './useTimeZone';
6
6
  export { default as useMessages } from './useMessages';
7
7
  export { default as useFormatter } from './useFormatter';
8
- export { default as useIntl } from './useIntl';
@@ -1,6 +1,6 @@
1
1
  export default function useFormatter(): {
2
2
  dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
3
3
  number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
4
- relativeTime: (date: number | Date, now?: number | Date | undefined) => string;
4
+ relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
5
5
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
6
6
  };
@@ -1 +1,2 @@
1
- export default function useMessages(): import("../core/AbstractIntlMessages").default | undefined;
1
+ import { AbstractIntlMessages } from '../core';
2
+ export default function useMessages(): AbstractIntlMessages;
@@ -1,6 +1,6 @@
1
1
  import { ReactElement, ReactNodeArray } from 'react';
2
2
  import Formats from '../core/Formats';
3
- import TranslationValues, { RichTranslationValues } from '../core/TranslationValues';
3
+ import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from '../core/TranslationValues';
4
4
  import MessageKeys from '../core/utils/MessageKeys';
5
5
  import NamespaceKeys from '../core/utils/NamespaceKeys';
6
6
  import NestedKeyOf from '../core/utils/NestedKeyOf';
@@ -32,6 +32,15 @@ export default function useTranslations<NestedKey extends NamespaceKeys<IntlMess
32
32
  }, [
33
33
  NestedKey
34
34
  ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValues, formats?: Partial<Formats>): string | ReactElement | ReactNodeArray;
35
+ markup<TargetKey extends MessageKeys<NestedValueOf<{
36
+ '!': IntlMessages;
37
+ }, [
38
+ NestedKey
39
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
40
+ '!': IntlMessages;
41
+ }, [
42
+ NestedKey
43
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: MarkupTranslationValues, formats?: Partial<Formats>): string;
35
44
  raw<TargetKey extends MessageKeys<NestedValueOf<{
36
45
  '!': IntlMessages;
37
46
  }, [
@@ -3,6 +3,7 @@ import AbstractIntlMessages from '../core/AbstractIntlMessages';
3
3
  import NestedKeyOf from '../core/utils/NestedKeyOf';
4
4
  export default function useTranslationsImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string): {
5
5
  <TargetKey extends unknown>(key: TargetKey, values?: import("../core/TranslationValues").default | undefined, formats?: Partial<import("../core/Formats").default> | undefined): string;
6
- rich: (key: string, values?: import("..").RichTranslationValues | undefined, formats?: Partial<import("../core/Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
6
+ rich: (key: string, values?: import("../core").RichTranslationValues | undefined, formats?: Partial<import("../core/Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
7
+ markup(key: string, values: import("../core").MarkupTranslationValues, formats?: Partial<import("../core/Formats").default> | undefined): string;
7
8
  raw(key: string): any;
8
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-intl",
3
- "version": "3.0.0-beta.3",
3
+ "version": "3.0.0-rc.6",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.work>",
6
6
  "description": "Minimal, but complete solution for managing internationalization in React apps.",
@@ -64,10 +64,11 @@
64
64
  "@size-limit/preset-big-lib": "^8.2.6",
65
65
  "@testing-library/react": "^13.0.0",
66
66
  "@types/node": "^17.0.23",
67
- "@types/react": "^18.2.5",
67
+ "@types/react": "^18.2.29",
68
+ "@types/react-dom": "^18.2.5",
68
69
  "date-fns": "^2.16.1",
69
70
  "eslint": "^8.46.0",
70
- "eslint-config-molindo": "7.0.0-alpha.7",
71
+ "eslint-config-molindo": "^7.0.0",
71
72
  "react": "^18.2.0",
72
73
  "react-dom": "^18.2.0",
73
74
  "rollup": "^3.28.1",
@@ -78,7 +79,7 @@
78
79
  "size-limit": [
79
80
  {
80
81
  "path": "dist/production/index.js",
81
- "limit": "12.25 kB"
82
+ "limit": "12.3 kB"
82
83
  }
83
84
  ],
84
85
  "scripts": {
@@ -1 +0,0 @@
1
- "use strict";var e=require("intl-messageformat"),r=require("react"),t=require("./initializeConfig-c316f266.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function a(e,r){return e?Object.keys(e).reduce(((t,n)=>(t[n]={timeZone:r,...e[n]},t)),{}):e}function s(e,r,t){if(!e)throw new Error("No messages available at `".concat(t,"`."));let n=e;return r.split(".").forEach((e=>{const o=n[e];if(null==e||null==o)throw new Error("Could not resolve `".concat(r,"` in ").concat(t?"`".concat(t,"`"):"messages","."));n=o})),n}const c=60,i=60*c,u=24*i,l=7*u,m=u*(365/12),f=365*u;function d(e){let{formats:r,locale:n,now:o,onError:a=t.defaultOnError,timeZone:s}=e;function d(e,r,n,o){let s;try{s=function(e,r){let n;if("string"==typeof r){const o=r;if(n=null==e?void 0:e[o],!n){const e=new t.IntlError(t.IntlErrorCode.MISSING_FORMAT,"Format `".concat(o,"` is not available. You can configure it on the provider or provide custom options."));throw a(e),e}}else n=r;return n}(n,r)}catch(r){return String(e)}try{return o(s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}}return{dateTime:function(e,t){return d(e,t,null==r?void 0:r.dateTime,(r=>{var t;return!s||null!==(t=r)&&void 0!==t&&t.timeZone||(r={...r,timeZone:s}),new Intl.DateTimeFormat(n,r).format(e)}))},number:function(e,t){return d(e,t,null==r?void 0:r.number,(r=>new Intl.NumberFormat(n,r).format(e)))},relativeTime:function(e,r){try{if(!r){if(!o)throw new Error("The `now` parameter wasn't provided and there was no global fallback configured on the provider.");r=o}const t=e instanceof Date?e:new Date(e),a=r instanceof Date?r:new Date(r),s=(t.getTime()-a.getTime())/1e3,{unit:d,value:g}=function(e){const r=Math.abs(e);let t,n;return r<c?(n="second",t=Math.round(e)):r<i?(n="minute",t=Math.round(e/c)):r<u?(n="hour",t=Math.round(e/i)):r<l?(n="day",t=Math.round(e/u)):r<m?(n="week",t=Math.round(e/l)):r<f?(n="month",t=Math.round(e/m)):(n="year",t=Math.round(e/f)),{value:t,unit:n}}(s);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(g,d)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}},list:function(e,t){return d(e,t,null==r?void 0:r.list,(r=>new Intl.ListFormat(n,r).format(e)))}}}exports.createBaseTranslator=function(e){const n=function(e){let{messages:r,namespace:n,onError:o=t.defaultOnError}=e;try{if(!r)throw new Error("No messages were configured on the provider.");const e=n?s(r,n):r;if(!e)throw new Error("No messages for namespace `".concat(n,"` found."));return e}catch(e){const r=new t.IntlError(t.IntlErrorCode.MISSING_MESSAGE,e.message);return o(r),r}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:c,getMessageFallback:i=t.defaultGetMessageFallback,locale:u,messageFormatCache:l,messagesOrError:m,namespace:f,onError:d,timeZone:g}=e;function E(e,r,n){const o=new t.IntlError(r,n);return d(o),i({error:o,key:e,namespace:f})}function I(e,d,I){if(m instanceof t.IntlError)return i({error:m,key:e,namespace:f});const h=m;let p;try{p=s(h,e,f)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}function v(e){return e.filter((e=>null!=e)).join(".")}const w=v([u,f,e,String(p)]);let y;if(null!=l&&l.has(w))y=l.get(w);else{if("object"==typeof p){let r,n;return Array.isArray(p)?(r=t.IntlErrorCode.INVALID_MESSAGE,n="Message at `".concat(v([f,e]),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(r=t.IntlErrorCode.INSUFFICIENT_PATH,n="Message at `".concat(v([f,e]),"` 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")),E(e,r,n)}const r=function(e,r){if(r)return;const t=e.replace(/'([{}])/gi,"$1");return/<|{/.test(t)?void 0:t}(p,d);if(r)return r;try{y=new o.default(p,u,function(e,r){const t=r?{...e,dateTime:a(e.dateTime,r)}:e,n=o.default.formats.date,s=r?a(n,r):n,c=o.default.formats.time,i=r?a(c,r):c;return{...t,date:{...s,...null==t?void 0:t.dateTime},time:{...i,...null==t?void 0:t.dateTime}}}({...c,...I},g))}catch(r){return E(e,t.IntlErrorCode.INVALID_MESSAGE,r.message)}null==l||l.set(w,y)}try{const t=y.format(function(e){if(0===Object.keys(e).length)return;const t={};return Object.keys(e).forEach((n=>{let o=0;const a=e[n];let s;s="function"==typeof a?e=>{const t=a(e);return r.isValidElement(t)?r.cloneElement(t,{key:n+o++}):t}:a,t[n]=s})),t}({...n,...d}));if(null==t)throw new Error("Unable to format `".concat(e,"` in ").concat(f?"namespace `".concat(f,"`"):"messages"));return r.isValidElement(t)||Array.isArray(t)||"string"==typeof t?t:String(t)}catch(r){return E(e,t.IntlErrorCode.FORMATTING_ERROR,r.message)}}function h(e,r,n){const o=I(e,r,n);return"string"!=typeof o?E(e,t.IntlErrorCode.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(f?"namespace `".concat(f,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):o}return h.rich=I,h.raw=e=>{if(m instanceof t.IntlError)return i({error:m,key:e,namespace:f});const r=m;try{return s(r,e,f)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}},h}({...e,messagesOrError:n})},exports.createFormatter=d,exports.createIntl=function(){const e=d(...arguments);return{formatDateTime:e.dateTime,formatNumber:e.number,formatRelativeTime:e.relativeTime}},exports.resolveNamespace=function(e,r){return e===r?void 0:e.slice((r+".").length)};
@@ -1 +0,0 @@
1
- "use strict";function e(e,n,t){return(n=function(e){var n=function(e,n){if("object"!=typeof e||null===e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,n||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(e)}(e,"string");return"symbol"==typeof n?n:String(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.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 r=n;t&&(r+=": "+t),super(r),e(this,"code",void 0),e(this,"originalMessage",void 0),this.code=n,t&&(this.originalMessage=t)}}function r(e){return[e.namespace,e.key].filter((e=>null!=e)).join(".")}function o(e){console.error(e)}function i(e,n,t){Object.entries(e).forEach((e=>{let[r,o]=e;if(r.includes(".")){let e=r;t&&(e+=" (at ".concat(t,")")),n.push(e)}null!=o&&"object"==typeof o&&i(o,n,[t,r].filter((e=>null!=e)).join("."))}))}function s(e,r){const o=[];i(e,o),o.length>0&&r(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===o.length?"key":"keys",": ").concat(o.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')))}exports.IntlError=t,exports.IntlErrorCode=n,exports.defaultGetMessageFallback=r,exports.defaultOnError=o,exports.initializeConfig=function(e){let{getMessageFallback:n,messages:t,onError:i,...a}=e;const c=i||o,l=n||r;return t&&s(t,c),{...a,messages:t,onError:c,getMessageFallback:l}};
@@ -1 +0,0 @@
1
- "use strict";var e=require("intl-messageformat"),t=require("react"),r=require("./initializeConfig-984a566d.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||null===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:String(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.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class u 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 s(e,t){return e?Object.keys(e).reduce(((r,n)=>(r[n]={timeZone:t,...e[n]},r)),{}):e}function c(e,t,r){if(!e)throw new Error(void 0);let n=e;return t.split(".").forEach((e=>{const t=n[e];if(null==e||null==t)throw new Error(void 0);n=t})),n}const l=60,f=60*l,m=24*f,d=7*m,E=m*(365/12),I=365*m;function S(e){let{formats:t,locale:n,now:o,onError:i=r.defaultOnError,timeZone:s}=e;function c(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 u(a.MISSING_FORMAT,void 0);throw i(e),e}}else r=t;return r}(r,t)}catch(t){return String(e)}try{return n(o)}catch(t){return i(new u(a.FORMATTING_ERROR,t.message)),String(e)}}return{dateTime:function(e,r){return c(e,r,null==t?void 0:t.dateTime,(t=>{var r;return!s||null!==(r=t)&&void 0!==r&&r.timeZone||(t={...t,timeZone:s}),new Intl.DateTimeFormat(n,t).format(e)}))},number:function(e,r){return c(e,r,null==t?void 0:t.number,(t=>new Intl.NumberFormat(n,t).format(e)))},relativeTime:function(e,t){try{if(!t){if(!o)throw new Error(void 0);t=o}const r=e instanceof Date?e:new Date(e),i=t instanceof Date?t:new Date(t),a=(r.getTime()-i.getTime())/1e3,{unit:u,value:s}=function(e){const t=Math.abs(e);let r,n;return t<l?(n="second",r=Math.round(e)):t<f?(n="minute",r=Math.round(e/l)):t<m?(n="hour",r=Math.round(e/f)):t<d?(n="day",r=Math.round(e/m)):t<E?(n="week",r=Math.round(e/d)):t<I?(n="month",r=Math.round(e/E)):(n="year",r=Math.round(e/I)),{value:r,unit:n}}(a);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(s,u)}catch(t){return i(new u(a.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){return c(e,r,null==t?void 0:t.list,(t=>new Intl.ListFormat(n,t).format(e)))}}}exports.IntlError=u,exports.IntlErrorCode=a,exports.createBaseTranslator=function(e){const n=function(e){let{messages:t,namespace:n,onError:o=r.defaultOnError}=e;try{if(!t)throw new Error(void 0);const e=n?c(t,n):t;if(!e)throw new Error(void 0);return e}catch(e){const t=new u(a.MISSING_MESSAGE,e.message);return o(t),t}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:l=r.defaultGetMessageFallback,locale:f,messageFormatCache:m,messagesOrError:d,namespace:E,onError:I,timeZone:S}=e;function g(e,t,r){const n=new u(t,r);return I(n),l({error:n,key:e,namespace:E})}function M(e,r,I){if(d instanceof u)return l({error:d,key:e,namespace:E});const M=d;let v;try{v=c(M,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}function h(e){return e.filter((e=>null!=e)).join(".")}const T=h([f,E,e,String(v)]);let y;if(null!=m&&m.has(T))y=m.get(T);else{if("object"==typeof v){let t,r;return t=Array.isArray(v)?a.INVALID_MESSAGE:a.INSUFFICIENT_PATH,g(e,t,r)}const t=function(e,t){if(t)return;const r=e.replace(/'([{}])/gi,"$1");return/<|{/.test(r)?void 0:r}(v,r);if(t)return t;try{y=new o.default(v,f,function(e,t){const r=t?{...e,dateTime:s(e.dateTime,t)}:e,n=o.default.formats.date,i=t?s(n,t):n,a=o.default.formats.time,u=t?s(a,t):a;return{...r,date:{...i,...null==r?void 0:r.dateTime},time:{...u,...null==r?void 0:r.dateTime}}}({...i,...I},S))}catch(t){return g(e,a.INVALID_MESSAGE,t.message)}null==m||m.set(T,y)}try{const e=y.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,...r}));if(null==e)throw new Error(void 0);return t.isValidElement(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(t){return g(e,a.FORMATTING_ERROR,t.message)}}function v(e,t,r){const n=M(e,t,r);return"string"!=typeof n?g(e,a.INVALID_MESSAGE,void 0):n}return v.rich=M,v.raw=e=>{if(d instanceof u)return l({error:d,key:e,namespace:E});const t=d;try{return c(t,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}},v}({...e,messagesOrError:n})},exports.createFormatter=S,exports.createIntl=function(){const e=S(...arguments);return{formatDateTime:e.dateTime,formatNumber:e.number,formatRelativeTime:e.relativeTime}},exports.resolveNamespace=function(e,t){return e===t?void 0:e.slice((t+".").length)};
@@ -1,7 +0,0 @@
1
- import createFormatter from './createFormatter';
2
- /** @deprecated Switch to `createFormatter` */
3
- export default function createIntl(...args: Parameters<typeof createFormatter>): {
4
- formatDateTime: (value: number | Date, formatOrOptions?: string | import("./DateTimeFormatOptions").default | undefined) => string;
5
- formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
6
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
7
- };
@@ -1 +0,0 @@
1
- export { default } from '../core/initializeConfig';
@@ -1,6 +0,0 @@
1
- /** @deprecated Switch to `useFormatter` instead. */
2
- export default function useIntl(): {
3
- formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
4
- formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
5
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
6
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};