use-intl 2.4.1-alpha.2 → 2.4.1-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
1
  import {createContext} from 'react';
2
+ import AbstractIntlMessages from './AbstractIntlMessages';
2
3
  import Formats from './Formats';
3
4
  import IntlError from './IntlError';
4
- import IntlMessages from './IntlMessages';
5
5
  import {RichTranslationValues} from './TranslationValues';
6
6
 
7
7
  export type IntlContextShape = {
8
- messages?: IntlMessages;
8
+ messages?: AbstractIntlMessages;
9
9
  locale: string;
10
10
  formats?: Partial<Formats>;
11
11
  timeZone?: string;
@@ -1,5 +1,5 @@
1
- type IntlMessages = {
2
- [id: string]: IntlMessages | string;
3
- };
1
+ // This module is intended to be overridden
2
+ // by the consumer for optional type safety
4
3
 
5
- export default IntlMessages;
4
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
+ declare interface IntlMessages extends Record<string, any> {}
@@ -1,13 +1,13 @@
1
1
  import React, {ReactNode} from 'react';
2
+ import AbstractIntlMessages from './AbstractIntlMessages';
2
3
  import Formats from './Formats';
3
4
  import IntlContext from './IntlContext';
4
- import IntlMessages from './IntlMessages';
5
5
  import {RichTranslationValues} from './TranslationValues';
6
6
  import {IntlError} from '.';
7
7
 
8
8
  type Props = {
9
9
  /** All messages that will be available in your components. */
10
- messages?: IntlMessages;
10
+ messages?: AbstractIntlMessages;
11
11
  /** A valid Unicode locale tag (e.g. "en" or "en-GB"). */
12
12
  locale: string;
13
13
  /** Global formats can be provided to achieve consistent
package/src/index.tsx CHANGED
@@ -1,6 +1,5 @@
1
1
  export {default as IntlProvider} from './IntlProvider';
2
- export {default as IntlMessages} from './IntlMessages';
3
- // export {default as GlobalMessages} from './GlobalMessages';
2
+ export {default as AbstractIntlMessages} from './AbstractIntlMessages';
4
3
  export {default as useTranslations} from './useTranslations';
5
4
  export {
6
5
  default as TranslationValues,
@@ -1,8 +1,13 @@
1
- // import GlobalMessages from './GlobalMessages';
1
+ import {ReactElement, ReactNodeArray} from 'react';
2
+ import Formats from './Formats';
3
+ import IntlError, {IntlErrorCode} from './IntlError';
4
+ import TranslationValues, {RichTranslationValues} from './TranslationValues';
2
5
  import useIntlContext from './useIntlContext';
3
6
  import useTranslationsImpl from './useTranslationsImpl';
7
+ import MessageKeys from './utils/MessageKeys';
4
8
  import NamespaceKeys from './utils/NamespaceKeys';
5
9
  import NestedKeyOf from './utils/NestedKeyOf';
10
+ import NestedValueOf from './utils/NestedValueOf';
6
11
 
7
12
  /**
8
13
  * Translates messages from the given namespace by using the ICU syntax.
@@ -13,23 +18,114 @@ import NestedKeyOf from './utils/NestedKeyOf';
13
18
  * (e.g. `namespace.Component`).
14
19
  */
15
20
  export default function useTranslations<
16
- NestedKey extends NamespaceKeys<GlobalMessages, NestedKeyOf<GlobalMessages>>
17
- >(namespace?: NestedKey) {
18
- // @ts-ignore
19
- const messages = useIntlContext().messages as GlobalMessages;
20
- if (!messages) throw new Error('TODO')
21
+ NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>>
22
+ >(
23
+ namespace?: NestedKey
24
+ ): // Explicitly defining the return type is necessary as TypeScript would get it wrong
25
+ {
26
+ // Default invocation
27
+ <
28
+ TargetKey extends MessageKeys<
29
+ NestedValueOf<
30
+ {'!': IntlMessages},
31
+ NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
32
+ ? '!'
33
+ : `!.${NestedKey}`
34
+ >,
35
+ NestedKeyOf<
36
+ NestedValueOf<
37
+ {'!': IntlMessages},
38
+ NamespaceKeys<
39
+ IntlMessages,
40
+ NestedKeyOf<IntlMessages>
41
+ > extends NestedKey
42
+ ? '!'
43
+ : `!.${NestedKey}`
44
+ >
45
+ >
46
+ >
47
+ >(
48
+ key: TargetKey,
49
+ values?: TranslationValues,
50
+ formats?: Partial<Formats>
51
+ ): string;
52
+
53
+ // `rich`
54
+ rich<
55
+ TargetKey extends MessageKeys<
56
+ NestedValueOf<
57
+ {'!': IntlMessages},
58
+ NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
59
+ ? '!'
60
+ : `!.${NestedKey}`
61
+ >,
62
+ NestedKeyOf<
63
+ NestedValueOf<
64
+ {'!': IntlMessages},
65
+ NamespaceKeys<
66
+ IntlMessages,
67
+ NestedKeyOf<IntlMessages>
68
+ > extends NestedKey
69
+ ? '!'
70
+ : `!.${NestedKey}`
71
+ >
72
+ >
73
+ >
74
+ >(
75
+ key: TargetKey,
76
+ values?: RichTranslationValues,
77
+ formats?: Partial<Formats>
78
+ ): string | ReactElement | ReactNodeArray;
79
+
80
+ // `raw`
81
+ raw<
82
+ TargetKey extends MessageKeys<
83
+ NestedValueOf<
84
+ {'!': IntlMessages},
85
+ NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
86
+ ? '!'
87
+ : `!.${NestedKey}`
88
+ >,
89
+ NestedKeyOf<
90
+ NestedValueOf<
91
+ {'!': IntlMessages},
92
+ NamespaceKeys<
93
+ IntlMessages,
94
+ NestedKeyOf<IntlMessages>
95
+ > extends NestedKey
96
+ ? '!'
97
+ : `!.${NestedKey}`
98
+ >
99
+ >
100
+ >
101
+ >(
102
+ key: TargetKey
103
+ ): any;
104
+ } {
105
+ const context = useIntlContext();
106
+
107
+ const messages = context.messages as IntlMessages;
108
+ if (!messages) {
109
+ const intlError = new IntlError(
110
+ IntlErrorCode.MISSING_MESSAGE,
111
+ __DEV__ ? `No messages were configured on the provider.` : undefined
112
+ );
113
+ context.onError(intlError);
114
+ throw intlError;
115
+ }
21
116
 
22
117
  // We have to wrap the actual hook so the type inference for the optional
23
118
  // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
119
+ // The prefix ("!"") is arbitrary, but we have to use some.
24
120
  return useTranslationsImpl<
25
- // @ts-ignore
26
- {__private: GlobalMessages},
27
- NamespaceKeys<GlobalMessages, NestedKeyOf<GlobalMessages>> extends NestedKey
28
- ? '__private'
29
- : `__private.${NestedKey}`
121
+ {'!': IntlMessages},
122
+ NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
123
+ ? '!'
124
+ : `!.${NestedKey}`
30
125
  >(
31
- {__private: messages},
126
+ {'!': messages},
32
127
  // @ts-ignore
33
- namespace ? `__private.${namespace}` : '__private'
128
+ namespace ? `!.${namespace}` : '!',
129
+ '!'
34
130
  );
35
131
  }
@@ -1,4 +1,4 @@
1
- import IntlMessageFormat from 'intl-messageformat';
1
+ import {IntlMessageFormat} from 'intl-messageformat';
2
2
  import {
3
3
  cloneElement,
4
4
  isValidElement,
@@ -8,9 +8,9 @@ import {
8
8
  useMemo,
9
9
  useRef
10
10
  } from 'react';
11
+ import AbstractIntlMessages from './AbstractIntlMessages';
11
12
  import Formats from './Formats';
12
13
  import IntlError, {IntlErrorCode} from './IntlError';
13
- import IntlMessages from './IntlMessages';
14
14
  import TranslationValues, {RichTranslationValues} from './TranslationValues';
15
15
  import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
16
16
  import useIntlContext from './useIntlContext';
@@ -19,7 +19,7 @@ import NestedKeyOf from './utils/NestedKeyOf';
19
19
  import NestedValueOf from './utils/NestedValueOf';
20
20
 
21
21
  function resolvePath(
22
- messages: IntlMessages | undefined,
22
+ messages: AbstractIntlMessages | undefined,
23
23
  idPath: string,
24
24
  namespace?: string
25
25
  ) {
@@ -79,9 +79,9 @@ function prepareTranslationValues(values: RichTranslationValues) {
79
79
  }
80
80
 
81
81
  export default function useTranslationsImpl<
82
- Messages extends IntlMessages,
82
+ Messages extends AbstractIntlMessages,
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,15 @@ 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 = (
98
+ namespace === namespacePrefix
99
+ ? undefined
100
+ : namespace.slice((namespacePrefix + '.').length)
101
+ ) as NestedKey;
102
+
94
103
  const cachedFormatsByLocaleRef = useRef<
95
104
  Record<string, Record<string, IntlMessageFormat>>
96
105
  >({});
@@ -1,2 +0,0 @@
1
- declare interface GlobalMessages {
2
- }
@@ -1,6 +0,0 @@
1
- // This module is intended to be overridden
2
- // by the consumer for optional type safety
3
-
4
- // declare interface GlobalMessages extends Record<any, any> {}
5
-
6
- declare interface GlobalMessages {}
package/src/en.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "About": {
3
- "title": "About",
4
- "lastUpdated": "This example was updated {lastUpdatedRelative} ({lastUpdated, date, short}).",
5
- "nested": {
6
- "hello": "Hello"
7
- }
8
- },
9
- "Index": {
10
- "title": "Home",
11
- "description": "<p>Only the minimum of <code>{locale}</code> messages are loaded to render this page.</p><p>These namespaces are available:</p>"
12
- },
13
- "Navigation": {
14
- "index": "Home",
15
- "about": "About",
16
- "switchLocale": "Switch to {locale, select, de {German} en {English}}"
17
- },
18
- "NotFound": {
19
- "title": "Sorry, this page could not be found."
20
- },
21
- "PageLayout": {
22
- "pageTitle": "next-intl"
23
- },
24
- "Test": {
25
- "nested": {
26
- "hello": "Hello",
27
- "another": {
28
- "level": "Level"
29
- }
30
- }
31
- }
32
- }