@formatjs/intl 3.1.8 → 4.0.0
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 +17 -14
- 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/lib/src/dateTime.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { __assign } from "tslib";
|
|
2
|
-
import { IntlFormatError } from './error';
|
|
3
|
-
import { filterProps, getNamedFormat } from './utils';
|
|
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 IntlFormatError('Error formatting date.', config.locale, 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 IntlFormatError('Error formatting time.', config.locale, 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 fromDate = typeof from === 'string' ? new Date(from || 0) : from;
|
|
79
|
-
var toDate = typeof to === 'string' ? new Date(to || 0) : to;
|
|
80
|
-
try {
|
|
81
|
-
return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate);
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
config.onError(new IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
85
|
-
}
|
|
86
|
-
return String(fromDate);
|
|
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 IntlFormatError('Error formatting date.', config.locale, 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 IntlFormatError('Error formatting time.', config.locale, e));
|
|
115
|
-
}
|
|
116
|
-
return [];
|
|
117
|
-
}
|
package/lib/src/displayName.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
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;
|
package/lib/src/displayName.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { filterProps } from './utils';
|
|
2
|
-
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
3
|
-
import { IntlFormatError } from './error';
|
|
4
|
-
var DISPLAY_NAMES_OPTONS = [
|
|
5
|
-
'style',
|
|
6
|
-
'type',
|
|
7
|
-
'fallback',
|
|
8
|
-
'languageDisplay',
|
|
9
|
-
];
|
|
10
|
-
export function formatDisplayName(_a, getDisplayNames, value, options) {
|
|
11
|
-
var locale = _a.locale, onError = _a.onError;
|
|
12
|
-
var DisplayNames = Intl.DisplayNames;
|
|
13
|
-
if (!DisplayNames) {
|
|
14
|
-
onError(new FormatError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n", ErrorCode.MISSING_INTL_API));
|
|
15
|
-
}
|
|
16
|
-
var filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
|
|
17
|
-
try {
|
|
18
|
-
return getDisplayNames(locale, filteredOptions).of(value);
|
|
19
|
-
}
|
|
20
|
-
catch (e) {
|
|
21
|
-
onError(new IntlFormatError('Error formatting display name.', locale, e));
|
|
22
|
-
}
|
|
23
|
-
}
|
package/lib/src/error.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
readonly locale: string;
|
|
25
|
-
constructor(message: string, locale: string, exception?: Error | unknown);
|
|
26
|
-
}
|
|
27
|
-
export declare class MessageFormatError extends IntlFormatError {
|
|
28
|
-
readonly descriptor?: MessageDescriptor;
|
|
29
|
-
readonly locale: string;
|
|
30
|
-
constructor(message: string, locale: string, descriptor?: MessageDescriptor, exception?: Error | unknown);
|
|
31
|
-
}
|
|
32
|
-
export declare class MissingTranslationError extends IntlError<IntlErrorCode.MISSING_TRANSLATION> {
|
|
33
|
-
readonly descriptor?: MessageDescriptor;
|
|
34
|
-
constructor(descriptor: MessageDescriptor, locale: string);
|
|
35
|
-
}
|
package/lib/src/error.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
var _this = _super.call(this, IntlErrorCode.FORMAT_ERROR, "".concat(message, "\nLocale: ").concat(locale, "\n"), exception) || this;
|
|
59
|
-
_this.locale = locale;
|
|
60
|
-
return _this;
|
|
61
|
-
}
|
|
62
|
-
return IntlFormatError;
|
|
63
|
-
}(IntlError));
|
|
64
|
-
export { IntlFormatError };
|
|
65
|
-
var MessageFormatError = /** @class */ (function (_super) {
|
|
66
|
-
__extends(MessageFormatError, _super);
|
|
67
|
-
function MessageFormatError(message, locale, descriptor, exception) {
|
|
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;
|
|
69
|
-
_this.descriptor = descriptor;
|
|
70
|
-
_this.locale = locale;
|
|
71
|
-
return _this;
|
|
72
|
-
}
|
|
73
|
-
return MessageFormatError;
|
|
74
|
-
}(IntlFormatError));
|
|
75
|
-
export { MessageFormatError };
|
|
76
|
-
var MissingTranslationError = /** @class */ (function (_super) {
|
|
77
|
-
__extends(MissingTranslationError, _super);
|
|
78
|
-
function MissingTranslationError(descriptor, locale) {
|
|
79
|
-
var _this = _super.call(this, IntlErrorCode.MISSING_TRANSLATION, "Missing message: \"".concat(descriptor.id, "\" for locale \"").concat(locale, "\", using ").concat(descriptor.defaultMessage
|
|
80
|
-
? "default message (".concat(typeof descriptor.defaultMessage === 'string'
|
|
81
|
-
? descriptor.defaultMessage
|
|
82
|
-
: descriptor.defaultMessage
|
|
83
|
-
.map(function (e) { var _a; return (_a = e.value) !== null && _a !== void 0 ? _a : JSON.stringify(e); })
|
|
84
|
-
.join(), ")")
|
|
85
|
-
: 'id', " as fallback.")) || this;
|
|
86
|
-
_this.descriptor = descriptor;
|
|
87
|
-
return _this;
|
|
88
|
-
}
|
|
89
|
-
return MissingTranslationError;
|
|
90
|
-
}(IntlError));
|
|
91
|
-
export { MissingTranslationError };
|
package/lib/src/list.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters, OnErrorFn, Part } from './types';
|
|
2
|
-
export declare function formatList(opts: {
|
|
3
|
-
locale: string;
|
|
4
|
-
onError: OnErrorFn;
|
|
5
|
-
}, getListFormat: Formatters['getListFormat'], values: Iterable<string>, options: Parameters<IntlFormatters['formatList']>[1]): string;
|
|
6
|
-
export declare function formatListToParts<T>(opts: {
|
|
7
|
-
locale: string;
|
|
8
|
-
onError: OnErrorFn;
|
|
9
|
-
}, getListFormat: Formatters['getListFormat'], values: Iterable<string | T>, options: Parameters<IntlFormatters['formatList']>[1]): Part[];
|
package/lib/src/list.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { __assign } from "tslib";
|
|
2
|
-
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
3
|
-
import { IntlFormatError } from './error';
|
|
4
|
-
import { filterProps } from './utils';
|
|
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 = Array.from(values).map(function (v, i) {
|
|
41
|
-
if (typeof v === 'object' && v !== null) {
|
|
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 IntlFormatError('Error formatting list.', locale, e));
|
|
58
|
-
}
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
return values;
|
|
61
|
-
}
|
package/lib/src/message.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { CustomFormats, Formatters, MessageDescriptor, OnErrorFn } from './types';
|
|
2
|
-
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
|
-
import { FormatXMLElementFn, Formatters as IntlMessageFormatFormatters, Options, PrimitiveType } from 'intl-messageformat';
|
|
4
|
-
export 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>;
|
package/lib/src/message.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { __assign } from "tslib";
|
|
2
|
-
import { TYPE } from '@formatjs/icu-messageformat-parser';
|
|
3
|
-
import { IntlMessageFormat, } from 'intl-messageformat';
|
|
4
|
-
import { MessageFormatError, MissingTranslationError } from './error';
|
|
5
|
-
import { invariant } from './utils';
|
|
6
|
-
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.github.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.github.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.github.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
|
|
32
|
-
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
|
-
};
|
package/lib/src/number.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { CustomFormats, Formatters, IntlFormatters, 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[];
|
package/lib/src/number.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { IntlFormatError } from './error';
|
|
2
|
-
import { filterProps, getNamedFormat } from './utils';
|
|
3
|
-
var NUMBER_FORMAT_OPTIONS = [
|
|
4
|
-
'style',
|
|
5
|
-
'currency',
|
|
6
|
-
'unit',
|
|
7
|
-
'unitDisplay',
|
|
8
|
-
'useGrouping',
|
|
9
|
-
'minimumIntegerDigits',
|
|
10
|
-
'minimumFractionDigits',
|
|
11
|
-
'maximumFractionDigits',
|
|
12
|
-
'minimumSignificantDigits',
|
|
13
|
-
'maximumSignificantDigits',
|
|
14
|
-
// ES2020 NumberFormat
|
|
15
|
-
'compactDisplay',
|
|
16
|
-
'currencyDisplay',
|
|
17
|
-
'currencySign',
|
|
18
|
-
'notation',
|
|
19
|
-
'signDisplay',
|
|
20
|
-
'unit',
|
|
21
|
-
'unitDisplay',
|
|
22
|
-
'numberingSystem',
|
|
23
|
-
// ES2023 NumberFormat
|
|
24
|
-
'trailingZeroDisplay',
|
|
25
|
-
'roundingPriority',
|
|
26
|
-
'roundingIncrement',
|
|
27
|
-
'roundingMode',
|
|
28
|
-
];
|
|
29
|
-
export function getFormatter(_a, getNumberFormat, options) {
|
|
30
|
-
var locale = _a.locale, formats = _a.formats, onError = _a.onError;
|
|
31
|
-
if (options === void 0) { options = {}; }
|
|
32
|
-
var format = options.format;
|
|
33
|
-
var defaults = ((format &&
|
|
34
|
-
getNamedFormat(formats, 'number', format, onError)) ||
|
|
35
|
-
{});
|
|
36
|
-
var filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults);
|
|
37
|
-
return getNumberFormat(locale, filteredOptions);
|
|
38
|
-
}
|
|
39
|
-
export function formatNumber(config, getNumberFormat, value, options) {
|
|
40
|
-
if (options === void 0) { options = {}; }
|
|
41
|
-
try {
|
|
42
|
-
return getFormatter(config, getNumberFormat, options).format(value);
|
|
43
|
-
}
|
|
44
|
-
catch (e) {
|
|
45
|
-
config.onError(new IntlFormatError('Error formatting number.', config.locale, e));
|
|
46
|
-
}
|
|
47
|
-
return String(value);
|
|
48
|
-
}
|
|
49
|
-
export function formatNumberToParts(config, getNumberFormat, value, options) {
|
|
50
|
-
if (options === void 0) { options = {}; }
|
|
51
|
-
try {
|
|
52
|
-
return getFormatter(config, getNumberFormat, options).formatToParts(value);
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
config.onError(new IntlFormatError('Error formatting number.', config.locale, e));
|
|
56
|
-
}
|
|
57
|
-
return [];
|
|
58
|
-
}
|
package/lib/src/plural.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
-
export declare function formatPlural({ locale, onError, }: {
|
|
3
|
-
locale: string;
|
|
4
|
-
onError: OnErrorFn;
|
|
5
|
-
}, getPluralRules: Formatters['getPluralRules'], value: Parameters<IntlFormatters['formatPlural']>[0], options?: Parameters<IntlFormatters['formatPlural']>[1]): Intl.LDMLPluralRule;
|
package/lib/src/plural.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
2
|
-
import { IntlFormatError } from './error';
|
|
3
|
-
import { filterProps } from './utils';
|
|
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
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
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;
|
package/lib/src/relativeTime.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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
|
-
}
|