use-intl 2.14.2 → 2.14.3

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.
@@ -1,37 +1,33 @@
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';
1
+ import { inheritsLoose as _inheritsLoose, wrapNativeSuper as _wrapNativeSuper } from '../_virtual/use-intl.esm.js';
4
2
 
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
- }
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));
35
31
 
36
- export { createTranslator as default };
32
+ export { IntlErrorCode, IntlError as default };
37
33
  //# sourceMappingURL=use-intl.esm.js.map
@@ -1,132 +1,37 @@
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';
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';
4
4
 
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,
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,
47
19
  _ref$onError = _ref.onError,
48
20
  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
128
- };
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
+ }), '!');
129
34
  }
130
35
 
131
- export { createFormatter as default };
36
+ export { createTranslator as default };
132
37
  //# sourceMappingURL=use-intl.esm2.js.map
@@ -1,14 +1,132 @@
1
- import createFormatter from './use-intl.esm2.js';
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';
2
4
 
3
- /** @deprecated Switch to `createFormatter` */
4
- function createIntl() {
5
- var formatter = createFormatter.apply(void 0, arguments);
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
+ }
6
38
  return {
7
- formatDateTime: formatter.dateTime,
8
- formatNumber: formatter.number,
9
- formatRelativeTime: formatter.relativeTime
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
10
128
  };
11
129
  }
12
130
 
13
- export { createIntl as default };
131
+ export { createFormatter as default };
14
132
  //# sourceMappingURL=use-intl.esm3.js.map
@@ -1,33 +1,14 @@
1
- import { inheritsLoose as _inheritsLoose, wrapNativeSuper as _wrapNativeSuper } from '../_virtual/use-intl.esm.js';
1
+ import createFormatter from './use-intl.esm3.js';
2
2
 
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));
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
+ }
31
12
 
32
- export { IntlErrorCode, IntlError as default };
13
+ export { createIntl as default };
33
14
  //# sourceMappingURL=use-intl.esm4.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.esm.js';
