@formatjs/intl 2.3.2 → 2.4.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/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 +10 -10
- 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
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MessageDescriptor } from './src/types';
|
|
2
|
+
export * from './src/types';
|
|
3
|
+
export declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
|
|
4
|
+
export declare function defineMessage<T>(msg: T): T;
|
|
5
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
|
|
6
|
+
export * from './src/error';
|
|
7
|
+
export { formatMessage } from './src/message';
|
|
8
|
+
export type { FormatMessageFn } from './src/message';
|
|
9
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
|
|
10
|
+
export { formatDisplayName } from './src/displayName';
|
|
11
|
+
export { formatList } from './src/list';
|
|
12
|
+
export { formatPlural } from './src/plural';
|
|
13
|
+
export { formatRelativeTime } from './src/relativeTime';
|
|
14
|
+
export { formatNumber, formatNumberToParts } from './src/number';
|
|
15
|
+
export { createIntl } from './src/create-intl';
|
|
16
|
+
export type { CreateIntlFn } from './src/create-intl';
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAA;AAC7C,cAAc,aAAa,CAAA;AAE3B,wBAAgB,cAAc,CAC5B,CAAC,SAAS,MAAM,GAAG,EACnB,CAAC,GAAG,iBAAiB,EACrB,CAAC,SAAS,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EACrC,IAAI,EAAE,CAAC,GAAG,CAAC,CAEZ;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAE1C;AAED,OAAO,EACL,eAAe,EACf,WAAW,EACX,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,GACf,MAAM,aAAa,CAAA;AACpB,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAC,aAAa,EAAC,MAAM,eAAe,CAAA;AAC3C,YAAY,EAAC,eAAe,EAAC,MAAM,eAAe,CAAA;AAClD,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GAClB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAA;AACrC,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAA;AACzC,OAAO,EAAC,kBAAkB,EAAC,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAC,YAAY,EAAE,mBAAmB,EAAC,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAC,UAAU,EAAC,MAAM,mBAAmB,CAAA;AAC5C,YAAY,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA"}
|
package/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createIntl = exports.formatNumberToParts = exports.formatNumber = exports.formatRelativeTime = exports.formatPlural = exports.formatList = exports.formatDisplayName = exports.formatTimeToParts = exports.formatTime = exports.formatDateToParts = exports.formatDate = exports.formatMessage = exports.getNamedFormat = exports.createFormatters = exports.DEFAULT_INTL_CONFIG = exports.filterProps = exports.createIntlCache = exports.defineMessage = exports.defineMessages = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./src/types"), exports);
|
|
6
|
+
function defineMessages(msgs) {
|
|
7
|
+
return msgs;
|
|
8
|
+
}
|
|
9
|
+
exports.defineMessages = defineMessages;
|
|
10
|
+
function defineMessage(msg) {
|
|
11
|
+
return msg;
|
|
12
|
+
}
|
|
13
|
+
exports.defineMessage = defineMessage;
|
|
14
|
+
var utils_1 = require("./src/utils");
|
|
15
|
+
Object.defineProperty(exports, "createIntlCache", { enumerable: true, get: function () { return utils_1.createIntlCache; } });
|
|
16
|
+
Object.defineProperty(exports, "filterProps", { enumerable: true, get: function () { return utils_1.filterProps; } });
|
|
17
|
+
Object.defineProperty(exports, "DEFAULT_INTL_CONFIG", { enumerable: true, get: function () { return utils_1.DEFAULT_INTL_CONFIG; } });
|
|
18
|
+
Object.defineProperty(exports, "createFormatters", { enumerable: true, get: function () { return utils_1.createFormatters; } });
|
|
19
|
+
Object.defineProperty(exports, "getNamedFormat", { enumerable: true, get: function () { return utils_1.getNamedFormat; } });
|
|
20
|
+
tslib_1.__exportStar(require("./src/error"), exports);
|
|
21
|
+
var message_1 = require("./src/message");
|
|
22
|
+
Object.defineProperty(exports, "formatMessage", { enumerable: true, get: function () { return message_1.formatMessage; } });
|
|
23
|
+
var dateTime_1 = require("./src/dateTime");
|
|
24
|
+
Object.defineProperty(exports, "formatDate", { enumerable: true, get: function () { return dateTime_1.formatDate; } });
|
|
25
|
+
Object.defineProperty(exports, "formatDateToParts", { enumerable: true, get: function () { return dateTime_1.formatDateToParts; } });
|
|
26
|
+
Object.defineProperty(exports, "formatTime", { enumerable: true, get: function () { return dateTime_1.formatTime; } });
|
|
27
|
+
Object.defineProperty(exports, "formatTimeToParts", { enumerable: true, get: function () { return dateTime_1.formatTimeToParts; } });
|
|
28
|
+
var displayName_1 = require("./src/displayName");
|
|
29
|
+
Object.defineProperty(exports, "formatDisplayName", { enumerable: true, get: function () { return displayName_1.formatDisplayName; } });
|
|
30
|
+
var list_1 = require("./src/list");
|
|
31
|
+
Object.defineProperty(exports, "formatList", { enumerable: true, get: function () { return list_1.formatList; } });
|
|
32
|
+
var plural_1 = require("./src/plural");
|
|
33
|
+
Object.defineProperty(exports, "formatPlural", { enumerable: true, get: function () { return plural_1.formatPlural; } });
|
|
34
|
+
var relativeTime_1 = require("./src/relativeTime");
|
|
35
|
+
Object.defineProperty(exports, "formatRelativeTime", { enumerable: true, get: function () { return relativeTime_1.formatRelativeTime; } });
|
|
36
|
+
var number_1 = require("./src/number");
|
|
37
|
+
Object.defineProperty(exports, "formatNumber", { enumerable: true, get: function () { return number_1.formatNumber; } });
|
|
38
|
+
Object.defineProperty(exports, "formatNumberToParts", { enumerable: true, get: function () { return number_1.formatNumberToParts; } });
|
|
39
|
+
var create_intl_1 = require("./src/create-intl");
|
|
40
|
+
Object.defineProperty(exports, "createIntl", { enumerable: true, get: function () { return create_intl_1.createIntl; } });
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MessageDescriptor } from './src/types';
|
|
2
|
+
export * from './src/types';
|
|
3
|
+
export declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
|
|
4
|
+
export declare function defineMessage<T>(msg: T): T;
|
|
5
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
|
|
6
|
+
export * from './src/error';
|
|
7
|
+
export { formatMessage } from './src/message';
|
|
8
|
+
export type { FormatMessageFn } from './src/message';
|
|
9
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
|
|
10
|
+
export { formatDisplayName } from './src/displayName';
|
|
11
|
+
export { formatList } from './src/list';
|
|
12
|
+
export { formatPlural } from './src/plural';
|
|
13
|
+
export { formatRelativeTime } from './src/relativeTime';
|
|
14
|
+
export { formatNumber, formatNumberToParts } from './src/number';
|
|
15
|
+
export { createIntl } from './src/create-intl';
|
|
16
|
+
export type { CreateIntlFn } from './src/create-intl';
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAA;AAC7C,cAAc,aAAa,CAAA;AAE3B,wBAAgB,cAAc,CAC5B,CAAC,SAAS,MAAM,GAAG,EACnB,CAAC,GAAG,iBAAiB,EACrB,CAAC,SAAS,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EACrC,IAAI,EAAE,CAAC,GAAG,CAAC,CAEZ;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAE1C;AAED,OAAO,EACL,eAAe,EACf,WAAW,EACX,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,GACf,MAAM,aAAa,CAAA;AACpB,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAC,aAAa,EAAC,MAAM,eAAe,CAAA;AAC3C,YAAY,EAAC,eAAe,EAAC,MAAM,eAAe,CAAA;AAClD,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GAClB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAA;AACrC,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAA;AACzC,OAAO,EAAC,kBAAkB,EAAC,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAC,YAAY,EAAE,mBAAmB,EAAC,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAC,UAAU,EAAC,MAAM,mBAAmB,CAAA;AAC5C,YAAY,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from './src/types';
|
|
2
|
+
export function defineMessages(msgs) {
|
|
3
|
+
return msgs;
|
|
4
|
+
}
|
|
5
|
+
export function defineMessage(msg) {
|
|
6
|
+
return msg;
|
|
7
|
+
}
|
|
8
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
|
|
9
|
+
export * from './src/error';
|
|
10
|
+
export { formatMessage } from './src/message';
|
|
11
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
|
|
12
|
+
export { formatDisplayName } from './src/displayName';
|
|
13
|
+
export { formatList } from './src/list';
|
|
14
|
+
export { formatPlural } from './src/plural';
|
|
15
|
+
export { formatRelativeTime } from './src/relativeTime';
|
|
16
|
+
export { formatNumber, formatNumberToParts } from './src/number';
|
|
17
|
+
export { createIntl } from './src/create-intl';
|
|
@@ -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":["../../src/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"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { createFormatters, DEFAULT_INTL_CONFIG } from './utils';
|
|
3
|
+
import { InvalidConfigError, MissingDataError } from './error';
|
|
4
|
+
import { formatNumber, formatNumberToParts } from './number';
|
|
5
|
+
import { formatRelativeTime } from './relativeTime';
|
|
6
|
+
import { formatDate, formatDateToParts, formatTime, formatTimeToParts, formatDateTimeRange, } from './dateTime';
|
|
7
|
+
import { formatPlural } from './plural';
|
|
8
|
+
import { formatMessage } from './message';
|
|
9
|
+
import { formatList, formatListToParts } from './list';
|
|
10
|
+
import { formatDisplayName } from './displayName';
|
|
11
|
+
function messagesContainString(messages) {
|
|
12
|
+
var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
|
|
13
|
+
return typeof firstMessage === 'string';
|
|
14
|
+
}
|
|
15
|
+
function verifyConfigMessages(config) {
|
|
16
|
+
if (config.onWarn &&
|
|
17
|
+
config.defaultRichTextElements &&
|
|
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");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create intl object
|
|
24
|
+
* @param config intl config
|
|
25
|
+
* @param cache cache for formatter instances to prevent memory leak
|
|
26
|
+
*/
|
|
27
|
+
export function createIntl(config, cache) {
|
|
28
|
+
var formatters = createFormatters(cache);
|
|
29
|
+
var resolvedConfig = __assign(__assign({}, DEFAULT_INTL_CONFIG), config);
|
|
30
|
+
var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
|
|
31
|
+
if (!locale) {
|
|
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")));
|
|
34
|
+
}
|
|
35
|
+
// Since there's no registered locale data for `locale`, this will
|
|
36
|
+
// fallback to the `defaultLocale` to make sure things can render.
|
|
37
|
+
// The `messages` are overridden to the `defaultProps` empty object
|
|
38
|
+
// to maintain referential equality across re-renders. It's assumed
|
|
39
|
+
// each <FormattedMessage> contains a `defaultMessage` prop.
|
|
40
|
+
resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
|
|
41
|
+
}
|
|
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")));
|
|
44
|
+
}
|
|
45
|
+
else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
|
|
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")));
|
|
48
|
+
}
|
|
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) });
|
|
55
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Formatters, IntlFormatters, CustomFormats, OnErrorFn } from './types';
|
|
2
|
+
import { DateTimeFormat } from '@formatjs/ecma402-abstract';
|
|
3
|
+
export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
4
|
+
locale: string;
|
|
5
|
+
timeZone?: string;
|
|
6
|
+
formats: CustomFormats;
|
|
7
|
+
onError: OnErrorFn;
|
|
8
|
+
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): DateTimeFormat;
|
|
9
|
+
export declare function formatDate(config: {
|
|
10
|
+
locale: string;
|
|
11
|
+
timeZone?: string;
|
|
12
|
+
formats: CustomFormats;
|
|
13
|
+
onError: OnErrorFn;
|
|
14
|
+
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): string;
|
|
15
|
+
export declare function formatTime(config: {
|
|
16
|
+
locale: string;
|
|
17
|
+
timeZone?: string;
|
|
18
|
+
formats: CustomFormats;
|
|
19
|
+
onError: OnErrorFn;
|
|
20
|
+
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTime']>): string;
|
|
21
|
+
export declare function formatDateTimeRange(config: {
|
|
22
|
+
locale: string;
|
|
23
|
+
timeZone?: string;
|
|
24
|
+
onError: OnErrorFn;
|
|
25
|
+
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[from, to, options]: Parameters<IntlFormatters['formatDateTimeRange']>): string;
|
|
26
|
+
export declare function formatDateToParts(config: {
|
|
27
|
+
locale: string;
|
|
28
|
+
timeZone?: string;
|
|
29
|
+
formats: CustomFormats;
|
|
30
|
+
onError: OnErrorFn;
|
|
31
|
+
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): Intl.DateTimeFormatPart[];
|
|
32
|
+
export declare function formatTimeToParts(config: {
|
|
33
|
+
locale: string;
|
|
34
|
+
timeZone?: string;
|
|
35
|
+
formats: CustomFormats;
|
|
36
|
+
onError: OnErrorFn;
|
|
37
|
+
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTimeToParts']>): Intl.DateTimeFormatPart[];
|
|
38
|
+
//# sourceMappingURL=dateTime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dateTime.d.ts","sourceRoot":"","sources":["../../src/dateTime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAI5E,OAAO,EAAC,cAAc,EAAC,MAAM,4BAA4B,CAAA;AA0BzD,wBAAgB,YAAY,CAC1B,EACE,MAAM,EACN,OAAO,EACP,OAAO,EACP,QAAQ,GACT,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,O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AAE;IACN,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,iBAAiB,EAAE,UAAU,CAAC,mBAAmB,CAAC,EAClD,GAAG,CAAC,KAAK,EAAE,OAAY,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,GACjE,IAAI,CAAC,kBAAkB,EAAE,CAgB3B;AAED,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,iBAAiB,EAAE,UAAU,CAAC,mBAAmB,CAAC,EAClD,GAAG,CAAC,KAAK,EAAE,OAAY,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,GACxE,IAAI,CAAC,kBAAkB,EAAE,CAiB3B"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { filterProps, getNamedFormat } from './utils';
|
|
3
|
+
import { IntlError, IntlErrorCode } from './error';
|
|
4
|
+
var 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
|
+
// 'dayPeriod',
|
|
22
|
+
'numberingSystem',
|
|
23
|
+
'fractionalSecondDigits',
|
|
24
|
+
];
|
|
25
|
+
export function getFormatter(_a, type, getDateTimeFormat, options) {
|
|
26
|
+
var locale = _a.locale, formats = _a.formats, onError = _a.onError, timeZone = _a.timeZone;
|
|
27
|
+
if (options === void 0) { options = {}; }
|
|
28
|
+
var format = options.format;
|
|
29
|
+
var defaults = __assign(__assign({}, (timeZone && { timeZone: timeZone })), (format && getNamedFormat(formats, type, format, onError)));
|
|
30
|
+
var filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
|
|
31
|
+
if (type === 'time' &&
|
|
32
|
+
!filteredOptions.hour &&
|
|
33
|
+
!filteredOptions.minute &&
|
|
34
|
+
!filteredOptions.second &&
|
|
35
|
+
!filteredOptions.timeStyle &&
|
|
36
|
+
!filteredOptions.dateStyle) {
|
|
37
|
+
// Add default formatting options if hour, minute, or second isn't defined.
|
|
38
|
+
filteredOptions = __assign(__assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' });
|
|
39
|
+
}
|
|
40
|
+
return getDateTimeFormat(locale, filteredOptions);
|
|
41
|
+
}
|
|
42
|
+
export function formatDate(config, getDateTimeFormat) {
|
|
43
|
+
var _a = [];
|
|
44
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
45
|
+
_a[_i - 2] = arguments[_i];
|
|
46
|
+
}
|
|
47
|
+
var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
|
48
|
+
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
49
|
+
try {
|
|
50
|
+
return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
config.onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting date.', e));
|
|
54
|
+
}
|
|
55
|
+
return String(date);
|
|
56
|
+
}
|
|
57
|
+
export function formatTime(config, getDateTimeFormat) {
|
|
58
|
+
var _a = [];
|
|
59
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
60
|
+
_a[_i - 2] = arguments[_i];
|
|
61
|
+
}
|
|
62
|
+
var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
|
63
|
+
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
64
|
+
try {
|
|
65
|
+
return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
config.onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting time.', e));
|
|
69
|
+
}
|
|
70
|
+
return String(date);
|
|
71
|
+
}
|
|
72
|
+
export function formatDateTimeRange(config, getDateTimeFormat) {
|
|
73
|
+
var _a = [];
|
|
74
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
75
|
+
_a[_i - 2] = arguments[_i];
|
|
76
|
+
}
|
|
77
|
+
var from = _a[0], to = _a[1], _b = _a[2], options = _b === void 0 ? {} : _b;
|
|
78
|
+
var timeZone = config.timeZone, locale = config.locale, onError = config.onError;
|
|
79
|
+
var filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, timeZone ? { timeZone: timeZone } : {});
|
|
80
|
+
try {
|
|
81
|
+
return getDateTimeFormat(locale, filteredOptions).formatRange(from, to);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting date time range.', e));
|
|
85
|
+
}
|
|
86
|
+
return String(from);
|
|
87
|
+
}
|
|
88
|
+
export function formatDateToParts(config, getDateTimeFormat) {
|
|
89
|
+
var _a = [];
|
|
90
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
91
|
+
_a[_i - 2] = arguments[_i];
|
|
92
|
+
}
|
|
93
|
+
var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
|
94
|
+
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
95
|
+
try {
|
|
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 IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting date.', e));
|
|
100
|
+
}
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
export function formatTimeToParts(config, getDateTimeFormat) {
|
|
104
|
+
var _a = [];
|
|
105
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
106
|
+
_a[_i - 2] = arguments[_i];
|
|
107
|
+
}
|
|
108
|
+
var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
|
109
|
+
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
110
|
+
try {
|
|
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 IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting time.', e));
|
|
115
|
+
}
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
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;
|
|
6
|
+
//# sourceMappingURL=displayName.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"displayName.d.ts","sourceRoot":"","sources":["../../src/displayName.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAe7D,wBAAgB,iBAAiB,CAC/B,EACE,MAAM,EACN,OAAO,GACR,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,EACzD,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,MAAM,GAAG,SAAS,CA2BpB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { filterProps } from './utils';
|
|
2
|
+
import { FormatError, ErrorCode } from 'intl-messageformat';
|
|
3
|
+
import { IntlErrorCode, IntlError } from './error';
|
|
4
|
+
var DISPLAY_NAMES_OPTONS = [
|
|
5
|
+
'style',
|
|
6
|
+
'type',
|
|
7
|
+
'fallback',
|
|
8
|
+
];
|
|
9
|
+
export function formatDisplayName(_a, getDisplayNames, value, options) {
|
|
10
|
+
var locale = _a.locale, onError = _a.onError;
|
|
11
|
+
var DisplayNames = Intl.DisplayNames;
|
|
12
|
+
if (!DisplayNames) {
|
|
13
|
+
onError(new FormatError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n", ErrorCode.MISSING_INTL_API));
|
|
14
|
+
}
|
|
15
|
+
var filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
|
|
16
|
+
try {
|
|
17
|
+
return getDisplayNames(locale, filteredOptions).of(value);
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting display name.', e));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { MessageDescriptor } from './types';
|
|
2
|
+
export declare enum IntlErrorCode {
|
|
3
|
+
FORMAT_ERROR = "FORMAT_ERROR",
|
|
4
|
+
UNSUPPORTED_FORMATTER = "UNSUPPORTED_FORMATTER",
|
|
5
|
+
INVALID_CONFIG = "INVALID_CONFIG",
|
|
6
|
+
MISSING_DATA = "MISSING_DATA",
|
|
7
|
+
MISSING_TRANSLATION = "MISSING_TRANSLATION"
|
|
8
|
+
}
|
|
9
|
+
export declare class IntlError<T extends IntlErrorCode = IntlErrorCode.FORMAT_ERROR> extends Error {
|
|
10
|
+
readonly code: T;
|
|
11
|
+
constructor(code: T, message: string, exception?: Error | unknown);
|
|
12
|
+
}
|
|
13
|
+
export declare class UnsupportedFormatterError extends IntlError<IntlErrorCode.UNSUPPORTED_FORMATTER> {
|
|
14
|
+
constructor(message: string, exception?: Error | unknown);
|
|
15
|
+
}
|
|
16
|
+
export declare class InvalidConfigError extends IntlError<IntlErrorCode.INVALID_CONFIG> {
|
|
17
|
+
constructor(message: string, exception?: Error | unknown);
|
|
18
|
+
}
|
|
19
|
+
export declare class MissingDataError extends IntlError<IntlErrorCode.MISSING_DATA> {
|
|
20
|
+
constructor(message: string, exception?: Error | unknown);
|
|
21
|
+
}
|
|
22
|
+
export declare class IntlFormatError extends IntlError<IntlErrorCode.FORMAT_ERROR> {
|
|
23
|
+
readonly descriptor?: MessageDescriptor;
|
|
24
|
+
constructor(message: string, locale: string, exception?: Error | unknown);
|
|
25
|
+
}
|
|
26
|
+
export declare class MessageFormatError extends IntlFormatError {
|
|
27
|
+
readonly descriptor?: MessageDescriptor;
|
|
28
|
+
constructor(message: string, locale: string, descriptor?: MessageDescriptor, exception?: Error | unknown);
|
|
29
|
+
}
|
|
30
|
+
export declare class MissingTranslationError extends IntlError<IntlErrorCode.MISSING_TRANSLATION> {
|
|
31
|
+
readonly descriptor?: MessageDescriptor;
|
|
32
|
+
constructor(descriptor: MessageDescriptor, locale: string);
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,SAAS,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AS,CAAC,aAAa,CAAC,mBAAmB,CAAC;IACvF,SAAgB,UAAU,CAAC,EAAE,iBAAiB,CAAA;gBAClC,UAAU,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM;CAiB1D"}
|
package/lib/src/error.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { __extends } from "tslib";
|
|
2
|
+
export var IntlErrorCode;
|
|
3
|
+
(function (IntlErrorCode) {
|
|
4
|
+
IntlErrorCode["FORMAT_ERROR"] = "FORMAT_ERROR";
|
|
5
|
+
IntlErrorCode["UNSUPPORTED_FORMATTER"] = "UNSUPPORTED_FORMATTER";
|
|
6
|
+
IntlErrorCode["INVALID_CONFIG"] = "INVALID_CONFIG";
|
|
7
|
+
IntlErrorCode["MISSING_DATA"] = "MISSING_DATA";
|
|
8
|
+
IntlErrorCode["MISSING_TRANSLATION"] = "MISSING_TRANSLATION";
|
|
9
|
+
})(IntlErrorCode || (IntlErrorCode = {}));
|
|
10
|
+
var IntlError = /** @class */ (function (_super) {
|
|
11
|
+
__extends(IntlError, _super);
|
|
12
|
+
function IntlError(code, message, exception) {
|
|
13
|
+
var _this = this;
|
|
14
|
+
var err = exception
|
|
15
|
+
? exception instanceof Error
|
|
16
|
+
? exception
|
|
17
|
+
: new Error(String(exception))
|
|
18
|
+
: undefined;
|
|
19
|
+
_this = _super.call(this, "[@formatjs/intl Error ".concat(code, "] ").concat(message, "\n").concat(err ? "\n".concat(err.message, "\n").concat(err.stack) : '')) || this;
|
|
20
|
+
_this.code = code;
|
|
21
|
+
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
22
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
23
|
+
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
24
|
+
Error.captureStackTrace(_this, IntlError);
|
|
25
|
+
}
|
|
26
|
+
return _this;
|
|
27
|
+
}
|
|
28
|
+
return IntlError;
|
|
29
|
+
}(Error));
|
|
30
|
+
export { IntlError };
|
|
31
|
+
var UnsupportedFormatterError = /** @class */ (function (_super) {
|
|
32
|
+
__extends(UnsupportedFormatterError, _super);
|
|
33
|
+
function UnsupportedFormatterError(message, exception) {
|
|
34
|
+
return _super.call(this, IntlErrorCode.UNSUPPORTED_FORMATTER, message, exception) || this;
|
|
35
|
+
}
|
|
36
|
+
return UnsupportedFormatterError;
|
|
37
|
+
}(IntlError));
|
|
38
|
+
export { UnsupportedFormatterError };
|
|
39
|
+
var InvalidConfigError = /** @class */ (function (_super) {
|
|
40
|
+
__extends(InvalidConfigError, _super);
|
|
41
|
+
function InvalidConfigError(message, exception) {
|
|
42
|
+
return _super.call(this, IntlErrorCode.INVALID_CONFIG, message, exception) || this;
|
|
43
|
+
}
|
|
44
|
+
return InvalidConfigError;
|
|
45
|
+
}(IntlError));
|
|
46
|
+
export { InvalidConfigError };
|
|
47
|
+
var MissingDataError = /** @class */ (function (_super) {
|
|
48
|
+
__extends(MissingDataError, _super);
|
|
49
|
+
function MissingDataError(message, exception) {
|
|
50
|
+
return _super.call(this, IntlErrorCode.MISSING_DATA, message, exception) || this;
|
|
51
|
+
}
|
|
52
|
+
return MissingDataError;
|
|
53
|
+
}(IntlError));
|
|
54
|
+
export { MissingDataError };
|
|
55
|
+
var IntlFormatError = /** @class */ (function (_super) {
|
|
56
|
+
__extends(IntlFormatError, _super);
|
|
57
|
+
function IntlFormatError(message, locale, exception) {
|
|
58
|
+
return _super.call(this, IntlErrorCode.FORMAT_ERROR, "".concat(message, "\nLocale: ").concat(locale, "\n"), exception) || this;
|
|
59
|
+
}
|
|
60
|
+
return IntlFormatError;
|
|
61
|
+
}(IntlError));
|
|
62
|
+
export { IntlFormatError };
|
|
63
|
+
var MessageFormatError = /** @class */ (function (_super) {
|
|
64
|
+
__extends(MessageFormatError, _super);
|
|
65
|
+
function MessageFormatError(message, locale, descriptor, exception) {
|
|
66
|
+
var _this = _super.call(this, "".concat(message, "\nMessageID: ").concat(descriptor === null || descriptor === void 0 ? void 0 : descriptor.id, "\nDefault Message: ").concat(descriptor === null || descriptor === void 0 ? void 0 : descriptor.defaultMessage, "\nDescription: ").concat(descriptor === null || descriptor === void 0 ? void 0 : descriptor.description, "\n"), locale, exception) || this;
|
|
67
|
+
_this.descriptor = descriptor;
|
|
68
|
+
return _this;
|
|
69
|
+
}
|
|
70
|
+
return MessageFormatError;
|
|
71
|
+
}(IntlFormatError));
|
|
72
|
+
export { MessageFormatError };
|
|
73
|
+
var MissingTranslationError = /** @class */ (function (_super) {
|
|
74
|
+
__extends(MissingTranslationError, _super);
|
|
75
|
+
function MissingTranslationError(descriptor, locale) {
|
|
76
|
+
var _this = _super.call(this, IntlErrorCode.MISSING_TRANSLATION, "Missing message: \"".concat(descriptor.id, "\" for locale \"").concat(locale, "\", using ").concat(descriptor.defaultMessage
|
|
77
|
+
? "default message (".concat(typeof descriptor.defaultMessage === 'string'
|
|
78
|
+
? descriptor.defaultMessage
|
|
79
|
+
: descriptor.defaultMessage
|
|
80
|
+
.map(function (e) { var _a; return (_a = e.value) !== null && _a !== void 0 ? _a : JSON.stringify(e); })
|
|
81
|
+
.join(), ")")
|
|
82
|
+
: 'id', " as fallback.")) || this;
|
|
83
|
+
_this.descriptor = descriptor;
|
|
84
|
+
return _this;
|
|
85
|
+
}
|
|
86
|
+
return MissingTranslationError;
|
|
87
|
+
}(IntlError));
|
|
88
|
+
export { MissingTranslationError };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
+
import type { Part } from '@formatjs/intl-listformat';
|
|
3
|
+
export declare function formatList(opts: {
|
|
4
|
+
locale: string;
|
|
5
|
+
onError: OnErrorFn;
|
|
6
|
+
}, getListFormat: Formatters['getListFormat'], values: ReadonlyArray<string>, options: Parameters<IntlFormatters['formatList']>[1]): string;
|
|
7
|
+
export declare function formatListToParts<T>(opts: {
|
|
8
|
+
locale: string;
|
|
9
|
+
onError: OnErrorFn;
|
|
10
|
+
}, getListFormat: Formatters['getListFormat'], values: ReadonlyArray<string>, options: Parameters<IntlFormatters['formatList']>[1]): Part[];
|
|
11
|
+
//# sourceMappingURL=list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAG7D,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,2BAA2B,CAAA;AAgBnD,wBAAgB,UAAU,CACxB,IAAI,EAAE;IACJ,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,aAAa,EAAE,UAAU,CAAC,eAAe,CAAC,EAC1C,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,MAAM,CAAA;AA6BT,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,IAAI,EAAE;IACJ,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,aAAa,EAAE,UAAU,CAAC,eAAe,CAAC,EAC1C,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,IAAI,EAAE,CAAA"}
|
package/lib/src/list.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { filterProps } from './utils';
|
|
3
|
+
import { FormatError, ErrorCode } from 'intl-messageformat';
|
|
4
|
+
import { IntlError, IntlErrorCode } from './error';
|
|
5
|
+
var LIST_FORMAT_OPTIONS = [
|
|
6
|
+
'type',
|
|
7
|
+
'style',
|
|
8
|
+
];
|
|
9
|
+
var now = Date.now();
|
|
10
|
+
function generateToken(i) {
|
|
11
|
+
return "".concat(now, "_").concat(i, "_").concat(now);
|
|
12
|
+
}
|
|
13
|
+
export function formatList(opts, getListFormat, values, options) {
|
|
14
|
+
if (options === void 0) { options = {}; }
|
|
15
|
+
var results = formatListToParts(opts, getListFormat, values, options).reduce(function (all, el) {
|
|
16
|
+
var val = el.value;
|
|
17
|
+
if (typeof val !== 'string') {
|
|
18
|
+
all.push(val);
|
|
19
|
+
}
|
|
20
|
+
else if (typeof all[all.length - 1] === 'string') {
|
|
21
|
+
all[all.length - 1] += val;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
all.push(val);
|
|
25
|
+
}
|
|
26
|
+
return all;
|
|
27
|
+
}, []);
|
|
28
|
+
return results.length === 1 ? results[0] : results.length === 0 ? '' : results;
|
|
29
|
+
}
|
|
30
|
+
export function formatListToParts(_a, getListFormat, values, options) {
|
|
31
|
+
var locale = _a.locale, onError = _a.onError;
|
|
32
|
+
if (options === void 0) { options = {}; }
|
|
33
|
+
var ListFormat = Intl.ListFormat;
|
|
34
|
+
if (!ListFormat) {
|
|
35
|
+
onError(new FormatError("Intl.ListFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-listformat\"\n", ErrorCode.MISSING_INTL_API));
|
|
36
|
+
}
|
|
37
|
+
var filteredOptions = filterProps(options, LIST_FORMAT_OPTIONS);
|
|
38
|
+
try {
|
|
39
|
+
var richValues_1 = {};
|
|
40
|
+
var serializedValues = values.map(function (v, i) {
|
|
41
|
+
if (typeof v === 'object') {
|
|
42
|
+
var id = generateToken(i);
|
|
43
|
+
richValues_1[id] = v;
|
|
44
|
+
return id;
|
|
45
|
+
}
|
|
46
|
+
return String(v);
|
|
47
|
+
});
|
|
48
|
+
return getListFormat(locale, filteredOptions)
|
|
49
|
+
.formatToParts(serializedValues)
|
|
50
|
+
.map(function (part) {
|
|
51
|
+
return part.type === 'literal'
|
|
52
|
+
? part
|
|
53
|
+
: __assign(__assign({}, part), { value: richValues_1[part.value] || part.value });
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
onError(new IntlError(IntlErrorCode.FORMAT_ERROR, 'Error formatting list.', e));
|
|
58
|
+
}
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
return values;
|
|
61
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Formatters, MessageDescriptor, CustomFormats, OnErrorFn } from './types';
|
|
2
|
+
import { FormatXMLElementFn, PrimitiveType, Formatters as IntlMessageFormatFormatters, Options } from 'intl-messageformat';
|
|
3
|
+
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
4
|
+
export declare type FormatMessageFn<T> = ({ locale, formats, messages, defaultLocale, defaultFormats, fallbackOnEmptyString, onError, timeZone, defaultRichTextElements, }: {
|
|
5
|
+
locale: string;
|
|
6
|
+
timeZone?: string;
|
|
7
|
+
formats: CustomFormats;
|
|
8
|
+
messages: Record<string, string> | Record<string, MessageFormatElement[]>;
|
|
9
|
+
defaultLocale: string;
|
|
10
|
+
defaultFormats: CustomFormats;
|
|
11
|
+
defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
|
|
12
|
+
fallbackOnEmptyString?: boolean;
|
|
13
|
+
onError: OnErrorFn;
|
|
14
|
+
}, state: IntlMessageFormatFormatters & Pick<Formatters, 'getMessageFormat'>, messageDescriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, opts?: Options) => T extends string ? string : Array<T | string> | string | T;
|
|
15
|
+
export declare const formatMessage: FormatMessageFn<any>;
|
|
16
|
+
//# sourceMappingURL=message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.d.ts","sourceRoot":"","sources":["../../src/message.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,UAAU,EAAE,iBAAiB,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAE/E,OAAO,EAEL,kBAAkB,EAClB,aAAa,EACb,UAAU,IAAI,2BAA2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