@formatjs/intl 2.3.2 → 2.3.4
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 +17 -0
- package/index.d.ts.map +1 -0
- package/index.js +40 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/src/create-intl.d.ts +11 -0
- package/lib/src/create-intl.d.ts.map +1 -0
- package/lib/src/create-intl.js +55 -0
- package/lib/src/dateTime.d.ts +38 -0
- package/lib/src/dateTime.d.ts.map +1 -0
- package/lib/src/dateTime.js +117 -0
- package/lib/src/displayName.d.ts +6 -0
- package/lib/src/displayName.d.ts.map +1 -0
- package/lib/src/displayName.js +22 -0
- package/lib/src/error.d.ts +34 -0
- package/lib/src/error.d.ts.map +1 -0
- package/lib/src/error.js +88 -0
- package/lib/src/list.d.ts +11 -0
- package/lib/src/list.d.ts.map +1 -0
- package/lib/src/list.js +61 -0
- package/lib/src/message.d.ts +16 -0
- package/lib/src/message.d.ts.map +1 -0
- package/lib/src/message.js +103 -0
- package/lib/src/number.d.ts +17 -0
- package/lib/src/number.d.ts.map +1 -0
- package/lib/src/number.js +54 -0
- package/lib/src/plural.d.ts +7 -0
- package/lib/src/plural.d.ts.map +1 -0
- package/lib/src/plural.js +19 -0
- package/lib/src/relativeTime.d.ts +7 -0
- package/lib/src/relativeTime.d.ts.map +1 -0
- package/lib/src/relativeTime.js +29 -0
- package/lib/src/types.d.ts +103 -0
- package/lib/src/types.d.ts.map +1 -0
- package/lib/src/types.js +1 -0
- package/lib/src/utils.d.ts +12 -0
- package/lib/src/utils.d.ts.map +1 -0
- package/lib/src/utils.js +162 -0
- package/package.json +9 -9
- package/src/create-intl.d.ts +11 -0
- package/src/create-intl.d.ts.map +1 -0
- package/src/create-intl.js +59 -0
- package/src/dateTime.d.ts +38 -0
- package/src/dateTime.d.ts.map +1 -0
- package/src/dateTime.js +126 -0
- package/src/displayName.d.ts +6 -0
- package/src/displayName.d.ts.map +1 -0
- package/src/displayName.js +26 -0
- package/src/error.d.ts +34 -0
- package/src/error.d.ts.map +1 -0
- package/src/error.js +91 -0
- package/src/list.d.ts +11 -0
- package/src/list.d.ts.map +1 -0
- package/src/list.js +66 -0
- package/src/message.d.ts +16 -0
- package/src/message.d.ts.map +1 -0
- package/src/message.js +107 -0
- package/src/number.d.ts +17 -0
- package/src/number.d.ts.map +1 -0
- package/src/number.js +60 -0
- package/src/plural.d.ts +7 -0
- package/src/plural.d.ts.map +1 -0
- package/src/plural.js +23 -0
- package/src/relativeTime.d.ts +7 -0
- package/src/relativeTime.d.ts.map +1 -0
- package/src/relativeTime.js +33 -0
- package/src/types.d.ts +103 -0
- package/src/types.d.ts.map +1 -0
- package/src/types.js +2 -0
- package/src/utils.d.ts +12 -0
- package/src/utils.d.ts.map +1 -0
- package/src/utils.js +169 -0
- package/BUILD +0 -79
- package/CHANGELOG.md +0 -537
- package/index.ts +0 -38
- package/src/create-intl.ts +0 -159
- package/src/dateTime.ts +0 -204
- package/src/displayName.ts +0 -54
- package/src/error.ts +0 -106
- package/src/list.ts +0 -119
- package/src/message.ts +0 -246
- package/src/number.ts +0 -101
- package/src/plural.ts +0 -45
- package/src/relativeTime.ts +0 -74
- package/src/types.ts +0 -234
- package/src/utils.ts +0 -177
- package/tests/__snapshots__/formatDate.test.ts.snap +0 -13
- package/tests/__snapshots__/formatMessage.test.ts.snap +0 -45
- package/tests/__snapshots__/formatNumber.test.ts.snap +0 -13
- package/tests/__snapshots__/formatRelativeTime.test.ts.snap +0 -19
- package/tests/__snapshots__/formatTime.test.ts.snap +0 -13
- package/tests/create-intl.test.ts +0 -60
- package/tests/error.test.ts +0 -25
- package/tests/formatDate.test.ts +0 -167
- package/tests/formatDisplayNames.test.ts +0 -63
- package/tests/formatList.test.ts +0 -51
- package/tests/formatMessage.test.ts +0 -495
- package/tests/formatNumber.test.ts +0 -172
- package/tests/formatPlural.test.ts +0 -119
- package/tests/formatRelativeTime.test.ts +0 -145
- package/tests/formatTime.test.ts +0 -234
- package/tests/global.d.ts +0 -7
- package/tsconfig.json +0 -5
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
import { TYPE } from '@formatjs/icu-messageformat-parser';
|
|
6
|
+
function setTimeZoneInOptions(opts, timeZone) {
|
|
7
|
+
return Object.keys(opts).reduce(function (all, k) {
|
|
8
|
+
all[k] = __assign({ timeZone: timeZone }, opts[k]);
|
|
9
|
+
return all;
|
|
10
|
+
}, {});
|
|
11
|
+
}
|
|
12
|
+
function deepMergeOptions(opts1, opts2) {
|
|
13
|
+
var keys = Object.keys(__assign(__assign({}, opts1), opts2));
|
|
14
|
+
return keys.reduce(function (all, k) {
|
|
15
|
+
all[k] = __assign(__assign({}, (opts1[k] || {})), (opts2[k] || {}));
|
|
16
|
+
return all;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
function deepMergeFormatsAndSetTimeZone(f1, timeZone) {
|
|
20
|
+
if (!timeZone) {
|
|
21
|
+
return f1;
|
|
22
|
+
}
|
|
23
|
+
var mfFormats = IntlMessageFormat.formats;
|
|
24
|
+
return __assign(__assign(__assign({}, mfFormats), f1), { date: deepMergeOptions(setTimeZoneInOptions(mfFormats.date, timeZone), setTimeZoneInOptions(f1.date || {}, timeZone)), time: deepMergeOptions(setTimeZoneInOptions(mfFormats.time, timeZone), setTimeZoneInOptions(f1.time || {}, timeZone)) });
|
|
25
|
+
}
|
|
26
|
+
export var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
27
|
+
var locale = _a.locale, formats = _a.formats, messages = _a.messages, defaultLocale = _a.defaultLocale, defaultFormats = _a.defaultFormats, fallbackOnEmptyString = _a.fallbackOnEmptyString, onError = _a.onError, timeZone = _a.timeZone, defaultRichTextElements = _a.defaultRichTextElements;
|
|
28
|
+
if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
|
|
29
|
+
var msgId = messageDescriptor.id, defaultMessage = messageDescriptor.defaultMessage;
|
|
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");
|
|
32
|
+
var id = String(msgId);
|
|
33
|
+
var message =
|
|
34
|
+
// In case messages is Object.create(null)
|
|
35
|
+
// e.g import('foo.json') from webpack)
|
|
36
|
+
// See https://github.com/formatjs/formatjs/issues/1914
|
|
37
|
+
messages &&
|
|
38
|
+
Object.prototype.hasOwnProperty.call(messages, id) &&
|
|
39
|
+
messages[id];
|
|
40
|
+
// IMPORTANT: Hot path if `message` is AST with a single literal node
|
|
41
|
+
if (Array.isArray(message) &&
|
|
42
|
+
message.length === 1 &&
|
|
43
|
+
message[0].type === TYPE.literal) {
|
|
44
|
+
return message[0].value;
|
|
45
|
+
}
|
|
46
|
+
// IMPORTANT: Hot path straight lookup for performance
|
|
47
|
+
if (!values &&
|
|
48
|
+
message &&
|
|
49
|
+
typeof message === 'string' &&
|
|
50
|
+
!defaultRichTextElements) {
|
|
51
|
+
return message.replace(/'\{(.*?)\}'/gi, "{$1}");
|
|
52
|
+
}
|
|
53
|
+
values = __assign(__assign({}, defaultRichTextElements), (values || {}));
|
|
54
|
+
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
|
|
55
|
+
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
|
|
56
|
+
if (!message) {
|
|
57
|
+
if (fallbackOnEmptyString === false && message === '') {
|
|
58
|
+
return message;
|
|
59
|
+
}
|
|
60
|
+
if (!defaultMessage ||
|
|
61
|
+
(locale && locale.toLowerCase() !== defaultLocale.toLowerCase())) {
|
|
62
|
+
// This prevents warnings from littering the console in development
|
|
63
|
+
// when no `messages` are passed into the <IntlProvider> for the
|
|
64
|
+
// default locale.
|
|
65
|
+
onError(new MissingTranslationError(messageDescriptor, locale));
|
|
66
|
+
}
|
|
67
|
+
if (defaultMessage) {
|
|
68
|
+
try {
|
|
69
|
+
var formatter = state.getMessageFormat(defaultMessage, defaultLocale, defaultFormats, opts);
|
|
70
|
+
return formatter.format(values);
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
onError(new MessageFormatError("Error formatting default message for: \"".concat(id, "\", rendering default message verbatim"), locale, messageDescriptor, e));
|
|
74
|
+
return typeof defaultMessage === 'string' ? defaultMessage : id;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return id;
|
|
78
|
+
}
|
|
79
|
+
// We have the translated message
|
|
80
|
+
try {
|
|
81
|
+
var formatter = state.getMessageFormat(message, locale, formats, __assign({ formatters: state }, (opts || {})));
|
|
82
|
+
return formatter.format(values);
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
onError(new MessageFormatError("Error formatting message: \"".concat(id, "\", using ").concat(defaultMessage ? 'default message' : 'id', " as fallback."), locale, messageDescriptor, e));
|
|
86
|
+
}
|
|
87
|
+
if (defaultMessage) {
|
|
88
|
+
try {
|
|
89
|
+
var formatter = state.getMessageFormat(defaultMessage, defaultLocale, defaultFormats, opts);
|
|
90
|
+
return formatter.format(values);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
onError(new MessageFormatError("Error formatting the default message for: \"".concat(id, "\", rendering message verbatim"), locale, messageDescriptor, e));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (typeof message === 'string') {
|
|
97
|
+
return message;
|
|
98
|
+
}
|
|
99
|
+
if (typeof defaultMessage === 'string') {
|
|
100
|
+
return defaultMessage;
|
|
101
|
+
}
|
|
102
|
+
return id;
|
|
103
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Formatters, IntlFormatters, CustomFormats, OnErrorFn } from './types';
|
|
2
|
+
export declare function getFormatter({ locale, formats, onError, }: {
|
|
3
|
+
locale: string;
|
|
4
|
+
formats: CustomFormats;
|
|
5
|
+
onError: OnErrorFn;
|
|
6
|
+
}, getNumberFormat: Formatters['getNumberFormat'], options?: Parameters<IntlFormatters['formatNumber']>[1]): Intl.NumberFormat;
|
|
7
|
+
export declare function formatNumber(config: {
|
|
8
|
+
locale: string;
|
|
9
|
+
formats: CustomFormats;
|
|
10
|
+
onError: OnErrorFn;
|
|
11
|
+
}, getNumberFormat: Formatters['getNumberFormat'], value: Parameters<IntlFormatters['formatNumber']>[0], options?: Parameters<IntlFormatters['formatNumber']>[1]): string;
|
|
12
|
+
export declare function formatNumberToParts(config: {
|
|
13
|
+
locale: string;
|
|
14
|
+
formats: CustomFormats;
|
|
15
|
+
onError: OnErrorFn;
|
|
16
|
+
}, getNumberFormat: Formatters['getNumberFormat'], value: Parameters<IntlFormatters['formatNumber']>[0], options?: Parameters<IntlFormatters['formatNumber']>[1]): Intl.NumberFormatPart[];
|
|
17
|
+
//# sourceMappingURL=number.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../src/number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AA8B5E,wBAAgB,YAAY,CAC1B,EACE,MAAM,EACN,OAAO,EACP,OAAO,GACR,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IAEd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAM,GAC1D,IAAI,CAAC,YAAY,CAYnB;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IAEd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,EACpD,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAM,GAC1D,MAAM,CAUR;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,EACpD,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAM,GAC1D,IAAI,CAAC,gBAAgB,EAAE,CAYzB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { getNamedFormat, filterProps } from './utils';
|
|
2
|
+
import { IntlError, IntlErrorCode } from './error';
|
|
3
|
+
var NUMBER_FORMAT_OPTIONS = [
|
|
4
|
+
'style',
|
|
5
|
+
'currency',
|
|
6
|
+
'currencyDisplay',
|
|
7
|
+
'unit',
|
|
8
|
+
'unitDisplay',
|
|
9
|
+
'useGrouping',
|
|
10
|
+
'minimumIntegerDigits',
|
|
11
|
+
'minimumFractionDigits',
|
|
12
|
+
'maximumFractionDigits',
|
|
13
|
+
'minimumSignificantDigits',
|
|
14
|
+
'maximumSignificantDigits',
|
|
15
|
+
// ES2020 NumberFormat
|
|
16
|
+
'compactDisplay',
|
|
17
|
+
'currencyDisplay',
|
|
18
|
+
'currencySign',
|
|
19
|
+
'notation',
|
|
20
|
+
'signDisplay',
|
|
21
|
+
'unit',
|
|
22
|
+
'unitDisplay',
|
|
23
|
+
'numberingSystem',
|
|
24
|
+
];
|
|
25
|
+
export function getFormatter(_a, getNumberFormat, options) {
|
|
26
|
+
var locale = _a.locale, formats = _a.formats, onError = _a.onError;
|
|
27
|
+
if (options === void 0) { options = {}; }
|
|
28
|
+
var format = options.format;
|
|
29
|
+
var defaults = ((format &&
|
|
30
|
+
getNamedFormat(formats, 'number', format, onError)) ||
|
|
31
|
+
{});
|
|
32
|
+
var filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults);
|
|
33
|
+
return getNumberFormat(locale, filteredOptions);
|
|
34
|
+
}
|
|
35
|
+
export function formatNumber(config, getNumberFormat, value, options) {
|
|
36
|
+
if (options === void 0) { options = {}; }
|
|
37
|
+
try {
|
|
38
|
+
return getFormatter(config, getNumberFormat, options).format(value);
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
config.onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting number.', e));
|
|
42
|
+
}
|
|
43
|
+
return String(value);
|
|
44
|
+
}
|
|
45
|
+
export function formatNumberToParts(config, getNumberFormat, value, options) {
|
|
46
|
+
if (options === void 0) { options = {}; }
|
|
47
|
+
try {
|
|
48
|
+
return getFormatter(config, getNumberFormat, options).formatToParts(value);
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
config.onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting number.', e));
|
|
52
|
+
}
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
+
import { LDMLPluralRule } from '@formatjs/ecma402-abstract';
|
|
3
|
+
export declare function formatPlural({ locale, onError, }: {
|
|
4
|
+
locale: string;
|
|
5
|
+
onError: OnErrorFn;
|
|
6
|
+
}, getPluralRules: Formatters['getPluralRules'], value: Parameters<IntlFormatters['formatPlural']>[0], options?: Parameters<IntlFormatters['formatPlural']>[1]): LDMLPluralRule;
|
|
7
|
+
//# sourceMappingURL=plural.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plural.d.ts","sourceRoot":"","sources":["../../src/plural.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAI7D,OAAO,EAAC,cAAc,EAAC,MAAM,4BAA4B,CAAA;AAIzD,wBAAgB,YAAY,CAC1B,EACE,MAAM,EACN,OAAO,GACR,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,cAAc,EAAE,UAAU,CAAC,gBAAgB,CAAC,EAC5C,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,EACpD,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAM,GAC1D,cAAc,CAyBhB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { filterProps } from './utils';
|
|
2
|
+
import { IntlFormatError } from './error';
|
|
3
|
+
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
4
|
+
var PLURAL_FORMAT_OPTIONS = ['type'];
|
|
5
|
+
export function formatPlural(_a, getPluralRules, value, options) {
|
|
6
|
+
var locale = _a.locale, onError = _a.onError;
|
|
7
|
+
if (options === void 0) { options = {}; }
|
|
8
|
+
if (!Intl.PluralRules) {
|
|
9
|
+
onError(new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API));
|
|
10
|
+
}
|
|
11
|
+
var filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
|
|
12
|
+
try {
|
|
13
|
+
return getPluralRules(locale, filteredOptions).select(value);
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
onError(new IntlFormatError('Error formatting plural.', locale, e));
|
|
17
|
+
}
|
|
18
|
+
return 'other';
|
|
19
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IntlFormatters, Formatters, CustomFormats, OnErrorFn } from './types';
|
|
2
|
+
export declare function formatRelativeTime(config: {
|
|
3
|
+
locale: string;
|
|
4
|
+
formats: CustomFormats;
|
|
5
|
+
onError: OnErrorFn;
|
|
6
|
+
}, getRelativeTimeFormat: Formatters['getRelativeTimeFormat'], value: Parameters<IntlFormatters['formatRelativeTime']>[0], unit?: Parameters<IntlFormatters['formatRelativeTime']>[1], options?: Parameters<IntlFormatters['formatRelativeTime']>[2]): string;
|
|
7
|
+
//# sourceMappingURL=relativeTime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relativeTime.d.ts","sourceRoot":"","sources":["../../src/relativeTime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAoC5E,wBAAgB,kBAAkB,CAChC,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,qBAAqB,EAAE,UAAU,CAAC,uBAAuB,CAAC,EAC1D,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1D,IAAI,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1D,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAM,GAChE,MAAM,CA2BR"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getNamedFormat, filterProps } from './utils';
|
|
2
|
+
import { FormatError, ErrorCode } from 'intl-messageformat';
|
|
3
|
+
import { IntlFormatError } from './error';
|
|
4
|
+
var RELATIVE_TIME_FORMAT_OPTIONS = ['numeric', 'style'];
|
|
5
|
+
function getFormatter(_a, getRelativeTimeFormat, options) {
|
|
6
|
+
var locale = _a.locale, formats = _a.formats, onError = _a.onError;
|
|
7
|
+
if (options === void 0) { options = {}; }
|
|
8
|
+
var format = options.format;
|
|
9
|
+
var defaults = (!!format && getNamedFormat(formats, 'relative', format, onError)) || {};
|
|
10
|
+
var filteredOptions = filterProps(options, RELATIVE_TIME_FORMAT_OPTIONS, defaults);
|
|
11
|
+
return getRelativeTimeFormat(locale, filteredOptions);
|
|
12
|
+
}
|
|
13
|
+
export function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options) {
|
|
14
|
+
if (options === void 0) { options = {}; }
|
|
15
|
+
if (!unit) {
|
|
16
|
+
unit = 'second';
|
|
17
|
+
}
|
|
18
|
+
var RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
19
|
+
if (!RelativeTimeFormat) {
|
|
20
|
+
config.onError(new FormatError("Intl.RelativeTimeFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-relativetimeformat\"\n", ErrorCode.MISSING_INTL_API));
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return getFormatter(config, getRelativeTimeFormat, options).format(value, unit);
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
config.onError(new IntlFormatError('Error formatting relative time.', config.locale, e));
|
|
27
|
+
}
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { IntlMessageFormat, Formats, FormatXMLElementFn, FormatError, PrimitiveType, Options as IntlMessageFormatOptions } from 'intl-messageformat';
|
|
2
|
+
import { DateTimeFormat } from '@formatjs/ecma402-abstract';
|
|
3
|
+
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
4
|
+
import IntlListFormat, { IntlListFormatOptions, Part } from '@formatjs/intl-listformat';
|
|
5
|
+
import { DisplayNames, DisplayNamesOptions } from '@formatjs/intl-displaynames';
|
|
6
|
+
import { MissingTranslationError, MessageFormatError, MissingDataError, InvalidConfigError, UnsupportedFormatterError } from './error';
|
|
7
|
+
import { DEFAULT_INTL_CONFIG } from './utils';
|
|
8
|
+
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
9
|
+
declare global {
|
|
10
|
+
namespace FormatjsIntl {
|
|
11
|
+
interface Message {
|
|
12
|
+
}
|
|
13
|
+
interface IntlConfig {
|
|
14
|
+
}
|
|
15
|
+
interface Formats {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
declare type MessageIds = FormatjsIntl.Message extends {
|
|
20
|
+
ids: string;
|
|
21
|
+
} ? FormatjsIntl.Message['ids'] : string;
|
|
22
|
+
declare type Locale = FormatjsIntl.IntlConfig extends {
|
|
23
|
+
locale: string;
|
|
24
|
+
} ? FormatjsIntl.IntlConfig['locale'] : string;
|
|
25
|
+
export declare type OnErrorFn = (err: MissingTranslationError | MessageFormatError | MissingDataError | InvalidConfigError | UnsupportedFormatterError | FormatError) => void;
|
|
26
|
+
export declare type OnWarnFn = (warning: string) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Config for intl object.
|
|
29
|
+
* Generic type T is the type of potential rich text element. For example:
|
|
30
|
+
* With React, T would be React.ReactNode
|
|
31
|
+
*/
|
|
32
|
+
export interface ResolvedIntlConfig<T = string> {
|
|
33
|
+
locale: Locale;
|
|
34
|
+
timeZone?: string;
|
|
35
|
+
fallbackOnEmptyString?: boolean;
|
|
36
|
+
formats: CustomFormats;
|
|
37
|
+
messages: Record<MessageIds, string> | Record<MessageIds, MessageFormatElement[]>;
|
|
38
|
+
defaultLocale: string;
|
|
39
|
+
defaultFormats: CustomFormats;
|
|
40
|
+
defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
|
|
41
|
+
onError: OnErrorFn;
|
|
42
|
+
onWarn?: OnWarnFn;
|
|
43
|
+
}
|
|
44
|
+
export interface CustomFormats extends Partial<Formats> {
|
|
45
|
+
relative?: Record<string, Intl.RelativeTimeFormatOptions>;
|
|
46
|
+
}
|
|
47
|
+
export interface CustomFormatConfig<Source = string> {
|
|
48
|
+
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
49
|
+
}
|
|
50
|
+
export declare type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
51
|
+
export declare type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
52
|
+
export declare type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
53
|
+
export declare type FormatPluralOptions = Omit<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
|
|
54
|
+
export declare type FormatListOptions = Omit<IntlListFormatOptions, 'localeMatcher'>;
|
|
55
|
+
export declare type FormatDisplayNameOptions = Omit<DisplayNamesOptions, 'localeMatcher'>;
|
|
56
|
+
export interface IntlFormatters<T = any, R = T> {
|
|
57
|
+
formatDateTimeRange(from: Parameters<DateTimeFormat['formatRange']>[0], to: Parameters<DateTimeFormat['formatRange']>[1], opts?: FormatDateOptions): string;
|
|
58
|
+
formatDate(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
59
|
+
formatTime(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
60
|
+
formatDateToParts(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
61
|
+
formatTimeToParts(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
62
|
+
formatRelativeTime(value: Parameters<Intl.RelativeTimeFormat['format']>[0], unit?: Parameters<Intl.RelativeTimeFormat['format']>[1], opts?: FormatRelativeTimeOptions): string;
|
|
63
|
+
formatNumber(value: Parameters<Intl.NumberFormat['format']>[0], opts?: FormatNumberOptions): string;
|
|
64
|
+
formatNumberToParts(value: Parameters<Intl.NumberFormat['format']>[0], opts?: FormatNumberOptions): Intl.NumberFormatPart[];
|
|
65
|
+
formatPlural(value: Parameters<Intl.PluralRules['select']>[0], opts?: FormatPluralOptions): ReturnType<Intl.PluralRules['select']>;
|
|
66
|
+
formatMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
67
|
+
formatMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T, R>>, opts?: IntlMessageFormatOptions): R;
|
|
68
|
+
$t(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
69
|
+
$t(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T, R>>, opts?: IntlMessageFormatOptions): R;
|
|
70
|
+
formatList(values: ReadonlyArray<string>, opts?: FormatListOptions): string;
|
|
71
|
+
formatList(values: ReadonlyArray<string | T>, opts?: FormatListOptions): T | string | Array<string | T>;
|
|
72
|
+
formatListToParts(values: ReadonlyArray<string | T>, opts?: FormatListOptions): Part[];
|
|
73
|
+
formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
74
|
+
}
|
|
75
|
+
export interface Formatters {
|
|
76
|
+
getDateTimeFormat(...args: ConstructorParameters<typeof Intl.DateTimeFormat>): DateTimeFormat;
|
|
77
|
+
getNumberFormat(locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
|
|
78
|
+
getMessageFormat(...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
|
|
79
|
+
getRelativeTimeFormat(...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
|
|
80
|
+
getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
81
|
+
getListFormat(...args: ConstructorParameters<typeof IntlListFormat>): IntlListFormat;
|
|
82
|
+
getDisplayNames(...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
|
|
83
|
+
}
|
|
84
|
+
export interface IntlShape<T = string> extends ResolvedIntlConfig<T>, IntlFormatters {
|
|
85
|
+
formatters: Formatters;
|
|
86
|
+
}
|
|
87
|
+
export interface IntlCache {
|
|
88
|
+
dateTime: Record<string, DateTimeFormat>;
|
|
89
|
+
number: Record<string, Intl.NumberFormat>;
|
|
90
|
+
message: Record<string, IntlMessageFormat>;
|
|
91
|
+
relativeTime: Record<string, Intl.RelativeTimeFormat>;
|
|
92
|
+
pluralRules: Record<string, Intl.PluralRules>;
|
|
93
|
+
list: Record<string, IntlListFormat>;
|
|
94
|
+
displayNames: Record<string, DisplayNames>;
|
|
95
|
+
}
|
|
96
|
+
export interface MessageDescriptor {
|
|
97
|
+
id?: MessageIds;
|
|
98
|
+
description?: string | object;
|
|
99
|
+
defaultMessage?: string | MessageFormatElement[];
|
|
100
|
+
}
|
|
101
|
+
export declare type IntlConfig<T = string> = Omit<ResolvedIntlConfig<T>, keyof typeof DEFAULT_INTL_CONFIG> & Partial<typeof DEFAULT_INTL_CONFIG>;
|
|
102
|
+
export {};
|
|
103
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,OAAO,EACP,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,OAAO,IAAI,wBAAwB,EACpC,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O,CAAC,OAAO,mBAAmB,CAAC,CAAA"}
|
package/lib/src/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IntlCache, CustomFormats, Formatters, OnErrorFn, ResolvedIntlConfig } from './types';
|
|
2
|
+
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
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
|
+
export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, 'fallbackOnEmptyString' | 'formats' | 'messages' | 'timeZone' | 'defaultLocale' | 'defaultFormats' | 'onError' | 'onWarn'>;
|
|
5
|
+
export declare function createIntlCache(): IntlCache;
|
|
6
|
+
/**
|
|
7
|
+
* Create intl formatters and populate cache
|
|
8
|
+
* @param cache explicit cache to prevent leaking memory
|
|
9
|
+
*/
|
|
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;
|
|
12
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,aAAa,EACb,UAAU,EACV,SAAS,EAET,kBAAkB,EACnB,MAAM,SAAS,CAAA;AAIhB,OAAO,EAAiB,mBAAmB,EAAC,MAAM,4BAA4B,CAAA;AAE9E,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,SAAS,MAAM,EACzE,KAAK,EAAE,CAAC,EACR,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC,EACnB,QAAQ,GAAE,OAAO,CAAC,CAAC,CAAM,GACxB,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAUZ;AAgBD,eAAO,MAAM,mBAAmB,EAAE,IAAI,CACpC,kBAAkB,CAAC,GAAG,CAAC,EACrB,uBAAuB,GACvB,SAAS,GACT,UAAU,GACV,UAAU,GACV,eAAe,GACf,gBAAgB,GAChB,SAAS,GACT,QAAQ,CAaX,CAAA;AAED,wBAAgB,eAAe,IAAI,SAAS,CAU3C;AAiBD;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,GAAE,SAA6B,GACnC,UAAU,CAsDZ;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,aAAa,EAC1D,OAAO,EAAE,aAAa,EACtB,IAAI,EAAE,CAAC,EACP,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,SAAS,GAEhB,mBAAmB,GACnB,IAAI,CAAC,qBAAqB,GAC1B,IAAI,CAAC,yBAAyB,GAC9B,SAAS,CAWZ"}
|
package/lib/src/utils.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { __assign, __spreadArray } from "tslib";
|
|
2
|
+
import { IntlMessageFormat } from 'intl-messageformat';
|
|
3
|
+
import memoize, { strategies } from '@formatjs/fast-memoize';
|
|
4
|
+
import { UnsupportedFormatterError } from './error';
|
|
5
|
+
export function filterProps(props, allowlist, defaults) {
|
|
6
|
+
if (defaults === void 0) { defaults = {}; }
|
|
7
|
+
return allowlist.reduce(function (filtered, name) {
|
|
8
|
+
if (name in props) {
|
|
9
|
+
filtered[name] = props[name];
|
|
10
|
+
}
|
|
11
|
+
else if (name in defaults) {
|
|
12
|
+
filtered[name] = defaults[name];
|
|
13
|
+
}
|
|
14
|
+
return filtered;
|
|
15
|
+
}, {});
|
|
16
|
+
}
|
|
17
|
+
var defaultErrorHandler = function (error) {
|
|
18
|
+
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
19
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
20
|
+
console.error(error);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var defaultWarnHandler = function (warning) {
|
|
24
|
+
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
25
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
26
|
+
console.warn(warning);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
export var DEFAULT_INTL_CONFIG = {
|
|
30
|
+
formats: {},
|
|
31
|
+
messages: {},
|
|
32
|
+
timeZone: undefined,
|
|
33
|
+
defaultLocale: 'en',
|
|
34
|
+
defaultFormats: {},
|
|
35
|
+
fallbackOnEmptyString: true,
|
|
36
|
+
onError: defaultErrorHandler,
|
|
37
|
+
onWarn: defaultWarnHandler,
|
|
38
|
+
};
|
|
39
|
+
export function createIntlCache() {
|
|
40
|
+
return {
|
|
41
|
+
dateTime: {},
|
|
42
|
+
number: {},
|
|
43
|
+
message: {},
|
|
44
|
+
relativeTime: {},
|
|
45
|
+
pluralRules: {},
|
|
46
|
+
list: {},
|
|
47
|
+
displayNames: {},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function createFastMemoizeCache(store) {
|
|
51
|
+
return {
|
|
52
|
+
create: function () {
|
|
53
|
+
return {
|
|
54
|
+
get: function (key) {
|
|
55
|
+
return store[key];
|
|
56
|
+
},
|
|
57
|
+
set: function (key, value) {
|
|
58
|
+
store[key] = value;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Create intl formatters and populate cache
|
|
66
|
+
* @param cache explicit cache to prevent leaking memory
|
|
67
|
+
*/
|
|
68
|
+
export function createFormatters(cache) {
|
|
69
|
+
if (cache === void 0) { cache = createIntlCache(); }
|
|
70
|
+
var RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
71
|
+
var ListFormat = Intl.ListFormat;
|
|
72
|
+
var DisplayNames = Intl.DisplayNames;
|
|
73
|
+
var getDateTimeFormat = memoize(function () {
|
|
74
|
+
var _a;
|
|
75
|
+
var args = [];
|
|
76
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
77
|
+
args[_i] = arguments[_i];
|
|
78
|
+
}
|
|
79
|
+
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
80
|
+
}, {
|
|
81
|
+
cache: createFastMemoizeCache(cache.dateTime),
|
|
82
|
+
strategy: strategies.variadic,
|
|
83
|
+
});
|
|
84
|
+
var getNumberFormat = memoize(function () {
|
|
85
|
+
var _a;
|
|
86
|
+
var args = [];
|
|
87
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
88
|
+
args[_i] = arguments[_i];
|
|
89
|
+
}
|
|
90
|
+
return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
91
|
+
}, {
|
|
92
|
+
cache: createFastMemoizeCache(cache.number),
|
|
93
|
+
strategy: strategies.variadic,
|
|
94
|
+
});
|
|
95
|
+
var getPluralRules = memoize(function () {
|
|
96
|
+
var _a;
|
|
97
|
+
var args = [];
|
|
98
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
99
|
+
args[_i] = arguments[_i];
|
|
100
|
+
}
|
|
101
|
+
return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
102
|
+
}, {
|
|
103
|
+
cache: createFastMemoizeCache(cache.pluralRules),
|
|
104
|
+
strategy: strategies.variadic,
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
getDateTimeFormat: getDateTimeFormat,
|
|
108
|
+
getNumberFormat: getNumberFormat,
|
|
109
|
+
getMessageFormat: memoize(function (message, locales, overrideFormats, opts) {
|
|
110
|
+
return new IntlMessageFormat(message, locales, overrideFormats, __assign({ formatters: {
|
|
111
|
+
getNumberFormat: getNumberFormat,
|
|
112
|
+
getDateTimeFormat: getDateTimeFormat,
|
|
113
|
+
getPluralRules: getPluralRules,
|
|
114
|
+
} }, (opts || {})));
|
|
115
|
+
}, {
|
|
116
|
+
cache: createFastMemoizeCache(cache.message),
|
|
117
|
+
strategy: strategies.variadic,
|
|
118
|
+
}),
|
|
119
|
+
getRelativeTimeFormat: memoize(function () {
|
|
120
|
+
var args = [];
|
|
121
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
122
|
+
args[_i] = arguments[_i];
|
|
123
|
+
}
|
|
124
|
+
return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat, __spreadArray([void 0], args, false)))();
|
|
125
|
+
}, {
|
|
126
|
+
cache: createFastMemoizeCache(cache.relativeTime),
|
|
127
|
+
strategy: strategies.variadic,
|
|
128
|
+
}),
|
|
129
|
+
getPluralRules: getPluralRules,
|
|
130
|
+
getListFormat: memoize(function () {
|
|
131
|
+
var args = [];
|
|
132
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
133
|
+
args[_i] = arguments[_i];
|
|
134
|
+
}
|
|
135
|
+
return new (ListFormat.bind.apply(ListFormat, __spreadArray([void 0], args, false)))();
|
|
136
|
+
}, {
|
|
137
|
+
cache: createFastMemoizeCache(cache.list),
|
|
138
|
+
strategy: strategies.variadic,
|
|
139
|
+
}),
|
|
140
|
+
getDisplayNames: memoize(function () {
|
|
141
|
+
var args = [];
|
|
142
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
143
|
+
args[_i] = arguments[_i];
|
|
144
|
+
}
|
|
145
|
+
return new (DisplayNames.bind.apply(DisplayNames, __spreadArray([void 0], args, false)))();
|
|
146
|
+
}, {
|
|
147
|
+
cache: createFastMemoizeCache(cache.displayNames),
|
|
148
|
+
strategy: strategies.variadic,
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export function getNamedFormat(formats, type, name, onError) {
|
|
153
|
+
var formatType = formats && formats[type];
|
|
154
|
+
var format;
|
|
155
|
+
if (formatType) {
|
|
156
|
+
format = formatType[name];
|
|
157
|
+
}
|
|
158
|
+
if (format) {
|
|
159
|
+
return format;
|
|
160
|
+
}
|
|
161
|
+
onError(new UnsupportedFormatterError("No ".concat(type, " format named: ").concat(name)));
|
|
162
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/intl",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.4",
|
|
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",
|
|
@@ -29,17 +29,17 @@
|
|
|
29
29
|
"module": "lib/index.js",
|
|
30
30
|
"sideEffects": false,
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@formatjs/ecma402-abstract": "1.11.
|
|
33
|
-
"@formatjs/fast-memoize": "1.2.
|
|
34
|
-
"@formatjs/icu-messageformat-parser": "2.1.
|
|
35
|
-
"@formatjs/intl-displaynames": "6.1.
|
|
36
|
-
"@formatjs/intl-listformat": "7.1.
|
|
37
|
-
"intl-messageformat": "10.1.
|
|
32
|
+
"@formatjs/ecma402-abstract": "1.11.10",
|
|
33
|
+
"@formatjs/fast-memoize": "1.2.6",
|
|
34
|
+
"@formatjs/icu-messageformat-parser": "2.1.6",
|
|
35
|
+
"@formatjs/intl-displaynames": "6.1.1",
|
|
36
|
+
"@formatjs/intl-listformat": "7.1.1",
|
|
37
|
+
"intl-messageformat": "10.1.3",
|
|
38
38
|
"tslib": "2.4.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@formatjs/intl-datetimeformat": "6.1.
|
|
42
|
-
"@formatjs/intl-numberformat": "8.1.
|
|
41
|
+
"@formatjs/intl-datetimeformat": "6.1.1",
|
|
42
|
+
"@formatjs/intl-numberformat": "8.1.2"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"typescript": "^4.5"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IntlCache, IntlShape, IntlConfig } from './types';
|
|
2
|
+
export interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
|
|
3
|
+
(config: C, cache?: IntlCache): S;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Create intl object
|
|
7
|
+
* @param config intl config
|
|
8
|
+
* @param cache cache for formatter instances to prevent memory leak
|
|
9
|
+
*/
|
|
10
|
+
export declare function createIntl<T = string>(config: IntlConfig<T>, cache?: IntlCache): IntlShape<T>;
|
|
11
|
+
//# sourceMappingURL=create-intl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-intl.d.ts","sourceRoot":"","sources":["create-intl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,SAAS,EAAE,UAAU,EAAqB,MAAM,SAAS,CAAA;AAkB5E,MAAM,WAAW,YAAY,CAC3B,CAAC,GAAG,MAAM,EACV,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,EACvC,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;IAErC,CAAC,MAAM,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,SAAS,GAAG,CAAC,CAAA;CAClC;AAsBD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,MAAM,EACnC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,EACrB,KAAK,CAAC,EAAE,SAAS,GAChB,SAAS,CAAC,CAAC,CAAC,CAwGd"}
|