use-intl 2.14.0 → 2.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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.esm.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,37 @@
1
- import createFormatter from './use-intl.esm3.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
- /** @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
- };
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
+ }), '!');
11
34
  }
12
35
 
13
- export { createIntl as default };
36
+ export { createTranslator as default };
14
37
  //# sourceMappingURL=use-intl.esm4.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.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.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.esm6.js.map