use-intl 2.22.0 → 2.22.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.
Files changed (298) hide show
  1. package/README.md +1 -1
  2. package/_IntlProvider.d.ts +1 -0
  3. package/_useLocale.d.ts +1 -0
  4. package/core.d.ts +1 -0
  5. package/dist/_IntlProvider.js +7 -0
  6. package/dist/_useLocale.js +7 -0
  7. package/dist/core.js +7 -0
  8. package/dist/development/IntlContext-b5cc6be8.js +8 -0
  9. package/dist/development/_IntlProvider.js +53 -0
  10. package/dist/development/_useLocale-321e619f.js +19 -0
  11. package/dist/development/_useLocale.js +11 -0
  12. package/dist/development/core.js +66 -0
  13. package/dist/development/createFormatter-4848f6c8.js +453 -0
  14. package/dist/development/index.js +28 -0
  15. package/dist/development/initializeConfig-0a0b87a7.js +120 -0
  16. package/dist/development/react.js +145 -0
  17. package/dist/esm/IntlContext-381f3ce4.js +1 -0
  18. package/dist/esm/_IntlProvider.js +1 -0
  19. package/dist/esm/_useLocale-89b32eb9.js +1 -0
  20. package/dist/esm/_useLocale.js +1 -0
  21. package/dist/esm/core.js +1 -0
  22. package/dist/esm/createFormatter-c871a8ce.js +1 -0
  23. package/dist/esm/index.js +1 -0
  24. package/dist/esm/initializeConfig-29e7ba4c.js +1 -0
  25. package/dist/esm/react.js +1 -0
  26. package/dist/index.js +3 -4
  27. package/dist/production/IntlContext-381f3ce4.js +1 -0
  28. package/dist/production/_IntlProvider.js +1 -0
  29. package/dist/production/_useLocale-8e23751a.js +1 -0
  30. package/dist/production/_useLocale.js +1 -0
  31. package/dist/production/core.js +1 -0
  32. package/dist/production/createFormatter-7a8c6f36.js +1 -0
  33. package/dist/production/index.js +1 -0
  34. package/dist/production/initializeConfig-984a566d.js +1 -0
  35. package/dist/production/react.js +1 -0
  36. package/dist/react.js +7 -0
  37. package/dist/types/src/_IntlProvider.d.ts +1 -0
  38. package/dist/types/src/_useLocale.d.ts +1 -0
  39. package/dist/{src → types/src}/core/IntlConfig.d.ts +1 -1
  40. package/dist/{core → types/src/core}/TranslationValues.d.ts +6 -5
  41. package/dist/{src → types/src}/core/createBaseTranslator.d.ts +5 -9
  42. package/dist/{src → types/src}/core/createTranslator.d.ts +16 -4
  43. package/dist/types/src/core/createTranslatorImpl.d.ts +16 -0
  44. package/dist/{core → types/src/core}/index.d.ts +14 -9
  45. package/dist/types/src/core/initializeConfig.d.ts +14 -0
  46. package/dist/types/src/core.d.ts +1 -0
  47. package/dist/types/src/react/IntlProvider.d.ts +7 -0
  48. package/dist/{src → types/src}/react/index.d.ts +0 -1
  49. package/dist/types/src/react/useMessages.d.ts +2 -0
  50. package/dist/{src → types/src}/react/useTranslations.d.ts +10 -1
  51. package/dist/{src → types/src}/react/useTranslationsImpl.d.ts +2 -1
  52. package/dist/types/src/react.d.ts +1 -0
  53. package/dist/types/test/core/createFormatter.test.d.ts +1 -0
  54. package/dist/types/test/core/createTranslator.test.d.ts +1 -0
  55. package/dist/types/test/react/IntlProvider.test.d.ts +1 -0
  56. package/dist/types/test/react/useFormatter.test.d.ts +1 -0
  57. package/dist/types/test/react/useLocale.test.d.ts +1 -0
  58. package/dist/types/test/react/useMessages.test.d.ts +1 -0
  59. package/dist/types/test/react/useNow.test.d.ts +1 -0
  60. package/dist/types/test/react/useTimeZone.test.d.ts +1 -0
  61. package/dist/types/test/react/useTranslations.test.d.ts +1 -0
  62. package/dist/types/test/setup.d.ts +1 -0
  63. package/package.json +38 -21
  64. package/react.d.ts +1 -0
  65. package/dist/_virtual/use-intl.esm.js +0 -102
  66. package/dist/_virtual/use-intl.esm.js.map +0 -1
  67. package/dist/core/AbstractIntlMessages.d.ts +0 -9
  68. package/dist/core/DateTimeFormatOptions.d.ts +0 -73
  69. package/dist/core/Formats.d.ts +0 -8
  70. package/dist/core/IntlConfig.d.ts +0 -54
  71. package/dist/core/IntlError.d.ts +0 -14
  72. package/dist/core/MessageFormatCache.d.ts +0 -5
  73. package/dist/core/NumberFormatOptions.d.ts +0 -2
  74. package/dist/core/RelativeTimeFormatOptions.d.ts +0 -5
  75. package/dist/core/TimeZone.d.ts +0 -2
  76. package/dist/core/convertFormatsToIntlMessageFormat.d.ts +0 -11
  77. package/dist/core/createBaseTranslator.d.ts +0 -26
  78. package/dist/core/createFormatter.d.ts +0 -20
  79. package/dist/core/createIntl.d.ts +0 -7
  80. package/dist/core/createTranslator.d.ts +0 -48
  81. package/dist/core/createTranslatorImpl.d.ts +0 -14
  82. package/dist/core/defaults.d.ts +0 -11
  83. package/dist/core/resolveNamespace.d.ts +0 -5
  84. package/dist/core/use-intl.esm.js +0 -34
  85. package/dist/core/use-intl.esm.js.map +0 -1
  86. package/dist/core/use-intl.esm10.js +0 -37
  87. package/dist/core/use-intl.esm10.js.map +0 -1
  88. package/dist/core/use-intl.esm2.js +0 -36
  89. package/dist/core/use-intl.esm2.js.map +0 -1
  90. package/dist/core/use-intl.esm3.js +0 -161
  91. package/dist/core/use-intl.esm3.js.map +0 -1
  92. package/dist/core/use-intl.esm4.js +0 -14
  93. package/dist/core/use-intl.esm4.js.map +0 -1
  94. package/dist/core/use-intl.esm5.js +0 -53
  95. package/dist/core/use-intl.esm5.js.map +0 -1
  96. package/dist/core/use-intl.esm6.js +0 -15
  97. package/dist/core/use-intl.esm6.js.map +0 -1
  98. package/dist/core/use-intl.esm7.js +0 -10
  99. package/dist/core/use-intl.esm7.js.map +0 -1
  100. package/dist/core/use-intl.esm8.js +0 -200
  101. package/dist/core/use-intl.esm8.js.map +0 -1
  102. package/dist/core/use-intl.esm9.js +0 -28
  103. package/dist/core/use-intl.esm9.js.map +0 -1
  104. package/dist/core/utils/MessageKeys.d.ts +0 -5
  105. package/dist/core/utils/NamespaceKeys.d.ts +0 -5
  106. package/dist/core/utils/NestedKeyOf.d.ts +0 -4
  107. package/dist/core/utils/NestedValueOf.d.ts +0 -2
  108. package/dist/core/validateMessages.d.ts +0 -3
  109. package/dist/index.d.ts +0 -2
  110. package/dist/react/IntlContext.d.ts +0 -13
  111. package/dist/react/IntlProvider.d.ts +0 -7
  112. package/dist/react/getInitializedConfig.d.ts +0 -13
  113. package/dist/react/index.d.ts +0 -8
  114. package/dist/react/use-intl.esm.js +0 -22
  115. package/dist/react/use-intl.esm.js.map +0 -1
  116. package/dist/react/use-intl.esm10.js +0 -29
  117. package/dist/react/use-intl.esm10.js.map +0 -1
  118. package/dist/react/use-intl.esm11.js +0 -13
  119. package/dist/react/use-intl.esm11.js.map +0 -1
  120. package/dist/react/use-intl.esm12.js +0 -43
  121. package/dist/react/use-intl.esm12.js.map +0 -1
  122. package/dist/react/use-intl.esm2.js +0 -26
  123. package/dist/react/use-intl.esm2.js.map +0 -1
  124. package/dist/react/use-intl.esm3.js +0 -8
  125. package/dist/react/use-intl.esm3.js.map +0 -1
  126. package/dist/react/use-intl.esm4.js +0 -45
  127. package/dist/react/use-intl.esm4.js.map +0 -1
  128. package/dist/react/use-intl.esm5.js +0 -8
  129. package/dist/react/use-intl.esm5.js.map +0 -1
  130. package/dist/react/use-intl.esm6.js +0 -8
  131. package/dist/react/use-intl.esm6.js.map +0 -1
  132. package/dist/react/use-intl.esm7.js +0 -24
  133. package/dist/react/use-intl.esm7.js.map +0 -1
  134. package/dist/react/use-intl.esm8.js +0 -30
  135. package/dist/react/use-intl.esm8.js.map +0 -1
  136. package/dist/react/use-intl.esm9.js +0 -6
  137. package/dist/react/use-intl.esm9.js.map +0 -1
  138. package/dist/react/useFormatter.d.ts +0 -6
  139. package/dist/react/useIntl.d.ts +0 -6
  140. package/dist/react/useIntlContext.d.ts +0 -10
  141. package/dist/react/useLocale.d.ts +0 -1
  142. package/dist/react/useMessages.d.ts +0 -1
  143. package/dist/react/useNow.d.ts +0 -23
  144. package/dist/react/useTimeZone.d.ts +0 -1
  145. package/dist/react/useTranslations.d.ts +0 -44
  146. package/dist/react/useTranslationsImpl.d.ts +0 -8
  147. package/dist/src/core/AbstractIntlMessages.js +0 -2
  148. package/dist/src/core/AbstractIntlMessages.js.map +0 -1
  149. package/dist/src/core/DateTimeFormatOptions.js +0 -3
  150. package/dist/src/core/DateTimeFormatOptions.js.map +0 -1
  151. package/dist/src/core/Formats.js +0 -2
  152. package/dist/src/core/Formats.js.map +0 -1
  153. package/dist/src/core/IntlConfig.js +0 -2
  154. package/dist/src/core/IntlConfig.js.map +0 -1
  155. package/dist/src/core/IntlError.js +0 -24
  156. package/dist/src/core/IntlError.js.map +0 -1
  157. package/dist/src/core/MessageFormatCache.js +0 -2
  158. package/dist/src/core/MessageFormatCache.js.map +0 -1
  159. package/dist/src/core/NumberFormatOptions.js +0 -2
  160. package/dist/src/core/NumberFormatOptions.js.map +0 -1
  161. package/dist/src/core/RelativeTimeFormatOptions.js +0 -2
  162. package/dist/src/core/RelativeTimeFormatOptions.js.map +0 -1
  163. package/dist/src/core/TimeZone.js +0 -2
  164. package/dist/src/core/TimeZone.js.map +0 -1
  165. package/dist/src/core/TranslationValues.d.ts +0 -5
  166. package/dist/src/core/TranslationValues.js +0 -2
  167. package/dist/src/core/TranslationValues.js.map +0 -1
  168. package/dist/src/core/convertFormatsToIntlMessageFormat.js +0 -47
  169. package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +0 -1
  170. package/dist/src/core/createBaseTranslator.js +0 -218
  171. package/dist/src/core/createBaseTranslator.js.map +0 -1
  172. package/dist/src/core/createFormatter.js +0 -163
  173. package/dist/src/core/createFormatter.js.map +0 -1
  174. package/dist/src/core/createIntl.d.ts +0 -7
  175. package/dist/src/core/createIntl.js +0 -11
  176. package/dist/src/core/createIntl.js.map +0 -1
  177. package/dist/src/core/createTranslator.js +0 -23
  178. package/dist/src/core/createTranslator.js.map +0 -1
  179. package/dist/src/core/createTranslatorImpl.d.ts +0 -14
  180. package/dist/src/core/createTranslatorImpl.js +0 -43
  181. package/dist/src/core/createTranslatorImpl.js.map +0 -1
  182. package/dist/src/core/defaults.js +0 -11
  183. package/dist/src/core/defaults.js.map +0 -1
  184. package/dist/src/core/index.d.ts +0 -9
  185. package/dist/src/core/index.js +0 -6
  186. package/dist/src/core/index.js.map +0 -1
  187. package/dist/src/core/resolveNamespace.js +0 -10
  188. package/dist/src/core/resolveNamespace.js.map +0 -1
  189. package/dist/src/core/utils/MessageKeys.js +0 -2
  190. package/dist/src/core/utils/MessageKeys.js.map +0 -1
  191. package/dist/src/core/utils/NamespaceKeys.js +0 -2
  192. package/dist/src/core/utils/NamespaceKeys.js.map +0 -1
  193. package/dist/src/core/utils/NestedKeyOf.js +0 -2
  194. package/dist/src/core/utils/NestedKeyOf.js.map +0 -1
  195. package/dist/src/core/utils/NestedValueOf.js +0 -2
  196. package/dist/src/core/utils/NestedValueOf.js.map +0 -1
  197. package/dist/src/core/validateMessages.js +0 -56
  198. package/dist/src/core/validateMessages.js.map +0 -1
  199. package/dist/src/index.js +0 -3
  200. package/dist/src/index.js.map +0 -1
  201. package/dist/src/react/IntlContext.js +0 -4
  202. package/dist/src/react/IntlContext.js.map +0 -1
  203. package/dist/src/react/IntlProvider.d.ts +0 -7
  204. package/dist/src/react/IntlProvider.js +0 -11
  205. package/dist/src/react/IntlProvider.js.map +0 -1
  206. package/dist/src/react/getInitializedConfig.d.ts +0 -13
  207. package/dist/src/react/getInitializedConfig.js +0 -21
  208. package/dist/src/react/getInitializedConfig.js.map +0 -1
  209. package/dist/src/react/index.js +0 -10
  210. package/dist/src/react/index.js.map +0 -1
  211. package/dist/src/react/useFormatter.js +0 -14
  212. package/dist/src/react/useFormatter.js.map +0 -1
  213. package/dist/src/react/useIntl.d.ts +0 -6
  214. package/dist/src/react/useIntl.js +0 -20
  215. package/dist/src/react/useIntl.js.map +0 -1
  216. package/dist/src/react/useIntlContext.js +0 -12
  217. package/dist/src/react/useIntlContext.js.map +0 -1
  218. package/dist/src/react/useLocale.js +0 -5
  219. package/dist/src/react/useLocale.js.map +0 -1
  220. package/dist/src/react/useMessages.d.ts +0 -1
  221. package/dist/src/react/useMessages.js +0 -5
  222. package/dist/src/react/useMessages.js.map +0 -1
  223. package/dist/src/react/useNow.js +0 -40
  224. package/dist/src/react/useNow.js.map +0 -1
  225. package/dist/src/react/useTimeZone.js +0 -5
  226. package/dist/src/react/useTimeZone.js.map +0 -1
  227. package/dist/src/react/useTranslations.js +0 -21
  228. package/dist/src/react/useTranslations.js.map +0 -1
  229. package/dist/src/react/useTranslationsImpl.js +0 -35
  230. package/dist/src/react/useTranslationsImpl.js.map +0 -1
  231. package/dist/use-intl.cjs.development.js +0 -863
  232. package/dist/use-intl.cjs.development.js.map +0 -1
  233. package/dist/use-intl.cjs.production.min.js +0 -2
  234. package/dist/use-intl.cjs.production.min.js.map +0 -1
  235. package/dist/use-intl.esm.js +0 -13
  236. package/dist/use-intl.esm.js.map +0 -1
  237. package/src/core/AbstractIntlMessages.tsx +0 -10
  238. package/src/core/DateTimeFormatOptions.tsx +0 -130
  239. package/src/core/Formats.tsx +0 -10
  240. package/src/core/IntlConfig.tsx +0 -59
  241. package/src/core/IntlError.tsx +0 -27
  242. package/src/core/MessageFormatCache.tsx +0 -10
  243. package/src/core/NumberFormatOptions.tsx +0 -3
  244. package/src/core/RelativeTimeFormatOptions.tsx +0 -6
  245. package/src/core/TimeZone.tsx +0 -426
  246. package/src/core/TranslationValues.tsx +0 -22
  247. package/src/core/convertFormatsToIntlMessageFormat.tsx +0 -63
  248. package/src/core/createBaseTranslator.tsx +0 -352
  249. package/src/core/createFormatter.tsx +0 -242
  250. package/src/core/createIntl.tsx +0 -13
  251. package/src/core/createTranslator.tsx +0 -111
  252. package/src/core/createTranslatorImpl.tsx +0 -87
  253. package/src/core/defaults.tsx +0 -18
  254. package/src/core/index.tsx +0 -14
  255. package/src/core/resolveNamespace.tsx +0 -12
  256. package/src/core/utils/MessageKeys.tsx +0 -9
  257. package/src/core/utils/NamespaceKeys.tsx +0 -9
  258. package/src/core/utils/NestedKeyOf.tsx +0 -9
  259. package/src/core/utils/NestedValueOf.tsx +0 -12
  260. package/src/core/validateMessages.tsx +0 -77
  261. package/src/index.tsx +0 -2
  262. package/src/react/IntlContext.tsx +0 -12
  263. package/src/react/IntlProvider.tsx +0 -23
  264. package/src/react/getInitializedConfig.tsx +0 -29
  265. package/src/react/index.tsx +0 -10
  266. package/src/react/useFormatter.tsx +0 -19
  267. package/src/react/useIntl.tsx +0 -29
  268. package/src/react/useIntlContext.tsx +0 -16
  269. package/src/react/useLocale.tsx +0 -5
  270. package/src/react/useMessages.tsx +0 -5
  271. package/src/react/useNow.tsx +0 -49
  272. package/src/react/useTimeZone.tsx +0 -5
  273. package/src/react/useTranslations.tsx +0 -103
  274. package/src/react/useTranslationsImpl.tsx +0 -61
  275. /package/dist/{src → types/src}/core/AbstractIntlMessages.d.ts +0 -0
  276. /package/dist/{src → types/src}/core/DateTimeFormatOptions.d.ts +0 -0
  277. /package/dist/{src → types/src}/core/Formats.d.ts +0 -0
  278. /package/dist/{src → types/src}/core/IntlError.d.ts +0 -0
  279. /package/dist/{src → types/src}/core/MessageFormatCache.d.ts +0 -0
  280. /package/dist/{src → types/src}/core/NumberFormatOptions.d.ts +0 -0
  281. /package/dist/{src → types/src}/core/RelativeTimeFormatOptions.d.ts +0 -0
  282. /package/dist/{src → types/src}/core/TimeZone.d.ts +0 -0
  283. /package/dist/{src → types/src}/core/convertFormatsToIntlMessageFormat.d.ts +0 -0
  284. /package/dist/{src → types/src}/core/createFormatter.d.ts +0 -0
  285. /package/dist/{src → types/src}/core/defaults.d.ts +0 -0
  286. /package/dist/{src → types/src}/core/resolveNamespace.d.ts +0 -0
  287. /package/dist/{src → types/src}/core/utils/MessageKeys.d.ts +0 -0
  288. /package/dist/{src → types/src}/core/utils/NamespaceKeys.d.ts +0 -0
  289. /package/dist/{src → types/src}/core/utils/NestedKeyOf.d.ts +0 -0
  290. /package/dist/{src → types/src}/core/utils/NestedValueOf.d.ts +0 -0
  291. /package/dist/{src → types/src}/core/validateMessages.d.ts +0 -0
  292. /package/dist/{src → types/src}/index.d.ts +0 -0
  293. /package/dist/{src → types/src}/react/IntlContext.d.ts +0 -0
  294. /package/dist/{src → types/src}/react/useFormatter.d.ts +0 -0
  295. /package/dist/{src → types/src}/react/useIntlContext.d.ts +0 -0
  296. /package/dist/{src → types/src}/react/useLocale.d.ts +0 -0
  297. /package/dist/{src → types/src}/react/useNow.d.ts +0 -0
  298. /package/dist/{src → types/src}/react/useTimeZone.d.ts +0 -0
