use-intl 2.14.0 → 2.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/core/use-intl.esm.js +34 -30
  2. package/dist/core/use-intl.esm.js.map +1 -1
  3. package/dist/core/use-intl.esm2.js +126 -31
  4. package/dist/core/use-intl.esm3.js +8 -126
  5. package/dist/core/use-intl.esm4.js +30 -11
  6. package/dist/core/use-intl.esm4.js.map +1 -1
  7. package/dist/core/use-intl.esm5.js +12 -50
  8. package/dist/core/use-intl.esm6.js +50 -12
  9. package/dist/core/use-intl.esm7.js +20 -183
  10. package/dist/core/use-intl.esm7.js.map +1 -1
  11. package/dist/core/use-intl.esm8.js +188 -7
  12. package/dist/core/use-intl.esm8.js.map +1 -1
  13. package/dist/core/use-intl.esm9.js +7 -25
  14. package/dist/core/use-intl.esm9.js.map +1 -1
  15. package/dist/react/use-intl.esm.js +1 -1
  16. package/dist/react/use-intl.esm.js.map +1 -1
  17. package/dist/react/use-intl.esm10.js +24 -8
  18. package/dist/react/use-intl.esm10.js.map +1 -1
  19. package/dist/react/use-intl.esm11.js +3 -3
  20. package/dist/react/use-intl.esm11.js.map +1 -1
  21. package/dist/react/use-intl.esm2.js +20 -22
  22. package/dist/react/use-intl.esm2.js.map +1 -1
  23. package/dist/react/use-intl.esm3.js +1 -1
  24. package/dist/react/use-intl.esm3.js.map +1 -1
  25. package/dist/react/use-intl.esm4.js +19 -38
  26. package/dist/react/use-intl.esm4.js.map +1 -1
  27. package/dist/react/use-intl.esm5.js +41 -4
  28. package/dist/react/use-intl.esm5.js.map +1 -1
  29. package/dist/react/use-intl.esm6.js +4 -20
  30. package/dist/react/use-intl.esm7.js +2 -2
  31. package/dist/react/use-intl.esm7.js.map +1 -1
  32. package/dist/react/use-intl.esm8.js.map +1 -1
  33. package/dist/react/use-intl.esm9.js +8 -24
  34. package/dist/use-intl.esm.js +8 -8
  35. package/package.json +1 -1
@@ -1,33 +1,37 @@
1
- import { inheritsLoose as _inheritsLoose, wrapNativeSuper as _wrapNativeSuper } from '../_virtual/use-intl.esm.js';
1
+ import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
+ import createTranslatorImpl from './use-intl.esm6.js';
3
+ import { defaultGetMessageFallback, defaultOnError } from './use-intl.esm5.js';
2
4
 
3
- var IntlErrorCode;
4
- (function (IntlErrorCode) {
5
- IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
6
- IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
7
- IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
8
- IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
9
- IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
10
- IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
11
- })(IntlErrorCode || (IntlErrorCode = {}));
12
- var IntlError = /*#__PURE__*/function (_Error) {
13
- _inheritsLoose(IntlError, _Error);
14
- function IntlError(code, originalMessage) {
15
- var _this;
16
- var message = code;
17
- if (originalMessage) {
18
- message += ': ' + originalMessage;
19
- }
20
- _this = _Error.call(this, message) || this;
21
- _this.code = void 0;
22
- _this.originalMessage = void 0;
23
- _this.code = code;
24
- if (originalMessage) {
25
- _this.originalMessage = originalMessage;
26
- }
27
- return _this;
28
- }
29
- return IntlError;
30
- }( /*#__PURE__*/_wrapNativeSuper(Error));
5
+ var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
6
+ /**
7
+ * Translates messages from the given namespace by using the ICU syntax.
8
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
9
+ *
10
+ * If no namespace is provided, all available messages are returned.
11
+ * The namespace can also indicate nesting by using a dot
12
+ * (e.g. `namespace.Component`).
13
+ */
14
+ function createTranslator(_ref) {
15
+ var _ref$getMessageFallba = _ref.getMessageFallback,
16
+ getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
17
+ messages = _ref.messages,
18
+ namespace = _ref.namespace,
19
+ _ref$onError = _ref.onError,
20
+ onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
21
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
22
+ // We have to wrap the actual function so the type inference for the optional
23
+ // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
24
+ // The prefix ("!") is arbitrary.
25
+ return createTranslatorImpl(_extends({}, rest, {
26
+ onError: onError,
27
+ getMessageFallback: getMessageFallback,
28
+ messages: {
29
+ '!': messages
30
+ },
31
+ // @ts-ignore
32
+ namespace: namespace ? "!." + namespace : '!'
33
+ }), '!');
34
+ }
31
35
 
