@formatjs/intl 3.1.7 → 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 +11 -8
- 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/src/message.js
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 { TYPE } from '@formatjs/icu-messageformat-parser';
|
|
3
|
+
import { IntlMessageFormat, } from 'intl-messageformat';
|
|
4
|
+
import { MessageFormatError, MissingTranslationError } from './error.js';
|
|
5
|
+
import { invariant } from './utils.js';
|
|
9
6
|
function setTimeZoneInOptions(opts, timeZone) {
|
|
10
7
|
return Object.keys(opts).reduce(function (all, k) {
|
|
11
|
-
all[k] =
|
|
8
|
+
all[k] = __assign({ timeZone: timeZone }, opts[k]);
|
|
12
9
|
return all;
|
|
13
10
|
}, {});
|
|
14
11
|
}
|
|
15
12
|
function deepMergeOptions(opts1, opts2) {
|
|
16
|
-
var keys = Object.keys(
|
|
13
|
+
var keys = Object.keys(__assign(__assign({}, opts1), opts2));
|
|
17
14
|
return keys.reduce(function (all, k) {
|
|
18
|
-
all[k] =
|
|
15
|
+
all[k] = __assign(__assign({}, opts1[k]), opts2[k]);
|
|
19
16
|
return all;
|
|
20
17
|
}, {});
|
|
21
18
|
}
|
|
@@ -23,15 +20,15 @@ function deepMergeFormatsAndSetTimeZone(f1, timeZone) {
|
|
|
23
20
|
if (!timeZone) {
|
|
24
21
|
return f1;
|
|
25
22
|
}
|
|
26
|
-
var mfFormats =
|
|
27
|
-
return
|
|
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)) });
|
|
28
25
|
}
|
|
29
|
-
var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
26
|
+
export var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
30
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;
|
|
31
28
|
if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
|
|
32
29
|
var msgId = messageDescriptor.id, defaultMessage = messageDescriptor.defaultMessage;
|
|
33
30
|
// `id` is a required field of a Message Descriptor.
|
|
34
|
-
|
|
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");
|
|
35
32
|
var id = String(msgId);
|
|
36
33
|
var message =
|
|
37
34
|
// In case messages is Object.create(null)
|
|
@@ -43,7 +40,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
43
40
|
// IMPORTANT: Hot path if `message` is AST with a single literal node
|
|
44
41
|
if (Array.isArray(message) &&
|
|
45
42
|
message.length === 1 &&
|
|
46
|
-
message[0].type ===
|
|
43
|
+
message[0].type === TYPE.literal) {
|
|
47
44
|
return message[0].value;
|
|
48
45
|
}
|
|
49
46
|
// IMPORTANT: Hot path straight lookup for performance
|
|
@@ -53,7 +50,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
53
50
|
!defaultRichTextElements) {
|
|
54
51
|
return message.replace(/'\{(.*?)\}'/gi, "{$1}");
|
|
55
52
|
}
|
|
56
|
-
values =
|
|
53
|
+
values = __assign(__assign({}, defaultRichTextElements), values);
|
|
57
54
|
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
|
|
58
55
|
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
|
|
59
56
|
if (!message) {
|
|
@@ -65,7 +62,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
65
62
|
// This prevents warnings from littering the console in development
|
|
66
63
|
// when no `messages` are passed into the <IntlProvider> for the
|
|
67
64
|
// default locale.
|
|
68
|
-
onError(new
|
|
65
|
+
onError(new MissingTranslationError(messageDescriptor, locale));
|
|
69
66
|
}
|
|
70
67
|
if (defaultMessage) {
|
|
71
68
|
try {
|
|
@@ -73,7 +70,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
73
70
|
return formatter.format(values);
|
|
74
71
|
}
|
|
75
72
|
catch (e) {
|
|
76
|
-
onError(new
|
|
73
|
+
onError(new MessageFormatError("Error formatting default message for: \"".concat(id, "\", rendering default message verbatim"), locale, messageDescriptor, e));
|
|
77
74
|
return typeof defaultMessage === 'string' ? defaultMessage : id;
|
|
78
75
|
}
|
|
79
76
|
}
|
|
@@ -81,11 +78,11 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
81
78
|
}
|
|
82
79
|
// We have the translated message
|
|
83
80
|
try {
|
|
84
|
-
var formatter = state.getMessageFormat(message, locale, formats,
|
|
81
|
+
var formatter = state.getMessageFormat(message, locale, formats, __assign({ formatters: state }, opts));
|
|
85
82
|
return formatter.format(values);
|
|
86
83
|
}
|
|
87
84
|
catch (e) {
|
|
88
|
-
onError(new
|
|
85
|
+
onError(new MessageFormatError("Error formatting message: \"".concat(id, "\", using ").concat(defaultMessage ? 'default message' : 'id', " as fallback."), locale, messageDescriptor, e));
|
|
89
86
|
}
|
|
90
87
|
if (defaultMessage) {
|
|
91
88
|
try {
|
|
@@ -93,7 +90,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
93
90
|
return formatter.format(values);
|
|
94
91
|
}
|
|
95
92
|
catch (e) {
|
|
96
|
-
onError(new
|
|
93
|
+
onError(new MessageFormatError("Error formatting the default message for: \"".concat(id, "\", rendering message verbatim"), locale, messageDescriptor, e));
|
|
97
94
|
}
|
|
98
95
|
}
|
|
99
96
|
if (typeof message === 'string') {
|
|
@@ -104,4 +101,3 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
|
|
|
104
101
|
}
|
|
105
102
|
return id;
|
|
106
103
|
};
|
|
107
|
-
exports.formatMessage = formatMessage;
|
package/src/number.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, }: {
|
|
3
3
|
locale: string;
|
|
4
4
|
formats: CustomFormats;
|
package/src/number.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.getFormatter = getFormatter;
|
|
4
|
-
exports.formatNumber = formatNumber;
|
|
5
|
-
exports.formatNumberToParts = formatNumberToParts;
|
|
6
|
-
var error_1 = require("./error");
|
|
7
|
-
var utils_1 = require("./utils");
|
|
1
|
+
import { IntlFormatError } from './error.js';
|
|
2
|
+
import { filterProps, getNamedFormat } from './utils.js';
|
|
8
3
|
var NUMBER_FORMAT_OPTIONS = [
|
|
9
4
|
'style',
|
|
10
5
|
'currency',
|
|
@@ -31,33 +26,33 @@ var NUMBER_FORMAT_OPTIONS = [
|
|
|
31
26
|
'roundingIncrement',
|
|
32
27
|
'roundingMode',
|
|
33
28
|
];
|
|
34
|
-
function getFormatter(_a, getNumberFormat, options) {
|
|
29
|
+
export function getFormatter(_a, getNumberFormat, options) {
|
|
35
30
|
var locale = _a.locale, formats = _a.formats, onError = _a.onError;
|
|
36
31
|
if (options === void 0) { options = {}; }
|
|
37
32
|
var format = options.format;
|
|
38
33
|
var defaults = ((format &&
|
|
39
|
-
|
|
34
|
+
getNamedFormat(formats, 'number', format, onError)) ||
|
|
40
35
|
{});
|
|
41
|
-
var filteredOptions =
|
|
36
|
+
var filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults);
|
|
42
37
|
return getNumberFormat(locale, filteredOptions);
|
|
43
38
|
}
|
|
44
|
-
function formatNumber(config, getNumberFormat, value, options) {
|
|
39
|
+
export function formatNumber(config, getNumberFormat, value, options) {
|
|
45
40
|
if (options === void 0) { options = {}; }
|
|
46
41
|
try {
|
|
47
42
|
return getFormatter(config, getNumberFormat, options).format(value);
|
|
48
43
|
}
|
|
49
44
|
catch (e) {
|
|
50
|
-
config.onError(new
|
|
45
|
+
config.onError(new IntlFormatError('Error formatting number.', config.locale, e));
|
|
51
46
|
}
|
|
52
47
|
return String(value);
|
|
53
48
|
}
|
|
54
|
-
function formatNumberToParts(config, getNumberFormat, value, options) {
|
|
49
|
+
export function formatNumberToParts(config, getNumberFormat, value, options) {
|
|
55
50
|
if (options === void 0) { options = {}; }
|
|
56
51
|
try {
|
|
57
52
|
return getFormatter(config, getNumberFormat, options).formatToParts(value);
|
|
58
53
|
}
|
|
59
54
|
catch (e) {
|
|
60
|
-
config.onError(new
|
|
55
|
+
config.onError(new IntlFormatError('Error formatting number.', config.locale, e));
|
|
61
56
|
}
|
|
62
57
|
return [];
|
|
63
58
|
}
|
package/src/plural.d.ts
CHANGED
package/src/plural.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var intl_messageformat_1 = require("intl-messageformat");
|
|
5
|
-
var error_1 = require("./error");
|
|
6
|
-
var utils_1 = require("./utils");
|
|
1
|
+
import { ErrorCode, FormatError } from 'intl-messageformat';
|
|
2
|
+
import { IntlFormatError } from './error.js';
|
|
3
|
+
import { filterProps } from './utils.js';
|
|
7
4
|
var PLURAL_FORMAT_OPTIONS = ['type'];
|
|
8
|
-
function formatPlural(_a, getPluralRules, value, options) {
|
|
5
|
+
export function formatPlural(_a, getPluralRules, value, options) {
|
|
9
6
|
var locale = _a.locale, onError = _a.onError;
|
|
10
7
|
if (options === void 0) { options = {}; }
|
|
11
8
|
if (!Intl.PluralRules) {
|
|
12
|
-
onError(new
|
|
9
|
+
onError(new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API));
|
|
13
10
|
}
|
|
14
|
-
var filteredOptions =
|
|
11
|
+
var filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
|
|
15
12
|
try {
|
|
16
13
|
return getPluralRules(locale, filteredOptions).select(value);
|
|
17
14
|
}
|
|
18
15
|
catch (e) {
|
|
19
|
-
onError(new
|
|
16
|
+
onError(new IntlFormatError('Error formatting plural.', locale, e));
|
|
20
17
|
}
|
|
21
18
|
return 'other';
|
|
22
19
|
}
|
package/src/relativeTime.d.ts
CHANGED
package/src/relativeTime.js
CHANGED
|
@@ -1,32 +1,29 @@
|
|
|
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 { getNamedFormat, filterProps } from './utils.js';
|
|
2
|
+
import { FormatError, ErrorCode } from 'intl-messageformat';
|
|
3
|
+
import { IntlFormatError } from './error.js';
|
|
7
4
|
var RELATIVE_TIME_FORMAT_OPTIONS = ['numeric', 'style'];
|
|
8
5
|
function getFormatter(_a, getRelativeTimeFormat, options) {
|
|
9
6
|
var locale = _a.locale, formats = _a.formats, onError = _a.onError;
|
|
10
7
|
if (options === void 0) { options = {}; }
|
|
11
8
|
var format = options.format;
|
|
12
|
-
var defaults = (!!format &&
|
|
13
|
-
var filteredOptions =
|
|
9
|
+
var defaults = (!!format && getNamedFormat(formats, 'relative', format, onError)) || {};
|
|
10
|
+
var filteredOptions = filterProps(options, RELATIVE_TIME_FORMAT_OPTIONS, defaults);
|
|
14
11
|
return getRelativeTimeFormat(locale, filteredOptions);
|
|
15
12
|
}
|
|
16
|
-
function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options) {
|
|
13
|
+
export function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options) {
|
|
17
14
|
if (options === void 0) { options = {}; }
|
|
18
15
|
if (!unit) {
|
|
19
16
|
unit = 'second';
|
|
20
17
|
}
|
|
21
18
|
var RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
22
19
|
if (!RelativeTimeFormat) {
|
|
23
|
-
config.onError(new
|
|
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));
|
|
24
21
|
}
|
|
25
22
|
try {
|
|
26
23
|
return getFormatter(config, getRelativeTimeFormat, options).format(value, unit);
|
|
27
24
|
}
|
|
28
25
|
catch (e) {
|
|
29
|
-
config.onError(new
|
|
26
|
+
config.onError(new IntlFormatError('Error formatting relative time.', config.locale, e));
|
|
30
27
|
}
|
|
31
28
|
return String(value);
|
|
32
29
|
}
|
package/src/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
2
2
|
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
3
3
|
import { FormatError, Formats, FormatXMLElementFn, IntlMessageFormat, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
|
|
4
|
-
import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error';
|
|
5
|
-
import { DEFAULT_INTL_CONFIG } from './utils';
|
|
4
|
+
import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error.js';
|
|
5
|
+
import { DEFAULT_INTL_CONFIG } from './utils.js';
|
|
6
6
|
export interface Part<T = string> {
|
|
7
7
|
type: 'element' | 'literal';
|
|
8
8
|
value: T;
|
|
@@ -51,6 +51,7 @@ export interface CustomFormatConfig<Source = string> {
|
|
|
51
51
|
}
|
|
52
52
|
export type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'dateTimeRange'>;
|
|
53
53
|
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
54
|
+
export type FormatTimeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
54
55
|
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
55
56
|
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
56
57
|
export type FormatPluralOptions = Omit<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
|
|
@@ -63,7 +64,7 @@ export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMat
|
|
|
63
64
|
export interface IntlFormatters<TBase = unknown> {
|
|
64
65
|
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0] | string, to: Parameters<Intl.DateTimeFormat['formatRange']>[1] | string, opts?: FormatDateTimeRangeOptions): string;
|
|
65
66
|
formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
66
|
-
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?:
|
|
67
|
+
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatTimeOptions): string;
|
|
67
68
|
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
68
69
|
formatTimeToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
69
70
|
formatRelativeTime(this: void, value: Parameters<Intl.RelativeTimeFormat['format']>[0], unit?: Parameters<Intl.RelativeTimeFormat['format']>[1], opts?: FormatRelativeTimeOptions): string;
|
package/src/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/src/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
|
-
import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types';
|
|
2
|
+
import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types.js';
|
|
3
3
|
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
|
|
4
4
|
export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
|
|
5
5
|
export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, 'fallbackOnEmptyString' | 'formats' | 'messages' | 'timeZone' | 'defaultLocale' | 'defaultFormats' | 'onError' | 'onWarn'>;
|
package/src/utils.js
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
exports.createIntlCache = createIntlCache;
|
|
7
|
-
exports.createFormatters = createFormatters;
|
|
8
|
-
exports.getNamedFormat = getNamedFormat;
|
|
9
|
-
var tslib_1 = require("tslib");
|
|
10
|
-
var fast_memoize_1 = require("@formatjs/fast-memoize");
|
|
11
|
-
var intl_messageformat_1 = require("intl-messageformat");
|
|
12
|
-
var error_1 = require("./error");
|
|
13
|
-
function invariant(condition, message, Err) {
|
|
1
|
+
import { __assign, __spreadArray } from "tslib";
|
|
2
|
+
import { memoize, strategies } from '@formatjs/fast-memoize';
|
|
3
|
+
import { IntlMessageFormat } from 'intl-messageformat';
|
|
4
|
+
import { UnsupportedFormatterError } from './error.js';
|
|
5
|
+
export function invariant(condition, message, Err) {
|
|
14
6
|
if (Err === void 0) { Err = Error; }
|
|
15
7
|
if (!condition) {
|
|
16
8
|
throw new Err(message);
|
|
17
9
|
}
|
|
18
10
|
}
|
|
19
|
-
function filterProps(props, allowlist, defaults) {
|
|
11
|
+
export function filterProps(props, allowlist, defaults) {
|
|
20
12
|
if (defaults === void 0) { defaults = {}; }
|
|
21
13
|
return allowlist.reduce(function (filtered, name) {
|
|
22
14
|
if (name in props) {
|
|
@@ -40,7 +32,7 @@ var defaultWarnHandler = function (warning) {
|
|
|
40
32
|
console.warn(warning);
|
|
41
33
|
}
|
|
42
34
|
};
|
|
43
|
-
|
|
35
|
+
export var DEFAULT_INTL_CONFIG = {
|
|
44
36
|
formats: {},
|
|
45
37
|
messages: {},
|
|
46
38
|
timeZone: undefined,
|
|
@@ -50,7 +42,7 @@ exports.DEFAULT_INTL_CONFIG = {
|
|
|
50
42
|
onError: defaultErrorHandler,
|
|
51
43
|
onWarn: defaultWarnHandler,
|
|
52
44
|
};
|
|
53
|
-
function createIntlCache() {
|
|
45
|
+
export function createIntlCache() {
|
|
54
46
|
return {
|
|
55
47
|
dateTime: {},
|
|
56
48
|
number: {},
|
|
@@ -79,91 +71,91 @@ function createFastMemoizeCache(store) {
|
|
|
79
71
|
* Create intl formatters and populate cache
|
|
80
72
|
* @param cache explicit cache to prevent leaking memory
|
|
81
73
|
*/
|
|
82
|
-
function createFormatters(cache) {
|
|
74
|
+
export function createFormatters(cache) {
|
|
83
75
|
if (cache === void 0) { cache = createIntlCache(); }
|
|
84
76
|
var RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
85
77
|
var ListFormat = Intl.ListFormat;
|
|
86
78
|
var DisplayNames = Intl.DisplayNames;
|
|
87
|
-
var getDateTimeFormat =
|
|
79
|
+
var getDateTimeFormat = memoize(function () {
|
|
88
80
|
var _a;
|
|
89
81
|
var args = [];
|
|
90
82
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
91
83
|
args[_i] = arguments[_i];
|
|
92
84
|
}
|
|
93
|
-
return new ((_a = Intl.DateTimeFormat).bind.apply(_a,
|
|
85
|
+
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
94
86
|
}, {
|
|
95
87
|
cache: createFastMemoizeCache(cache.dateTime),
|
|
96
|
-
strategy:
|
|
88
|
+
strategy: strategies.variadic,
|
|
97
89
|
});
|
|
98
|
-
var getNumberFormat =
|
|
90
|
+
var getNumberFormat = memoize(function () {
|
|
99
91
|
var _a;
|
|
100
92
|
var args = [];
|
|
101
93
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
102
94
|
args[_i] = arguments[_i];
|
|
103
95
|
}
|
|
104
|
-
return new ((_a = Intl.NumberFormat).bind.apply(_a,
|
|
96
|
+
return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
105
97
|
}, {
|
|
106
98
|
cache: createFastMemoizeCache(cache.number),
|
|
107
|
-
strategy:
|
|
99
|
+
strategy: strategies.variadic,
|
|
108
100
|
});
|
|
109
|
-
var getPluralRules =
|
|
101
|
+
var getPluralRules = memoize(function () {
|
|
110
102
|
var _a;
|
|
111
103
|
var args = [];
|
|
112
104
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
113
105
|
args[_i] = arguments[_i];
|
|
114
106
|
}
|
|
115
|
-
return new ((_a = Intl.PluralRules).bind.apply(_a,
|
|
107
|
+
return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
116
108
|
}, {
|
|
117
109
|
cache: createFastMemoizeCache(cache.pluralRules),
|
|
118
|
-
strategy:
|
|
110
|
+
strategy: strategies.variadic,
|
|
119
111
|
});
|
|
120
112
|
return {
|
|
121
113
|
getDateTimeFormat: getDateTimeFormat,
|
|
122
114
|
getNumberFormat: getNumberFormat,
|
|
123
|
-
getMessageFormat:
|
|
124
|
-
return new
|
|
115
|
+
getMessageFormat: memoize(function (message, locales, overrideFormats, opts) {
|
|
116
|
+
return new IntlMessageFormat(message, locales, overrideFormats, __assign({ formatters: {
|
|
125
117
|
getNumberFormat: getNumberFormat,
|
|
126
118
|
getDateTimeFormat: getDateTimeFormat,
|
|
127
119
|
getPluralRules: getPluralRules,
|
|
128
|
-
} },
|
|
120
|
+
} }, opts));
|
|
129
121
|
}, {
|
|
130
122
|
cache: createFastMemoizeCache(cache.message),
|
|
131
|
-
strategy:
|
|
123
|
+
strategy: strategies.variadic,
|
|
132
124
|
}),
|
|
133
|
-
getRelativeTimeFormat:
|
|
125
|
+
getRelativeTimeFormat: memoize(function () {
|
|
134
126
|
var args = [];
|
|
135
127
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
136
128
|
args[_i] = arguments[_i];
|
|
137
129
|
}
|
|
138
|
-
return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat,
|
|
130
|
+
return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat, __spreadArray([void 0], args, false)))();
|
|
139
131
|
}, {
|
|
140
132
|
cache: createFastMemoizeCache(cache.relativeTime),
|
|
141
|
-
strategy:
|
|
133
|
+
strategy: strategies.variadic,
|
|
142
134
|
}),
|
|
143
135
|
getPluralRules: getPluralRules,
|
|
144
|
-
getListFormat:
|
|
136
|
+
getListFormat: memoize(function () {
|
|
145
137
|
var args = [];
|
|
146
138
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
147
139
|
args[_i] = arguments[_i];
|
|
148
140
|
}
|
|
149
|
-
return new (ListFormat.bind.apply(ListFormat,
|
|
141
|
+
return new (ListFormat.bind.apply(ListFormat, __spreadArray([void 0], args, false)))();
|
|
150
142
|
}, {
|
|
151
143
|
cache: createFastMemoizeCache(cache.list),
|
|
152
|
-
strategy:
|
|
144
|
+
strategy: strategies.variadic,
|
|
153
145
|
}),
|
|
154
|
-
getDisplayNames:
|
|
146
|
+
getDisplayNames: memoize(function () {
|
|
155
147
|
var args = [];
|
|
156
148
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
157
149
|
args[_i] = arguments[_i];
|
|
158
150
|
}
|
|
159
|
-
return new (DisplayNames.bind.apply(DisplayNames,
|
|
151
|
+
return new (DisplayNames.bind.apply(DisplayNames, __spreadArray([void 0], args, false)))();
|
|
160
152
|
}, {
|
|
161
153
|
cache: createFastMemoizeCache(cache.displayNames),
|
|
162
|
-
strategy:
|
|
154
|
+
strategy: strategies.variadic,
|
|
163
155
|
}),
|
|
164
156
|
};
|
|
165
157
|
}
|
|
166
|
-
function getNamedFormat(formats, type, name, onError) {
|
|
158
|
+
export function getNamedFormat(formats, type, name, onError) {
|
|
167
159
|
var formatType = formats && formats[type];
|
|
168
160
|
var format;
|
|
169
161
|
if (formatType) {
|
|
@@ -172,5 +164,5 @@ function getNamedFormat(formats, type, name, onError) {
|
|
|
172
164
|
if (format) {
|
|
173
165
|
return format;
|
|
174
166
|
}
|
|
175
|
-
onError(new
|
|
167
|
+
onError(new UnsupportedFormatterError("No ".concat(type, " format named: ").concat(name)));
|
|
176
168
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { MessageDescriptor } from './src/types';
|
|
2
|
-
export * from './src/types';
|
|
3
|
-
export declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
|
|
4
|
-
export declare function defineMessage<T>(msg: T): T;
|
|
5
|
-
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
|
|
6
|
-
export * from './src/error';
|
|
7
|
-
export { formatMessage } from './src/message';
|
|
8
|
-
export type { FormatMessageFn } from './src/message';
|
|
9
|
-
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
|
|
10
|
-
export { formatDisplayName } from './src/displayName';
|
|
11
|
-
export { formatList } from './src/list';
|
|
12
|
-
export { formatPlural } from './src/plural';
|
|
13
|
-
export { formatRelativeTime } from './src/relativeTime';
|
|
14
|
-
export { formatNumber, formatNumberToParts } from './src/number';
|
|
15
|
-
export { createIntl } from './src/create-intl';
|
|
16
|
-
export type { CreateIntlFn } from './src/create-intl';
|
package/lib/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export * from './src/types';
|
|
2
|
-
export function defineMessages(msgs) {
|
|
3
|
-
return msgs;
|
|
4
|
-
}
|
|
5
|
-
export function defineMessage(msg) {
|
|
6
|
-
return msg;
|
|
7
|
-
}
|
|
8
|
-
export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
|
|
9
|
-
export * from './src/error';
|
|
10
|
-
export { formatMessage } from './src/message';
|
|
11
|
-
export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
|
|
12
|
-
export { formatDisplayName } from './src/displayName';
|
|
13
|
-
export { formatList } from './src/list';
|
|
14
|
-
export { formatPlural } from './src/plural';
|
|
15
|
-
export { formatRelativeTime } from './src/relativeTime';
|
|
16
|
-
export { formatNumber, formatNumberToParts } from './src/number';
|
|
17
|
-
export { createIntl } from './src/create-intl';
|
package/lib/src/create-intl.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { IntlCache, IntlConfig, IntlShape } from './types';
|
|
2
|
-
export interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
|
|
3
|
-
(config: C, cache?: IntlCache): S;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Create intl object
|
|
7
|
-
* @param config intl config
|
|
8
|
-
* @param cache cache for formatter instances to prevent memory leak
|
|
9
|
-
*/
|
|
10
|
-
export declare function createIntl<T = string>(config: IntlConfig<T>, cache?: IntlCache): IntlShape<T>;
|
package/lib/src/create-intl.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { __assign } from "tslib";
|
|
2
|
-
import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts, } from './dateTime';
|
|
3
|
-
import { formatDisplayName } from './displayName';
|
|
4
|
-
import { InvalidConfigError, MissingDataError } from './error';
|
|
5
|
-
import { formatList, formatListToParts } from './list';
|
|
6
|
-
import { formatMessage } from './message';
|
|
7
|
-
import { formatNumber, formatNumberToParts } from './number';
|
|
8
|
-
import { formatPlural } from './plural';
|
|
9
|
-
import { formatRelativeTime } from './relativeTime';
|
|
10
|
-
import { createFormatters, DEFAULT_INTL_CONFIG } from './utils';
|
|
11
|
-
function messagesContainString(messages) {
|
|
12
|
-
var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
|
|
13
|
-
return typeof firstMessage === 'string';
|
|
14
|
-
}
|
|
15
|
-
function verifyConfigMessages(config) {
|
|
16
|
-
if (config.onWarn &&
|
|
17
|
-
config.defaultRichTextElements &&
|
|
18
|
-
messagesContainString(config.messages || {})) {
|
|
19
|
-
config.onWarn("[@formatjs/intl] \"defaultRichTextElements\" was specified but \"message\" was not pre-compiled. \nPlease consider using \"@formatjs/cli\" to pre-compile your messages for performance.\nFor more details see https://formatjs.github.io/docs/getting-started/message-distribution");
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Create intl object
|
|
24
|
-
* @param config intl config
|
|
25
|
-
* @param cache cache for formatter instances to prevent memory leak
|
|
26
|
-
*/
|
|
27
|
-
export function createIntl(config, cache) {
|
|
28
|
-
var formatters = createFormatters(cache);
|
|
29
|
-
var resolvedConfig = __assign(__assign({}, DEFAULT_INTL_CONFIG), config);
|
|
30
|
-
var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
|
|
31
|
-
if (!locale) {
|
|
32
|
-
if (onError) {
|
|
33
|
-
onError(new InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details")));
|
|
34
|
-
}
|
|
35
|
-
// Since there's no registered locale data for `locale`, this will
|
|
36
|
-
// fallback to the `defaultLocale` to make sure things can render.
|
|
37
|
-
// The `messages` are overridden to the `defaultProps` empty object
|
|
38
|
-
// to maintain referential equality across re-renders. It's assumed
|
|
39
|
-
// each <FormattedMessage> contains a `defaultMessage` prop.
|
|
40
|
-
resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
|
|
41
|
-
}
|
|
42
|
-
else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
|
|
43
|
-
onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
|
|
44
|
-
}
|
|
45
|
-
else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
|
|
46
|
-
onError) {
|
|
47
|
-
onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
|
|
48
|
-
}
|
|
49
|
-
verifyConfigMessages(resolvedConfig);
|
|
50
|
-
return __assign(__assign({}, resolvedConfig), { formatters: formatters, formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), 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) });
|
|
51
|
-
}
|
package/lib/src/dateTime.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types';
|
|
2
|
-
export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
3
|
-
locale: string;
|
|
4
|
-
timeZone?: string;
|
|
5
|
-
formats: CustomFormats;
|
|
6
|
-
onError: OnErrorFn;
|
|
7
|
-
}, type: 'date' | 'time' | 'dateTimeRange', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
8
|
-
export declare function formatDate(config: {
|
|
9
|
-
locale: string;
|
|
10
|
-
timeZone?: string;
|
|
11
|
-
formats: CustomFormats;
|
|
12
|
-
onError: OnErrorFn;
|
|
13
|
-
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): string;
|
|
14
|
-
export declare function formatTime(config: {
|
|
15
|
-
locale: string;
|
|
16
|
-
timeZone?: string;
|
|
17
|
-
formats: CustomFormats;
|
|
18
|
-
onError: OnErrorFn;
|
|
19
|
-
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTime']>): string;
|
|
20
|
-
export declare function formatDateTimeRange(config: {
|
|
21
|
-
locale: string;
|
|
22
|
-
timeZone?: string;
|
|
23
|
-
formats: CustomFormats;
|
|
24
|
-
onError: OnErrorFn;
|
|
25
|
-
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[from, to, options]: Parameters<IntlFormatters['formatDateTimeRange']>): string;
|
|
26
|
-
export declare function formatDateToParts(config: {
|
|
27
|
-
locale: string;
|
|
28
|
-
timeZone?: string;
|
|
29
|
-
formats: CustomFormats;
|
|
30
|
-
onError: OnErrorFn;
|
|
31
|
-
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): Intl.DateTimeFormatPart[];
|
|
32
|
-
export declare function formatTimeToParts(config: {
|
|
33
|
-
locale: string;
|
|
34
|
-
timeZone?: string;
|
|
35
|
-
formats: CustomFormats;
|
|
36
|
-
onError: OnErrorFn;
|
|
37
|
-
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTimeToParts']>): Intl.DateTimeFormatPart[];
|