intl-messageformat
Advanced tools
Comparing version
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/* | ||
@@ -6,6 +7,8 @@ Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
*/ | ||
import { IntlMessageFormat } from './src/core'; | ||
export * from './src/formatters'; | ||
export * from './src/core'; | ||
export * from './src/error'; | ||
export default IntlMessageFormat; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var core_1 = require("./src/core"); | ||
tslib_1.__exportStar(require("./src/formatters"), exports); | ||
tslib_1.__exportStar(require("./src/core"), exports); | ||
tslib_1.__exportStar(require("./src/error"), exports); | ||
exports.default = core_1.IntlMessageFormat; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/* | ||
@@ -6,6 +7,8 @@ Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
*/ | ||
import { __assign, __rest, __spreadArray } from "tslib"; | ||
import { parse, } from '@formatjs/icu-messageformat-parser'; | ||
import { memoize, strategies } from '@formatjs/fast-memoize'; | ||
import { formatToParts, PART_TYPE, } from './formatters'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IntlMessageFormat = void 0; | ||
var tslib_1 = require("tslib"); | ||
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser"); | ||
var fast_memoize_1 = require("@formatjs/fast-memoize"); | ||
var formatters_1 = require("./formatters"); | ||
// -- MessageFormat -------------------------------------------------------- | ||
@@ -16,4 +19,4 @@ function mergeConfig(c1, c2) { | ||
} | ||
return __assign(__assign(__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) { | ||
all[k] = __assign(__assign({}, c1[k]), (c2[k] || {})); | ||
return tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) { | ||
all[k] = tslib_1.__assign(tslib_1.__assign({}, c1[k]), (c2[k] || {})); | ||
return all; | ||
@@ -29,3 +32,3 @@ }, {})); | ||
return all; | ||
}, __assign({}, defaultConfig)); | ||
}, tslib_1.__assign({}, defaultConfig)); | ||
} | ||
@@ -53,3 +56,3 @@ function createFastMemoizeCache(store) { | ||
return { | ||
getNumberFormat: memoize(function () { | ||
getNumberFormat: (0, fast_memoize_1.memoize)(function () { | ||
var _a; | ||
@@ -60,8 +63,8 @@ var args = []; | ||
} | ||
return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))(); | ||
return new ((_a = Intl.NumberFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))(); | ||
}, { | ||
cache: createFastMemoizeCache(cache.number), | ||
strategy: strategies.variadic, | ||
strategy: fast_memoize_1.strategies.variadic, | ||
}), | ||
getDateTimeFormat: memoize(function () { | ||
getDateTimeFormat: (0, fast_memoize_1.memoize)(function () { | ||
var _a; | ||
@@ -72,8 +75,8 @@ var args = []; | ||
} | ||
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))(); | ||
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))(); | ||
}, { | ||
cache: createFastMemoizeCache(cache.dateTime), | ||
strategy: strategies.variadic, | ||
strategy: fast_memoize_1.strategies.variadic, | ||
}), | ||
getPluralRules: memoize(function () { | ||
getPluralRules: (0, fast_memoize_1.memoize)(function () { | ||
var _a; | ||
@@ -84,13 +87,13 @@ var args = []; | ||
} | ||
return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))(); | ||
return new ((_a = Intl.PluralRules).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))(); | ||
}, { | ||
cache: createFastMemoizeCache(cache.pluralRules), | ||
strategy: strategies.variadic, | ||
strategy: fast_memoize_1.strategies.variadic, | ||
}), | ||
}; | ||
} | ||
export var IntlMessageFormat = /** @class */ (function () { | ||
var IntlMessageFormat = /** @class */ (function () { | ||
function IntlMessageFormat(message, locales, overrideFormats, opts) { | ||
var _this = this; | ||
if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; } | ||
var _this = this; | ||
this.formatterCache = { | ||
@@ -109,3 +112,3 @@ number: {}, | ||
if (!all.length || | ||
part.type !== PART_TYPE.literal || | ||
part.type !== formatters_1.PART_TYPE.literal || | ||
typeof all[all.length - 1] !== 'string') { | ||
@@ -125,3 +128,3 @@ all.push(part.value); | ||
this.formatToParts = function (values) { | ||
return formatToParts(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message); | ||
return (0, formatters_1.formatToParts)(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message); | ||
}; | ||
@@ -144,5 +147,5 @@ this.resolvedOptions = function () { | ||
} | ||
var _a = opts || {}, formatters = _a.formatters, parseOpts = __rest(_a, ["formatters"]); | ||
var _a = opts || {}, formatters = _a.formatters, parseOpts = tslib_1.__rest(_a, ["formatters"]); | ||
// Parse string messages into an AST. | ||
this.ast = IntlMessageFormat.__parse(message, __assign(__assign({}, parseOpts), { locale: this.resolvedLocale })); | ||
this.ast = IntlMessageFormat.__parse(message, tslib_1.__assign(tslib_1.__assign({}, parseOpts), { locale: this.resolvedLocale })); | ||
} | ||
@@ -183,3 +186,3 @@ else { | ||
}; | ||
IntlMessageFormat.__parse = parse; | ||
IntlMessageFormat.__parse = icu_messageformat_parser_1.parse; | ||
// Default format options used as the prototype of the `formats` provided to the | ||
@@ -249,1 +252,2 @@ // constructor. These are used when constructing the internal Intl.NumberFormat | ||
}()); | ||
exports.IntlMessageFormat = IntlMessageFormat; |
@@ -1,3 +0,6 @@ | ||
import { __extends } from "tslib"; | ||
export var ErrorCode; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MissingValueError = exports.InvalidValueTypeError = exports.InvalidValueError = exports.FormatError = exports.ErrorCode = void 0; | ||
var tslib_1 = require("tslib"); | ||
var ErrorCode; | ||
(function (ErrorCode) { | ||
@@ -10,5 +13,5 @@ // When we have a placeholder but no value to format | ||
ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API"; | ||
})(ErrorCode || (ErrorCode = {})); | ||
})(ErrorCode || (exports.ErrorCode = ErrorCode = {})); | ||
var FormatError = /** @class */ (function (_super) { | ||
__extends(FormatError, _super); | ||
tslib_1.__extends(FormatError, _super); | ||
function FormatError(msg, code, originalMessage) { | ||
@@ -25,5 +28,5 @@ var _this = _super.call(this, msg) || this; | ||
}(Error)); | ||
export { FormatError }; | ||
exports.FormatError = FormatError; | ||
var InvalidValueError = /** @class */ (function (_super) { | ||
__extends(InvalidValueError, _super); | ||
tslib_1.__extends(InvalidValueError, _super); | ||
function InvalidValueError(variableId, value, options, originalMessage) { | ||
@@ -34,5 +37,5 @@ return _super.call(this, "Invalid values for \"".concat(variableId, "\": \"").concat(value, "\". Options are \"").concat(Object.keys(options).join('", "'), "\""), ErrorCode.INVALID_VALUE, originalMessage) || this; | ||
}(FormatError)); | ||
export { InvalidValueError }; | ||
exports.InvalidValueError = InvalidValueError; | ||
var InvalidValueTypeError = /** @class */ (function (_super) { | ||
__extends(InvalidValueTypeError, _super); | ||
tslib_1.__extends(InvalidValueTypeError, _super); | ||
function InvalidValueTypeError(value, type, originalMessage) { | ||
@@ -43,5 +46,5 @@ return _super.call(this, "Value for \"".concat(value, "\" must be of type ").concat(type), ErrorCode.INVALID_VALUE, originalMessage) || this; | ||
}(FormatError)); | ||
export { InvalidValueTypeError }; | ||
exports.InvalidValueTypeError = InvalidValueTypeError; | ||
var MissingValueError = /** @class */ (function (_super) { | ||
__extends(MissingValueError, _super); | ||
tslib_1.__extends(MissingValueError, _super); | ||
function MissingValueError(variableId, originalMessage) { | ||
@@ -52,2 +55,2 @@ return _super.call(this, "The intl string context variable \"".concat(variableId, "\" was not provided to the string \"").concat(originalMessage, "\""), ErrorCode.MISSING_VALUE, originalMessage) || this; | ||
}(FormatError)); | ||
export { MissingValueError }; | ||
exports.MissingValueError = MissingValueError; |
@@ -1,8 +0,11 @@ | ||
import { isArgumentElement, isDateElement, isDateTimeSkeleton, isLiteralElement, isNumberElement, isNumberSkeleton, isPluralElement, isPoundElement, isSelectElement, isTimeElement, isTagElement, } from '@formatjs/icu-messageformat-parser'; | ||
import { MissingValueError, InvalidValueError, ErrorCode, FormatError, InvalidValueTypeError, } from './error'; | ||
export var PART_TYPE; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatToParts = exports.isFormatXMLElementFn = exports.PART_TYPE = void 0; | ||
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser"); | ||
var error_1 = require("./error"); | ||
var PART_TYPE; | ||
(function (PART_TYPE) { | ||
PART_TYPE[PART_TYPE["literal"] = 0] = "literal"; | ||
PART_TYPE[PART_TYPE["object"] = 1] = "object"; | ||
})(PART_TYPE || (PART_TYPE = {})); | ||
})(PART_TYPE || (exports.PART_TYPE = PART_TYPE = {})); | ||
function mergeLiteral(parts) { | ||
@@ -25,11 +28,12 @@ if (parts.length < 2) { | ||
} | ||
export function isFormatXMLElementFn(el) { | ||
function isFormatXMLElementFn(el) { | ||
return typeof el === 'function'; | ||
} | ||
exports.isFormatXMLElementFn = isFormatXMLElementFn; | ||
// TODO(skeleton): add skeleton support | ||
export function formatToParts(els, locales, formatters, formats, values, currentPluralValue, | ||
function formatToParts(els, locales, formatters, formats, values, currentPluralValue, | ||
// For debugging | ||
originalMessage) { | ||
// Hot path for straight simple msg translations | ||
if (els.length === 1 && isLiteralElement(els[0])) { | ||
if (els.length === 1 && (0, icu_messageformat_parser_1.isLiteralElement)(els[0])) { | ||
return [ | ||
@@ -46,3 +50,3 @@ { | ||
// Exit early for string parts. | ||
if (isLiteralElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isLiteralElement)(el)) { | ||
result.push({ | ||
@@ -56,3 +60,3 @@ type: PART_TYPE.literal, | ||
// Replace `#` in plural rules with the actual numeric value. | ||
if (isPoundElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isPoundElement)(el)) { | ||
if (typeof currentPluralValue === 'number') { | ||
@@ -69,6 +73,6 @@ result.push({ | ||
if (!(values && varName in values)) { | ||
throw new MissingValueError(varName, originalMessage); | ||
throw new error_1.MissingValueError(varName, originalMessage); | ||
} | ||
var value = values[varName]; | ||
if (isArgumentElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isArgumentElement)(el)) { | ||
if (!value || typeof value === 'string' || typeof value === 'number') { | ||
@@ -89,6 +93,6 @@ value = | ||
// abstracted-by and delegated-to the part helper object. | ||
if (isDateElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isDateElement)(el)) { | ||
var style = typeof el.style === 'string' | ||
? formats.date[el.style] | ||
: isDateTimeSkeleton(el.style) | ||
: (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style) | ||
? el.style.parsedOptions | ||
@@ -104,6 +108,6 @@ : undefined; | ||
} | ||
if (isTimeElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isTimeElement)(el)) { | ||
var style = typeof el.style === 'string' | ||
? formats.time[el.style] | ||
: isDateTimeSkeleton(el.style) | ||
: (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style) | ||
? el.style.parsedOptions | ||
@@ -119,6 +123,6 @@ : formats.time.medium; | ||
} | ||
if (isNumberElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isNumberElement)(el)) { | ||
var style = typeof el.style === 'string' | ||
? formats.number[el.style] | ||
: isNumberSkeleton(el.style) | ||
: (0, icu_messageformat_parser_1.isNumberSkeleton)(el.style) | ||
? el.style.parsedOptions | ||
@@ -139,7 +143,7 @@ : undefined; | ||
} | ||
if (isTagElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isTagElement)(el)) { | ||
var children = el.children, value_1 = el.value; | ||
var formatFn = values[value_1]; | ||
if (!isFormatXMLElementFn(formatFn)) { | ||
throw new InvalidValueTypeError(value_1, 'function', originalMessage); | ||
throw new error_1.InvalidValueTypeError(value_1, 'function', originalMessage); | ||
} | ||
@@ -158,6 +162,6 @@ var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue); | ||
} | ||
if (isSelectElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isSelectElement)(el)) { | ||
var opt = el.options[value] || el.options.other; | ||
if (!opt) { | ||
throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
} | ||
@@ -167,7 +171,7 @@ result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values)); | ||
} | ||
if (isPluralElement(el)) { | ||
if ((0, icu_messageformat_parser_1.isPluralElement)(el)) { | ||
var opt = el.options["=".concat(value)]; | ||
if (!opt) { | ||
if (!Intl.PluralRules) { | ||
throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API, originalMessage); | ||
throw new error_1.FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", error_1.ErrorCode.MISSING_INTL_API, originalMessage); | ||
} | ||
@@ -180,3 +184,3 @@ var rule = formatters | ||
if (!opt) { | ||
throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage); | ||
} | ||
@@ -189,1 +193,2 @@ result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0))); | ||
} | ||
exports.formatToParts = formatToParts; |
{ | ||
"name": "intl-messageformat", | ||
"version": "10.5.4", | ||
"version": "10.5.5", | ||
"description": "Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.", | ||
@@ -35,5 +35,5 @@ "keywords": [ | ||
"tslib": "^2.4.0", | ||
"@formatjs/ecma402-abstract": "1.17.2", | ||
"@formatjs/icu-messageformat-parser": "2.7.0", | ||
"@formatjs/fast-memoize": "2.2.0" | ||
"@formatjs/ecma402-abstract": "1.17.3", | ||
"@formatjs/fast-memoize": "2.2.0", | ||
"@formatjs/icu-messageformat-parser": "2.7.1" | ||
}, | ||
@@ -40,0 +40,0 @@ "sideEffects": false, |
@@ -88,6 +88,6 @@ "use strict"; | ||
} | ||
var IntlMessageFormat = exports.IntlMessageFormat = /** @class */ (function () { | ||
var IntlMessageFormat = /** @class */ (function () { | ||
function IntlMessageFormat(message, locales, overrideFormats, opts) { | ||
var _this = this; | ||
if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; } | ||
var _this = this; | ||
this.formatterCache = { | ||
@@ -242,1 +242,2 @@ number: {}, | ||
}()); | ||
exports.IntlMessageFormat = IntlMessageFormat; |
@@ -13,3 +13,3 @@ "use strict"; | ||
ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API"; | ||
})(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {})); | ||
})(ErrorCode || (exports.ErrorCode = ErrorCode = {})); | ||
var FormatError = /** @class */ (function (_super) { | ||
@@ -16,0 +16,0 @@ tslib_1.__extends(FormatError, _super); |
@@ -10,3 +10,3 @@ "use strict"; | ||
PART_TYPE[PART_TYPE["object"] = 1] = "object"; | ||
})(PART_TYPE = exports.PART_TYPE || (exports.PART_TYPE = {})); | ||
})(PART_TYPE || (exports.PART_TYPE = PART_TYPE = {})); | ||
function mergeLiteral(parts) { | ||
@@ -13,0 +13,0 @@ if (parts.length < 2) { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
299493
33.67%8876
14.32%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed