use-intl 2.11.0-alpha.1 → 2.11.0-alpha.2
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/CHANGELOG.md +11 -0
- package/dist/core/use-intl.esm2.js +2 -2
- package/dist/core/use-intl.esm3.js +1 -1
- package/dist/core/use-intl.esm4.js +56 -12
- package/dist/core/use-intl.esm4.js.map +1 -1
- package/dist/core/use-intl.esm5.js +12 -56
- package/dist/core/use-intl.esm5.js.map +1 -1
- package/dist/core/use-intl.esm6.js +199 -18
- package/dist/core/use-intl.esm6.js.map +1 -1
- package/dist/core/use-intl.esm7.js +7 -210
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/core/use-intl.esm8.js +29 -7
- package/dist/core/use-intl.esm8.js.map +1 -1
- package/dist/react/use-intl.esm10.js +2 -2
- package/dist/react/use-intl.esm2.js +20 -3
- package/dist/react/use-intl.esm2.js.map +1 -1
- package/dist/react/use-intl.esm3.js +20 -19
- package/dist/react/use-intl.esm3.js.map +1 -1
- package/dist/react/use-intl.esm4.js +3 -45
- package/dist/react/use-intl.esm4.js.map +1 -1
- package/dist/react/use-intl.esm5.js +3 -21
- package/dist/react/use-intl.esm5.js.map +1 -1
- package/dist/react/use-intl.esm6.js +45 -3
- package/dist/react/use-intl.esm6.js.map +1 -1
- package/dist/react/use-intl.esm8.js +2 -2
- package/dist/use-intl.esm.js +5 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## 2.10.2 (2022-12-09)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* Remove magic `__DEV__` global ([#151](https://github.com/amannn/next-intl/tree/main/packages/use-intl/issues/151)) ([7d5aa6a](https://github.com/amannn/next-intl/tree/main/packages/use-intl/commit/7d5aa6a8fda0189adc6440214270e07a5593d98f))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## 2.10.1 (2022-12-09)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package use-intl
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
-
import createTranslatorImpl from './use-intl.
|
|
3
|
-
import { defaultOnError, defaultGetMessageFallback } from './use-intl.
|
|
2
|
+
import createTranslatorImpl from './use-intl.esm4.js';
|
|
3
|
+
import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm5.js';
|
|
4
4
|
|
|
5
5
|
var _excluded = ["onError", "getMessageFallback", "messages", "namespace"];
|
|
6
6
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
2
|
import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
3
|
-
import { defaultOnError } from './use-intl.
|
|
3
|
+
import { defaultOnError } from './use-intl.esm5.js';
|
|
4
4
|
|
|
5
5
|
var MINUTE = 60;
|
|
6
6
|
var HOUR = MINUTE * 60;
|
|
@@ -1,15 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
+
import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
3
|
+
import createBaseTranslator, { getMessagesOrError } from './use-intl.esm6.js';
|
|
4
|
+
import resolveNamespace from './use-intl.esm7.js';
|
|
5
|
+
|
|
6
|
+
var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
7
|
+
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
8
|
+
var getMessageFallback = _ref.getMessageFallback,
|
|
9
|
+
messages = _ref.messages,
|
|
10
|
+
namespace = _ref.namespace,
|
|
11
|
+
onError = _ref.onError,
|
|
12
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
13
|
+
|
|
14
|
+
// The `namespacePrefix` is part of the type system.
|
|
15
|
+
// See the comment in the function invocation.
|
|
16
|
+
messages = messages[namespacePrefix];
|
|
17
|
+
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
18
|
+
var translator = createBaseTranslator(_extends({}, rest, {
|
|
19
|
+
onError: onError,
|
|
20
|
+
getMessageFallback: getMessageFallback,
|
|
21
|
+
messagesOrError: getMessagesOrError({
|
|
22
|
+
messages: messages,
|
|
23
|
+
namespace: namespace,
|
|
24
|
+
onError: onError
|
|
25
|
+
})
|
|
26
|
+
}));
|
|
27
|
+
var originalRich = translator.rich;
|
|
28
|
+
|
|
29
|
+
function base() {
|
|
30
|
+
return translator.apply(void 0, arguments);
|
|
31
|
+
} // Augment `t.rich` to return plain strings
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
base.rich = function (key,
|
|
35
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
36
|
+
values, formats) {
|
|
37
|
+
// `chunks` is returned as a string when no React element
|
|
38
|
+
// is used, therefore it's safe to cast this type.
|
|
39
|
+
var result = originalRich(key, values, formats); // When only string chunks are provided to the parser, only strings should be returned here.
|
|
40
|
+
|
|
41
|
+
if (typeof result !== 'string') {
|
|
42
|
+
var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== 'production' ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
|
|
43
|
+
onError(error);
|
|
44
|
+
return getMessageFallback({
|
|
45
|
+
error: error,
|
|
46
|
+
key: key,
|
|
47
|
+
namespace: namespace
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
base.raw = translator.raw;
|
|
55
|
+
return base;
|
|
12
56
|
}
|
|
13
57
|
|
|
14
|
-
export {
|
|
58
|
+
export { createTranslatorImpl as default };
|
|
15
59
|
//# sourceMappingURL=use-intl.esm4.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm4.js","sources":["../../src/core/
|
|
1
|
+
{"version":3,"file":"use-intl.esm4.js","sources":["../../src/core/createTranslatorImpl.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport {InitializedIntlConfiguration} from './IntlConfiguration';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport {RichTranslationValues, TranslationValue} from './TranslationValues';\nimport createBaseTranslator, {getMessagesOrError} from './createBaseTranslator';\nimport resolveNamespace from './resolveNamespace';\nimport NestedKeyOf from './utils/NestedKeyOf';\n\nexport type CoreRichTranslationValues = Record<\n string,\n TranslationValue | ((chunks: string) => string)\n>;\n\nexport type CreateTranslatorImplProps<Messages> =\n InitializedIntlConfiguration & {\n namespace: string;\n messages: Messages;\n };\n\nexport default function createTranslatorImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(\n {\n getMessageFallback,\n messages,\n namespace,\n onError,\n ...rest\n }: CreateTranslatorImplProps<Messages>,\n namespacePrefix: string\n) {\n // The `namespacePrefix` is part of the type system.\n // See the comment in the function invocation.\n messages = messages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const translator = createBaseTranslator<Messages, NestedKey>({\n ...rest,\n onError,\n getMessageFallback,\n messagesOrError: getMessagesOrError({\n messages,\n namespace,\n onError\n }) as Messages | IntlError\n });\n\n const originalRich = translator.rich;\n\n function base(...args: Parameters<typeof translator>) {\n return translator(...args);\n }\n\n // Augment `t.rich` to return plain strings\n base.rich = (\n key: Parameters<typeof originalRich>[0],\n /** Key value pairs for values to interpolate into the message. */\n values: CoreRichTranslationValues,\n formats?: Parameters<typeof originalRich>[2]\n ): string => {\n // `chunks` is returned as a string when no React element\n // is used, therefore it's safe to cast this type.\n const result = originalRich(key, values as RichTranslationValues, formats);\n\n // When only string chunks are provided to the parser, only strings should be returned here.\n if (typeof result !== 'string') {\n const error = new IntlError(\n IntlErrorCode.FORMATTING_ERROR,\n process.env.NODE_ENV !== 'production'\n ? \"`createTranslator` only accepts functions for rich text formatting that receive and return strings.\\n\\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})\"\n : undefined\n );\n\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n return result;\n };\n\n base.raw = translator.raw;\n\n return base;\n}\n"],"names":["createTranslatorImpl","namespacePrefix","getMessageFallback","messages","namespace","onError","rest","resolveNamespace","translator","createBaseTranslator","messagesOrError","getMessagesOrError","originalRich","rich","base","key","values","formats","result","error","IntlError","IntlErrorCode","FORMATTING_ERROR","process","env","NODE_ENV","undefined","raw"],"mappings":";;;;;;AAmBc,SAAUA,oBAAV,CAWZC,IAAAA,EAAAA,eAXY,EAWW;AAAA,EANrBC,IAAAA,kBAMqB,QANrBA,kBAMqB;AAAA,MALrBC,QAKqB,QALrBA,QAKqB;AAAA,MAJrBC,SAIqB,QAJrBA,SAIqB;AAAA,MAHrBC,OAGqB,QAHrBA,OAGqB;AAAA,MAFlBC,IAEkB,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AAEvB;AACA;AACAH,EAAAA,QAAQ,GAAGA,QAAQ,CAACF,eAAD,CAAnB,CAAA;AACAG,EAAAA,SAAS,GAAGG,gBAAgB,CAACH,SAAD,EAAYH,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMO,UAAU,GAAGC,oBAAoB,CAAA,QAAA,CAAA,EAAA,EAClCH,IADkC,EAAA;AAErCD,IAAAA,OAAO,E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|
@@ -1,59 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
13
|
-
|
|
14
|
-
// The `namespacePrefix` is part of the type system.
|
|
15
|
-
// See the comment in the function invocation.
|
|
16
|
-
messages = messages[namespacePrefix];
|
|
17
|
-
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
18
|
-
var translator = createBaseTranslator(_extends({}, rest, {
|
|
19
|
-
onError: onError,
|
|
20
|
-
getMessageFallback: getMessageFallback,
|
|
21
|
-
messagesOrError: getMessagesOrError({
|
|
22
|
-
messages: messages,
|
|
23
|
-
namespace: namespace,
|
|
24
|
-
onError: onError
|
|
25
|
-
})
|
|
26
|
-
}));
|
|
27
|
-
var originalRich = translator.rich;
|
|
28
|
-
|
|
29
|
-
function base() {
|
|
30
|
-
return translator.apply(void 0, arguments);
|
|
31
|
-
} // Augment `t.rich` to return plain strings
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
base.rich = function (key,
|
|
35
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
36
|
-
values, formats) {
|
|
37
|
-
// `chunks` is returned as a string when no React element
|
|
38
|
-
// is used, therefore it's safe to cast this type.
|
|
39
|
-
var result = originalRich(key, values, formats); // When only string chunks are provided to the parser, only strings should be returned here.
|
|
40
|
-
|
|
41
|
-
if (typeof result !== 'string') {
|
|
42
|
-
var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== 'production' ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
|
|
43
|
-
onError(error);
|
|
44
|
-
return getMessageFallback({
|
|
45
|
-
error: error,
|
|
46
|
-
key: key,
|
|
47
|
-
namespace: namespace
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return result;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
base.raw = translator.raw;
|
|
55
|
-
return base;
|
|
1
|
+
/**
|
|
2
|
+
* Contains defaults that are used for all entry points into the core.
|
|
3
|
+
* See also `InitializedIntlConfiguration`.
|
|
4
|
+
*/
|
|
5
|
+
function defaultGetMessageFallback(props) {
|
|
6
|
+
return [props.namespace, props.key].filter(function (part) {
|
|
7
|
+
return part != null;
|
|
8
|
+
}).join('.');
|
|
9
|
+
}
|
|
10
|
+
function defaultOnError(error) {
|
|
11
|
+
console.error(error);
|
|
56
12
|
}
|
|
57
13
|
|
|
58
|
-
export {
|
|
14
|
+
export { defaultGetMessageFallback, defaultOnError };
|
|
59
15
|
//# sourceMappingURL=use-intl.esm5.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/
|
|
1
|
+
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/defaults.tsx"],"sourcesContent":["import IntlError from './IntlError';\n\n/**\n * Contains defaults that are used for all entry points into the core.\n * See also `InitializedIntlConfiguration`.\n */\n\nexport function defaultGetMessageFallback(props: {\n error: IntlError;\n key: string;\n namespace?: string;\n}) {\n return [props.namespace, props.key].filter((part) => part != null).join('.');\n}\n\nexport function defaultOnError(error: IntlError) {\n console.error(error);\n}\n"],"names":["defaultGetMessageFallback","props","namespace","key","filter","part","join","defaultOnError","error","console"],"mappings":"AAEA;;;AAGG;AAEG,SAAUA,yBAAV,CAAoCC,KAApC,EAIL;AACC,EAAA,OAAO,CAACA,KAAK,CAACC,SAAP,EAAkBD,KAAK,CAACE,GAAxB,CAA6BC,CAAAA,MAA7B,CAAoC,UAACC,IAAD,EAAA;AAAA,IAAUA,OAAAA,IAAI,IAAI,IAAlB,CAAA;AAAA,GAApC,CAA4DC,CAAAA,IAA5D,CAAiE,GAAjE,CAAP,CAAA;AACD,CAAA;AAEK,SAAUC,cAAV,CAAyBC,KAAzB,EAAyC;AAC7CC,EAAAA,OAAO,CAACD,KAAR,CAAcA,KAAd,CAAA,CAAA;AACD;;;;"}
|
|
@@ -1,32 +1,213 @@
|
|
|
1
|
+
import { extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
+
import IntlMessageFormat from 'intl-messageformat';
|
|
3
|
+
import { isValidElement, cloneElement } from 'react';
|
|
1
4
|
import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
5
|
+
import convertFormatsToIntlMessageFormat from './use-intl.esm9.js';
|
|
6
|
+
import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm5.js';
|
|
2
7
|
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
function resolvePath(messages, key, namespace) {
|
|
9
|
+
if (!messages) {
|
|
10
|
+
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages available at `" + namespace + "`." : undefined);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
var message = messages;
|
|
14
|
+
key.split('.').forEach(function (part) {
|
|
15
|
+
var next = message[part];
|
|
7
16
|
|
|
8
|
-
if (
|
|
9
|
-
|
|
10
|
-
if (parentPath) keyLabel += " (at " + parentPath + ")";
|
|
11
|
-
invalidKeyLabels.push(keyLabel);
|
|
17
|
+
if (part == null || next == null) {
|
|
18
|
+
throw new Error(process.env.NODE_ENV !== 'production' ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
|
|
12
19
|
}
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
message = next;
|
|
22
|
+
});
|
|
23
|
+
return message;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function prepareTranslationValues(values) {
|
|
27
|
+
if (Object.keys(values).length === 0) return undefined; // Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
28
|
+
|
|
29
|
+
var transformedValues = {};
|
|
30
|
+
Object.keys(values).forEach(function (key) {
|
|
31
|
+
var index = 0;
|
|
32
|
+
var value = values[key];
|
|
33
|
+
var transformed;
|
|
34
|
+
|
|
35
|
+
if (typeof value === 'function') {
|
|
36
|
+
transformed = function transformed(chunks) {
|
|
37
|
+
var result = value(chunks);
|
|
38
|
+
return isValidElement(result) ? cloneElement(result, {
|
|
39
|
+
key: key + index++
|
|
40
|
+
}) : result;
|
|
41
|
+
};
|
|
42
|
+
} else {
|
|
43
|
+
transformed = value;
|
|
18
44
|
}
|
|
45
|
+
|
|
46
|
+
transformedValues[key] = transformed;
|
|
19
47
|
});
|
|
48
|
+
return transformedValues;
|
|
20
49
|
}
|
|
21
50
|
|
|
22
|
-
function
|
|
23
|
-
var
|
|
24
|
-
|
|
51
|
+
function getMessagesOrError(_ref) {
|
|
52
|
+
var messages = _ref.messages,
|
|
53
|
+
namespace = _ref.namespace,
|
|
54
|
+
_ref$onError = _ref.onError,
|
|
55
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
if (!messages) {
|
|
59
|
+
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages were configured on the provider." : undefined);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
25
63
|
|
|
26
|
-
|
|
27
|
-
|
|
64
|
+
if (!retrievedMessages) {
|
|
65
|
+
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return retrievedMessages;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
var intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
71
|
+
onError(intlError);
|
|
72
|
+
return intlError;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function createBaseTranslator(_ref2) {
|
|
76
|
+
var cachedFormatsByLocale = _ref2.cachedFormatsByLocale,
|
|
77
|
+
defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
78
|
+
globalFormats = _ref2.formats,
|
|
79
|
+
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
80
|
+
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
81
|
+
locale = _ref2.locale,
|
|
82
|
+
messagesOrError = _ref2.messagesOrError,
|
|
83
|
+
namespace = _ref2.namespace,
|
|
84
|
+
onError = _ref2.onError,
|
|
85
|
+
timeZone = _ref2.timeZone;
|
|
86
|
+
|
|
87
|
+
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
88
|
+
var error = new IntlError(code, message);
|
|
89
|
+
onError(error);
|
|
90
|
+
return getMessageFallback({
|
|
91
|
+
error: error,
|
|
92
|
+
key: key,
|
|
93
|
+
namespace: namespace
|
|
94
|
+
});
|
|
28
95
|
}
|
|
96
|
+
|
|
97
|
+
function translateBaseFn(
|
|
98
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
99
|
+
key,
|
|
100
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
101
|
+
values,
|
|
102
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
103
|
+
formats) {
|
|
104
|
+
var _cachedFormatsByLocal;
|
|
105
|
+
|
|
106
|
+
if (messagesOrError instanceof IntlError) {
|
|
107
|
+
// We have already warned about this during render
|
|
108
|
+
return getMessageFallback({
|
|
109
|
+
error: messagesOrError,
|
|
110
|
+
key: key,
|
|
111
|
+
namespace: namespace
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
var messages = messagesOrError;
|
|
116
|
+
var cacheKey = [namespace, key].filter(function (part) {
|
|
117
|
+
return part != null;
|
|
118
|
+
}).join('.');
|
|
119
|
+
var messageFormat;
|
|
120
|
+
|
|
121
|
+
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
122
|
+
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
123
|
+
} else {
|
|
124
|
+
var message;
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
message = resolvePath(messages, key, namespace);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (typeof message === 'object') {
|
|
133
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INSUFFICIENT_PATH, process.env.NODE_ENV !== 'production' ? "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." : undefined);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (cachedFormatsByLocale) {
|
|
143
|
+
if (!cachedFormatsByLocale[locale]) {
|
|
144
|
+
cachedFormatsByLocale[locale] = {};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
var formattedMessage = messageFormat.format( // @ts-ignore `intl-messageformat` expects a different format
|
|
153
|
+
// for rich text elements since a recent minor update. This
|
|
154
|
+
// needs to be evaluated in detail, possibly also in regards
|
|
155
|
+
// to be able to format to parts.
|
|
156
|
+
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
157
|
+
|
|
158
|
+
if (formattedMessage == null) {
|
|
159
|
+
throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
160
|
+
} // Limit the function signature to return strings or React elements
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
return isValidElement(formattedMessage) || // Arrays of React elements
|
|
164
|
+
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function translateFn(
|
|
171
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
172
|
+
key,
|
|
173
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
174
|
+
values,
|
|
175
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
176
|
+
formats) {
|
|
177
|
+
var result = translateBaseFn(key, values, formats);
|
|
178
|
+
|
|
179
|
+
if (typeof result !== 'string') {
|
|
180
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, process.env.NODE_ENV !== 'production' ? "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." : undefined);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
translateFn.rich = translateBaseFn;
|
|
187
|
+
|
|
188
|
+
translateFn.raw = function (
|
|
189
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
190
|
+
key) {
|
|
191
|
+
if (messagesOrError instanceof IntlError) {
|
|
192
|
+
// We have already warned about this during render
|
|
193
|
+
return getMessageFallback({
|
|
194
|
+
error: messagesOrError,
|
|
195
|
+
key: key,
|
|
196
|
+
namespace: namespace
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var messages = messagesOrError;
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
return resolvePath(messages, key, namespace);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return translateFn;
|
|
29
210
|
}
|
|
30
211
|
|
|
31
|
-
export {
|
|
212
|
+
export { createBaseTranslator as default, getMessagesOrError };
|
|
32
213
|
//# sourceMappingURL=use-intl.esm6.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n `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 ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}`\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMess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L,IAAIE,SAAJ,CACEC,aAAa,CAACC,WADhB,EAAA,2JAAA,IAGIlB,gBAAgB,CAACe,MAAjB,KAA4B,CAA5B,GAAgC,KAAhC,GAAwC,MAH5C,CAIOf,GAAAA,IAAAA,GAAAA,gBAAgB,CAACY,IAAjB,CAAsB,IAAtB,CAJP,CADK,CAAP,CAAA;AAQD,GAAA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/createBaseTranslator.tsx"],"sourcesContent":["import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray\n} from 'react';\nimport AbstractIntlMessages from './AbstractIntlMessages';\nimport Formats from './Formats';\nimport {InitializedIntlConfiguration} from './IntlConfiguration';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\nfunction resolvePath(\n messages: AbstractIntlMessages | undefined,\n key: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages available at \\`${namespace}\\`.`\n : undefined\n );\n }\n\n let message = messages;\n\n key.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Could not resolve \\`${key}\\` in ${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }.`\n : undefined\n );\n }\n\n message = next;\n });\n\n return message;\n}\n\nfunction prepareTranslationValues(values: RichTranslationValues) {\n if (Object.keys(values).length === 0) return undefined;\n\n // Workaround for https://github.com/formatjs/formatjs/issues/1467\n const transformedValues: RichTranslationValues = {};\n Object.keys(values).forEach((key) => {\n let index = 0;\n const value = values[key];\n\n let transformed;\n if (typeof value === 'function') {\n transformed = (chunks: ReactNode) => {\n const result = value(chunks);\n\n return isValidElement(result)\n ? cloneElement(result, {key: key + index++})\n : result;\n };\n } else {\n transformed = value;\n }\n\n transformedValues[key] = transformed;\n });\n\n return transformedValues;\n}\n\nexport function getMessagesOrError<Messages extends AbstractIntlMessages>({\n messages,\n namespace,\n onError = defaultOnError\n}: {\n messages: Messages;\n namespace?: string;\n onError?(error: IntlError): void;\n}) {\n try {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages were configured on the provider.`\n : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(messages, namespace)\n : messages;\n\n if (!retrievedMessages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages for namespace \\`${namespace}\\` found.`\n : undefined\n );\n }\n\n return retrievedMessages;\n } catch (error) {\n const intlError = new IntlError(\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n onError(intlError);\n return intlError;\n }\n}\n\nexport type CreateBaseTranslatorProps<Messages> =\n InitializedIntlConfiguration & {\n cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;\n defaultTranslationValues?: RichTranslationValues;\n namespace?: string;\n messagesOrError: Messages | IntlError;\n };\n\nexport default function createBaseTranslator<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>({\n cachedFormatsByLocale,\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback = defaultGetMessageFallback,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n}: CreateBaseTranslatorProps<Messages>) {\n function getFallbackFromErrorAndNotify(\n key: string,\n code: IntlErrorCode,\n message?: string\n ) {\n const error = new IntlError(code, message);\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n function translateBaseFn(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string,\n /** Key value pairs for values to interpolate into the message. */\n values?: RichTranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n const cacheKey = [namespace, key].filter((part) => part != null).join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale?.[locale][cacheKey];\n } else {\n let message;\n try {\n message = resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n\n if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n process.env.NODE_ENV !== 'production'\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (cachedFormatsByLocale) {\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n }\n\n try {\n const formattedMessage = messageFormat.format(\n // @ts-ignore `intl-messageformat` expects a different format\n // for rich text elements since a recent minor update. This\n // needs to be evaluated in detail, possibly also in regards\n // to be able to format to parts.\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Unable to format \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n }`\n : undefined\n );\n }\n\n // Limit the function signature to return strings or React elements\n return isValidElement(formattedMessage) ||\n // Arrays of React elements\n Array.isArray(formattedMessage) ||\n typeof formattedMessage === 'string'\n ? formattedMessage\n : String(formattedMessage);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.FORMATTING_ERROR,\n (error as Error).message\n );\n }\n }\n\n function translateFn<\n TargetKey extends MessageKeys<\n NestedValueOf<Messages, NestedKey>,\n NestedKeyOf<NestedValueOf<Messages, NestedKey>>\n >\n >(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: TargetKey,\n /** Key value pairs for values to interpolate into the message. */\n values?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const result = translateBaseFn(key, values, formats);\n\n if (typeof result !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n process.env.NODE_ENV !== 'production'\n ? `The message \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n } didn't resolve to a string. If you want to format rich text, use \\`t.rich\\` instead.`\n : undefined\n );\n }\n\n return result;\n }\n\n translateFn.rich = translateBaseFn;\n\n translateFn.raw = (\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string\n ): any => {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n try {\n return resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n };\n\n return translateFn;\n}\n"],"names":["resolvePath","messages","key","namespace","Error","process","env","NODE_ENV","undefined","message","split","forEach","part","next","prepareTranslationValues","values","Object","keys","length","transformedValues","index","value","transformed","chunks","result","isValidElement","cloneElement","getMessagesOrError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","createBaseTranslator","cachedFormatsByLocale","defaultTranslationValues","globalFormats","formats","getMessageFallback","defaultGetMessageFallback","locale","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","cacheKey","filter","join","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","convertFormatsToIntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAmBA,SAASA,WAAT,CACEC,QADF,EAEEC,GAFF,EAGEC,SAHF,EAGoB;AAElB,EAAI,IAAA,CAACF,QAAL,EAAe;AACb,IAAA,MAAM,IAAIG,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GAAA,4BAAA,GACkCJ,SADlC,GAAA,IAAA,GAEIK,SAHA,CAAN,CAAA;AAKD,GAAA;;AAED,EAAIC,IAAAA,OAAO,GAAGR,QAAd,CAAA;AAEAC,EAAAA,GAAG,CAACQ,KAAJ,CAAU,GAAV,EAAeC,OAAf,CAAuB,UAACC,IAAD,EAAS;AAC9B,IAAA,IAAMC,IAAI,GAAIJ,OAAe,CAACG,IAAD,CAA7B,CAAA;;AAEA,IAAA,IAAIA,IAAI,IAAI,IAAR,IAAgBC,IAAI,IAAI,IAA5B,EAAkC;AAChC,MAAM,MAAA,IAAIT,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,2BAC2BL,GAD3B,GAAA,OAAA,IAEMC,SAAS,GAAQA,GAAAA,GAAAA,SAAR,SAAwB,UAFvC,CAAA,GAAA,GAAA,GAIIK,SALA,CAAN,CAAA;AAOD,KAAA;;AAEDC,IAAAA,OAAO,GAAGI,IAAV,CAAA;AACD,GAdD,CAAA,CAAA;AAgBA,EAAA,OAAOJ,OAAP,CAAA;AACD,CAAA;;AAED,SAASK,wBAAT,CAAkCC,MAAlC,EAA+D;AAC7D,EAAA,IAAIC,MAAM,CAACC,IAAP,CAAYF,MAAZ,CAAA,CAAoBG,MAApB,KA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L,eAAAA,GAAAA,GADrB,GAEMC,OAAAA,IAAAA,SAAS,GAAkBA,aAAAA,GAAAA,SAAlB,GAAkC,GAAA,GAAA,UAFjD,CAIIK,GAAAA,qFAAAA,GAAAA,SAP8B,CAApC,CAAA;AASD,KAAA;;AAED,IAAA,OAAOgB,MAAP,CAAA;AACD,GAAA;;AAEDuC,EAAAA,WAAW,CAACC,IAAZ,GAAmBhB,eAAnB,CAAA;;AAEAe,EAAAA,WAAW,CAACE,GAAZ,GAAkB;AAChB;AACA/D,EAAAA,GAFgB,EAGT;AACP,IAAI0C,IAAAA,eAAe,YAAYX,SAA/B,EAA0C;AACxC;AACA,MAAA,OAAOQ,kBAAkB,CAAC;AACxBV,QAAAA,KAAK,EAAEa,eADiB;AAExB1C,QAAAA,GAAG,EAAHA,GAFwB;AAGxBC,QAAAA,SAAS,EAATA,SAAAA;AAHwB,OAAD,CAAzB,CAAA;AAKD,KAAA;;AACD,IAAMF,IAAAA,QAAQ,GAAG2C,eAAjB,CAAA;;AAEA,IAAI,IAAA;AACF,MAAA,OAAO5C,WAAW,CAACC,QAAD,EAAWC,GAAX,EAAgBC,SAAhB,CAAlB,CAAA;AACD,KAFD,CAEE,OAAO4B,KAAP,EAAc;AACd,MAAOe,OAAAA,6BAA6B,CAClC5C,GADkC,EAElCgC,aAAa,CAACC,eAFoB,EAGjCJ,KAAe,CAACtB,OAHiB,CAApC,CAAA;AAKD,KAAA;AACF,GAvBD,CAAA;;AAyBA,EAAA,OAAOsD,WAAP,CAAA;AACD;;;;"}
|
|
@@ -1,213 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function resolvePath(messages, key, namespace) {
|
|
9
|
-
if (!messages) {
|
|
10
|
-
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages available at `" + namespace + "`." : undefined);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
var message = messages;
|
|
14
|
-
key.split('.').forEach(function (part) {
|
|
15
|
-
var next = message[part];
|
|
16
|
-
|
|
17
|
-
if (part == null || next == null) {
|
|
18
|
-
throw new Error(process.env.NODE_ENV !== 'production' ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
message = next;
|
|
22
|
-
});
|
|
23
|
-
return message;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function prepareTranslationValues(values) {
|
|
27
|
-
if (Object.keys(values).length === 0) return undefined; // Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
28
|
-
|
|
29
|
-
var transformedValues = {};
|
|
30
|
-
Object.keys(values).forEach(function (key) {
|
|
31
|
-
var index = 0;
|
|
32
|
-
var value = values[key];
|
|
33
|
-
var transformed;
|
|
34
|
-
|
|
35
|
-
if (typeof value === 'function') {
|
|
36
|
-
transformed = function transformed(chunks) {
|
|
37
|
-
var result = value(chunks);
|
|
38
|
-
return isValidElement(result) ? cloneElement(result, {
|
|
39
|
-
key: key + index++
|
|
40
|
-
}) : result;
|
|
41
|
-
};
|
|
42
|
-
} else {
|
|
43
|
-
transformed = value;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
transformedValues[key] = transformed;
|
|
47
|
-
});
|
|
48
|
-
return transformedValues;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getMessagesOrError(_ref) {
|
|
52
|
-
var messages = _ref.messages,
|
|
53
|
-
namespace = _ref.namespace,
|
|
54
|
-
_ref$onError = _ref.onError,
|
|
55
|
-
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
if (!messages) {
|
|
59
|
-
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages were configured on the provider." : undefined);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
63
|
-
|
|
64
|
-
if (!retrievedMessages) {
|
|
65
|
-
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return retrievedMessages;
|
|
69
|
-
} catch (error) {
|
|
70
|
-
var intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
71
|
-
onError(intlError);
|
|
72
|
-
return intlError;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function createBaseTranslator(_ref2) {
|
|
76
|
-
var cachedFormatsByLocale = _ref2.cachedFormatsByLocale,
|
|
77
|
-
defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
78
|
-
globalFormats = _ref2.formats,
|
|
79
|
-
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
80
|
-
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
81
|
-
locale = _ref2.locale,
|
|
82
|
-
messagesOrError = _ref2.messagesOrError,
|
|
83
|
-
namespace = _ref2.namespace,
|
|
84
|
-
onError = _ref2.onError,
|
|
85
|
-
timeZone = _ref2.timeZone;
|
|
86
|
-
|
|
87
|
-
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
88
|
-
var error = new IntlError(code, message);
|
|
89
|
-
onError(error);
|
|
90
|
-
return getMessageFallback({
|
|
91
|
-
error: error,
|
|
92
|
-
key: key,
|
|
93
|
-
namespace: namespace
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function translateBaseFn(
|
|
98
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
99
|
-
key,
|
|
100
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
101
|
-
values,
|
|
102
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
103
|
-
formats) {
|
|
104
|
-
var _cachedFormatsByLocal;
|
|
105
|
-
|
|
106
|
-
if (messagesOrError instanceof IntlError) {
|
|
107
|
-
// We have already warned about this during render
|
|
108
|
-
return getMessageFallback({
|
|
109
|
-
error: messagesOrError,
|
|
110
|
-
key: key,
|
|
111
|
-
namespace: namespace
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
var messages = messagesOrError;
|
|
116
|
-
var cacheKey = [namespace, key].filter(function (part) {
|
|
117
|
-
return part != null;
|
|
118
|
-
}).join('.');
|
|
119
|
-
var messageFormat;
|
|
120
|
-
|
|
121
|
-
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
122
|
-
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
123
|
-
} else {
|
|
124
|
-
var message;
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
message = resolvePath(messages, key, namespace);
|
|
128
|
-
} catch (error) {
|
|
129
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (typeof message === 'object') {
|
|
133
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INSUFFICIENT_PATH, process.env.NODE_ENV !== 'production' ? "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." : undefined);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
138
|
-
} catch (error) {
|
|
139
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (cachedFormatsByLocale) {
|
|
143
|
-
if (!cachedFormatsByLocale[locale]) {
|
|
144
|
-
cachedFormatsByLocale[locale] = {};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
var formattedMessage = messageFormat.format( // @ts-ignore `intl-messageformat` expects a different format
|
|
153
|
-
// for rich text elements since a recent minor update. This
|
|
154
|
-
// needs to be evaluated in detail, possibly also in regards
|
|
155
|
-
// to be able to format to parts.
|
|
156
|
-
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
157
|
-
|
|
158
|
-
if (formattedMessage == null) {
|
|
159
|
-
throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
160
|
-
} // Limit the function signature to return strings or React elements
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return isValidElement(formattedMessage) || // Arrays of React elements
|
|
164
|
-
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
165
|
-
} catch (error) {
|
|
166
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function translateFn(
|
|
171
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
172
|
-
key,
|
|
173
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
174
|
-
values,
|
|
175
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
176
|
-
formats) {
|
|
177
|
-
var result = translateBaseFn(key, values, formats);
|
|
178
|
-
|
|
179
|
-
if (typeof result !== 'string') {
|
|
180
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, process.env.NODE_ENV !== 'production' ? "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." : undefined);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return result;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
translateFn.rich = translateBaseFn;
|
|
187
|
-
|
|
188
|
-
translateFn.raw = function (
|
|
189
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
190
|
-
key) {
|
|
191
|
-
if (messagesOrError instanceof IntlError) {
|
|
192
|
-
// We have already warned about this during render
|
|
193
|
-
return getMessageFallback({
|
|
194
|
-
error: messagesOrError,
|
|
195
|
-
key: key,
|
|
196
|
-
namespace: namespace
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
var messages = messagesOrError;
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
return resolvePath(messages, key, namespace);
|
|
204
|
-
} catch (error) {
|
|
205
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
return translateFn;
|
|
1
|
+
/**
|
|
2
|
+
* For the strictly typed messages to work we have to wrap the namespace into
|
|
3
|
+
* a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
|
|
4
|
+
*/
|
|
5
|
+
function resolveNamespace(namespace, namespacePrefix) {
|
|
6
|
+
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
210
7
|
}
|
|
211
8
|
|
|
212
|
-
export {
|
|
9
|
+
export { resolveNamespace as default };
|
|
213
10
|
//# sourceMappingURL=use-intl.esm7.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm7.js","sources":["../../src/core/createBaseTranslator.tsx"],"sourcesContent":["import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray\n} from 'react';\nimport AbstractIntlMessages from './AbstractIntlMessages';\nimport Formats from './Formats';\nimport {InitializedIntlConfiguration} from './IntlConfiguration';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\nfunction resolvePath(\n messages: AbstractIntlMessages | undefined,\n key: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages available at \\`${namespace}\\`.`\n : undefined\n );\n }\n\n let message = messages;\n\n key.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Could not resolve \\`${key}\\` in ${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }.`\n : undefined\n );\n }\n\n message = next;\n });\n\n return message;\n}\n\nfunction prepareTranslationValues(values: RichTranslationValues) {\n if (Object.keys(values).length === 0) return undefined;\n\n // Workaround for https://github.com/formatjs/formatjs/issues/1467\n const transformedValues: RichTranslationValues = {};\n Object.keys(values).forEach((key) => {\n let index = 0;\n const value = values[key];\n\n let transformed;\n if (typeof value === 'function') {\n transformed = (chunks: ReactNode) => {\n const result = value(chunks);\n\n return isValidElement(result)\n ? cloneElement(result, {key: key + index++})\n : result;\n };\n } else {\n transformed = value;\n }\n\n transformedValues[key] = transformed;\n });\n\n return transformedValues;\n}\n\nexport function getMessagesOrError<Messages extends AbstractIntlMessages>({\n messages,\n namespace,\n onError = defaultOnError\n}: {\n messages: Messages;\n namespace?: string;\n onError?(error: IntlError): void;\n}) {\n try {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages were configured on the provider.`\n : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(messages, namespace)\n : messages;\n\n if (!retrievedMessages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages for namespace \\`${namespace}\\` found.`\n : undefined\n );\n }\n\n return retrievedMessages;\n } catch (error) {\n const intlError = new IntlError(\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n onError(intlError);\n return intlError;\n }\n}\n\nexport type CreateBaseTranslatorProps<Messages> =\n InitializedIntlConfiguration & {\n cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;\n defaultTranslationValues?: RichTranslationValues;\n namespace?: string;\n messagesOrError: Messages | IntlError;\n };\n\nexport default function createBaseTranslator<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>({\n cachedFormatsByLocale,\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback = defaultGetMessageFallback,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n}: CreateBaseTranslatorProps<Messages>) {\n function getFallbackFromErrorAndNotify(\n key: string,\n code: IntlErrorCode,\n message?: string\n ) {\n const error = new IntlError(code, message);\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n function translateBaseFn(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string,\n /** Key value pairs for values to interpolate into the message. */\n values?: RichTranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n const cacheKey = [namespace, key].filter((part) => part != null).join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale?.[locale][cacheKey];\n } else {\n let message;\n try {\n message = resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n\n if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n process.env.NODE_ENV !== 'production'\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (cachedFormatsByLocale) {\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n }\n\n try {\n const formattedMessage = messageFormat.format(\n // @ts-ignore `intl-messageformat` expects a different format\n // for rich text elements since a recent minor update. This\n // needs to be evaluated in detail, possibly also in regards\n // to be able to format to parts.\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Unable to format \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n }`\n : undefined\n );\n }\n\n // Limit the function signature to return strings or React elements\n return isValidElement(formattedMessage) ||\n // Arrays of React elements\n Array.isArray(formattedMessage) ||\n typeof formattedMessage === 'string'\n ? formattedMessage\n : String(formattedMessage);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.FORMATTING_ERROR,\n (error as Error).message\n );\n }\n }\n\n function translateFn<\n TargetKey extends MessageKeys<\n NestedValueOf<Messages, NestedKey>,\n NestedKeyOf<NestedValueOf<Messages, NestedKey>>\n >\n >(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: TargetKey,\n /** Key value pairs for values to interpolate into the message. */\n values?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const result = translateBaseFn(key, values, formats);\n\n if (typeof result !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n process.env.NODE_ENV !== 'production'\n ? `The message \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n } didn't resolve to a string. If you want to format rich text, use \\`t.rich\\` instead.`\n : undefined\n );\n }\n\n return result;\n }\n\n translateFn.rich = translateBaseFn;\n\n translateFn.raw = (\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string\n ): any => {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n try {\n return resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n };\n\n return translateFn;\n}\n"],"names":["resolvePath","messages","key","namespace","Error","process","env","NODE_ENV","undefined","message","split","forEach","part","next","prepareTranslationValues","values","Object","keys","length","transformedValues","index","value","transformed","chunks","result","isValidElement","cloneElement","getMessagesOrError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","createBaseTranslator","cachedFormatsByLocale","defaultTranslationValues","globalFormats","formats","getMessageFallback","defaultGetMessageFallback","locale","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","cacheKey","filter","join","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","convertFormatsToIntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAmBA,SAASA,WAAT,CACEC,QADF,EAEEC,GAFF,EAGEC,SAHF,EAGoB;AAElB,EAAI,IAAA,CAACF,QAAL,EAAe;AACb,IAAA,MAAM,IAAIG,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GAAA,4BAAA,GACkCJ,SADlC,GAAA,IAAA,GAEIK,SAHA,CAAN,CAAA;AAKD,GAAA;;AAED,EAAIC,IAAAA,O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AAA;AAHwB,OAAD,CAAzB,CAAA;AAKD,KAAA;;AACD,IAAMF,IAAAA,QAAQ,GAAG2C,eAAjB,CAAA;;AAEA,IAAI,IAAA;AACF,MAAA,OAAO5C,WAAW,CAACC,QAAD,EAAWC,GAAX,EAAgBC,SAAhB,CAAlB,CAAA;AACD,KAFD,CAEE,OAAO4B,KAAP,EAAc;AACd,MAAOe,OAAAA,6BAA6B,CAClC5C,GADkC,EAElCgC,aAAa,CAACC,eAFoB,EAGjCJ,KAAe,CAACtB,OAHiB,CAApC,CAAA;AAKD,KAAA;AACF,GAvBD,CAAA;;AAyBA,EAAA,OAAOsD,WAAP,CAAA;AACD;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.esm7.js","sources":["../../src/core/resolveNamespace.tsx"],"sourcesContent":["/**\n * For the strictly typed messages to work we have to wrap the namespace into\n * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045\n */\nexport default function resolveNamespace(\n namespace: string,\n namespacePrefix: string\n) {\n return namespace === namespacePrefix\n ? undefined\n : namespace.slice((namespacePrefix + '.').length);\n}\n"],"names":["resolveNamespace","namespace","namespacePrefix","undefined","slice","length"],"mappings":"AAAA;;;AAGG;AACW,SAAUA,gBAAV,CACZC,SADY,EAEZC,eAFY,EAEW;AAEvB,EAAA,OAAOD,SAAS,KAAKC,eAAd,GACHC,SADG,GAEHF,SAAS,CAACG,KAAV,CAAgB,CAACF,eAAe,GAAG,GAAnB,EAAwBG,MAAxC,CAFJ,CAAA;AAGD;;;;"}
|
|
@@ -1,10 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
2
|
+
|
|
3
|
+
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
4
|
+
Object.entries(messages).forEach(function (_ref) {
|
|
5
|
+
var key = _ref[0],
|
|
6
|
+
messageOrMessages = _ref[1];
|
|
7
|
+
|
|
8
|
+
if (key.includes('.')) {
|
|
9
|
+
var keyLabel = key;
|
|
10
|
+
if (parentPath) keyLabel += " (at " + parentPath + ")";
|
|
11
|
+
invalidKeyLabels.push(keyLabel);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (messageOrMessages != null && typeof messageOrMessages === 'object') {
|
|
15
|
+
validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
|
|
16
|
+
return part != null;
|
|
17
|
+
}).join('.'));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function validateMessages(messages, onError) {
|
|
23
|
+
var invalidKeyLabels = [];
|
|
24
|
+
validateMessagesSegment(messages, invalidKeyLabels);
|
|
25
|
+
|
|
26
|
+
if (invalidKeyLabels.length > 0) {
|
|
27
|
+
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 " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ')));
|
|
28
|
+
}
|
|
7
29
|
}
|
|
8
30
|
|
|
9
|
-
export {
|
|
31
|
+
export { validateMessages as default };
|
|
10
32
|
//# sourceMappingURL=use-intl.esm8.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm8.js","sources":["../../src/core/
|
|
1
|
+
{"version":3,"file":"use-intl.esm8.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n `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 ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}`\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY"],"mappings":";;AAGA,SAASA,uBAAT,CACEC,QADF,EAEEC,gBAFF,EAGEC,UAHF,EAGqB;AAEnBC,EAAAA,MAAM,CAACC,OAAP,CAAeJ,QAAf,CAAyBK,CAAAA,OAAzB,CAAiC,UAA6B,IAAA,EAAA;AAAA,IAAA,IAA3BC,GAA2B,GAAA,IAAA,CAAA,CAAA,CAAA;AAAA,QAAtBC,iBAAsB,GAAA,IAAA,CAAA,CAAA,CAAA,CAAA;;AAC5D,IAAA,IAAID,GAAG,CAACE,QAAJ,CAAa,GAAb,CAAJ,EAAuB;AACrB,MAAIC,IAAAA,QAAQ,GAAGH,GAAf,CAAA;AACA,MAAA,IAAIJ,UAAJ,EAAgBO,QAAQ,IAAA,OAAA,GAAYP,UAAZ,GAAR,GAAA,CAAA;AAChBD,MAAAA,gBAAgB,CAACS,IAAjB,CAAsBD,QAAtB,CAAA,CAAA;AACD,KAAA;;AAED,IAAIF,IAAAA,iBAAiB,IAAI,IAArB,IAA6B,OAAOA,iBAAP,KAA6B,QAA9D,EAAwE;AACtER,MAAAA,uBAAuB,CACrBQ,iBADqB,EAErBN,gBAFqB,EAGrB,CAACC,UAAD,EAAaI,GAAb,CAAA,CAAkBK,MAAlB,CAAyB,UAACC,IAAD,EAAA;AAAA,QAAUA,OAAAA,IAAI,IAAI,IAAlB,CAAA;AAAA,OAAzB,CAAiDC,CAAAA,IAAjD,CAAsD,GAAtD,CAHqB,CAAvB,CAAA;AAKD,KAAA;AACF,GAdD,CAAA,CAAA;AAeD,CAAA;;AAEa,SAAUC,gBAAV,CACZd,QADY,EAEZe,OAFY,EAEuB;AAEnC,EAAMd,IAAAA,gBAAgB,GAAkB,EAAxC,CAAA;AACAF,EAAAA,uBAAuB,CAACC,QAAD,EAAWC,gBAAX,CAAvB,CAAA;;AAEA,EAAA,IAAIA,gBAAgB,CAACe,MAAjB,GAA0B,CAA9B,EAAiC;AAC/BD,IAAAA,OAAO,CACL,IAAIE,SAAJ,CACEC,aAAa,CAACC,WADhB,EAAA,2JAAA,IAGIlB,gBAAgB,CAACe,MAAjB,KAA4B,CAA5B,GAAgC,KAAhC,GAAwC,MAH5C,CAIOf,GAAAA,IAAAA,GAAAA,gBAAgB,CAACY,IAAjB,CAAsB,IAAtB,CAJP,CADK,CAAP,CAAA;AAQD,GAAA;AACF;;;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useRef, useMemo } from 'react';
|
|
2
|
-
import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.
|
|
3
|
-
import resolveNamespace from '../core/use-intl.
|
|
2
|
+
import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm6.js';
|
|
3
|
+
import resolveNamespace from '../core/use-intl.esm7.js';
|
|
4
4
|
import useIntlContext from './use-intl.esm9.js';
|
|
5
5
|
|
|
6
6
|
function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
@@ -1,8 +1,25 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import createIntl from '../core/use-intl.esm3.js';
|
|
1
3
|
import useIntlContext from './use-intl.esm9.js';
|
|
2
4
|
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
+
function useIntl() {
|
|
6
|
+
var _useIntlContext = useIntlContext(),
|
|
7
|
+
formats = _useIntlContext.formats,
|
|
8
|
+
locale = _useIntlContext.locale,
|
|
9
|
+
globalNow = _useIntlContext.now,
|
|
10
|
+
onError = _useIntlContext.onError,
|
|
11
|
+
timeZone = _useIntlContext.timeZone;
|
|
12
|
+
|
|
13
|
+
return useMemo(function () {
|
|
14
|
+
return createIntl({
|
|
15
|
+
formats: formats,
|
|
16
|
+
locale: locale,
|
|
17
|
+
now: globalNow,
|
|
18
|
+
onError: onError,
|
|
19
|
+
timeZone: timeZone
|
|
20
|
+
});
|
|
21
|
+
}, [formats, globalNow, locale, onError, timeZone]);
|
|
5
22
|
}
|
|
6
23
|
|
|
7
|
-
export {
|
|
24
|
+
export { useIntl as default };
|
|
8
25
|
//# sourceMappingURL=use-intl.esm2.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm2.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm2.js","sources":["../../src/react/useIntl.tsx"],"sourcesContent":["import {useMemo} from 'react';\nimport createIntl from '../core/createIntl';\nimport useIntlContext from './useIntlContext';\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n return useMemo(\n () =>\n createIntl({\n formats,\n locale,\n now: globalNow,\n onError,\n timeZone\n }),\n [formats, globalNow, locale, onError, timeZone]\n );\n}\n"],"names":["useIntl","useIntlContext","formats","locale","globalNow","now","onError","timeZone","useMemo","createIntl"],"mappings":";;;;AAIc,SAAUA,OAAV,GAAiB;AAC7B,EAAA,IAAA,eAAA,GAA6DC,cAAc,EAA3E;AAAA,MAAOC,OAAP,mBAAOA,OAAP;AAAA,MAAgBC,MAAhB,mBAAgBA,MAAhB;AAAA,MAA6BC,SAA7B,mBAAwBC,GAAxB;AAAA,MAAwCC,OAAxC,mBAAwCA,OAAxC;AAAA,MAAiDC,QAAjD,mBAAiDA,QAAjD,CAAA;;AAEA,EAAA,OAAOC,OAAO,CACZ,YAAA;AAAA,IAAA,OACEC,UAAU,CAAC;AACTP,MAAAA,OAAO,EAAPA,OADS;AAETC,MAAAA,MAAM,EAANA,MAFS;AAGTE,MAAAA,GAAG,EAAED,SAHI;AAITE,MAAAA,OAAO,EAAPA,OAJS;AAKTC,MAAAA,QAAQ,EAARA,QAAAA;AALS,KAAD,CADZ,CAAA;AAAA,GADY,EASZ,CAACL,OAAD,EAAUE,SAAV,EAAqBD,MAArB,EAA6BG,OAA7B,EAAsCC,QAAtC,CATY,CAAd,CAAA;AAWD;;;;"}
|
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
import createIntl from '../core/use-intl.esm3.js';
|
|
3
1
|
import useIntlContext from './use-intl.esm9.js';
|
|
2
|
+
import useTranslationsImpl from './use-intl.esm10.js';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
6
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
7
|
+
*
|
|
8
|
+
* If no namespace is provided, all available messages are returned.
|
|
9
|
+
* The namespace can also indicate nesting by using a dot
|
|
10
|
+
* (e.g. `namespace.Component`).
|
|
11
|
+
*/
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
},
|
|
13
|
+
function useTranslations(namespace) {
|
|
14
|
+
var context = useIntlContext();
|
|
15
|
+
var messages = context.messages; // We have to wrap the actual hook so the type inference for the optional
|
|
16
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
17
|
+
// The prefix ("!") is arbitrary.
|
|
18
|
+
|
|
19
|
+
return useTranslationsImpl({
|
|
20
|
+
'!': messages
|
|
21
|
+
}, // @ts-ignore
|
|
22
|
+
namespace ? "!." + namespace : '!', '!');
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export {
|
|
25
|
+
export { useTranslations as default };
|
|
25
26
|
//# sourceMappingURL=use-intl.esm3.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm3.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm3.js","sources":["../../src/react/useTranslations.tsx"],"sourcesContent":["import {ReactElement, ReactNodeArray} from 'react';\nimport Formats from '../core/Formats';\nimport TranslationValues, {\n RichTranslationValues\n} from '../core/TranslationValues';\nimport MessageKeys from '../core/utils/MessageKeys';\nimport NamespaceKeys from '../core/utils/NamespaceKeys';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport NestedValueOf from '../core/utils/NestedValueOf';\nimport useIntlContext from './useIntlContext';\nimport useTranslationsImpl from './useTranslationsImpl';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>(\n namespace?: NestedKey\n): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: RichTranslationValues,\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n const context = useIntlContext();\n const messages = context.messages as IntlMessages;\n\n // We have to wrap the actual hook so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return useTranslationsImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {'!': messages},\n // @ts-ignore\n namespace ? `!.${namespace}` : '!',\n '!'\n );\n}\n"],"names":["useTranslations","namespace","context","useIntlContext","messages","useTranslationsImpl"],"mappings":";;;AAYA;;;;;;;AAOG;;AACqB,SAAAA,eAAA,CAMtBC,SANsB,EAMD;AA6DrB,EAAMC,IAAAA,OAAO,GAAGC,cAAc,EAA9B,CAAA;AACA,EAAA,IAAMC,QAAQ,GAAGF,OAAO,CAACE,QAAzB,CA9DqB;AAiErB;AACA;;AACA,EAAA,OAAOC,mBAAmB,CAIxB;AAAC,IAAKD,GAAAA,EAAAA,QAAAA;AAAN,GAJwB;AAMxBH,EAAAA,SAAS,GAAQA,IAAAA,GAAAA,SAAR,GAAsB,GANP,EAOxB,GAPwB,CAA1B,CAAA;AASD;;;;"}
|
|
@@ -1,50 +1,8 @@
|
|
|
1
|
-
import { useState, useEffect } from 'react';
|
|
2
1
|
import useIntlContext from './use-intl.esm9.js';
|
|
3
2
|
|
|
4
|
-
function
|
|
5
|
-
return
|
|
3
|
+
function useLocale() {
|
|
4
|
+
return useIntlContext().locale;
|
|
6
5
|
}
|
|
7
|
-
/**
|
|
8
|
-
* Reading the current date via `new Date()` in components should be avoided, as
|
|
9
|
-
* it causes components to be impure and can lead to flaky tests. Instead, this
|
|
10
|
-
* hook can be used.
|
|
11
|
-
*
|
|
12
|
-
* By default, it returns the time when the component mounts. If `updateInterval`
|
|
13
|
-
* is specified, the value will be updated based on the interval.
|
|
14
|
-
*
|
|
15
|
-
* You can however also return a static value from this hook, if you
|
|
16
|
-
* configure the `now` parameter on the context provider. Note however,
|
|
17
|
-
* that if `updateInterval` is configured in this case, the component
|
|
18
|
-
* will initialize with the global value, but will afterwards update
|
|
19
|
-
* continuously based on the interval.
|
|
20
|
-
*
|
|
21
|
-
* For unit tests, this can be mocked to a constant value. For end-to-end
|
|
22
|
-
* testing, an environment parameter can be passed to the `now` parameter
|
|
23
|
-
* of the provider to mock this to a static value.
|
|
24
|
-
*/
|
|
25
6
|
|
|
26
|
-
|
|
27
|
-
function useNow(options) {
|
|
28
|
-
var updateInterval = options == null ? void 0 : options.updateInterval;
|
|
29
|
-
|
|
30
|
-
var _useIntlContext = useIntlContext(),
|
|
31
|
-
globalNow = _useIntlContext.now;
|
|
32
|
-
|
|
33
|
-
var _useState = useState(globalNow || getNow()),
|
|
34
|
-
now = _useState[0],
|
|
35
|
-
setNow = _useState[1];
|
|
36
|
-
|
|
37
|
-
useEffect(function () {
|
|
38
|
-
if (!updateInterval) return;
|
|
39
|
-
var intervalId = setInterval(function () {
|
|
40
|
-
setNow(getNow());
|
|
41
|
-
}, updateInterval);
|
|
42
|
-
return function () {
|
|
43
|
-
clearInterval(intervalId);
|
|
44
|
-
};
|
|
45
|
-
}, [globalNow, updateInterval]);
|
|
46
|
-
return now;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export { useNow as default };
|
|
7
|
+
export { useLocale as default };
|
|
50
8
|
//# sourceMappingURL=use-intl.esm4.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm4.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm4.js","sources":["../../src/react/useLocale.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n"],"names":["useLocale","useIntlContext","locale"],"mappings":";;AAEc,SAAUA,SAAV,GAAmB;AAC/B,EAAOC,OAAAA,cAAc,GAAGC,MAAxB,CAAA;AACD;;;;"}
|
|
@@ -1,26 +1,8 @@
|
|
|
1
1
|
import useIntlContext from './use-intl.esm9.js';
|
|
2
|
-
import useTranslationsImpl from './use-intl.esm10.js';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
7
|
-
*
|
|
8
|
-
* If no namespace is provided, all available messages are returned.
|
|
9
|
-
* The namespace can also indicate nesting by using a dot
|
|
10
|
-
* (e.g. `namespace.Component`).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
function useTranslations(namespace) {
|
|
14
|
-
var context = useIntlContext();
|
|
15
|
-
var messages = context.messages; // We have to wrap the actual hook so the type inference for the optional
|
|
16
|
-
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
17
|
-
// The prefix ("!") is arbitrary.
|
|
18
|
-
|
|
19
|
-
return useTranslationsImpl({
|
|
20
|
-
'!': messages
|
|
21
|
-
}, // @ts-ignore
|
|
22
|
-
namespace ? "!." + namespace : '!', '!');
|
|
3
|
+
function useTimeZone() {
|
|
4
|
+
return useIntlContext().timeZone;
|
|
23
5
|
}
|
|
24
6
|
|
|
25
|
-
export {
|
|
7
|
+
export { useTimeZone as default };
|
|
26
8
|
//# sourceMappingURL=use-intl.esm5.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/react/useTimeZone.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["useTimeZone","useIntlContext","timeZone"],"mappings":";;AAEc,SAAUA,WAAV,GAAqB;AACjC,EAAOC,OAAAA,cAAc,GAAGC,QAAxB,CAAA;AACD;;;;"}
|
|
@@ -1,8 +1,50 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
1
2
|
import useIntlContext from './use-intl.esm9.js';
|
|
2
3
|
|
|
3
|
-
function
|
|
4
|
-
return
|
|
4
|
+
function getNow() {
|
|
5
|
+
return new Date();
|
|
5
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Reading the current date via `new Date()` in components should be avoided, as
|
|
9
|
+
* it causes components to be impure and can lead to flaky tests. Instead, this
|
|
10
|
+
* hook can be used.
|
|
11
|
+
*
|
|
12
|
+
* By default, it returns the time when the component mounts. If `updateInterval`
|
|
13
|
+
* is specified, the value will be updated based on the interval.
|
|
14
|
+
*
|
|
15
|
+
* You can however also return a static value from this hook, if you
|
|
16
|
+
* configure the `now` parameter on the context provider. Note however,
|
|
17
|
+
* that if `updateInterval` is configured in this case, the component
|
|
18
|
+
* will initialize with the global value, but will afterwards update
|
|
19
|
+
* continuously based on the interval.
|
|
20
|
+
*
|
|
21
|
+
* For unit tests, this can be mocked to a constant value. For end-to-end
|
|
22
|
+
* testing, an environment parameter can be passed to the `now` parameter
|
|
23
|
+
* of the provider to mock this to a static value.
|
|
24
|
+
*/
|
|
6
25
|
|
|
7
|
-
|
|
26
|
+
|
|
27
|
+
function useNow(options) {
|
|
28
|
+
var updateInterval = options == null ? void 0 : options.updateInterval;
|
|
29
|
+
|
|
30
|
+
var _useIntlContext = useIntlContext(),
|
|
31
|
+
globalNow = _useIntlContext.now;
|
|
32
|
+
|
|
33
|
+
var _useState = useState(globalNow || getNow()),
|
|
34
|
+
now = _useState[0],
|
|
35
|
+
setNow = _useState[1];
|
|
36
|
+
|
|
37
|
+
useEffect(function () {
|
|
38
|
+
if (!updateInterval) return;
|
|
39
|
+
var intervalId = setInterval(function () {
|
|
40
|
+
setNow(getNow());
|
|
41
|
+
}, updateInterval);
|
|
42
|
+
return function () {
|
|
43
|
+
clearInterval(intervalId);
|
|
44
|
+
};
|
|
45
|
+
}, [globalNow, updateInterval]);
|
|
46
|
+
return now;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { useNow as default };
|
|
8
50
|
//# sourceMappingURL=use-intl.esm6.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/react/useNow.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n"],"names":["getNow","Date","useNow","options","updateInterval","useIntlContext","globalNow","now","useState","setNow","useEffect","intervalId","setInterval","clearInterval"],"mappings":";;;AAOA,SAASA,MAAT,GAAe;AACb,EAAO,OAAA,IAAIC,IAAJ,EAAP,CAAA;AACD,CAAA;AAED;;;;;;;;;;;;;;;;;AAiBG;;;AACqB,SAAAC,MAAA,CAAOC,OAAP,EAAwB;AAC9C,EAAA,IAAMC,cAAc,GAAGD,OAAH,IAAGA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,OAAO,CAAEC,cAAhC,CAAA;;AAEA,EAAA,IAAA,eAAA,GAAyBC,cAAc,EAAvC;AAAA,MAAYC,SAAZ,mBAAOC,GAAP,CAAA;;AACA,EAAA,IAAA,SAAA,GAAsBC,QAAQ,CAACF,SAAS,IAAIN,MAAM,EAApB,CAA9B;AAAA,MAAOO,GAAP,GAAA,SAAA,CAAA,CAAA,CAAA;AAAA,MAAYE,MAAZ,GAAA,SAAA,CAAA,CAAA,CAAA,CAAA;;AAEAC,EAAAA,SAAS,CAAC,YAAK;AACb,IAAI,IAAA,CAACN,cAAL,EAAqB,OAAA;AAErB,IAAA,IAAMO,UAAU,GAAGC,WAAW,CAAC,YAAK;AAClCH,MAAAA,MAAM,CAACT,MAAM,EAAP,CAAN,CAAA;AACD,KAF6B,EAE3BI,cAF2B,CAA9B,CAAA;AAIA,IAAA,OAAO,YAAK;AACVS,MAAAA,aAAa,CAACF,UAAD,CAAb,CAAA;AACD,KAFD,CAAA;AAGD,GAVQ,EAUN,CAACL,SAAD,EAAYF,cAAZ,CAVM,CAAT,CAAA;AAYA,EAAA,OAAOG,GAAP,CAAA;AACD;;;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
-
import { defaultOnError, defaultGetMessageFallback } from '../core/use-intl.
|
|
3
|
-
import validateMessages from '../core/use-intl.
|
|
2
|
+
import { defaultOnError, defaultGetMessageFallback } from '../core/use-intl.esm5.js';
|
|
3
|
+
import validateMessages from '../core/use-intl.esm8.js';
|
|
4
4
|
|
|
5
5
|
var _excluded = ["getMessageFallback", "messages", "onError"];
|
|
6
6
|
/**
|
package/dist/use-intl.esm.js
CHANGED
|
@@ -2,9 +2,9 @@ export { default as IntlError, IntlErrorCode } from './core/use-intl.esm.js';
|
|
|
2
2
|
export { default as createTranslator } from './core/use-intl.esm2.js';
|
|
3
3
|
export { default as createIntl } from './core/use-intl.esm3.js';
|
|
4
4
|
export { default as IntlProvider } from './react/use-intl.esm.js';
|
|
5
|
-
export { default as useTranslations } from './react/use-intl.
|
|
6
|
-
export { default as useIntl } from './react/use-intl.
|
|
7
|
-
export { default as useLocale } from './react/use-intl.
|
|
8
|
-
export { default as useNow } from './react/use-intl.
|
|
9
|
-
export { default as useTimeZone } from './react/use-intl.
|
|
5
|
+
export { default as useTranslations } from './react/use-intl.esm3.js';
|
|
6
|
+
export { default as useIntl } from './react/use-intl.esm2.js';
|
|
7
|
+
export { default as useLocale } from './react/use-intl.esm4.js';
|
|
8
|
+
export { default as useNow } from './react/use-intl.esm6.js';
|
|
9
|
+
export { default as useTimeZone } from './react/use-intl.esm5.js';
|
|
10
10
|
//# sourceMappingURL=use-intl.esm.js.map
|
package/package.json
CHANGED