use-intl 2.19.1 → 3.0.0-beta.1

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 (270) 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 +32 -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 +201 -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.esm.js +34 -30
  31. package/dist/core/use-intl.esm.js.map +1 -1
  32. package/dist/core/use-intl.esm10.js +23 -27
  33. package/dist/core/use-intl.esm10.js.map +1 -1
  34. package/dist/core/use-intl.esm11.js +10 -0
  35. package/dist/core/use-intl.esm11.js.map +1 -0
  36. package/dist/core/use-intl.esm2.js +30 -34
  37. package/dist/core/use-intl.esm2.js.map +1 -1
  38. package/dist/core/use-intl.esm3.js +8 -132
  39. package/dist/core/use-intl.esm3.js.map +1 -1
  40. package/dist/core/use-intl.esm4.js +196 -9
  41. package/dist/core/use-intl.esm4.js.map +1 -1
  42. package/dist/core/use-intl.esm5.js +19 -43
  43. package/dist/core/use-intl.esm5.js.map +1 -1
  44. package/dist/core/use-intl.esm6.js +134 -11
  45. package/dist/core/use-intl.esm6.js.map +1 -1
  46. package/dist/core/use-intl.esm7.js +10 -5
  47. package/dist/core/use-intl.esm7.js.map +1 -1
  48. package/dist/core/use-intl.esm8.js +37 -178
  49. package/dist/core/use-intl.esm8.js.map +1 -1
  50. package/dist/core/use-intl.esm9.js +27 -23
  51. package/dist/core/use-intl.esm9.js.map +1 -1
  52. package/dist/core/validateMessages.esm.js +28 -0
  53. package/dist/core/validateMessages.esm.js.map +1 -0
  54. package/dist/core.cjs.development.js +93 -0
  55. package/dist/core.cjs.development.js.map +1 -0
  56. package/dist/core.cjs.production.min.js +2 -0
  57. package/dist/core.cjs.production.min.js.map +1 -0
  58. package/dist/core.d.ts +1 -0
  59. package/dist/core.esm.js +7 -0
  60. package/dist/core.esm.js.map +1 -0
  61. package/dist/core.js +8 -0
  62. package/dist/createIntl-8a2b87eb.js +579 -0
  63. package/dist/createIntl-8a2b87eb.js.map +1 -0
  64. package/dist/createIntl-f7979de1.js +2 -0
  65. package/dist/createIntl-f7979de1.js.map +1 -0
  66. package/dist/react/IntlContext.esm.js +6 -0
  67. package/dist/react/IntlContext.esm.js.map +1 -0
  68. package/dist/react/IntlProvider.d.ts +1 -1
  69. package/dist/react/IntlProvider.esm.js +16 -0
  70. package/dist/react/IntlProvider.esm.js.map +1 -0
  71. package/dist/react/getInitializedConfig.d.ts +1 -13
  72. package/dist/react/use-intl.esm.js +22 -12
  73. package/dist/react/use-intl.esm.js.map +1 -1
  74. package/dist/react/use-intl.esm10.js +8 -24
  75. package/dist/react/use-intl.esm10.js.map +1 -1
  76. package/dist/react/use-intl.esm11.js +3 -10
  77. package/dist/react/use-intl.esm11.js.map +1 -1
  78. package/dist/react/use-intl.esm2.js +20 -22
  79. package/dist/react/use-intl.esm2.js.map +1 -1
  80. package/dist/react/use-intl.esm3.js +41 -4
  81. package/dist/react/use-intl.esm3.js.map +1 -1
  82. package/dist/react/use-intl.esm4.js +12 -41
  83. package/dist/react/use-intl.esm4.js.map +1 -1
  84. package/dist/react/use-intl.esm5.js +1 -1
  85. package/dist/react/use-intl.esm6.js +1 -1
  86. package/dist/react/use-intl.esm7.js +11 -5
  87. package/dist/react/use-intl.esm7.js.map +1 -1
  88. package/dist/react/use-intl.esm8.js +4 -26
  89. package/dist/react/use-intl.esm8.js.map +1 -1
  90. package/dist/react/use-intl.esm9.js +33 -3
  91. package/dist/react/use-intl.esm9.js.map +1 -1
  92. package/dist/react/useFormatter.esm.js +24 -0
  93. package/dist/react/useFormatter.esm.js.map +1 -0
  94. package/dist/react/useIntl.esm.js +30 -0
  95. package/dist/react/useIntl.esm.js.map +1 -0
  96. package/dist/react/useIntlContext.esm.js +13 -0
  97. package/dist/react/useIntlContext.esm.js.map +1 -0
  98. package/dist/react/useLocale.esm.js +8 -0
  99. package/dist/react/useLocale.esm.js.map +1 -0
  100. package/dist/react/useMessages.esm.js +8 -0
  101. package/dist/react/useMessages.esm.js.map +1 -0
  102. package/{src/react/useNow.tsx → dist/react/useNow.esm.js} +32 -36
  103. package/dist/react/useNow.esm.js.map +1 -0
  104. package/dist/react/useTimeZone.esm.js +8 -0
  105. package/dist/react/useTimeZone.esm.js.map +1 -0
  106. package/dist/react/useTranslations.esm.js +26 -0
  107. package/dist/react/useTranslations.esm.js.map +1 -0
  108. package/dist/react/{use-intl.esm12.js → useTranslationsImpl.esm.js} +6 -13
  109. package/dist/react/useTranslationsImpl.esm.js.map +1 -0
  110. package/dist/react.cjs.development.js +184 -0
  111. package/dist/react.cjs.development.js.map +1 -0
  112. package/dist/react.cjs.production.min.js +2 -0
  113. package/dist/react.cjs.production.min.js.map +1 -0
  114. package/dist/react.d.ts +1 -0
  115. package/dist/react.esm.js +9 -0
  116. package/dist/react.esm.js.map +1 -0
  117. package/dist/react.js +8 -0
  118. package/dist/test/setup.d.ts +1 -0
  119. package/dist/use-intl.cjs.development.js +32 -30
  120. package/dist/use-intl.cjs.development.js.map +1 -1
  121. package/dist/use-intl.cjs.production.min.js +1 -1
  122. package/dist/use-intl.cjs.production.min.js.map +1 -1
  123. package/dist/use-intl.esm.js +12 -10
  124. package/dist/use-intl.esm.js.map +1 -1
  125. package/package.json +17 -15
  126. package/react.d.ts +1 -0
  127. package/dist/react/use-intl.esm12.js.map +0 -1
  128. package/dist/src/core/AbstractIntlMessages.d.ts +0 -9
  129. package/dist/src/core/AbstractIntlMessages.js +0 -2
  130. package/dist/src/core/AbstractIntlMessages.js.map +0 -1
  131. package/dist/src/core/DateTimeFormatOptions.d.ts +0 -73
  132. package/dist/src/core/DateTimeFormatOptions.js +0 -3
  133. package/dist/src/core/DateTimeFormatOptions.js.map +0 -1
  134. package/dist/src/core/Formats.d.ts +0 -8
  135. package/dist/src/core/Formats.js +0 -2
  136. package/dist/src/core/Formats.js.map +0 -1
  137. package/dist/src/core/IntlConfig.d.ts +0 -54
  138. package/dist/src/core/IntlConfig.js +0 -2
  139. package/dist/src/core/IntlConfig.js.map +0 -1
  140. package/dist/src/core/IntlError.d.ts +0 -13
  141. package/dist/src/core/IntlError.js +0 -23
  142. package/dist/src/core/IntlError.js.map +0 -1
  143. package/dist/src/core/NumberFormatOptions.d.ts +0 -2
  144. package/dist/src/core/NumberFormatOptions.js +0 -2
  145. package/dist/src/core/NumberFormatOptions.js.map +0 -1
  146. package/dist/src/core/TimeZone.d.ts +0 -2
  147. package/dist/src/core/TimeZone.js +0 -2
  148. package/dist/src/core/TimeZone.js.map +0 -1
  149. package/dist/src/core/TranslationValues.d.ts +0 -5
  150. package/dist/src/core/TranslationValues.js +0 -2
  151. package/dist/src/core/TranslationValues.js.map +0 -1
  152. package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +0 -11
  153. package/dist/src/core/convertFormatsToIntlMessageFormat.js +0 -31
  154. package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +0 -1
  155. package/dist/src/core/createBaseTranslator.d.ts +0 -26
  156. package/dist/src/core/createBaseTranslator.js +0 -206
  157. package/dist/src/core/createBaseTranslator.js.map +0 -1
  158. package/dist/src/core/createFormatter.d.ts +0 -19
  159. package/dist/src/core/createFormatter.js +0 -129
  160. package/dist/src/core/createFormatter.js.map +0 -1
  161. package/dist/src/core/createIntl.d.ts +0 -7
  162. package/dist/src/core/createIntl.js +0 -11
  163. package/dist/src/core/createIntl.js.map +0 -1
  164. package/dist/src/core/createTranslator.d.ts +0 -48
  165. package/dist/src/core/createTranslator.js +0 -24
  166. package/dist/src/core/createTranslator.js.map +0 -1
  167. package/dist/src/core/createTranslatorImpl.d.ts +0 -14
  168. package/dist/src/core/createTranslatorImpl.js +0 -43
  169. package/dist/src/core/createTranslatorImpl.js.map +0 -1
  170. package/dist/src/core/defaults.d.ts +0 -11
  171. package/dist/src/core/defaults.js +0 -11
  172. package/dist/src/core/defaults.js.map +0 -1
  173. package/dist/src/core/index.d.ts +0 -9
  174. package/dist/src/core/index.js +0 -6
  175. package/dist/src/core/index.js.map +0 -1
  176. package/dist/src/core/resolveNamespace.d.ts +0 -5
  177. package/dist/src/core/resolveNamespace.js +0 -10
  178. package/dist/src/core/resolveNamespace.js.map +0 -1
  179. package/dist/src/core/utils/MessageKeys.d.ts +0 -5
  180. package/dist/src/core/utils/MessageKeys.js +0 -2
  181. package/dist/src/core/utils/MessageKeys.js.map +0 -1
  182. package/dist/src/core/utils/NamespaceKeys.d.ts +0 -5
  183. package/dist/src/core/utils/NamespaceKeys.js +0 -2
  184. package/dist/src/core/utils/NamespaceKeys.js.map +0 -1
  185. package/dist/src/core/utils/NestedKeyOf.d.ts +0 -4
  186. package/dist/src/core/utils/NestedKeyOf.js +0 -2
  187. package/dist/src/core/utils/NestedKeyOf.js.map +0 -1
  188. package/dist/src/core/utils/NestedValueOf.d.ts +0 -2
  189. package/dist/src/core/utils/NestedValueOf.js +0 -2
  190. package/dist/src/core/utils/NestedValueOf.js.map +0 -1
  191. package/dist/src/core/validateMessages.d.ts +0 -3
  192. package/dist/src/core/validateMessages.js +0 -56
  193. package/dist/src/core/validateMessages.js.map +0 -1
  194. package/dist/src/index.d.ts +0 -2
  195. package/dist/src/index.js +0 -3
  196. package/dist/src/index.js.map +0 -1
  197. package/dist/src/react/IntlContext.d.ts +0 -4
  198. package/dist/src/react/IntlContext.js +0 -4
  199. package/dist/src/react/IntlContext.js.map +0 -1
  200. package/dist/src/react/IntlProvider.d.ts +0 -7
  201. package/dist/src/react/IntlProvider.js +0 -7
  202. package/dist/src/react/IntlProvider.js.map +0 -1
  203. package/dist/src/react/getInitializedConfig.d.ts +0 -13
  204. package/dist/src/react/getInitializedConfig.js +0 -21
  205. package/dist/src/react/getInitializedConfig.js.map +0 -1
  206. package/dist/src/react/index.d.ts +0 -8
  207. package/dist/src/react/index.js +0 -10
  208. package/dist/src/react/index.js.map +0 -1
  209. package/dist/src/react/useFormatter.d.ts +0 -6
  210. package/dist/src/react/useFormatter.js +0 -14
  211. package/dist/src/react/useFormatter.js.map +0 -1
  212. package/dist/src/react/useIntl.d.ts +0 -6
  213. package/dist/src/react/useIntl.js +0 -20
  214. package/dist/src/react/useIntl.js.map +0 -1
  215. package/dist/src/react/useIntlContext.d.ts +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.d.ts +0 -1
  219. package/dist/src/react/useLocale.js +0 -5
  220. package/dist/src/react/useLocale.js.map +0 -1
  221. package/dist/src/react/useMessages.d.ts +0 -1
  222. package/dist/src/react/useMessages.js +0 -5
  223. package/dist/src/react/useMessages.js.map +0 -1
  224. package/dist/src/react/useNow.d.ts +0 -23
  225. package/dist/src/react/useNow.js +0 -40
  226. package/dist/src/react/useNow.js.map +0 -1
  227. package/dist/src/react/useTimeZone.d.ts +0 -1
  228. package/dist/src/react/useTimeZone.js +0 -5
  229. package/dist/src/react/useTimeZone.js.map +0 -1
  230. package/dist/src/react/useTranslations.d.ts +0 -44
  231. package/dist/src/react/useTranslations.js +0 -21
  232. package/dist/src/react/useTranslations.js.map +0 -1
  233. package/dist/src/react/useTranslationsImpl.d.ts +0 -8
  234. package/dist/src/react/useTranslationsImpl.js +0 -35
  235. package/dist/src/react/useTranslationsImpl.js.map +0 -1
  236. package/src/core/AbstractIntlMessages.tsx +0 -10
  237. package/src/core/DateTimeFormatOptions.tsx +0 -130
  238. package/src/core/Formats.tsx +0 -10
  239. package/src/core/IntlConfig.tsx +0 -59
  240. package/src/core/IntlError.tsx +0 -26
  241. package/src/core/NumberFormatOptions.tsx +0 -3
  242. package/src/core/TimeZone.tsx +0 -426
  243. package/src/core/TranslationValues.tsx +0 -22
  244. package/src/core/convertFormatsToIntlMessageFormat.tsx +0 -46
  245. package/src/core/createBaseTranslator.tsx +0 -335
  246. package/src/core/createFormatter.tsx +0 -191
  247. package/src/core/createIntl.tsx +0 -13
  248. package/src/core/createTranslator.tsx +0 -112
  249. package/src/core/createTranslatorImpl.tsx +0 -87
  250. package/src/core/defaults.tsx +0 -18
  251. package/src/core/index.tsx +0 -14
  252. package/src/core/resolveNamespace.tsx +0 -12
  253. package/src/core/utils/MessageKeys.tsx +0 -9
  254. package/src/core/utils/NamespaceKeys.tsx +0 -9
  255. package/src/core/utils/NestedKeyOf.tsx +0 -9
  256. package/src/core/utils/NestedValueOf.tsx +0 -12
  257. package/src/core/validateMessages.tsx +0 -77
  258. package/src/index.tsx +0 -2
  259. package/src/react/IntlContext.tsx +0 -6
  260. package/src/react/IntlProvider.tsx +0 -16
  261. package/src/react/getInitializedConfig.tsx +0 -29
  262. package/src/react/index.tsx +0 -10
  263. package/src/react/useFormatter.tsx +0 -19
  264. package/src/react/useIntl.tsx +0 -29
  265. package/src/react/useIntlContext.tsx +0 -16
  266. package/src/react/useLocale.tsx +0 -5
  267. package/src/react/useMessages.tsx +0 -5
  268. package/src/react/useTimeZone.tsx +0 -5
  269. package/src/react/useTranslations.tsx +0 -103
  270. package/src/react/useTranslationsImpl.tsx +0 -65
