use-intl 2.20.0 → 3.0.0-beta.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 (273) hide show
  1. package/core.d.ts +1 -0
  2. package/dist/_virtual/_rollupPluginBabelHelpers.esm.js +102 -0
  3. package/dist/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
  4. package/dist/core/IntlError.esm.js +33 -0
  5. package/dist/core/IntlError.esm.js.map +1 -0
  6. package/dist/core/TranslationValues.d.ts +1 -0
  7. package/dist/core/convertFormatsToIntlMessageFormat.esm.js +37 -0
  8. package/dist/core/convertFormatsToIntlMessageFormat.esm.js.map +1 -0
  9. package/dist/core/createBaseTranslator.d.ts +3 -8
  10. package/dist/core/createBaseTranslator.esm.js +210 -0
  11. package/dist/core/createBaseTranslator.esm.js.map +1 -0
  12. package/dist/core/createFormatter.esm.js +138 -0
  13. package/dist/core/createFormatter.esm.js.map +1 -0
  14. package/dist/core/createIntl.esm.js +14 -0
  15. package/dist/core/createIntl.esm.js.map +1 -0
  16. package/dist/core/createTranslator.d.ts +2 -3
  17. package/dist/core/createTranslator.esm.js +37 -0
  18. package/dist/core/createTranslator.esm.js.map +1 -0
  19. package/dist/core/createTranslatorImpl.d.ts +2 -3
  20. package/dist/core/createTranslatorImpl.esm.js +50 -0
  21. package/dist/core/createTranslatorImpl.esm.js.map +1 -0
  22. package/dist/core/defaults.esm.js +15 -0
  23. package/dist/core/defaults.esm.js.map +1 -0
  24. package/dist/core/index.d.ts +8 -1
  25. package/dist/core/initializeConfig.d.ts +13 -0
  26. package/dist/core/initializeConfig.esm.js +29 -0
  27. package/dist/core/initializeConfig.esm.js.map +1 -0
  28. package/dist/core/resolveNamespace.esm.js +10 -0
  29. package/dist/core/resolveNamespace.esm.js.map +1 -0
  30. package/dist/core/use-intl.esm10.js +23 -32
  31. package/dist/core/use-intl.esm10.js.map +1 -1
  32. package/dist/core/use-intl.esm11.js +10 -0
  33. package/dist/core/use-intl.esm11.js.map +1 -0
  34. package/dist/core/use-intl.esm2.js +204 -31
  35. package/dist/core/use-intl.esm2.js.map +1 -1
  36. package/dist/core/use-intl.esm3.js +31 -132
  37. package/dist/core/use-intl.esm3.js.map +1 -1
  38. package/dist/core/use-intl.esm4.js +1 -1
  39. package/dist/core/use-intl.esm5.js +19 -43
  40. package/dist/core/use-intl.esm5.js.map +1 -1
  41. package/dist/core/use-intl.esm6.js +134 -11
  42. package/dist/core/use-intl.esm6.js.map +1 -1
  43. package/dist/core/use-intl.esm7.js +29 -192
  44. package/dist/core/use-intl.esm7.js.map +1 -1
  45. package/dist/core/use-intl.esm8.js +10 -5
  46. package/dist/core/use-intl.esm8.js.map +1 -1
  47. package/dist/core/use-intl.esm9.js +44 -22
  48. package/dist/core/use-intl.esm9.js.map +1 -1
  49. package/dist/core/validateMessages.esm.js +28 -0
  50. package/dist/core/validateMessages.esm.js.map +1 -0
  51. package/dist/core.cjs.development.js +93 -0
  52. package/dist/core.cjs.development.js.map +1 -0
  53. package/dist/core.cjs.production.min.js +2 -0
  54. package/dist/core.cjs.production.min.js.map +1 -0
  55. package/dist/core.d.ts +1 -0
  56. package/dist/core.esm.js +7 -0
  57. package/dist/core.esm.js.map +1 -0
  58. package/dist/core.js +8 -0
  59. package/dist/createIntl-34d4281e.js +592 -0
  60. package/dist/createIntl-34d4281e.js.map +1 -0
  61. package/dist/createIntl-bc325740.js +2 -0
  62. package/dist/createIntl-bc325740.js.map +1 -0
  63. package/dist/react/IntlContext.esm.js +6 -0
  64. package/dist/react/IntlContext.esm.js.map +1 -0
  65. package/dist/react/IntlProvider.d.ts +1 -1
  66. package/dist/react/IntlProvider.esm.js +22 -0
  67. package/dist/react/IntlProvider.esm.js.map +1 -0
  68. package/dist/react/getInitializedConfig.d.ts +1 -13
  69. package/dist/react/use-intl.esm.js +22 -18
  70. package/dist/react/use-intl.esm.js.map +1 -1
  71. package/dist/react/use-intl.esm10.js +8 -24
  72. package/dist/react/use-intl.esm10.js.map +1 -1
  73. package/dist/react/use-intl.esm11.js +3 -10
  74. package/dist/react/use-intl.esm11.js.map +1 -1
  75. package/dist/react/use-intl.esm2.js +4 -22
  76. package/dist/react/use-intl.esm2.js.map +1 -1
  77. package/dist/react/use-intl.esm3.js +26 -4
  78. package/dist/react/use-intl.esm3.js.map +1 -1
  79. package/dist/react/use-intl.esm4.js +18 -41
  80. package/dist/react/use-intl.esm4.js.map +1 -1
  81. package/dist/react/use-intl.esm5.js +20 -4
  82. package/dist/react/use-intl.esm5.js.map +1 -1
  83. package/dist/react/use-intl.esm6.js +41 -4
  84. package/dist/react/use-intl.esm6.js.map +1 -1
  85. package/dist/react/use-intl.esm7.js +4 -20
  86. package/dist/react/use-intl.esm7.js.map +1 -1
  87. package/dist/react/use-intl.esm8.js +4 -26
  88. package/dist/react/use-intl.esm8.js.map +1 -1
  89. package/dist/react/use-intl.esm9.js +33 -3
  90. package/dist/react/use-intl.esm9.js.map +1 -1
  91. package/dist/react/useFormatter.esm.js +24 -0
  92. package/dist/react/useFormatter.esm.js.map +1 -0
  93. package/dist/react/useIntl.esm.js +30 -0
  94. package/dist/react/useIntl.esm.js.map +1 -0
  95. package/dist/react/useIntlContext.esm.js +13 -0
  96. package/dist/react/useIntlContext.esm.js.map +1 -0
  97. package/dist/react/useLocale.esm.js +8 -0
  98. package/dist/react/useLocale.esm.js.map +1 -0
  99. package/dist/react/useMessages.esm.js +8 -0
  100. package/dist/react/useMessages.esm.js.map +1 -0
  101. package/{src/react/useNow.tsx → dist/react/useNow.esm.js} +32 -36
  102. package/dist/react/useNow.esm.js.map +1 -0
  103. package/dist/react/useTimeZone.esm.js +8 -0
  104. package/dist/react/useTimeZone.esm.js.map +1 -0
  105. package/dist/react/useTranslations.esm.js +26 -0
  106. package/dist/react/useTranslations.esm.js.map +1 -0
  107. package/dist/react/{use-intl.esm12.js → useTranslationsImpl.esm.js} +6 -13
  108. package/dist/react/useTranslationsImpl.esm.js.map +1 -0
  109. package/dist/react.cjs.development.js +190 -0
  110. package/dist/react.cjs.development.js.map +1 -0
  111. package/dist/react.cjs.production.min.js +2 -0
  112. package/dist/react.cjs.production.min.js.map +1 -0
  113. package/dist/react.d.ts +1 -0
  114. package/dist/react.esm.js +9 -0
  115. package/dist/react.esm.js.map +1 -0
  116. package/dist/react.js +8 -0
  117. package/dist/test/setup.d.ts +1 -0
  118. package/dist/use-intl.cjs.development.js +32 -30
  119. package/dist/use-intl.cjs.development.js.map +1 -1
  120. package/dist/use-intl.cjs.production.min.js +1 -1
  121. package/dist/use-intl.cjs.production.min.js.map +1 -1
  122. package/dist/use-intl.esm.js +12 -10
  123. package/dist/use-intl.esm.js.map +1 -1
  124. package/package.json +17 -15
  125. package/react.d.ts +1 -0
  126. package/dist/react/use-intl.esm12.js.map +0 -1
  127. package/dist/src/core/AbstractIntlMessages.d.ts +0 -9
  128. package/dist/src/core/AbstractIntlMessages.js +0 -2
  129. package/dist/src/core/AbstractIntlMessages.js.map +0 -1
  130. package/dist/src/core/DateTimeFormatOptions.d.ts +0 -73
  131. package/dist/src/core/DateTimeFormatOptions.js +0 -3
  132. package/dist/src/core/DateTimeFormatOptions.js.map +0 -1
  133. package/dist/src/core/Formats.d.ts +0 -8
  134. package/dist/src/core/Formats.js +0 -2
  135. package/dist/src/core/Formats.js.map +0 -1
  136. package/dist/src/core/IntlConfig.d.ts +0 -54
  137. package/dist/src/core/IntlConfig.js +0 -2
  138. package/dist/src/core/IntlConfig.js.map +0 -1
  139. package/dist/src/core/IntlError.d.ts +0 -13
  140. package/dist/src/core/IntlError.js +0 -23
  141. package/dist/src/core/IntlError.js.map +0 -1
  142. package/dist/src/core/MessageFormatCache.d.ts +0 -5
  143. package/dist/src/core/MessageFormatCache.js +0 -2
  144. package/dist/src/core/MessageFormatCache.js.map +0 -1
  145. package/dist/src/core/NumberFormatOptions.d.ts +0 -2
  146. package/dist/src/core/NumberFormatOptions.js +0 -2
  147. package/dist/src/core/NumberFormatOptions.js.map +0 -1
  148. package/dist/src/core/TimeZone.d.ts +0 -2
  149. package/dist/src/core/TimeZone.js +0 -2
  150. package/dist/src/core/TimeZone.js.map +0 -1
  151. package/dist/src/core/TranslationValues.d.ts +0 -5
  152. package/dist/src/core/TranslationValues.js +0 -2
  153. package/dist/src/core/TranslationValues.js.map +0 -1
  154. package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +0 -11
  155. package/dist/src/core/convertFormatsToIntlMessageFormat.js +0 -47
  156. package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +0 -1
  157. package/dist/src/core/createBaseTranslator.d.ts +0 -26
  158. package/dist/src/core/createBaseTranslator.js +0 -218
  159. package/dist/src/core/createBaseTranslator.js.map +0 -1
  160. package/dist/src/core/createFormatter.d.ts +0 -19
  161. package/dist/src/core/createFormatter.js +0 -129
  162. package/dist/src/core/createFormatter.js.map +0 -1
  163. package/dist/src/core/createIntl.d.ts +0 -7
  164. package/dist/src/core/createIntl.js +0 -11
  165. package/dist/src/core/createIntl.js.map +0 -1
  166. package/dist/src/core/createTranslator.d.ts +0 -48
  167. package/dist/src/core/createTranslator.js +0 -24
  168. package/dist/src/core/createTranslator.js.map +0 -1
  169. package/dist/src/core/createTranslatorImpl.d.ts +0 -14
  170. package/dist/src/core/createTranslatorImpl.js +0 -43
  171. package/dist/src/core/createTranslatorImpl.js.map +0 -1
  172. package/dist/src/core/defaults.d.ts +0 -11
  173. package/dist/src/core/defaults.js +0 -11
  174. package/dist/src/core/defaults.js.map +0 -1
  175. package/dist/src/core/index.d.ts +0 -9
  176. package/dist/src/core/index.js +0 -6
  177. package/dist/src/core/index.js.map +0 -1
  178. package/dist/src/core/resolveNamespace.d.ts +0 -5
  179. package/dist/src/core/resolveNamespace.js +0 -10
  180. package/dist/src/core/resolveNamespace.js.map +0 -1
  181. package/dist/src/core/utils/MessageKeys.d.ts +0 -5
  182. package/dist/src/core/utils/MessageKeys.js +0 -2
  183. package/dist/src/core/utils/MessageKeys.js.map +0 -1
  184. package/dist/src/core/utils/NamespaceKeys.d.ts +0 -5
  185. package/dist/src/core/utils/NamespaceKeys.js +0 -2
  186. package/dist/src/core/utils/NamespaceKeys.js.map +0 -1
  187. package/dist/src/core/utils/NestedKeyOf.d.ts +0 -4
  188. package/dist/src/core/utils/NestedKeyOf.js +0 -2
  189. package/dist/src/core/utils/NestedKeyOf.js.map +0 -1
  190. package/dist/src/core/utils/NestedValueOf.d.ts +0 -2
  191. package/dist/src/core/utils/NestedValueOf.js +0 -2
  192. package/dist/src/core/utils/NestedValueOf.js.map +0 -1
  193. package/dist/src/core/validateMessages.d.ts +0 -3
  194. package/dist/src/core/validateMessages.js +0 -56
  195. package/dist/src/core/validateMessages.js.map +0 -1
  196. package/dist/src/index.d.ts +0 -2
  197. package/dist/src/index.js +0 -3
  198. package/dist/src/index.js.map +0 -1
  199. package/dist/src/react/IntlContext.d.ts +0 -13
  200. package/dist/src/react/IntlContext.js +0 -4
  201. package/dist/src/react/IntlContext.js.map +0 -1
  202. package/dist/src/react/IntlProvider.d.ts +0 -7
  203. package/dist/src/react/IntlProvider.js +0 -11
  204. package/dist/src/react/IntlProvider.js.map +0 -1
  205. package/dist/src/react/getInitializedConfig.d.ts +0 -13
  206. package/dist/src/react/getInitializedConfig.js +0 -21
  207. package/dist/src/react/getInitializedConfig.js.map +0 -1
  208. package/dist/src/react/index.d.ts +0 -8
  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.d.ts +0 -6
  212. package/dist/src/react/useFormatter.js +0 -14
  213. package/dist/src/react/useFormatter.js.map +0 -1
  214. package/dist/src/react/useIntl.d.ts +0 -6
  215. package/dist/src/react/useIntl.js +0 -20
  216. package/dist/src/react/useIntl.js.map +0 -1
  217. package/dist/src/react/useIntlContext.d.ts +0 -10
  218. package/dist/src/react/useIntlContext.js +0 -12
  219. package/dist/src/react/useIntlContext.js.map +0 -1
  220. package/dist/src/react/useLocale.d.ts +0 -1
  221. package/dist/src/react/useLocale.js +0 -5
  222. package/dist/src/react/useLocale.js.map +0 -1
  223. package/dist/src/react/useMessages.d.ts +0 -1
  224. package/dist/src/react/useMessages.js +0 -5
  225. package/dist/src/react/useMessages.js.map +0 -1
  226. package/dist/src/react/useNow.d.ts +0 -23
  227. package/dist/src/react/useNow.js +0 -40
  228. package/dist/src/react/useNow.js.map +0 -1
  229. package/dist/src/react/useTimeZone.d.ts +0 -1
  230. package/dist/src/react/useTimeZone.js +0 -5
  231. package/dist/src/react/useTimeZone.js.map +0 -1
  232. package/dist/src/react/useTranslations.d.ts +0 -44
  233. package/dist/src/react/useTranslations.js +0 -21
  234. package/dist/src/react/useTranslations.js.map +0 -1
  235. package/dist/src/react/useTranslationsImpl.d.ts +0 -8
  236. package/dist/src/react/useTranslationsImpl.js +0 -35
  237. package/dist/src/react/useTranslationsImpl.js.map +0 -1
  238. package/src/core/AbstractIntlMessages.tsx +0 -10
  239. package/src/core/DateTimeFormatOptions.tsx +0 -130
  240. package/src/core/Formats.tsx +0 -10
  241. package/src/core/IntlConfig.tsx +0 -59
  242. package/src/core/IntlError.tsx +0 -26
  243. package/src/core/MessageFormatCache.tsx +0 -10
  244. package/src/core/NumberFormatOptions.tsx +0 -3
  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 -191
  250. package/src/core/createIntl.tsx +0 -13
  251. package/src/core/createTranslator.tsx +0 -112
  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/useTimeZone.tsx +0 -5
  272. package/src/react/useTranslations.tsx +0 -103
  273. package/src/react/useTranslationsImpl.tsx +0 -61
