use-intl 3.0.0-beta.2 → 3.0.0-rc.5
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/_IntlProvider.d.ts +1 -0
- package/_useLocale.d.ts +1 -0
- package/core.d.ts +1 -1
- package/dist/_IntlProvider.js +7 -0
- package/dist/_useLocale.js +7 -0
- package/dist/core.js +3 -4
- package/dist/development/IntlContext-b5cc6be8.js +8 -0
- package/dist/development/_IntlProvider.js +27 -0
- package/dist/development/_useLocale-321e619f.js +19 -0
- package/dist/development/_useLocale.js +11 -0
- package/dist/development/core.js +91 -0
- package/dist/development/createFormatter-bcc5a49f.js +430 -0
- package/dist/development/index.js +29 -0
- package/dist/development/initializeConfig-0a0b87a7.js +120 -0
- package/dist/development/react.js +145 -0
- package/dist/esm/IntlContext-381f3ce4.js +1 -0
- package/dist/esm/_IntlProvider.js +1 -0
- package/dist/esm/_useLocale-89b32eb9.js +1 -0
- package/dist/esm/_useLocale.js +1 -0
- package/dist/esm/core.js +1 -0
- package/dist/esm/createFormatter-cccbd794.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/initializeConfig-29e7ba4c.js +1 -0
- package/dist/esm/react.js +1 -0
- package/dist/index.js +3 -4
- package/dist/production/IntlContext-381f3ce4.js +1 -0
- package/dist/production/_IntlProvider.js +1 -0
- package/dist/production/_useLocale-8e23751a.js +1 -0
- package/dist/production/_useLocale.js +1 -0
- package/dist/production/core.js +1 -0
- package/dist/production/createFormatter-8c0b8eb2.js +1 -0
- package/dist/production/index.js +1 -0
- package/dist/production/initializeConfig-984a566d.js +1 -0
- package/dist/production/react.js +1 -0
- package/dist/react.js +3 -4
- package/dist/types/src/_IntlProvider.d.ts +1 -0
- package/dist/types/src/_useLocale.d.ts +1 -0
- package/dist/{core → types/src/core}/AbstractIntlMessages.d.ts +9 -9
- package/dist/{core → types/src/core}/DateTimeFormatOptions.d.ts +73 -73
- package/dist/{core → types/src/core}/Formats.d.ts +8 -8
- package/dist/{core → types/src/core}/IntlConfig.d.ts +54 -54
- package/dist/{core → types/src/core}/IntlError.d.ts +14 -13
- package/dist/{core → types/src/core}/MessageFormatCache.d.ts +5 -5
- package/dist/{core → types/src/core}/NumberFormatOptions.d.ts +2 -2
- package/dist/types/src/core/RelativeTimeFormatOptions.d.ts +5 -0
- package/dist/{core → types/src/core}/TimeZone.d.ts +2 -2
- package/dist/{core → types/src/core}/TranslationValues.d.ts +6 -6
- package/dist/{core → types/src/core}/convertFormatsToIntlMessageFormat.d.ts +11 -11
- package/dist/{core → types/src/core}/createBaseTranslator.d.ts +21 -21
- package/dist/{core → types/src/core}/createFormatter.d.ts +20 -19
- package/dist/{core → types/src/core}/createTranslator.d.ts +47 -47
- package/dist/{core → types/src/core}/createTranslatorImpl.d.ts +13 -13
- package/dist/{core → types/src/core}/defaults.d.ts +11 -11
- package/dist/{core → types/src/core}/index.d.ts +15 -16
- package/dist/{core → types/src/core}/initializeConfig.d.ts +14 -13
- package/dist/{core → types/src/core}/resolveNamespace.d.ts +5 -5
- package/dist/{core → types/src/core}/utils/MessageKeys.d.ts +5 -5
- package/dist/{core → types/src/core}/utils/NamespaceKeys.d.ts +5 -5
- package/dist/{core → types/src/core}/utils/NestedKeyOf.d.ts +4 -4
- package/dist/{core → types/src/core}/utils/NestedValueOf.d.ts +2 -2
- package/dist/{core → types/src/core}/validateMessages.d.ts +3 -3
- package/dist/{core.d.ts → types/src/core.d.ts} +1 -1
- package/dist/{index.d.ts → types/src/index.d.ts} +2 -2
- package/dist/{react → types/src/react}/IntlContext.d.ts +13 -13
- package/dist/{react → types/src/react}/IntlProvider.d.ts +7 -7
- package/dist/{react → types/src/react}/index.d.ts +7 -8
- package/dist/{react → types/src/react}/useFormatter.d.ts +6 -6
- package/dist/{react → types/src/react}/useIntlContext.d.ts +10 -10
- package/dist/{react → types/src/react}/useLocale.d.ts +1 -1
- package/dist/types/src/react/useMessages.d.ts +2 -0
- package/dist/{react → types/src/react}/useNow.d.ts +23 -23
- package/dist/{react → types/src/react}/useTimeZone.d.ts +1 -1
- package/dist/{react → types/src/react}/useTranslations.d.ts +44 -44
- package/dist/{react → types/src/react}/useTranslationsImpl.d.ts +8 -8
- package/dist/{react.d.ts → types/src/react.d.ts} +1 -1
- package/dist/types/test/core/createFormatter.test.d.ts +1 -0
- package/dist/types/test/core/createTranslator.test.d.ts +1 -0
- package/dist/types/test/react/useFormatter.test.d.ts +1 -0
- package/dist/types/test/react/useLocale.test.d.ts +1 -0
- package/dist/types/test/react/useMessages.test.d.ts +1 -0
- package/dist/types/test/react/useNow.test.d.ts +1 -0
- package/dist/types/test/react/useTimeZone.test.d.ts +1 -0
- package/dist/types/test/react/useTranslations.test.d.ts +1 -0
- package/dist/{test → types/test}/setup.d.ts +1 -1
- package/package.json +33 -21
- package/react.d.ts +1 -1
- package/dist/_virtual/_rollupPluginBabelHelpers.esm.js +0 -102
- package/dist/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
- package/dist/_virtual/use-intl.esm.js +0 -102
- package/dist/_virtual/use-intl.esm.js.map +0 -1
- package/dist/core/IntlError.esm.js +0 -33
- package/dist/core/IntlError.esm.js.map +0 -1
- package/dist/core/convertFormatsToIntlMessageFormat.esm.js +0 -37
- package/dist/core/convertFormatsToIntlMessageFormat.esm.js.map +0 -1
- package/dist/core/createBaseTranslator.esm.js +0 -210
- package/dist/core/createBaseTranslator.esm.js.map +0 -1
- package/dist/core/createFormatter.esm.js +0 -138
- package/dist/core/createFormatter.esm.js.map +0 -1
- package/dist/core/createIntl.d.ts +0 -7
- package/dist/core/createIntl.esm.js +0 -14
- package/dist/core/createIntl.esm.js.map +0 -1
- package/dist/core/createTranslator.esm.js +0 -37
- package/dist/core/createTranslator.esm.js.map +0 -1
- package/dist/core/createTranslatorImpl.esm.js +0 -50
- package/dist/core/createTranslatorImpl.esm.js.map +0 -1
- package/dist/core/defaults.esm.js +0 -15
- package/dist/core/defaults.esm.js.map +0 -1
- package/dist/core/initializeConfig.esm.js +0 -29
- package/dist/core/initializeConfig.esm.js.map +0 -1
- package/dist/core/resolveNamespace.esm.js +0 -10
- package/dist/core/resolveNamespace.esm.js.map +0 -1
- package/dist/core/use-intl.esm.js +0 -33
- package/dist/core/use-intl.esm.js.map +0 -1
- package/dist/core/use-intl.esm10.js +0 -28
- package/dist/core/use-intl.esm10.js.map +0 -1
- package/dist/core/use-intl.esm11.js +0 -10
- package/dist/core/use-intl.esm11.js.map +0 -1
- package/dist/core/use-intl.esm2.js +0 -210
- package/dist/core/use-intl.esm2.js.map +0 -1
- package/dist/core/use-intl.esm3.js +0 -37
- package/dist/core/use-intl.esm3.js.map +0 -1
- package/dist/core/use-intl.esm4.js +0 -14
- package/dist/core/use-intl.esm4.js.map +0 -1
- package/dist/core/use-intl.esm5.js +0 -29
- package/dist/core/use-intl.esm5.js.map +0 -1
- package/dist/core/use-intl.esm6.js +0 -138
- package/dist/core/use-intl.esm6.js.map +0 -1
- package/dist/core/use-intl.esm7.js +0 -37
- package/dist/core/use-intl.esm7.js.map +0 -1
- package/dist/core/use-intl.esm8.js +0 -15
- package/dist/core/use-intl.esm8.js.map +0 -1
- package/dist/core/use-intl.esm9.js +0 -50
- package/dist/core/use-intl.esm9.js.map +0 -1
- package/dist/core/validateMessages.esm.js +0 -28
- package/dist/core/validateMessages.esm.js.map +0 -1
- package/dist/core.cjs.development.js +0 -93
- package/dist/core.cjs.development.js.map +0 -1
- package/dist/core.cjs.production.min.js +0 -2
- package/dist/core.cjs.production.min.js.map +0 -1
- package/dist/core.esm.js +0 -7
- package/dist/core.esm.js.map +0 -1
- package/dist/createIntl-34d4281e.js +0 -592
- package/dist/createIntl-34d4281e.js.map +0 -1
- package/dist/createIntl-bc325740.js +0 -2
- package/dist/createIntl-bc325740.js.map +0 -1
- package/dist/react/IntlContext.esm.js +0 -6
- package/dist/react/IntlContext.esm.js.map +0 -1
- package/dist/react/IntlProvider.esm.js +0 -22
- package/dist/react/IntlProvider.esm.js.map +0 -1
- package/dist/react/getInitializedConfig.d.ts +0 -1
- package/dist/react/use-intl.esm.js +0 -26
- package/dist/react/use-intl.esm.js.map +0 -1
- package/dist/react/use-intl.esm10.js +0 -13
- package/dist/react/use-intl.esm10.js.map +0 -1
- package/dist/react/use-intl.esm11.js +0 -6
- package/dist/react/use-intl.esm11.js.map +0 -1
- package/dist/react/use-intl.esm2.js +0 -8
- package/dist/react/use-intl.esm2.js.map +0 -1
- package/dist/react/use-intl.esm3.js +0 -30
- package/dist/react/use-intl.esm3.js.map +0 -1
- package/dist/react/use-intl.esm4.js +0 -22
- package/dist/react/use-intl.esm4.js.map +0 -1
- package/dist/react/use-intl.esm5.js +0 -24
- package/dist/react/use-intl.esm5.js.map +0 -1
- package/dist/react/use-intl.esm6.js +0 -45
- package/dist/react/use-intl.esm6.js.map +0 -1
- package/dist/react/use-intl.esm7.js +0 -8
- package/dist/react/use-intl.esm7.js.map +0 -1
- package/dist/react/use-intl.esm8.js +0 -8
- package/dist/react/use-intl.esm8.js.map +0 -1
- package/dist/react/use-intl.esm9.js +0 -36
- package/dist/react/use-intl.esm9.js.map +0 -1
- package/dist/react/useFormatter.esm.js +0 -24
- package/dist/react/useFormatter.esm.js.map +0 -1
- package/dist/react/useIntl.d.ts +0 -6
- package/dist/react/useIntl.esm.js +0 -30
- package/dist/react/useIntl.esm.js.map +0 -1
- package/dist/react/useIntlContext.esm.js +0 -13
- package/dist/react/useIntlContext.esm.js.map +0 -1
- package/dist/react/useLocale.esm.js +0 -8
- package/dist/react/useLocale.esm.js.map +0 -1
- package/dist/react/useMessages.d.ts +0 -1
- package/dist/react/useMessages.esm.js +0 -8
- package/dist/react/useMessages.esm.js.map +0 -1
- package/dist/react/useNow.esm.js +0 -45
- package/dist/react/useNow.esm.js.map +0 -1
- package/dist/react/useTimeZone.esm.js +0 -8
- package/dist/react/useTimeZone.esm.js.map +0 -1
- package/dist/react/useTranslations.esm.js +0 -26
- package/dist/react/useTranslations.esm.js.map +0 -1
- package/dist/react/useTranslationsImpl.esm.js +0 -36
- package/dist/react/useTranslationsImpl.esm.js.map +0 -1
- package/dist/react.cjs.development.js +0 -190
- package/dist/react.cjs.development.js.map +0 -1
- package/dist/react.cjs.production.min.js +0 -2
- package/dist/react.cjs.production.min.js.map +0 -1
- package/dist/react.esm.js +0 -9
- package/dist/react.esm.js.map +0 -1
- package/dist/use-intl.cjs.development.js +0 -842
- package/dist/use-intl.cjs.development.js.map +0 -1
- package/dist/use-intl.cjs.production.min.js +0 -2
- package/dist/use-intl.cjs.production.min.js.map +0 -1
- package/dist/use-intl.esm.js +0 -15
- package/dist/use-intl.esm.js.map +0 -1
|
@@ -1,842 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var IntlMessageFormat = require('intl-messageformat');
|
|
6
|
-
var React = require('react');
|
|
7
|
-
|
|
8
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
-
|
|
10
|
-
var IntlMessageFormat__default = /*#__PURE__*/_interopDefaultLegacy(IntlMessageFormat);
|
|
11
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
12
|
-
|
|
13
|
-
function _extends() {
|
|
14
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
15
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
16
|
-
var source = arguments[i];
|
|
17
|
-
for (var key in source) {
|
|
18
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
19
|
-
target[key] = source[key];
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return target;
|
|
24
|
-
};
|
|
25
|
-
return _extends.apply(this, arguments);
|
|
26
|
-
}
|
|
27
|
-
function _inheritsLoose(subClass, superClass) {
|
|
28
|
-
subClass.prototype = Object.create(superClass.prototype);
|
|
29
|
-
subClass.prototype.constructor = subClass;
|
|
30
|
-
_setPrototypeOf(subClass, superClass);
|
|
31
|
-
}
|
|
32
|
-
function _getPrototypeOf(o) {
|
|
33
|
-
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
|
|
34
|
-
return o.__proto__ || Object.getPrototypeOf(o);
|
|
35
|
-
};
|
|
36
|
-
return _getPrototypeOf(o);
|
|
37
|
-
}
|
|
38
|
-
function _setPrototypeOf(o, p) {
|
|
39
|
-
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
40
|
-
o.__proto__ = p;
|
|
41
|
-
return o;
|
|
42
|
-
};
|
|
43
|
-
return _setPrototypeOf(o, p);
|
|
44
|
-
}
|
|
45
|
-
function _isNativeReflectConstruct() {
|
|
46
|
-
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
47
|
-
if (Reflect.construct.sham) return false;
|
|
48
|
-
if (typeof Proxy === "function") return true;
|
|
49
|
-
try {
|
|
50
|
-
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
51
|
-
return true;
|
|
52
|
-
} catch (e) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function _construct(Parent, args, Class) {
|
|
57
|
-
if (_isNativeReflectConstruct()) {
|
|
58
|
-
_construct = Reflect.construct.bind();
|
|
59
|
-
} else {
|
|
60
|
-
_construct = function _construct(Parent, args, Class) {
|
|
61
|
-
var a = [null];
|
|
62
|
-
a.push.apply(a, args);
|
|
63
|
-
var Constructor = Function.bind.apply(Parent, a);
|
|
64
|
-
var instance = new Constructor();
|
|
65
|
-
if (Class) _setPrototypeOf(instance, Class.prototype);
|
|
66
|
-
return instance;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
return _construct.apply(null, arguments);
|
|
70
|
-
}
|
|
71
|
-
function _isNativeFunction(fn) {
|
|
72
|
-
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
73
|
-
}
|
|
74
|
-
function _wrapNativeSuper(Class) {
|
|
75
|
-
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
76
|
-
_wrapNativeSuper = function _wrapNativeSuper(Class) {
|
|
77
|
-
if (Class === null || !_isNativeFunction(Class)) return Class;
|
|
78
|
-
if (typeof Class !== "function") {
|
|
79
|
-
throw new TypeError("Super expression must either be null or a function");
|
|
80
|
-
}
|
|
81
|
-
if (typeof _cache !== "undefined") {
|
|
82
|
-
if (_cache.has(Class)) return _cache.get(Class);
|
|
83
|
-
_cache.set(Class, Wrapper);
|
|
84
|
-
}
|
|
85
|
-
function Wrapper() {
|
|
86
|
-
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
|
87
|
-
}
|
|
88
|
-
Wrapper.prototype = Object.create(Class.prototype, {
|
|
89
|
-
constructor: {
|
|
90
|
-
value: Wrapper,
|
|
91
|
-
enumerable: false,
|
|
92
|
-
writable: true,
|
|
93
|
-
configurable: true
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
return _setPrototypeOf(Wrapper, Class);
|
|
97
|
-
};
|
|
98
|
-
return _wrapNativeSuper(Class);
|
|
99
|
-
}
|
|
100
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
101
|
-
if (source == null) return {};
|
|
102
|
-
var target = {};
|
|
103
|
-
var sourceKeys = Object.keys(source);
|
|
104
|
-
var key, i;
|
|
105
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
106
|
-
key = sourceKeys[i];
|
|
107
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
108
|
-
target[key] = source[key];
|
|
109
|
-
}
|
|
110
|
-
return target;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
exports.IntlErrorCode = void 0;
|
|
114
|
-
(function (IntlErrorCode) {
|
|
115
|
-
IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
|
|
116
|
-
IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
|
|
117
|
-
IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
|
|
118
|
-
IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
|
|
119
|
-
IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
|
|
120
|
-
IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
|
|
121
|
-
})(exports.IntlErrorCode || (exports.IntlErrorCode = {}));
|
|
122
|
-
var IntlError = /*#__PURE__*/function (_Error) {
|
|
123
|
-
_inheritsLoose(IntlError, _Error);
|
|
124
|
-
function IntlError(code, originalMessage) {
|
|
125
|
-
var _this;
|
|
126
|
-
var message = code;
|
|
127
|
-
if (originalMessage) {
|
|
128
|
-
message += ': ' + originalMessage;
|
|
129
|
-
}
|
|
130
|
-
_this = _Error.call(this, message) || this;
|
|
131
|
-
_this.code = void 0;
|
|
132
|
-
_this.originalMessage = void 0;
|
|
133
|
-
_this.code = code;
|
|
134
|
-
if (originalMessage) {
|
|
135
|
-
_this.originalMessage = originalMessage;
|
|
136
|
-
}
|
|
137
|
-
return _this;
|
|
138
|
-
}
|
|
139
|
-
return IntlError;
|
|
140
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
141
|
-
|
|
142
|
-
function setTimeZoneInFormats(formats, timeZone) {
|
|
143
|
-
if (!formats) return formats;
|
|
144
|
-
// The only way to set a time zone with `intl-messageformat` is to merge it into the formats
|
|
145
|
-
// https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15
|
|
146
|
-
return Object.keys(formats).reduce(function (acc, key) {
|
|
147
|
-
acc[key] = _extends({
|
|
148
|
-
timeZone: timeZone
|
|
149
|
-
}, formats[key]);
|
|
150
|
-
return acc;
|
|
151
|
-
}, {});
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* `intl-messageformat` uses separate keys for `date` and `time`, but there's
|
|
155
|
-
* only one native API: `Intl.DateTimeFormat`. Additionally you might want to
|
|
156
|
-
* include both a time and a date in a value, therefore the separation doesn't
|
|
157
|
-
* seem so useful. We offer a single `dateTime` namespace instead, but we have
|
|
158
|
-
* to convert the format before `intl-messageformat` can be used.
|
|
159
|
-
*/
|
|
160
|
-
function convertFormatsToIntlMessageFormat(formats, timeZone) {
|
|
161
|
-
var formatsWithTimeZone = timeZone ? _extends({}, formats, {
|
|
162
|
-
dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)
|
|
163
|
-
}) : formats;
|
|
164
|
-
var mfDateDefaults = IntlMessageFormat__default["default"].formats.date;
|
|
165
|
-
var defaultDateFormats = timeZone ? setTimeZoneInFormats(mfDateDefaults, timeZone) : mfDateDefaults;
|
|
166
|
-
var mfTimeDefaults = IntlMessageFormat__default["default"].formats.time;
|
|
167
|
-
var defaultTimeFormats = timeZone ? setTimeZoneInFormats(mfTimeDefaults, timeZone) : mfTimeDefaults;
|
|
168
|
-
return _extends({}, formatsWithTimeZone, {
|
|
169
|
-
date: _extends({}, defaultDateFormats, formatsWithTimeZone == null ? void 0 : formatsWithTimeZone.dateTime),
|
|
170
|
-
time: _extends({}, defaultTimeFormats, formatsWithTimeZone == null ? void 0 : formatsWithTimeZone.dateTime)
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Contains defaults that are used for all entry points into the core.
|
|
176
|
-
* See also `InitializedIntlConfiguration`.
|
|
177
|
-
*/
|
|
178
|
-
function defaultGetMessageFallback(props) {
|
|
179
|
-
return [props.namespace, props.key].filter(function (part) {
|
|
180
|
-
return part != null;
|
|
181
|
-
}).join('.');
|
|
182
|
-
}
|
|
183
|
-
function defaultOnError(error) {
|
|
184
|
-
console.error(error);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function resolvePath(messages, key, namespace) {
|
|
188
|
-
if (!messages) {
|
|
189
|
-
throw new Error("No messages available at `" + namespace + "`." );
|
|
190
|
-
}
|
|
191
|
-
var message = messages;
|
|
192
|
-
key.split('.').forEach(function (part) {
|
|
193
|
-
var next = message[part];
|
|
194
|
-
if (part == null || next == null) {
|
|
195
|
-
throw new Error("Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." );
|
|
196
|
-
}
|
|
197
|
-
message = next;
|
|
198
|
-
});
|
|
199
|
-
return message;
|
|
200
|
-
}
|
|
201
|
-
function prepareTranslationValues(values) {
|
|
202
|
-
if (Object.keys(values).length === 0) return undefined;
|
|
203
|
-
// Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
204
|
-
var transformedValues = {};
|
|
205
|
-
Object.keys(values).forEach(function (key) {
|
|
206
|
-
var index = 0;
|
|
207
|
-
var value = values[key];
|
|
208
|
-
var transformed;
|
|
209
|
-
if (typeof value === 'function') {
|
|
210
|
-
transformed = function transformed(chunks) {
|
|
211
|
-
var result = value(chunks);
|
|
212
|
-
return React.isValidElement(result) ? React.cloneElement(result, {
|
|
213
|
-
key: key + index++
|
|
214
|
-
}) : result;
|
|
215
|
-
};
|
|
216
|
-
} else {
|
|
217
|
-
transformed = value;
|
|
218
|
-
}
|
|
219
|
-
transformedValues[key] = transformed;
|
|
220
|
-
});
|
|
221
|
-
return transformedValues;
|
|
222
|
-
}
|
|
223
|
-
function getMessagesOrError(_ref) {
|
|
224
|
-
var messages = _ref.messages,
|
|
225
|
-
namespace = _ref.namespace,
|
|
226
|
-
_ref$onError = _ref.onError,
|
|
227
|
-
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
|
|
228
|
-
try {
|
|
229
|
-
if (!messages) {
|
|
230
|
-
throw new Error("development" !== 'production' ? "No messages were configured on the provider." : undefined);
|
|
231
|
-
}
|
|
232
|
-
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
233
|
-
if (!retrievedMessages) {
|
|
234
|
-
throw new Error("development" !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
235
|
-
}
|
|
236
|
-
return retrievedMessages;
|
|
237
|
-
} catch (error) {
|
|
238
|
-
var intlError = new IntlError(exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
239
|
-
onError(intlError);
|
|
240
|
-
return intlError;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
function getPlainMessage(candidate, values) {
|
|
244
|
-
if (values) return undefined;
|
|
245
|
-
var unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
|
|
246
|
-
// Placeholders can be in the message if there are default values,
|
|
247
|
-
// or if the user has forgotten to provide values. In the latter
|
|
248
|
-
// case we need to compile the message to receive an error.
|
|
249
|
-
var hasPlaceholders = /<|{/.test(unescapedMessage);
|
|
250
|
-
if (!hasPlaceholders) {
|
|
251
|
-
return unescapedMessage;
|
|
252
|
-
}
|
|
253
|
-
return undefined;
|
|
254
|
-
}
|
|
255
|
-
function createBaseTranslator(config) {
|
|
256
|
-
var messagesOrError = getMessagesOrError({
|
|
257
|
-
messages: config.messages,
|
|
258
|
-
namespace: config.namespace,
|
|
259
|
-
onError: config.onError
|
|
260
|
-
});
|
|
261
|
-
return createBaseTranslatorImpl(_extends({}, config, {
|
|
262
|
-
messagesOrError: messagesOrError
|
|
263
|
-
}));
|
|
264
|
-
}
|
|
265
|
-
function createBaseTranslatorImpl(_ref2) {
|
|
266
|
-
var defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
267
|
-
globalFormats = _ref2.formats,
|
|
268
|
-
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
269
|
-
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
270
|
-
locale = _ref2.locale,
|
|
271
|
-
messageFormatCache = _ref2.messageFormatCache,
|
|
272
|
-
messagesOrError = _ref2.messagesOrError,
|
|
273
|
-
namespace = _ref2.namespace,
|
|
274
|
-
onError = _ref2.onError,
|
|
275
|
-
timeZone = _ref2.timeZone;
|
|
276
|
-
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
277
|
-
var error = new IntlError(code, message);
|
|
278
|
-
onError(error);
|
|
279
|
-
return getMessageFallback({
|
|
280
|
-
error: error,
|
|
281
|
-
key: key,
|
|
282
|
-
namespace: namespace
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
function translateBaseFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
286
|
-
key, /** Key value pairs for values to interpolate into the message. */
|
|
287
|
-
values, /** Provide custom formats for numbers, dates and times. */
|
|
288
|
-
formats) {
|
|
289
|
-
if (messagesOrError instanceof IntlError) {
|
|
290
|
-
// We have already warned about this during render
|
|
291
|
-
return getMessageFallback({
|
|
292
|
-
error: messagesOrError,
|
|
293
|
-
key: key,
|
|
294
|
-
namespace: namespace
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
var messages = messagesOrError;
|
|
298
|
-
var message;
|
|
299
|
-
try {
|
|
300
|
-
message = resolvePath(messages, key, namespace);
|
|
301
|
-
} catch (error) {
|
|
302
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
303
|
-
}
|
|
304
|
-
function joinPath(parts) {
|
|
305
|
-
return parts.filter(function (part) {
|
|
306
|
-
return part != null;
|
|
307
|
-
}).join('.');
|
|
308
|
-
}
|
|
309
|
-
var cacheKey = joinPath([locale, namespace, key, String(message)]);
|
|
310
|
-
var messageFormat;
|
|
311
|
-
if (messageFormatCache != null && messageFormatCache.has(cacheKey)) {
|
|
312
|
-
messageFormat = messageFormatCache.get(cacheKey);
|
|
313
|
-
} else {
|
|
314
|
-
if (typeof message === 'object') {
|
|
315
|
-
var code, errorMessage;
|
|
316
|
-
if (Array.isArray(message)) {
|
|
317
|
-
code = exports.IntlErrorCode.INVALID_MESSAGE;
|
|
318
|
-
{
|
|
319
|
-
errorMessage = "Message at `" + joinPath([namespace, key]) + "` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages";
|
|
320
|
-
}
|
|
321
|
-
} else {
|
|
322
|
-
code = exports.IntlErrorCode.INSUFFICIENT_PATH;
|
|
323
|
-
{
|
|
324
|
-
errorMessage = "Message at `" + joinPath([namespace, key]) + "` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages";
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
328
|
-
}
|
|
329
|
-
// Hot path that avoids creating an `IntlMessageFormat` instance
|
|
330
|
-
var plainMessage = getPlainMessage(message, values);
|
|
331
|
-
if (plainMessage) return plainMessage;
|
|
332
|
-
try {
|
|
333
|
-
messageFormat = new IntlMessageFormat__default["default"](message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
334
|
-
} catch (error) {
|
|
335
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
336
|
-
}
|
|
337
|
-
messageFormatCache == null ? void 0 : messageFormatCache.set(cacheKey, messageFormat);
|
|
338
|
-
}
|
|
339
|
-
try {
|
|
340
|
-
var formattedMessage = messageFormat.format(
|
|
341
|
-
// @ts-ignore `intl-messageformat` expects a different format
|
|
342
|
-
// for rich text elements since a recent minor update. This
|
|
343
|
-
// needs to be evaluated in detail, possibly also in regards
|
|
344
|
-
// to be able to format to parts.
|
|
345
|
-
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
346
|
-
if (formattedMessage == null) {
|
|
347
|
-
throw new Error("development" !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
348
|
-
}
|
|
349
|
-
// Limit the function signature to return strings or React elements
|
|
350
|
-
return React.isValidElement(formattedMessage) ||
|
|
351
|
-
// Arrays of React elements
|
|
352
|
-
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
353
|
-
} catch (error) {
|
|
354
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
function translateFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
358
|
-
key, /** Key value pairs for values to interpolate into the message. */
|
|
359
|
-
values, /** Provide custom formats for numbers, dates and times. */
|
|
360
|
-
formats) {
|
|
361
|
-
var result = translateBaseFn(key, values, formats);
|
|
362
|
-
if (typeof result !== 'string') {
|
|
363
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, "The message `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') + " didn't resolve to a string. If you want to format rich text, use `t.rich` instead." );
|
|
364
|
-
}
|
|
365
|
-
return result;
|
|
366
|
-
}
|
|
367
|
-
translateFn.rich = translateBaseFn;
|
|
368
|
-
translateFn.raw = function ( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
369
|
-
key) {
|
|
370
|
-
if (messagesOrError instanceof IntlError) {
|
|
371
|
-
// We have already warned about this during render
|
|
372
|
-
return getMessageFallback({
|
|
373
|
-
error: messagesOrError,
|
|
374
|
-
key: key,
|
|
375
|
-
namespace: namespace
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
var messages = messagesOrError;
|
|
379
|
-
try {
|
|
380
|
-
return resolvePath(messages, key, namespace);
|
|
381
|
-
} catch (error) {
|
|
382
|
-
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
return translateFn;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* For the strictly typed messages to work we have to wrap the namespace into
|
|
390
|
-
* a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
|
|
391
|
-
*/
|
|
392
|
-
function resolveNamespace(namespace, namespacePrefix) {
|
|
393
|
-
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
var _excluded$3 = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
397
|
-
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
398
|
-
var getMessageFallback = _ref.getMessageFallback,
|
|
399
|
-
messages = _ref.messages,
|
|
400
|
-
namespace = _ref.namespace,
|
|
401
|
-
onError = _ref.onError,
|
|
402
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$3);
|
|
403
|
-
// The `namespacePrefix` is part of the type system.
|
|
404
|
-
// See the comment in the function invocation.
|
|
405
|
-
messages = messages[namespacePrefix];
|
|
406
|
-
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
407
|
-
var translator = createBaseTranslator(_extends({}, rest, {
|
|
408
|
-
onError: onError,
|
|
409
|
-
getMessageFallback: getMessageFallback,
|
|
410
|
-
messages: messages,
|
|
411
|
-
namespace: namespace
|
|
412
|
-
}));
|
|
413
|
-
var originalRich = translator.rich;
|
|
414
|
-
function base() {
|
|
415
|
-
return translator.apply(void 0, arguments);
|
|
416
|
-
}
|
|
417
|
-
// Augment `t.rich` to return plain strings
|
|
418
|
-
base.rich = function (key, /** Key value pairs for values to interpolate into the message. */
|
|
419
|
-
values, formats) {
|
|
420
|
-
// `chunks` is returned as a string when no React element
|
|
421
|
-
// is used, therefore it's safe to cast this type.
|
|
422
|
-
var result = originalRich(key, values, formats);
|
|
423
|
-
// When only string chunks are provided to the parser, only strings should be returned here.
|
|
424
|
-
if (typeof result !== 'string') {
|
|
425
|
-
var error = new IntlError(exports.IntlErrorCode.FORMATTING_ERROR, "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" );
|
|
426
|
-
onError(error);
|
|
427
|
-
return getMessageFallback({
|
|
428
|
-
error: error,
|
|
429
|
-
key: key,
|
|
430
|
-
namespace: namespace
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
return result;
|
|
434
|
-
};
|
|
435
|
-
base.raw = translator.raw;
|
|
436
|
-
return base;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
var _excluded$2 = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
440
|
-
/**
|
|
441
|
-
* Translates messages from the given namespace by using the ICU syntax.
|
|
442
|
-
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
443
|
-
*
|
|
444
|
-
* If no namespace is provided, all available messages are returned.
|
|
445
|
-
* The namespace can also indicate nesting by using a dot
|
|
446
|
-
* (e.g. `namespace.Component`).
|
|
447
|
-
*/
|
|
448
|
-
function createTranslator(_ref) {
|
|
449
|
-
var _ref$getMessageFallba = _ref.getMessageFallback,
|
|
450
|
-
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
451
|
-
messages = _ref.messages,
|
|
452
|
-
namespace = _ref.namespace,
|
|
453
|
-
_ref$onError = _ref.onError,
|
|
454
|
-
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
455
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
|
|
456
|
-
// We have to wrap the actual function so the type inference for the optional
|
|
457
|
-
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
458
|
-
// The prefix ("!") is arbitrary.
|
|
459
|
-
return createTranslatorImpl(_extends({}, rest, {
|
|
460
|
-
onError: onError,
|
|
461
|
-
getMessageFallback: getMessageFallback,
|
|
462
|
-
messages: {
|
|
463
|
-
'!': messages
|
|
464
|
-
},
|
|
465
|
-
// @ts-ignore
|
|
466
|
-
namespace: namespace ? "!." + namespace : '!'
|
|
467
|
-
}), '!');
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
var MINUTE = 60;
|
|
471
|
-
var HOUR = MINUTE * 60;
|
|
472
|
-
var DAY = HOUR * 24;
|
|
473
|
-
var WEEK = DAY * 7;
|
|
474
|
-
var MONTH = DAY * (365 / 12); // Approximation
|
|
475
|
-
var YEAR = DAY * 365;
|
|
476
|
-
function getRelativeTimeFormatConfig(seconds) {
|
|
477
|
-
var absValue = Math.abs(seconds);
|
|
478
|
-
var value, unit;
|
|
479
|
-
// We have to round the resulting values, as `Intl.RelativeTimeFormat`
|
|
480
|
-
// will include fractions like '2.1 hours ago'.
|
|
481
|
-
if (absValue < MINUTE) {
|
|
482
|
-
unit = 'second';
|
|
483
|
-
value = Math.round(seconds);
|
|
484
|
-
} else if (absValue < HOUR) {
|
|
485
|
-
unit = 'minute';
|
|
486
|
-
value = Math.round(seconds / MINUTE);
|
|
487
|
-
} else if (absValue < DAY) {
|
|
488
|
-
unit = 'hour';
|
|
489
|
-
value = Math.round(seconds / HOUR);
|
|
490
|
-
} else if (absValue < WEEK) {
|
|
491
|
-
unit = 'day';
|
|
492
|
-
value = Math.round(seconds / DAY);
|
|
493
|
-
} else if (absValue < MONTH) {
|
|
494
|
-
unit = 'week';
|
|
495
|
-
value = Math.round(seconds / WEEK);
|
|
496
|
-
} else if (absValue < YEAR) {
|
|
497
|
-
unit = 'month';
|
|
498
|
-
value = Math.round(seconds / MONTH);
|
|
499
|
-
} else {
|
|
500
|
-
unit = 'year';
|
|
501
|
-
value = Math.round(seconds / YEAR);
|
|
502
|
-
}
|
|
503
|
-
return {
|
|
504
|
-
value: value,
|
|
505
|
-
unit: unit
|
|
506
|
-
};
|
|
507
|
-
}
|
|
508
|
-
function createFormatter(_ref) {
|
|
509
|
-
var formats = _ref.formats,
|
|
510
|
-
locale = _ref.locale,
|
|
511
|
-
globalNow = _ref.now,
|
|
512
|
-
_ref$onError = _ref.onError,
|
|
513
|
-
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
514
|
-
timeZone = _ref.timeZone;
|
|
515
|
-
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
|
|
516
|
-
var options;
|
|
517
|
-
if (typeof formatOrOptions === 'string') {
|
|
518
|
-
var formatName = formatOrOptions;
|
|
519
|
-
options = typeFormats == null ? void 0 : typeFormats[formatName];
|
|
520
|
-
if (!options) {
|
|
521
|
-
var error = new IntlError(exports.IntlErrorCode.MISSING_FORMAT, "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." );
|
|
522
|
-
onError(error);
|
|
523
|
-
throw error;
|
|
524
|
-
}
|
|
525
|
-
} else {
|
|
526
|
-
options = formatOrOptions;
|
|
527
|
-
}
|
|
528
|
-
return options;
|
|
529
|
-
}
|
|
530
|
-
function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
|
|
531
|
-
var options;
|
|
532
|
-
try {
|
|
533
|
-
options = resolveFormatOrOptions(typeFormats, formatOrOptions);
|
|
534
|
-
} catch (error) {
|
|
535
|
-
return String(value);
|
|
536
|
-
}
|
|
537
|
-
try {
|
|
538
|
-
return formatter(options);
|
|
539
|
-
} catch (error) {
|
|
540
|
-
onError(new IntlError(exports.IntlErrorCode.FORMATTING_ERROR, error.message));
|
|
541
|
-
return String(value);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
function dateTime( /** If a number is supplied, this is interpreted as a UTC timestamp. */
|
|
545
|
-
value,
|
|
546
|
-
/** If a time zone is supplied, the `value` is converted to that time zone.
|
|
547
|
-
* Otherwise the user time zone will be used. */
|
|
548
|
-
formatOrOptions) {
|
|
549
|
-
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
|
|
550
|
-
var _options;
|
|
551
|
-
if (timeZone && !((_options = options) != null && _options.timeZone)) {
|
|
552
|
-
options = _extends({}, options, {
|
|
553
|
-
timeZone: timeZone
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
return new Intl.DateTimeFormat(locale, options).format(value);
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
function number(value, formatOrOptions) {
|
|
560
|
-
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
|
|
561
|
-
return new Intl.NumberFormat(locale, options).format(value);
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
function relativeTime( /** The date time that needs to be formatted. */
|
|
565
|
-
date, /** The reference point in time to which `date` will be formatted in relation to. */
|
|
566
|
-
now) {
|
|
567
|
-
try {
|
|
568
|
-
if (!now) {
|
|
569
|
-
if (globalNow) {
|
|
570
|
-
now = globalNow;
|
|
571
|
-
} else {
|
|
572
|
-
throw new Error("development" !== 'production' ? "The `now` parameter wasn't provided and there was no global fallback configured on the provider." : undefined);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
var dateDate = date instanceof Date ? date : new Date(date);
|
|
576
|
-
var nowDate = now instanceof Date ? now : new Date(now);
|
|
577
|
-
var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
|
|
578
|
-
var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
|
|
579
|
-
unit = _getRelativeTimeForma.unit,
|
|
580
|
-
value = _getRelativeTimeForma.value;
|
|
581
|
-
return new Intl.RelativeTimeFormat(locale, {
|
|
582
|
-
numeric: 'auto'
|
|
583
|
-
}).format(value, unit);
|
|
584
|
-
} catch (error) {
|
|
585
|
-
onError(new IntlError(exports.IntlErrorCode.FORMATTING_ERROR, error.message));
|
|
586
|
-
return String(date);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
function list(value, formatOrOptions) {
|
|
590
|
-
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.list, function (options) {
|
|
591
|
-
return new Intl.ListFormat(locale, options).format(value);
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
|
-
return {
|
|
595
|
-
dateTime: dateTime,
|
|
596
|
-
number: number,
|
|
597
|
-
relativeTime: relativeTime,
|
|
598
|
-
list: list
|
|
599
|
-
};
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
603
|
-
Object.entries(messages).forEach(function (_ref) {
|
|
604
|
-
var key = _ref[0],
|
|
605
|
-
messageOrMessages = _ref[1];
|
|
606
|
-
if (key.includes('.')) {
|
|
607
|
-
var keyLabel = key;
|
|
608
|
-
if (parentPath) keyLabel += " (at " + parentPath + ")";
|
|
609
|
-
invalidKeyLabels.push(keyLabel);
|
|
610
|
-
}
|
|
611
|
-
if (messageOrMessages != null && typeof messageOrMessages === 'object') {
|
|
612
|
-
validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
|
|
613
|
-
return part != null;
|
|
614
|
-
}).join('.'));
|
|
615
|
-
}
|
|
616
|
-
});
|
|
617
|
-
}
|
|
618
|
-
function validateMessages(messages, onError) {
|
|
619
|
-
var invalidKeyLabels = [];
|
|
620
|
-
validateMessagesSegment(messages, invalidKeyLabels);
|
|
621
|
-
if (invalidKeyLabels.length > 0) {
|
|
622
|
-
onError(new IntlError(exports.IntlErrorCode.INVALID_KEY, "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ') + "\n\nIf you're migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from \"lodash\";\n\nconst input = {\n \"one.one\": \"1.1\",\n \"one.two\": \"1.2\",\n \"two.one.one\": \"2.1.1\"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// \"one\": {\n// \"one\": \"1.1\",\n// \"two\": \"1.2\"\n// },\n// \"two\": {\n// \"one\": {\n// \"one\": \"2.1.1\"\n// }\n// }\n// }\n" ));
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
var _excluded$1 = ["getMessageFallback", "messages", "onError"];
|
|
627
|
-
/**
|
|
628
|
-
* Enhances the incoming props with defaults.
|
|
629
|
-
*/
|
|
630
|
-
function initializeConfig(_ref) {
|
|
631
|
-
var getMessageFallback = _ref.getMessageFallback,
|
|
632
|
-
messages = _ref.messages,
|
|
633
|
-
onError = _ref.onError,
|
|
634
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
635
|
-
var finalOnError = onError || defaultOnError;
|
|
636
|
-
var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
|
|
637
|
-
{
|
|
638
|
-
if (messages) {
|
|
639
|
-
validateMessages(messages, finalOnError);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
return _extends({}, rest, {
|
|
643
|
-
messages: messages,
|
|
644
|
-
onError: finalOnError,
|
|
645
|
-
getMessageFallback: finalGetMessageFallback
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
/** @deprecated Switch to `createFormatter` */
|
|
650
|
-
function createIntl() {
|
|
651
|
-
var formatter = createFormatter.apply(void 0, arguments);
|
|
652
|
-
return {
|
|
653
|
-
formatDateTime: formatter.dateTime,
|
|
654
|
-
formatNumber: formatter.number,
|
|
655
|
-
formatRelativeTime: formatter.relativeTime
|
|
656
|
-
};
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
var IntlContext = /*#__PURE__*/React.createContext(undefined);
|
|
660
|
-
|
|
661
|
-
var _excluded = ["children"];
|
|
662
|
-
function IntlProvider(_ref) {
|
|
663
|
-
var children = _ref.children,
|
|
664
|
-
config = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
665
|
-
var _useState = React.useState(function () {
|
|
666
|
-
return new Map();
|
|
667
|
-
}),
|
|
668
|
-
messageFormatCache = _useState[0];
|
|
669
|
-
return React__default["default"].createElement(IntlContext.Provider, {
|
|
670
|
-
value: _extends({}, initializeConfig(config), {
|
|
671
|
-
messageFormatCache: messageFormatCache
|
|
672
|
-
})
|
|
673
|
-
}, children);
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
function useIntlContext() {
|
|
677
|
-
var context = React.useContext(IntlContext);
|
|
678
|
-
if (!context) {
|
|
679
|
-
throw new Error('No intl context found. Have you configured the provider?' );
|
|
680
|
-
}
|
|
681
|
-
return context;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
685
|
-
var _useIntlContext = useIntlContext(),
|
|
686
|
-
defaultTranslationValues = _useIntlContext.defaultTranslationValues,
|
|
687
|
-
globalFormats = _useIntlContext.formats,
|
|
688
|
-
getMessageFallback = _useIntlContext.getMessageFallback,
|
|
689
|
-
locale = _useIntlContext.locale,
|
|
690
|
-
messageFormatCache = _useIntlContext.messageFormatCache,
|
|
691
|
-
onError = _useIntlContext.onError,
|
|
692
|
-
timeZone = _useIntlContext.timeZone;
|
|
693
|
-
// The `namespacePrefix` is part of the type system.
|
|
694
|
-
// See the comment in the hook invocation.
|
|
695
|
-
allMessages = allMessages[namespacePrefix];
|
|
696
|
-
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
697
|
-
var translate = React.useMemo(function () {
|
|
698
|
-
return createBaseTranslator({
|
|
699
|
-
messageFormatCache: messageFormatCache,
|
|
700
|
-
getMessageFallback: getMessageFallback,
|
|
701
|
-
messages: allMessages,
|
|
702
|
-
defaultTranslationValues: defaultTranslationValues,
|
|
703
|
-
namespace: namespace,
|
|
704
|
-
onError: onError,
|
|
705
|
-
formats: globalFormats,
|
|
706
|
-
locale: locale,
|
|
707
|
-
timeZone: timeZone
|
|
708
|
-
});
|
|
709
|
-
}, [messageFormatCache, getMessageFallback, allMessages, namespace, onError, defaultTranslationValues, globalFormats, locale, timeZone]);
|
|
710
|
-
return translate;
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
/**
|
|
714
|
-
* Translates messages from the given namespace by using the ICU syntax.
|
|
715
|
-
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
716
|
-
*
|
|
717
|
-
* If no namespace is provided, all available messages are returned.
|
|
718
|
-
* The namespace can also indicate nesting by using a dot
|
|
719
|
-
* (e.g. `namespace.Component`).
|
|
720
|
-
*/
|
|
721
|
-
function useTranslations(namespace) {
|
|
722
|
-
var context = useIntlContext();
|
|
723
|
-
var messages = context.messages;
|
|
724
|
-
// We have to wrap the actual hook so the type inference for the optional
|
|
725
|
-
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
726
|
-
// The prefix ("!") is arbitrary.
|
|
727
|
-
return useTranslationsImpl({
|
|
728
|
-
'!': messages
|
|
729
|
-
},
|
|
730
|
-
// @ts-ignore
|
|
731
|
-
namespace ? "!." + namespace : '!', '!');
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
function useLocale() {
|
|
735
|
-
return useIntlContext().locale;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
function getNow() {
|
|
739
|
-
return new Date();
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* Reading the current date via `new Date()` in components should be avoided, as
|
|
743
|
-
* it causes components to be impure and can lead to flaky tests. Instead, this
|
|
744
|
-
* hook can be used.
|
|
745
|
-
*
|
|
746
|
-
* By default, it returns the time when the component mounts. If `updateInterval`
|
|
747
|
-
* is specified, the value will be updated based on the interval.
|
|
748
|
-
*
|
|
749
|
-
* You can however also return a static value from this hook, if you
|
|
750
|
-
* configure the `now` parameter on the context provider. Note however,
|
|
751
|
-
* that if `updateInterval` is configured in this case, the component
|
|
752
|
-
* will initialize with the global value, but will afterwards update
|
|
753
|
-
* continuously based on the interval.
|
|
754
|
-
*
|
|
755
|
-
* For unit tests, this can be mocked to a constant value. For end-to-end
|
|
756
|
-
* testing, an environment parameter can be passed to the `now` parameter
|
|
757
|
-
* of the provider to mock this to a static value.
|
|
758
|
-
*/
|
|
759
|
-
function useNow(options) {
|
|
760
|
-
var updateInterval = options == null ? void 0 : options.updateInterval;
|
|
761
|
-
var _useIntlContext = useIntlContext(),
|
|
762
|
-
globalNow = _useIntlContext.now;
|
|
763
|
-
var _useState = React.useState(globalNow || getNow()),
|
|
764
|
-
now = _useState[0],
|
|
765
|
-
setNow = _useState[1];
|
|
766
|
-
React.useEffect(function () {
|
|
767
|
-
if (!updateInterval) return;
|
|
768
|
-
var intervalId = setInterval(function () {
|
|
769
|
-
setNow(getNow());
|
|
770
|
-
}, updateInterval);
|
|
771
|
-
return function () {
|
|
772
|
-
clearInterval(intervalId);
|
|
773
|
-
};
|
|
774
|
-
}, [globalNow, updateInterval]);
|
|
775
|
-
return now;
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
function useTimeZone() {
|
|
779
|
-
return useIntlContext().timeZone;
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
function useMessages() {
|
|
783
|
-
return useIntlContext().messages;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
function useFormatter() {
|
|
787
|
-
var _useIntlContext = useIntlContext(),
|
|
788
|
-
formats = _useIntlContext.formats,
|
|
789
|
-
locale = _useIntlContext.locale,
|
|
790
|
-
globalNow = _useIntlContext.now,
|
|
791
|
-
onError = _useIntlContext.onError,
|
|
792
|
-
timeZone = _useIntlContext.timeZone;
|
|
793
|
-
return React.useMemo(function () {
|
|
794
|
-
return createFormatter({
|
|
795
|
-
formats: formats,
|
|
796
|
-
locale: locale,
|
|
797
|
-
now: globalNow,
|
|
798
|
-
onError: onError,
|
|
799
|
-
timeZone: timeZone
|
|
800
|
-
});
|
|
801
|
-
}, [formats, globalNow, locale, onError, timeZone]);
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
var hasWarned = false;
|
|
805
|
-
/** @deprecated Switch to `useFormatter` instead. */
|
|
806
|
-
function useIntl() {
|
|
807
|
-
var _useIntlContext = useIntlContext(),
|
|
808
|
-
formats = _useIntlContext.formats,
|
|
809
|
-
locale = _useIntlContext.locale,
|
|
810
|
-
globalNow = _useIntlContext.now,
|
|
811
|
-
onError = _useIntlContext.onError,
|
|
812
|
-
timeZone = _useIntlContext.timeZone;
|
|
813
|
-
if (!hasWarned) {
|
|
814
|
-
hasWarned = true;
|
|
815
|
-
console.warn('`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.');
|
|
816
|
-
}
|
|
817
|
-
return React.useMemo(function () {
|
|
818
|
-
return createIntl({
|
|
819
|
-
formats: formats,
|
|
820
|
-
locale: locale,
|
|
821
|
-
now: globalNow,
|
|
822
|
-
onError: onError,
|
|
823
|
-
timeZone: timeZone
|
|
824
|
-
});
|
|
825
|
-
}, [formats, globalNow, locale, onError, timeZone]);
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
exports.IntlError = IntlError;
|
|
829
|
-
exports.IntlProvider = IntlProvider;
|
|
830
|
-
exports.createBaseTranslator = createBaseTranslator;
|
|
831
|
-
exports.createFormatter = createFormatter;
|
|
832
|
-
exports.createIntl = createIntl;
|
|
833
|
-
exports.createTranslator = createTranslator;
|
|
834
|
-
exports.initializeConfig = initializeConfig;
|
|
835
|
-
exports.useFormatter = useFormatter;
|
|
836
|
-
exports.useIntl = useIntl;
|
|
837
|
-
exports.useLocale = useLocale;
|
|
838
|
-
exports.useMessages = useMessages;
|
|
839
|
-
exports.useNow = useNow;
|
|
840
|
-
exports.useTimeZone = useTimeZone;
|
|
841
|
-
exports.useTranslations = useTranslations;
|
|
842
|
-
//# sourceMappingURL=use-intl.cjs.development.js.map
|