@@ -1,335 +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 TranslationValues, {RichTranslationValues} from './TranslationValues';
15
- import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
16
- import {defaultGetMessageFallback, defaultOnError} from './defaults';
17
- import MessageKeys from './utils/MessageKeys';
18
- import NestedKeyOf from './utils/NestedKeyOf';
19
- import NestedValueOf from './utils/NestedValueOf';
20
-
21
- function resolvePath(
22
- messages: AbstractIntlMessages | undefined,
23
- key: string,
24
- namespace?: string
25
- ) {
26
- if (!messages) {
27
- throw new Error(
28
- process.env.NODE_ENV !== 'production'
29
- ? `No messages available at \`${namespace}\`.`
30
- : undefined
31
- );
32
- }
33
-
34
- let message = messages;
35
-
36
- key.split('.').forEach((part) => {
37
- const next = (message as any)[part];
38
-
39
- if (part == null || next == null) {
40
- throw new Error(
41
- process.env.NODE_ENV !== 'production'
42
- ? `Could not resolve \`${key}\` in ${
43
- namespace ? `\`${namespace}\`` : 'messages'
44
- }.`
45
- : undefined
46
- );
47
- }
48
-
49
- message = next;
50
- });
51
-
52
- return message;
53
- }
54
-
55
- function prepareTranslationValues(values: RichTranslationValues) {
56
- if (Object.keys(values).length === 0) return undefined;
57
-
58
- // Workaround for https://github.com/formatjs/formatjs/issues/1467
59
- const transformedValues: RichTranslationValues = {};
60
- Object.keys(values).forEach((key) => {
61
- let index = 0;
62
- const value = values[key];
63
-
64
- let transformed;
65
- if (typeof value === 'function') {
66
- transformed = (chunks: ReactNode) => {
67
- const result = value(chunks);
68
-
69
- return isValidElement(result)
70
- ? cloneElement(result, {key: key + index++})
71
- : result;
72
- };
73
- } else {
74
- transformed = value;
75
- }
76
-
77
- transformedValues[key] = transformed;
78
- });
79
-
80
- return transformedValues;
81
- }
82
-
83
- export function getMessagesOrError<Messages extends AbstractIntlMessages>({
84
- messages,
85
- namespace,
86
- onError = defaultOnError
87
- }: {
88
- messages: Messages;
89
- namespace?: string;
90
- onError?(error: IntlError): void;
91
- }) {
92
- try {
93
- if (!messages) {
94
- throw new Error(
95
- process.env.NODE_ENV !== 'production'
96
- ? `No messages were configured on the provider.`
97
- : undefined
98
- );
99
- }
100
-
101
- const retrievedMessages = namespace
102
- ? resolvePath(messages, namespace)
103
- : messages;
104
-
105
- if (!retrievedMessages) {
106
- throw new Error(
107
- process.env.NODE_ENV !== 'production'
108
- ? `No messages for namespace \`${namespace}\` found.`
109
- : undefined
110
- );
111
- }
112
-
113
- return retrievedMessages;
114
- } catch (error) {
115
- const intlError = new IntlError(
116
- IntlErrorCode.MISSING_MESSAGE,
117
- (error as Error).message
118
- );
119
- onError(intlError);
120
- return intlError;
121
- }
122
- }
123
-
124
- export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
125
- cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;
126
- defaultTranslationValues?: RichTranslationValues;
127
- namespace?: string;
128
- messagesOrError: Messages | IntlError;
129
- };
130
-
131
- export default function createBaseTranslator<
132
- Messages extends AbstractIntlMessages,
133
- NestedKey extends NestedKeyOf<Messages>
134
- >({
135
- cachedFormatsByLocale,
136
- defaultTranslationValues,
137
- formats: globalFormats,
138
- getMessageFallback = defaultGetMessageFallback,
139
- locale,
140
- messagesOrError,
141
- namespace,
142
- onError,
143
- timeZone
144
- }: CreateBaseTranslatorProps<Messages>) {
145
- function getFallbackFromErrorAndNotify(
146
- key: string,
147
- code: IntlErrorCode,
148
- message?: string
149
- ) {
150
- const error = new IntlError(code, message);
151
- onError(error);
152
- return getMessageFallback({error, key, namespace});
153
- }
154
-
155
- function translateBaseFn(
156
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
157
- key: string,
158
- /** Key value pairs for values to interpolate into the message. */
159
- values?: RichTranslationValues,
160
- /** Provide custom formats for numbers, dates and times. */
161
- formats?: Partial<Formats>
162
- ): string | ReactElement | ReactNodeArray {
163
- if (messagesOrError instanceof IntlError) {
164
- // We have already warned about this during render
165
- return getMessageFallback({
166
- error: messagesOrError,
167
- key,
168
- namespace
169
- });
170
- }
171
- const messages = messagesOrError;
172
-
173
- let message;
174
- try {
175
- message = resolvePath(messages, key, namespace);
176
- } catch (error) {
177
- return getFallbackFromErrorAndNotify(
178
- key,
179
- IntlErrorCode.MISSING_MESSAGE,
180
- (error as Error).message
181
- );
182
- }
183
-
184
- function joinPath(parts: Array<string | undefined>) {
185
- return parts.filter((part) => part != null).join('.');
186
- }
187
-
188
- const cacheKey = joinPath([namespace, key, String(message)]);
189
-
190
- let messageFormat;
191
- if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {
192
- messageFormat = cachedFormatsByLocale?.[locale][cacheKey];
193
- } else {
194
- if (typeof message === 'object') {
195
- let code, errorMessage;
196
- if (Array.isArray(message)) {
197
- code = IntlErrorCode.INVALID_MESSAGE;
198
- if (process.env.NODE_ENV !== 'production') {
199
- errorMessage = `Message at \`${joinPath([
200
- namespace,
201
- key
202
- ])}\` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages`;
203
- }
204
- } else {
205
- code = IntlErrorCode.INSUFFICIENT_PATH;
206
- if (process.env.NODE_ENV !== 'production') {
207
- errorMessage = `Message at \`${joinPath([
208
- namespace,
209
- key
210
- ])}\` 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`;
211
- }
212
- }
213
-
214
- return getFallbackFromErrorAndNotify(key, code, errorMessage);
215
- }
216
-
217
- try {
218
- messageFormat = new IntlMessageFormat(
219
- message,
220
- locale,
221
- convertFormatsToIntlMessageFormat(
222
- {...globalFormats, ...formats},
223
- timeZone
224
- )
225
- );
226
- } catch (error) {
227
- return getFallbackFromErrorAndNotify(
228
- key,
229
- IntlErrorCode.INVALID_MESSAGE,
230
- (error as Error).message
231
- );
232
- }
233
-
234
- if (cachedFormatsByLocale) {
235
- if (!cachedFormatsByLocale[locale]) {
236
- cachedFormatsByLocale[locale] = {};
237
- }
238
- cachedFormatsByLocale[locale][cacheKey] = messageFormat;
239
- }
240
- }
241
-
242
- try {
243
- const formattedMessage = messageFormat.format(
244
- // @ts-ignore `intl-messageformat` expects a different format
245
- // for rich text elements since a recent minor update. This
246
- // needs to be evaluated in detail, possibly also in regards
247
- // to be able to format to parts.
248
- prepareTranslationValues({...defaultTranslationValues, ...values})
249
- );
250
-
251
- if (formattedMessage == null) {
252
- throw new Error(
253
- process.env.NODE_ENV !== 'production'
254
- ? `Unable to format \`${key}\` in ${
255
- namespace ? `namespace \`${namespace}\`` : 'messages'
256
- }`
257
- : undefined
258
- );
259
- }
260
-
261
- // Limit the function signature to return strings or React elements
262
- return isValidElement(formattedMessage) ||
263
- // Arrays of React elements
264
- Array.isArray(formattedMessage) ||
265
- typeof formattedMessage === 'string'
266
- ? formattedMessage
267
- : String(formattedMessage);
268
- } catch (error) {
269
- return getFallbackFromErrorAndNotify(
270
- key,
271
- IntlErrorCode.FORMATTING_ERROR,
272
- (error as Error).message
273
- );
274
- }
275
- }
276
-
277
- function translateFn<
278
- TargetKey extends MessageKeys<
279
- NestedValueOf<Messages, NestedKey>,
280
- NestedKeyOf<NestedValueOf<Messages, NestedKey>>
281
- >
282
- >(
283
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
284
- key: TargetKey,
285
- /** Key value pairs for values to interpolate into the message. */
286
- values?: TranslationValues,
287
- /** Provide custom formats for numbers, dates and times. */
288
- formats?: Partial<Formats>
289
- ): string {
290
- const result = translateBaseFn(key, values, formats);
291
-
292
- if (typeof result !== 'string') {
293
- return getFallbackFromErrorAndNotify(
294
- key,
295
- IntlErrorCode.INVALID_MESSAGE,
296
- process.env.NODE_ENV !== 'production'
297
- ? `The message \`${key}\` in ${
298
- namespace ? `namespace \`${namespace}\`` : 'messages'
299
- } didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
300
- : undefined
301
- );
302
- }
303
-
304
- return result;
305
- }
306
-
307
- translateFn.rich = translateBaseFn;
308
-
309
- translateFn.raw = (
310
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
311
- key: string
312
- ): any => {
313
- if (messagesOrError instanceof IntlError) {
314
- // We have already warned about this during render
315
- return getMessageFallback({
316
- error: messagesOrError,
317
- key,
318
- namespace
319
- });
320
- }
321
- const messages = messagesOrError;
322
-
323
- try {
324
- return resolvePath(messages, key, namespace);
325
- } catch (error) {
326
- return getFallbackFromErrorAndNotify(
327
- key,
328
- IntlErrorCode.MISSING_MESSAGE,
329
- (error as Error).message
330
- );
331
- }
332
- };
333
-
334
- return translateFn;
335
- }
@@ -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
- }