32
- export { IntlErrorCode, IntlError as default };
36
+ export { createTranslator as default };
33
37
  //# sourceMappingURL=use-intl.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm.js","sources":["../../src/core/IntlError.tsx"],"sourcesContent":["export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n INVALID_KEY = 'INVALID_KEY',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n"],"names":["IntlErrorCode","IntlError","_Error","_inheritsLoose","code","originalMessage","_this","message","call","_wrapNativeSuper","Error"],"mappings":";;IAAYA,cAOX;AAPD,CAAA,UAAYA,aAAa,EAAA;AACvBA,EAAAA,aAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC,CAAA;AACnCA,EAAAA,a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from './IntlConfig';\nimport TranslationValues from './TranslationValues';\nimport createTranslatorImpl, {\n CoreRichTranslationValues\n} from './createTranslatorImpl';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NamespaceKeys from './utils/NamespaceKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function createTranslator<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>({\n getMessageFallback = defaultGetMessageFallback,\n messages,\n namespace,\n onError = defaultOnError,\n ...rest\n}: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {\n messages: NonNullable<IntlConfig<IntlMessages>['messages']>;\n namespace?: NestedKey;\n}): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: CoreRichTranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n // We have to wrap the actual function so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return createTranslatorImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {\n ...rest,\n onError,\n getMessageFallback,\n messages: {'!': messages},\n // @ts-ignore\n namespace: namespace ? `!.${namespace}` : '!'\n },\n '!'\n );\n}\n"],"names":["createTranslator","_ref","_ref$getMessageFallba","getMessageFallback","defaultGetMessageFallback","messages","namespace","_ref$onError","onError","defaultOnError","rest","_objectWithoutPropertiesLoose","_excluded","createTranslatorImpl","_extends"],"mappings":";;;;;AAYA;;;;;;;AAOG;AACqB,SAAAA,gBAAgBA,CAAAC,IAAA,EAcvC;AAAA,EAAA,IAAAC,qBAAA,GAAAD,IAAA,CARCE,kBAAkB;AAAlBA,IAAAA,kBAAkB,GAAAD,qBAAA,KAAGE,KAAAA,CAAAA,GAAAA,yBAAyB,GAAAF,qBAAA;IAC9CG,QAAQ,GAAAJ,IAAA,CAARI,QAAQ;IACRC,SAAS,GAAAL,IAAA,CAATK,SAAS;IAAAC,YAAA,GAAAN,IAAA,CACTO,OAAO;AAAPA,IAAAA,OAAO,GAAAD,YAAA,KAAGE,KAAAA,CAAAA,GAAAA,cAAc,GAAAF,YAAA;AACrBG,IAAAA,IAAI,GAAAC,6BAAA,CAAAV,IAAA,EAAAW,SAAA,CAAA,CAAA;AAgEP;AACA;AACA;AACA,EAAA,OAAOC,oBAAoB,CAAAC,QAAA,CAAA,EAAA,EAKpBJ,IAAI,EAAA;AACPF,IAAAA,OAAO,EAAPA,OAAO;AACPL,IAAAA,kBAAkB,EAAlBA,kBAAkB;AAClBE,IAAAA,QAAQ,EAAE;AAAC,MAAA,GAAG,EAAEA,QAAAA;KAAS;AACzB;AACAC,IAAAA,SAAS,EAAEA,SAAS,GAAQA,IAAAA,GAAAA,SAAS,GAAK,GAAA;AAAG,GAAA,CAAA,EAE/C,GAAG,CACJ,CAAA;AACH;;;;"}
@@ -1,37 +1,132 @@
1
- import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import createTranslatorImpl from './use-intl.esm5.js';
3
- import { defaultGetMessageFallback, defaultOnError } from './use-intl.esm6.js';
1
+ import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
+ import IntlError, { IntlErrorCode } from './use-intl.esm4.js';
3
+ import { defaultOnError } from './use-intl.esm5.js';
4
4
 
