use-intl 2.11.0-alpha.2 → 2.11.0-useFormatter-alpha.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.
- package/CHANGELOG.md +19 -0
- package/README.md +32 -20
- package/dist/core/createFormatter.d.ts +17 -0
- package/dist/core/createIntl.d.ts +5 -15
- package/dist/core/index.d.ts +1 -0
- package/dist/core/use-intl.esm.js +38 -41
- package/dist/core/use-intl.esm.js.map +1 -1
- package/dist/core/use-intl.esm10.js +34 -0
- package/dist/core/use-intl.esm10.js.map +1 -0
- package/dist/core/use-intl.esm2.js +151 -35
- package/dist/core/use-intl.esm2.js.map +1 -1
- package/dist/core/use-intl.esm3.js +27 -140
- package/dist/core/use-intl.esm3.js.map +1 -1
- package/dist/core/use-intl.esm4.js +9 -53
- package/dist/core/use-intl.esm4.js.map +1 -1
- package/dist/core/use-intl.esm5.js +28 -11
- package/dist/core/use-intl.esm5.js.map +1 -1
- package/dist/core/use-intl.esm6.js +11 -209
- package/dist/core/use-intl.esm6.js.map +1 -1
- package/dist/core/use-intl.esm7.js +56 -7
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/core/use-intl.esm8.js +200 -19
- package/dist/core/use-intl.esm8.js.map +1 -1
- package/dist/core/use-intl.esm9.js +5 -29
- package/dist/core/use-intl.esm9.js.map +1 -1
- package/dist/react/IntlContext.d.ts +19 -2
- package/dist/react/IntlProvider.d.ts +16 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/use-intl.esm.js +26 -7
- package/dist/react/use-intl.esm.js.map +1 -1
- package/dist/react/use-intl.esm10.js +2 -2
- package/dist/react/use-intl.esm2.js +3 -20
- package/dist/react/use-intl.esm2.js.map +1 -1
- package/dist/react/use-intl.esm3.js +40 -16
- package/dist/react/use-intl.esm3.js.map +1 -1
- package/dist/react/use-intl.esm4.js +21 -3
- package/dist/react/use-intl.esm4.js.map +1 -1
- package/dist/react/use-intl.esm5.js +20 -3
- package/dist/react/use-intl.esm5.js.map +1 -1
- package/dist/react/use-intl.esm6.js +24 -41
- package/dist/react/use-intl.esm6.js.map +1 -1
- package/dist/react/use-intl.esm7.js +5 -3
- package/dist/react/use-intl.esm7.js.map +1 -1
- package/dist/react/use-intl.esm8.js +3 -30
- package/dist/react/use-intl.esm8.js.map +1 -1
- package/dist/react/use-intl.esm9.js +1 -1
- package/dist/react/useFormatter.d.ts +5 -0
- package/dist/react/useIntl.d.ts +1 -0
- package/dist/react/useIntlContext.d.ts +1 -1
- package/dist/src/core/createBaseTranslator.js +10 -8
- package/dist/src/core/createBaseTranslator.js.map +1 -1
- package/dist/src/core/createFormatter.d.ts +17 -0
- package/dist/src/core/createFormatter.js +126 -0
- package/dist/src/core/createFormatter.js.map +1 -0
- package/dist/src/core/createIntl.d.ts +5 -15
- package/dist/src/core/createIntl.js +9 -124
- package/dist/src/core/createIntl.js.map +1 -1
- package/dist/src/core/index.d.ts +1 -0
- package/dist/src/core/index.js +2 -0
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/react/IntlContext.d.ts +19 -2
- package/dist/src/react/IntlContext.js.map +1 -1
- package/dist/src/react/IntlProvider.d.ts +16 -2
- package/dist/src/react/IntlProvider.js +13 -4
- package/dist/src/react/IntlProvider.js.map +1 -1
- package/dist/src/react/index.d.ts +2 -1
- package/dist/src/react/index.js +3 -1
- package/dist/src/react/index.js.map +1 -1
- package/dist/src/react/useFormatter.d.ts +5 -0
- package/dist/src/react/useFormatter.js +14 -0
- package/dist/src/react/useFormatter.js.map +1 -0
- package/dist/src/react/useIntl.d.ts +1 -0
- package/dist/src/react/useIntl.js +6 -0
- package/dist/src/react/useIntl.js.map +1 -1
- package/dist/src/react/useIntlContext.d.ts +1 -1
- package/dist/use-intl.cjs.development.js +99 -68
- package/dist/use-intl.cjs.development.js.map +1 -1
- package/dist/use-intl.cjs.production.min.js +1 -1
- package/dist/use-intl.cjs.production.min.js.map +1 -1
- package/dist/use-intl.esm.js +10 -8
- package/dist/use-intl.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/core/createBaseTranslator.tsx +14 -12
- package/src/core/createFormatter.tsx +181 -0
- package/src/core/createIntl.tsx +12 -180
- package/src/core/index.tsx +3 -0
- package/src/react/IntlContext.tsx +20 -2
- package/src/react/IntlProvider.tsx +36 -5
- package/src/react/index.tsx +4 -1
- package/src/react/useFormatter.tsx +19 -0
- package/src/react/useIntl.tsx +10 -0
- package/dist/react/IntlContextValue.d.ts +0 -19
- package/dist/react/IntlProviderProps.d.ts +0 -15
- package/dist/react/getIntlContextValue.d.ts +0 -18
- package/dist/src/react/IntlContextValue.d.ts +0 -19
- package/dist/src/react/IntlContextValue.js +0 -2
- package/dist/src/react/IntlContextValue.js.map +0 -1
- package/dist/src/react/IntlProviderProps.d.ts +0 -15
- package/dist/src/react/IntlProviderProps.js +0 -2
- package/dist/src/react/IntlProviderProps.js.map +0 -1
- package/dist/src/react/getIntlContextValue.d.ts +0 -18
- package/dist/src/react/getIntlContextValue.js +0 -21
- package/dist/src/react/getIntlContextValue.js.map +0 -1
- package/src/react/IntlContextValue.tsx +0 -21
- package/src/react/IntlProviderProps.tsx +0 -17
- package/src/react/getIntlContextValue.tsx +0 -30
|
@@ -1,126 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const absValue = Math.abs(seconds);
|
|
11
|
-
let value, unit;
|
|
12
|
-
// We have to round the resulting values, as `Intl.RelativeTimeFormat`
|
|
13
|
-
// will include fractions like '2.1 hours ago'.
|
|
14
|
-
if (absValue < MINUTE) {
|
|
15
|
-
unit = 'second';
|
|
16
|
-
value = Math.round(seconds);
|
|
17
|
-
}
|
|
18
|
-
else if (absValue < HOUR) {
|
|
19
|
-
unit = 'minute';
|
|
20
|
-
value = Math.round(seconds / MINUTE);
|
|
21
|
-
}
|
|
22
|
-
else if (absValue < DAY) {
|
|
23
|
-
unit = 'hour';
|
|
24
|
-
value = Math.round(seconds / HOUR);
|
|
25
|
-
}
|
|
26
|
-
else if (absValue < WEEK) {
|
|
27
|
-
unit = 'day';
|
|
28
|
-
value = Math.round(seconds / DAY);
|
|
29
|
-
}
|
|
30
|
-
else if (absValue < MONTH) {
|
|
31
|
-
unit = 'week';
|
|
32
|
-
value = Math.round(seconds / WEEK);
|
|
33
|
-
}
|
|
34
|
-
else if (absValue < YEAR) {
|
|
35
|
-
unit = 'month';
|
|
36
|
-
value = Math.round(seconds / MONTH);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
unit = 'year';
|
|
40
|
-
value = Math.round(seconds / YEAR);
|
|
41
|
-
}
|
|
42
|
-
return { value, unit };
|
|
43
|
-
}
|
|
44
|
-
export default function createIntl({ formats, locale, now: globalNow, onError = defaultOnError, timeZone }) {
|
|
45
|
-
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
|
|
46
|
-
let options;
|
|
47
|
-
if (typeof formatOrOptions === 'string') {
|
|
48
|
-
const formatName = formatOrOptions;
|
|
49
|
-
options = typeFormats?.[formatName];
|
|
50
|
-
if (!options) {
|
|
51
|
-
const error = new IntlError(IntlErrorCode.MISSING_FORMAT, process.env.NODE_ENV !== 'production'
|
|
52
|
-
? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
|
|
53
|
-
: undefined);
|
|
54
|
-
onError(error);
|
|
55
|
-
throw error;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
options = formatOrOptions;
|
|
60
|
-
}
|
|
61
|
-
return options;
|
|
62
|
-
}
|
|
63
|
-
function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
|
|
64
|
-
let options;
|
|
65
|
-
try {
|
|
66
|
-
options = resolveFormatOrOptions(typeFormats, formatOrOptions);
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
return String(value);
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
return formatter(options);
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
|
|
76
|
-
return String(value);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
function formatDateTime(
|
|
80
|
-
/** If a number is supplied, this is interpreted as a UTC timestamp. */
|
|
81
|
-
value,
|
|
82
|
-
/** If a time zone is supplied, the `value` is converted to that time zone.
|
|
83
|
-
* Otherwise the user time zone will be used. */
|
|
84
|
-
formatOrOptions) {
|
|
85
|
-
return getFormattedValue(value, formatOrOptions, formats?.dateTime, (options) => {
|
|
86
|
-
if (timeZone && !options?.timeZone) {
|
|
87
|
-
options = { ...options, timeZone };
|
|
88
|
-
}
|
|
89
|
-
return new Intl.DateTimeFormat(locale, options).format(value);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
function formatNumber(value, formatOrOptions) {
|
|
93
|
-
return getFormattedValue(value, formatOrOptions, formats?.number, (options) => new Intl.NumberFormat(locale, options).format(value));
|
|
94
|
-
}
|
|
95
|
-
function formatRelativeTime(
|
|
96
|
-
/** The date time that needs to be formatted. */
|
|
97
|
-
date,
|
|
98
|
-
/** The reference point in time to which `date` will be formatted in relation to. */
|
|
99
|
-
now) {
|
|
100
|
-
try {
|
|
101
|
-
if (!now) {
|
|
102
|
-
if (globalNow) {
|
|
103
|
-
now = globalNow;
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
throw new Error(process.env.NODE_ENV !== 'production'
|
|
107
|
-
? `The \`now\` parameter wasn't provided to \`formatRelativeTime\` and there was no global fallback configured on the provider.`
|
|
108
|
-
: undefined);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const dateDate = date instanceof Date ? date : new Date(date);
|
|
112
|
-
const nowDate = now instanceof Date ? now : new Date(now);
|
|
113
|
-
const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
|
|
114
|
-
const { unit, value } = getRelativeTimeFormatConfig(seconds);
|
|
115
|
-
return new Intl.RelativeTimeFormat(locale, {
|
|
116
|
-
numeric: 'auto'
|
|
117
|
-
}).format(value, unit);
|
|
118
|
-
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
|
|
121
|
-
return String(date);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return { formatDateTime, formatNumber, formatRelativeTime };
|
|
1
|
+
import createFormatter from './createFormatter';
|
|
2
|
+
/** @deprecated Switch to `createFormatter` */
|
|
3
|
+
export default function createIntl(...args) {
|
|
4
|
+
const formatter = createFormatter(...args);
|
|
5
|
+
return {
|
|
6
|
+
formatDateTime: formatter.dateTime,
|
|
7
|
+
formatNumber: formatter.number,
|
|
8
|
+
formatRelativeTime: formatter.relativeTime
|
|
9
|
+
};
|
|
125
10
|
}
|
|
126
11
|
//# sourceMappingURL=createIntl.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createIntl.js","sourceRoot":"","sources":["../../../src/core/createIntl.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createIntl.js","sourceRoot":"","sources":["../../../src/core/createIntl.tsx"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAEhD,8CAA8C;AAC9C,MAAM,CAAC,OAAO,UAAU,UAAU,CAChC,GAAG,IAAwC;IAE3C,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,IAAI,CAAC,CAAC;IAC3C,OAAO;QACL,cAAc,EAAE,SAAS,CAAC,QAAQ;QAClC,YAAY,EAAE,SAAS,CAAC,MAAM;QAC9B,kBAAkB,EAAE,SAAS,CAAC,YAAY;KAC3C,CAAC;AACJ,CAAC"}
|
package/dist/src/core/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export { default as DateTimeFormatOptions } from './DateTimeFormatOptions';
|
|
|
5
5
|
export { default as NumberFormatOptions } from './NumberFormatOptions';
|
|
6
6
|
export { default as IntlError, IntlErrorCode } from './IntlError';
|
|
7
7
|
export { default as createTranslator } from './createTranslator';
|
|
8
|
+
export { default as createFormatter } from './createFormatter';
|
|
8
9
|
export { default as createIntl } from './createIntl';
|
package/dist/src/core/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { default as IntlError, IntlErrorCode } from './IntlError';
|
|
2
2
|
export { default as createTranslator } from './createTranslator';
|
|
3
|
+
export { default as createFormatter } from './createFormatter';
|
|
4
|
+
// TODO: Remove in next major version
|
|
3
5
|
export { default as createIntl } from './createIntl';
|
|
4
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/index.tsx"],"names":[],"mappings":"AAQA,OAAO,EAAC,OAAO,IAAI,SAAS,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAChE,OAAO,EAAC,OAAO,IAAI,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/index.tsx"],"names":[],"mappings":"AAQA,OAAO,EAAC,OAAO,IAAI,SAAS,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAChE,OAAO,EAAC,OAAO,IAAI,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAC,OAAO,IAAI,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAE7D,qCAAqC;AACrC,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,MAAM,cAAc,CAAC"}
|
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import
|
|
3
|
-
|
|
2
|
+
import AbstractIntlMessages from '../core/AbstractIntlMessages';
|
|
3
|
+
import Formats from '../core/Formats';
|
|
4
|
+
import IntlError from '../core/IntlError';
|
|
5
|
+
import { RichTranslationValues } from '../core/TranslationValues';
|
|
6
|
+
export declare type IntlContextShape = {
|
|
7
|
+
messages?: AbstractIntlMessages;
|
|
8
|
+
locale: string;
|
|
9
|
+
formats?: Partial<Formats>;
|
|
10
|
+
timeZone?: string;
|
|
11
|
+
onError(error: IntlError): void;
|
|
12
|
+
getMessageFallback(info: {
|
|
13
|
+
error: IntlError;
|
|
14
|
+
key: string;
|
|
15
|
+
namespace?: string;
|
|
16
|
+
}): string;
|
|
17
|
+
now?: Date;
|
|
18
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
19
|
+
};
|
|
20
|
+
declare const IntlContext: import("react").Context<IntlContextShape | undefined>;
|
|
4
21
|
export default IntlContext;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntlContext.js","sourceRoot":"","sources":["../../../src/react/IntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"IntlContext.js","sourceRoot":"","sources":["../../../src/react/IntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;AAqBpC,MAAM,WAAW,GAAG,aAAa,CAA+B,SAAS,CAAC,CAAC;AAE3E,eAAe,WAAW,CAAC"}
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { AbstractIntlMessages } from '../core';
|
|
3
|
+
import IntlConfiguration from '../core/IntlConfiguration';
|
|
4
|
+
import { RichTranslationValues } from '../core/TranslationValues';
|
|
5
|
+
declare type Props = IntlConfiguration & {
|
|
6
|
+
/** All components that use the provided hooks should be within this tree. */
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
/** Global default values for translation values and rich text elements.
|
|
9
|
+
* Can be used for consistent usage or styling of rich text elements.
|
|
10
|
+
* Defaults will be overidden by locally provided values. */
|
|
11
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
12
|
+
/** All messages that will be available in your components. */
|
|
13
|
+
messages?: AbstractIntlMessages;
|
|
14
|
+
};
|
|
15
|
+
export default function IntlProvider({ children, onError, getMessageFallback, messages, ...contextValues }: Props): JSX.Element;
|
|
16
|
+
export {};
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
2
|
+
import { defaultGetMessageFallback, defaultOnError } from '../core/defaults';
|
|
3
|
+
import validateMessages from '../core/validateMessages';
|
|
2
4
|
import IntlContext from './IntlContext';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
export default function IntlProvider({ children, onError = defaultOnError, getMessageFallback = defaultGetMessageFallback, messages, ...contextValues }) {
|
|
6
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
7
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (messages) {
|
|
10
|
+
validateMessages(messages, onError);
|
|
11
|
+
}
|
|
12
|
+
}, [messages, onError]);
|
|
13
|
+
}
|
|
14
|
+
return (React.createElement(IntlContext.Provider, { value: { ...contextValues, messages, onError, getMessageFallback } }, children));
|
|
6
15
|
}
|
|
7
16
|
//# sourceMappingURL=IntlProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntlProvider.js","sourceRoot":"","sources":["../../../src/react/IntlProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"IntlProvider.js","sourceRoot":"","sources":["../../../src/react/IntlProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAY,SAAS,EAAC,MAAM,OAAO,CAAC;AAIlD,OAAO,EAAC,yBAAyB,EAAE,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAC3E,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AACxD,OAAO,WAAW,MAAM,eAAe,CAAC;AAaxC,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,EACnC,QAAQ,EACR,OAAO,GAAG,cAAc,EACxB,kBAAkB,GAAG,yBAAyB,EAC9C,QAAQ,EACR,GAAG,aAAa,EACV;IACN,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QACzC,sDAAsD;QACtD,SAAS,CAAC,GAAG,EAAE;YACb,IAAI,QAAQ,EAAE;gBACZ,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;aACrC;QACH,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;KACzB;IAED,OAAO,CACL,oBAAC,WAAW,CAAC,QAAQ,IACnB,KAAK,EAAE,EAAC,GAAG,aAAa,EAAE,QAAQ,EAAE,OAAO,EAAE,kBAAkB,EAAC,IAE/D,QAAQ,CACY,CACxB,CAAC;AACJ,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { default as IntlProvider } from './IntlProvider';
|
|
2
2
|
export { default as useTranslations } from './useTranslations';
|
|
3
|
-
export { default as useIntl } from './useIntl';
|
|
4
3
|
export { default as useLocale } from './useLocale';
|
|
5
4
|
export { default as useNow } from './useNow';
|
|
6
5
|
export { default as useTimeZone } from './useTimeZone';
|
|
6
|
+
export { default as useFormatter } from './useFormatter';
|
|
7
|
+
export { default as useIntl } from './useIntl';
|
package/dist/src/react/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { default as IntlProvider } from './IntlProvider';
|
|
2
2
|
export { default as useTranslations } from './useTranslations';
|
|
3
|
-
export { default as useIntl } from './useIntl';
|
|
4
3
|
export { default as useLocale } from './useLocale';
|
|
5
4
|
export { default as useNow } from './useNow';
|
|
6
5
|
export { default as useTimeZone } from './useTimeZone';
|
|
6
|
+
export { default as useFormatter } from './useFormatter';
|
|
7
|
+
// TODO: Remove in next major version
|
|
8
|
+
export { default as useIntl } from './useIntl';
|
|
7
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAC,OAAO,IAAI,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAC,OAAO,IAAI,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAC,OAAO,IAAI,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAC;AACjD,OAAO,EAAC,OAAO,IAAI,MAAM,EAAC,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAC,OAAO,IAAI,WAAW,EAAC,MAAM,eAAe,CAAC;AACrD,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAEvD,qCAAqC;AACrC,OAAO,EAAC,OAAO,IAAI,OAAO,EAAC,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export default function useFormatter(): {
|
|
2
|
+
dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
|
|
3
|
+
number: (value: number, formatOrOptions?: string | import("@formatjs/ecma402-abstract").NumberFormatOptions | undefined) => string;
|
|
4
|
+
relativeTime: (date: number | Date, now?: number | Date | undefined) => string;
|
|
5
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import createFormatter from '../core/createFormatter';
|
|
3
|
+
import useIntlContext from './useIntlContext';
|
|
4
|
+
export default function useFormatter() {
|
|
5
|
+
const { formats, locale, now: globalNow, onError, timeZone } = useIntlContext();
|
|
6
|
+
return useMemo(() => createFormatter({
|
|
7
|
+
formats,
|
|
8
|
+
locale,
|
|
9
|
+
now: globalNow,
|
|
10
|
+
onError,
|
|
11
|
+
timeZone
|
|
12
|
+
}), [formats, globalNow, locale, onError, timeZone]);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=useFormatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormatter.js","sourceRoot":"","sources":["../../../src/react/useFormatter.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,eAAe,MAAM,yBAAyB,CAAC;AACtD,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,YAAY;IAClC,MAAM,EAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,GAAG,cAAc,EAAE,CAAC;IAE9E,OAAO,OAAO,CACZ,GAAG,EAAE,CACH,eAAe,CAAC;QACd,OAAO;QACP,MAAM;QACN,GAAG,EAAE,SAAS;QACd,OAAO;QACP,QAAQ;KACT,CAAC,EACJ,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAChD,CAAC;AACJ,CAAC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** @deprecated Switch to `useFormatter` */
|
|
1
2
|
export default function useIntl(): {
|
|
2
3
|
formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
|
|
3
4
|
formatNumber: (value: number, formatOrOptions?: string | import("@formatjs/ecma402-abstract").NumberFormatOptions | undefined) => string;
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
2
|
import createIntl from '../core/createIntl';
|
|
3
3
|
import useIntlContext from './useIntlContext';
|
|
4
|
+
let hasWarned = false;
|
|
5
|
+
/** @deprecated Switch to `useFormatter` */
|
|
4
6
|
export default function useIntl() {
|
|
5
7
|
const { formats, locale, now: globalNow, onError, timeZone } = useIntlContext();
|
|
8
|
+
if (!hasWarned) {
|
|
9
|
+
hasWarned = true;
|
|
10
|
+
console.warn('`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.');
|
|
11
|
+
}
|
|
6
12
|
return useMemo(() => createIntl({
|
|
7
13
|
formats,
|
|
8
14
|
locale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIntl.js","sourceRoot":"","sources":["../../../src/react/useIntl.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,OAAO;IAC7B,MAAM,EAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,GAAG,cAAc,EAAE,CAAC;IAE9E,OAAO,OAAO,CACZ,GAAG,EAAE,CACH,UAAU,CAAC;QACT,OAAO;QACP,MAAM;QACN,GAAG,EAAE,SAAS;QACd,OAAO;QACP,QAAQ;KACT,CAAC,EACJ,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAChD,CAAC;AACJ,CAAC"}
|
|
1
|
+
{"version":3,"file":"useIntl.js","sourceRoot":"","sources":["../../../src/react/useIntl.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,IAAI,SAAS,GAAG,KAAK,CAAC;AAEtB,4CAA4C;AAC5C,MAAM,CAAC,OAAO,UAAU,OAAO;IAC7B,MAAM,EAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,GAAG,cAAc,EAAE,CAAC;IAE9E,IAAI,CAAC,SAAS,EAAE;QACd,SAAS,GAAG,IAAI,CAAC;QACjB,OAAO,CAAC,IAAI,CACV,6GAA6G,CAC9G,CAAC;KACH;IAED,OAAO,OAAO,CACZ,GAAG,EAAE,CACH,UAAU,CAAC;QACT,OAAO;QACP,MAAM;QACN,GAAG,EAAE,SAAS;QACd,OAAO;QACP,QAAQ;KACT,CAAC,EACJ,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAChD,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default function useIntlContext(): import("./
|
|
1
|
+
export default function useIntlContext(): import("./IntlContext").IntlContextShape;
|
|
@@ -323,7 +323,15 @@ function createBaseTranslator(_ref2) {
|
|
|
323
323
|
}
|
|
324
324
|
|
|
325
325
|
var messages = messagesOrError;
|
|
326
|
-
var
|
|
326
|
+
var message;
|
|
327
|
+
|
|
328
|
+
try {
|
|
329
|
+
message = resolvePath(messages, key, namespace);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
var cacheKey = [namespace, key, message].filter(function (part) {
|
|
327
335
|
return part != null;
|
|
328
336
|
}).join('.');
|
|
329
337
|
var messageFormat;
|
|
@@ -331,14 +339,6 @@ function createBaseTranslator(_ref2) {
|
|
|
331
339
|
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
332
340
|
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
333
341
|
} else {
|
|
334
|
-
var message;
|
|
335
|
-
|
|
336
|
-
try {
|
|
337
|
-
message = resolvePath(messages, key, namespace);
|
|
338
|
-
} catch (error) {
|
|
339
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
342
|
if (typeof message === 'object') {
|
|
343
343
|
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INSUFFICIENT_PATH, "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." );
|
|
344
344
|
}
|
|
@@ -427,13 +427,13 @@ function resolveNamespace(namespace, namespacePrefix) {
|
|
|
427
427
|
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
428
428
|
}
|
|
429
429
|
|
|
430
|
-
var _excluded$
|
|
430
|
+
var _excluded$2 = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
431
431
|
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
432
432
|
var getMessageFallback = _ref.getMessageFallback,
|
|
433
433
|
messages = _ref.messages,
|
|
434
434
|
namespace = _ref.namespace,
|
|
435
435
|
onError = _ref.onError,
|
|
436
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$
|
|
436
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
|
|
437
437
|
|
|
438
438
|
// The `namespacePrefix` is part of the type system.
|
|
439
439
|
// See the comment in the function invocation.
|
|
@@ -479,7 +479,7 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
479
479
|
return base;
|
|
480
480
|
}
|
|
481
481
|
|
|
482
|
-
var _excluded$
|
|
482
|
+
var _excluded$1 = ["onError", "getMessageFallback", "messages", "namespace"];
|
|
483
483
|
/**
|
|
484
484
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
485
485
|
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
@@ -496,7 +496,7 @@ function createTranslator(_ref) {
|
|
|
496
496
|
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
497
497
|
messages = _ref.messages,
|
|
498
498
|
namespace = _ref.namespace,
|
|
499
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$
|
|
499
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
500
500
|
|
|
501
501
|
// We have to wrap the actual function so the type inference for the optional
|
|
502
502
|
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
@@ -554,7 +554,7 @@ function getRelativeTimeFormatConfig(seconds) {
|
|
|
554
554
|
};
|
|
555
555
|
}
|
|
556
556
|
|
|
557
|
-
function
|
|
557
|
+
function createFormatter(_ref) {
|
|
558
558
|
var formats = _ref.formats,
|
|
559
559
|
locale = _ref.locale,
|
|
560
560
|
globalNow = _ref.now,
|
|
@@ -598,7 +598,7 @@ function createIntl(_ref) {
|
|
|
598
598
|
}
|
|
599
599
|
}
|
|
600
600
|
|
|
601
|
-
function
|
|
601
|
+
function dateTime(
|
|
602
602
|
/** If a number is supplied, this is interpreted as a UTC timestamp. */
|
|
603
603
|
value,
|
|
604
604
|
/** If a time zone is supplied, the `value` is converted to that time zone.
|
|
@@ -617,13 +617,13 @@ function createIntl(_ref) {
|
|
|
617
617
|
});
|
|
618
618
|
}
|
|
619
619
|
|
|
620
|
-
function
|
|
620
|
+
function number(value, formatOrOptions) {
|
|
621
621
|
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
|
|
622
622
|
return new Intl.NumberFormat(locale, options).format(value);
|
|
623
623
|
});
|
|
624
624
|
}
|
|
625
625
|
|
|
626
|
-
function
|
|
626
|
+
function relativeTime(
|
|
627
627
|
/** The date time that needs to be formatted. */
|
|
628
628
|
date,
|
|
629
629
|
/** The reference point in time to which `date` will be formatted in relation to. */
|
|
@@ -633,7 +633,7 @@ function createIntl(_ref) {
|
|
|
633
633
|
if (globalNow) {
|
|
634
634
|
now = globalNow;
|
|
635
635
|
} else {
|
|
636
|
-
throw new Error("development" !== 'production' ? "The `now` parameter wasn't provided
|
|
636
|
+
throw new Error("development" !== 'production' ? "The `now` parameter wasn't provided and there was no global fallback configured on the provider." : undefined);
|
|
637
637
|
}
|
|
638
638
|
}
|
|
639
639
|
|
|
@@ -655,13 +655,22 @@ function createIntl(_ref) {
|
|
|
655
655
|
}
|
|
656
656
|
|
|
657
657
|
return {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
658
|
+
dateTime: dateTime,
|
|
659
|
+
number: number,
|
|
660
|
+
relativeTime: relativeTime
|
|
661
661
|
};
|
|
662
662
|
}
|
|
663
663
|
|
|
664
|
-
|
|
664
|
+
/** @deprecated Switch to `createFormatter` */
|
|
665
|
+
|
|
666
|
+
function createIntl() {
|
|
667
|
+
var formatter = createFormatter.apply(void 0, arguments);
|
|
668
|
+
return {
|
|
669
|
+
formatDateTime: formatter.dateTime,
|
|
670
|
+
formatNumber: formatter.number,
|
|
671
|
+
formatRelativeTime: formatter.relativeTime
|
|
672
|
+
};
|
|
673
|
+
}
|
|
665
674
|
|
|
666
675
|
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
667
676
|
Object.entries(messages).forEach(function (_ref) {
|
|
@@ -691,40 +700,33 @@ function validateMessages(messages, onError) {
|
|
|
691
700
|
}
|
|
692
701
|
}
|
|
693
702
|
|
|
694
|
-
var
|
|
695
|
-
/**
|
|
696
|
-
* Enhances the incoming props with defaults.
|
|
697
|
-
*/
|
|
703
|
+
var IntlContext = /*#__PURE__*/React.createContext(undefined);
|
|
698
704
|
|
|
699
|
-
|
|
700
|
-
|
|
705
|
+
var _excluded = ["children", "onError", "getMessageFallback", "messages"];
|
|
706
|
+
function IntlProvider(_ref) {
|
|
707
|
+
var children = _ref.children,
|
|
708
|
+
_ref$onError = _ref.onError,
|
|
709
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
710
|
+
_ref$getMessageFallba = _ref.getMessageFallback,
|
|
711
|
+
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
701
712
|
messages = _ref.messages,
|
|
702
|
-
|
|
703
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
704
|
-
|
|
705
|
-
var finalOnError = onError || defaultOnError;
|
|
706
|
-
var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
|
|
713
|
+
contextValues = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
707
714
|
|
|
708
715
|
{
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
716
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
717
|
+
React.useEffect(function () {
|
|
718
|
+
if (messages) {
|
|
719
|
+
validateMessages(messages, onError);
|
|
720
|
+
}
|
|
721
|
+
}, [messages, onError]);
|
|
712
722
|
}
|
|
713
723
|
|
|
714
|
-
return _extends({}, rest, {
|
|
715
|
-
messages: messages,
|
|
716
|
-
onError: finalOnError,
|
|
717
|
-
getMessageFallback: finalGetMessageFallback
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
var _excluded = ["children"];
|
|
722
|
-
function IntlProvider(_ref) {
|
|
723
|
-
var children = _ref.children,
|
|
724
|
-
props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
725
|
-
|
|
726
724
|
return React__default["default"].createElement(IntlContext.Provider, {
|
|
727
|
-
value:
|
|
725
|
+
value: _extends({}, contextValues, {
|
|
726
|
+
messages: messages,
|
|
727
|
+
onError: onError,
|
|
728
|
+
getMessageFallback: getMessageFallback
|
|
729
|
+
})
|
|
728
730
|
}, children);
|
|
729
731
|
}
|
|
730
732
|
|
|
@@ -796,25 +798,6 @@ function useTranslations(namespace) {
|
|
|
796
798
|
namespace ? "!." + namespace : '!', '!');
|
|
797
799
|
}
|
|
798
800
|
|
|
799
|
-
function useIntl() {
|
|
800
|
-
var _useIntlContext = useIntlContext(),
|
|
801
|
-
formats = _useIntlContext.formats,
|
|
802
|
-
locale = _useIntlContext.locale,
|
|
803
|
-
globalNow = _useIntlContext.now,
|
|
804
|
-
onError = _useIntlContext.onError,
|
|
805
|
-
timeZone = _useIntlContext.timeZone;
|
|
806
|
-
|
|
807
|
-
return React.useMemo(function () {
|
|
808
|
-
return createIntl({
|
|
809
|
-
formats: formats,
|
|
810
|
-
locale: locale,
|
|
811
|
-
now: globalNow,
|
|
812
|
-
onError: onError,
|
|
813
|
-
timeZone: timeZone
|
|
814
|
-
});
|
|
815
|
-
}, [formats, globalNow, locale, onError, timeZone]);
|
|
816
|
-
}
|
|
817
|
-
|
|
818
801
|
function useLocale() {
|
|
819
802
|
return useIntlContext().locale;
|
|
820
803
|
}
|
|
@@ -868,10 +851,58 @@ function useTimeZone() {
|
|
|
868
851
|
return useIntlContext().timeZone;
|
|
869
852
|
}
|
|
870
853
|
|
|
854
|
+
function useFormatter() {
|
|
855
|
+
var _useIntlContext = useIntlContext(),
|
|
856
|
+
formats = _useIntlContext.formats,
|
|
857
|
+
locale = _useIntlContext.locale,
|
|
858
|
+
globalNow = _useIntlContext.now,
|
|
859
|
+
onError = _useIntlContext.onError,
|
|
860
|
+
timeZone = _useIntlContext.timeZone;
|
|
861
|
+
|
|
862
|
+
return React.useMemo(function () {
|
|
863
|
+
return createFormatter({
|
|
864
|
+
formats: formats,
|
|
865
|
+
locale: locale,
|
|
866
|
+
now: globalNow,
|
|
867
|
+
onError: onError,
|
|
868
|
+
timeZone: timeZone
|
|
869
|
+
});
|
|
870
|
+
}, [formats, globalNow, locale, onError, timeZone]);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
var hasWarned = false;
|
|
874
|
+
/** @deprecated Switch to `useFormatter` */
|
|
875
|
+
|
|
876
|
+
function useIntl() {
|
|
877
|
+
var _useIntlContext = useIntlContext(),
|
|
878
|
+
formats = _useIntlContext.formats,
|
|
879
|
+
locale = _useIntlContext.locale,
|
|
880
|
+
globalNow = _useIntlContext.now,
|
|
881
|
+
onError = _useIntlContext.onError,
|
|
882
|
+
timeZone = _useIntlContext.timeZone;
|
|
883
|
+
|
|
884
|
+
if (!hasWarned) {
|
|
885
|
+
hasWarned = true;
|
|
886
|
+
console.warn('`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.');
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
return React.useMemo(function () {
|
|
890
|
+
return createIntl({
|
|
891
|
+
formats: formats,
|
|
892
|
+
locale: locale,
|
|
893
|
+
now: globalNow,
|
|
894
|
+
onError: onError,
|
|
895
|
+
timeZone: timeZone
|
|
896
|
+
});
|
|
897
|
+
}, [formats, globalNow, locale, onError, timeZone]);
|
|
898
|
+
}
|
|
899
|
+
|
|
871
900
|
exports.IntlError = IntlError;
|
|
872
901
|
exports.IntlProvider = IntlProvider;
|
|
902
|
+
exports.createFormatter = createFormatter;
|
|
873
903
|
exports.createIntl = createIntl;
|
|
874
904
|
exports.createTranslator = createTranslator;
|
|
905
|
+
exports.useFormatter = useFormatter;
|
|
875
906
|
exports.useIntl = useIntl;
|
|
876
907
|
exports.useLocale = useLocale;
|
|
877
908
|
exports.useNow = useNow;
|