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,352 +0,0 @@
1
- // eslint-disable-next-line import/no-named-as-default -- False positive
2
- import IntlMessageFormat from 'intl-messageformat';
3
- import {
4
- cloneElement,
5
- isValidElement,
6
- ReactElement,
7
- ReactNode,
8
- ReactNodeArray
9
- } from 'react';
10
- import AbstractIntlMessages from './AbstractIntlMessages';
11
- import Formats from './Formats';
12
- import {InitializedIntlConfig} from './IntlConfig';
13
- import IntlError, {IntlErrorCode} from './IntlError';
14
- import MessageFormatCache from './MessageFormatCache';
15
- import TranslationValues, {RichTranslationValues} from './TranslationValues';
16
- import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
17
- import {defaultGetMessageFallback, defaultOnError} from './defaults';
18
- import MessageKeys from './utils/MessageKeys';
19
- import NestedKeyOf from './utils/NestedKeyOf';
20
- import NestedValueOf from './utils/NestedValueOf';
21
-
22
- function resolvePath(
23
- messages: AbstractIntlMessages | undefined,
24
- key: string,
25
- namespace?: string
26
- ) {
27
- if (!messages) {
28
- throw new Error(
29
- process.env.NODE_ENV !== 'production'
30
- ? `No messages available at \`${namespace}\`.`
31
- : undefined
32
- );
33
- }
34
-
35
- let message = messages;
36
-
37
- key.split('.').forEach((part) => {
38
- const next = (message as any)[part];
39
-
40
- if (part == null || next == null) {
41
- throw new Error(
42
- process.env.NODE_ENV !== 'production'
43
- ? `Could not resolve \`${key}\` in ${
44
- namespace ? `\`${namespace}\`` : 'messages'
45
- }.`
46
- : undefined
47
- );
48
- }
49
-
50
- message = next;
51
- });
52
-
53
- return message;
54
- }
55
-
56
- function prepareTranslationValues(values: RichTranslationValues) {
57
- if (Object.keys(values).length === 0) return undefined;
58
-
59
- // Workaround for https://github.com/formatjs/formatjs/issues/1467
60
- const transformedValues: RichTranslationValues = {};
61
- Object.keys(values).forEach((key) => {
62
- let index = 0;
63
- const value = values[key];
64
-
65
- let transformed;
66
- if (typeof value === 'function') {
67
- transformed = (chunks: ReactNode) => {
68
- const result = value(chunks);
69
-
70
- return isValidElement(result)
71
- ? cloneElement(result, {key: key + index++})
72
- : result;
73
- };
74
- } else {
75
- transformed = value;
76
- }
77
-
78
- transformedValues[key] = transformed;
79
- });
80
-
81
- return transformedValues;
82
- }
83
-
84
- export function getMessagesOrError<Messages extends AbstractIntlMessages>({
85
- messages,
86
- namespace,
87
- onError = defaultOnError
88
- }: {
89
- messages: Messages;
90
- namespace?: string;
91
- onError?(error: IntlError): void;
92
- }) {
93
- try {
94
- if (!messages) {
95
- throw new Error(
96
- process.env.NODE_ENV !== 'production'
97
- ? `No messages were configured on the provider.`
98
- : undefined
99
- );
100
- }
101
-
102
- const retrievedMessages = namespace
103
- ? resolvePath(messages, namespace)
104
- : messages;
105
-
106
- if (!retrievedMessages) {
107
- throw new Error(
108
- process.env.NODE_ENV !== 'production'
109
- ? `No messages for namespace \`${namespace}\` found.`
110
- : undefined
111
- );
112
- }
113
-
114
- return retrievedMessages;
115
- } catch (error) {
116
- const intlError = new IntlError(
117
- IntlErrorCode.MISSING_MESSAGE,
118
- (error as Error).message
119
- );
120
- onError(intlError);
121
- return intlError;
122
- }
123
- }
124
-
125
- export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
126
- messageFormatCache?: MessageFormatCache;
127
- defaultTranslationValues?: RichTranslationValues;
128
- namespace?: string;
129
- messagesOrError: Messages | IntlError;
130
- };
131
-
132
- function getPlainMessage(candidate: string, values?: unknown) {
133
- if (values) return undefined;
134
-
135
- const unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
136
-
137
- // Placeholders can be in the message if there are default values,
138
- // or if the user has forgotten to provide values. In the latter
139
- // case we need to compile the message to receive an error.
140
- const hasPlaceholders = /<|{/.test(unescapedMessage);
141
-
142
- if (!hasPlaceholders) {
143
- return unescapedMessage;
144
- }
145
-
146
- return undefined;
147
- }
148
-
149
- export default function createBaseTranslator<
150
- Messages extends AbstractIntlMessages,
151
- NestedKey extends NestedKeyOf<Messages>
152
- >({
153
- defaultTranslationValues,
154
- formats: globalFormats,
155
- getMessageFallback = defaultGetMessageFallback,
156
- locale,
157
- messageFormatCache,
158
- messagesOrError,
159
- namespace,
160
- onError,
161
- timeZone
162
- }: CreateBaseTranslatorProps<Messages>) {
163
- function getFallbackFromErrorAndNotify(
164
- key: string,
165
- code: IntlErrorCode,
166
- message?: string
167
- ) {
168
- const error = new IntlError(code, message);
169
- onError(error);
170
- return getMessageFallback({error, key, namespace});
171
- }
172
-
173
- function translateBaseFn(
174
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
175
- key: string,
176
- /** Key value pairs for values to interpolate into the message. */
177
- values?: RichTranslationValues,
178
- /** Provide custom formats for numbers, dates and times. */
179
- formats?: Partial<Formats>
180
- ): string | ReactElement | ReactNodeArray {
181
- if (messagesOrError instanceof IntlError) {
182
- // We have already warned about this during render
183
- return getMessageFallback({
184
- error: messagesOrError,
185
- key,
186
- namespace
187
- });
188
- }
189
- const messages = messagesOrError;
190
-
191
- let message;
192
- try {
193
- message = resolvePath(messages, key, namespace);
194
- } catch (error) {
195
- return getFallbackFromErrorAndNotify(
196
- key,
197
- IntlErrorCode.MISSING_MESSAGE,
198
- (error as Error).message
199
- );
200
- }
201
-
202
- function joinPath(parts: Array<string | undefined>) {
203
- return parts.filter((part) => part != null).join('.');
204
- }
205
-
206
- const cacheKey = joinPath([locale, namespace, key, String(message)]);
207
-
208
- let messageFormat: IntlMessageFormat;
209
- if (messageFormatCache?.has(cacheKey)) {
210
- messageFormat = messageFormatCache.get(cacheKey)!;
211
- } else {
212
- if (typeof message === 'object') {
213
- let code, errorMessage;
214
- if (Array.isArray(message)) {
215
- code = IntlErrorCode.INVALID_MESSAGE;
216
- if (process.env.NODE_ENV !== 'production') {
217
- errorMessage = `Message at \`${joinPath([
218
- namespace,
219
- key
220
- ])}\` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages`;
221
- }
222
- } else {
223
- code = IntlErrorCode.INSUFFICIENT_PATH;
224
- if (process.env.NODE_ENV !== 'production') {
225
- errorMessage = `Message at \`${joinPath([
226
- namespace,
227
- key
228
- ])}\` resolved to an object, but only strings are supported. Use a \`.\` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages`;
229
- }
230
- }
231
-
232
- return getFallbackFromErrorAndNotify(key, code, errorMessage);
233
- }
234
-
235
- // Hot path that avoids creating an `IntlMessageFormat` instance
236
- const plainMessage = getPlainMessage(message as string, values);
237
- if (plainMessage) return plainMessage;
238
-
239
- try {
240
- messageFormat = new IntlMessageFormat(
241
- message,
242
- locale,
243
- convertFormatsToIntlMessageFormat(
244
- {...globalFormats, ...formats},
245
- timeZone
246
- )
247
- );
248
- } catch (error) {
249
- return getFallbackFromErrorAndNotify(
250
- key,
251
- IntlErrorCode.INVALID_MESSAGE,
252
- (error as Error).message
253
- );
254
- }
255
-
256
- messageFormatCache?.set(cacheKey, messageFormat);
257
- }
258
-
259
- try {
260
- const formattedMessage = messageFormat.format(
261
- // @ts-ignore `intl-messageformat` expects a different format
262
- // for rich text elements since a recent minor update. This
263
- // needs to be evaluated in detail, possibly also in regards
264
- // to be able to format to parts.
265
- prepareTranslationValues({...defaultTranslationValues, ...values})
266
- );
267
-
268
- if (formattedMessage == null) {
269
- throw new Error(
270
- process.env.NODE_ENV !== 'production'
271
- ? `Unable to format \`${key}\` in ${
272
- namespace ? `namespace \`${namespace}\`` : 'messages'
273
- }`
274
- : undefined
275
- );
276
- }
277
-
278
- // Limit the function signature to return strings or React elements
279
- return isValidElement(formattedMessage) ||
280
- // Arrays of React elements
281
- Array.isArray(formattedMessage) ||
282
- typeof formattedMessage === 'string'
283
- ? formattedMessage
284
- : String(formattedMessage);
285
- } catch (error) {
286
- return getFallbackFromErrorAndNotify(
287
- key,
288
- IntlErrorCode.FORMATTING_ERROR,
289
- (error as Error).message
290
- );
291
- }
292
- }
293
-
294
- function translateFn<
295
- TargetKey extends MessageKeys<
296
- NestedValueOf<Messages, NestedKey>,
297
- NestedKeyOf<NestedValueOf<Messages, NestedKey>>
298
- >
299
- >(
300
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
301
- key: TargetKey,
302
- /** Key value pairs for values to interpolate into the message. */
303
- values?: TranslationValues,
304
- /** Provide custom formats for numbers, dates and times. */
305
- formats?: Partial<Formats>
306
- ): string {
307
- const result = translateBaseFn(key, values, formats);
308
-
309
- if (typeof result !== 'string') {
310
- return getFallbackFromErrorAndNotify(
311
- key,
312
- IntlErrorCode.INVALID_MESSAGE,
313
- process.env.NODE_ENV !== 'production'
314
- ? `The message \`${key}\` in ${
315
- namespace ? `namespace \`${namespace}\`` : 'messages'
316
- } didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
317
- : undefined
318
- );
319
- }
320
-
321
- return result;
322
- }
323
-
324
- translateFn.rich = translateBaseFn;
325
-
326
- translateFn.raw = (
327
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
328
- key: string
329
- ): any => {
330
- if (messagesOrError instanceof IntlError) {
331
- // We have already warned about this during render
332
- return getMessageFallback({
333
- error: messagesOrError,
334
- key,
335
- namespace
336
- });
337
- }
338
- const messages = messagesOrError;
339
-
340
- try {
341
- return resolvePath(messages, key, namespace);
342
- } catch (error) {
343
- return getFallbackFromErrorAndNotify(
344
- key,
345
- IntlErrorCode.MISSING_MESSAGE,
346
- (error as Error).message
347
- );
348
- }
349
- };
350
-
351
- return translateFn;
352
- }
@@ -1,242 +0,0 @@
1
- import DateTimeFormatOptions from './DateTimeFormatOptions';
2
- import Formats from './Formats';
3
- import IntlError, {IntlErrorCode} from './IntlError';
4
- import NumberFormatOptions from './NumberFormatOptions';
5
- import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
6
- import TimeZone from './TimeZone';
7
- import {defaultOnError} from './defaults';
8
-
9
- const SECOND = 1;
10
- const MINUTE = SECOND * 60;
11
- const HOUR = MINUTE * 60;
12
- const DAY = HOUR * 24;
13
- const WEEK = DAY * 7;
14
- const MONTH = DAY * (365 / 12); // Approximation
15
- const QUARTER = MONTH * 3;
16
- const YEAR = DAY * 365;
17
-
18
- const UNIT_SECONDS: Record<Intl.RelativeTimeFormatUnit, number> = {
19
- second: SECOND,
20
- seconds: SECOND,
21
- minute: MINUTE,
22
- minutes: MINUTE,
23
- hour: HOUR,
24
- hours: HOUR,
25
- day: DAY,
26
- days: DAY,
27
- week: WEEK,
28
- weeks: WEEK,
29
- month: MONTH,
30
- months: MONTH,
31
- quarter: QUARTER,
32
- quarters: QUARTER,
33
- year: YEAR,
34
- years: YEAR
35
- } as const;
36
-
37
- function resolveRelativeTimeUnit(seconds: number) {
38
- const absValue = Math.abs(seconds);
39
-
40
- if (absValue < MINUTE) {
41
- return 'second';
42
- } else if (absValue < HOUR) {
43
- return 'minute';
44
- } else if (absValue < DAY) {
45
- return 'hour';
46
- } else if (absValue < WEEK) {
47
- return 'day';
48
- } else if (absValue < MONTH) {
49
- return 'week';
50
- } else if (absValue < YEAR) {
51
- return 'month';
52
- }
53
- return 'year';
54
- }
55
-
56
- function calculateRelativeTimeValue(
57
- seconds: number,
58
- unit: Intl.RelativeTimeFormatUnit
59
- ) {
60
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
61
- // will include fractions like '2.1 hours ago'.
62
- return Math.round(seconds / UNIT_SECONDS[unit]);
63
- }
64
-
65
- type Props = {
66
- locale: string;
67
- timeZone?: TimeZone;
68
- onError?(error: IntlError): void;
69
- formats?: Partial<Formats>;
70
- now?: Date;
71
- };
72
-
73
- export default function createFormatter({
74
- formats,
75
- locale,
76
- now: globalNow,
77
- onError = defaultOnError,
78
- timeZone: globalTimeZone
79
- }: Props) {
80
- function resolveFormatOrOptions<Options>(
81
- typeFormats: Record<string, Options> | undefined,
82
- formatOrOptions?: string | Options
83
- ) {
84
- let options;
85
- if (typeof formatOrOptions === 'string') {
86
- const formatName = formatOrOptions;
87
- options = typeFormats?.[formatName];
88
-
89
- if (!options) {
90
- const error = new IntlError(
91
- IntlErrorCode.MISSING_FORMAT,
92
- process.env.NODE_ENV !== 'production'
93
- ? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
94
- : undefined
95
- );
96
- onError(error);
97
- throw error;
98
- }
99
- } else {
100
- options = formatOrOptions;
101
- }
102
-
103
- return options;
104
- }
105
-
106
- function getFormattedValue<Value, Options>(
107
- value: Value,
108
- formatOrOptions: string | Options | undefined,
109
- typeFormats: Record<string, Options> | undefined,
110
- formatter: (options?: Options) => string
111
- ) {
112
- let options;
113
- try {
114
- options = resolveFormatOrOptions(typeFormats, formatOrOptions);
115
- } catch (error) {
116
- return String(value);
117
- }
118
-
119
- try {
120
- return formatter(options);
121
- } catch (error) {
122
- onError(
123
- new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)
124
- );
125
- return String(value);
126
- }
127
- }
128
-
129
- function dateTime(
130
- /** If a number is supplied, this is interpreted as a UTC timestamp. */
131
- value: Date | number,
132
- /** If a time zone is supplied, the `value` is converted to that time zone.
133
- * Otherwise the user time zone will be used. */
134
- formatOrOptions?: string | DateTimeFormatOptions
135
- ) {
136
- return getFormattedValue(
137
- value,
138
- formatOrOptions,
139
- formats?.dateTime,
140
- (options) => {
141
- if (!options?.timeZone) {
142
- if (globalTimeZone) {
143
- options = {...options, timeZone: globalTimeZone};
144
- } else {
145
- onError(
146
- new IntlError(
147
- IntlErrorCode.ENVIRONMENT_FALLBACK,
148
- process.env.NODE_ENV !== 'production'
149
- ? `The \`timeZone\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone`
150
- : undefined
151
- )
152
- );
153
- }
154
- }
155
-
156
- return new Intl.DateTimeFormat(locale, options).format(value);
157
- }
158
- );
159
- }
160
-
161
- function number(
162
- value: number | bigint,
163
- formatOrOptions?: string | NumberFormatOptions
164
- ) {
165
- return getFormattedValue(
166
- value,
167
- formatOrOptions,
168
- formats?.number,
169
- (options) => new Intl.NumberFormat(locale, options).format(value)
170
- );
171
- }
172
-
173
- function getGlobalNow() {
174
- if (globalNow) {
175
- return globalNow;
176
- } else {
177
- onError(
178
- new IntlError(
179
- IntlErrorCode.ENVIRONMENT_FALLBACK,
180
- process.env.NODE_ENV !== 'production'
181
- ? `The \`now\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`
182
- : undefined
183
- )
184
- );
185
- return new Date();
186
- }
187
- }
188
-
189
- function extractNowDate(
190
- nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions
191
- ) {
192
- if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
193
- return new Date(nowOrOptions);
194
- }
195
- if (nowOrOptions?.now !== undefined) {
196
- return new Date(nowOrOptions.now);
197
- }
198
- return getGlobalNow();
199
- }
200
-
201
- function relativeTime(
202
- /** The date time that needs to be formatted. */
203
- date: number | Date,
204
- /** The reference point in time to which `date` will be formatted in relation to. */
205
- nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions
206
- ) {
207
- try {
208
- const dateDate = new Date(date);
209
- const nowDate = extractNowDate(nowOrOptions);
210
- const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
211
-
212
- const unit =
213
- typeof nowOrOptions === 'number' ||
214
- nowOrOptions instanceof Date ||
215
- nowOrOptions?.unit === undefined
216
- ? resolveRelativeTimeUnit(seconds)
217
- : nowOrOptions.unit;
218
-
219
- const value = calculateRelativeTimeValue(seconds, unit);
220
-
221
- return new Intl.RelativeTimeFormat(locale, {
222
- numeric: 'auto'
223
- }).format(value, unit);
224
- } catch (error) {
225
- onError(
226
- new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)
227
- );
228
- return String(date);
229
- }
230
- }
231
-
232
- function list(
233
- value: Iterable<string>,
234
- formatOrOptions?: string | Intl.ListFormatOptions
235
- ) {
236
- return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>
237
- new Intl.ListFormat(locale, options).format(value)
238
- );
239
- }
240
-
241
- return {dateTime, number, relativeTime, list};
242
- }
@@ -1,13 +0,0 @@
1
- import createFormatter from './createFormatter';
2
-
3
- /** @deprecated Switch to `createFormatter` */
4
- export default function createIntl(
5
- ...args: Parameters<typeof createFormatter>
6
- ) {
7
- const formatter = createFormatter(...args);
8
- return {
9
- formatDateTime: formatter.dateTime,
10
- formatNumber: formatter.number,
11
- formatRelativeTime: formatter.relativeTime
12
- };
13
- }
@@ -1,111 +0,0 @@
1
- import Formats from './Formats';
2
- import IntlConfig from './IntlConfig';
3
- import TranslationValues from './TranslationValues';
4
- import createTranslatorImpl, {
5
- CoreRichTranslationValues
6
- } from './createTranslatorImpl';
7
- import {defaultGetMessageFallback, defaultOnError} from './defaults';
8
- import MessageKeys from './utils/MessageKeys';
9
- import NamespaceKeys from './utils/NamespaceKeys';
10
- import NestedKeyOf from './utils/NestedKeyOf';
11
- import NestedValueOf from './utils/NestedValueOf';
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 createTranslator<
22
- NestedKey extends NamespaceKeys<
23
- IntlMessages,
24
- NestedKeyOf<IntlMessages>
25
- > = never
26
- >({
27
- getMessageFallback = defaultGetMessageFallback,
28
- messages,
29
- namespace,
30
- onError = defaultOnError,
31
- ...rest
32
- }: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {
33
- messages: NonNullable<IntlConfig<IntlMessages>['messages']>;
34
- namespace?: NestedKey;
35
- }): // Explicitly defining the return type is necessary as TypeScript would get it wrong
36
- {
37
- // Default invocation
38
- <
39
- TargetKey extends MessageKeys<
40
- NestedValueOf<
41
- {'!': IntlMessages},
42
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
43
- >,
44
- NestedKeyOf<
45
- NestedValueOf<
46
- {'!': IntlMessages},
47
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
48
- >
49
- >
50
- >
51
- >(
52
- key: TargetKey,
53
- values?: TranslationValues,
54
- formats?: Partial<Formats>
55
- ): string;
56
-
57
- // `rich`
58
- rich<
59
- TargetKey extends MessageKeys<
60
- NestedValueOf<
61
- {'!': IntlMessages},
62
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
63
- >,
64
- NestedKeyOf<
65
- NestedValueOf<
66
- {'!': IntlMessages},
67
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
68
- >
69
- >
70
- >
71
- >(
72
- key: TargetKey,
73
- values?: CoreRichTranslationValues,
74
- formats?: Partial<Formats>
75
- ): string;
76
-
77
- // `raw`
78
- raw<
79
- TargetKey extends MessageKeys<
80
- NestedValueOf<
81
- {'!': IntlMessages},
82
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
83
- >,
84
- NestedKeyOf<
85
- NestedValueOf<
86
- {'!': IntlMessages},
87
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
88
- >
89
- >
90
- >
91
- >(
92
- key: TargetKey
93
- ): any;
94
- } {
95
- // We have to wrap the actual function so the type inference for the optional
96
- // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
97
- // The prefix ("!") is arbitrary.
98
- return createTranslatorImpl<
99
- {'!': IntlMessages},
100
- [NestedKey] extends [never] ? '!' : `!.${NestedKey}`
101
- >(
102
- {
103
- ...rest,
104
- onError,
105
- getMessageFallback,
106
- messages: {'!': messages},
107
- namespace: namespace ? `!.${namespace}` : '!'
108
- },
109
- '!'
110
- );
111
- }