5
- var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
6
- /**
7
- * Translates messages from the given namespace by using the ICU syntax.
8
- * See https://formatjs.io/docs/core-concepts/icu-syntax.
9
- *
10
- * If no namespace is provided, all available messages are returned.
11
- * The namespace can also indicate nesting by using a dot
12
- * (e.g. `namespace.Component`).
13
- */
14
- function createTranslator(_ref) {
15
- var _ref$getMessageFallba = _ref.getMessageFallback,
16
- getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
17
- messages = _ref.messages,
18
- namespace = _ref.namespace,
5
+ var MINUTE = 60;
6
+ var HOUR = MINUTE * 60;
7
+ var DAY = HOUR * 24;
8
+ var WEEK = DAY * 7;
9
+ var MONTH = DAY * (365 / 12); // Approximation
10
+ var YEAR = DAY * 365;
11
+ function getRelativeTimeFormatConfig(seconds) {
12
+ var absValue = Math.abs(seconds);
13
+ var value, unit;
14
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
15
+ // will include fractions like '2.1 hours ago'.
16
+ if (absValue < MINUTE) {
17
+ unit = 'second';
18
+ value = Math.round(seconds);
19
+ } else if (absValue < HOUR) {
20
+ unit = 'minute';
21
+ value = Math.round(seconds / MINUTE);
22
+ } else if (absValue < DAY) {
23
+ unit = 'hour';
24
+ value = Math.round(seconds / HOUR);
25
+ } else if (absValue < WEEK) {
26
+ unit = 'day';
27
+ value = Math.round(seconds / DAY);
28
+ } else if (absValue < MONTH) {
29
+ unit = 'week';
30
+ value = Math.round(seconds / WEEK);
31
+ } else if (absValue < YEAR) {
32
+ unit = 'month';
33
+ value = Math.round(seconds / MONTH);
34
+ } else {
35
+ unit = 'year';
36
+ value = Math.round(seconds / YEAR);
37
+ }
38
+ return {
39
+ value: value,
40
+ unit: unit
41
+ };
42
+ }
43
+ function createFormatter(_ref) {
44
+ var formats = _ref.formats,
45
+ locale = _ref.locale,
46
+ globalNow = _ref.now,
19
47
  _ref$onError = _ref.onError,
20
48
  onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
21
- rest = _objectWithoutPropertiesLoose(_ref, _excluded);
22
- // We have to wrap the actual function so the type inference for the optional
23
- // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
24
- // The prefix ("!") is arbitrary.
25
- return createTranslatorImpl(_extends({}, rest, {
26
- onError: onError,
27
- getMessageFallback: getMessageFallback,
28
- messages: {
29
- '!': messages
30
- },
31
- // @ts-ignore
32
- namespace: namespace ? "!." + namespace : '!'
33
- }), '!');
49
+ timeZone = _ref.timeZone;
50
+ function resolveFormatOrOptions(typeFormats, formatOrOptions) {
51
+ var options;
52
+ if (typeof formatOrOptions === 'string') {
53
+ var formatName = formatOrOptions;
54
+ options = typeFormats == null ? void 0 : typeFormats[formatName];
55
+ if (!options) {
56
+ var error = new IntlError(IntlErrorCode.MISSING_FORMAT, process.env.NODE_ENV !== 'production' ? "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." : undefined);
57
+ onError(error);
58
+ throw error;
59
+ }
60
+ } else {
61
+ options = formatOrOptions;
62
+ }
63
+ return options;
64
+ }
65
+ function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
66
+ var options;
67
+ try {
68
+ options = resolveFormatOrOptions(typeFormats, formatOrOptions);
69
+ } catch (error) {
70
+ return String(value);
71
+ }
72
+ try {
73
+ return formatter(options);
74
+ } catch (error) {
75
+ onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
76
+ return String(value);
77
+ }
78
+ }
79
+ function dateTime( /** If a number is supplied, this is interpreted as a UTC timestamp. */
80
+ value,
81
+ /** If a time zone is supplied, the `value` is converted to that time zone.
82
+ * Otherwise the user time zone will be used. */
83
+ formatOrOptions) {
84
+ return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
85
+ var _options;
86
+ if (timeZone && !((_options = options) != null && _options.timeZone)) {
87
+ options = _extends({}, options, {
88
+ timeZone: timeZone
89
+ });
90
+ }
91
+ return new Intl.DateTimeFormat(locale, options).format(value);
92
+ });
93
+ }
94
+ function number(value, formatOrOptions) {
95
+ return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
96
+ return new Intl.NumberFormat(locale, options).format(value);
97
+ });
98
+ }
99
+ function relativeTime( /** The date time that needs to be formatted. */
100
+ date, /** The reference point in time to which `date` will be formatted in relation to. */
101
+ now) {
102
+ try {
103
+ if (!now) {
104
+ if (globalNow) {
105
+ now = globalNow;
106
+ } else {
107
+ throw new Error(process.env.NODE_ENV !== 'production' ? "The `now` parameter wasn't provided and there was no global fallback configured on the provider." : undefined);
108
+ }
109
+ }
110
+ var dateDate = date instanceof Date ? date : new Date(date);
111
+ var nowDate = now instanceof Date ? now : new Date(now);
112
+ var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
113
+ var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
114
+ unit = _getRelativeTimeForma.unit,
115
+ value = _getRelativeTimeForma.value;
116
+ return new Intl.RelativeTimeFormat(locale, {
117
+ numeric: 'auto'
118
+ }).format(value, unit);
119
+ } catch (error) {
120
+ onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
121
+ return String(date);
122
+ }
123
+ }
124
+ return {
125
+ dateTime: dateTime,
126
+ number: number,
127
+ relativeTime: relativeTime
128
+ };
34
129
  }
