use-intl 2.9.1 → 2.10.0-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.
Files changed (162) hide show
  1. package/CHANGELOG.md +463 -0
  2. package/dist/core/use-intl.esm.js +140 -27
  3. package/dist/core/use-intl.esm.js.map +1 -1
  4. package/dist/core/use-intl.esm2.js +41 -38
  5. package/dist/core/use-intl.esm2.js.map +1 -1
  6. package/dist/core/use-intl.esm3.js +35 -151
  7. package/dist/core/use-intl.esm3.js.map +1 -1
  8. package/dist/core/use-intl.esm4.js +12 -56
  9. package/dist/core/use-intl.esm4.js.map +1 -1
  10. package/dist/core/use-intl.esm5.js +56 -12
  11. package/dist/core/use-intl.esm5.js.map +1 -1
  12. package/dist/core/use-intl.esm6.js +7 -29
  13. package/dist/core/use-intl.esm6.js.map +1 -1
  14. package/dist/core/use-intl.esm7.js +9 -9
  15. package/dist/core/use-intl.esm7.js.map +1 -1
  16. package/dist/core/use-intl.esm8.js +29 -7
  17. package/dist/core/use-intl.esm8.js.map +1 -1
  18. package/dist/react/IntlContext.d.ts +2 -19
  19. package/dist/react/IntlContextValue.d.ts +19 -0
  20. package/dist/react/IntlProvider.d.ts +2 -16
  21. package/dist/react/IntlProviderProps.d.ts +15 -0
  22. package/dist/react/getIntlContextValue.d.ts +18 -0
  23. package/dist/react/use-intl.esm.js +6 -25
  24. package/dist/react/use-intl.esm.js.map +1 -1
  25. package/dist/react/use-intl.esm10.js +44 -0
  26. package/dist/react/use-intl.esm10.js.map +1 -0
  27. package/dist/react/use-intl.esm2.js +20 -21
  28. package/dist/react/use-intl.esm2.js.map +1 -1
  29. package/dist/react/use-intl.esm3.js +21 -20
  30. package/dist/react/use-intl.esm3.js.map +1 -1
  31. package/dist/react/use-intl.esm4.js +46 -4
  32. package/dist/react/use-intl.esm4.js.map +1 -1
  33. package/dist/react/use-intl.esm5.js +4 -46
  34. package/dist/react/use-intl.esm5.js.map +1 -1
  35. package/dist/react/use-intl.esm6.js +1 -1
  36. package/dist/react/use-intl.esm7.js.map +1 -1
  37. package/dist/react/use-intl.esm8.js +26 -8
  38. package/dist/react/use-intl.esm8.js.map +1 -1
  39. package/dist/react/use-intl.esm9.js +9 -38
  40. package/dist/react/use-intl.esm9.js.map +1 -1
  41. package/dist/react/useIntlContext.d.ts +1 -1
  42. package/dist/src/core/AbstractIntlMessages.d.ts +9 -0
  43. package/dist/src/core/AbstractIntlMessages.js +2 -0
  44. package/dist/src/core/AbstractIntlMessages.js.map +1 -0
  45. package/dist/src/core/DateTimeFormatOptions.d.ts +72 -0
  46. package/dist/src/core/DateTimeFormatOptions.js +3 -0
  47. package/dist/src/core/DateTimeFormatOptions.js.map +1 -0
  48. package/dist/src/core/Formats.d.ts +7 -0
  49. package/dist/src/core/Formats.js +2 -0
  50. package/dist/src/core/Formats.js.map +1 -0
  51. package/dist/src/core/IntlConfiguration.d.ts +46 -0
  52. package/dist/src/core/IntlConfiguration.js +2 -0
  53. package/dist/src/core/IntlConfiguration.js.map +1 -0
  54. package/dist/src/core/IntlError.d.ts +13 -0
  55. package/dist/src/core/IntlError.js +23 -0
  56. package/dist/src/core/IntlError.js.map +1 -0
  57. package/dist/src/core/IntlMessages.d.ts +2 -0
  58. package/dist/src/core/IntlMessages.js +4 -0
  59. package/dist/src/core/IntlMessages.js.map +1 -0
  60. package/dist/src/core/NumberFormatOptions.d.ts +2 -0
  61. package/dist/src/core/NumberFormatOptions.js +2 -0
  62. package/dist/src/core/NumberFormatOptions.js.map +1 -0
  63. package/dist/src/core/TranslationValues.d.ts +5 -0
  64. package/dist/src/core/TranslationValues.js +2 -0
  65. package/dist/src/core/TranslationValues.js.map +1 -0
  66. package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +10 -0
  67. package/dist/src/core/convertFormatsToIntlMessageFormat.js +31 -0
  68. package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +1 -0
  69. package/dist/src/core/createBaseTranslator.d.ts +26 -0
  70. package/dist/src/core/createBaseTranslator.js +185 -0
  71. package/dist/src/core/createBaseTranslator.js.map +1 -0
  72. package/dist/src/core/createIntl.d.ts +17 -0
  73. package/dist/src/core/createIntl.js +126 -0
  74. package/dist/src/core/createIntl.js.map +1 -0
  75. package/dist/src/core/createTranslator.d.ts +48 -0
  76. package/dist/src/core/createTranslator.js +24 -0
  77. package/dist/src/core/createTranslator.js.map +1 -0
  78. package/dist/src/core/createTranslatorImpl.d.ts +14 -0
  79. package/dist/src/core/createTranslatorImpl.js +43 -0
  80. package/dist/src/core/createTranslatorImpl.js.map +1 -0
  81. package/dist/src/core/defaults.d.ts +11 -0
  82. package/dist/src/core/defaults.js +11 -0
  83. package/dist/src/core/defaults.js.map +1 -0
  84. package/dist/src/core/index.d.ts +8 -0
  85. package/dist/src/core/index.js +4 -0
  86. package/dist/src/core/index.js.map +1 -0
  87. package/dist/src/core/resolveNamespace.d.ts +5 -0
  88. package/dist/src/core/resolveNamespace.js +10 -0
  89. package/dist/src/core/resolveNamespace.js.map +1 -0
  90. package/dist/src/core/utils/MessageKeys.d.ts +5 -0
  91. package/dist/src/core/utils/MessageKeys.js +2 -0
  92. package/dist/src/core/utils/MessageKeys.js.map +1 -0
  93. package/dist/src/core/utils/NamespaceKeys.d.ts +5 -0
  94. package/dist/src/core/utils/NamespaceKeys.js +2 -0
  95. package/dist/src/core/utils/NamespaceKeys.js.map +1 -0
  96. package/dist/src/core/utils/NestedKeyOf.d.ts +4 -0
  97. package/dist/src/core/utils/NestedKeyOf.js +2 -0
  98. package/dist/src/core/utils/NestedKeyOf.js.map +1 -0
  99. package/dist/src/core/utils/NestedValueOf.d.ts +2 -0
  100. package/dist/src/core/utils/NestedValueOf.js +2 -0
  101. package/dist/src/core/utils/NestedValueOf.js.map +1 -0
  102. package/dist/src/core/validateMessages.d.ts +3 -0
  103. package/dist/src/core/validateMessages.js +22 -0
  104. package/dist/src/core/validateMessages.js.map +1 -0
  105. package/dist/src/index.d.ts +2 -0
  106. package/dist/src/index.js +3 -0
  107. package/dist/src/index.js.map +1 -0
  108. package/dist/src/react/IntlContext.d.ts +4 -0
  109. package/dist/src/react/IntlContext.js +4 -0
  110. package/dist/src/react/IntlContext.js.map +1 -0
  111. package/dist/src/react/IntlContextValue.d.ts +19 -0
  112. package/dist/src/react/IntlContextValue.js +2 -0
  113. package/dist/src/react/IntlContextValue.js.map +1 -0
  114. package/dist/src/react/IntlProvider.d.ts +2 -0
  115. package/dist/src/react/IntlProvider.js +7 -0
  116. package/dist/src/react/IntlProvider.js.map +1 -0
  117. package/dist/src/react/IntlProviderProps.d.ts +15 -0
  118. package/dist/src/react/IntlProviderProps.js +2 -0
  119. package/dist/src/react/IntlProviderProps.js.map +1 -0
  120. package/dist/src/react/getIntlContextValue.d.ts +18 -0
  121. package/dist/src/react/getIntlContextValue.js +21 -0
  122. package/dist/src/react/getIntlContextValue.js.map +1 -0
  123. package/dist/src/react/index.d.ts +6 -0
  124. package/dist/src/react/index.js +7 -0
  125. package/dist/src/react/index.js.map +1 -0
  126. package/dist/src/react/useIntl.d.ts +5 -0
  127. package/dist/src/react/useIntl.js +14 -0
  128. package/dist/src/react/useIntl.js.map +1 -0
  129. package/dist/src/react/useIntlContext.d.ts +1 -0
  130. package/dist/src/react/useIntlContext.js +12 -0
  131. package/dist/src/react/useIntlContext.js.map +1 -0
  132. package/dist/src/react/useLocale.d.ts +1 -0
  133. package/dist/src/react/useLocale.js +5 -0
  134. package/dist/src/react/useLocale.js.map +1 -0
  135. package/dist/src/react/useNow.d.ts +23 -0
  136. package/dist/src/react/useNow.js +40 -0
  137. package/dist/src/react/useNow.js.map +1 -0
  138. package/dist/src/react/useTimeZone.d.ts +1 -0
  139. package/dist/src/react/useTimeZone.js +5 -0
  140. package/dist/src/react/useTimeZone.js.map +1 -0
  141. package/dist/src/react/useTranslations.d.ts +44 -0
  142. package/dist/src/react/useTranslations.js +21 -0
  143. package/dist/src/react/useTranslations.js.map +1 -0
  144. package/dist/src/react/useTranslationsImpl.d.ts +8 -0
  145. package/dist/src/react/useTranslationsImpl.js +35 -0
  146. package/dist/src/react/useTranslationsImpl.js.map +1 -0
  147. package/dist/use-intl.cjs.development.js +37 -28
  148. package/dist/use-intl.cjs.development.js.map +1 -1
  149. package/dist/use-intl.cjs.production.min.js +1 -1
  150. package/dist/use-intl.cjs.production.min.js.map +1 -1
  151. package/dist/use-intl.esm.js +7 -7
  152. package/package.json +6 -5
  153. package/src/core/createBaseTranslator.tsx +11 -7
  154. package/src/core/createIntl.tsx +2 -2
  155. package/src/core/createTranslatorImpl.tsx +1 -1
  156. package/src/react/IntlContext.tsx +2 -20
  157. package/src/react/IntlContextValue.tsx +21 -0
  158. package/src/react/IntlProvider.tsx +5 -36
  159. package/src/react/IntlProviderProps.tsx +17 -0
  160. package/src/react/getIntlContextValue.tsx +30 -0
  161. package/src/react/useIntlContext.tsx +1 -1
  162. package/LICENSE +0 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-intl",
