use-intl 2.4.1-alpha.2 → 2.4.1-alpha.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-intl",
|
|
3
|
-
"version": "2.4.1-alpha.
|
|
3
|
+
"version": "2.4.1-alpha.3",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "Jan Amann <jan@amann.me>",
|
|
6
6
|
"description": "Minimal, but complete solution for managing internationalization in React apps.",
|
|
@@ -19,6 +19,9 @@
|
|
|
19
19
|
"main": "dist/index.js",
|
|
20
20
|
"module": "dist/use-intl.esm.js",
|
|
21
21
|
"typings": "dist/index.d.ts",
|
|
22
|
+
"jest": {
|
|
23
|
+
"testEnvironment": "jsdom"
|
|
24
|
+
},
|
|
22
25
|
"files": [
|
|
23
26
|
"dist",
|
|
24
27
|
"src"
|
|
@@ -56,5 +59,5 @@
|
|
|
56
59
|
"engines": {
|
|
57
60
|
"node": ">=10"
|
|
58
61
|
},
|
|
59
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "20f7811f70b0a1ae8b0d338b1a01269b8307a3cc"
|
|
60
63
|
}
|
package/src/GlobalMessages.tsx
CHANGED
package/src/useTranslations.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import IntlError, { IntlErrorCode } from './IntlError';
|
|
2
2
|
import useIntlContext from './useIntlContext';
|
|
3
3
|
import useTranslationsImpl from './useTranslationsImpl';
|
|
4
4
|
import NamespaceKeys from './utils/NamespaceKeys';
|
|
@@ -15,21 +15,32 @@ import NestedKeyOf from './utils/NestedKeyOf';
|
|
|
15
15
|
export default function useTranslations<
|
|
16
16
|
NestedKey extends NamespaceKeys<GlobalMessages, NestedKeyOf<GlobalMessages>>
|
|
17
17
|
>(namespace?: NestedKey) {
|
|
18
|
+
const context = useIntlContext()
|
|
19
|
+
|
|
18
20
|
// @ts-ignore
|
|
19
|
-
const messages =
|
|
20
|
-
if (!messages)
|
|
21
|
+
const messages = context.messages as GlobalMessages;
|
|
22
|
+
if (!messages) {
|
|
23
|
+
const intlError = new IntlError(
|
|
24
|
+
IntlErrorCode.MISSING_MESSAGE,
|
|
25
|
+
__DEV__ ? `No messages were configured on the provider.` : undefined
|
|
26
|
+
);
|
|
27
|
+
context.onError(intlError);
|
|
28
|
+
throw intlError;
|
|
29
|
+
}
|
|
21
30
|
|
|
22
31
|
// We have to wrap the actual hook so the type inference for the optional
|
|
23
32
|
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
33
|
+
// The prefix ("!"") is arbitrary, but we have to use some.
|
|
24
34
|
return useTranslationsImpl<
|
|
25
35
|
// @ts-ignore
|
|
26
|
-
{
|
|
36
|
+
{'!': GlobalMessages},
|
|
27
37
|
NamespaceKeys<GlobalMessages, NestedKeyOf<GlobalMessages>> extends NestedKey
|
|
28
|
-
? '
|
|
29
|
-
:
|
|
38
|
+
? '!'
|
|
39
|
+
: `!.${NestedKey}`
|
|
30
40
|
>(
|
|
31
|
-
{
|
|
41
|
+
{'!': messages},
|
|
32
42
|
// @ts-ignore
|
|
33
|
-
namespace ?
|
|
43
|
+
namespace ? `!.${namespace}` : '!',
|
|
44
|
+
'!'
|
|
34
45
|
);
|
|
35
46
|
}
|
|
@@ -81,7 +81,7 @@ function prepareTranslationValues(values: RichTranslationValues) {
|
|
|
81
81
|
export default function useTranslationsImpl<
|
|
82
82
|
Messages extends IntlMessages,
|
|
83
83
|
NestedKey extends NestedKeyOf<Messages>
|
|
84
|
-
>(allMessages: Messages, namespace: NestedKey) {
|
|
84
|
+
>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string) {
|
|
85
85
|
const {
|
|
86
86
|
defaultTranslationValues,
|
|
87
87
|
formats: globalFormats,
|
|
@@ -91,6 +91,13 @@ export default function useTranslationsImpl<
|
|
|
91
91
|
timeZone
|
|
92
92
|
} = useIntlContext();
|
|
93
93
|
|
|
94
|
+
// The `namespacePrefix` is part of the type system.
|
|
95
|
+
// See the comment in the hook invocation.
|
|
96
|
+
allMessages = allMessages[namespacePrefix] as Messages;
|
|
97
|
+
namespace = (namespace === namespacePrefix
|
|
98
|
+
? undefined
|
|
99
|
+
: namespace.slice((namespacePrefix + '.').length)) as NestedKey;
|
|
100
|
+
|
|
94
101
|
const cachedFormatsByLocaleRef = useRef<
|
|
95
102
|
Record<string, Record<string, IntlMessageFormat>>
|
|
96
103
|
>({});
|