@common.js/use-intl 4.13.1
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/LICENSE +21 -0
- package/README.md +5 -0
- package/core.d.ts +2 -0
- package/dist/esm/development/core.js +174 -0
- package/dist/esm/development/format-message/format-only.js +314 -0
- package/dist/esm/development/format-message/index.js +238 -0
- package/dist/esm/development/formatters-r4aAmsMP.js +266 -0
- package/dist/esm/development/index.js +64 -0
- package/dist/esm/development/initializeConfig-CUsOI8u2.js +651 -0
- package/dist/esm/development/react.js +390 -0
- package/dist/esm/production/core.js +149 -0
- package/dist/esm/production/format-message/format-only.js +299 -0
- package/dist/esm/production/format-message/index.js +188 -0
- package/dist/esm/production/formatters-CJcico0N.js +244 -0
- package/dist/esm/production/index.js +64 -0
- package/dist/esm/production/initializeConfig-CHgjheii.js +399 -0
- package/dist/esm/production/react.js +270 -0
- package/dist/types/core/AbstractIntlMessages.d.ts +10 -0
- package/dist/types/core/AppConfig.d.ts +29 -0
- package/dist/types/core/DateTimeFormatOptions.d.ts +73 -0
- package/dist/types/core/Formats.d.ts +9 -0
- package/dist/types/core/ICUArgs.d.ts +3 -0
- package/dist/types/core/ICUTags.d.ts +2 -0
- package/dist/types/core/IntlConfig.d.ts +54 -0
- package/dist/types/core/IntlError.d.ts +6 -0
- package/dist/types/core/IntlErrorCode.d.ts +10 -0
- package/dist/types/core/MessageKeys.d.ts +10 -0
- package/dist/types/core/NumberFormatOptions.d.ts +3 -0
- package/dist/types/core/RelativeTimeFormatOptions.d.ts +7 -0
- package/dist/types/core/TimeZone.d.ts +2 -0
- package/dist/types/core/TranslationValues.d.ts +6 -0
- package/dist/types/core/convertFormatsToIntlMessageFormat.d.ts +11 -0
- package/dist/types/core/createBaseTranslator.d.ts +21 -0
- package/dist/types/core/createFormatter.d.ts +47 -0
- package/dist/types/core/createTranslator.d.ts +59 -0
- package/dist/types/core/createTranslatorImpl.d.ts +17 -0
- package/dist/types/core/defaults.d.ts +11 -0
- package/dist/types/core/format-message/compile-format.d.ts +12 -0
- package/dist/types/core/format-message/format-only.d.ts +13 -0
- package/dist/types/core/format-message/index.d.ts +5 -0
- package/dist/types/core/format-message/types.d.ts +15 -0
- package/dist/types/core/formatters.d.ts +25 -0
- package/dist/types/core/hasLocale.d.ts +7 -0
- package/dist/types/core/index.d.ts +22 -0
- package/dist/types/core/initializeConfig.d.ts +14 -0
- package/dist/types/core/joinPath.d.ts +1 -0
- package/dist/types/core/resolveNamespace.d.ts +5 -0
- package/dist/types/core/types.d.ts +6 -0
- package/dist/types/core/validateMessages.d.ts +3 -0
- package/dist/types/core.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/react/IntlContext.d.ts +8 -0
- package/dist/types/react/IntlProvider.d.ts +7 -0
- package/dist/types/react/index.d.ts +9 -0
- package/dist/types/react/useExtracted.d.ts +42 -0
- package/dist/types/react/useFormatter.d.ts +2 -0
- package/dist/types/react/useIntlContext.d.ts +2 -0
- package/dist/types/react/useLocale.d.ts +2 -0
- package/dist/types/react/useMessages.d.ts +2 -0
- package/dist/types/react/useNow.d.ts +8 -0
- package/dist/types/react/useTimeZone.d.ts +1 -0
- package/dist/types/react/useTranslations.d.ts +12 -0
- package/dist/types/react/useTranslationsImpl.d.ts +9 -0
- package/dist/types/react.d.ts +1 -0
- package/format-message/format-only.d.ts +2 -0
- package/format-message.d.ts +2 -0
- package/package.json +45 -0
- package/react.d.ts +2 -0
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
a: function() {
|
|
13
|
+
return createBaseTranslator;
|
|
14
|
+
},
|
|
15
|
+
b: function() {
|
|
16
|
+
return defaultOnError;
|
|
17
|
+
},
|
|
18
|
+
c: function() {
|
|
19
|
+
return createFormatter;
|
|
20
|
+
},
|
|
21
|
+
d: function() {
|
|
22
|
+
return defaultGetMessageFallback;
|
|
23
|
+
},
|
|
24
|
+
i: function() {
|
|
25
|
+
return initializeConfig;
|
|
26
|
+
},
|
|
27
|
+
r: function() {
|
|
28
|
+
return resolveNamespace;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
var _react = require("react");
|
|
32
|
+
var _formatMessage = /*#__PURE__*/ _interopRequireDefault(require("use-intl/format-message"));
|
|
33
|
+
var _formattersR4AAmsMPJs = require("./formatters-r4aAmsMP.js");
|
|
34
|
+
function _arrayLikeToArray(arr, len) {
|
|
35
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
36
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
37
|
+
return arr2;
|
|
38
|
+
}
|
|
39
|
+
function _arrayWithHoles(arr) {
|
|
40
|
+
if (Array.isArray(arr)) return arr;
|
|
41
|
+
}
|
|
42
|
+
function _defineProperty(obj, key, value) {
|
|
43
|
+
if (key in obj) {
|
|
44
|
+
Object.defineProperty(obj, key, {
|
|
45
|
+
value: value,
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true
|
|
49
|
+
});
|
|
50
|
+
} else {
|
|
51
|
+
obj[key] = value;
|
|
52
|
+
}
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
function _instanceof(left, right) {
|
|
56
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
57
|
+
return !!right[Symbol.hasInstance](left);
|
|
58
|
+
} else {
|
|
59
|
+
return left instanceof right;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function _interopRequireDefault(obj) {
|
|
63
|
+
return obj && obj.__esModule ? obj : {
|
|
64
|
+
default: obj
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function _iterableToArrayLimit(arr, i) {
|
|
68
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
69
|
+
if (_i == null) return;
|
|
70
|
+
var _arr = [];
|
|
71
|
+
var _n = true;
|
|
72
|
+
var _d = false;
|
|
73
|
+
var _s, _e;
|
|
74
|
+
try {
|
|
75
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
76
|
+
_arr.push(_s.value);
|
|
77
|
+
if (i && _arr.length === i) break;
|
|
78
|
+
}
|
|
79
|
+
} catch (err) {
|
|
80
|
+
_d = true;
|
|
81
|
+
_e = err;
|
|
82
|
+
} finally{
|
|
83
|
+
try {
|
|
84
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
85
|
+
} finally{
|
|
86
|
+
if (_d) throw _e;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return _arr;
|
|
90
|
+
}
|
|
91
|
+
function _nonIterableRest() {
|
|
92
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
93
|
+
}
|
|
94
|
+
function _objectSpread(target) {
|
|
95
|
+
for(var i = 1; i < arguments.length; i++){
|
|
96
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
97
|
+
var ownKeys = Object.keys(source);
|
|
98
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
99
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
100
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
ownKeys.forEach(function(key) {
|
|
104
|
+
_defineProperty(target, key, source[key]);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return target;
|
|
108
|
+
}
|
|
109
|
+
function ownKeys(object, enumerableOnly) {
|
|
110
|
+
var keys = Object.keys(object);
|
|
111
|
+
if (Object.getOwnPropertySymbols) {
|
|
112
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
113
|
+
if (enumerableOnly) {
|
|
114
|
+
symbols = symbols.filter(function(sym) {
|
|
115
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
keys.push.apply(keys, symbols);
|
|
119
|
+
}
|
|
120
|
+
return keys;
|
|
121
|
+
}
|
|
122
|
+
function _objectSpreadProps(target, source) {
|
|
123
|
+
source = source != null ? source : {};
|
|
124
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
125
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
126
|
+
} else {
|
|
127
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
128
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return target;
|
|
132
|
+
}
|
|
133
|
+
function _objectWithoutProperties(source, excluded) {
|
|
134
|
+
if (source == null) return {};
|
|
135
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
136
|
+
var key, i;
|
|
137
|
+
if (Object.getOwnPropertySymbols) {
|
|
138
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
139
|
+
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
140
|
+
key = sourceSymbolKeys[i];
|
|
141
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
142
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
143
|
+
target[key] = source[key];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return target;
|
|
147
|
+
}
|
|
148
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
149
|
+
if (source == null) return {};
|
|
150
|
+
var target = {};
|
|
151
|
+
var sourceKeys = Object.keys(source);
|
|
152
|
+
var key, i;
|
|
153
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
154
|
+
key = sourceKeys[i];
|
|
155
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
156
|
+
target[key] = source[key];
|
|
157
|
+
}
|
|
158
|
+
return target;
|
|
159
|
+
}
|
|
160
|
+
function _slicedToArray(arr, i) {
|
|
161
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
162
|
+
}
|
|
163
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
164
|
+
if (!o) return;
|
|
165
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
166
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
167
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
168
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
169
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
170
|
+
}
|
|
171
|
+
function joinPath() {
|
|
172
|
+
for(var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++){
|
|
173
|
+
parts[_key] = arguments[_key];
|
|
174
|
+
}
|
|
175
|
+
return parts.filter(Boolean).join(".");
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Contains defaults that are used for all entry points into the core.
|
|
179
|
+
* See also `InitializedIntlConfiguration`.
|
|
180
|
+
*/ function defaultGetMessageFallback(props) {
|
|
181
|
+
return joinPath(props.namespace, props.key);
|
|
182
|
+
}
|
|
183
|
+
function defaultOnError(error) {
|
|
184
|
+
console.error(error);
|
|
185
|
+
}
|
|
186
|
+
function prepareTranslationValues(values) {
|
|
187
|
+
// Related to https://github.com/formatjs/formatjs/issues/1467
|
|
188
|
+
var transformedValues = {};
|
|
189
|
+
Object.keys(values).forEach(function(key) {
|
|
190
|
+
var index = 0;
|
|
191
|
+
var value = values[key];
|
|
192
|
+
var transformed;
|
|
193
|
+
if (typeof value === "function") {
|
|
194
|
+
transformed = function(chunks) {
|
|
195
|
+
var result = value(chunks);
|
|
196
|
+
return /*#__PURE__*/ (0, _react.isValidElement)(result) ? /*#__PURE__*/ (0, _react.cloneElement)(result, {
|
|
197
|
+
key: key + index++
|
|
198
|
+
}) : result;
|
|
199
|
+
};
|
|
200
|
+
} else {
|
|
201
|
+
transformed = value;
|
|
202
|
+
}
|
|
203
|
+
transformedValues[key] = transformed;
|
|
204
|
+
});
|
|
205
|
+
return transformedValues;
|
|
206
|
+
}
|
|
207
|
+
function resolvePath(locale, messages, key, namespace) {
|
|
208
|
+
var fullKey = joinPath(namespace, key);
|
|
209
|
+
if (!messages) {
|
|
210
|
+
throw new Error("No messages available at `".concat(namespace, "`."));
|
|
211
|
+
}
|
|
212
|
+
var message = messages;
|
|
213
|
+
key.split(".").forEach(function(part) {
|
|
214
|
+
var next = message[part];
|
|
215
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
216
|
+
if (part == null || next == null) {
|
|
217
|
+
throw new Error("Could not resolve `".concat(fullKey, "` in messages for locale `").concat(locale, "`."));
|
|
218
|
+
}
|
|
219
|
+
message = next;
|
|
220
|
+
});
|
|
221
|
+
return message;
|
|
222
|
+
}
|
|
223
|
+
function getMessagesOrError(locale, messages, namespace) {
|
|
224
|
+
try {
|
|
225
|
+
if (!messages) {
|
|
226
|
+
throw new Error("No messages were configured.");
|
|
227
|
+
}
|
|
228
|
+
var retrievedMessages = namespace ? resolvePath(locale, messages, namespace) : messages;
|
|
229
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
230
|
+
if (!retrievedMessages) {
|
|
231
|
+
throw new Error("No messages for namespace `".concat(namespace, "` found."));
|
|
232
|
+
}
|
|
233
|
+
return retrievedMessages;
|
|
234
|
+
} catch (error) {
|
|
235
|
+
var intlError = new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.MISSING_MESSAGE, error.message);
|
|
236
|
+
return intlError;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function createBaseTranslator(config) {
|
|
240
|
+
var messagesOrError = getMessagesOrError(config.locale, config.messages, config.namespace);
|
|
241
|
+
return createBaseTranslatorImpl(_objectSpreadProps(_objectSpread({}, config), {
|
|
242
|
+
messagesOrError: messagesOrError
|
|
243
|
+
}));
|
|
244
|
+
}
|
|
245
|
+
function createBaseTranslatorImpl(param) {
|
|
246
|
+
var cache = param.cache, globalFormats = param.formats, formatters = param.formatters, _getMessageFallback = param.getMessageFallback, getMessageFallback = _getMessageFallback === void 0 ? defaultGetMessageFallback : _getMessageFallback, locale = param.locale, messagesOrError = param.messagesOrError, namespace = param.namespace, onError = param.onError, timeZone = param.timeZone;
|
|
247
|
+
var getFallbackFromErrorAndNotify = function getFallbackFromErrorAndNotify(key, code, message, fallback) {
|
|
248
|
+
var error = new _formattersR4AAmsMPJs.I(code, message);
|
|
249
|
+
onError(error);
|
|
250
|
+
return fallback !== null && fallback !== void 0 ? fallback : getMessageFallback({
|
|
251
|
+
error: error,
|
|
252
|
+
key: key,
|
|
253
|
+
namespace: namespace
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
var translateBaseFn = function translateBaseFn(/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */ key, /** Key value pairs for values to interpolate into the message. */ values, /** Provide custom formats for numbers, dates and times. */ formats, _fallback) {
|
|
257
|
+
var fallback = _fallback;
|
|
258
|
+
var message;
|
|
259
|
+
if (hasMessagesError) {
|
|
260
|
+
if (fallback) {
|
|
261
|
+
message = fallback;
|
|
262
|
+
} else {
|
|
263
|
+
onError(messagesOrError);
|
|
264
|
+
return getMessageFallback({
|
|
265
|
+
error: messagesOrError,
|
|
266
|
+
key: key,
|
|
267
|
+
namespace: namespace
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
var messages = messagesOrError;
|
|
272
|
+
try {
|
|
273
|
+
message = resolvePath(locale, messages, key, namespace);
|
|
274
|
+
} catch (error) {
|
|
275
|
+
if (fallback) {
|
|
276
|
+
message = fallback;
|
|
277
|
+
} else {
|
|
278
|
+
return getFallbackFromErrorAndNotify(key, _formattersR4AAmsMPJs.a.MISSING_MESSAGE, error.message, fallback);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
var messagePath = joinPath(namespace, key);
|
|
284
|
+
return (0, _formatMessage.default)(messagePath, // @ts-expect-error -- We have additional validation either in `compile-format.tsx` or in case of `format-only.tsx` in the loader
|
|
285
|
+
message, values ? prepareTranslationValues(values) : values, {
|
|
286
|
+
cache: cache,
|
|
287
|
+
formatters: formatters,
|
|
288
|
+
globalFormats: globalFormats,
|
|
289
|
+
formats: formats,
|
|
290
|
+
locale: locale,
|
|
291
|
+
timeZone: timeZone
|
|
292
|
+
});
|
|
293
|
+
} catch (error1) {
|
|
294
|
+
var errorCode, errorMessage;
|
|
295
|
+
if (_instanceof(error1, _formattersR4AAmsMPJs.I)) {
|
|
296
|
+
errorCode = error1.code;
|
|
297
|
+
errorMessage = error1.originalMessage;
|
|
298
|
+
} else {
|
|
299
|
+
errorCode = _formattersR4AAmsMPJs.a.FORMATTING_ERROR;
|
|
300
|
+
errorMessage = error1.message;
|
|
301
|
+
}
|
|
302
|
+
return getFallbackFromErrorAndNotify(key, errorCode, errorMessage, fallback);
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
var translateFn = function translateFn(/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */ key, /** Key value pairs for values to interpolate into the message. */ values, /** Custom formats for numbers, dates and times. */ formats, _fallback) {
|
|
306
|
+
var result = translateBaseFn(key, values, formats, _fallback);
|
|
307
|
+
if (typeof result !== "string") {
|
|
308
|
+
return getFallbackFromErrorAndNotify(key, _formattersR4AAmsMPJs.a.INVALID_MESSAGE, "The message `".concat(key, "` in ").concat(namespace ? "namespace `".concat(namespace, "`") : "messages", " didn't resolve to a string. If you want to format rich text, use `t.rich` instead."));
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
311
|
+
};
|
|
312
|
+
var hasMessagesError = _instanceof(messagesOrError, _formattersR4AAmsMPJs.I);
|
|
313
|
+
translateFn.rich = translateBaseFn;
|
|
314
|
+
// Augment `translateBaseFn` to return plain strings
|
|
315
|
+
translateFn.markup = function(key, values, formats, _fallback) {
|
|
316
|
+
var result = translateBaseFn(key, // @ts-expect-error -- `MarkupTranslationValues` is practically a sub type
|
|
317
|
+
// of `RichTranslationValues` but TypeScript isn't smart enough here.
|
|
318
|
+
values, formats, _fallback);
|
|
319
|
+
if (typeof result !== "string") {
|
|
320
|
+
var error = new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.FORMATTING_ERROR, "`t.markup` only accepts functions for formatting that receive and return strings.\n\nE.g. t.markup('markup', {b: (chunks) => `<b>${chunks}</b>`})");
|
|
321
|
+
onError(error);
|
|
322
|
+
return getMessageFallback({
|
|
323
|
+
error: error,
|
|
324
|
+
key: key,
|
|
325
|
+
namespace: namespace
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
};
|
|
330
|
+
translateFn.raw = function(key) {
|
|
331
|
+
{
|
|
332
|
+
if (!_formatMessage.default.raw) {
|
|
333
|
+
throw new Error("`t.raw` is not supported when messages are precompiled.");
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (hasMessagesError) {
|
|
337
|
+
onError(messagesOrError);
|
|
338
|
+
return getMessageFallback({
|
|
339
|
+
error: messagesOrError,
|
|
340
|
+
key: key,
|
|
341
|
+
namespace: namespace
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
var messages = messagesOrError;
|
|
345
|
+
try {
|
|
346
|
+
return resolvePath(locale, messages, key, namespace);
|
|
347
|
+
} catch (error) {
|
|
348
|
+
return getFallbackFromErrorAndNotify(key, _formattersR4AAmsMPJs.a.MISSING_MESSAGE, error.message);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
translateFn.has = function(key) {
|
|
352
|
+
if (hasMessagesError) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
try {
|
|
356
|
+
resolvePath(locale, messagesOrError, key, namespace);
|
|
357
|
+
return true;
|
|
358
|
+
} catch (e) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
return translateFn;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* For the strictly typed messages to work we have to wrap the namespace into
|
|
366
|
+
* a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
|
|
367
|
+
*/ function resolveNamespace(namespace, namespacePrefix) {
|
|
368
|
+
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + ".").length);
|
|
369
|
+
}
|
|
370
|
+
var SECOND = 1;
|
|
371
|
+
var MINUTE = SECOND * 60;
|
|
372
|
+
var HOUR = MINUTE * 60;
|
|
373
|
+
var DAY = HOUR * 24;
|
|
374
|
+
var WEEK = DAY * 7;
|
|
375
|
+
var MONTH = DAY * (365 / 12); // Approximation
|
|
376
|
+
var QUARTER = MONTH * 3;
|
|
377
|
+
var YEAR = DAY * 365;
|
|
378
|
+
var UNIT_SECONDS = {
|
|
379
|
+
second: SECOND,
|
|
380
|
+
seconds: SECOND,
|
|
381
|
+
minute: MINUTE,
|
|
382
|
+
minutes: MINUTE,
|
|
383
|
+
hour: HOUR,
|
|
384
|
+
hours: HOUR,
|
|
385
|
+
day: DAY,
|
|
386
|
+
days: DAY,
|
|
387
|
+
week: WEEK,
|
|
388
|
+
weeks: WEEK,
|
|
389
|
+
month: MONTH,
|
|
390
|
+
months: MONTH,
|
|
391
|
+
quarter: QUARTER,
|
|
392
|
+
quarters: QUARTER,
|
|
393
|
+
year: YEAR,
|
|
394
|
+
years: YEAR
|
|
395
|
+
};
|
|
396
|
+
function resolveRelativeTimeUnit(seconds) {
|
|
397
|
+
var absValue = Math.abs(seconds);
|
|
398
|
+
if (absValue < MINUTE) {
|
|
399
|
+
return "second";
|
|
400
|
+
} else if (absValue < HOUR) {
|
|
401
|
+
return "minute";
|
|
402
|
+
} else if (absValue < DAY) {
|
|
403
|
+
return "hour";
|
|
404
|
+
} else if (absValue < WEEK) {
|
|
405
|
+
return "day";
|
|
406
|
+
} else if (absValue < MONTH) {
|
|
407
|
+
return "week";
|
|
408
|
+
} else if (absValue < YEAR) {
|
|
409
|
+
return "month";
|
|
410
|
+
}
|
|
411
|
+
return "year";
|
|
412
|
+
}
|
|
413
|
+
function calculateRelativeTimeValue(seconds, unit) {
|
|
414
|
+
// We have to round the resulting values, as `Intl.RelativeTimeFormat`
|
|
415
|
+
// will include fractions like '2.1 hours ago'.
|
|
416
|
+
return Math.round(seconds / UNIT_SECONDS[unit]);
|
|
417
|
+
}
|
|
418
|
+
function createFormatter(props) {
|
|
419
|
+
var applyTimeZone = function applyTimeZone(options) {
|
|
420
|
+
if (!(options === null || options === void 0 ? void 0 : options.timeZone)) {
|
|
421
|
+
if (globalTimeZone) {
|
|
422
|
+
options = _objectSpreadProps(_objectSpread({}, options), {
|
|
423
|
+
timeZone: globalTimeZone
|
|
424
|
+
});
|
|
425
|
+
} else {
|
|
426
|
+
onError(new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.ENVIRONMENT_FALLBACK, "The `timeZone` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl.dev/docs/configuration#time-zone"));
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return options;
|
|
430
|
+
};
|
|
431
|
+
var resolveFormatOrOptions = function resolveFormatOrOptions(typeFormats, formatOrOptions, overrides) {
|
|
432
|
+
var options;
|
|
433
|
+
if (typeof formatOrOptions === "string") {
|
|
434
|
+
var formatName = formatOrOptions;
|
|
435
|
+
options = typeFormats === null || typeFormats === void 0 ? void 0 : typeFormats[formatName];
|
|
436
|
+
if (!options) {
|
|
437
|
+
var error = new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.MISSING_FORMAT, "Format `".concat(formatName, "` is not available."));
|
|
438
|
+
onError(error);
|
|
439
|
+
throw error;
|
|
440
|
+
}
|
|
441
|
+
} else {
|
|
442
|
+
options = formatOrOptions;
|
|
443
|
+
}
|
|
444
|
+
if (overrides) {
|
|
445
|
+
options = _objectSpread({}, options, overrides);
|
|
446
|
+
}
|
|
447
|
+
return options;
|
|
448
|
+
};
|
|
449
|
+
var getFormattedValue = function getFormattedValue(formatOrOptions, overrides, typeFormats, formatter, getFallback) {
|
|
450
|
+
var options;
|
|
451
|
+
try {
|
|
452
|
+
options = resolveFormatOrOptions(typeFormats, formatOrOptions, overrides);
|
|
453
|
+
} catch (e) {
|
|
454
|
+
return getFallback();
|
|
455
|
+
}
|
|
456
|
+
try {
|
|
457
|
+
return formatter(options);
|
|
458
|
+
} catch (error) {
|
|
459
|
+
onError(new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.FORMATTING_ERROR, error.message));
|
|
460
|
+
return getFallback();
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
var dateTime = function dateTime(value, formatOrOptions, overrides) {
|
|
464
|
+
return getFormattedValue(formatOrOptions, overrides, formats === null || formats === void 0 ? void 0 : formats.dateTime, function(options) {
|
|
465
|
+
options = applyTimeZone(options);
|
|
466
|
+
return formatters.getDateTimeFormat(locale, options).format(value);
|
|
467
|
+
}, function() {
|
|
468
|
+
return String(value);
|
|
469
|
+
});
|
|
470
|
+
};
|
|
471
|
+
var dateTimeRange = function dateTimeRange(start, end, formatOrOptions, overrides) {
|
|
472
|
+
return getFormattedValue(formatOrOptions, overrides, formats === null || formats === void 0 ? void 0 : formats.dateTime, function(options) {
|
|
473
|
+
options = applyTimeZone(options);
|
|
474
|
+
return formatters.getDateTimeFormat(locale, options).formatRange(start, end);
|
|
475
|
+
}, function() {
|
|
476
|
+
return [
|
|
477
|
+
dateTime(start),
|
|
478
|
+
dateTime(end)
|
|
479
|
+
].join(" – ");
|
|
480
|
+
});
|
|
481
|
+
};
|
|
482
|
+
var number = function number(value, formatOrOptions, overrides) {
|
|
483
|
+
return getFormattedValue(formatOrOptions, overrides, formats === null || formats === void 0 ? void 0 : formats.number, function(options) {
|
|
484
|
+
return formatters.getNumberFormat(locale, options).format(value);
|
|
485
|
+
}, function() {
|
|
486
|
+
return String(value);
|
|
487
|
+
});
|
|
488
|
+
};
|
|
489
|
+
var getGlobalNow = function getGlobalNow() {
|
|
490
|
+
// Only read when necessary to avoid triggering a `dynamicIO` error
|
|
491
|
+
// unnecessarily (`now` is only needed for `format.relativeTime`)
|
|
492
|
+
if (props.now) {
|
|
493
|
+
return props.now;
|
|
494
|
+
} else {
|
|
495
|
+
onError(new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.ENVIRONMENT_FALLBACK, "The `now` parameter wasn't provided to `relativeTime` and there is no global default configured, therefore the current time will be used as a fallback. See https://next-intl.dev/docs/usage/dates-times#relative-times-usenow"));
|
|
496
|
+
return new Date();
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
var relativeTime = function relativeTime(date, nowOrOptions) {
|
|
500
|
+
try {
|
|
501
|
+
var nowDate, unit;
|
|
502
|
+
var opts = {};
|
|
503
|
+
if (_instanceof(nowOrOptions, Date) || typeof nowOrOptions === "number") {
|
|
504
|
+
nowDate = new Date(nowOrOptions);
|
|
505
|
+
} else if (nowOrOptions) {
|
|
506
|
+
if (nowOrOptions.now != null) {
|
|
507
|
+
nowDate = new Date(nowOrOptions.now);
|
|
508
|
+
} else {
|
|
509
|
+
nowDate = getGlobalNow();
|
|
510
|
+
}
|
|
511
|
+
unit = nowOrOptions.unit;
|
|
512
|
+
opts.style = nowOrOptions.style;
|
|
513
|
+
// @ts-expect-error -- Types are slightly outdated
|
|
514
|
+
opts.numberingSystem = nowOrOptions.numberingSystem;
|
|
515
|
+
}
|
|
516
|
+
if (!nowDate) {
|
|
517
|
+
nowDate = getGlobalNow();
|
|
518
|
+
}
|
|
519
|
+
var dateDate = new Date(date);
|
|
520
|
+
var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
|
|
521
|
+
if (!unit) {
|
|
522
|
+
unit = resolveRelativeTimeUnit(seconds);
|
|
523
|
+
}
|
|
524
|
+
// `numeric: 'auto'` can theoretically produce output like "yesterday",
|
|
525
|
+
// but it only works with integers. E.g. -1 day will produce "yesterday",
|
|
526
|
+
// but -1.1 days will produce "-1.1 days". Rounding before formatting is
|
|
527
|
+
// not desired, as the given dates might cross a threshold were the
|
|
528
|
+
// output isn't correct anymore. Example: 2024-01-08T23:00:00.000Z and
|
|
529
|
+
// 2024-01-08T01:00:00.000Z would produce "yesterday", which is not the
|
|
530
|
+
// case. By using `always` we can ensure correct output. The only exception
|
|
531
|
+
// is the formatting of times <1 second as "now".
|
|
532
|
+
opts.numeric = unit === "second" ? "auto" : "always";
|
|
533
|
+
var value = calculateRelativeTimeValue(seconds, unit);
|
|
534
|
+
return formatters.getRelativeTimeFormat(locale, opts).format(value, unit);
|
|
535
|
+
} catch (error) {
|
|
536
|
+
onError(new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.FORMATTING_ERROR, error.message));
|
|
537
|
+
return String(date);
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
var list = function list(value, formatOrOptions, overrides) {
|
|
541
|
+
var serializedValue = [];
|
|
542
|
+
var richValues = new Map();
|
|
543
|
+
// `formatToParts` only accepts strings, therefore we have to temporarily
|
|
544
|
+
// replace React elements with a placeholder ID that can be used to retrieve
|
|
545
|
+
// the original value afterwards.
|
|
546
|
+
var index = 0;
|
|
547
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
548
|
+
try {
|
|
549
|
+
for(var _iterator = value[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
550
|
+
var item = _step.value;
|
|
551
|
+
var serializedItem = void 0;
|
|
552
|
+
if (typeof item === "object") {
|
|
553
|
+
serializedItem = String(index);
|
|
554
|
+
richValues.set(serializedItem, item);
|
|
555
|
+
} else {
|
|
556
|
+
serializedItem = String(item);
|
|
557
|
+
}
|
|
558
|
+
serializedValue.push(serializedItem);
|
|
559
|
+
index++;
|
|
560
|
+
}
|
|
561
|
+
} catch (err) {
|
|
562
|
+
_didIteratorError = true;
|
|
563
|
+
_iteratorError = err;
|
|
564
|
+
} finally{
|
|
565
|
+
try {
|
|
566
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
567
|
+
_iterator.return();
|
|
568
|
+
}
|
|
569
|
+
} finally{
|
|
570
|
+
if (_didIteratorError) {
|
|
571
|
+
throw _iteratorError;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return getFormattedValue(formatOrOptions, overrides, formats === null || formats === void 0 ? void 0 : formats.list, // @ts-expect-error -- `richValues.size` is used to determine the return type, but TypeScript can't infer the meaning of this correctly
|
|
576
|
+
function(options) {
|
|
577
|
+
var result = formatters.getListFormat(locale, options).formatToParts(serializedValue).map(function(part) {
|
|
578
|
+
return part.type === "literal" ? part.value : richValues.get(part.value) || part.value;
|
|
579
|
+
});
|
|
580
|
+
if (richValues.size > 0) {
|
|
581
|
+
return result;
|
|
582
|
+
} else {
|
|
583
|
+
return result.join("");
|
|
584
|
+
}
|
|
585
|
+
}, function() {
|
|
586
|
+
return String(value);
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
var displayName = function displayName(value, formatOrOptions, overrides) {
|
|
590
|
+
return getFormattedValue(formatOrOptions, overrides, formats === null || formats === void 0 ? void 0 : formats.displayName, function(options) {
|
|
591
|
+
return(// `options` is guaranteed non-null because our overloads require
|
|
592
|
+
// either inline options or a named format that resolves to options.
|
|
593
|
+
formatters.getDisplayNames(locale, options).of(value));
|
|
594
|
+
}, function() {
|
|
595
|
+
return value;
|
|
596
|
+
});
|
|
597
|
+
};
|
|
598
|
+
var tmp = props._cache, cache = tmp === void 0 ? (0, _formattersR4AAmsMPJs.b)() : tmp, tmp1 = props._formatters, formatters = tmp1 === void 0 ? (0, _formattersR4AAmsMPJs.c)(cache) : tmp1, formats = props.formats, locale = props.locale, _onError = props.onError, onError = _onError === void 0 ? defaultOnError : _onError, globalTimeZone = props.timeZone;
|
|
599
|
+
return {
|
|
600
|
+
dateTime: dateTime,
|
|
601
|
+
number: number,
|
|
602
|
+
relativeTime: relativeTime,
|
|
603
|
+
list: list,
|
|
604
|
+
dateTimeRange: dateTimeRange,
|
|
605
|
+
displayName: displayName
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
609
|
+
Object.entries(messages).forEach(function(param) {
|
|
610
|
+
var _param = _slicedToArray(param, 2), key = _param[0], messageOrMessages = _param[1];
|
|
611
|
+
if (key.includes(".")) {
|
|
612
|
+
var keyLabel = key;
|
|
613
|
+
if (parentPath) keyLabel += " (at ".concat(parentPath, ")");
|
|
614
|
+
invalidKeyLabels.push(keyLabel);
|
|
615
|
+
}
|
|
616
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
617
|
+
if (messageOrMessages != null && typeof messageOrMessages === "object") {
|
|
618
|
+
validateMessagesSegment(messageOrMessages, invalidKeyLabels, joinPath(parentPath, key));
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
function validateMessages(messages, onError) {
|
|
623
|
+
var invalidKeyLabels = [];
|
|
624
|
+
validateMessagesSegment(messages, invalidKeyLabels);
|
|
625
|
+
if (invalidKeyLabels.length > 0) {
|
|
626
|
+
onError(new _formattersR4AAmsMPJs.I(_formattersR4AAmsMPJs.a.INVALID_KEY, 'Namespace keys cannot contain the character "." as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid '.concat(invalidKeyLabels.length === 1 ? "key" : "keys", ": ").concat(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')));
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Enhances the incoming props with defaults.
|
|
631
|
+
*/ function initializeConfig(_param) {
|
|
632
|
+
var formats = _param.formats, getMessageFallback = _param.getMessageFallback, messages = _param.messages, onError = _param.onError, rest = _objectWithoutProperties(_param, [
|
|
633
|
+
"formats",
|
|
634
|
+
"getMessageFallback",
|
|
635
|
+
"messages",
|
|
636
|
+
"onError"
|
|
637
|
+
]);
|
|
638
|
+
var finalOnError = onError || defaultOnError;
|
|
639
|
+
var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
|
|
640
|
+
{
|
|
641
|
+
if (messages) {
|
|
642
|
+
validateMessages(messages, finalOnError);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return _objectSpreadProps(_objectSpread({}, rest), {
|
|
646
|
+
formats: formats || undefined,
|
|
647
|
+
messages: messages || undefined,
|
|
648
|
+
onError: finalOnError,
|
|
649
|
+
getMessageFallback: finalGetMessageFallback
|
|
650
|
+
});
|
|
651
|
+
}
|