3
- "version": "2.9.1",
3
+ "version": "2.10.0-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.",
@@ -12,9 +12,11 @@
12
12
  },
13
13
  "scripts": {
14
14
  "start": "dts watch",
15
- "build": "dts build",
15
+ "build": "yarn build:default && yarn build:rsc",
16
+ "build:default": "rm -rf dist && dts build",
17
+ "build:rsc": "tsc && rm -rf dist/test",
16
18
  "test": "TZ=Europe/Berlin dts test",
17
- "lint": "eslint src test && tsc",
19
+ "lint": "eslint src test && tsc --noEmit",
18
20
  "prepublishOnly": "yarn test && yarn lint && yarn build"
19
21
  },
20
22
  "main": "dist/index.js",
@@ -59,6 +61,5 @@
59
61
  },
60
62
  "engines": {
61
63
  "node": ">=10"
62
- },
63
- "gitHead": "ef170366c1b0689d969b17e70f9e70a296e86254"
64
+ }
64
65
  }
@@ -24,7 +24,9 @@ function resolvePath(
24
24
  ) {
25
25
  if (!messages) {
26
26
  throw new Error(
27
- __DEV__ ? `No messages available at \`${namespace}\`.` : undefined
27
+ process.env.NODE_ENV !== 'production'
28
+ ? `No messages available at \`${namespace}\`.`
29
+ : undefined
28
30
  );
29
31
  }
30
32
 
@@ -35,7 +37,7 @@ function resolvePath(
35
37
 
36
38
  if (part == null || next == null) {
37
39
  throw new Error(
38
- __DEV__
40
+ process.env.NODE_ENV !== 'production'
39
41
  ? `Could not resolve \`${key}\` in ${
40
42
  namespace ? `\`${namespace}\`` : 'messages'
41
43
  }.`
@@ -89,7 +91,9 @@ export function getMessagesOrError<Messages extends AbstractIntlMessages>({
89
91
  try {
90
92
  if (!messages) {
91
93
  throw new Error(
92
- __DEV__ ? `No messages were configured on the provider.` : undefined
94
+ process.env.NODE_ENV !== 'production'
95
+ ? `No messages were configured on the provider.`
96
+ : undefined
93
97
  );
94
98
  }
95
99
 
@@ -99,7 +103,7 @@ export function getMessagesOrError<Messages extends AbstractIntlMessages>({
99
103
 
100
104
  if (!retrievedMessages) {
101
105
  throw new Error(
102
- __DEV__
106
+ process.env.NODE_ENV !== 'production'
103
107
  ? `No messages for namespace \`${namespace}\` found.`
104
108
  : undefined
105
109
  );
@@ -187,7 +191,7 @@ export default function createBaseTranslator<
187
191
  return getFallbackFromErrorAndNotify(
188
192
  key,
189
193
  IntlErrorCode.INSUFFICIENT_PATH,
190
- __DEV__
194
+ process.env.NODE_ENV !== 'production'
191
195
  ? `Insufficient path specified for \`${key}\` in \`${
192
196
  namespace ? `\`${namespace}\`` : 'messages'
193
197
  }\`.`
@@ -231,7 +235,7 @@ export default function createBaseTranslator<
231
235
 
232
236
  if (formattedMessage == null) {
233
237
  throw new Error(
234
- __DEV__
238
+ process.env.NODE_ENV !== 'production'
235
239
  ? `Unable to format \`${key}\` in ${
236
240
  namespace ? `namespace \`${namespace}\`` : 'messages'
237
241
  }`
@@ -274,7 +278,7 @@ export default function createBaseTranslator<
274
278
  return getFallbackFromErrorAndNotify(
275
279
  key,
276
280
  IntlErrorCode.INVALID_MESSAGE,
277
- __DEV__
281
+ process.env.NODE_ENV !== 'production'
278
282
  ? `The message \`${key}\` in ${
279
283
  namespace ? `namespace \`${namespace}\`` : 'messages'
280
284
  } didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
@@ -71,7 +71,7 @@ export default function createIntl({
71
71
  if (!options) {
72
72
  const error = new IntlError(
73
73
  IntlErrorCode.MISSING_FORMAT,
74
- __DEV__
74
+ process.env.NODE_ENV !== 'production'
75
75
  ? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
76
76
  : undefined
77
77
  );
@@ -153,7 +153,7 @@ export default function createIntl({
153
153
  now = globalNow;
154
154
  } else {
155
155
  throw new Error(
156
- __DEV__
156
+ process.env.NODE_ENV !== 'production'
157
157
  ? `The \`now\` parameter wasn't provided to \`formatRelativeTime\` and there was no global fallback configured on the provider.`
158
158
  : undefined
159
159
  );
@@ -67,7 +67,7 @@ export default function createTranslatorImpl<
67
67
  if (typeof result !== 'string') {
68
68
  const error = new IntlError(
69
69
  IntlErrorCode.FORMATTING_ERROR,
70
- __DEV__
70
+ process.env.NODE_ENV !== 'production'
71
71
  ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})"
72
72
  : undefined
73
73
  );
@@ -1,24 +1,6 @@
1
1
  import {createContext} from 'react';
2
- import AbstractIntlMessages from '../core/AbstractIntlMessages';
3
- import Formats from '../core/Formats';
4
- import IntlError from '../core/IntlError';
5
- import {RichTranslationValues} from '../core/TranslationValues';
2
+ import IntlContextValue from './IntlContextValue';
6
3
 
7
- export type IntlContextShape = {
8
- messages?: AbstractIntlMessages;
9
- locale: string;
10
- formats?: Partial<Formats>;
11
- timeZone?: string;
12
- onError(error: IntlError): void;
13
- getMessageFallback(info: {
14
- error: IntlError;
15
- key: string;
16
- namespace?: string;
17
- }): string;
18
- now?: Date;
19
- defaultTranslationValues?: RichTranslationValues;
20
- };
21
-
22
- const IntlContext = createContext<IntlContextShape | undefined>(undefined);
4
+ const IntlContext = createContext<IntlContextValue | undefined>(undefined);
23
5
 
24
6
  export default IntlContext;
@@ -0,0 +1,21 @@
1
+ import AbstractIntlMessages from '../core/AbstractIntlMessages';
2
+ import Formats from '../core/Formats';
3
+ import IntlError from '../core/IntlError';
4
+ import {RichTranslationValues} from '../core/TranslationValues';
5
+
6
+ type IntlContextValue = {
7
+ messages?: AbstractIntlMessages;
8
+ locale: string;
9
+ formats?: Partial<Formats>;
10
+ timeZone?: string;
11
+ onError(error: IntlError): void;
12
+ getMessageFallback(info: {
13
+ error: IntlError;
14
+ key: string;
15
+ namespace?: string;
16
+ }): string;
17
+ now?: Date;
18
+ defaultTranslationValues?: RichTranslationValues;
19
+ };
20
+
21
+ export default IntlContextValue;
@@ -1,42 +1,11 @@
1
- import React, {ReactNode, useEffect} from 'react';
2
- import {AbstractIntlMessages} from '../core';
3
- import IntlConfiguration from '../core/IntlConfiguration';
4
- import {RichTranslationValues} from '../core/TranslationValues';
5
- import {defaultGetMessageFallback, defaultOnError} from '../core/defaults';
6
- import validateMessages from '../core/validateMessages';
1
+ import React from 'react';
7
2
  import IntlContext from './IntlContext';
3
+ import IntlProviderProps from './IntlProviderProps';
4
+ import getIntlContextValue from './getIntlContextValue';
8
5
 
9
- type Props = IntlConfiguration & {
10
- /** All components that use the provided hooks should be within this tree. */
11
- children: ReactNode;
12
- /** Global default values for translation values and rich text elements.
13
- * Can be used for consistent usage or styling of rich text elements.
14
- * Defaults will be overidden by locally provided values. */
15
- defaultTranslationValues?: RichTranslationValues;
16
- /** All messages that will be available in your components. */
17
- messages?: AbstractIntlMessages;
18
- };
19
-
20
- export default function IntlProvider({
21
- children,
22
- onError = defaultOnError,
23
- getMessageFallback = defaultGetMessageFallback,
24
- messages,
25
- ...contextValues
26
- }: Props) {
27
- if (__DEV__) {
28
- // eslint-disable-next-line react-hooks/rules-of-hooks
29
- useEffect(() => {
30
- if (messages) {
31
- validateMessages(messages, onError);
32
- }
33
- }, [messages, onError]);
34
- }
35
-
6
+ export default function IntlProvider({children, ...props}: IntlProviderProps) {
36
7
  return (
37
- <IntlContext.Provider
38
- value={{...contextValues, messages, onError, getMessageFallback}}
39
- >
8
+ <IntlContext.Provider value={getIntlContextValue(props)}>
40
9
  {children}
41
10
  </IntlContext.Provider>
42
11
  );
@@ -0,0 +1,17 @@
1
+ import {ReactNode} from 'react';
2
+ import {AbstractIntlMessages} from '../core';
3
+ import IntlConfiguration from '../core/IntlConfiguration';
4
+ import {RichTranslationValues} from '../core/TranslationValues';
5
+
6
+ type IntlProviderProps = IntlConfiguration & {
7
+ /** All components that use the provided hooks should be within this tree. */
8
+ children: ReactNode;
9
+ /** Global default values for translation values and rich text elements.
10
+ * Can be used for consistent usage or styling of rich text elements.
11
+ * Defaults will be overidden by locally provided values. */
12
+ defaultTranslationValues?: RichTranslationValues;
13
+ /** All messages that will be available in your components. */
14
+ messages?: AbstractIntlMessages;
15
+ };
16
+
17
+ export default IntlProviderProps;
@@ -0,0 +1,30 @@
1
+ import {defaultGetMessageFallback, defaultOnError} from '../core/defaults';
2
+ import validateMessages from '../core/validateMessages';
3
+ import IntlProviderProps from './IntlProviderProps';
4
+
5
+ /**
6
+ * Enhances the incoming props with defaults.
7
+ */
8
+ export default function getIntlContextValue({
9
+ getMessageFallback,
10
+ messages,
11
+ onError,
12
+ ...rest
13
+ }: Omit<IntlProviderProps, 'children'>) {
14
+ const finalOnError = onError || defaultOnError;
15
+ const finalGetMessageFallback =
16
+ getMessageFallback || defaultGetMessageFallback;
17
+
18
+ if (process.env.NODE_ENV !== 'production') {
19
+ if (messages) {
20
+ validateMessages(messages, finalOnError);
21
+ }
22
+ }
23
+
24
+ return {
25
+ ...rest,
26
+ messages,
27
+ onError: finalOnError,
28
+ getMessageFallback: finalGetMessageFallback
29
+ };
30
+ }
@@ -6,7 +6,7 @@ export default function useIntlContext() {
6
6
 
7
7
  if (!context) {
8
8
  throw new Error(
9
- __DEV__
9
+ process.env.NODE_ENV !== 'production'
10
10
  ? 'No intl context found. Have you configured the provider?'
11
11
  : undefined
12
12
  );
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Jan Amann
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.