@formatjs/intl 2.10.15 → 3.0.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/README.md +1 -1
- package/lib/src/create-intl.d.ts +1 -1
- package/lib/src/create-intl.js +11 -15
- package/lib/src/dateTime.d.ts +2 -3
- package/lib/src/dateTime.js +1 -1
- package/lib/src/displayName.js +1 -1
- package/lib/src/list.d.ts +1 -2
- package/lib/src/list.js +4 -4
- package/lib/src/message.d.ts +2 -2
- package/lib/src/message.js +4 -4
- package/lib/src/number.js +4 -0
- package/lib/src/plural.d.ts +1 -2
- package/lib/src/plural.js +2 -2
- package/lib/src/types.d.ts +16 -15
- package/lib/src/utils.d.ts +3 -3
- package/lib/src/utils.js +7 -1
- package/package.json +5 -12
- package/src/create-intl.d.ts +1 -1
- package/src/create-intl.js +11 -15
- package/src/dateTime.d.ts +2 -3
- package/src/dateTime.js +1 -1
- package/src/list.d.ts +1 -2
- package/src/list.js +3 -3
- package/src/message.d.ts +2 -2
- package/src/message.js +3 -3
- package/src/number.js +4 -0
- package/src/plural.d.ts +1 -2
- package/src/plural.js +2 -2
- package/src/types.d.ts +16 -15
- package/src/utils.d.ts +3 -3
- package/src/utils.js +8 -1
package/README.md
CHANGED
package/lib/src/create-intl.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IntlCache,
|
|
1
|
+
import { IntlCache, IntlConfig, IntlShape } from './types';
|
|
2
2
|
export interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
|
|
3
3
|
(config: C, cache?: IntlCache): S;
|
|
4
4
|
}
|
package/lib/src/create-intl.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
|
-
import {
|
|
2
|
+
import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts, } from './dateTime';
|
|
3
|
+
import { formatDisplayName } from './displayName';
|
|
3
4
|
import { InvalidConfigError, MissingDataError } from './error';
|
|
5
|
+
import { formatList, formatListToParts } from './list';
|
|
6
|
+
import { formatMessage } from './message';
|
|
4
7
|
import { formatNumber, formatNumberToParts } from './number';
|
|
5
|
-
import { formatRelativeTime } from './relativeTime';
|
|
6
|
-
import { formatDate, formatDateToParts, formatTime, formatTimeToParts, formatDateTimeRange, } from './dateTime';
|
|
7
8
|
import { formatPlural } from './plural';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { formatDisplayName } from './displayName';
|
|
9
|
+
import { formatRelativeTime } from './relativeTime';
|
|
10
|
+
import { createFormatters, DEFAULT_INTL_CONFIG } from './utils';
|
|
11
11
|
function messagesContainString(messages) {
|
|
12
12
|
var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
|
|
13
13
|
return typeof firstMessage === 'string';
|
|
@@ -16,7 +16,7 @@ function verifyConfigMessages(config) {
|
|
|
16
16
|
if (config.onWarn &&
|
|
17
17
|
config.defaultRichTextElements &&
|
|
18
18
|
messagesContainString(config.messages || {})) {
|
|
19
|
-
config.onWarn("[@formatjs/intl] \"defaultRichTextElements\" was specified but \"message\" was not pre-compiled. \nPlease consider using \"@formatjs/cli\" to pre-compile your messages for performance.\nFor more details see https://formatjs.io/docs/getting-started/message-distribution");
|
|
19
|
+
config.onWarn("[@formatjs/intl] \"defaultRichTextElements\" was specified but \"message\" was not pre-compiled. \nPlease consider using \"@formatjs/cli\" to pre-compile your messages for performance.\nFor more details see https://formatjs.github.io/docs/getting-started/message-distribution");
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
@@ -30,7 +30,7 @@ export function createIntl(config, cache) {
|
|
|
30
30
|
var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
|
|
31
31
|
if (!locale) {
|
|
32
32
|
if (onError) {
|
|
33
|
-
onError(new InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl/api#intlshape for more details")));
|
|
33
|
+
onError(new InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details")));
|
|
34
34
|
}
|
|
35
35
|
// Since there's no registered locale data for `locale`, this will
|
|
36
36
|
// fallback to the `defaultLocale` to make sure things can render.
|
|
@@ -40,16 +40,12 @@ export function createIntl(config, cache) {
|
|
|
40
40
|
resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
|
|
41
41
|
}
|
|
42
42
|
else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
|
|
43
|
-
onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl#runtime-requirements for more details")));
|
|
43
|
+
onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
|
|
44
44
|
}
|
|
45
45
|
else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
|
|
46
46
|
onError) {
|
|
47
|
-
onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl#runtime-requirements for more details")));
|
|
47
|
+
onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
|
|
48
48
|
}
|
|
49
49
|
verifyConfigMessages(resolvedConfig);
|
|
50
|
-
return __assign(__assign({}, resolvedConfig), { formatters: formatters, formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules),
|
|
51
|
-
// @ts-expect-error TODO: will get to this later
|
|
52
|
-
formatMessage: formatMessage.bind(null, resolvedConfig, formatters),
|
|
53
|
-
// @ts-expect-error TODO: will get to this later
|
|
54
|
-
$t: formatMessage.bind(null, resolvedConfig, formatters), formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat), formatListToParts: formatListToParts.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
|
|
50
|
+
return __assign(__assign({}, resolvedConfig), { formatters: formatters, formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: formatMessage.bind(null, resolvedConfig, formatters), $t: formatMessage.bind(null, resolvedConfig, formatters), formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat), formatListToParts: formatListToParts.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
|
|
55
51
|
}
|
package/lib/src/dateTime.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters,
|
|
2
|
-
import { DateTimeFormat } from '@formatjs/ecma402-abstract';
|
|
1
|
+
import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
3
2
|
export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
4
3
|
locale: string;
|
|
5
4
|
timeZone?: string;
|
|
6
5
|
formats: CustomFormats;
|
|
7
6
|
onError: OnErrorFn;
|
|
8
|
-
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): DateTimeFormat;
|
|
7
|
+
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
9
8
|
export declare function formatDate(config: {
|
|
10
9
|
locale: string;
|
|
11
10
|
timeZone?: string;
|
package/lib/src/dateTime.js
CHANGED
package/lib/src/displayName.js
CHANGED
package/lib/src/list.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
-
import type { Part } from '@formatjs/intl-listformat';
|
|
1
|
+
import { Formatters, IntlFormatters, OnErrorFn, Part } from './types';
|
|
3
2
|
export declare function formatList(opts: {
|
|
4
3
|
locale: string;
|
|
5
4
|
onError: OnErrorFn;
|
package/lib/src/list.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
|
-
import {
|
|
3
|
-
import { FormatError, ErrorCode } from 'intl-messageformat';
|
|
2
|
+
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
4
3
|
import { IntlFormatError } from './error';
|
|
4
|
+
import { filterProps } from './utils';
|
|
5
5
|
var LIST_FORMAT_OPTIONS = [
|
|
6
6
|
'type',
|
|
7
7
|
'style',
|
|
@@ -48,9 +48,9 @@ export function formatListToParts(_a, getListFormat, values, options) {
|
|
|
48
48
|
return getListFormat(locale, filteredOptions)
|
|
49
49
|
.formatToParts(serializedValues)
|
|
50
50
|
.map(function (part) {
|
|
51
|
-
return part.type === 'literal'
|
|
51
|
+
return (part.type === 'literal'
|
|
52
52
|
? part
|
|
53
|
-
: __assign(__assign({}, part), { value: richValues_1[part.value] || part.value });
|
|
53
|
+
: __assign(__assign({}, part), { value: richValues_1[part.value] || part.value }));
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
catch (e) {
|
package/lib/src/message.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Formatters, MessageDescriptor,
|
|
2
|
-
import { FormatXMLElementFn, PrimitiveType, Formatters as IntlMessageFormatFormatters, Options } from 'intl-messageformat';
|
|
1
|
+
import { CustomFormats, Formatters, MessageDescriptor, OnErrorFn } from './types';
|
|
3
2
|
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
|
+
import { FormatXMLElementFn, Formatters as IntlMessageFormatFormatters, Options, PrimitiveType } from 'intl-messageformat';
|
|
4
4
|
export type FormatMessageFn<T> = ({ locale, formats, messages, defaultLocale, defaultFormats, fallbackOnEmptyString, onError, timeZone, defaultRichTextElements, }: {
|
|
5
5
|
locale: string;
|
|
6
6
|
timeZone?: string;
|
package/lib/src/message.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
|
-
import { invariant } from '@formatjs/ecma402-abstract';
|
|
3
|
-
import { IntlMessageFormat, } from 'intl-messageformat';
|
|
4
|
-
import { MissingTranslationError, MessageFormatError } from './error';
|
|
5
2
|
import { TYPE } from '@formatjs/icu-messageformat-parser';
|
|
3
|
+
import { IntlMessageFormat, } from 'intl-messageformat';
|
|
4
|
+
import { MessageFormatError, MissingTranslationError } from './error';
|
|
5
|
+
import { invariant } from './utils';
|
|
6
6
|
function setTimeZoneInOptions(opts, timeZone) {
|
|
7
7
|
return Object.keys(opts).reduce(function (all, k) {
|
|
8
8
|
all[k] = __assign({ timeZone: timeZone }, opts[k]);
|
|
@@ -28,7 +28,7 @@ export var formatMessage = function (_a, state, messageDescriptor, values, opts)
|
|
|
28
28
|
if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
|
|
29
29
|
var msgId = messageDescriptor.id, defaultMessage = messageDescriptor.defaultMessage;
|
|
30
30
|
// `id` is a required field of a Message Descriptor.
|
|
31
|
-
invariant(!!msgId, "[@formatjs/intl] An `id` must be provided to format a message. You can either:\n1. Configure your build toolchain with [babel-plugin-formatjs](https://formatjs.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
|
|
31
|
+
invariant(!!msgId, "[@formatjs/intl] An `id` must be provided to format a message. You can either:\n1. Configure your build toolchain with [babel-plugin-formatjs](https://formatjs.github.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.github.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.github.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
|
|
32
32
|
var id = String(msgId);
|
|
33
33
|
var message =
|
|
34
34
|
// In case messages is Object.create(null)
|
package/lib/src/number.js
CHANGED
|
@@ -21,9 +21,13 @@ var NUMBER_FORMAT_OPTIONS = [
|
|
|
21
21
|
'unitDisplay',
|
|
22
22
|
'numberingSystem',
|
|
23
23
|
// ES2023 NumberFormat
|
|
24
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
24
25
|
'trailingZeroDisplay',
|
|
26
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
25
27
|
'roundingPriority',
|
|
28
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
26
29
|
'roundingIncrement',
|
|
30
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
27
31
|
'roundingMode',
|
|
28
32
|
];
|
|
29
33
|
export function getFormatter(_a, getNumberFormat, options) {
|
package/lib/src/plural.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
-
import { LDMLPluralRule } from '@formatjs/ecma402-abstract';
|
|
3
2
|
export declare function formatPlural({ locale, onError, }: {
|
|
4
3
|
locale: string;
|
|
5
4
|
onError: OnErrorFn;
|
|
6
|
-
}, getPluralRules: Formatters['getPluralRules'], value: Parameters<IntlFormatters['formatPlural']>[0], options?: Parameters<IntlFormatters['formatPlural']>[1]): LDMLPluralRule;
|
|
5
|
+
}, getPluralRules: Formatters['getPluralRules'], value: Parameters<IntlFormatters['formatPlural']>[0], options?: Parameters<IntlFormatters['formatPlural']>[1]): Intl.LDMLPluralRule;
|
package/lib/src/plural.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { filterProps } from './utils';
|
|
2
|
-
import { IntlFormatError } from './error';
|
|
3
1
|
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
2
|
+
import { IntlFormatError } from './error';
|
|
3
|
+
import { filterProps } from './utils';
|
|
4
4
|
var PLURAL_FORMAT_OPTIONS = ['type'];
|
|
5
5
|
export function formatPlural(_a, getPluralRules, value, options) {
|
|
6
6
|
var locale = _a.locale, onError = _a.onError;
|
package/lib/src/types.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { DateTimeFormat, NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
1
|
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
|
-
import { DisplayNames, DisplayNamesOptions } from '@formatjs/intl-displaynames';
|
|
4
|
-
import IntlListFormat, { IntlListFormatOptions, Part } from '@formatjs/intl-listformat';
|
|
5
2
|
import { FormatError, Formats, FormatXMLElementFn, IntlMessageFormat, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
|
|
6
3
|
import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error';
|
|
7
4
|
import { DEFAULT_INTL_CONFIG } from './utils';
|
|
5
|
+
export interface Part<T = string> {
|
|
6
|
+
type: 'element' | 'literal';
|
|
7
|
+
value: T;
|
|
8
|
+
}
|
|
8
9
|
declare global {
|
|
9
10
|
namespace FormatjsIntl {
|
|
10
11
|
interface Message {
|
|
@@ -47,17 +48,17 @@ export interface CustomFormatConfig<Source = string> {
|
|
|
47
48
|
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
48
49
|
}
|
|
49
50
|
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
50
|
-
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
51
|
+
export type FormatNumberOptions = Omit<Intl.NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
51
52
|
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
52
53
|
export type FormatPluralOptions = Omit<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
|
|
53
|
-
export type FormatListOptions = Omit<
|
|
54
|
-
export type FormatDisplayNameOptions = Omit<DisplayNamesOptions, 'localeMatcher'>;
|
|
54
|
+
export type FormatListOptions = Omit<Intl.ListFormatOptions, 'localeMatcher'>;
|
|
55
|
+
export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMatcher'>;
|
|
55
56
|
/**
|
|
56
57
|
* `TBase` is the type constraints of the rich text element in the formatted output.
|
|
57
58
|
* For example, with React, `TBase` should be `React.ReactNode`.
|
|
58
59
|
*/
|
|
59
60
|
export interface IntlFormatters<TBase = unknown> {
|
|
60
|
-
formatDateTimeRange(this: void, from: Parameters<DateTimeFormat['formatRange']>[0], to: Parameters<DateTimeFormat['formatRange']>[1], opts?: FormatDateOptions): string;
|
|
61
|
+
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0], to: Parameters<Intl.DateTimeFormat['formatRange']>[1], opts?: FormatDateOptions): string;
|
|
61
62
|
formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
62
63
|
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
63
64
|
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
@@ -73,28 +74,28 @@ export interface IntlFormatters<TBase = unknown> {
|
|
|
73
74
|
formatList(this: void, values: ReadonlyArray<string>, opts?: FormatListOptions): string;
|
|
74
75
|
formatList<T extends TBase>(this: void, values: ReadonlyArray<string | T>, opts?: FormatListOptions): T | string | (string | T)[];
|
|
75
76
|
formatListToParts<T extends TBase>(this: void, values: ReadonlyArray<string | T>, opts?: FormatListOptions): Part[];
|
|
76
|
-
formatDisplayName(this: void, value: Parameters<DisplayNames['of']>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
77
|
+
formatDisplayName(this: void, value: Parameters<Intl.DisplayNames['of']>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
77
78
|
}
|
|
78
79
|
export interface Formatters {
|
|
79
|
-
getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): DateTimeFormat;
|
|
80
|
-
getNumberFormat(this: void, locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
|
|
80
|
+
getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
|
|
81
|
+
getNumberFormat(this: void, locales?: string | string[], opts?: Intl.NumberFormatOptions): Intl.NumberFormat;
|
|
81
82
|
getMessageFormat(this: void, ...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
|
|
82
83
|
getRelativeTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
|
|
83
84
|
getPluralRules(this: void, ...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
84
|
-
getListFormat(this: void, ...args: ConstructorParameters<typeof
|
|
85
|
-
getDisplayNames(this: void, ...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
|
|
85
|
+
getListFormat(this: void, ...args: ConstructorParameters<typeof Intl.ListFormat>): Intl.ListFormat;
|
|
86
|
+
getDisplayNames(this: void, ...args: ConstructorParameters<typeof Intl.DisplayNames>): Intl.DisplayNames;
|
|
86
87
|
}
|
|
87
88
|
export interface IntlShape<T = string> extends ResolvedIntlConfig<T>, IntlFormatters<T> {
|
|
88
89
|
formatters: Formatters;
|
|
89
90
|
}
|
|
90
91
|
export interface IntlCache {
|
|
91
|
-
dateTime: Record<string, DateTimeFormat>;
|
|
92
|
+
dateTime: Record<string, Intl.DateTimeFormat>;
|
|
92
93
|
number: Record<string, Intl.NumberFormat>;
|
|
93
94
|
message: Record<string, IntlMessageFormat>;
|
|
94
95
|
relativeTime: Record<string, Intl.RelativeTimeFormat>;
|
|
95
96
|
pluralRules: Record<string, Intl.PluralRules>;
|
|
96
|
-
list: Record<string,
|
|
97
|
-
displayNames: Record<string, DisplayNames>;
|
|
97
|
+
list: Record<string, Intl.ListFormat>;
|
|
98
|
+
displayNames: Record<string, Intl.DisplayNames>;
|
|
98
99
|
}
|
|
99
100
|
export interface MessageDescriptor {
|
|
100
101
|
id?: MessageIds;
|
package/lib/src/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types';
|
|
2
|
+
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
|
|
3
3
|
export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
|
|
4
4
|
export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, 'fallbackOnEmptyString' | 'formats' | 'messages' | 'timeZone' | 'defaultLocale' | 'defaultFormats' | 'onError' | 'onWarn'>;
|
|
5
5
|
export declare function createIntlCache(): IntlCache;
|
|
@@ -8,4 +8,4 @@ export declare function createIntlCache(): IntlCache;
|
|
|
8
8
|
* @param cache explicit cache to prevent leaking memory
|
|
9
9
|
*/
|
|
10
10
|
export declare function createFormatters(cache?: IntlCache): Formatters;
|
|
11
|
-
export declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
|
|
11
|
+
export declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): Intl.NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
|
package/lib/src/utils.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { __assign, __spreadArray } from "tslib";
|
|
2
|
-
import { IntlMessageFormat } from 'intl-messageformat';
|
|
3
2
|
import { memoize, strategies } from '@formatjs/fast-memoize';
|
|
3
|
+
import { IntlMessageFormat } from 'intl-messageformat';
|
|
4
4
|
import { UnsupportedFormatterError } from './error';
|
|
5
|
+
export function invariant(condition, message, Err) {
|
|
6
|
+
if (Err === void 0) { Err = Error; }
|
|
7
|
+
if (!condition) {
|
|
8
|
+
throw new Err(message);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
5
11
|
export function filterProps(props, allowlist, defaults) {
|
|
6
12
|
if (defaults === void 0) { defaults = {}; }
|
|
7
13
|
return allowlist.reduce(function (filtered, name) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/intl",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Internationalize JS apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"intl",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"author": "Long Ho <holevietlong@gmail.com>",
|
|
19
19
|
"license": "MIT",
|
|
20
|
-
"homepage": "https://formatjs.io",
|
|
20
|
+
"homepage": "https://formatjs.github.io",
|
|
21
21
|
"bugs": {
|
|
22
22
|
"url": "https://github.com/formatjs/formatjs/issues"
|
|
23
23
|
},
|
|
@@ -30,19 +30,12 @@
|
|
|
30
30
|
"sideEffects": false,
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"tslib": "2",
|
|
33
|
-
"
|
|
33
|
+
"intl-messageformat": "10.7.7",
|
|
34
34
|
"@formatjs/icu-messageformat-parser": "2.9.4",
|
|
35
|
-
"@formatjs/
|
|
36
|
-
"@formatjs/fast-memoize": "2.2.3",
|
|
37
|
-
"@formatjs/intl-displaynames": "6.8.5",
|
|
38
|
-
"intl-messageformat": "10.7.7"
|
|
39
|
-
},
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"@formatjs/intl-datetimeformat": "6.16.5",
|
|
42
|
-
"@formatjs/intl-numberformat": "8.14.5"
|
|
35
|
+
"@formatjs/fast-memoize": "2.2.3"
|
|
43
36
|
},
|
|
44
37
|
"peerDependencies": {
|
|
45
|
-
"typescript": "
|
|
38
|
+
"typescript": "5"
|
|
46
39
|
},
|
|
47
40
|
"peerDependenciesMeta": {
|
|
48
41
|
"typescript": {
|
package/src/create-intl.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IntlCache,
|
|
1
|
+
import { IntlCache, IntlConfig, IntlShape } from './types';
|
|
2
2
|
export interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
|
|
3
3
|
(config: C, cache?: IntlCache): S;
|
|
4
4
|
}
|
package/src/create-intl.js
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createIntl = createIntl;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
-
var
|
|
5
|
+
var dateTime_1 = require("./dateTime");
|
|
6
|
+
var displayName_1 = require("./displayName");
|
|
6
7
|
var error_1 = require("./error");
|
|
8
|
+
var list_1 = require("./list");
|
|
9
|
+
var message_1 = require("./message");
|
|
7
10
|
var number_1 = require("./number");
|
|
8
|
-
var relativeTime_1 = require("./relativeTime");
|
|
9
|
-
var dateTime_1 = require("./dateTime");
|
|
10
11
|
var plural_1 = require("./plural");
|
|
11
|
-
var
|
|
12
|
-
var
|
|
13
|
-
var displayName_1 = require("./displayName");
|
|
12
|
+
var relativeTime_1 = require("./relativeTime");
|
|
13
|
+
var utils_1 = require("./utils");
|
|
14
14
|
function messagesContainString(messages) {
|
|
15
15
|
var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
|
|
16
16
|
return typeof firstMessage === 'string';
|
|
@@ -19,7 +19,7 @@ function verifyConfigMessages(config) {
|
|
|
19
19
|
if (config.onWarn &&
|
|
20
20
|
config.defaultRichTextElements &&
|
|
21
21
|
messagesContainString(config.messages || {})) {
|
|
22
|
-
config.onWarn("[@formatjs/intl] \"defaultRichTextElements\" was specified but \"message\" was not pre-compiled. \nPlease consider using \"@formatjs/cli\" to pre-compile your messages for performance.\nFor more details see https://formatjs.io/docs/getting-started/message-distribution");
|
|
22
|
+
config.onWarn("[@formatjs/intl] \"defaultRichTextElements\" was specified but \"message\" was not pre-compiled. \nPlease consider using \"@formatjs/cli\" to pre-compile your messages for performance.\nFor more details see https://formatjs.github.io/docs/getting-started/message-distribution");
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
@@ -33,7 +33,7 @@ function createIntl(config, cache) {
|
|
|
33
33
|
var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
|
|
34
34
|
if (!locale) {
|
|
35
35
|
if (onError) {
|
|
36
|
-
onError(new error_1.InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl/api#intlshape for more details")));
|
|
36
|
+
onError(new error_1.InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details")));
|
|
37
37
|
}
|
|
38
38
|
// Since there's no registered locale data for `locale`, this will
|
|
39
39
|
// fallback to the `defaultLocale` to make sure things can render.
|
|
@@ -43,16 +43,12 @@ function createIntl(config, cache) {
|
|
|
43
43
|
resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
|
|
44
44
|
}
|
|
45
45
|
else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
|
|
46
|
-
onError(new error_1.MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl#runtime-requirements for more details")));
|
|
46
|
+
onError(new error_1.MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
|
|
47
47
|
}
|
|
48
48
|
else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
|
|
49
49
|
onError) {
|
|
50
|
-
onError(new error_1.MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl#runtime-requirements for more details")));
|
|
50
|
+
onError(new error_1.MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
|
|
51
51
|
}
|
|
52
52
|
verifyConfigMessages(resolvedConfig);
|
|
53
|
-
return tslib_1.__assign(tslib_1.__assign({}, resolvedConfig), { formatters: formatters, formatNumber: number_1.formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: number_1.formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: relativeTime_1.formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: dateTime_1.formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: dateTime_1.formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: dateTime_1.formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: dateTime_1.formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules),
|
|
54
|
-
// @ts-expect-error TODO: will get to this later
|
|
55
|
-
formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters),
|
|
56
|
-
// @ts-expect-error TODO: will get to this later
|
|
57
|
-
$t: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat), formatListToParts: list_1.formatListToParts.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: displayName_1.formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
|
|
53
|
+
return tslib_1.__assign(tslib_1.__assign({}, resolvedConfig), { formatters: formatters, formatNumber: number_1.formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: number_1.formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: relativeTime_1.formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: dateTime_1.formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: dateTime_1.formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: dateTime_1.formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: dateTime_1.formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters), $t: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat), formatListToParts: list_1.formatListToParts.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: displayName_1.formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
|
|
58
54
|
}
|
package/src/dateTime.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters,
|
|
2
|
-
import { DateTimeFormat } from '@formatjs/ecma402-abstract';
|
|
1
|
+
import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
3
2
|
export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
4
3
|
locale: string;
|
|
5
4
|
timeZone?: string;
|
|
6
5
|
formats: CustomFormats;
|
|
7
6
|
onError: OnErrorFn;
|
|
8
|
-
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): DateTimeFormat;
|
|
7
|
+
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
9
8
|
export declare function formatDate(config: {
|
|
10
9
|
locale: string;
|
|
11
10
|
timeZone?: string;
|
package/src/dateTime.js
CHANGED
|
@@ -7,8 +7,8 @@ exports.formatDateTimeRange = formatDateTimeRange;
|
|
|
7
7
|
exports.formatDateToParts = formatDateToParts;
|
|
8
8
|
exports.formatTimeToParts = formatTimeToParts;
|
|
9
9
|
var tslib_1 = require("tslib");
|
|
10
|
-
var utils_1 = require("./utils");
|
|
11
10
|
var error_1 = require("./error");
|
|
11
|
+
var utils_1 = require("./utils");
|
|
12
12
|
var DATE_TIME_FORMAT_OPTIONS = [
|
|
13
13
|
'formatMatcher',
|
|
14
14
|
'timeZone',
|
package/src/list.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
-
import type { Part } from '@formatjs/intl-listformat';
|
|
1
|
+
import { Formatters, IntlFormatters, OnErrorFn, Part } from './types';
|
|
3
2
|
export declare function formatList(opts: {
|
|
4
3
|
locale: string;
|
|
5
4
|
onError: OnErrorFn;
|
package/src/list.js
CHANGED
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.formatList = formatList;
|
|
4
4
|
exports.formatListToParts = formatListToParts;
|
|
5
5
|
var tslib_1 = require("tslib");
|
|
6
|
-
var utils_1 = require("./utils");
|
|
7
6
|
var intl_messageformat_1 = require("intl-messageformat");
|
|
8
7
|
var error_1 = require("./error");
|
|
8
|
+
var utils_1 = require("./utils");
|
|
9
9
|
var LIST_FORMAT_OPTIONS = [
|
|
10
10
|
'type',
|
|
11
11
|
'style',
|
|
@@ -52,9 +52,9 @@ function formatListToParts(_a, getListFormat, values, options) {
|
|
|
52
52
|
return getListFormat(locale, filteredOptions)
|
|
53
53
|
.formatToParts(serializedValues)
|
|
54
54
|
.map(function (part) {
|
|
55
|
-
return part.type === 'literal'
|
|
55
|
+
return (part.type === 'literal'
|
|
56
56
|
? part
|
|
57
|
-
: tslib_1.__assign(tslib_1.__assign({}, part), { value: richValues_1[part.value] || part.value });
|
|
57
|
+
: tslib_1.__assign(tslib_1.__assign({}, part), { value: richValues_1[part.value] || part.value }));
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
catch (e) {
|
package/src/message.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Formatters, MessageDescriptor,
|
|
2
|
-
import { FormatXMLElementFn, PrimitiveType, Formatters as IntlMessageFormatFormatters, Options } from 'intl-messageformat';
|
|
1
|
+
import { CustomFormats, Formatters, MessageDescriptor, OnErrorFn } from './types';
|
|
3
2
|
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
|
+
import { FormatXMLElementFn, Formatters as IntlMessageFormatFormatters, Options, PrimitiveType } from 'intl-messageformat';
|
|
4
4
|
export type FormatMessageFn<T> = ({ locale, formats, messages, defaultLocale, defaultFormats, fallbackOnEmptyString, onError, timeZone, defaultRichTextElements, }: {
|
|
5
5
|
locale: string;
|
|
6
6
|
timeZone?: string;
|
package/src/message.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatMessage = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
-
var
|
|
5
|
+
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
6
6
|
var intl_messageformat_1 = require("intl-messageformat");
|
|
7
7
|
var error_1 = require("./error");
|
|
8
|
-
var
|
|
8
|
+
var utils_1 = require("./utils");
|
|
9
9
|
function setTimeZoneInOptions(opts, timeZone) {
|
|
10
10
|
return Object.keys(opts).reduce(function (all, k) {
|
|
11
11
|
all[k] = tslib_1.__assign({ timeZone: timeZone }, opts[k]);
|
|
@@ -31,7 +31,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
31
31
|
if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
|
|
32
32
|
var msgId = messageDescriptor.id, defaultMessage = messageDescriptor.defaultMessage;
|
|
33
33
|
// `id` is a required field of a Message Descriptor.
|
|
34
|
-
(0,
|
|
34
|
+
(0, utils_1.invariant)(!!msgId, "[@formatjs/intl] An `id` must be provided to format a message. You can either:\n1. Configure your build toolchain with [babel-plugin-formatjs](https://formatjs.github.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.github.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.github.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
|
|
35
35
|
var id = String(msgId);
|
|
36
36
|
var message =
|
|
37
37
|
// In case messages is Object.create(null)
|
package/src/number.js
CHANGED
|
@@ -26,9 +26,13 @@ var NUMBER_FORMAT_OPTIONS = [
|
|
|
26
26
|
'unitDisplay',
|
|
27
27
|
'numberingSystem',
|
|
28
28
|
// ES2023 NumberFormat
|
|
29
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
29
30
|
'trailingZeroDisplay',
|
|
31
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
30
32
|
'roundingPriority',
|
|
33
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
31
34
|
'roundingIncrement',
|
|
35
|
+
// @ts-expect-error: TypeScript doesn't know about this yet
|
|
32
36
|
'roundingMode',
|
|
33
37
|
];
|
|
34
38
|
function getFormatter(_a, getNumberFormat, options) {
|
package/src/plural.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
-
import { LDMLPluralRule } from '@formatjs/ecma402-abstract';
|
|
3
2
|
export declare function formatPlural({ locale, onError, }: {
|
|
4
3
|
locale: string;
|
|
5
4
|
onError: OnErrorFn;
|
|
6
|
-
}, getPluralRules: Formatters['getPluralRules'], value: Parameters<IntlFormatters['formatPlural']>[0], options?: Parameters<IntlFormatters['formatPlural']>[1]): LDMLPluralRule;
|
|
5
|
+
}, getPluralRules: Formatters['getPluralRules'], value: Parameters<IntlFormatters['formatPlural']>[0], options?: Parameters<IntlFormatters['formatPlural']>[1]): Intl.LDMLPluralRule;
|
package/src/plural.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatPlural = formatPlural;
|
|
4
|
-
var utils_1 = require("./utils");
|
|
5
|
-
var error_1 = require("./error");
|
|
6
4
|
var intl_messageformat_1 = require("intl-messageformat");
|
|
5
|
+
var error_1 = require("./error");
|
|
6
|
+
var utils_1 = require("./utils");
|
|
7
7
|
var PLURAL_FORMAT_OPTIONS = ['type'];
|
|
8
8
|
function formatPlural(_a, getPluralRules, value, options) {
|
|
9
9
|
var locale = _a.locale, onError = _a.onError;
|
package/src/types.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { DateTimeFormat, NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
1
|
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
|
-
import { DisplayNames, DisplayNamesOptions } from '@formatjs/intl-displaynames';
|
|
4
|
-
import IntlListFormat, { IntlListFormatOptions, Part } from '@formatjs/intl-listformat';
|
|
5
2
|
import { FormatError, Formats, FormatXMLElementFn, IntlMessageFormat, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
|
|
6
3
|
import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error';
|
|
7
4
|
import { DEFAULT_INTL_CONFIG } from './utils';
|
|
5
|
+
export interface Part<T = string> {
|
|
6
|
+
type: 'element' | 'literal';
|
|
7
|
+
value: T;
|
|
8
|
+
}
|
|
8
9
|
declare global {
|
|
9
10
|
namespace FormatjsIntl {
|
|
10
11
|
interface Message {
|
|
@@ -47,17 +48,17 @@ export interface CustomFormatConfig<Source = string> {
|
|
|
47
48
|
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
48
49
|
}
|
|
49
50
|
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
50
|
-
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
51
|
+
export type FormatNumberOptions = Omit<Intl.NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
51
52
|
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
52
53
|
export type FormatPluralOptions = Omit<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
|
|
53
|
-
export type FormatListOptions = Omit<
|
|
54
|
-
export type FormatDisplayNameOptions = Omit<DisplayNamesOptions, 'localeMatcher'>;
|
|
54
|
+
export type FormatListOptions = Omit<Intl.ListFormatOptions, 'localeMatcher'>;
|
|
55
|
+
export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMatcher'>;
|
|
55
56
|
/**
|
|
56
57
|
* `TBase` is the type constraints of the rich text element in the formatted output.
|
|
57
58
|
* For example, with React, `TBase` should be `React.ReactNode`.
|
|
58
59
|
*/
|
|
59
60
|
export interface IntlFormatters<TBase = unknown> {
|
|
60
|
-
formatDateTimeRange(this: void, from: Parameters<DateTimeFormat['formatRange']>[0], to: Parameters<DateTimeFormat['formatRange']>[1], opts?: FormatDateOptions): string;
|
|
61
|
+
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0], to: Parameters<Intl.DateTimeFormat['formatRange']>[1], opts?: FormatDateOptions): string;
|
|
61
62
|
formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
62
63
|
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
63
64
|
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
@@ -73,28 +74,28 @@ export interface IntlFormatters<TBase = unknown> {
|
|
|
73
74
|
formatList(this: void, values: ReadonlyArray<string>, opts?: FormatListOptions): string;
|
|
74
75
|
formatList<T extends TBase>(this: void, values: ReadonlyArray<string | T>, opts?: FormatListOptions): T | string | (string | T)[];
|
|
75
76
|
formatListToParts<T extends TBase>(this: void, values: ReadonlyArray<string | T>, opts?: FormatListOptions): Part[];
|
|
76
|
-
formatDisplayName(this: void, value: Parameters<DisplayNames['of']>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
77
|
+
formatDisplayName(this: void, value: Parameters<Intl.DisplayNames['of']>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
77
78
|
}
|
|
78
79
|
export interface Formatters {
|
|
79
|
-
getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): DateTimeFormat;
|
|
80
|
-
getNumberFormat(this: void, locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
|
|
80
|
+
getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
|
|
81
|
+
getNumberFormat(this: void, locales?: string | string[], opts?: Intl.NumberFormatOptions): Intl.NumberFormat;
|
|
81
82
|
getMessageFormat(this: void, ...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
|
|
82
83
|
getRelativeTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
|
|
83
84
|
getPluralRules(this: void, ...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
84
|
-
getListFormat(this: void, ...args: ConstructorParameters<typeof
|
|
85
|
-
getDisplayNames(this: void, ...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
|
|
85
|
+
getListFormat(this: void, ...args: ConstructorParameters<typeof Intl.ListFormat>): Intl.ListFormat;
|
|
86
|
+
getDisplayNames(this: void, ...args: ConstructorParameters<typeof Intl.DisplayNames>): Intl.DisplayNames;
|
|
86
87
|
}
|
|
87
88
|
export interface IntlShape<T = string> extends ResolvedIntlConfig<T>, IntlFormatters<T> {
|
|
88
89
|
formatters: Formatters;
|
|
89
90
|
}
|
|
90
91
|
export interface IntlCache {
|
|
91
|
-
dateTime: Record<string, DateTimeFormat>;
|
|
92
|
+
dateTime: Record<string, Intl.DateTimeFormat>;
|
|
92
93
|
number: Record<string, Intl.NumberFormat>;
|
|
93
94
|
message: Record<string, IntlMessageFormat>;
|
|
94
95
|
relativeTime: Record<string, Intl.RelativeTimeFormat>;
|
|
95
96
|
pluralRules: Record<string, Intl.PluralRules>;
|
|
96
|
-
list: Record<string,
|
|
97
|
-
displayNames: Record<string, DisplayNames>;
|
|
97
|
+
list: Record<string, Intl.ListFormat>;
|
|
98
|
+
displayNames: Record<string, Intl.DisplayNames>;
|
|
98
99
|
}
|
|
99
100
|
export interface MessageDescriptor {
|
|
100
101
|
id?: MessageIds;
|
package/src/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types';
|
|
2
|
+
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
|
|
3
3
|
export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
|
|
4
4
|
export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, 'fallbackOnEmptyString' | 'formats' | 'messages' | 'timeZone' | 'defaultLocale' | 'defaultFormats' | 'onError' | 'onWarn'>;
|
|
5
5
|
export declare function createIntlCache(): IntlCache;
|
|
@@ -8,4 +8,4 @@ export declare function createIntlCache(): IntlCache;
|
|
|
8
8
|
* @param cache explicit cache to prevent leaking memory
|
|
9
9
|
*/
|
|
10
10
|
export declare function createFormatters(cache?: IntlCache): Formatters;
|
|
11
|
-
export declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
|
|
11
|
+
export declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): Intl.NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
|
package/src/utils.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DEFAULT_INTL_CONFIG = void 0;
|
|
4
|
+
exports.invariant = invariant;
|
|
4
5
|
exports.filterProps = filterProps;
|
|
5
6
|
exports.createIntlCache = createIntlCache;
|
|
6
7
|
exports.createFormatters = createFormatters;
|
|
7
8
|
exports.getNamedFormat = getNamedFormat;
|
|
8
9
|
var tslib_1 = require("tslib");
|
|
9
|
-
var intl_messageformat_1 = require("intl-messageformat");
|
|
10
10
|
var fast_memoize_1 = require("@formatjs/fast-memoize");
|
|
11
|
+
var intl_messageformat_1 = require("intl-messageformat");
|
|
11
12
|
var error_1 = require("./error");
|
|
13
|
+
function invariant(condition, message, Err) {
|
|
14
|
+
if (Err === void 0) { Err = Error; }
|
|
15
|
+
if (!condition) {
|
|
16
|
+
throw new Err(message);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
12
19
|
function filterProps(props, allowlist, defaults) {
|
|
13
20
|
if (defaults === void 0) { defaults = {}; }
|
|
14
21
|
return allowlist.reduce(function (filtered, name) {
|