use-intl 2.20.1 → 2.21.0
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/dist/core/IntlError.d.ts +1 -0
- package/dist/core/RelativeTimeFormatOptions.d.ts +5 -0
- package/dist/core/createFormatter.d.ts +3 -2
- package/dist/core/createIntl.d.ts +1 -1
- package/dist/core/use-intl.esm.js +1 -0
- package/dist/core/use-intl.esm.js.map +1 -1
- package/dist/core/use-intl.esm3.js +65 -42
- package/dist/core/use-intl.esm3.js.map +1 -1
- package/dist/react/useFormatter.d.ts +1 -1
- package/dist/react/useIntl.d.ts +1 -1
- package/dist/src/core/IntlError.d.ts +1 -0
- package/dist/src/core/IntlError.js +1 -0
- package/dist/src/core/IntlError.js.map +1 -1
- package/dist/src/core/RelativeTimeFormatOptions.d.ts +5 -0
- package/dist/src/core/RelativeTimeFormatOptions.js +2 -0
- package/dist/src/core/RelativeTimeFormatOptions.js.map +1 -0
- package/dist/src/core/createFormatter.d.ts +3 -2
- package/dist/src/core/createFormatter.js +73 -39
- package/dist/src/core/createFormatter.js.map +1 -1
- package/dist/src/core/createIntl.d.ts +1 -1
- package/dist/src/react/useFormatter.d.ts +1 -1
- package/dist/src/react/useIntl.d.ts +1 -1
- package/dist/use-intl.cjs.development.js +66 -42
- 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/package.json +2 -2
- package/src/core/IntlError.tsx +1 -0
- package/src/core/RelativeTimeFormatOptions.tsx +6 -0
- package/src/core/createFormatter.tsx +92 -41
package/dist/core/IntlError.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare enum IntlErrorCode {
|
|
2
2
|
MISSING_MESSAGE = "MISSING_MESSAGE",
|
|
3
3
|
MISSING_FORMAT = "MISSING_FORMAT",
|
|
4
|
+
ENVIRONMENT_FALLBACK = "ENVIRONMENT_FALLBACK",
|
|
4
5
|
INSUFFICIENT_PATH = "INSUFFICIENT_PATH",
|
|
5
6
|
INVALID_MESSAGE = "INVALID_MESSAGE",
|
|
6
7
|
INVALID_KEY = "INVALID_KEY",
|
|
@@ -2,6 +2,7 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlError from './IntlError';
|
|
4
4
|
import NumberFormatOptions from './NumberFormatOptions';
|
|
5
|
+
import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
|
|
5
6
|
import TimeZone from './TimeZone';
|
|
6
7
|
type Props = {
|
|
7
8
|
locale: string;
|
|
@@ -10,10 +11,10 @@ type Props = {
|
|
|
10
11
|
formats?: Partial<Formats>;
|
|
11
12
|
now?: Date;
|
|
12
13
|
};
|
|
13
|
-
export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone }: Props): {
|
|
14
|
+
export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
|
|
14
15
|
dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
|
|
15
16
|
number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
|
|
16
|
-
relativeTime: (date: number | Date,
|
|
17
|
+
relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions) => string;
|
|
17
18
|
list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions) => string;
|
|
18
19
|
};
|
|
19
20
|
export {};
|
|
@@ -3,5 +3,5 @@ import createFormatter from './createFormatter';
|
|
|
3
3
|
export default function createIntl(...args: Parameters<typeof createFormatter>): {
|
|
4
4
|
formatDateTime: (value: number | Date, formatOrOptions?: string | import("./DateTimeFormatOptions").default | undefined) => string;
|
|
5
5
|
formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
|
|
6
|
-
formatRelativeTime: (date: number | Date,
|
|
6
|
+
formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("./RelativeTimeFormatOptions").default | undefined) => string;
|
|
7
7
|
};
|
|
@@ -4,6 +4,7 @@ var IntlErrorCode;
|
|
|
4
4
|
(function (IntlErrorCode) {
|
|
5
5
|
IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
|
|
6
6
|
IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
|
|
7
|
+
IntlErrorCode["ENVIRONMENT_FALLBACK"] = "ENVIRONMENT_FALLBACK";
|
|
7
8
|
IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
|
|
8
9
|
IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
|
|
9
10
|
IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
|
|
@@ -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,
|
|
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 ENVIRONMENT_FALLBACK = 'ENVIRONMENT_FALLBACK',\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,cAQX;AARD,CAAA,UAAYA,aAAa,EAAA;AACvBA,EAAAA,aAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC,CAAA;AACnCA,EAAAA,aAAA,CAAA,gBAAA,CAAA,GAAA,gBAAiC,CAAA;AACjCA,EAAAA,aAAA,CAAA,sBAAA,CAAA,GAAA,sBAA6C,CAAA;AAC7CA,EAAAA,aAAA,CAAA,mBAAA,CAAA,GAAA,mBAAuC,CAAA;AACvCA,EAAAA,aAAA,CAAA,iBAAA,CAAA,GAAA,iBAAmC,CAAA;AACnCA,EAAAA,aAAA,CAAA,aAAA,CAAA,GAAA,aAA2B,CAAA;AAC3BA,EAAAA,aAAA,CAAA,kBAAA,CAAA,GAAA,kBAAqC,CAAA;AACvC,CAAC,EARWA,aAAa,KAAbA,aAAa,GAQxB,EAAA,CAAA,CAAA,CAAA;AAEoBC,IAAAA,SAAU,0BAAAC,MAAA,EAAA;EAAAC,cAAA,CAAAF,SAAA,EAAAC,MAAA,CAAA,CAAA;AAI7B,EAAA,SAAAD,SAAYG,CAAAA,IAAmB,EAAEC,eAAwB,EAAA;AAAA,IAAA,IAAAC,KAAA,CAAA;IACvD,IAAIC,OAAO,GAAWH,IAAI,CAAA;AAC1B,IAAA,IAAIC,eAAe,EAAE;MACnBE,OAAO,IAAI,IAAI,GAAGF,eAAe,CAAA;AAClC,KAAA;AACDC,IAAAA,KAAA,GAAAJ,MAAA,CAAAM,IAAA,CAAA,IAAA,EAAMD,OAAO,CAAC,IAAA,IAAA,CAAA;AAACD,IAAAA,KAAA,CARDF,IAAI,GAAA,KAAA,CAAA,CAAA;AAAAE,IAAAA,KAAA,CACJD,eAAe,GAAA,KAAA,CAAA,CAAA;IAS7BC,KAAA,CAAKF,IAAI,GAAGA,IAAI,CAAA;AAChB,IAAA,IAAIC,eAAe,EAAE;MACnBC,KAAA,CAAKD,eAAe,GAAGA,eAAe,CAAA;AACvC,KAAA;AAAA,IAAA,OAAAC,KAAA,CAAA;AACH,GAAA;AAAC,EAAA,OAAAL,SAAA,CAAA;AAAA,CAAAQ,eAAAA,gBAAA,CAfoCC,KAAK,CAAA;;;;"}
|
|
@@ -2,43 +2,53 @@ import { extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
|
2
2
|
import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
3
3
|
import { defaultOnError } from './use-intl.esm6.js';
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var SECOND = 1;
|
|
6
|
+
var MINUTE = SECOND * 60;
|
|
6
7
|
var HOUR = MINUTE * 60;
|
|
7
8
|
var DAY = HOUR * 24;
|
|
8
9
|
var WEEK = DAY * 7;
|
|
9
10
|
var MONTH = DAY * (365 / 12); // Approximation
|
|
11
|
+
var QUARTER = MONTH * 3;
|
|
10
12
|
var YEAR = DAY * 365;
|
|
11
|
-
|
|
13
|
+
var UNIT_SECONDS = {
|
|
14
|
+
second: SECOND,
|
|
15
|
+
seconds: SECOND,
|
|
16
|
+
minute: MINUTE,
|
|
17
|
+
minutes: MINUTE,
|
|
18
|
+
hour: HOUR,
|
|
19
|
+
hours: HOUR,
|
|
20
|
+
day: DAY,
|
|
21
|
+
days: DAY,
|
|
22
|
+
week: WEEK,
|
|
23
|
+
weeks: WEEK,
|
|
24
|
+
month: MONTH,
|
|
25
|
+
months: MONTH,
|
|
26
|
+
quarter: QUARTER,
|
|
27
|
+
quarters: QUARTER,
|
|
28
|
+
year: YEAR,
|
|
29
|
+
years: YEAR
|
|
30
|
+
};
|
|
31
|
+
function resolveRelativeTimeUnit(seconds) {
|
|
12
32
|
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
33
|
if (absValue < MINUTE) {
|
|
17
|
-
|
|
18
|
-
value = Math.round(seconds);
|
|
34
|
+
return 'second';
|
|
19
35
|
} else if (absValue < HOUR) {
|
|
20
|
-
|
|
21
|
-
value = Math.round(seconds / MINUTE);
|
|
36
|
+
return 'minute';
|
|
22
37
|
} else if (absValue < DAY) {
|
|
23
|
-
|
|
24
|
-
value = Math.round(seconds / HOUR);
|
|
38
|
+
return 'hour';
|
|
25
39
|
} else if (absValue < WEEK) {
|
|
26
|
-
|
|
27
|
-
value = Math.round(seconds / DAY);
|
|
40
|
+
return 'day';
|
|
28
41
|
} else if (absValue < MONTH) {
|
|
29
|
-
|
|
30
|
-
value = Math.round(seconds / WEEK);
|
|
42
|
+
return 'week';
|
|
31
43
|
} else if (absValue < YEAR) {
|
|
32
|
-
|
|
33
|
-
value = Math.round(seconds / MONTH);
|
|
34
|
-
} else {
|
|
35
|
-
unit = 'year';
|
|
36
|
-
value = Math.round(seconds / YEAR);
|
|
44
|
+
return 'month';
|
|
37
45
|
}
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
46
|
+
return 'year';
|
|
47
|
+
}
|
|
48
|
+
function calculateRelativeTimeValue(seconds, unit) {
|
|
49
|
+
// We have to round the resulting values, as `Intl.RelativeTimeFormat`
|
|
50
|
+
// will include fractions like '2.1 hours ago'.
|
|
51
|
+
return Math.round(seconds / UNIT_SECONDS[unit]);
|
|
42
52
|
}
|
|
43
53
|
function createFormatter(_ref) {
|
|
44
54
|
var formats = _ref.formats,
|
|
@@ -46,7 +56,7 @@ function createFormatter(_ref) {
|
|
|
46
56
|
globalNow = _ref.now,
|
|
47
57
|
_ref$onError = _ref.onError,
|
|
48
58
|
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
49
|
-
|
|
59
|
+
globalTimeZone = _ref.timeZone;
|
|
50
60
|
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
|
|
51
61
|
var options;
|
|
52
62
|
if (typeof formatOrOptions === 'string') {
|
|
@@ -83,10 +93,14 @@ function createFormatter(_ref) {
|
|
|
83
93
|
formatOrOptions) {
|
|
84
94
|
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
|
|
85
95
|
var _options;
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
if (!((_options = options) != null && _options.timeZone)) {
|
|
97
|
+
if (globalTimeZone) {
|
|
98
|
+
options = _extends({}, options, {
|
|
99
|
+
timeZone: globalTimeZone
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production' ? "The `timeZone` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone" : undefined));
|
|
103
|
+
}
|
|
90
104
|
}
|
|
91
105
|
return new Intl.DateTimeFormat(locale, options).format(value);
|
|
92
106
|
});
|
|
@@ -96,23 +110,32 @@ function createFormatter(_ref) {
|
|
|
96
110
|
return new Intl.NumberFormat(locale, options).format(value);
|
|
97
111
|
});
|
|
98
112
|
}
|
|
113
|
+
function getGlobalNow() {
|
|
114
|
+
if (globalNow) {
|
|
115
|
+
return globalNow;
|
|
116
|
+
} else {
|
|
117
|
+
onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production' ? "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" : undefined));
|
|
118
|
+
return new Date();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function extractNowDate(nowOrOptions) {
|
|
122
|
+
if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
|
|
123
|
+
return new Date(nowOrOptions);
|
|
124
|
+
}
|
|
125
|
+
if ((nowOrOptions == null ? void 0 : nowOrOptions.now) !== undefined) {
|
|
126
|
+
return new Date(nowOrOptions.now);
|
|
127
|
+
}
|
|
128
|
+
return getGlobalNow();
|
|
129
|
+
}
|
|
99
130
|
function relativeTime( /** The date time that needs to be formatted. */
|
|
100
131
|
date, /** The reference point in time to which `date` will be formatted in relation to. */
|
|
101
|
-
|
|
132
|
+
nowOrOptions) {
|
|
102
133
|
try {
|
|
103
|
-
|
|
104
|
-
|
|
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);
|
|
134
|
+
var dateDate = new Date(date);
|
|
135
|
+
var nowDate = extractNowDate(nowOrOptions);
|
|
112
136
|
var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
|
|
113
|
-
var
|
|
114
|
-
|
|
115
|
-
value = _getRelativeTimeForma.value;
|
|
137
|
+
var unit = typeof nowOrOptions === 'number' || nowOrOptions instanceof Date || (nowOrOptions == null ? void 0 : nowOrOptions.unit) === undefined ? resolveRelativeTimeUnit(seconds) : nowOrOptions.unit;
|
|
138
|
+
var value = calculateRelativeTimeValue(seconds, unit);
|
|
116
139
|
return new Intl.RelativeTimeFormat(locale, {
|
|
117
140
|
numeric: 'auto'
|
|
118
141
|
}).format(value, unit);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createFormatter.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport TimeZone from './TimeZone';\nimport {defaultOnError} from './defaults';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\ntype Props = {\n locale: string;\n timeZone?: TimeZone;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createFormatter({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone\n}: Props) {\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n process.env.NODE_ENV !== 'production'\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function dateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function number(\n value: number | bigint,\n formatOrOptions?: string | NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function relativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `The \\`now\\` parameter wasn't provided and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n function list(\n value: Iterable<string>,\n formatOrOptions?: string | Intl.ListFormatOptions\n ) {\n return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>\n new Intl.ListFormat(locale, options).format(value)\n );\n }\n\n return {dateTime, number, relativeTime, list};\n}\n"],"names":["MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","value","unit","round","createFormatter","_ref","formats","locale","globalNow","now","_ref$onError","onError","defaultOnError","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","process","env","NODE_ENV","undefined","getFormattedValue","formatter","String","FORMATTING_ERROR","message","dateTime","_options","_extends","Intl","DateTimeFormat","format","number","NumberFormat","relativeTime","date","Error","dateDate","Date","nowDate","getTime","_getRelativeTimeForma","RelativeTimeFormat","numeric","list","ListFormat"],"mappings":";;;;AAOA,IAAMA,MAAM,GAAG,EAAE,CAAA;AACjB,IAAMC,IAAI,GAAGD,MAAM,GAAG,EAAE,CAAA;AACxB,IAAME,GAAG,GAAGD,IAAI,GAAG,EAAE,CAAA;AACrB,IAAME,IAAI,GAAGD,GAAG,GAAG,CAAC,CAAA;AACpB,IAAME,KAAK,GAAGF,GAAG,IAAI,GAAG,GAAG,EAAE,CAAC,CAAC;AAC/B,IAAMG,IAAI,GAAGH,GAAG,GAAG,GAAG,CAAA;AAEtB,SAASI,2BAA2BA,CAACC,OAAe,EAAA;AAClD,EAAA,IAAMC,QAAQ,GAAGC,IAAI,CAACC,GAAG,CAACH,OAAO,CAAC,CAAA;EAClC,IAAII,KAAK,EAAEC,IAAiC,CAAA;AAE5C;AACA;EAEA,IAAIJ,Q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,CAAA;MACD,OAAOF,MAAM,CAACY,IAAI,CAAC,CAAA;AACpB,KAAA;AACH,GAAA;AAEA,EAAA,SAASS,IAAIA,CACXjD,KAAuB,EACvBe,eAAiD,EAAA;AAEjD,IAAA,OAAOW,iBAAiB,CAAC1B,KAAK,EAAEe,eAAe,EAAEV,OAAO,IAAA,IAAA,GAAA,KAAA,CAAA,GAAPA,OAAO,CAAE4C,IAAI,EAAE,UAACjC,OAAO,EAAA;AAAA,MAAA,OACtE,IAAIkB,IAAI,CAACgB,UAAU,CAAC5C,MAAM,EAAEU,OAAO,CAAC,CAACoB,MAAM,CAACpC,KAAK,CAAC,CAAA;KACnD,CAAA,CAAA;AACH,GAAA;EAEA,OAAO;AAAC+B,IAAAA,QAAQ,EAARA,QAAQ;AAAEM,IAAAA,MAAM,EAANA,MAAM;AAAEE,IAAAA,YAAY,EAAZA,YAAY;AAAEU,IAAAA,IAAI,EAAJA,IAAAA;GAAK,CAAA;AAC/C;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createFormatter.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport RelativeTimeFormatOptions from './RelativeTimeFormatOptions';\nimport TimeZone from './TimeZone';\nimport {defaultOnError} from './defaults';\n\nconst SECOND = 1;\nconst MINUTE = SECOND * 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst QUARTER = MONTH * 3;\nconst YEAR = DAY * 365;\n\nconst UNIT_SECONDS: Record<Intl.RelativeTimeFormatUnit, number> = {\n second: SECOND,\n seconds: SECOND,\n minute: MINUTE,\n minutes: MINUTE,\n hour: HOUR,\n hours: HOUR,\n day: DAY,\n days: DAY,\n week: WEEK,\n weeks: WEEK,\n month: MONTH,\n months: MONTH,\n quarter: QUARTER,\n quarters: QUARTER,\n year: YEAR,\n years: YEAR\n} as const;\n\nfunction resolveRelativeTimeUnit(seconds: number) {\n const absValue = Math.abs(seconds);\n\n if (absValue < MINUTE) {\n return 'second';\n } else if (absValue < HOUR) {\n return 'minute';\n } else if (absValue < DAY) {\n return 'hour';\n } else if (absValue < WEEK) {\n return 'day';\n } else if (absValue < MONTH) {\n return 'week';\n } else if (absValue < YEAR) {\n return 'month';\n }\n return 'year';\n}\n\nfunction calculateRelativeTimeValue(\n seconds: number,\n unit: Intl.RelativeTimeFormatUnit\n) {\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n return Math.round(seconds / UNIT_SECONDS[unit]);\n}\n\ntype Props = {\n locale: string;\n timeZone?: TimeZone;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createFormatter({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone: globalTimeZone\n}: Props) {\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n process.env.NODE_ENV !== 'production'\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function dateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (!options?.timeZone) {\n if (globalTimeZone) {\n options = {...options, timeZone: globalTimeZone};\n } else {\n onError(\n new IntlError(\n IntlErrorCode.ENVIRONMENT_FALLBACK,\n process.env.NODE_ENV !== 'production'\n ? `The \\`timeZone\\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone`\n : undefined\n )\n );\n }\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function number(\n value: number | bigint,\n formatOrOptions?: string | NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function getGlobalNow() {\n if (globalNow) {\n return globalNow;\n } else {\n onError(\n new IntlError(\n IntlErrorCode.ENVIRONMENT_FALLBACK,\n process.env.NODE_ENV !== 'production'\n ? `The \\`now\\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`\n : undefined\n )\n );\n return new Date();\n }\n }\n\n function extractNowDate(\n nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions\n ) {\n if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {\n return new Date(nowOrOptions);\n }\n if (nowOrOptions?.now !== undefined) {\n return new Date(nowOrOptions.now);\n }\n return getGlobalNow();\n }\n\n function relativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions\n ) {\n try {\n const dateDate = new Date(date);\n const nowDate = extractNowDate(nowOrOptions);\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n\n const unit =\n typeof nowOrOptions === 'number' ||\n nowOrOptions instanceof Date ||\n nowOrOptions?.unit === undefined\n ? resolveRelativeTimeUnit(seconds)\n : nowOrOptions.unit;\n\n const value = calculateRelativeTimeValue(seconds, unit);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n function list(\n value: Iterable<string>,\n formatOrOptions?: string | Intl.ListFormatOptions\n ) {\n return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>\n new Intl.ListFormat(locale, options).format(value)\n );\n }\n\n return {dateTime, number, relativeTime, list};\n}\n"],"names":["SECOND","MINUTE","HOUR","DAY","WEEK","MONTH","QUARTER","YEAR","UNIT_SECONDS","second","seconds","minute","minutes","hour","hours","day","days","week","weeks","month","months","quarter","quarters","year","years","resolveRelativeTimeUnit","absValue","Math","abs","calculateRelativeTimeValue","unit","round","createFormatter","_ref","formats","locale","globalNow","now","_ref$onError","onError","defaultOnError","globalTimeZone","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","process","env","NODE_ENV","undefined","getFormattedValue","value","formatter","String","FORMATTING_ERROR","message","dateTime","_options","_extends","ENVIRONMENT_FALLBACK","Intl","DateTimeFormat","format","number","NumberFormat","getGlobalNow","Date","extractNowDate","nowOrOptions","relativeTime","date","dateDate","nowDate","getTime","RelativeTimeFormat","numeric","list","ListFormat"],"mappings":";;;;AAQA,IAAMA,MAAM,GAAG,CAAC,CAAA;AAChB,IAAMC,MAAM,GAAGD,MAAM,GAAG,EAAE,CAAA;AAC1B,IAAME,IAAI,GAAGD,MAAM,GAAG,EAAE,CAAA;AACxB,IAAME,GAAG,GAAGD,IAAI,GAAG,EAAE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AA;AAEA,EAAA,SAASM,IAAIA,CACXzB,KAAuB,EACvBZ,eAAiD,EAAA;AAEjD,IAAA,OAAOW,iBAAiB,CAACC,KAAK,EAAEZ,eAAe,EAAEX,OAAO,IAAA,IAAA,GAAA,KAAA,CAAA,GAAPA,OAAO,CAAEgD,IAAI,EAAE,UAACpC,OAAO,EAAA;AAAA,MAAA,OACtE,IAAIoB,IAAI,CAACiB,UAAU,CAAChD,MAAM,EAAEW,OAAO,CAAC,CAACsB,MAAM,CAACX,KAAK,CAAC,CAAA;KACnD,CAAA,CAAA;AACH,GAAA;EAEA,OAAO;AAACK,IAAAA,QAAQ,EAARA,QAAQ;AAAEO,IAAAA,MAAM,EAANA,MAAM;AAAEM,IAAAA,YAAY,EAAZA,YAAY;AAAEO,IAAAA,IAAI,EAAJA,IAAAA;GAAK,CAAA;AAC/C;;;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export default function useFormatter(): {
|
|
2
2
|
dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
|
|
3
3
|
number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
|
|
4
|
-
relativeTime: (date: number | Date,
|
|
4
|
+
relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
|
|
5
5
|
list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
|
|
6
6
|
};
|
package/dist/react/useIntl.d.ts
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
export default function useIntl(): {
|
|
3
3
|
formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
|
|
4
4
|
formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
|
|
5
|
-
formatRelativeTime: (date: number | Date,
|
|
5
|
+
formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
|
|
6
6
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare enum IntlErrorCode {
|
|
2
2
|
MISSING_MESSAGE = "MISSING_MESSAGE",
|
|
3
3
|
MISSING_FORMAT = "MISSING_FORMAT",
|
|
4
|
+
ENVIRONMENT_FALLBACK = "ENVIRONMENT_FALLBACK",
|
|
4
5
|
INSUFFICIENT_PATH = "INSUFFICIENT_PATH",
|
|
5
6
|
INVALID_MESSAGE = "INVALID_MESSAGE",
|
|
6
7
|
INVALID_KEY = "INVALID_KEY",
|
|
@@ -2,6 +2,7 @@ export var IntlErrorCode;
|
|
|
2
2
|
(function (IntlErrorCode) {
|
|
3
3
|
IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
|
|
4
4
|
IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
|
|
5
|
+
IntlErrorCode["ENVIRONMENT_FALLBACK"] = "ENVIRONMENT_FALLBACK";
|
|
5
6
|
IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
|
|
6
7
|
IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
|
|
7
8
|
IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntlError.js","sourceRoot":"","sources":["../../../src/core/IntlError.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,
|
|
1
|
+
{"version":3,"file":"IntlError.js","sourceRoot":"","sources":["../../../src/core/IntlError.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,aAQX;AARD,WAAY,aAAa;IACvB,oDAAmC,CAAA;IACnC,kDAAiC,CAAA;IACjC,8DAA6C,CAAA;IAC7C,wDAAuC,CAAA;IACvC,oDAAmC,CAAA;IACnC,4CAA2B,CAAA;IAC3B,sDAAqC,CAAA;AACvC,CAAC,EARW,aAAa,KAAb,aAAa,QAQxB;AAED,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,KAAK;IAI1C,YAAY,IAAmB,EAAE,eAAwB;QACvD,IAAI,OAAO,GAAW,IAAI,CAAC;QAC3B,IAAI,eAAe,EAAE;YACnB,OAAO,IAAI,IAAI,GAAG,eAAe,CAAC;SACnC;QACD,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,eAAe,EAAE;YACnB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;SACxC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RelativeTimeFormatOptions.js","sourceRoot":"","sources":["../../../src/core/RelativeTimeFormatOptions.tsx"],"names":[],"mappings":""}
|
|
@@ -2,6 +2,7 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlError from './IntlError';
|
|
4
4
|
import NumberFormatOptions from './NumberFormatOptions';
|
|
5
|
+
import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
|
|
5
6
|
import TimeZone from './TimeZone';
|
|
6
7
|
type Props = {
|
|
7
8
|
locale: string;
|
|
@@ -10,10 +11,10 @@ type Props = {
|
|
|
10
11
|
formats?: Partial<Formats>;
|
|
11
12
|
now?: Date;
|
|
12
13
|
};
|
|
13
|
-
export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone }: Props): {
|
|
14
|
+
export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
|
|
14
15
|
dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
|
|
15
16
|
number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
|
|
16
|
-
relativeTime: (date: number | Date,
|
|
17
|
+
relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions) => string;
|
|
17
18
|
list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions) => string;
|
|
18
19
|
};
|
|
19
20
|
export {};
|
|
@@ -1,47 +1,59 @@
|
|
|
1
1
|
import IntlError, { IntlErrorCode } from './IntlError';
|
|
2
2
|
import { defaultOnError } from './defaults';
|
|
3
|
-
const
|
|
3
|
+
const SECOND = 1;
|
|
4
|
+
const MINUTE = SECOND * 60;
|
|
4
5
|
const HOUR = MINUTE * 60;
|
|
5
6
|
const DAY = HOUR * 24;
|
|
6
7
|
const WEEK = DAY * 7;
|
|
7
8
|
const MONTH = DAY * (365 / 12); // Approximation
|
|
9
|
+
const QUARTER = MONTH * 3;
|
|
8
10
|
const YEAR = DAY * 365;
|
|
9
|
-
|
|
11
|
+
const UNIT_SECONDS = {
|
|
12
|
+
second: SECOND,
|
|
13
|
+
seconds: SECOND,
|
|
14
|
+
minute: MINUTE,
|
|
15
|
+
minutes: MINUTE,
|
|
16
|
+
hour: HOUR,
|
|
17
|
+
hours: HOUR,
|
|
18
|
+
day: DAY,
|
|
19
|
+
days: DAY,
|
|
20
|
+
week: WEEK,
|
|
21
|
+
weeks: WEEK,
|
|
22
|
+
month: MONTH,
|
|
23
|
+
months: MONTH,
|
|
24
|
+
quarter: QUARTER,
|
|
25
|
+
quarters: QUARTER,
|
|
26
|
+
year: YEAR,
|
|
27
|
+
years: YEAR
|
|
28
|
+
};
|
|
29
|
+
function resolveRelativeTimeUnit(seconds) {
|
|
10
30
|
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
31
|
if (absValue < MINUTE) {
|
|
15
|
-
|
|
16
|
-
value = Math.round(seconds);
|
|
32
|
+
return 'second';
|
|
17
33
|
}
|
|
18
34
|
else if (absValue < HOUR) {
|
|
19
|
-
|
|
20
|
-
value = Math.round(seconds / MINUTE);
|
|
35
|
+
return 'minute';
|
|
21
36
|
}
|
|
22
37
|
else if (absValue < DAY) {
|
|
23
|
-
|
|
24
|
-
value = Math.round(seconds / HOUR);
|
|
38
|
+
return 'hour';
|
|
25
39
|
}
|
|
26
40
|
else if (absValue < WEEK) {
|
|
27
|
-
|
|
28
|
-
value = Math.round(seconds / DAY);
|
|
41
|
+
return 'day';
|
|
29
42
|
}
|
|
30
43
|
else if (absValue < MONTH) {
|
|
31
|
-
|
|
32
|
-
value = Math.round(seconds / WEEK);
|
|
44
|
+
return 'week';
|
|
33
45
|
}
|
|
34
46
|
else if (absValue < YEAR) {
|
|
35
|
-
|
|
36
|
-
value = Math.round(seconds / MONTH);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
unit = 'year';
|
|
40
|
-
value = Math.round(seconds / YEAR);
|
|
47
|
+
return 'month';
|
|
41
48
|
}
|
|
42
|
-
return
|
|
49
|
+
return 'year';
|
|
50
|
+
}
|
|
51
|
+
function calculateRelativeTimeValue(seconds, unit) {
|
|
52
|
+
// We have to round the resulting values, as `Intl.RelativeTimeFormat`
|
|
53
|
+
// will include fractions like '2.1 hours ago'.
|
|
54
|
+
return Math.round(seconds / UNIT_SECONDS[unit]);
|
|
43
55
|
}
|
|
44
|
-
export default function createFormatter({ formats, locale, now: globalNow, onError = defaultOnError, timeZone }) {
|
|
56
|
+
export default function createFormatter({ formats, locale, now: globalNow, onError = defaultOnError, timeZone: globalTimeZone }) {
|
|
45
57
|
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
|
|
46
58
|
let options;
|
|
47
59
|
if (typeof formatOrOptions === 'string') {
|
|
@@ -83,8 +95,15 @@ export default function createFormatter({ formats, locale, now: globalNow, onErr
|
|
|
83
95
|
* Otherwise the user time zone will be used. */
|
|
84
96
|
formatOrOptions) {
|
|
85
97
|
return getFormattedValue(value, formatOrOptions, formats?.dateTime, (options) => {
|
|
86
|
-
if (
|
|
87
|
-
|
|
98
|
+
if (!options?.timeZone) {
|
|
99
|
+
if (globalTimeZone) {
|
|
100
|
+
options = { ...options, timeZone: globalTimeZone };
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production'
|
|
104
|
+
? `The \`timeZone\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone`
|
|
105
|
+
: undefined));
|
|
106
|
+
}
|
|
88
107
|
}
|
|
89
108
|
return new Intl.DateTimeFormat(locale, options).format(value);
|
|
90
109
|
});
|
|
@@ -92,26 +111,41 @@ export default function createFormatter({ formats, locale, now: globalNow, onErr
|
|
|
92
111
|
function number(value, formatOrOptions) {
|
|
93
112
|
return getFormattedValue(value, formatOrOptions, formats?.number, (options) => new Intl.NumberFormat(locale, options).format(value));
|
|
94
113
|
}
|
|
114
|
+
function getGlobalNow() {
|
|
115
|
+
if (globalNow) {
|
|
116
|
+
return globalNow;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production'
|
|
120
|
+
? `The \`now\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`
|
|
121
|
+
: undefined));
|
|
122
|
+
return new Date();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function extractNowDate(nowOrOptions) {
|
|
126
|
+
if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
|
|
127
|
+
return new Date(nowOrOptions);
|
|
128
|
+
}
|
|
129
|
+
if (nowOrOptions?.now !== undefined) {
|
|
130
|
+
return new Date(nowOrOptions.now);
|
|
131
|
+
}
|
|
132
|
+
return getGlobalNow();
|
|
133
|
+
}
|
|
95
134
|
function relativeTime(
|
|
96
135
|
/** The date time that needs to be formatted. */
|
|
97
136
|
date,
|
|
98
137
|
/** The reference point in time to which `date` will be formatted in relation to. */
|
|
99
|
-
|
|
138
|
+
nowOrOptions) {
|
|
100
139
|
try {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
now = globalNow;
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
throw new Error(process.env.NODE_ENV !== 'production'
|
|
107
|
-
? `The \`now\` parameter wasn't provided 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);
|
|
140
|
+
const dateDate = new Date(date);
|
|
141
|
+
const nowDate = extractNowDate(nowOrOptions);
|
|
113
142
|
const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
|
|
114
|
-
const
|
|
143
|
+
const unit = typeof nowOrOptions === 'number' ||
|
|
144
|
+
nowOrOptions instanceof Date ||
|
|
145
|
+
nowOrOptions?.unit === undefined
|
|
146
|
+
? resolveRelativeTimeUnit(seconds)
|
|
147
|
+
: nowOrOptions.unit;
|
|
148
|
+
const value = calculateRelativeTimeValue(seconds, unit);
|
|
115
149
|
return new Intl.RelativeTimeFormat(locale, {
|
|
116
150
|
numeric: 'auto'
|
|
117
151
|
}).format(value, unit);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormatter.js","sourceRoot":"","sources":["../../../src/core/createFormatter.tsx"],"names":[],"mappings":"AAEA,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"createFormatter.js","sourceRoot":"","sources":["../../../src/core/createFormatter.tsx"],"names":[],"mappings":"AAEA,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAIrD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAC;AAE1C,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,MAAM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AO,KAAK,EAAE;YACd,OAAO,CACL,IAAI,SAAS,CAAC,aAAa,CAAC,gBAAgB,EAAG,KAAe,CAAC,OAAO,CAAC,CACxE,CAAC;YACF,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;SACrB;IACH,CAAC;IAED,SAAS,IAAI,CACX,KAAuB,EACvB,eAAiD;QAEjD,OAAO,iBAAiB,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAC1E,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CACnD,CAAC;IACJ,CAAC;IAED,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAC,CAAC;AAChD,CAAC"}
|
|
@@ -3,5 +3,5 @@ import createFormatter from './createFormatter';
|
|
|
3
3
|
export default function createIntl(...args: Parameters<typeof createFormatter>): {
|
|
4
4
|
formatDateTime: (value: number | Date, formatOrOptions?: string | import("./DateTimeFormatOptions").default | undefined) => string;
|
|
5
5
|
formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
|
|
6
|
-
formatRelativeTime: (date: number | Date,
|
|
6
|
+
formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("./RelativeTimeFormatOptions").default | undefined) => string;
|
|
7
7
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export default function useFormatter(): {
|
|
2
2
|
dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
|
|
3
3
|
number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
|
|
4
|
-
relativeTime: (date: number | Date,
|
|
4
|
+
relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
|
|
5
5
|
list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
|
|
6
6
|
};
|
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
export default function useIntl(): {
|
|
3
3
|
formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
|
|
4
4
|
formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
|
|
5
|
-
formatRelativeTime: (date: number | Date,
|
|
5
|
+
formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
|
|
6
6
|
};
|