35
130
 
36
- export { createTranslator as default };
131
+ export { createFormatter as default };
37
132
  //# sourceMappingURL=use-intl.esm2.js.map
@@ -1,132 +1,14 @@
1
- import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import IntlError, { IntlErrorCode } from './use-intl.esm.js';
3
- import { defaultOnError } from './use-intl.esm6.js';
1
+ import createFormatter from './use-intl.esm2.js';
4
2
 
5
- var MINUTE = 60;
6
- var HOUR = MINUTE * 60;
7
- var DAY = HOUR * 24;
8
- var WEEK = DAY * 7;
9
- var MONTH = DAY * (365 / 12); // Approximation
10
- var YEAR = DAY * 365;
11
- function getRelativeTimeFormatConfig(seconds) {
12
- var absValue = Math.abs(seconds);
13
- var value, unit;
14
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
15
- // will include fractions like '2.1 hours ago'.
16
- if (absValue < MINUTE) {
17
- unit = 'second';
18
- value = Math.round(seconds);
19
- } else if (absValue < HOUR) {
20
- unit = 'minute';
21
- value = Math.round(seconds / MINUTE);
22
- } else if (absValue < DAY) {
23
- unit = 'hour';
24
- value = Math.round(seconds / HOUR);
25
- } else if (absValue < WEEK) {
26
- unit = 'day';
27
- value = Math.round(seconds / DAY);
28
- } else if (absValue < MONTH) {
29
- unit = 'week';
30
- value = Math.round(seconds / WEEK);
31
- } else if (absValue < YEAR) {
32
- unit = 'month';
33
- value = Math.round(seconds / MONTH);
34
- } else {
35
- unit = 'year';
36
- value = Math.round(seconds / YEAR);
37
- }
3
+ /** @deprecated Switch to `createFormatter` */
4
+ function createIntl() {
5
+ var formatter = createFormatter.apply(void 0, arguments);
38
6
  return {
39
- value: value,
40
- unit: unit
41
- };
42
- }
43
- function createFormatter(_ref) {
44
- var formats = _ref.formats,
45
- locale = _ref.locale,
46
- globalNow = _ref.now,
47
- _ref$onError = _ref.onError,
48
- onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
49
- timeZone = _ref.timeZone;
50
- function resolveFormatOrOptions(typeFormats, formatOrOptions) {
51
- var options;
52
- if (typeof formatOrOptions === 'string') {
53
- var formatName = formatOrOptions;
54
- options = typeFormats == null ? void 0 : typeFormats[formatName];
55
- if (!options) {
56
- var error = new IntlError(IntlErrorCode.MISSING_FORMAT, process.env.NODE_ENV !== 'production' ? "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." : undefined);
57
- onError(error);
58
- throw error;
59
- }
60
- } else {
61
- options = formatOrOptions;
62
- }
63
- return options;
64
- }
65
- function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
66
- var options;
67
- try {
68
- options = resolveFormatOrOptions(typeFormats, formatOrOptions);
69
- } catch (error) {
70
- return String(value);
71
- }
72
- try {
73
- return formatter(options);
74
- } catch (error) {
75
- onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
76
- return String(value);
77
- }
78
- }
79
- function dateTime( /** If a number is supplied, this is interpreted as a UTC timestamp. */
80
- value,
81
- /** If a time zone is supplied, the `value` is converted to that time zone.
82
- * Otherwise the user time zone will be used. */
83
- formatOrOptions) {
84
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
85
- var _options;
86
- if (timeZone && !((_options = options) != null && _options.timeZone)) {
87
- options = _extends({}, options, {
88
- timeZone: timeZone
89
- });
90
- }
91
- return new Intl.DateTimeFormat(locale, options).format(value);
92
- });
93
- }
94
- function number(value, formatOrOptions) {
95
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
96
- return new Intl.NumberFormat(locale, options).format(value);
97
- });
98
- }
99
- function relativeTime( /** The date time that needs to be formatted. */
100
- date, /** The reference point in time to which `date` will be formatted in relation to. */
101
- now) {
102
- try {
103
- if (!now) {
104
- if (globalNow) {
105
- now = globalNow;
106
- } else {
107
- throw new Error(process.env.NODE_ENV !== 'production' ? "The `now` parameter wasn't provided and there was no global fallback configured on the provider." : undefined);
108
- }
109
- }
110
- var dateDate = date instanceof Date ? date : new Date(date);
111
- var nowDate = now instanceof Date ? now : new Date(now);
112
- var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
113
- var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
114
- unit = _getRelativeTimeForma.unit,
115
- value = _getRelativeTimeForma.value;
116
- return new Intl.RelativeTimeFormat(locale, {
117
- numeric: 'auto'
118
- }).format(value, unit);
119
- } catch (error) {
120
- onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
121
- return String(date);
122
- }
123
- }
124
- return {
125
- dateTime: dateTime,
126
- number: number,
127
- relativeTime: relativeTime
7
+ formatDateTime: formatter.dateTime,
8
+ formatNumber: formatter.number,
9
+ formatRelativeTime: formatter.relativeTime
128
10
  };
