@formatjs/intl 4.0.7 → 4.0.9
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/index.d.ts +19 -15
- package/index.js +14 -13
- package/package.json +5 -5
- package/src/create-intl.d.ts +11 -7
- package/src/create-intl.js +62 -44
- package/src/dateTime.d.ts +32 -32
- package/src/dateTime.js +79 -110
- package/src/displayName.d.ts +5 -5
- package/src/displayName.js +22 -21
- package/src/error.d.ts +19 -19
- package/src/error.js +68 -91
- package/src/list.d.ts +7 -7
- package/src/list.js +47 -57
- package/src/message.d.ts +14 -14
- package/src/message.js +103 -96
- package/src/number.d.ts +14 -14
- package/src/number.js +46 -54
- package/src/plural.d.ts +5 -5
- package/src/plural.js +18 -18
- package/src/relativeTime.d.ts +5 -5
- package/src/relativeTime.js +26 -27
- package/src/types.d.ts +84 -80
- package/src/types.js +3 -1
- package/src/utils.d.ts +10 -7
- package/src/utils.js +112 -155
package/index.d.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import { MessageDescriptor } from
|
|
2
|
-
export * from
|
|
3
|
-
export declare function defineMessages<
|
|
1
|
+
import { type MessageDescriptor } from "./src/types.js";
|
|
2
|
+
export * from "./src/types.js";
|
|
3
|
+
export declare function defineMessages<
|
|
4
|
+
K extends keyof any,
|
|
5
|
+
T = MessageDescriptor,
|
|
6
|
+
U extends Record<K, T> = Record<K, T>
|
|
7
|
+
>(msgs: U): U;
|
|
4
8
|
export declare function defineMessage<T>(msg: T): T;
|
|
5
|
-
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat
|
|
6
|
-
export * from
|
|
7
|
-
export { formatMessage } from
|
|
8
|
-
export type { FormatMessageFn } from
|
|
9
|
-
export { formatDate, formatDateToParts, formatTime, formatTimeToParts
|
|
10
|
-
export { formatDisplayName } from
|
|
11
|
-
export { formatList } from
|
|
12
|
-
export { formatPlural } from
|
|
13
|
-
export { formatRelativeTime } from
|
|
14
|
-
export { formatNumber, formatNumberToParts } from
|
|
15
|
-
export { createIntl } from
|
|
16
|
-
export type { CreateIntlFn } from
|
|
9
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat } from "./src/utils.js";
|
|
10
|
+
export * from "./src/error.js";
|
|
11
|
+
export { formatMessage } from "./src/message.js";
|
|
12
|
+
export type { FormatMessageFn } from "./src/message.js";
|
|
13
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts } from "./src/dateTime.js";
|
|
14
|
+
export { formatDisplayName } from "./src/displayName.js";
|
|
15
|
+
export { formatList } from "./src/list.js";
|
|
16
|
+
export { formatPlural } from "./src/plural.js";
|
|
17
|
+
export { formatRelativeTime } from "./src/relativeTime.js";
|
|
18
|
+
export { formatNumber, formatNumberToParts } from "./src/number.js";
|
|
19
|
+
export { createIntl } from "./src/create-intl.js";
|
|
20
|
+
export type { CreateIntlFn } from "./src/create-intl.js";
|
package/index.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import "./src/types.js";
|
|
2
|
+
export * from "./src/types.js";
|
|
2
3
|
export function defineMessages(msgs) {
|
|
3
|
-
|
|
4
|
+
return msgs;
|
|
4
5
|
}
|
|
5
6
|
export function defineMessage(msg) {
|
|
6
|
-
|
|
7
|
+
return msg;
|
|
7
8
|
}
|
|
8
|
-
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat
|
|
9
|
-
export * from
|
|
10
|
-
export { formatMessage } from
|
|
11
|
-
export { formatDate, formatDateToParts, formatTime, formatTimeToParts
|
|
12
|
-
export { formatDisplayName } from
|
|
13
|
-
export { formatList } from
|
|
14
|
-
export { formatPlural } from
|
|
15
|
-
export { formatRelativeTime } from
|
|
16
|
-
export { formatNumber, formatNumberToParts } from
|
|
17
|
-
export { createIntl } from
|
|
9
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat } from "./src/utils.js";
|
|
10
|
+
export * from "./src/error.js";
|
|
11
|
+
export { formatMessage } from "./src/message.js";
|
|
12
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts } from "./src/dateTime.js";
|
|
13
|
+
export { formatDisplayName } from "./src/displayName.js";
|
|
14
|
+
export { formatList } from "./src/list.js";
|
|
15
|
+
export { formatPlural } from "./src/plural.js";
|
|
16
|
+
export { formatRelativeTime } from "./src/relativeTime.js";
|
|
17
|
+
export { formatNumber, formatNumberToParts } from "./src/number.js";
|
|
18
|
+
export { createIntl } from "./src/create-intl.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/intl",
|
|
3
3
|
"description": "Internationalize JS apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.",
|
|
4
|
-
"version": "4.0.
|
|
4
|
+
"version": "4.0.9",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Long Ho <holevietlong@gmail.com>",
|
|
7
7
|
"type": "module",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"tslib": "^2.8.0",
|
|
15
|
-
"@formatjs/ecma402-abstract": "3.0.
|
|
16
|
-
"@formatjs/fast-memoize": "3.0.
|
|
17
|
-
"@formatjs/icu-messageformat-parser": "3.
|
|
18
|
-
"intl-messageformat": "11.0.
|
|
15
|
+
"@formatjs/ecma402-abstract": "3.0.8",
|
|
16
|
+
"@formatjs/fast-memoize": "3.0.3",
|
|
17
|
+
"@formatjs/icu-messageformat-parser": "3.3.0",
|
|
18
|
+
"intl-messageformat": "11.0.9"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"typescript": "^5.6.0"
|
package/src/create-intl.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { IntlCache, IntlConfig, IntlShape } from
|
|
2
|
-
export interface CreateIntlFn<
|
|
3
|
-
|
|
1
|
+
import { type IntlCache, type IntlConfig, type IntlShape } from "./types.js";
|
|
2
|
+
export interface CreateIntlFn<
|
|
3
|
+
T = string,
|
|
4
|
+
C extends IntlConfig<T> = IntlConfig<T>,
|
|
5
|
+
S extends IntlShape<T> = IntlShape<T>
|
|
6
|
+
> {
|
|
7
|
+
(config: C, cache?: IntlCache): S;
|
|
4
8
|
}
|
|
5
9
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
* Create intl object
|
|
11
|
+
* @param config intl config
|
|
12
|
+
* @param cache cache for formatter instances to prevent memory leak
|
|
13
|
+
*/
|
|
10
14
|
export declare function createIntl<T = string>(config: IntlConfig<T>, cache?: IntlCache): IntlShape<T>;
|
package/src/create-intl.js
CHANGED
|
@@ -1,51 +1,69 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts
|
|
3
|
-
import { formatDisplayName } from
|
|
4
|
-
import { InvalidConfigError, MissingDataError } from
|
|
5
|
-
import { formatList, formatListToParts } from
|
|
6
|
-
import { formatMessage } from
|
|
7
|
-
import { formatNumber, formatNumberToParts } from
|
|
8
|
-
import { formatPlural } from
|
|
9
|
-
import { formatRelativeTime } from
|
|
10
|
-
import
|
|
1
|
+
import "@formatjs/icu-messageformat-parser";
|
|
2
|
+
import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts } from "./dateTime.js";
|
|
3
|
+
import { formatDisplayName } from "./displayName.js";
|
|
4
|
+
import { InvalidConfigError, MissingDataError } from "./error.js";
|
|
5
|
+
import { formatList, formatListToParts } from "./list.js";
|
|
6
|
+
import { formatMessage } from "./message.js";
|
|
7
|
+
import { formatNumber, formatNumberToParts } from "./number.js";
|
|
8
|
+
import { formatPlural } from "./plural.js";
|
|
9
|
+
import { formatRelativeTime } from "./relativeTime.js";
|
|
10
|
+
import "./types.js";
|
|
11
|
+
import { createFormatters, DEFAULT_INTL_CONFIG } from "./utils.js";
|
|
11
12
|
function messagesContainString(messages) {
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
|
|
14
|
+
return typeof firstMessage === "string";
|
|
14
15
|
}
|
|
15
16
|
function verifyConfigMessages(config) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
if (config.onWarn && config.defaultRichTextElements && messagesContainString(config.messages || {})) {
|
|
18
|
+
config.onWarn(`[@formatjs/intl] "defaultRichTextElements" was specified but "message" was not pre-compiled.
|
|
19
|
+
Please consider using "@formatjs/cli" to pre-compile your messages for performance.
|
|
20
|
+
For more details see https://formatjs.github.io/docs/getting-started/message-distribution`);
|
|
21
|
+
}
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
* Create intl object
|
|
25
|
+
* @param config intl config
|
|
26
|
+
* @param cache cache for formatter instances to prevent memory leak
|
|
27
|
+
*/
|
|
27
28
|
export function createIntl(config, cache) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
29
|
+
const formatters = createFormatters(cache);
|
|
30
|
+
const resolvedConfig = {
|
|
31
|
+
...DEFAULT_INTL_CONFIG,
|
|
32
|
+
...config
|
|
33
|
+
};
|
|
34
|
+
const { locale, defaultLocale, onError } = resolvedConfig;
|
|
35
|
+
if (!locale) {
|
|
36
|
+
if (onError) {
|
|
37
|
+
onError(new InvalidConfigError(`"locale" was not configured, using "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details`));
|
|
38
|
+
}
|
|
39
|
+
// Since there's no registered locale data for `locale`, this will
|
|
40
|
+
// fallback to the `defaultLocale` to make sure things can render.
|
|
41
|
+
// The `messages` are overridden to the `defaultProps` empty object
|
|
42
|
+
// to maintain referential equality across re-renders. It's assumed
|
|
43
|
+
// each <FormattedMessage> contains a `defaultMessage` prop.
|
|
44
|
+
resolvedConfig.locale = resolvedConfig.defaultLocale || "en";
|
|
45
|
+
} else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
|
|
46
|
+
onError(new MissingDataError(`Missing locale data for locale: "${locale}" in Intl.NumberFormat. Using default locale: "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details`));
|
|
47
|
+
} else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length && onError) {
|
|
48
|
+
onError(new MissingDataError(`Missing locale data for locale: "${locale}" in Intl.DateTimeFormat. Using default locale: "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details`));
|
|
49
|
+
}
|
|
50
|
+
verifyConfigMessages(resolvedConfig);
|
|
51
|
+
return {
|
|
52
|
+
...resolvedConfig,
|
|
53
|
+
formatters,
|
|
54
|
+
formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat),
|
|
55
|
+
formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat),
|
|
56
|
+
formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat),
|
|
57
|
+
formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat),
|
|
58
|
+
formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat),
|
|
59
|
+
formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat),
|
|
60
|
+
formatDateTimeRange: formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat),
|
|
61
|
+
formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat),
|
|
62
|
+
formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules),
|
|
63
|
+
formatMessage: formatMessage.bind(null, resolvedConfig, formatters),
|
|
64
|
+
$t: formatMessage.bind(null, resolvedConfig, formatters),
|
|
65
|
+
formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat),
|
|
66
|
+
formatListToParts: formatListToParts.bind(null, resolvedConfig, formatters.getListFormat),
|
|
67
|
+
formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames)
|
|
68
|
+
};
|
|
51
69
|
}
|
package/src/dateTime.d.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from
|
|
2
|
-
export declare function getFormatter({ locale, formats, onError, timeZone
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}, type:
|
|
1
|
+
import { type CustomFormats, type Formatters, type IntlFormatters, type OnErrorFn } from "./types.js";
|
|
2
|
+
export declare function getFormatter({ locale, formats, onError, timeZone }: {
|
|
3
|
+
locale: string;
|
|
4
|
+
timeZone?: string;
|
|
5
|
+
formats: CustomFormats;
|
|
6
|
+
onError: OnErrorFn;
|
|
7
|
+
}, type: "date" | "time" | "dateTimeRange", getDateTimeFormat: Formatters["getDateTimeFormat"], options?: Parameters<IntlFormatters["formatDate"]>[1]): Intl.DateTimeFormat;
|
|
8
8
|
export declare function formatDate(config: {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}, getDateTimeFormat: Formatters[
|
|
9
|
+
locale: string;
|
|
10
|
+
timeZone?: string;
|
|
11
|
+
formats: CustomFormats;
|
|
12
|
+
onError: OnErrorFn;
|
|
13
|
+
}, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatDate"]>[0], options?: Parameters<IntlFormatters["formatDate"]>[1]): string;
|
|
14
14
|
export declare function formatTime(config: {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}, getDateTimeFormat: Formatters[
|
|
15
|
+
locale: string;
|
|
16
|
+
timeZone?: string;
|
|
17
|
+
formats: CustomFormats;
|
|
18
|
+
onError: OnErrorFn;
|
|
19
|
+
}, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatTime"]>[0], options?: Parameters<IntlFormatters["formatTime"]>[1]): string;
|
|
20
20
|
export declare function formatDateTimeRange(config: {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}, getDateTimeFormat: Formatters[
|
|
21
|
+
locale: string;
|
|
22
|
+
timeZone?: string;
|
|
23
|
+
formats: CustomFormats;
|
|
24
|
+
onError: OnErrorFn;
|
|
25
|
+
}, getDateTimeFormat: Formatters["getDateTimeFormat"], from: Parameters<IntlFormatters["formatDateTimeRange"]>[0], to: Parameters<IntlFormatters["formatDateTimeRange"]>[1], options?: Parameters<IntlFormatters["formatDateTimeRange"]>[2]): string;
|
|
26
26
|
export declare function formatDateToParts(config: {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}, getDateTimeFormat: Formatters[
|
|
27
|
+
locale: string;
|
|
28
|
+
timeZone?: string;
|
|
29
|
+
formats: CustomFormats;
|
|
30
|
+
onError: OnErrorFn;
|
|
31
|
+
}, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatDate"]>[0], options?: Parameters<IntlFormatters["formatDate"]>[1]): Intl.DateTimeFormatPart[];
|
|
32
32
|
export declare function formatTimeToParts(config: {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}, getDateTimeFormat: Formatters[
|
|
33
|
+
locale: string;
|
|
34
|
+
timeZone?: string;
|
|
35
|
+
formats: CustomFormats;
|
|
36
|
+
onError: OnErrorFn;
|
|
37
|
+
}, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatTimeToParts"]>[0], options?: Parameters<IntlFormatters["formatTimeToParts"]>[1]): Intl.DateTimeFormatPart[];
|
package/src/dateTime.js
CHANGED
|
@@ -1,117 +1,86 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { IntlFormatError } from
|
|
3
|
-
import { filterProps, getNamedFormat } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
'fractionalSecondDigits',
|
|
1
|
+
import "./types.js";
|
|
2
|
+
import { IntlFormatError } from "./error.js";
|
|
3
|
+
import { filterProps, getNamedFormat } from "./utils.js";
|
|
4
|
+
const DATE_TIME_FORMAT_OPTIONS = [
|
|
5
|
+
"formatMatcher",
|
|
6
|
+
"timeZone",
|
|
7
|
+
"hour12",
|
|
8
|
+
"weekday",
|
|
9
|
+
"era",
|
|
10
|
+
"year",
|
|
11
|
+
"month",
|
|
12
|
+
"day",
|
|
13
|
+
"hour",
|
|
14
|
+
"minute",
|
|
15
|
+
"second",
|
|
16
|
+
"timeZoneName",
|
|
17
|
+
"hourCycle",
|
|
18
|
+
"dateStyle",
|
|
19
|
+
"timeStyle",
|
|
20
|
+
"calendar",
|
|
21
|
+
"numberingSystem",
|
|
22
|
+
"fractionalSecondDigits"
|
|
24
23
|
];
|
|
25
|
-
export function getFormatter(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
24
|
+
export function getFormatter({ locale, formats, onError, timeZone }, type, getDateTimeFormat, options = {}) {
|
|
25
|
+
const { format } = options;
|
|
26
|
+
const defaults = {
|
|
27
|
+
...timeZone && { timeZone },
|
|
28
|
+
...format && getNamedFormat(formats, type, format, onError)
|
|
29
|
+
};
|
|
30
|
+
let filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
|
|
31
|
+
if (type === "time" && !filteredOptions.hour && !filteredOptions.minute && !filteredOptions.second && !filteredOptions.timeStyle && !filteredOptions.dateStyle) {
|
|
32
|
+
// Add default formatting options if hour, minute, or second isn't defined.
|
|
33
|
+
filteredOptions = {
|
|
34
|
+
...filteredOptions,
|
|
35
|
+
hour: "numeric",
|
|
36
|
+
minute: "numeric"
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return getDateTimeFormat(locale, filteredOptions);
|
|
41
40
|
}
|
|
42
|
-
export function formatDate(config, getDateTimeFormat) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
|
|
51
|
-
}
|
|
52
|
-
catch (e) {
|
|
53
|
-
config.onError(new IntlFormatError('Error formatting date.', config.locale, e));
|
|
54
|
-
}
|
|
55
|
-
return String(date);
|
|
41
|
+
export function formatDate(config, getDateTimeFormat, value, options = {}) {
|
|
42
|
+
const date = typeof value === "string" ? new Date(value || 0) : value;
|
|
43
|
+
try {
|
|
44
|
+
return getFormatter(config, "date", getDateTimeFormat, options).format(date);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
config.onError(new IntlFormatError("Error formatting date.", config.locale, e));
|
|
47
|
+
}
|
|
48
|
+
return String(date);
|
|
56
49
|
}
|
|
57
|
-
export function formatTime(config, getDateTimeFormat) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
|
|
66
|
-
}
|
|
67
|
-
catch (e) {
|
|
68
|
-
config.onError(new IntlFormatError('Error formatting time.', config.locale, e));
|
|
69
|
-
}
|
|
70
|
-
return String(date);
|
|
50
|
+
export function formatTime(config, getDateTimeFormat, value, options = {}) {
|
|
51
|
+
const date = typeof value === "string" ? new Date(value || 0) : value;
|
|
52
|
+
try {
|
|
53
|
+
return getFormatter(config, "time", getDateTimeFormat, options).format(date);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
config.onError(new IntlFormatError("Error formatting time.", config.locale, e));
|
|
56
|
+
}
|
|
57
|
+
return String(date);
|
|
71
58
|
}
|
|
72
|
-
export function formatDateTimeRange(config, getDateTimeFormat) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate);
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
config.onError(new IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
85
|
-
}
|
|
86
|
-
return String(fromDate);
|
|
59
|
+
export function formatDateTimeRange(config, getDateTimeFormat, from, to, options = {}) {
|
|
60
|
+
const fromDate = typeof from === "string" ? new Date(from || 0) : from;
|
|
61
|
+
const toDate = typeof to === "string" ? new Date(to || 0) : to;
|
|
62
|
+
try {
|
|
63
|
+
return getFormatter(config, "dateTimeRange", getDateTimeFormat, options).formatRange(fromDate, toDate);
|
|
64
|
+
} catch (e) {
|
|
65
|
+
config.onError(new IntlFormatError("Error formatting date time range.", config.locale, e));
|
|
66
|
+
}
|
|
67
|
+
return String(fromDate);
|
|
87
68
|
}
|
|
88
|
-
export function formatDateToParts(config, getDateTimeFormat) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged
|
|
97
|
-
}
|
|
98
|
-
catch (e) {
|
|
99
|
-
config.onError(new IntlFormatError('Error formatting date.', config.locale, e));
|
|
100
|
-
}
|
|
101
|
-
return [];
|
|
69
|
+
export function formatDateToParts(config, getDateTimeFormat, value, options = {}) {
|
|
70
|
+
const date = typeof value === "string" ? new Date(value || 0) : value;
|
|
71
|
+
try {
|
|
72
|
+
return getFormatter(config, "date", getDateTimeFormat, options).formatToParts(date);
|
|
73
|
+
} catch (e) {
|
|
74
|
+
config.onError(new IntlFormatError("Error formatting date.", config.locale, e));
|
|
75
|
+
}
|
|
76
|
+
return [];
|
|
102
77
|
}
|
|
103
|
-
export function formatTimeToParts(config, getDateTimeFormat) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged
|
|
112
|
-
}
|
|
113
|
-
catch (e) {
|
|
114
|
-
config.onError(new IntlFormatError('Error formatting time.', config.locale, e));
|
|
115
|
-
}
|
|
116
|
-
return [];
|
|
78
|
+
export function formatTimeToParts(config, getDateTimeFormat, value, options = {}) {
|
|
79
|
+
const date = typeof value === "string" ? new Date(value || 0) : value;
|
|
80
|
+
try {
|
|
81
|
+
return getFormatter(config, "time", getDateTimeFormat, options).formatToParts(date);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
config.onError(new IntlFormatError("Error formatting time.", config.locale, e));
|
|
84
|
+
}
|
|
85
|
+
return [];
|
|
117
86
|
}
|
package/src/displayName.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters, OnErrorFn } from
|
|
2
|
-
export declare function formatDisplayName({ locale, onError
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}, getDisplayNames: Formatters[
|
|
1
|
+
import { type Formatters, type IntlFormatters, type OnErrorFn } from "./types.js";
|
|
2
|
+
export declare function formatDisplayName({ locale, onError }: {
|
|
3
|
+
locale: string;
|
|
4
|
+
onError: OnErrorFn;
|
|
5
|
+
}, getDisplayNames: Formatters["getDisplayNames"], value: Parameters<IntlFormatters["formatDisplayName"]>[0], options: Parameters<IntlFormatters["formatDisplayName"]>[1]): string | undefined;
|
package/src/displayName.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import "./types.js";
|
|
2
|
+
import { filterProps } from "./utils.js";
|
|
3
|
+
import { ErrorCode, FormatError } from "intl-messageformat";
|
|
4
|
+
import { IntlFormatError } from "./error.js";
|
|
5
|
+
const DISPLAY_NAMES_OPTONS = [
|
|
6
|
+
"style",
|
|
7
|
+
"type",
|
|
8
|
+
"fallback",
|
|
9
|
+
"languageDisplay"
|
|
9
10
|
];
|
|
10
|
-
export function formatDisplayName(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
export function formatDisplayName({ locale, onError }, getDisplayNames, value, options) {
|
|
12
|
+
const DisplayNames = Intl.DisplayNames;
|
|
13
|
+
if (!DisplayNames) {
|
|
14
|
+
onError(new FormatError(`Intl.DisplayNames is not available in this environment.
|
|
15
|
+
Try polyfilling it using "@formatjs/intl-displaynames"
|
|
16
|
+
`, ErrorCode.MISSING_INTL_API));
|
|
17
|
+
}
|
|
18
|
+
const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
|
|
19
|
+
try {
|
|
20
|
+
return getDisplayNames(locale, filteredOptions).of(value);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
onError(new IntlFormatError("Error formatting display name.", locale, e));
|
|
23
|
+
}
|
|
23
24
|
}
|