@formatjs/intl 3.1.8 → 4.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/index.d.ts +14 -14
- package/index.js +13 -36
- package/package.json +10 -7
- package/src/create-intl.d.ts +1 -1
- package/src/create-intl.js +17 -20
- package/src/dateTime.d.ts +1 -1
- package/src/dateTime.js +17 -25
- package/src/displayName.d.ts +1 -1
- package/src/displayName.js +7 -10
- package/src/error.d.ts +1 -1
- package/src/error.js +17 -20
- package/src/list.d.ts +1 -1
- package/src/list.js +10 -14
- package/src/message.d.ts +1 -1
- package/src/message.js +19 -23
- package/src/number.d.ts +1 -1
- package/src/number.js +9 -14
- package/src/plural.d.ts +1 -1
- package/src/plural.js +7 -10
- package/src/relativeTime.d.ts +1 -1
- package/src/relativeTime.js +8 -11
- package/src/types.d.ts +4 -3
- package/src/types.js +1 -2
- package/src/utils.d.ts +1 -1
- package/src/utils.js +33 -41
- package/lib/index.d.ts +0 -16
- package/lib/index.js +0 -17
- package/lib/src/create-intl.d.ts +0 -10
- package/lib/src/create-intl.js +0 -51
- package/lib/src/dateTime.d.ts +0 -37
- package/lib/src/dateTime.js +0 -117
- package/lib/src/displayName.d.ts +0 -5
- package/lib/src/displayName.js +0 -23
- package/lib/src/error.d.ts +0 -35
- package/lib/src/error.js +0 -91
- package/lib/src/list.d.ts +0 -9
- package/lib/src/list.js +0 -61
- package/lib/src/message.d.ts +0 -15
- package/lib/src/message.js +0 -103
- package/lib/src/number.d.ts +0 -16
- package/lib/src/number.js +0 -58
- package/lib/src/plural.d.ts +0 -5
- package/lib/src/plural.js +0 -19
- package/lib/src/relativeTime.d.ts +0 -6
- package/lib/src/relativeTime.js +0 -29
- package/lib/src/types.d.ts +0 -109
- package/lib/src/types.js +0 -1
- package/lib/src/utils.d.ts +0 -12
- package/lib/src/utils.js +0 -168
package/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { MessageDescriptor } from './src/types';
|
|
2
|
-
export * from './src/types';
|
|
1
|
+
import { MessageDescriptor } from './src/types.js';
|
|
2
|
+
export * from './src/types.js';
|
|
3
3
|
export declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
|
|
4
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';
|
|
5
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils.js';
|
|
6
|
+
export * from './src/error.js';
|
|
7
|
+
export { formatMessage } from './src/message.js';
|
|
8
|
+
export type { FormatMessageFn } from './src/message.js';
|
|
9
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime.js';
|
|
10
|
+
export { formatDisplayName } from './src/displayName.js';
|
|
11
|
+
export { formatList } from './src/list.js';
|
|
12
|
+
export { formatPlural } from './src/plural.js';
|
|
13
|
+
export { formatRelativeTime } from './src/relativeTime.js';
|
|
14
|
+
export { formatNumber, formatNumberToParts } from './src/number.js';
|
|
15
|
+
export { createIntl } from './src/create-intl.js';
|
|
16
|
+
export type { CreateIntlFn } from './src/create-intl.js';
|
package/index.js
CHANGED
|
@@ -1,40 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 = void 0;
|
|
4
|
-
exports.defineMessages = defineMessages;
|
|
5
|
-
exports.defineMessage = defineMessage;
|
|
6
|
-
var tslib_1 = require("tslib");
|
|
7
|
-
tslib_1.__exportStar(require("./src/types"), exports);
|
|
8
|
-
function defineMessages(msgs) {
|
|
1
|
+
export * from './src/types.js';
|
|
2
|
+
export function defineMessages(msgs) {
|
|
9
3
|
return msgs;
|
|
10
4
|
}
|
|
11
|
-
function defineMessage(msg) {
|
|
5
|
+
export function defineMessage(msg) {
|
|
12
6
|
return msg;
|
|
13
7
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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; } });
|
|
8
|
+
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils.js';
|
|
9
|
+
export * from './src/error.js';
|
|
10
|
+
export { formatMessage } from './src/message.js';
|
|
11
|
+
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime.js';
|
|
12
|
+
export { formatDisplayName } from './src/displayName.js';
|
|
13
|
+
export { formatList } from './src/list.js';
|
|
14
|
+
export { formatPlural } from './src/plural.js';
|
|
15
|
+
export { formatRelativeTime } from './src/relativeTime.js';
|
|
16
|
+
export { formatNumber, formatNumberToParts } from './src/number.js';
|
|
17
|
+
export { createIntl } from './src/create-intl.js';
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/intl",
|
|
3
3
|
"description": "Internationalize JS apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "4.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Long Ho <holevietlong@gmail.com>",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"sideEffects": false,
|
|
9
|
+
"types": "index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./index.js"
|
|
12
|
+
},
|
|
8
13
|
"dependencies": {
|
|
9
14
|
"tslib": "^2.8.0",
|
|
10
|
-
"@formatjs/icu-messageformat-parser": "
|
|
11
|
-
"@formatjs/ecma402-abstract": "
|
|
12
|
-
"
|
|
13
|
-
"
|
|
15
|
+
"@formatjs/icu-messageformat-parser": "3.0.1",
|
|
16
|
+
"@formatjs/ecma402-abstract": "3.0.1",
|
|
17
|
+
"@formatjs/fast-memoize": "3.0.1",
|
|
18
|
+
"intl-messageformat": "11.0.1"
|
|
14
19
|
},
|
|
15
20
|
"peerDependencies": {
|
|
16
21
|
"typescript": "^5.6.0"
|
|
@@ -30,8 +35,6 @@
|
|
|
30
35
|
"translate",
|
|
31
36
|
"translation"
|
|
32
37
|
],
|
|
33
|
-
"main": "index.js",
|
|
34
|
-
"module": "lib/index.js",
|
|
35
38
|
"peerDependenciesMeta": {
|
|
36
39
|
"typescript": {
|
|
37
40
|
"optional": true
|
package/src/create-intl.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IntlCache, IntlConfig, IntlShape } from './types';
|
|
1
|
+
import { IntlCache, IntlConfig, IntlShape } from './types.js';
|
|
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
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var plural_1 = require("./plural");
|
|
12
|
-
var relativeTime_1 = require("./relativeTime");
|
|
13
|
-
var utils_1 = require("./utils");
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts, } from './dateTime.js';
|
|
3
|
+
import { formatDisplayName } from './displayName.js';
|
|
4
|
+
import { InvalidConfigError, MissingDataError } from './error.js';
|
|
5
|
+
import { formatList, formatListToParts } from './list.js';
|
|
6
|
+
import { formatMessage } from './message.js';
|
|
7
|
+
import { formatNumber, formatNumberToParts } from './number.js';
|
|
8
|
+
import { formatPlural } from './plural.js';
|
|
9
|
+
import { formatRelativeTime } from './relativeTime.js';
|
|
10
|
+
import { createFormatters, DEFAULT_INTL_CONFIG } from './utils.js';
|
|
14
11
|
function messagesContainString(messages) {
|
|
15
12
|
var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
|
|
16
13
|
return typeof firstMessage === 'string';
|
|
@@ -27,13 +24,13 @@ function verifyConfigMessages(config) {
|
|
|
27
24
|
* @param config intl config
|
|
28
25
|
* @param cache cache for formatter instances to prevent memory leak
|
|
29
26
|
*/
|
|
30
|
-
function createIntl(config, cache) {
|
|
31
|
-
var formatters =
|
|
32
|
-
var resolvedConfig =
|
|
27
|
+
export function createIntl(config, cache) {
|
|
28
|
+
var formatters = createFormatters(cache);
|
|
29
|
+
var resolvedConfig = __assign(__assign({}, DEFAULT_INTL_CONFIG), config);
|
|
33
30
|
var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
|
|
34
31
|
if (!locale) {
|
|
35
32
|
if (onError) {
|
|
36
|
-
onError(new
|
|
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")));
|
|
37
34
|
}
|
|
38
35
|
// Since there's no registered locale data for `locale`, this will
|
|
39
36
|
// fallback to the `defaultLocale` to make sure things can render.
|
|
@@ -43,12 +40,12 @@ function createIntl(config, cache) {
|
|
|
43
40
|
resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
|
|
44
41
|
}
|
|
45
42
|
else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
|
|
46
|
-
onError(new
|
|
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")));
|
|
47
44
|
}
|
|
48
45
|
else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
|
|
49
46
|
onError) {
|
|
50
|
-
onError(new
|
|
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")));
|
|
51
48
|
}
|
|
52
49
|
verifyConfigMessages(resolvedConfig);
|
|
53
|
-
return
|
|
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) });
|
|
54
51
|
}
|
package/src/dateTime.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
1
|
+
import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types.js';
|
|
2
2
|
export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
3
3
|
locale: string;
|
|
4
4
|
timeZone?: string;
|
package/src/dateTime.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.formatDate = formatDate;
|
|
5
|
-
exports.formatTime = formatTime;
|
|
6
|
-
exports.formatDateTimeRange = formatDateTimeRange;
|
|
7
|
-
exports.formatDateToParts = formatDateToParts;
|
|
8
|
-
exports.formatTimeToParts = formatTimeToParts;
|
|
9
|
-
var tslib_1 = require("tslib");
|
|
10
|
-
var error_1 = require("./error");
|
|
11
|
-
var utils_1 = require("./utils");
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { IntlFormatError } from './error.js';
|
|
3
|
+
import { filterProps, getNamedFormat } from './utils.js';
|
|
12
4
|
var DATE_TIME_FORMAT_OPTIONS = [
|
|
13
5
|
'formatMatcher',
|
|
14
6
|
'timeZone',
|
|
@@ -30,12 +22,12 @@ var DATE_TIME_FORMAT_OPTIONS = [
|
|
|
30
22
|
'numberingSystem',
|
|
31
23
|
'fractionalSecondDigits',
|
|
32
24
|
];
|
|
33
|
-
function getFormatter(_a, type, getDateTimeFormat, options) {
|
|
25
|
+
export function getFormatter(_a, type, getDateTimeFormat, options) {
|
|
34
26
|
var locale = _a.locale, formats = _a.formats, onError = _a.onError, timeZone = _a.timeZone;
|
|
35
27
|
if (options === void 0) { options = {}; }
|
|
36
28
|
var format = options.format;
|
|
37
|
-
var defaults =
|
|
38
|
-
var filteredOptions =
|
|
29
|
+
var defaults = __assign(__assign({}, (timeZone && { timeZone: timeZone })), (format && getNamedFormat(formats, type, format, onError)));
|
|
30
|
+
var filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
|
|
39
31
|
if (type === 'time' &&
|
|
40
32
|
!filteredOptions.hour &&
|
|
41
33
|
!filteredOptions.minute &&
|
|
@@ -43,11 +35,11 @@ function getFormatter(_a, type, getDateTimeFormat, options) {
|
|
|
43
35
|
!filteredOptions.timeStyle &&
|
|
44
36
|
!filteredOptions.dateStyle) {
|
|
45
37
|
// Add default formatting options if hour, minute, or second isn't defined.
|
|
46
|
-
filteredOptions =
|
|
38
|
+
filteredOptions = __assign(__assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' });
|
|
47
39
|
}
|
|
48
40
|
return getDateTimeFormat(locale, filteredOptions);
|
|
49
41
|
}
|
|
50
|
-
function formatDate(config, getDateTimeFormat) {
|
|
42
|
+
export function formatDate(config, getDateTimeFormat) {
|
|
51
43
|
var _a = [];
|
|
52
44
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
53
45
|
_a[_i - 2] = arguments[_i];
|
|
@@ -58,11 +50,11 @@ function formatDate(config, getDateTimeFormat) {
|
|
|
58
50
|
return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
|
|
59
51
|
}
|
|
60
52
|
catch (e) {
|
|
61
|
-
config.onError(new
|
|
53
|
+
config.onError(new IntlFormatError('Error formatting date.', config.locale, e));
|
|
62
54
|
}
|
|
63
55
|
return String(date);
|
|
64
56
|
}
|
|
65
|
-
function formatTime(config, getDateTimeFormat) {
|
|
57
|
+
export function formatTime(config, getDateTimeFormat) {
|
|
66
58
|
var _a = [];
|
|
67
59
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
68
60
|
_a[_i - 2] = arguments[_i];
|
|
@@ -73,11 +65,11 @@ function formatTime(config, getDateTimeFormat) {
|
|
|
73
65
|
return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
|
|
74
66
|
}
|
|
75
67
|
catch (e) {
|
|
76
|
-
config.onError(new
|
|
68
|
+
config.onError(new IntlFormatError('Error formatting time.', config.locale, e));
|
|
77
69
|
}
|
|
78
70
|
return String(date);
|
|
79
71
|
}
|
|
80
|
-
function formatDateTimeRange(config, getDateTimeFormat) {
|
|
72
|
+
export function formatDateTimeRange(config, getDateTimeFormat) {
|
|
81
73
|
var _a = [];
|
|
82
74
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
83
75
|
_a[_i - 2] = arguments[_i];
|
|
@@ -89,11 +81,11 @@ function formatDateTimeRange(config, getDateTimeFormat) {
|
|
|
89
81
|
return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate);
|
|
90
82
|
}
|
|
91
83
|
catch (e) {
|
|
92
|
-
config.onError(new
|
|
84
|
+
config.onError(new IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
93
85
|
}
|
|
94
86
|
return String(fromDate);
|
|
95
87
|
}
|
|
96
|
-
function formatDateToParts(config, getDateTimeFormat) {
|
|
88
|
+
export function formatDateToParts(config, getDateTimeFormat) {
|
|
97
89
|
var _a = [];
|
|
98
90
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
99
91
|
_a[_i - 2] = arguments[_i];
|
|
@@ -104,11 +96,11 @@ function formatDateToParts(config, getDateTimeFormat) {
|
|
|
104
96
|
return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged
|
|
105
97
|
}
|
|
106
98
|
catch (e) {
|
|
107
|
-
config.onError(new
|
|
99
|
+
config.onError(new IntlFormatError('Error formatting date.', config.locale, e));
|
|
108
100
|
}
|
|
109
101
|
return [];
|
|
110
102
|
}
|
|
111
|
-
function formatTimeToParts(config, getDateTimeFormat) {
|
|
103
|
+
export function formatTimeToParts(config, getDateTimeFormat) {
|
|
112
104
|
var _a = [];
|
|
113
105
|
for (var _i = 2; _i < arguments.length; _i++) {
|
|
114
106
|
_a[_i - 2] = arguments[_i];
|
|
@@ -119,7 +111,7 @@ function formatTimeToParts(config, getDateTimeFormat) {
|
|
|
119
111
|
return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged
|
|
120
112
|
}
|
|
121
113
|
catch (e) {
|
|
122
|
-
config.onError(new
|
|
114
|
+
config.onError(new IntlFormatError('Error formatting time.', config.locale, e));
|
|
123
115
|
}
|
|
124
116
|
return [];
|
|
125
117
|
}
|
package/src/displayName.d.ts
CHANGED
package/src/displayName.js
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var utils_1 = require("./utils");
|
|
5
|
-
var intl_messageformat_1 = require("intl-messageformat");
|
|
6
|
-
var error_1 = require("./error");
|
|
1
|
+
import { filterProps } from './utils.js';
|
|
2
|
+
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
3
|
+
import { IntlFormatError } from './error.js';
|
|
7
4
|
var DISPLAY_NAMES_OPTONS = [
|
|
8
5
|
'style',
|
|
9
6
|
'type',
|
|
10
7
|
'fallback',
|
|
11
8
|
'languageDisplay',
|
|
12
9
|
];
|
|
13
|
-
function formatDisplayName(_a, getDisplayNames, value, options) {
|
|
10
|
+
export function formatDisplayName(_a, getDisplayNames, value, options) {
|
|
14
11
|
var locale = _a.locale, onError = _a.onError;
|
|
15
12
|
var DisplayNames = Intl.DisplayNames;
|
|
16
13
|
if (!DisplayNames) {
|
|
17
|
-
onError(new
|
|
14
|
+
onError(new FormatError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n", ErrorCode.MISSING_INTL_API));
|
|
18
15
|
}
|
|
19
|
-
var filteredOptions =
|
|
16
|
+
var filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
|
|
20
17
|
try {
|
|
21
18
|
return getDisplayNames(locale, filteredOptions).of(value);
|
|
22
19
|
}
|
|
23
20
|
catch (e) {
|
|
24
|
-
onError(new
|
|
21
|
+
onError(new IntlFormatError('Error formatting display name.', locale, e));
|
|
25
22
|
}
|
|
26
23
|
}
|
package/src/error.d.ts
CHANGED
package/src/error.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.MissingTranslationError = exports.MessageFormatError = exports.IntlFormatError = exports.MissingDataError = exports.InvalidConfigError = exports.UnsupportedFormatterError = exports.IntlError = exports.IntlErrorCode = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var IntlErrorCode;
|
|
1
|
+
import { __extends } from "tslib";
|
|
2
|
+
export var IntlErrorCode;
|
|
6
3
|
(function (IntlErrorCode) {
|
|
7
4
|
IntlErrorCode["FORMAT_ERROR"] = "FORMAT_ERROR";
|
|
8
5
|
IntlErrorCode["UNSUPPORTED_FORMATTER"] = "UNSUPPORTED_FORMATTER";
|
|
9
6
|
IntlErrorCode["INVALID_CONFIG"] = "INVALID_CONFIG";
|
|
10
7
|
IntlErrorCode["MISSING_DATA"] = "MISSING_DATA";
|
|
11
8
|
IntlErrorCode["MISSING_TRANSLATION"] = "MISSING_TRANSLATION";
|
|
12
|
-
})(IntlErrorCode || (
|
|
9
|
+
})(IntlErrorCode || (IntlErrorCode = {}));
|
|
13
10
|
var IntlError = /** @class */ (function (_super) {
|
|
14
|
-
|
|
11
|
+
__extends(IntlError, _super);
|
|
15
12
|
function IntlError(code, message, exception) {
|
|
16
13
|
var _this = this;
|
|
17
14
|
var err = exception
|
|
@@ -30,33 +27,33 @@ var IntlError = /** @class */ (function (_super) {
|
|
|
30
27
|
}
|
|
31
28
|
return IntlError;
|
|
32
29
|
}(Error));
|
|
33
|
-
|
|
30
|
+
export { IntlError };
|
|
34
31
|
var UnsupportedFormatterError = /** @class */ (function (_super) {
|
|
35
|
-
|
|
32
|
+
__extends(UnsupportedFormatterError, _super);
|
|
36
33
|
function UnsupportedFormatterError(message, exception) {
|
|
37
34
|
return _super.call(this, IntlErrorCode.UNSUPPORTED_FORMATTER, message, exception) || this;
|
|
38
35
|
}
|
|
39
36
|
return UnsupportedFormatterError;
|
|
40
37
|
}(IntlError));
|
|
41
|
-
|
|
38
|
+
export { UnsupportedFormatterError };
|
|
42
39
|
var InvalidConfigError = /** @class */ (function (_super) {
|
|
43
|
-
|
|
40
|
+
__extends(InvalidConfigError, _super);
|
|
44
41
|
function InvalidConfigError(message, exception) {
|
|
45
42
|
return _super.call(this, IntlErrorCode.INVALID_CONFIG, message, exception) || this;
|
|
46
43
|
}
|
|
47
44
|
return InvalidConfigError;
|
|
48
45
|
}(IntlError));
|
|
49
|
-
|
|
46
|
+
export { InvalidConfigError };
|
|
50
47
|
var MissingDataError = /** @class */ (function (_super) {
|
|
51
|
-
|
|
48
|
+
__extends(MissingDataError, _super);
|
|
52
49
|
function MissingDataError(message, exception) {
|
|
53
50
|
return _super.call(this, IntlErrorCode.MISSING_DATA, message, exception) || this;
|
|
54
51
|
}
|
|
55
52
|
return MissingDataError;
|
|
56
53
|
}(IntlError));
|
|
57
|
-
|
|
54
|
+
export { MissingDataError };
|
|
58
55
|
var IntlFormatError = /** @class */ (function (_super) {
|
|
59
|
-
|
|
56
|
+
__extends(IntlFormatError, _super);
|
|
60
57
|
function IntlFormatError(message, locale, exception) {
|
|
61
58
|
var _this = _super.call(this, IntlErrorCode.FORMAT_ERROR, "".concat(message, "\nLocale: ").concat(locale, "\n"), exception) || this;
|
|
62
59
|
_this.locale = locale;
|
|
@@ -64,9 +61,9 @@ var IntlFormatError = /** @class */ (function (_super) {
|
|
|
64
61
|
}
|
|
65
62
|
return IntlFormatError;
|
|
66
63
|
}(IntlError));
|
|
67
|
-
|
|
64
|
+
export { IntlFormatError };
|
|
68
65
|
var MessageFormatError = /** @class */ (function (_super) {
|
|
69
|
-
|
|
66
|
+
__extends(MessageFormatError, _super);
|
|
70
67
|
function MessageFormatError(message, locale, descriptor, exception) {
|
|
71
68
|
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;
|
|
72
69
|
_this.descriptor = descriptor;
|
|
@@ -75,9 +72,9 @@ var MessageFormatError = /** @class */ (function (_super) {
|
|
|
75
72
|
}
|
|
76
73
|
return MessageFormatError;
|
|
77
74
|
}(IntlFormatError));
|
|
78
|
-
|
|
75
|
+
export { MessageFormatError };
|
|
79
76
|
var MissingTranslationError = /** @class */ (function (_super) {
|
|
80
|
-
|
|
77
|
+
__extends(MissingTranslationError, _super);
|
|
81
78
|
function MissingTranslationError(descriptor, locale) {
|
|
82
79
|
var _this = _super.call(this, IntlErrorCode.MISSING_TRANSLATION, "Missing message: \"".concat(descriptor.id, "\" for locale \"").concat(locale, "\", using ").concat(descriptor.defaultMessage
|
|
83
80
|
? "default message (".concat(typeof descriptor.defaultMessage === 'string'
|
|
@@ -91,4 +88,4 @@ var MissingTranslationError = /** @class */ (function (_super) {
|
|
|
91
88
|
}
|
|
92
89
|
return MissingTranslationError;
|
|
93
90
|
}(IntlError));
|
|
94
|
-
|
|
91
|
+
export { MissingTranslationError };
|
package/src/list.d.ts
CHANGED
package/src/list.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var tslib_1 = require("tslib");
|
|
6
|
-
var intl_messageformat_1 = require("intl-messageformat");
|
|
7
|
-
var error_1 = require("./error");
|
|
8
|
-
var utils_1 = require("./utils");
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
3
|
+
import { IntlFormatError } from './error.js';
|
|
4
|
+
import { filterProps } from './utils.js';
|
|
9
5
|
var LIST_FORMAT_OPTIONS = [
|
|
10
6
|
'type',
|
|
11
7
|
'style',
|
|
@@ -14,7 +10,7 @@ var now = Date.now();
|
|
|
14
10
|
function generateToken(i) {
|
|
15
11
|
return "".concat(now, "_").concat(i, "_").concat(now);
|
|
16
12
|
}
|
|
17
|
-
function formatList(opts, getListFormat, values, options) {
|
|
13
|
+
export function formatList(opts, getListFormat, values, options) {
|
|
18
14
|
if (options === void 0) { options = {}; }
|
|
19
15
|
var results = formatListToParts(opts, getListFormat, values, options).reduce(function (all, el) {
|
|
20
16
|
var val = el.value;
|
|
@@ -31,14 +27,14 @@ function formatList(opts, getListFormat, values, options) {
|
|
|
31
27
|
}, []);
|
|
32
28
|
return results.length === 1 ? results[0] : results.length === 0 ? '' : results;
|
|
33
29
|
}
|
|
34
|
-
function formatListToParts(_a, getListFormat, values, options) {
|
|
30
|
+
export function formatListToParts(_a, getListFormat, values, options) {
|
|
35
31
|
var locale = _a.locale, onError = _a.onError;
|
|
36
32
|
if (options === void 0) { options = {}; }
|
|
37
33
|
var ListFormat = Intl.ListFormat;
|
|
38
34
|
if (!ListFormat) {
|
|
39
|
-
onError(new
|
|
35
|
+
onError(new FormatError("Intl.ListFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-listformat\"\n", ErrorCode.MISSING_INTL_API));
|
|
40
36
|
}
|
|
41
|
-
var filteredOptions =
|
|
37
|
+
var filteredOptions = filterProps(options, LIST_FORMAT_OPTIONS);
|
|
42
38
|
try {
|
|
43
39
|
var richValues_1 = {};
|
|
44
40
|
var serializedValues = Array.from(values).map(function (v, i) {
|
|
@@ -54,11 +50,11 @@ function formatListToParts(_a, getListFormat, values, options) {
|
|
|
54
50
|
.map(function (part) {
|
|
55
51
|
return (part.type === 'literal'
|
|
56
52
|
? part
|
|
57
|
-
:
|
|
53
|
+
: __assign(__assign({}, part), { value: richValues_1[part.value] || part.value }));
|
|
58
54
|
});
|
|
59
55
|
}
|
|
60
56
|
catch (e) {
|
|
61
|
-
onError(new
|
|
57
|
+
onError(new IntlFormatError('Error formatting list.', locale, e));
|
|
62
58
|
}
|
|
63
59
|
// @ts-ignore
|
|
64
60
|
return values;
|
package/src/message.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CustomFormats, Formatters, MessageDescriptor, OnErrorFn } from './types';
|
|
1
|
+
import { CustomFormats, Formatters, MessageDescriptor, OnErrorFn } from './types.js';
|
|
2
2
|
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
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, }: {
|