use-intl 3.0.0-beta.2 → 3.0.0-beta.3
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 +92 -0
- package/dist/development/createIntl-dfba4462.js +421 -0
- package/dist/development/index.js +31 -0
- package/dist/development/initializeConfig-cde42612.js +119 -0
- package/dist/development/react.js +160 -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/createIntl-164a7766.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/initializeConfig-c316f266.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/createIntl-e408cd5c.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 +13 -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/{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 +19 -19
- package/dist/{core → types/src/core}/createIntl.d.ts +7 -7
- 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 +16 -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}/getInitializedConfig.d.ts +1 -1
- package/dist/{react → types/src/react}/index.d.ts +8 -8
- package/dist/{react → types/src/react}/useFormatter.d.ts +6 -6
- package/dist/{react → types/src/react}/useIntl.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/{react → types/src/react}/useMessages.d.ts +1 -1
- 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/createIntl.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/useIntl.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.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/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.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.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
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _defineProperty(obj, key, value) {
|
|
4
|
+
key = _toPropertyKey(key);
|
|
5
|
+
if (key in obj) {
|
|
6
|
+
Object.defineProperty(obj, key, {
|
|
7
|
+
value: value,
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true
|
|
11
|
+
});
|
|
12
|
+
} else {
|
|
13
|
+
obj[key] = value;
|
|
14
|
+
}
|
|
15
|
+
return obj;
|
|
16
|
+
}
|
|
17
|
+
function _toPrimitive(input, hint) {
|
|
18
|
+
if (typeof input !== "object" || input === null) return input;
|
|
19
|
+
var prim = input[Symbol.toPrimitive];
|
|
20
|
+
if (prim !== undefined) {
|
|
21
|
+
var res = prim.call(input, hint || "default");
|
|
22
|
+
if (typeof res !== "object") return res;
|
|
23
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
24
|
+
}
|
|
25
|
+
return (hint === "string" ? String : Number)(input);
|
|
26
|
+
}
|
|
27
|
+
function _toPropertyKey(arg) {
|
|
28
|
+
var key = _toPrimitive(arg, "string");
|
|
29
|
+
return typeof key === "symbol" ? key : String(key);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let IntlErrorCode = /*#__PURE__*/function (IntlErrorCode) {
|
|
33
|
+
IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
|
|
34
|
+
IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
|
|
35
|
+
IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
|
|
36
|
+
IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
|
|
37
|
+
IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
|
|
38
|
+
IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
|
|
39
|
+
return IntlErrorCode;
|
|
40
|
+
}({});
|
|
41
|
+
class IntlError extends Error {
|
|
42
|
+
constructor(code, originalMessage) {
|
|
43
|
+
let message = code;
|
|
44
|
+
if (originalMessage) {
|
|
45
|
+
message += ': ' + originalMessage;
|
|
46
|
+
}
|
|
47
|
+
super(message);
|
|
48
|
+
_defineProperty(this, "code", void 0);
|
|
49
|
+
_defineProperty(this, "originalMessage", void 0);
|
|
50
|
+
this.code = code;
|
|
51
|
+
if (originalMessage) {
|
|
52
|
+
this.originalMessage = originalMessage;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Contains defaults that are used for all entry points into the core.
|
|
59
|
+
* See also `InitializedIntlConfiguration`.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
function defaultGetMessageFallback(props) {
|
|
63
|
+
return [props.namespace, props.key].filter(part => part != null).join('.');
|
|
64
|
+
}
|
|
65
|
+
function defaultOnError(error) {
|
|
66
|
+
console.error(error);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
70
|
+
Object.entries(messages).forEach(_ref => {
|
|
71
|
+
let [key, messageOrMessages] = _ref;
|
|
72
|
+
if (key.includes('.')) {
|
|
73
|
+
let keyLabel = key;
|
|
74
|
+
if (parentPath) keyLabel += " (at ".concat(parentPath, ")");
|
|
75
|
+
invalidKeyLabels.push(keyLabel);
|
|
76
|
+
}
|
|
77
|
+
if (messageOrMessages != null && typeof messageOrMessages === 'object') {
|
|
78
|
+
validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(part => part != null).join('.'));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function validateMessages(messages, onError) {
|
|
83
|
+
const invalidKeyLabels = [];
|
|
84
|
+
validateMessagesSegment(messages, invalidKeyLabels);
|
|
85
|
+
if (invalidKeyLabels.length > 0) {
|
|
86
|
+
onError(new IntlError(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 ".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") ));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Enhances the incoming props with defaults.
|
|
92
|
+
*/
|
|
93
|
+
function initializeConfig(_ref) {
|
|
94
|
+
let {
|
|
95
|
+
getMessageFallback,
|
|
96
|
+
messages,
|
|
97
|
+
onError,
|
|
98
|
+
...rest
|
|
99
|
+
} = _ref;
|
|
100
|
+
const finalOnError = onError || defaultOnError;
|
|
101
|
+
const finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
|
|
102
|
+
{
|
|
103
|
+
if (messages) {
|
|
104
|
+
validateMessages(messages, finalOnError);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
...rest,
|
|
109
|
+
messages,
|
|
110
|
+
onError: finalOnError,
|
|
111
|
+
getMessageFallback: finalGetMessageFallback
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
exports.IntlError = IntlError;
|
|
116
|
+
exports.IntlErrorCode = IntlErrorCode;
|
|
117
|
+
exports.defaultGetMessageFallback = defaultGetMessageFallback;
|
|
118
|
+
exports.defaultOnError = defaultOnError;
|
|
119
|
+
exports.initializeConfig = initializeConfig;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _IntlProvider = require('./_IntlProvider.js');
|
|
6
|
+
var _useLocale = require('./_useLocale-321e619f.js');
|
|
7
|
+
var React = require('react');
|
|
8
|
+
var createIntl = require('./createIntl-dfba4462.js');
|
|
9
|
+
require('./initializeConfig-cde42612.js');
|
|
10
|
+
require('./IntlContext-b5cc6be8.js');
|
|
11
|
+
require('intl-messageformat');
|
|
12
|
+
|
|
13
|
+
function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
14
|
+
const {
|
|
15
|
+
defaultTranslationValues,
|
|
16
|
+
formats: globalFormats,
|
|
17
|
+
getMessageFallback,
|
|
18
|
+
locale,
|
|
19
|
+
messageFormatCache,
|
|
20
|
+
onError,
|
|
21
|
+
timeZone
|
|
22
|
+
} = _useLocale.useIntlContext();
|
|
23
|
+
|
|
24
|
+
// The `namespacePrefix` is part of the type system.
|
|
25
|
+
// See the comment in the hook invocation.
|
|
26
|
+
allMessages = allMessages[namespacePrefix];
|
|
27
|
+
namespace = createIntl.resolveNamespace(namespace, namespacePrefix);
|
|
28
|
+
const translate = React.useMemo(() => createIntl.createBaseTranslator({
|
|
29
|
+
messageFormatCache,
|
|
30
|
+
getMessageFallback,
|
|
31
|
+
messages: allMessages,
|
|
32
|
+
defaultTranslationValues,
|
|
33
|
+
namespace,
|
|
34
|
+
onError,
|
|
35
|
+
formats: globalFormats,
|
|
36
|
+
locale,
|
|
37
|
+
timeZone
|
|
38
|
+
}), [messageFormatCache, getMessageFallback, allMessages, namespace, onError, defaultTranslationValues, globalFormats, locale, timeZone]);
|
|
39
|
+
return translate;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
44
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
45
|
+
*
|
|
46
|
+
* If no namespace is provided, all available messages are returned.
|
|
47
|
+
* The namespace can also indicate nesting by using a dot
|
|
48
|
+
* (e.g. `namespace.Component`).
|
|
49
|
+
*/
|
|
50
|
+
function useTranslations(namespace) {
|
|
51
|
+
const context = _useLocale.useIntlContext();
|
|
52
|
+
const messages = context.messages;
|
|
53
|
+
|
|
54
|
+
// We have to wrap the actual hook so the type inference for the optional
|
|
55
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
56
|
+
// The prefix ("!") is arbitrary.
|
|
57
|
+
return useTranslationsImpl({
|
|
58
|
+
'!': messages
|
|
59
|
+
},
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
namespace ? "!.".concat(namespace) : '!', '!');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getNow() {
|
|
65
|
+
return new Date();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Reading the current date via `new Date()` in components should be avoided, as
|
|
70
|
+
* it causes components to be impure and can lead to flaky tests. Instead, this
|
|
71
|
+
* hook can be used.
|
|
72
|
+
*
|
|
73
|
+
* By default, it returns the time when the component mounts. If `updateInterval`
|
|
74
|
+
* is specified, the value will be updated based on the interval.
|
|
75
|
+
*
|
|
76
|
+
* You can however also return a static value from this hook, if you
|
|
77
|
+
* configure the `now` parameter on the context provider. Note however,
|
|
78
|
+
* that if `updateInterval` is configured in this case, the component
|
|
79
|
+
* will initialize with the global value, but will afterwards update
|
|
80
|
+
* continuously based on the interval.
|
|
81
|
+
*
|
|
82
|
+
* For unit tests, this can be mocked to a constant value. For end-to-end
|
|
83
|
+
* testing, an environment parameter can be passed to the `now` parameter
|
|
84
|
+
* of the provider to mock this to a static value.
|
|
85
|
+
*/
|
|
86
|
+
function useNow(options) {
|
|
87
|
+
const updateInterval = options === null || options === void 0 ? void 0 : options.updateInterval;
|
|
88
|
+
const {
|
|
89
|
+
now: globalNow
|
|
90
|
+
} = _useLocale.useIntlContext();
|
|
91
|
+
const [now, setNow] = React.useState(globalNow || getNow());
|
|
92
|
+
React.useEffect(() => {
|
|
93
|
+
if (!updateInterval) return;
|
|
94
|
+
const intervalId = setInterval(() => {
|
|
95
|
+
setNow(getNow());
|
|
96
|
+
}, updateInterval);
|
|
97
|
+
return () => {
|
|
98
|
+
clearInterval(intervalId);
|
|
99
|
+
};
|
|
100
|
+
}, [globalNow, updateInterval]);
|
|
101
|
+
return now;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function useTimeZone() {
|
|
105
|
+
return _useLocale.useIntlContext().timeZone;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function useMessages() {
|
|
109
|
+
return _useLocale.useIntlContext().messages;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function useFormatter() {
|
|
113
|
+
const {
|
|
114
|
+
formats,
|
|
115
|
+
locale,
|
|
116
|
+
now: globalNow,
|
|
117
|
+
onError,
|
|
118
|
+
timeZone
|
|
119
|
+
} = _useLocale.useIntlContext();
|
|
120
|
+
return React.useMemo(() => createIntl.createFormatter({
|
|
121
|
+
formats,
|
|
122
|
+
locale,
|
|
123
|
+
now: globalNow,
|
|
124
|
+
onError,
|
|
125
|
+
timeZone
|
|
126
|
+
}), [formats, globalNow, locale, onError, timeZone]);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let hasWarned = false;
|
|
130
|
+
|
|
131
|
+
/** @deprecated Switch to `useFormatter` instead. */
|
|
132
|
+
function useIntl() {
|
|
133
|
+
const {
|
|
134
|
+
formats,
|
|
135
|
+
locale,
|
|
136
|
+
now: globalNow,
|
|
137
|
+
onError,
|
|
138
|
+
timeZone
|
|
139
|
+
} = _useLocale.useIntlContext();
|
|
140
|
+
if (!hasWarned) {
|
|
141
|
+
hasWarned = true;
|
|
142
|
+
console.warn('`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.');
|
|
143
|
+
}
|
|
144
|
+
return React.useMemo(() => createIntl.createIntl({
|
|
145
|
+
formats,
|
|
146
|
+
locale,
|
|
147
|
+
now: globalNow,
|
|
148
|
+
onError,
|
|
149
|
+
timeZone
|
|
150
|
+
}), [formats, globalNow, locale, onError, timeZone]);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
exports.IntlProvider = _IntlProvider.IntlProvider;
|
|
154
|
+
exports.useLocale = _useLocale.useLocale;
|
|
155
|
+
exports.useFormatter = useFormatter;
|
|
156
|
+
exports.useIntl = useIntl;
|
|
157
|
+
exports.useMessages = useMessages;
|
|
158
|
+
exports.useNow = useNow;
|
|
159
|
+
exports.useTimeZone = useTimeZone;
|
|
160
|
+
exports.useTranslations = useTranslations;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("react").createContext(void 0);exports.IntlContext=t;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-c316f266.js"),r=require("./IntlContext-381f3ce4.js");function i(e){return e&&e.__esModule?e:{default:e}}var n=i(e);exports.IntlProvider=function(i){let{children:a,...o}=i;const[u]=e.useState((()=>new Map));return n.default.createElement(r.IntlContext.Provider,{value:{...t.initializeConfig(o),messageFormatCache:u}},a)};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("react"),e=require("./IntlContext-381f3ce4.js");function r(){const r=t.useContext(e.IntlContext);if(!r)throw new Error("No intl context found. Have you configured the provider?");return r}exports.useIntlContext=r,exports.useLocale=function(){return r().locale};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_useLocale-89b32eb9.js");require("react"),require("./IntlContext-381f3ce4.js"),exports.useLocale=e.useLocale;
|
package/dist/esm/core.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-c316f266.js"),r=require("./createIntl-164a7766.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createBaseTranslator=r.createBaseTranslator,exports.createFormatter=r.createFormatter,exports.createIntl=r.createIntl,exports.createTranslator=function(t){let{getMessageFallback:a=e.defaultGetMessageFallback,messages:s,namespace:n,onError:o=e.defaultOnError,...c}=t;return function(t,a){let{getMessageFallback:s,messages:n,namespace:o,onError:c,...i}=t;n=n[a],o=r.resolveNamespace(o,a);const l=r.createBaseTranslator({...i,onError:c,getMessageFallback:s,messages:n,namespace:o}),g=l.rich;function u(){return l(...arguments)}return u.rich=(r,t,a)=>{const n=g(r,t,a);if("string"!=typeof n){const t=new e.IntlError(e.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>`})");return c(t),s({error:t,key:r,namespace:o})}return n},u.raw=l.raw,u}({...c,onError:o,getMessageFallback:a,messages:{"!":s},namespace:n?"!.".concat(n):"!"},"!")};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("intl-messageformat"),r=require("react"),t=require("./initializeConfig-c316f266.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function a(e,r){return e?Object.keys(e).reduce(((t,n)=>(t[n]={timeZone:r,...e[n]},t)),{}):e}function s(e,r,t){if(!e)throw new Error("No messages available at `".concat(t,"`."));let n=e;return r.split(".").forEach((e=>{const o=n[e];if(null==e||null==o)throw new Error("Could not resolve `".concat(r,"` in ").concat(t?"`".concat(t,"`"):"messages","."));n=o})),n}const c=60,i=60*c,u=24*i,l=7*u,m=u*(365/12),f=365*u;function d(e){let{formats:r,locale:n,now:o,onError:a=t.defaultOnError,timeZone:s}=e;function d(e,r,n,o){let s;try{s=function(e,r){let n;if("string"==typeof r){const o=r;if(n=null==e?void 0:e[o],!n){const e=new t.IntlError(t.IntlErrorCode.MISSING_FORMAT,"Format `".concat(o,"` is not available. You can configure it on the provider or provide custom options."));throw a(e),e}}else n=r;return n}(n,r)}catch(r){return String(e)}try{return o(s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}}return{dateTime:function(e,t){return d(e,t,null==r?void 0:r.dateTime,(r=>{var t;return!s||null!==(t=r)&&void 0!==t&&t.timeZone||(r={...r,timeZone:s}),new Intl.DateTimeFormat(n,r).format(e)}))},number:function(e,t){return d(e,t,null==r?void 0:r.number,(r=>new Intl.NumberFormat(n,r).format(e)))},relativeTime:function(e,r){try{if(!r){if(!o)throw new Error("The `now` parameter wasn't provided and there was no global fallback configured on the provider.");r=o}const t=e instanceof Date?e:new Date(e),a=r instanceof Date?r:new Date(r),s=(t.getTime()-a.getTime())/1e3,{unit:d,value:g}=function(e){const r=Math.abs(e);let t,n;return r<c?(n="second",t=Math.round(e)):r<i?(n="minute",t=Math.round(e/c)):r<u?(n="hour",t=Math.round(e/i)):r<l?(n="day",t=Math.round(e/u)):r<m?(n="week",t=Math.round(e/l)):r<f?(n="month",t=Math.round(e/m)):(n="year",t=Math.round(e/f)),{value:t,unit:n}}(s);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(g,d)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}},list:function(e,t){return d(e,t,null==r?void 0:r.list,(r=>new Intl.ListFormat(n,r).format(e)))}}}exports.createBaseTranslator=function(e){const n=function(e){let{messages:r,namespace:n,onError:o=t.defaultOnError}=e;try{if(!r)throw new Error("No messages were configured on the provider.");const e=n?s(r,n):r;if(!e)throw new Error("No messages for namespace `".concat(n,"` found."));return e}catch(e){const r=new t.IntlError(t.IntlErrorCode.MISSING_MESSAGE,e.message);return o(r),r}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:c,getMessageFallback:i=t.defaultGetMessageFallback,locale:u,messageFormatCache:l,messagesOrError:m,namespace:f,onError:d,timeZone:g}=e;function E(e,r,n){const o=new t.IntlError(r,n);return d(o),i({error:o,key:e,namespace:f})}function I(e,d,I){if(m instanceof t.IntlError)return i({error:m,key:e,namespace:f});const h=m;let p;try{p=s(h,e,f)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}function v(e){return e.filter((e=>null!=e)).join(".")}const w=v([u,f,e,String(p)]);let y;if(null!=l&&l.has(w))y=l.get(w);else{if("object"==typeof p){let r,n;return Array.isArray(p)?(r=t.IntlErrorCode.INVALID_MESSAGE,n="Message at `".concat(v([f,e]),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(r=t.IntlErrorCode.INSUFFICIENT_PATH,n="Message at `".concat(v([f,e]),"` 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")),E(e,r,n)}const r=function(e,r){if(r)return;const t=e.replace(/'([{}])/gi,"$1");return/<|{/.test(t)?void 0:t}(p,d);if(r)return r;try{y=new o.default(p,u,function(e,r){const t=r?{...e,dateTime:a(e.dateTime,r)}:e,n=o.default.formats.date,s=r?a(n,r):n,c=o.default.formats.time,i=r?a(c,r):c;return{...t,date:{...s,...null==t?void 0:t.dateTime},time:{...i,...null==t?void 0:t.dateTime}}}({...c,...I},g))}catch(r){return E(e,t.IntlErrorCode.INVALID_MESSAGE,r.message)}null==l||l.set(w,y)}try{const t=y.format(function(e){if(0===Object.keys(e).length)return;const t={};return Object.keys(e).forEach((n=>{let o=0;const a=e[n];let s;s="function"==typeof a?e=>{const t=a(e);return r.isValidElement(t)?r.cloneElement(t,{key:n+o++}):t}:a,t[n]=s})),t}({...n,...d}));if(null==t)throw new Error("Unable to format `".concat(e,"` in ").concat(f?"namespace `".concat(f,"`"):"messages"));return r.isValidElement(t)||Array.isArray(t)||"string"==typeof t?t:String(t)}catch(r){return E(e,t.IntlErrorCode.FORMATTING_ERROR,r.message)}}function h(e,r,n){const o=I(e,r,n);return"string"!=typeof o?E(e,t.IntlErrorCode.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(f?"namespace `".concat(f,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):o}return h.rich=I,h.raw=e=>{if(m instanceof t.IntlError)return i({error:m,key:e,namespace:f});const r=m;try{return s(r,e,f)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}},h}({...e,messagesOrError:n})},exports.createFormatter=d,exports.createIntl=function(){const e=d(...arguments);return{formatDateTime:e.dateTime,formatNumber:e.number,formatRelativeTime:e.relativeTime}},exports.resolveNamespace=function(e,r){return e===r?void 0:e.slice((r+".").length)};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-c316f266.js"),r=require("./core.js"),t=require("./createIntl-164a7766.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),a=require("./_useLocale-89b32eb9.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createTranslator=r.createTranslator,exports.createBaseTranslator=t.createBaseTranslator,exports.createFormatter=t.createFormatter,exports.createIntl=t.createIntl,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useIntl=o.useIntl,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=a.useLocale;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e,n,t){return(n=function(e){var n=function(e,n){if("object"!=typeof e||null===e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,n||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(e)}(e,"string");return"symbol"==typeof n?n:String(n)}(n))in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}let n=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class t extends Error{constructor(n,t){let r=n;t&&(r+=": "+t),super(r),e(this,"code",void 0),e(this,"originalMessage",void 0),this.code=n,t&&(this.originalMessage=t)}}function r(e){return[e.namespace,e.key].filter((e=>null!=e)).join(".")}function o(e){console.error(e)}function i(e,n,t){Object.entries(e).forEach((e=>{let[r,o]=e;if(r.includes(".")){let e=r;t&&(e+=" (at ".concat(t,")")),n.push(e)}null!=o&&"object"==typeof o&&i(o,n,[t,r].filter((e=>null!=e)).join("."))}))}function s(e,r){const o=[];i(e,o),o.length>0&&r(new t(n.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 '.concat(1===o.length?"key":"keys",": ").concat(o.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')))}exports.IntlError=t,exports.IntlErrorCode=n,exports.defaultGetMessageFallback=r,exports.defaultOnError=o,exports.initializeConfig=function(e){let{getMessageFallback:n,messages:t,onError:i,...a}=e;const c=i||o,l=n||r;return t&&s(t,c),{...a,messages:t,onError:c,getMessageFallback:l}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-89b32eb9.js"),r=require("react"),o=require("./createIntl-164a7766.js");function n(){return new Date}require("./initializeConfig-c316f266.js"),require("./IntlContext-381f3ce4.js"),require("intl-messageformat");let s=!1;exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:l}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:l})),[e,s,n,a,l])},exports.useIntl=function(){const{formats:e,locale:n,now:a,onError:l,timeZone:u}=t.useIntlContext();return s||(s=!0,console.warn("`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.")),r.useMemo((()=>o.createIntl({formats:e,locale:n,now:a,onError:l,timeZone:u})),[e,a,n,l,u])},exports.useMessages=function(){return t.useIntlContext().messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:s}=t.useIntlContext(),[a,l]=r.useState(s||n());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{l(n())}),o);return()=>{clearInterval(e)}}),[s,o]),a},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,n,s){const{defaultTranslationValues:a,formats:l,getMessageFallback:u,locale:i,messageFormatCache:c,onError:m,timeZone:f}=t.useIntlContext();return e=e[s],n=o.resolveNamespace(n,s),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:c,getMessageFallback:u,messages:e,defaultTranslationValues:a,namespace:n,onError:m,formats:l,locale:i,timeZone:f})),[c,u,e,n,m,a,l,i,f])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
3
2
|
|
|
4
3
|
if (process.env.NODE_ENV === 'production') {
|
|
5
|
-
module.exports = require('./
|
|
4
|
+
module.exports = require('./production/index.js');
|
|
6
5
|
} else {
|
|
7
|
-
module.exports = require('./
|
|
6
|
+
module.exports = require('./development/index.js');
|
|
8
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("react").createContext(void 0);exports.IntlContext=t;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-984a566d.js"),r=require("./IntlContext-381f3ce4.js");function i(e){return e&&e.__esModule?e:{default:e}}var n=i(e);exports.IntlProvider=function(i){let{children:a,...o}=i;const[u]=e.useState((()=>new Map));return n.default.createElement(r.IntlContext.Provider,{value:{...t.initializeConfig(o),messageFormatCache:u}},a)};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("react"),e=require("./IntlContext-381f3ce4.js");function r(){const r=t.useContext(e.IntlContext);if(!r)throw new Error(void 0);return r}exports.useIntlContext=r,exports.useLocale=function(){return r().locale};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_useLocale-8e23751a.js");require("react"),require("./IntlContext-381f3ce4.js"),exports.useLocale=e.useLocale;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createIntl-e408cd5c.js"),r=require("./initializeConfig-984a566d.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createBaseTranslator=e.createBaseTranslator,exports.createFormatter=e.createFormatter,exports.createIntl=e.createIntl,exports.initializeConfig=r.initializeConfig,exports.createTranslator=function(t){let{getMessageFallback:a=r.defaultGetMessageFallback,messages:s,namespace:o,onError:n=r.defaultOnError,...l}=t;return function(r,t){let{getMessageFallback:a,messages:s,namespace:o,onError:n,...l}=r;s=s[t],o=e.resolveNamespace(o,t);const c=e.createBaseTranslator({...l,onError:n,getMessageFallback:a,messages:s,namespace:o}),i=c.rich;function g(){return c(...arguments)}return g.rich=(r,t,s)=>{const l=i(r,t,s);if("string"!=typeof l){const t=new e.IntlError(e.IntlErrorCode.FORMATTING_ERROR,void 0);return n(t),a({error:t,key:r,namespace:o})}return l},g.raw=c.raw,g}({...l,onError:n,getMessageFallback:a,messages:{"!":s},namespace:o?"!.".concat(o):"!"},"!")};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("intl-messageformat"),t=require("react"),r=require("./initializeConfig-984a566d.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function i(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}let a=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}({});class u extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),i(this,"code",void 0),i(this,"originalMessage",void 0),this.code=e,t&&(this.originalMessage=t)}}function s(e,t){return e?Object.keys(e).reduce(((r,n)=>(r[n]={timeZone:t,...e[n]},r)),{}):e}function c(e,t,r){if(!e)throw new Error(void 0);let n=e;return t.split(".").forEach((e=>{const t=n[e];if(null==e||null==t)throw new Error(void 0);n=t})),n}const l=60,f=60*l,m=24*f,d=7*m,E=m*(365/12),I=365*m;function S(e){let{formats:t,locale:n,now:o,onError:i=r.defaultOnError,timeZone:s}=e;function c(e,t,r,n){let o;try{o=function(e,t){let r;if("string"==typeof t){if(r=null==e?void 0:e[t],!r){const e=new u(a.MISSING_FORMAT,void 0);throw i(e),e}}else r=t;return r}(r,t)}catch(t){return String(e)}try{return n(o)}catch(t){return i(new u(a.FORMATTING_ERROR,t.message)),String(e)}}return{dateTime:function(e,r){return c(e,r,null==t?void 0:t.dateTime,(t=>{var r;return!s||null!==(r=t)&&void 0!==r&&r.timeZone||(t={...t,timeZone:s}),new Intl.DateTimeFormat(n,t).format(e)}))},number:function(e,r){return c(e,r,null==t?void 0:t.number,(t=>new Intl.NumberFormat(n,t).format(e)))},relativeTime:function(e,t){try{if(!t){if(!o)throw new Error(void 0);t=o}const r=e instanceof Date?e:new Date(e),i=t instanceof Date?t:new Date(t),a=(r.getTime()-i.getTime())/1e3,{unit:u,value:s}=function(e){const t=Math.abs(e);let r,n;return t<l?(n="second",r=Math.round(e)):t<f?(n="minute",r=Math.round(e/l)):t<m?(n="hour",r=Math.round(e/f)):t<d?(n="day",r=Math.round(e/m)):t<E?(n="week",r=Math.round(e/d)):t<I?(n="month",r=Math.round(e/E)):(n="year",r=Math.round(e/I)),{value:r,unit:n}}(a);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(s,u)}catch(t){return i(new u(a.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){return c(e,r,null==t?void 0:t.list,(t=>new Intl.ListFormat(n,t).format(e)))}}}exports.IntlError=u,exports.IntlErrorCode=a,exports.createBaseTranslator=function(e){const n=function(e){let{messages:t,namespace:n,onError:o=r.defaultOnError}=e;try{if(!t)throw new Error(void 0);const e=n?c(t,n):t;if(!e)throw new Error(void 0);return e}catch(e){const t=new u(a.MISSING_MESSAGE,e.message);return o(t),t}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:l=r.defaultGetMessageFallback,locale:f,messageFormatCache:m,messagesOrError:d,namespace:E,onError:I,timeZone:S}=e;function g(e,t,r){const n=new u(t,r);return I(n),l({error:n,key:e,namespace:E})}function M(e,r,I){if(d instanceof u)return l({error:d,key:e,namespace:E});const M=d;let v;try{v=c(M,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}function h(e){return e.filter((e=>null!=e)).join(".")}const T=h([f,E,e,String(v)]);let y;if(null!=m&&m.has(T))y=m.get(T);else{if("object"==typeof v){let t,r;return t=Array.isArray(v)?a.INVALID_MESSAGE:a.INSUFFICIENT_PATH,g(e,t,r)}const t=function(e,t){if(t)return;const r=e.replace(/'([{}])/gi,"$1");return/<|{/.test(r)?void 0:r}(v,r);if(t)return t;try{y=new o.default(v,f,function(e,t){const r=t?{...e,dateTime:s(e.dateTime,t)}:e,n=o.default.formats.date,i=t?s(n,t):n,a=o.default.formats.time,u=t?s(a,t):a;return{...r,date:{...i,...null==r?void 0:r.dateTime},time:{...u,...null==r?void 0:r.dateTime}}}({...i,...I},S))}catch(t){return g(e,a.INVALID_MESSAGE,t.message)}null==m||m.set(T,y)}try{const e=y.format(function(e){if(0===Object.keys(e).length)return;const r={};return Object.keys(e).forEach((n=>{let o=0;const i=e[n];let a;a="function"==typeof i?e=>{const r=i(e);return t.isValidElement(r)?t.cloneElement(r,{key:n+o++}):r}:i,r[n]=a})),r}({...n,...r}));if(null==e)throw new Error(void 0);return t.isValidElement(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(t){return g(e,a.FORMATTING_ERROR,t.message)}}function v(e,t,r){const n=M(e,t,r);return"string"!=typeof n?g(e,a.INVALID_MESSAGE,void 0):n}return v.rich=M,v.raw=e=>{if(d instanceof u)return l({error:d,key:e,namespace:E});const t=d;try{return c(t,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}},v}({...e,messagesOrError:n})},exports.createFormatter=S,exports.createIntl=function(){const e=S(...arguments);return{formatDateTime:e.dateTime,formatNumber:e.number,formatRelativeTime:e.relativeTime}},exports.resolveNamespace=function(e,t){return e===t?void 0:e.slice((t+".").length)};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createIntl-e408cd5c.js"),r=require("./core.js"),t=require("./initializeConfig-984a566d.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),a=require("./_useLocale-8e23751a.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createBaseTranslator=e.createBaseTranslator,exports.createFormatter=e.createFormatter,exports.createIntl=e.createIntl,exports.createTranslator=r.createTranslator,exports.initializeConfig=t.initializeConfig,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useIntl=o.useIntl,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=a.useLocale;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e){return[e.namespace,e.key].filter((e=>null!=e)).join(".")}function r(e){console.error(e)}exports.defaultGetMessageFallback=e,exports.defaultOnError=r,exports.initializeConfig=function(s){let{getMessageFallback:t,messages:n,onError:a,...o}=s;return{...o,messages:n,onError:a||r,getMessageFallback:t||e}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-8e23751a.js"),r=require("react"),o=require("./createIntl-e408cd5c.js");function n(){return new Date}require("./initializeConfig-984a566d.js"),require("./IntlContext-381f3ce4.js"),require("intl-messageformat"),exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useIntl=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createIntl({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useMessages=function(){return t.useIntlContext().messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:s}=t.useIntlContext(),[a,u]=r.useState(s||n());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{u(n())}),o);return()=>{clearInterval(e)}}),[s,o]),a},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,n,s){const{defaultTranslationValues:a,formats:u,getMessageFallback:l,locale:c,messageFormatCache:i,onError:m,timeZone:f}=t.useIntlContext();return e=e[s],n=o.resolveNamespace(n,s),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:i,getMessageFallback:l,messages:e,defaultTranslationValues:a,namespace:n,onError:m,formats:u,locale:c,timeZone:f})),[i,l,e,n,m,a,u,c,f])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
|
package/dist/react.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
3
2
|
|
|
4
3
|
if (process.env.NODE_ENV === 'production') {
|
|
5
|
-
module.exports = require('./react.
|
|
4
|
+
module.exports = require('./production/react.js');
|
|
6
5
|
} else {
|
|
7
|
-
module.exports = require('./react.
|
|
6
|
+
module.exports = require('./development/react.js');
|
|
8
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as IntlProvider } from './react/IntlProvider';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useLocale } from './react/useLocale';
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/** A generic type that describes the shape of messages.
|
|
2
|
-
*
|
|
3
|
-
* Optionally `IntlMessages` can be provided to get type safety for message
|
|
4
|
-
* namespaces and keys. See https://next-intl-docs.vercel.app/docs/usage/typescript
|
|
5
|
-
*/
|
|
6
|
-
type AbstractIntlMessages = {
|
|
7
|
-
[id: string]: AbstractIntlMessages | string;
|
|
8
|
-
};
|
|
9
|
-
export default AbstractIntlMessages;
|
|
1
|
+
/** A generic type that describes the shape of messages.
|
|
2
|
+
*
|
|
3
|
+
* Optionally `IntlMessages` can be provided to get type safety for message
|
|
4
|
+
* namespaces and keys. See https://next-intl-docs.vercel.app/docs/usage/typescript
|
|
5
|
+
*/
|
|
6
|
+
type AbstractIntlMessages = {
|
|
7
|
+
[id: string]: AbstractIntlMessages | string;
|
|
8
|
+
};
|
|
9
|
+
export default AbstractIntlMessages;
|
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import TimeZone from './TimeZone';
|
|
2
|
-
/**
|
|
3
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
|
|
4
|
-
*/
|
|
5
|
-
type DateTimeFormatOptions = Intl.DateTimeFormatOptions & {
|
|
6
|
-
/**
|
|
7
|
-
* Examples:
|
|
8
|
-
* - numeric: "2021"
|
|
9
|
-
* - 2-digit: "21"
|
|
10
|
-
*/
|
|
11
|
-
year?: 'numeric' | '2-digit';
|
|
12
|
-
/** Examples:
|
|
13
|
-
* - numeric: "3"
|
|
14
|
-
* - 2-digit: "03"
|
|
15
|
-
* - long: "March"
|
|
16
|
-
* - short: "Mar"
|
|
17
|
-
* - narrow: "M"
|
|
18
|
-
*/
|
|
19
|
-
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
|
|
20
|
-
/** Examples:
|
|
21
|
-
* - numeric: "2"
|
|
22
|
-
* - 2-digit: "02"
|
|
23
|
-
*/
|
|
24
|
-
day?: 'numeric' | '2-digit';
|
|
25
|
-
/** Examples:
|
|
26
|
-
* - numeric: "2"
|
|
27
|
-
* - 2-digit: "02"
|
|
28
|
-
*/
|
|
29
|
-
hour?: 'numeric' | '2-digit';
|
|
30
|
-
/** Examples:
|
|
31
|
-
* - numeric: "2"
|
|
32
|
-
* - 2-digit: "02"
|
|
33
|
-
*/
|
|
34
|
-
minute?: 'numeric' | '2-digit';
|
|
35
|
-
/** Examples:
|
|
36
|
-
* - numeric: "2"
|
|
37
|
-
* - 2-digit: "02"
|
|
38
|
-
*/
|
|
39
|
-
second?: 'numeric' | '2-digit';
|
|
40
|
-
/** Examples:
|
|
41
|
-
* - long: "Thursday"
|
|
42
|
-
* - short: "Thu"
|
|
43
|
-
* - narrow: "T"
|
|
44
|
-
*/
|
|
45
|
-
weekday?: 'long' | 'short' | 'narrow';
|
|
46
|
-
/** Examples:
|
|
47
|
-
* - long: "Anno Domini"
|
|
48
|
-
* - short: "AD", narrow "A"
|
|
49
|
-
*/
|
|
50
|
-
era?: 'long' | 'short' | 'narrow';
|
|
51
|
-
/** If this is set to `true`, a 12-hour am/pm format is used. Otherwise a 24-hour time.
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
hour12?: boolean;
|
|
55
|
-
/** Examples:
|
|
56
|
-
* - long: "Pacific Daylight Time"
|
|
57
|
-
* - short: "PDT"
|
|
58
|
-
*/
|
|
59
|
-
timeZoneName?: 'long' | 'short';
|
|
60
|
-
/**
|
|
61
|
-
* One of the [database names from the TZ database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List).
|
|
62
|
-
*/
|
|
63
|
-
timeZone?: TimeZone;
|
|
64
|
-
localeMatcher?: 'best fit' | 'lookup';
|
|
65
|
-
formatMatcher?: 'best fit' | 'basic';
|
|
66
|
-
dateStyle?: 'full' | 'long' | 'medium' | 'short';
|
|
67
|
-
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
|
68
|
-
calendar?: 'buddhist' | 'chinese' | 'coptic' | 'ethiopia' | 'ethiopic' | 'gregory' | 'hebrew' | 'indian' | 'islamic' | 'iso8601' | 'japanese' | 'persian' | 'roc';
|
|
69
|
-
dayPeriod?: 'narrow' | 'short' | 'long';
|
|
70
|
-
numberingSystem?: 'arab' | 'arabext' | 'bali' | 'beng' | 'deva' | 'fullwide' | 'gujr' | 'guru' | 'hanidec' | 'khmr' | 'knda' | 'laoo' | 'latn' | 'limb' | 'mlym' | 'mong' | 'mymr' | 'orya' | 'tamldec' | 'telu' | 'thai' | 'tibt';
|
|
71
|
-
hourCycle?: 'h11' | 'h12' | 'h23' | 'h24';
|
|
72
|
-
};
|
|
73
|
-
export default DateTimeFormatOptions;
|
|
1
|
+
import TimeZone from './TimeZone';
|
|
2
|
+
/**
|
|
3
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
|
|
4
|
+
*/
|
|
5
|
+
type DateTimeFormatOptions = Intl.DateTimeFormatOptions & {
|
|
6
|
+
/**
|
|
7
|
+
* Examples:
|
|
8
|
+
* - numeric: "2021"
|
|
9
|
+
* - 2-digit: "21"
|
|
10
|
+
*/
|
|
11
|
+
year?: 'numeric' | '2-digit';
|
|
12
|
+
/** Examples:
|
|
13
|
+
* - numeric: "3"
|
|
14
|
+
* - 2-digit: "03"
|
|
15
|
+
* - long: "March"
|
|
16
|
+
* - short: "Mar"
|
|
17
|
+
* - narrow: "M"
|
|
18
|
+
*/
|
|
19
|
+
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
|
|
20
|
+
/** Examples:
|
|
21
|
+
* - numeric: "2"
|
|
22
|
+
* - 2-digit: "02"
|
|
23
|
+
*/
|
|
24
|
+
day?: 'numeric' | '2-digit';
|
|
25
|
+
/** Examples:
|
|
26
|
+
* - numeric: "2"
|
|
27
|
+
* - 2-digit: "02"
|
|
28
|
+
*/
|
|
29
|
+
hour?: 'numeric' | '2-digit';
|
|
30
|
+
/** Examples:
|
|
31
|
+
* - numeric: "2"
|
|
32
|
+
* - 2-digit: "02"
|
|
33
|
+
*/
|
|
34
|
+
minute?: 'numeric' | '2-digit';
|
|
35
|
+
/** Examples:
|
|
36
|
+
* - numeric: "2"
|
|
37
|
+
* - 2-digit: "02"
|
|
38
|
+
*/
|
|
39
|
+
second?: 'numeric' | '2-digit';
|
|
40
|
+
/** Examples:
|
|
41
|
+
* - long: "Thursday"
|
|
42
|
+
* - short: "Thu"
|
|
43
|
+
* - narrow: "T"
|
|
44
|
+
*/
|
|
45
|
+
weekday?: 'long' | 'short' | 'narrow';
|
|
46
|
+
/** Examples:
|
|
47
|
+
* - long: "Anno Domini"
|
|
48
|
+
* - short: "AD", narrow "A"
|
|
49
|
+
*/
|
|
50
|
+
era?: 'long' | 'short' | 'narrow';
|
|
51
|
+
/** If this is set to `true`, a 12-hour am/pm format is used. Otherwise a 24-hour time.
|
|
52
|
+
*
|
|
53
|
+
*/
|
|
54
|
+
hour12?: boolean;
|
|
55
|
+
/** Examples:
|
|
56
|
+
* - long: "Pacific Daylight Time"
|
|
57
|
+
* - short: "PDT"
|
|
58
|
+
*/
|
|
59
|
+
timeZoneName?: 'long' | 'short';
|
|
60
|
+
/**
|
|
61
|
+
* One of the [database names from the TZ database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List).
|
|
62
|
+
*/
|
|
63
|
+
timeZone?: TimeZone;
|
|
64
|
+
localeMatcher?: 'best fit' | 'lookup';
|
|
65
|
+
formatMatcher?: 'best fit' | 'basic';
|
|
66
|
+
dateStyle?: 'full' | 'long' | 'medium' | 'short';
|
|
67
|
+
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
|
68
|
+
calendar?: 'buddhist' | 'chinese' | 'coptic' | 'ethiopia' | 'ethiopic' | 'gregory' | 'hebrew' | 'indian' | 'islamic' | 'iso8601' | 'japanese' | 'persian' | 'roc';
|
|
69
|
+
dayPeriod?: 'narrow' | 'short' | 'long';
|
|
70
|
+
numberingSystem?: 'arab' | 'arabext' | 'bali' | 'beng' | 'deva' | 'fullwide' | 'gujr' | 'guru' | 'hanidec' | 'khmr' | 'knda' | 'laoo' | 'latn' | 'limb' | 'mlym' | 'mong' | 'mymr' | 'orya' | 'tamldec' | 'telu' | 'thai' | 'tibt';
|
|
71
|
+
hourCycle?: 'h11' | 'h12' | 'h23' | 'h24';
|
|
72
|
+
};
|
|
73
|
+
export default DateTimeFormatOptions;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import DateTimeFormatOptions from './DateTimeFormatOptions';
|
|
2
|
-
import NumberFormatOptions from './NumberFormatOptions';
|
|
3
|
-
type Formats = {
|
|
4
|
-
number: Record<string, NumberFormatOptions>;
|
|
5
|
-
dateTime: Record<string, DateTimeFormatOptions>;
|
|
6
|
-
list: Record<string, Intl.ListFormatOptions>;
|
|
7
|
-
};
|
|
8
|
-
export default Formats;
|
|
1
|
+
import DateTimeFormatOptions from './DateTimeFormatOptions';
|
|
2
|
+
import NumberFormatOptions from './NumberFormatOptions';
|
|
3
|
+
type Formats = {
|
|
4
|
+
number: Record<string, NumberFormatOptions>;
|
|
5
|
+
dateTime: Record<string, DateTimeFormatOptions>;
|
|
6
|
+
list: Record<string, Intl.ListFormatOptions>;
|
|
7
|
+
};
|
|
8
|
+
export default Formats;
|