intl-messageformat 10.5.6 → 10.5.7
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 +2 -1
- package/index.js +3 -1
- package/intl-messageformat.esm.js +3081 -3658
- package/intl-messageformat.iife.js +3154 -3672
- package/lib/index.d.ts +2 -1
- package/lib/index.js +6 -8
- package/lib/src/core.js +22 -25
- package/lib/src/error.js +11 -14
- package/lib/src/formatters.js +24 -29
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*
|
|
3
2
|
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
|
|
4
3
|
Copyrights licensed under the New BSD License.
|
|
5
4
|
See the accompanying LICENSE file for terms.
|
|
6
5
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.default = core_1.IntlMessageFormat;
|
|
6
|
+
import { IntlMessageFormat } from './src/core';
|
|
7
|
+
export * from './src/core';
|
|
8
|
+
export * from './src/error';
|
|
9
|
+
export * from './src/formatters';
|
|
10
|
+
export { IntlMessageFormat };
|
|
11
|
+
export default IntlMessageFormat;
|
package/lib/src/core.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*
|
|
3
2
|
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
|
|
4
3
|
Copyrights licensed under the New BSD License.
|
|
5
4
|
See the accompanying LICENSE file for terms.
|
|
6
5
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var fast_memoize_1 = require("@formatjs/fast-memoize");
|
|
12
|
-
var formatters_1 = require("./formatters");
|
|
6
|
+
import { __assign, __rest, __spreadArray } from "tslib";
|
|
7
|
+
import { parse, } from '@formatjs/icu-messageformat-parser';
|
|
8
|
+
import { memoize, strategies } from '@formatjs/fast-memoize';
|
|
9
|
+
import { formatToParts, PART_TYPE, } from './formatters';
|
|
13
10
|
// -- MessageFormat --------------------------------------------------------
|
|
14
11
|
function mergeConfig(c1, c2) {
|
|
15
12
|
if (!c2) {
|
|
16
13
|
return c1;
|
|
17
14
|
}
|
|
18
|
-
return
|
|
19
|
-
all[k] =
|
|
15
|
+
return __assign(__assign(__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) {
|
|
16
|
+
all[k] = __assign(__assign({}, c1[k]), (c2[k] || {}));
|
|
20
17
|
return all;
|
|
21
18
|
}, {}));
|
|
22
19
|
}
|
|
@@ -27,7 +24,7 @@ function mergeConfigs(defaultConfig, configs) {
|
|
|
27
24
|
return Object.keys(defaultConfig).reduce(function (all, k) {
|
|
28
25
|
all[k] = mergeConfig(defaultConfig[k], configs[k]);
|
|
29
26
|
return all;
|
|
30
|
-
},
|
|
27
|
+
}, __assign({}, defaultConfig));
|
|
31
28
|
}
|
|
32
29
|
function createFastMemoizeCache(store) {
|
|
33
30
|
return {
|
|
@@ -50,38 +47,38 @@ function createDefaultFormatters(cache) {
|
|
|
50
47
|
pluralRules: {},
|
|
51
48
|
}; }
|
|
52
49
|
return {
|
|
53
|
-
getNumberFormat:
|
|
50
|
+
getNumberFormat: memoize(function () {
|
|
54
51
|
var _a;
|
|
55
52
|
var args = [];
|
|
56
53
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
57
54
|
args[_i] = arguments[_i];
|
|
58
55
|
}
|
|
59
|
-
return new ((_a = Intl.NumberFormat).bind.apply(_a,
|
|
56
|
+
return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
60
57
|
}, {
|
|
61
58
|
cache: createFastMemoizeCache(cache.number),
|
|
62
|
-
strategy:
|
|
59
|
+
strategy: strategies.variadic,
|
|
63
60
|
}),
|
|
64
|
-
getDateTimeFormat:
|
|
61
|
+
getDateTimeFormat: memoize(function () {
|
|
65
62
|
var _a;
|
|
66
63
|
var args = [];
|
|
67
64
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
68
65
|
args[_i] = arguments[_i];
|
|
69
66
|
}
|
|
70
|
-
return new ((_a = Intl.DateTimeFormat).bind.apply(_a,
|
|
67
|
+
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
71
68
|
}, {
|
|
72
69
|
cache: createFastMemoizeCache(cache.dateTime),
|
|
73
|
-
strategy:
|
|
70
|
+
strategy: strategies.variadic,
|
|
74
71
|
}),
|
|
75
|
-
getPluralRules:
|
|
72
|
+
getPluralRules: memoize(function () {
|
|
76
73
|
var _a;
|
|
77
74
|
var args = [];
|
|
78
75
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
79
76
|
args[_i] = arguments[_i];
|
|
80
77
|
}
|
|
81
|
-
return new ((_a = Intl.PluralRules).bind.apply(_a,
|
|
78
|
+
return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
82
79
|
}, {
|
|
83
80
|
cache: createFastMemoizeCache(cache.pluralRules),
|
|
84
|
-
strategy:
|
|
81
|
+
strategy: strategies.variadic,
|
|
85
82
|
}),
|
|
86
83
|
};
|
|
87
84
|
}
|
|
@@ -102,7 +99,7 @@ var IntlMessageFormat = /** @class */ (function () {
|
|
|
102
99
|
}
|
|
103
100
|
var result = parts.reduce(function (all, part) {
|
|
104
101
|
if (!all.length ||
|
|
105
|
-
part.type !==
|
|
102
|
+
part.type !== PART_TYPE.literal ||
|
|
106
103
|
typeof all[all.length - 1] !== 'string') {
|
|
107
104
|
all.push(part.value);
|
|
108
105
|
}
|
|
@@ -117,7 +114,7 @@ var IntlMessageFormat = /** @class */ (function () {
|
|
|
117
114
|
return result;
|
|
118
115
|
};
|
|
119
116
|
this.formatToParts = function (values) {
|
|
120
|
-
return
|
|
117
|
+
return formatToParts(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message);
|
|
121
118
|
};
|
|
122
119
|
this.resolvedOptions = function () {
|
|
123
120
|
var _a;
|
|
@@ -135,9 +132,9 @@ var IntlMessageFormat = /** @class */ (function () {
|
|
|
135
132
|
if (!IntlMessageFormat.__parse) {
|
|
136
133
|
throw new TypeError('IntlMessageFormat.__parse must be set to process `message` of type `string`');
|
|
137
134
|
}
|
|
138
|
-
var _a = opts || {}, formatters = _a.formatters, parseOpts =
|
|
135
|
+
var _a = opts || {}, formatters = _a.formatters, parseOpts = __rest(_a, ["formatters"]);
|
|
139
136
|
// Parse string messages into an AST.
|
|
140
|
-
this.ast = IntlMessageFormat.__parse(message,
|
|
137
|
+
this.ast = IntlMessageFormat.__parse(message, __assign(__assign({}, parseOpts), { locale: this.resolvedLocale }));
|
|
141
138
|
}
|
|
142
139
|
else {
|
|
143
140
|
this.ast = message;
|
|
@@ -173,7 +170,7 @@ var IntlMessageFormat = /** @class */ (function () {
|
|
|
173
170
|
}
|
|
174
171
|
return new Intl.Locale(typeof locales === 'string' ? locales : locales[0]);
|
|
175
172
|
};
|
|
176
|
-
IntlMessageFormat.__parse =
|
|
173
|
+
IntlMessageFormat.__parse = parse;
|
|
177
174
|
// Default format options used as the prototype of the `formats` provided to the
|
|
178
175
|
// constructor. These are used when constructing the internal Intl.NumberFormat
|
|
179
176
|
// and Intl.DateTimeFormat instances.
|
|
@@ -238,4 +235,4 @@ var IntlMessageFormat = /** @class */ (function () {
|
|
|
238
235
|
};
|
|
239
236
|
return IntlMessageFormat;
|
|
240
237
|
}());
|
|
241
|
-
|
|
238
|
+
export { IntlMessageFormat };
|
package/lib/src/error.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.MissingValueError = exports.InvalidValueTypeError = exports.InvalidValueError = exports.FormatError = exports.ErrorCode = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var ErrorCode;
|
|
1
|
+
import { __extends } from "tslib";
|
|
2
|
+
export var ErrorCode;
|
|
6
3
|
(function (ErrorCode) {
|
|
7
4
|
// When we have a placeholder but no value to format
|
|
8
5
|
ErrorCode["MISSING_VALUE"] = "MISSING_VALUE";
|
|
@@ -10,9 +7,9 @@ var ErrorCode;
|
|
|
10
7
|
ErrorCode["INVALID_VALUE"] = "INVALID_VALUE";
|
|
11
8
|
// When we need specific Intl API but it's not available
|
|
12
9
|
ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API";
|
|
13
|
-
})(ErrorCode || (
|
|
10
|
+
})(ErrorCode || (ErrorCode = {}));
|
|
14
11
|
var FormatError = /** @class */ (function (_super) {
|
|
15
|
-
|
|
12
|
+
__extends(FormatError, _super);
|
|
16
13
|
function FormatError(msg, code, originalMessage) {
|
|
17
14
|
var _this = _super.call(this, msg) || this;
|
|
18
15
|
_this.code = code;
|
|
@@ -24,28 +21,28 @@ var FormatError = /** @class */ (function (_super) {
|
|
|
24
21
|
};
|
|
25
22
|
return FormatError;
|
|
26
23
|
}(Error));
|
|
27
|
-
|
|
24
|
+
export { FormatError };
|
|
28
25
|
var InvalidValueError = /** @class */ (function (_super) {
|
|
29
|
-
|
|
26
|
+
__extends(InvalidValueError, _super);
|
|
30
27
|
function InvalidValueError(variableId, value, options, originalMessage) {
|
|
31
28
|
return _super.call(this, "Invalid values for \"".concat(variableId, "\": \"").concat(value, "\". Options are \"").concat(Object.keys(options).join('", "'), "\""), ErrorCode.INVALID_VALUE, originalMessage) || this;
|
|
32
29
|
}
|
|
33
30
|
return InvalidValueError;
|
|
34
31
|
}(FormatError));
|
|
35
|
-
|
|
32
|
+
export { InvalidValueError };
|
|
36
33
|
var InvalidValueTypeError = /** @class */ (function (_super) {
|
|
37
|
-
|
|
34
|
+
__extends(InvalidValueTypeError, _super);
|
|
38
35
|
function InvalidValueTypeError(value, type, originalMessage) {
|
|
39
36
|
return _super.call(this, "Value for \"".concat(value, "\" must be of type ").concat(type), ErrorCode.INVALID_VALUE, originalMessage) || this;
|
|
40
37
|
}
|
|
41
38
|
return InvalidValueTypeError;
|
|
42
39
|
}(FormatError));
|
|
43
|
-
|
|
40
|
+
export { InvalidValueTypeError };
|
|
44
41
|
var MissingValueError = /** @class */ (function (_super) {
|
|
45
|
-
|
|
42
|
+
__extends(MissingValueError, _super);
|
|
46
43
|
function MissingValueError(variableId, originalMessage) {
|
|
47
44
|
return _super.call(this, "The intl string context variable \"".concat(variableId, "\" was not provided to the string \"").concat(originalMessage, "\""), ErrorCode.MISSING_VALUE, originalMessage) || this;
|
|
48
45
|
}
|
|
49
46
|
return MissingValueError;
|
|
50
47
|
}(FormatError));
|
|
51
|
-
|
|
48
|
+
export { MissingValueError };
|
package/lib/src/formatters.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
5
|
-
var error_1 = require("./error");
|
|
6
|
-
var PART_TYPE;
|
|
1
|
+
import { isArgumentElement, isDateElement, isDateTimeSkeleton, isLiteralElement, isNumberElement, isNumberSkeleton, isPluralElement, isPoundElement, isSelectElement, isTimeElement, isTagElement, } from '@formatjs/icu-messageformat-parser';
|
|
2
|
+
import { MissingValueError, InvalidValueError, ErrorCode, FormatError, InvalidValueTypeError, } from './error';
|
|
3
|
+
export var PART_TYPE;
|
|
7
4
|
(function (PART_TYPE) {
|
|
8
5
|
PART_TYPE[PART_TYPE["literal"] = 0] = "literal";
|
|
9
6
|
PART_TYPE[PART_TYPE["object"] = 1] = "object";
|
|
10
|
-
})(PART_TYPE || (
|
|
7
|
+
})(PART_TYPE || (PART_TYPE = {}));
|
|
11
8
|
function mergeLiteral(parts) {
|
|
12
9
|
if (parts.length < 2) {
|
|
13
10
|
return parts;
|
|
@@ -25,16 +22,15 @@ function mergeLiteral(parts) {
|
|
|
25
22
|
return all;
|
|
26
23
|
}, []);
|
|
27
24
|
}
|
|
28
|
-
function isFormatXMLElementFn(el) {
|
|
25
|
+
export function isFormatXMLElementFn(el) {
|
|
29
26
|
return typeof el === 'function';
|
|
30
27
|
}
|
|
31
|
-
exports.isFormatXMLElementFn = isFormatXMLElementFn;
|
|
32
28
|
// TODO(skeleton): add skeleton support
|
|
33
|
-
function formatToParts(els, locales, formatters, formats, values, currentPluralValue,
|
|
29
|
+
export function formatToParts(els, locales, formatters, formats, values, currentPluralValue,
|
|
34
30
|
// For debugging
|
|
35
31
|
originalMessage) {
|
|
36
32
|
// Hot path for straight simple msg translations
|
|
37
|
-
if (els.length === 1 &&
|
|
33
|
+
if (els.length === 1 && isLiteralElement(els[0])) {
|
|
38
34
|
return [
|
|
39
35
|
{
|
|
40
36
|
type: PART_TYPE.literal,
|
|
@@ -46,7 +42,7 @@ originalMessage) {
|
|
|
46
42
|
for (var _i = 0, els_1 = els; _i < els_1.length; _i++) {
|
|
47
43
|
var el = els_1[_i];
|
|
48
44
|
// Exit early for string parts.
|
|
49
|
-
if (
|
|
45
|
+
if (isLiteralElement(el)) {
|
|
50
46
|
result.push({
|
|
51
47
|
type: PART_TYPE.literal,
|
|
52
48
|
value: el.value,
|
|
@@ -55,7 +51,7 @@ originalMessage) {
|
|
|
55
51
|
}
|
|
56
52
|
// TODO: should this part be literal type?
|
|
57
53
|
// Replace `#` in plural rules with the actual numeric value.
|
|
58
|
-
if (
|
|
54
|
+
if (isPoundElement(el)) {
|
|
59
55
|
if (typeof currentPluralValue === 'number') {
|
|
60
56
|
result.push({
|
|
61
57
|
type: PART_TYPE.literal,
|
|
@@ -67,10 +63,10 @@ originalMessage) {
|
|
|
67
63
|
var varName = el.value;
|
|
68
64
|
// Enforce that all required values are provided by the caller.
|
|
69
65
|
if (!(values && varName in values)) {
|
|
70
|
-
throw new
|
|
66
|
+
throw new MissingValueError(varName, originalMessage);
|
|
71
67
|
}
|
|
72
68
|
var value = values[varName];
|
|
73
|
-
if (
|
|
69
|
+
if (isArgumentElement(el)) {
|
|
74
70
|
if (!value || typeof value === 'string' || typeof value === 'number') {
|
|
75
71
|
value =
|
|
76
72
|
typeof value === 'string' || typeof value === 'number'
|
|
@@ -86,10 +82,10 @@ originalMessage) {
|
|
|
86
82
|
// Recursively format plural and select parts' option — which can be a
|
|
87
83
|
// nested pattern structure. The choosing of the option to use is
|
|
88
84
|
// abstracted-by and delegated-to the part helper object.
|
|
89
|
-
if (
|
|
85
|
+
if (isDateElement(el)) {
|
|
90
86
|
var style = typeof el.style === 'string'
|
|
91
87
|
? formats.date[el.style]
|
|
92
|
-
:
|
|
88
|
+
: isDateTimeSkeleton(el.style)
|
|
93
89
|
? el.style.parsedOptions
|
|
94
90
|
: undefined;
|
|
95
91
|
result.push({
|
|
@@ -100,10 +96,10 @@ originalMessage) {
|
|
|
100
96
|
});
|
|
101
97
|
continue;
|
|
102
98
|
}
|
|
103
|
-
if (
|
|
99
|
+
if (isTimeElement(el)) {
|
|
104
100
|
var style = typeof el.style === 'string'
|
|
105
101
|
? formats.time[el.style]
|
|
106
|
-
:
|
|
102
|
+
: isDateTimeSkeleton(el.style)
|
|
107
103
|
? el.style.parsedOptions
|
|
108
104
|
: formats.time.medium;
|
|
109
105
|
result.push({
|
|
@@ -114,10 +110,10 @@ originalMessage) {
|
|
|
114
110
|
});
|
|
115
111
|
continue;
|
|
116
112
|
}
|
|
117
|
-
if (
|
|
113
|
+
if (isNumberElement(el)) {
|
|
118
114
|
var style = typeof el.style === 'string'
|
|
119
115
|
? formats.number[el.style]
|
|
120
|
-
:
|
|
116
|
+
: isNumberSkeleton(el.style)
|
|
121
117
|
? el.style.parsedOptions
|
|
122
118
|
: undefined;
|
|
123
119
|
if (style && style.scale) {
|
|
@@ -133,11 +129,11 @@ originalMessage) {
|
|
|
133
129
|
});
|
|
134
130
|
continue;
|
|
135
131
|
}
|
|
136
|
-
if (
|
|
132
|
+
if (isTagElement(el)) {
|
|
137
133
|
var children = el.children, value_1 = el.value;
|
|
138
134
|
var formatFn = values[value_1];
|
|
139
135
|
if (!isFormatXMLElementFn(formatFn)) {
|
|
140
|
-
throw new
|
|
136
|
+
throw new InvalidValueTypeError(value_1, 'function', originalMessage);
|
|
141
137
|
}
|
|
142
138
|
var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue);
|
|
143
139
|
var chunks = formatFn(parts.map(function (p) { return p.value; }));
|
|
@@ -151,19 +147,19 @@ originalMessage) {
|
|
|
151
147
|
};
|
|
152
148
|
}));
|
|
153
149
|
}
|
|
154
|
-
if (
|
|
150
|
+
if (isSelectElement(el)) {
|
|
155
151
|
var opt = el.options[value] || el.options.other;
|
|
156
152
|
if (!opt) {
|
|
157
|
-
throw new
|
|
153
|
+
throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
|
|
158
154
|
}
|
|
159
155
|
result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values));
|
|
160
156
|
continue;
|
|
161
157
|
}
|
|
162
|
-
if (
|
|
158
|
+
if (isPluralElement(el)) {
|
|
163
159
|
var opt = el.options["=".concat(value)];
|
|
164
160
|
if (!opt) {
|
|
165
161
|
if (!Intl.PluralRules) {
|
|
166
|
-
throw new
|
|
162
|
+
throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API, originalMessage);
|
|
167
163
|
}
|
|
168
164
|
var rule = formatters
|
|
169
165
|
.getPluralRules(locales, { type: el.pluralType })
|
|
@@ -171,7 +167,7 @@ originalMessage) {
|
|
|
171
167
|
opt = el.options[rule] || el.options.other;
|
|
172
168
|
}
|
|
173
169
|
if (!opt) {
|
|
174
|
-
throw new
|
|
170
|
+
throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
|
|
175
171
|
}
|
|
176
172
|
result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0)));
|
|
177
173
|
continue;
|
|
@@ -179,4 +175,3 @@ originalMessage) {
|
|
|
179
175
|
}
|
|
180
176
|
return mergeLiteral(result);
|
|
181
177
|
}
|
|
182
|
-
exports.formatToParts = formatToParts;
|