129
11
  }
130
12
 
131
- export { createFormatter as default };
13
+ export { createIntl as default };
132
14
  //# sourceMappingURL=use-intl.esm3.js.map
@@ -1,14 +1,33 @@
1
- import createFormatter from './use-intl.esm3.js';
1
+ import { inheritsLoose as _inheritsLoose, wrapNativeSuper as _wrapNativeSuper } from '../_virtual/use-intl.esm.js';
2
2
 
3
- /** @deprecated Switch to `createFormatter` */
4
- function createIntl() {
5
- var formatter = createFormatter.apply(void 0, arguments);
6
- return {
7
- formatDateTime: formatter.dateTime,
8
- formatNumber: formatter.number,
9
- formatRelativeTime: formatter.relativeTime
10
- };
11
- }
3
+ var IntlErrorCode;
4
+ (function (IntlErrorCode) {
5
+ IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
6
+ IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
7
+ IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
8
+ IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
9
+ IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
10
+ IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
11
+ })(IntlErrorCode || (IntlErrorCode = {}));
12
+ var IntlError = /*#__PURE__*/function (_Error) {
13
+ _inheritsLoose(IntlError, _Error);
14
+ function IntlError(code, originalMessage) {
15
+ var _this;
16
+ var message = code;
17
+ if (originalMessage) {
18
+ message += ': ' + originalMessage;
19
+ }
20
+ _this = _Error.call(this, message) || this;
21
+ _this.code = void 0;
22
+ _this.originalMessage = void 0;
23
+ _this.code = code;
24
+ if (originalMessage) {
25
+ _this.originalMessage = originalMessage;
26
+ }
27
+ return _this;
28
+ }
29
+ return IntlError;
30
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
12
31
 
13
- export { createIntl as default };
32
+ export { IntlErrorCode, IntlError as default };
14
33
  //# sourceMappingURL=use-intl.esm4.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm4.js","sources":["../../src/core/createTranslator.tsx"],"sourcesContent":["import Formats from './Formats';\nimport IntlConfig from './IntlConfig';\nimport TranslationValues from './TranslationValues';\nimport createTranslatorImpl, {\n CoreRichTranslationValues\n} from './createTranslatorImpl';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NamespaceKeys from './utils/NamespaceKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function createTranslator<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>({\n getMessageFallback = defaultGetMessageFallback,\n messages,\n namespace,\n onError = defaultOnError,\n ...rest\n}: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {\n messages: NonNullable<IntlConfig<IntlMessages>['messages']>;\n namespace?: NestedKey;\n}): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: CoreRichTranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n // We have to wrap the actual function so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return createTranslatorImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {\n ...rest,\n onError,\n getMessageFallback,\n messages: {'!': messages},\n // @ts-ignore\n namespace: namespace ? `!.${namespace}` : '!'\n },\n '!'\n );\n}\n"],"names":["createTranslator","_ref","_ref$getMessageFallba","getMessageFallback","defaultGetMessageFallback","messages","namespace","_ref$onError","onError","defaultOnError","rest","_objectWithoutPropertiesLoose","_excluded","createTranslatorImpl","_extends"],"mappings":";;;;;AAYA;;;;;;;AAOG;AACqB,SAAAA,gBAAgBA,CAAAC,IAAA,EAcvC;AAAA,EAAA,IAAAC,qBAAA,GAAAD,IAAA,CARCE,kBAAkB;AAAlBA,IAAAA,kBAAkB,GAAAD,qBAAA,KAAGE,KAAAA,CAAAA,GAAAA,yBAAyB,GAAAF,qBAAA;IAC9CG,QAAQ,GAAAJ,IAAA,CAARI,QAAQ;IACRC,SAAS,GAAAL,IAAA,CAATK,SAAS;IAAAC,YAAA,GAAAN,IAAA,CACTO,OAAO;AAAPA,IAAAA,OAAO,GAAAD,YAAA,KAAGE,KAAAA,CAAAA,GAAAA,cAAc,GAAAF,YAAA;AACrBG,IAAAA,IAAI,GAAAC,6BAAA,CAAAV,IAAA,EAAAW,SAAA,CAAA,CAAA;AAgEP;AACA;AACA;AACA,EAAA,OAAOC,oBAAoB,CAAAC,QAAA,CAAA,EAAA,EAKpBJ,IAAI,EAAA;AACPF,IAAAA,OAAO,EAAPA,OAAO;AACPL,IAAAA,kBAAkB,EAAlBA,kBAAkB;AAClBE,IAAAA,QAAQ,EAAE;AAAC,MAAA,GAAG,EAAEA,QAAAA;KAAS;AACzB;AACAC,IAAAA,SAAS,EAAEA,SAAS,GAAQA,IAAAA,GAAAA,SAAS,GAAK,GAAA;AAAG,GAAA,CAAA,EAE/C,GAAG,CACJ,CAAA;AACH;;;;"}
1
+ {"version":3,"file":"use-intl.esm4.js","sources":["../../src/core/IntlError.tsx"],"sourcesContent":["export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n INVALID_KEY = 'INVALID_KEY',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n"],"names":["IntlErrorCode","IntlError","_Error","_inheritsLoose","code","originalMessage","_this","message","call","_wrapN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,CAAAQ,eAAAA,gBAAA,CAfoCC,KAAK,CAAA;;;;"}
@@ -1,53 +1,15 @@
1
- import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import IntlError, { IntlErrorCode } from './use-intl.esm.js';
3
- import createBaseTranslator, { getMessagesOrError } from './use-intl.esm7.js';
4
- import resolveNamespace from './use-intl.esm8.js';
5
-
6
- var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
7
- function createTranslatorImpl(_ref, namespacePrefix) {
8
- var getMessageFallback = _ref.getMessageFallback,
9
- messages = _ref.messages,
10
- namespace = _ref.namespace,
11
- onError = _ref.onError,
12
- rest = _objectWithoutPropertiesLoose(_ref, _excluded);
13
- // The `namespacePrefix` is part of the type system.
14
- // See the comment in the function invocation.
15
- messages = messages[namespacePrefix];
16
- namespace = resolveNamespace(namespace, namespacePrefix);
17
- var translator = createBaseTranslator(_extends({}, rest, {
18
- onError: onError,
19
- getMessageFallback: getMessageFallback,
20
- messagesOrError: getMessagesOrError({
21
- messages: messages,
22
- namespace: namespace,
23
- onError: onError
24
- })
25
- }));
26
- var originalRich = translator.rich;
27
- function base() {
28
- return translator.apply(void 0, arguments);
29
- }
30
- // Augment `t.rich` to return plain strings
31
- base.rich = function (key, /** Key value pairs for values to interpolate into the message. */
32
- values, formats) {
33
- // `chunks` is returned as a string when no React element
34
- // is used, therefore it's safe to cast this type.
35
- var result = originalRich(key, values, formats);
36
- // When only string chunks are provided to the parser, only strings should be returned here.
37
- if (typeof result !== 'string') {
38
- var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== 'production' ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
39
- onError(error);
40
- return getMessageFallback({
41
- error: error,
42
- key: key,
43
- namespace: namespace
44
- });
45
- }
46
- return result;
47
- };
48
- base.raw = translator.raw;
49
- return base;
1
+ /**
2
+ * Contains defaults that are used for all entry points into the core.
3
+ * See also `InitializedIntlConfiguration`.
4
+ */
5
+ function defaultGetMessageFallback(props) {
6
+ return [props.namespace, props.key].filter(function (part) {
7
+ return part != null;
8
+ }).join('.');
9
+ }
10
+ function defaultOnError(error) {
11
+ console.error(error);
50
12
  }
51
13
 
52
- export { createTranslatorImpl as default };
14
+ export { defaultGetMessageFallback, defaultOnError };
53
15
  //# sourceMappingURL=use-intl.esm5.js.map
@@ -1,15 +1,53 @@
1
- /**
2
- * Contains defaults that are used for all entry points into the core.
3
- * See also `InitializedIntlConfiguration`.
4
- */
5
- function defaultGetMessageFallback(props) {
6
- return [props.namespace, props.key].filter(function (part) {
7
- return part != null;
8
- }).join('.');
9
- }
10
- function defaultOnError(error) {
11
- console.error(error);
1
+ import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
+ import IntlError, { IntlErrorCode } from './use-intl.esm4.js';
3
+ import createBaseTranslator, { getMessagesOrError } from './use-intl.esm8.js';
4
+ import resolveNamespace from './use-intl.esm9.js';
5
+
6
+ var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
7
+ function createTranslatorImpl(_ref, namespacePrefix) {
8
+ var getMessageFallback = _ref.getMessageFallback,
9
+ messages = _ref.messages,
10
+ namespace = _ref.namespace,
11
+ onError = _ref.onError,
12
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
13
+ // The `namespacePrefix` is part of the type system.
14
+ // See the comment in the function invocation.
15
+ messages = messages[namespacePrefix];
16
+ namespace = resolveNamespace(namespace, namespacePrefix);
17
+ var translator = createBaseTranslator(_extends({}, rest, {
18
+ onError: onError,
19
+ getMessageFallback: getMessageFallback,
20
+ messagesOrError: getMessagesOrError({
21
+ messages: messages,
22
+ namespace: namespace,
23
+ onError: onError
24
+ })
25
+ }));
26
+ var originalRich = translator.rich;
27
+ function base() {
28
+ return translator.apply(void 0, arguments);
29
+ }
30
+ // Augment `t.rich` to return plain strings
31
+ base.rich = function (key, /** Key value pairs for values to interpolate into the message. */
32
+ values, formats) {
33
+ // `chunks` is returned as a string when no React element
34
+ // is used, therefore it's safe to cast this type.
35
+ var result = originalRich(key, values, formats);
36
+ // When only string chunks are provided to the parser, only strings should be returned here.
37
+ if (typeof result !== 'string') {
38
+ var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== 'production' ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
39
+ onError(error);
40
+ return getMessageFallback({
41
+ error: error,
42
+ key: key,
43
+ namespace: namespace
44
+ });
45
+ }
46
+ return result;
47
+ };
48
+ base.raw = translator.raw;
49
+ return base;
12
50
  }
13
51
 
14
- export { defaultGetMessageFallback, defaultOnError };
52
+ export { createTranslatorImpl as default };
15
53
  //# sourceMappingURL=use-intl.esm6.js.map