3
+ import createBaseTranslator, { getMessagesOrError } from './use-intl.esm8.js';
4
+ import resolveNamespace from './use-intl.esm7.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.esm5.js.map
@@ -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.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;
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.esm6.js.map
@@ -1,28 +1,10 @@
1
- import IntlError, { IntlErrorCode } from './use-intl.esm4.js';
2
-
3
- function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
4
- Object.entries(messages).forEach(function (_ref) {
5
- var key = _ref[0],
6
- messageOrMessages = _ref[1];
7
- if (key.includes('.')) {
8
- var keyLabel = key;
9
- if (parentPath) keyLabel += " (at " + parentPath + ")";
10
- invalidKeyLabels.push(keyLabel);
11
- }
12
- if (messageOrMessages != null && typeof messageOrMessages === 'object') {
13
- validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
14
- return part != null;
15
- }).join('.'));
16
- }
17
- });
18
- }
19
- function validateMessages(messages, onError) {
20
- var invalidKeyLabels = [];
21
- validateMessagesSegment(messages, invalidKeyLabels);
22
- if (invalidKeyLabels.length > 0) {
23
- onError(new IntlError(IntlErrorCode.INVALID_KEY, process.env.NODE_ENV !== 'production' ? "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ') + "\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" : undefined));
24
- }
1
+ /**
2
+ * For the strictly typed messages to work we have to wrap the namespace into
3
+ * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
4
+ */
5
+ function resolveNamespace(namespace, namespacePrefix) {
6
+ return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
25
7
  }
26
8
 
27
- export { validateMessages as default };
9
+ export { resolveNamespace as default };
28
10
  //# sourceMappingURL=use-intl.esm7.js.map
@@ -1,9 +1,9 @@
1
1
  import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
2
  import IntlMessageFormat from 'intl-messageformat';
3
3
  import { isValidElement, cloneElement } from 'react';
4
- import IntlError, { IntlErrorCode } from './use-intl.esm4.js';
4
+ import IntlError, { IntlErrorCode } from './use-intl.esm.js';
5
5
  import convertFormatsToIntlMessageFormat from './use-intl.esm10.js';
6
- import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm5.js';
6
+ import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm6.js';
7
7
 
8
8
  function resolvePath(messages, key, namespace) {
9
9
  if (!messages) {
@@ -1,10 +1,28 @@
1
- /**
2
- * For the strictly typed messages to work we have to wrap the namespace into
3
- * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
4
- */
5
- function resolveNamespace(namespace, namespacePrefix) {
6
- return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
1
+ import IntlError, { IntlErrorCode } from './use-intl.esm.js';
2
+
3
+ function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
4
+ Object.entries(messages).forEach(function (_ref) {
5
+ var key = _ref[0],
6
+ messageOrMessages = _ref[1];
7
+ if (key.includes('.')) {
8
+ var keyLabel = key;
9
+ if (parentPath) keyLabel += " (at " + parentPath + ")";
10
+ invalidKeyLabels.push(keyLabel);
11
+ }
12
+ if (messageOrMessages != null && typeof messageOrMessages === 'object') {
13
+ validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
14
+ return part != null;
15
+ }).join('.'));
16
+ }
17
+ });
18
+ }
19
+ function validateMessages(messages, onError) {
20
+ var invalidKeyLabels = [];
21
+ validateMessagesSegment(messages, invalidKeyLabels);
22
+ if (invalidKeyLabels.length > 0) {
23
+ onError(new IntlError(IntlErrorCode.INVALID_KEY, process.env.NODE_ENV !== 'production' ? "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ') + "\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" : undefined));
24
+ }
7
25
  }
8
26
 
9
- export { resolveNamespace as default };
27
+ export { validateMessages as default };
10
28
  //# sourceMappingURL=use-intl.esm9.js.map
@@ -1,7 +1,7 @@
1
1
  import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose } from '../_virtual/use-intl.esm.js';
2
2
  import React from 'react';
3
3
  import IntlContext from './use-intl.esm8.js';
4
- import getInitializedConfig from './use-intl.esm10.js';
4
+ import getInitializedConfig from './use-intl.esm9.js';
5
5
 
6
6
  var _excluded = ["children"];
7
7
  function IntlProvider(_ref) {
@@ -1,29 +1,13 @@
1
- import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import { defaultOnError, defaultGetMessageFallback } from '../core/use-intl.esm5.js';
3
- import validateMessages from '../core/use-intl.esm7.js';
1
+ import { useContext } from 'react';
2
+ import IntlContext from './use-intl.esm8.js';
4
3
 
5
- var _excluded = ["getMessageFallback", "messages", "onError"];
6
- /**
7
- * Enhances the incoming props with defaults.
8
- */
9
- function getInitializedConfig(_ref) {
10
- var getMessageFallback = _ref.getMessageFallback,
11
- messages = _ref.messages,
12
- onError = _ref.onError,
13
- rest = _objectWithoutPropertiesLoose(_ref, _excluded);
14
- var finalOnError = onError || defaultOnError;
15
- var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
16
- if (process.env.NODE_ENV !== 'production') {
17
- if (messages) {
18
- validateMessages(messages, finalOnError);
19
- }
4
+ function useIntlContext() {
5
+ var context = useContext(IntlContext);
6
+ if (!context) {
7
+ throw new Error(process.env.NODE_ENV !== 'production' ? 'No intl context found. Have you configured the provider?' : undefined);
20
8
  }
21
- return _extends({}, rest, {
22
- messages: messages,
23
- onError: finalOnError,
24
- getMessageFallback: finalGetMessageFallback
25
- });
9
+ return context;
26
10
  }
27
11
 
28
- export { getInitializedConfig as default };
12
+ export { useIntlContext as default };
29
13
  //# sourceMappingURL=use-intl.esm10.js.map
@@ -1,7 +1,7 @@
1
1
  import { useRef, useMemo } from 'react';
2
2
  import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm8.js';
3
- import resolveNamespace from '../core/use-intl.esm9.js';
4
- import useIntlContext from './use-intl.esm9.js';
3
+ import resolveNamespace from '../core/use-intl.esm7.js';
4
+ import useIntlContext from './use-intl.esm10.js';
5
5
 
6
6
  function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
7
7
  var _useIntlContext = useIntlContext(),
@@ -1,24 +1,26 @@
1
- import { useMemo } from 'react';
2
- import createFormatter from '../core/use-intl.esm2.js';
3
- import useIntlContext from './use-intl.esm9.js';
1
+ import useIntlContext from './use-intl.esm10.js';
2
+ import useTranslationsImpl from './use-intl.esm11.js';
4
3
 
5
- function useFormatter() {
6
- var _useIntlContext = useIntlContext(),
7
- formats = _useIntlContext.formats,
8
- locale = _useIntlContext.locale,
9
- globalNow = _useIntlContext.now,
10
- onError = _useIntlContext.onError,
11
- timeZone = _useIntlContext.timeZone;
12
- return useMemo(function () {
13
- return createFormatter({
14
- formats: formats,
15
- locale: locale,
16
- now: globalNow,
17
- onError: onError,
18
- timeZone: timeZone
19
- });
20
- }, [formats, globalNow, locale, onError, timeZone]);
4
+ /**
5
+ * Translates messages from the given namespace by using the ICU syntax.
6
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
7
+ *
8
+ * If no namespace is provided, all available messages are returned.
9
+ * The namespace can also indicate nesting by using a dot
10
+ * (e.g. `namespace.Component`).
11
+ */
12
+ function useTranslations(namespace) {
13
+ var context = useIntlContext();
14
+ var messages = context.messages;
15
+ // We have to wrap the actual hook so the type inference for the optional
16
+ // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
17
+ // The prefix ("!") is arbitrary.
18
+ return useTranslationsImpl({
19
+ '!': messages
20
+ },
21
+ // @ts-ignore
22
+ namespace ? "!." + namespace : '!', '!');
21
23
  }
22
24
 
23
- export { useFormatter as default };
25
+ export { useTranslations as default };
24
26
  //# sourceMappingURL=use-intl.esm2.js.map
@@ -1,4 +1,4 @@
1
- import useIntlContext from './use-intl.esm9.js';
1
+ import useIntlContext from './use-intl.esm10.js';
2
2
 
3
3
  function useLocale() {
4
4
  return useIntlContext().locale;
@@ -1,26 +1,8 @@
1
- import useIntlContext from './use-intl.esm9.js';
2
- import useTranslationsImpl from './use-intl.esm11.js';
1
+ import useIntlContext from './use-intl.esm10.js';
3
2
 
4
- /**
5
- * Translates messages from the given namespace by using the ICU syntax.
6
- * See https://formatjs.io/docs/core-concepts/icu-syntax.
7
- *
8
- * If no namespace is provided, all available messages are returned.
9
- * The namespace can also indicate nesting by using a dot
10
- * (e.g. `namespace.Component`).
11
- */
12
- function useTranslations(namespace) {
13
- var context = useIntlContext();
14
- var messages = context.messages;
15
- // We have to wrap the actual hook so the type inference for the optional
16
- // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
17
- // The prefix ("!") is arbitrary.
18
- return useTranslationsImpl({
19
- '!': messages
20
- },
21
- // @ts-ignore
22
- namespace ? "!." + namespace : '!', '!');
3
+ function useTimeZone() {
4
+ return useIntlContext().timeZone;
23
5
  }
24
6
 
25
- export { useTranslations as default };
7
+ export { useTimeZone as default };
26
8
  //# sourceMappingURL=use-intl.esm4.js.map
@@ -1,5 +1,5 @@
1
1
  import { useState, useEffect } from 'react';
2
- import useIntlContext from './use-intl.esm9.js';
2
+ import useIntlContext from './use-intl.esm10.js';
3
3
 
4
4
  function getNow() {
5
5
  return new Date();
@@ -1,8 +1,24 @@
1
- import useIntlContext from './use-intl.esm9.js';
1
+ import { useMemo } from 'react';
2
+ import createFormatter from '../core/use-intl.esm3.js';
3
+ import useIntlContext from './use-intl.esm10.js';
2
4
 
3
- function useTimeZone() {
4
- return useIntlContext().timeZone;
5
+ function useFormatter() {
6
+ var _useIntlContext = useIntlContext(),
7
+ formats = _useIntlContext.formats,
8
+ locale = _useIntlContext.locale,
9
+ globalNow = _useIntlContext.now,
10
+ onError = _useIntlContext.onError,
11
+ timeZone = _useIntlContext.timeZone;
12
+ return useMemo(function () {
13
+ return createFormatter({
14
+ formats: formats,
15
+ locale: locale,
16
+ now: globalNow,
17
+ onError: onError,
18
+ timeZone: timeZone
19
+ });
20
+ }, [formats, globalNow, locale, onError, timeZone]);
5
21
  }
6
22
 
7
- export { useTimeZone as default };
23
+ export { useFormatter as default };
8
24
  //# sourceMappingURL=use-intl.esm6.js.map
@@ -1,6 +1,6 @@
1
1
  import { useMemo } from 'react';
2
- import createIntl from '../core/use-intl.esm3.js';
3
- import useIntlContext from './use-intl.esm9.js';
2
+ import createIntl from '../core/use-intl.esm4.js';
3
+ import useIntlContext from './use-intl.esm10.js';
4
4
 
5
5
  var hasWarned = false;
6
6
  /** @deprecated Switch to `useFormatter` instead. */
@@ -1,13 +1,29 @@
1
- import { useContext } from 'react';
2
- import IntlContext from './use-intl.esm8.js';
1
+ import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
+ import { defaultOnError, defaultGetMessageFallback } from '../core/use-intl.esm6.js';
3
+ import validateMessages from '../core/use-intl.esm9.js';
3
4
 
4
- function useIntlContext() {
5
- var context = useContext(IntlContext);
6
- if (!context) {
7
- throw new Error(process.env.NODE_ENV !== 'production' ? 'No intl context found. Have you configured the provider?' : undefined);
5
+ var _excluded = ["getMessageFallback", "messages", "onError"];
6
+ /**
7
+ * Enhances the incoming props with defaults.
8
+ */
9
+ function getInitializedConfig(_ref) {
10
+ var getMessageFallback = _ref.getMessageFallback,
11
+ messages = _ref.messages,
12
+ onError = _ref.onError,
13
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
14
+ var finalOnError = onError || defaultOnError;
15
+ var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
16
+ if (process.env.NODE_ENV !== 'production') {
17
+ if (messages) {
18
+ validateMessages(messages, finalOnError);
19
+ }
8
20
  }
9
- return context;
21
+ return _extends({}, rest, {
22
+ messages: messages,
23
+ onError: finalOnError,
24
+ getMessageFallback: finalGetMessageFallback
25
+ });
10
26
  }
11
27
 
12
- export { useIntlContext as default };
28
+ export { getInitializedConfig as default };
13
29
  //# sourceMappingURL=use-intl.esm9.js.map
@@ -1,12 +1,12 @@
1
- export { default as IntlError, IntlErrorCode } from './core/use-intl.esm4.js';
2
- export { default as createTranslator } from './core/use-intl.esm.js';
3
- export { default as createFormatter } from './core/use-intl.esm2.js';
4
- export { default as createIntl } from './core/use-intl.esm3.js';
1
+ export { default as IntlError, IntlErrorCode } from './core/use-intl.esm.js';
2
+ export { default as createTranslator } from './core/use-intl.esm2.js';
3
+ export { default as createFormatter } from './core/use-intl.esm3.js';
4
+ export { default as createIntl } from './core/use-intl.esm4.js';
5
5
  export { default as IntlProvider } from './react/use-intl.esm.js';
6
- export { default as useTranslations } from './react/use-intl.esm4.js';
6
+ export { default as useTranslations } from './react/use-intl.esm2.js';
7
7
  export { default as useLocale } from './react/use-intl.esm3.js';
8
8
  export { default as useNow } from './react/use-intl.esm5.js';
9
- export { default as useTimeZone } from './react/use-intl.esm6.js';
10
- export { default as useFormatter } from './react/use-intl.esm2.js';
9
+ export { default as useTimeZone } from './react/use-intl.esm4.js';
10
+ export { default as useFormatter } from './react/use-intl.esm6.js';
11
11
  export { default as useIntl } from './react/use-intl.esm7.js';
12
12
  //# sourceMappingURL=use-intl.esm.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-intl",
3
- "version": "2.14.2",
3
+ "version": "2.14.3",
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.",