@@ -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,191 +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 TimeZone from './TimeZone';
6
- import {defaultOnError} from './defaults';
7
-
8
- const MINUTE = 60;
9
- const HOUR = MINUTE * 60;
10
- const DAY = HOUR * 24;
11
- const WEEK = DAY * 7;
12
- const MONTH = DAY * (365 / 12); // Approximation
13
- const YEAR = DAY * 365;
14
-
15
- function getRelativeTimeFormatConfig(seconds: number) {
16
- const absValue = Math.abs(seconds);
17
- let value, unit: Intl.RelativeTimeFormatUnit;
18
-
19
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
20
- // will include fractions like '2.1 hours ago'.
21
-
22
- if (absValue < MINUTE) {
23
- unit = 'second';
24
- value = Math.round(seconds);
25
- } else if (absValue < HOUR) {
26
- unit = 'minute';
27
- value = Math.round(seconds / MINUTE);
28
- } else if (absValue < DAY) {
29
- unit = 'hour';
30
- value = Math.round(seconds / HOUR);
31
- } else if (absValue < WEEK) {
32
- unit = 'day';
33
- value = Math.round(seconds / DAY);
34
- } else if (absValue < MONTH) {
35
- unit = 'week';
36
- value = Math.round(seconds / WEEK);
37
- } else if (absValue < YEAR) {
38
- unit = 'month';
39
- value = Math.round(seconds / MONTH);
40
- } else {
41
- unit = 'year';
42
- value = Math.round(seconds / YEAR);
43
- }
44
-
45
- return {value, unit};
46
- }
47
-
48
- type Props = {
49
- locale: string;
50
- timeZone?: TimeZone;
51
- onError?(error: IntlError): void;
52
- formats?: Partial<Formats>;
53
- now?: Date;
54
- };
55
-
56
- export default function createFormatter({
57
- formats,
58
- locale,
59
- now: globalNow,
60
- onError = defaultOnError,
61
- timeZone
62
- }: Props) {
63
- function resolveFormatOrOptions<Options>(
64
- typeFormats: Record<string, Options> | undefined,
65
- formatOrOptions?: string | Options
66
- ) {
67
- let options;
68
- if (typeof formatOrOptions === 'string') {
69
- const formatName = formatOrOptions;
70
- options = typeFormats?.[formatName];
71
-
72
- if (!options) {
73
- const error = new IntlError(
74
- IntlErrorCode.MISSING_FORMAT,
75
- process.env.NODE_ENV !== 'production'
76
- ? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
77
- : undefined
78
- );
79
- onError(error);
80
- throw error;
81
- }
82
- } else {
83
- options = formatOrOptions;
84
- }
85
-
86
- return options;
87
- }
88
-
89
- function getFormattedValue<Value, Options>(
90
- value: Value,
91
- formatOrOptions: string | Options | undefined,
92
- typeFormats: Record<string, Options> | undefined,
93
- formatter: (options?: Options) => string
94
- ) {
95
- let options;
96
- try {
97
- options = resolveFormatOrOptions(typeFormats, formatOrOptions);
98
- } catch (error) {
99
- return String(value);
100
- }
101
-
102
- try {
103
- return formatter(options);
104
- } catch (error) {
105
- onError(
106
- new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)
107
- );
108
- return String(value);
109
- }
110
- }
111
-
112
- function dateTime(
113
- /** If a number is supplied, this is interpreted as a UTC timestamp. */
114
- value: Date | number,
115
- /** If a time zone is supplied, the `value` is converted to that time zone.
116
- * Otherwise the user time zone will be used. */
117
- formatOrOptions?: string | DateTimeFormatOptions
118
- ) {
119
- return getFormattedValue(
120
- value,
121
- formatOrOptions,
122
- formats?.dateTime,
123
- (options) => {
124
- if (timeZone && !options?.timeZone) {
125
- options = {...options, timeZone};
126
- }
127
-
128
- return new Intl.DateTimeFormat(locale, options).format(value);
129
- }
130
- );
131
- }
132
-
133
- function number(
134
- value: number | bigint,
135
- formatOrOptions?: string | NumberFormatOptions
136
- ) {
137
- return getFormattedValue(
138
- value,
139
- formatOrOptions,
140
- formats?.number,
141
- (options) => new Intl.NumberFormat(locale, options).format(value)
142
- );
143
- }
144
-
145
- function relativeTime(
146
- /** The date time that needs to be formatted. */
147
- date: number | Date,
148
- /** The reference point in time to which `date` will be formatted in relation to. */
149
- now?: number | Date
150
- ) {
151
- try {
152
- if (!now) {
153
- if (globalNow) {
154
- now = globalNow;
155
- } else {
156
- throw new Error(
157
- process.env.NODE_ENV !== 'production'
158
- ? `The \`now\` parameter wasn't provided and there was no global fallback configured on the provider.`
159
- : undefined
160
- );
161
- }
162
- }
163
-
164
- const dateDate = date instanceof Date ? date : new Date(date);
165
- const nowDate = now instanceof Date ? now : new Date(now);
166
-
167
- const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
168
- const {unit, value} = getRelativeTimeFormatConfig(seconds);
169
-
170
- return new Intl.RelativeTimeFormat(locale, {
171
- numeric: 'auto'
172
- }).format(value, unit);
173
- } catch (error) {
174
- onError(
175
- new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)
176
- );
177
- return String(date);
178
- }
179
- }
180
-
181
- function list(
182
- value: Iterable<string>,
183
- formatOrOptions?: string | Intl.ListFormatOptions
184
- ) {
185
- return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>
186
- new Intl.ListFormat(locale, options).format(value)
187
- );
188
- }
189
-
190
- return {dateTime, number, relativeTime, list};
191
- }
@@ -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,112 +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
- // @ts-ignore
108
- namespace: namespace ? `!.${namespace}` : '!'
109
- },
110
- '!'
111
- );
112
- }