@@ -1,87 +0,0 @@
1
- import AbstractIntlMessages from './AbstractIntlMessages';
2
- import {InitializedIntlConfig} from './IntlConfig';
3
- import IntlError, {IntlErrorCode} from './IntlError';
4
- import {RichTranslationValues, TranslationValue} from './TranslationValues';
5
- import createBaseTranslator, {getMessagesOrError} from './createBaseTranslator';
6
- import resolveNamespace from './resolveNamespace';
7
- import NestedKeyOf from './utils/NestedKeyOf';
8
-
9
- export type CoreRichTranslationValues = Record<
10
- string,
11
- TranslationValue | ((chunks: string) => string)
12
- >;
13
-
14
- export type CreateTranslatorImplProps<Messages> = Omit<
15
- InitializedIntlConfig,
16
- 'messages'
17
- > & {
18
- namespace: string;
19
- messages: Messages;
20
- };
21
-
22
- export default function createTranslatorImpl<
23
- Messages extends AbstractIntlMessages,
24
- NestedKey extends NestedKeyOf<Messages>
25
- >(
26
- {
27
- getMessageFallback,
28
- messages,
29
- namespace,
30
- onError,
31
- ...rest
32
- }: CreateTranslatorImplProps<Messages>,
33
- namespacePrefix: string
34
- ) {
35
- // The `namespacePrefix` is part of the type system.
36
- // See the comment in the function invocation.
37
- messages = messages[namespacePrefix] as Messages;
38
- namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;
39
-
40
- const translator = createBaseTranslator<Messages, NestedKey>({
41
- ...rest,
42
- onError,
43
- getMessageFallback,
44
- messagesOrError: getMessagesOrError({
45
- messages,
46
- namespace,
47
- onError
48
- }) as Messages | IntlError
49
- });
50
-
51
- const originalRich = translator.rich;
52
-
53
- function base(...args: Parameters<typeof translator>) {
54
- return translator(...args);
55
- }
56
-
57
- // Augment `t.rich` to return plain strings
58
- base.rich = (
59
- key: Parameters<typeof originalRich>[0],
60
- /** Key value pairs for values to interpolate into the message. */
61
- values: CoreRichTranslationValues,
62
- formats?: Parameters<typeof originalRich>[2]
63
- ): string => {
64
- // `chunks` is returned as a string when no React element
65
- // is used, therefore it's safe to cast this type.
66
- const result = originalRich(key, values as RichTranslationValues, formats);
67
-
68
- // When only string chunks are provided to the parser, only strings should be returned here.
69
- if (typeof result !== 'string') {
70
- const error = new IntlError(
71
- IntlErrorCode.FORMATTING_ERROR,
72
- process.env.NODE_ENV !== 'production'
73
- ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})"
74
- : undefined
75
- );
76
-
77
- onError(error);
78
- return getMessageFallback({error, key, namespace});
79
- }
80
-
81
- return result;
82
- };
83
-
84
- base.raw = translator.raw;
85
-
86
- return base;
87
- }
@@ -1,18 +0,0 @@
1
- import IntlError from './IntlError';
2
-
3
- /**
4
- * Contains defaults that are used for all entry points into the core.
5
- * See also `InitializedIntlConfiguration`.
6
- */
7
-
8
- export function defaultGetMessageFallback(props: {
9
- error: IntlError;
10
- key: string;
11
- namespace?: string;
12
- }) {
13
- return [props.namespace, props.key].filter((part) => part != null).join('.');
14
- }
15
-
16
- export function defaultOnError(error: IntlError) {
17
- console.error(error);
18
- }
@@ -1,14 +0,0 @@
1
- export type {default as AbstractIntlMessages} from './AbstractIntlMessages';
2
- export type {
3
- default as TranslationValues,
4
- RichTranslationValues
5
- } from './TranslationValues';
6
- export type {default as Formats} from './Formats';
7
- export type {default as DateTimeFormatOptions} from './DateTimeFormatOptions';
8
- export type {default as NumberFormatOptions} from './NumberFormatOptions';
9
- export {default as IntlError, IntlErrorCode} from './IntlError';
10
- export {default as createTranslator} from './createTranslator';
11
- export {default as createFormatter} from './createFormatter';
12
-
13
- // TODO: Remove in next major version
14
- export {default as createIntl} from './createIntl';
@@ -1,12 +0,0 @@
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
- export default function resolveNamespace(
6
- namespace: string,
7
- namespacePrefix: string
8
- ) {
9
- return namespace === namespacePrefix
10
- ? undefined
11
- : namespace.slice((namespacePrefix + '.').length);
12
- }
@@ -1,9 +0,0 @@
1
- import NestedValueOf from './NestedValueOf';
2
-
3
- type MessageKeys<ObjectType, Keys extends string> = {
4
- [Property in Keys]: NestedValueOf<ObjectType, Property> extends string
5
- ? Property
6
- : never;
7
- }[Keys];
8
-
9
- export default MessageKeys;
@@ -1,9 +0,0 @@
1
- import NestedValueOf from './NestedValueOf';
2
-
3
- type NamespaceKeys<ObjectType, Keys extends string> = {
4
- [Property in Keys]: NestedValueOf<ObjectType, Property> extends string
5
- ? never
6
- : Property;
7
- }[Keys];
8
-
9
- export default NamespaceKeys;
@@ -1,9 +0,0 @@
1
- type NestedKeyOf<ObjectType> = ObjectType extends object
2
- ? {
3
- [Key in keyof ObjectType]:
4
- | `${Key & string}`
5
- | `${Key & string}.${NestedKeyOf<ObjectType[Key]>}`;
6
- }[keyof ObjectType]
7
- : never;
8
-
9
- export default NestedKeyOf;
@@ -1,12 +0,0 @@
1
- type NestedValueOf<
2
- ObjectType,
3
- Property extends string
4
- > = Property extends `${infer Key}.${infer Rest}`
5
- ? Key extends keyof ObjectType
6
- ? NestedValueOf<ObjectType[Key], Rest>
7
- : never
8
- : Property extends keyof ObjectType
9
- ? ObjectType[Property]
10
- : never;
11
-
12
- export default NestedValueOf;
@@ -1,77 +0,0 @@
1
- import AbstractIntlMessages from './AbstractIntlMessages';
2
- import IntlError, {IntlErrorCode} from './IntlError';
3
-
4
- function validateMessagesSegment(
5
- messages: AbstractIntlMessages,
6
- invalidKeyLabels: Array<string>,
7
- parentPath?: string
8
- ) {
9
- Object.entries(messages).forEach(([key, messageOrMessages]) => {
10
- if (key.includes('.')) {
11
- let keyLabel = key;
12
- if (parentPath) keyLabel += ` (at ${parentPath})`;
13
- invalidKeyLabels.push(keyLabel);
14
- }
15
-
16
- if (messageOrMessages != null && typeof messageOrMessages === 'object') {
17
- validateMessagesSegment(
18
- messageOrMessages,
19
- invalidKeyLabels,
20
- [parentPath, key].filter((part) => part != null).join('.')
21
- );
22
- }
23
- });
24
- }
25
-
26
- export default function validateMessages(
27
- messages: AbstractIntlMessages,
28
- onError: (error: IntlError) => void
29
- ) {
30
- const invalidKeyLabels: Array<string> = [];
31
- validateMessagesSegment(messages, invalidKeyLabels);
32
-
33
- if (invalidKeyLabels.length > 0) {
34
- onError(
35
- new IntlError(
36
- IntlErrorCode.INVALID_KEY,
37
- process.env.NODE_ENV !== 'production'
38
- ? `Namespace keys can not contain the character "." as this is used to express nesting. Please remove it or replace it with another character.
39
-
40
- Invalid ${
41
- invalidKeyLabels.length === 1 ? 'key' : 'keys'
42
- }: ${invalidKeyLabels.join(', ')}
43
-
44
- If you're migrating from a flat structure, you can convert your messages as follows:
45
-
46
- import {set} from "lodash";
47
-
48
- const input = {
49
- "one.one": "1.1",
50
- "one.two": "1.2",
51
- "two.one.one": "2.1.1"
52
- };
53
-
54
- const output = Object.entries(input).reduce(
55
- (acc, [key, value]) => set(acc, key, value),
56
- {}
57
- );
58
-
59
- // Output:
60
- //
61
- // {
62
- // "one": {
63
- // "one": "1.1",
64
- // "two": "1.2"
65
- // },
66
- // "two": {
67
- // "one": {
68
- // "one": "2.1.1"
69
- // }
70
- // }
71
- // }
72
- `
73
- : undefined
74
- )
75
- );
76
- }
77
- }
package/src/index.tsx DELETED
@@ -1,2 +0,0 @@
1
- export * from './core';
2
- export * from './react';
@@ -1,12 +0,0 @@
1
- import {createContext} from 'react';
2
- import {InitializedIntlConfig} from '../core/IntlConfig';
3
- import MessageFormatCache from '../core/MessageFormatCache';
4
-
5
- const IntlContext = createContext<
6
- | (InitializedIntlConfig & {
7
- messageFormatCache?: MessageFormatCache;
8
- })
9
- | undefined
10
- >(undefined);
11
-
12
- export default IntlContext;
@@ -1,23 +0,0 @@
1
- import React, {ReactNode, useState} from 'react';
2
- import IntlConfig from '../core/IntlConfig';
3
- import IntlContext from './IntlContext';
4
- import getInitializedConfig from './getInitializedConfig';
5
-
6
- type Props = IntlConfig & {
7
- children: ReactNode;
8
- };
9
-
10
- export default function IntlProvider({children, ...props}: Props) {
11
- const [messageFormatCache] = useState(() => new Map());
12
-
13
- return (
14
- <IntlContext.Provider
15
- value={{
16
- ...getInitializedConfig(props),
17
- messageFormatCache
18
- }}
19
- >
20
- {children}
21
- </IntlContext.Provider>
22
- );
23
- }
@@ -1,29 +0,0 @@
1
- import IntlConfig from '../core/IntlConfig';
2
- import {defaultGetMessageFallback, defaultOnError} from '../core/defaults';
3
- import validateMessages from '../core/validateMessages';
4
-
5
- /**
6
- * Enhances the incoming props with defaults.
7
- */
8
- export default function getInitializedConfig<
9
- // This is a generic to allow for stricter typing. E.g.
10
- // the RSC integration always provides a `now` value.
11
- Props extends Omit<IntlConfig, 'children'>
12
- >({getMessageFallback, messages, onError, ...rest}: Props) {
13
- const finalOnError = onError || defaultOnError;
14
- const finalGetMessageFallback =
15
- getMessageFallback || defaultGetMessageFallback;
16
-
17
- if (process.env.NODE_ENV !== 'production') {
18
- if (messages) {
19
- validateMessages(messages, finalOnError);
20
- }
21
- }
22
-
23
- return {
24
- ...rest,
25
- messages,
26
- onError: finalOnError,
27
- getMessageFallback: finalGetMessageFallback
28
- };
29
- }
@@ -1,10 +0,0 @@
1
- export {default as IntlProvider} from './IntlProvider';
2
- export {default as useTranslations} from './useTranslations';
3
- export {default as useLocale} from './useLocale';
4
- export {default as useNow} from './useNow';
5
- export {default as useTimeZone} from './useTimeZone';
6
- export {default as useMessages} from './useMessages';
7
- export {default as useFormatter} from './useFormatter';
8
-
9
- // TODO: Remove in next major version
10
- export {default as useIntl} from './useIntl';
@@ -1,19 +0,0 @@
1
- import {useMemo} from 'react';
2
- import createFormatter from '../core/createFormatter';
3
- import useIntlContext from './useIntlContext';
4
-
5
- export default function useFormatter() {
6
- const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();
7
-
8
- return useMemo(
9
- () =>
10
- createFormatter({
11
- formats,
12
- locale,
13
- now: globalNow,
14
- onError,
15
- timeZone
16
- }),
17
- [formats, globalNow, locale, onError, timeZone]
18
- );
19
- }
@@ -1,29 +0,0 @@
1
- import {useMemo} from 'react';
2
- import createIntl from '../core/createIntl';
3
- import useIntlContext from './useIntlContext';
4
-
5
- let hasWarned = false;
6
-
7
- /** @deprecated Switch to `useFormatter` instead. */
8
- export default function useIntl() {
9
- const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();
10
-
11
- if (!hasWarned) {
12
- hasWarned = true;
13
- console.warn(
14
- '`useIntl()` is deprecated and will be removed in the next major version. Please switch to `useFormatter()`.'
15
- );
16
- }
17
-
18
- return useMemo(
19
- () =>
20
- createIntl({
21
- formats,
22
- locale,
23
- now: globalNow,
24
- onError,
25
- timeZone
26
- }),
27
- [formats, globalNow, locale, onError, timeZone]
28
- );
29
- }
@@ -1,16 +0,0 @@
1
- import {useContext} from 'react';
2
- import IntlContext from './IntlContext';
3
-
4
- export default function useIntlContext() {
5
- const context = useContext(IntlContext);
6
-
7
- if (!context) {
8
- throw new Error(
9
- process.env.NODE_ENV !== 'production'
10
- ? 'No intl context found. Have you configured the provider?'
11
- : undefined
12
- );
13
- }
14
-
15
- return context;
16
- }
@@ -1,5 +0,0 @@
1
- import useIntlContext from './useIntlContext';
2
-
3
- export default function useLocale() {
4
- return useIntlContext().locale;
5
- }
@@ -1,5 +0,0 @@
1
- import useIntlContext from './useIntlContext';
2
-
3
- export default function useMessages() {
4
- return useIntlContext().messages;
5
- }
@@ -1,49 +0,0 @@
1
- import {useState, useEffect} from 'react';
2
- import useIntlContext from './useIntlContext';
3
-
4
- type Options = {
5
- updateInterval?: number;
6
- };
7
-
8
- function getNow() {
9
- return new Date();
10
- }
11
-
12
- /**
13
- * Reading the current date via `new Date()` in components should be avoided, as
14
- * it causes components to be impure and can lead to flaky tests. Instead, this
15
- * hook can be used.
16
- *
17
- * By default, it returns the time when the component mounts. If `updateInterval`
18
- * is specified, the value will be updated based on the interval.
19
- *
20
- * You can however also return a static value from this hook, if you
21
- * configure the `now` parameter on the context provider. Note however,
22
- * that if `updateInterval` is configured in this case, the component
23
- * will initialize with the global value, but will afterwards update
24
- * continuously based on the interval.
25
- *
26
- * For unit tests, this can be mocked to a constant value. For end-to-end
27
- * testing, an environment parameter can be passed to the `now` parameter
28
- * of the provider to mock this to a static value.
29
- */
30
- export default function useNow(options?: Options) {
31
- const updateInterval = options?.updateInterval;
32
-
33
- const {now: globalNow} = useIntlContext();
34
- const [now, setNow] = useState(globalNow || getNow());
35
-
36
- useEffect(() => {
37
- if (!updateInterval) return;
38
-
39
- const intervalId = setInterval(() => {
40
- setNow(getNow());
41
- }, updateInterval);
42
-
43
- return () => {
44
- clearInterval(intervalId);
45
- };
46
- }, [globalNow, updateInterval]);
47
-
48
- return now;
49
- }
@@ -1,5 +0,0 @@
1
- import useIntlContext from './useIntlContext';
2
-
3
- export default function useTimeZone() {
4
- return useIntlContext().timeZone;
5
- }
@@ -1,103 +0,0 @@
1
- import {ReactElement, ReactNodeArray} from 'react';
2
- import Formats from '../core/Formats';
3
- import TranslationValues, {
4
- RichTranslationValues
5
- } from '../core/TranslationValues';
6
- import MessageKeys from '../core/utils/MessageKeys';
7
- import NamespaceKeys from '../core/utils/NamespaceKeys';
8
- import NestedKeyOf from '../core/utils/NestedKeyOf';
9
- import NestedValueOf from '../core/utils/NestedValueOf';
10
- import useIntlContext from './useIntlContext';
11
- import useTranslationsImpl from './useTranslationsImpl';
12
-
13
- /**
14
- * Translates messages from the given namespace by using the ICU syntax.
15
- * See https://formatjs.io/docs/core-concepts/icu-syntax.
16
- *
17
- * If no namespace is provided, all available messages are returned.
18
- * The namespace can also indicate nesting by using a dot
19
- * (e.g. `namespace.Component`).
20
- */
21
- export default function useTranslations<
22
- NestedKey extends NamespaceKeys<
23
- IntlMessages,
24
- NestedKeyOf<IntlMessages>
25
- > = never
26
- >(
27
- namespace?: NestedKey
28
- ): // Explicitly defining the return type is necessary as TypeScript would get it wrong
29
- {
30
- // Default invocation
31
- <
32
- TargetKey extends MessageKeys<
33
- NestedValueOf<
34
- {'!': IntlMessages},
35
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
36
- >,
37
- NestedKeyOf<
38
- NestedValueOf<
39
- {'!': IntlMessages},
40
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
41
- >
42
- >
43
- >
44
- >(
45
- key: TargetKey,
46
- values?: TranslationValues,
47
- formats?: Partial<Formats>
48
- ): string;
49
-
50
- // `rich`
51
- rich<
52
- TargetKey extends MessageKeys<
53
- NestedValueOf<
54
- {'!': IntlMessages},
55
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
56
- >,
57
- NestedKeyOf<
58
- NestedValueOf<
59
- {'!': IntlMessages},
60
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
61
- >
62
- >
63
- >
64
- >(
65
- key: TargetKey,
66
- values?: RichTranslationValues,
67
- formats?: Partial<Formats>
68
- ): string | ReactElement | ReactNodeArray;
69
-
70
- // `raw`
71
- raw<
72
- TargetKey extends MessageKeys<
73
- NestedValueOf<
74
- {'!': IntlMessages},
75
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
76
- >,
77
- NestedKeyOf<
78
- NestedValueOf<
79
- {'!': IntlMessages},
80
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
81
- >
82
- >
83
- >
84
- >(
85
- key: TargetKey
86
- ): any;
87
- } {
88
- const context = useIntlContext();
89
- const messages = context.messages as IntlMessages;
90
-
91
- // We have to wrap the actual hook so the type inference for the optional
92
- // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
93
- // The prefix ("!") is arbitrary.
94
- return useTranslationsImpl<
95
- {'!': IntlMessages},
96
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
97
- >(
98
- {'!': messages},
99
- // @ts-expect-error
100
- namespace ? `!.${namespace}` : '!',
101
- '!'
102
- );
103
- }
@@ -1,61 +0,0 @@
1
- import {useMemo} from 'react';
2
- import AbstractIntlMessages from '../core/AbstractIntlMessages';
3
- import createBaseTranslator, {
4
- getMessagesOrError
5
- } from '../core/createBaseTranslator';
6
- import resolveNamespace from '../core/resolveNamespace';
7
- import NestedKeyOf from '../core/utils/NestedKeyOf';
8
- import useIntlContext from './useIntlContext';
9
-
10
- export default function useTranslationsImpl<
11
- Messages extends AbstractIntlMessages,
12
- NestedKey extends NestedKeyOf<Messages>
13
- >(allMessages: Messages, namespace: NestedKey, namespacePrefix: string) {
14
- const {
15
- defaultTranslationValues,
16
- formats: globalFormats,
17
- getMessageFallback,
18
- locale,
19
- messageFormatCache,
20
- onError,
21
- timeZone
22
- } = useIntlContext();
23
-
24
- // The `namespacePrefix` is part of the type system.
25
- // See the comment in the hook invocation.
26
- allMessages = allMessages[namespacePrefix] as Messages;
27
- namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;
28
-
29
- const messagesOrError = useMemo(
30
- () => getMessagesOrError({messages: allMessages, namespace, onError}),
31
- [allMessages, namespace, onError]
32
- );
33
-
34
- const translate = useMemo(
35
- () =>
36
- createBaseTranslator({
37
- messageFormatCache,
38
- getMessageFallback,
39
- messagesOrError,
40
- defaultTranslationValues,
41
- namespace,
42
- onError,
43
- formats: globalFormats,
44
- locale,
45
- timeZone
46
- }),
47
- [
48
- messageFormatCache,
49
- getMessageFallback,
50
- messagesOrError,
51
- defaultTranslationValues,
52
- namespace,
53
- onError,
54
- globalFormats,
55
- locale,
56
- timeZone
57
- ]
58
- );
59
-
60
- return translate;
61
- }
File without changes
File without changes
File without changes
File without changes