use-intl 2.13.1 → 2.13.4

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 (91) hide show
  1. package/README.md +1 -1
  2. package/dist/_virtual/use-intl.esm.js +4 -28
  3. package/dist/_virtual/use-intl.esm.js.map +1 -1
  4. package/dist/core/AbstractIntlMessages.d.ts +1 -1
  5. package/dist/core/DateTimeFormatOptions.d.ts +1 -1
  6. package/dist/core/Formats.d.ts +1 -1
  7. package/dist/core/IntlConfig.d.ts +2 -2
  8. package/dist/core/TranslationValues.d.ts +3 -3
  9. package/dist/core/createBaseTranslator.d.ts +3 -3
  10. package/dist/core/createFormatter.d.ts +4 -4
  11. package/dist/core/createIntl.d.ts +1 -1
  12. package/dist/core/createTranslator.d.ts +1 -1
  13. package/dist/core/createTranslatorImpl.d.ts +2 -2
  14. package/dist/core/use-intl.esm.js +0 -9
  15. package/dist/core/use-intl.esm.js.map +1 -1
  16. package/dist/core/use-intl.esm10.js +2 -4
  17. package/dist/core/use-intl.esm10.js.map +1 -1
  18. package/dist/core/use-intl.esm2.js +10 -12
  19. package/dist/core/use-intl.esm2.js.map +1 -1
  20. package/dist/core/use-intl.esm3.js +13 -36
  21. package/dist/core/use-intl.esm3.js.map +1 -1
  22. package/dist/core/use-intl.esm4.js +0 -1
  23. package/dist/core/use-intl.esm4.js.map +1 -1
  24. package/dist/core/use-intl.esm5.js +12 -56
  25. package/dist/core/use-intl.esm5.js.map +1 -1
  26. package/dist/core/use-intl.esm6.js +50 -12
  27. package/dist/core/use-intl.esm6.js.map +1 -1
  28. package/dist/core/use-intl.esm7.js +188 -7
  29. package/dist/core/use-intl.esm7.js.map +1 -1
  30. package/dist/core/use-intl.esm8.js +7 -230
  31. package/dist/core/use-intl.esm8.js.map +1 -1
  32. package/dist/core/use-intl.esm9.js +2 -6
  33. package/dist/core/use-intl.esm9.js.map +1 -1
  34. package/dist/core/utils/MessageKeys.d.ts +1 -1
  35. package/dist/core/utils/NamespaceKeys.d.ts +1 -1
  36. package/dist/core/utils/NestedKeyOf.d.ts +1 -1
  37. package/dist/core/utils/NestedValueOf.d.ts +1 -1
  38. package/dist/react/IntlProvider.d.ts +1 -1
  39. package/dist/react/use-intl.esm.js +1 -2
  40. package/dist/react/use-intl.esm.js.map +1 -1
  41. package/dist/react/use-intl.esm10.js +0 -2
  42. package/dist/react/use-intl.esm10.js.map +1 -1
  43. package/dist/react/use-intl.esm11.js +9 -10
  44. package/dist/react/use-intl.esm11.js.map +1 -1
  45. package/dist/react/use-intl.esm2.js +4 -4
  46. package/dist/react/use-intl.esm2.js.map +1 -1
  47. package/dist/react/use-intl.esm3.js +40 -3
  48. package/dist/react/use-intl.esm3.js.map +1 -1
  49. package/dist/react/use-intl.esm4.js +3 -45
  50. package/dist/react/use-intl.esm4.js.map +1 -1
  51. package/dist/react/use-intl.esm5.js.map +1 -1
  52. package/dist/react/use-intl.esm6.js +15 -10
  53. package/dist/react/use-intl.esm6.js.map +1 -1
  54. package/dist/react/use-intl.esm7.js +9 -18
  55. package/dist/react/use-intl.esm7.js.map +1 -1
  56. package/dist/react/use-intl.esm8.js.map +1 -1
  57. package/dist/react/use-intl.esm9.js +4 -8
  58. package/dist/react/use-intl.esm9.js.map +1 -1
  59. package/dist/react/useFormatter.d.ts +1 -1
  60. package/dist/react/useIntl.d.ts +1 -1
  61. package/dist/react/useNow.d.ts +1 -1
  62. package/dist/src/core/AbstractIntlMessages.d.ts +1 -1
  63. package/dist/src/core/DateTimeFormatOptions.d.ts +1 -1
  64. package/dist/src/core/Formats.d.ts +1 -1
  65. package/dist/src/core/IntlConfig.d.ts +2 -2
  66. package/dist/src/core/TranslationValues.d.ts +3 -3
  67. package/dist/src/core/createBaseTranslator.d.ts +3 -3
  68. package/dist/src/core/createFormatter.d.ts +4 -4
  69. package/dist/src/core/createIntl.d.ts +1 -1
  70. package/dist/src/core/createTranslator.d.ts +1 -1
  71. package/dist/src/core/createTranslator.js +1 -1
  72. package/dist/src/core/createTranslator.js.map +1 -1
  73. package/dist/src/core/createTranslatorImpl.d.ts +2 -2
  74. package/dist/src/core/utils/MessageKeys.d.ts +1 -1
  75. package/dist/src/core/utils/NamespaceKeys.d.ts +1 -1
  76. package/dist/src/core/utils/NestedKeyOf.d.ts +1 -1
  77. package/dist/src/core/utils/NestedValueOf.d.ts +1 -1
  78. package/dist/src/core/validateMessages.js +35 -1
  79. package/dist/src/core/validateMessages.js.map +1 -1
  80. package/dist/src/react/IntlProvider.d.ts +1 -1
  81. package/dist/src/react/useFormatter.d.ts +1 -1
  82. package/dist/src/react/useIntl.d.ts +1 -1
  83. package/dist/src/react/useNow.d.ts +1 -1
  84. package/dist/use-intl.cjs.development.js +92 -222
  85. package/dist/use-intl.cjs.development.js.map +1 -1
  86. package/dist/use-intl.cjs.production.min.js +1 -1
  87. package/dist/use-intl.cjs.production.min.js.map +1 -1
  88. package/dist/use-intl.esm.js +4 -4
  89. package/package.json +17 -17
  90. package/src/core/createTranslator.tsx +1 -1
  91. package/src/core/validateMessages.tsx +37 -3
@@ -1,233 +1,10 @@
1
- import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import IntlMessageFormat from 'intl-messageformat';
3
- import { isValidElement, cloneElement } from 'react';
4
- import IntlError, { IntlErrorCode } from './use-intl.esm.js';
5
- import convertFormatsToIntlMessageFormat from './use-intl.esm10.js';
6
- import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm6.js';
7
-
8
- function resolvePath(messages, key, namespace) {
9
- if (!messages) {
10
- throw new Error(process.env.NODE_ENV !== 'production' ? "No messages available at `" + namespace + "`." : undefined);
11
- }
12
-
13
- var message = messages;
14
- key.split('.').forEach(function (part) {
15
- var next = message[part];
16
-
17
- if (part == null || next == null) {
18
- throw new Error(process.env.NODE_ENV !== 'production' ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
19
- }
20
-
21
- message = next;
22
- });
23
- return message;
24
- }
25
-
26
- function prepareTranslationValues(values) {
27
- if (Object.keys(values).length === 0) return undefined; // Workaround for https://github.com/formatjs/formatjs/issues/1467
28
-
29
- var transformedValues = {};
30
- Object.keys(values).forEach(function (key) {
31
- var index = 0;
32
- var value = values[key];
33
- var transformed;
34
-
35
- if (typeof value === 'function') {
36
- transformed = function transformed(chunks) {
37
- var result = value(chunks);
38
- return isValidElement(result) ? cloneElement(result, {
39
- key: key + index++
40
- }) : result;
41
- };
42
- } else {
43
- transformed = value;
44
- }
45
-
46
- transformedValues[key] = transformed;
47
- });
48
- return transformedValues;
49
- }
50
-
51
- function getMessagesOrError(_ref) {
52
- var messages = _ref.messages,
53
- namespace = _ref.namespace,
54
- _ref$onError = _ref.onError,
55
- onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
56
-
57
- try {
58
- if (!messages) {
59
- throw new Error(process.env.NODE_ENV !== 'production' ? "No messages were configured on the provider." : undefined);
60
- }
61
-
62
- var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
63
-
64
- if (!retrievedMessages) {
65
- throw new Error(process.env.NODE_ENV !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
66
- }
67
-
68
- return retrievedMessages;
69
- } catch (error) {
70
- var intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
71
- onError(intlError);
72
- return intlError;
73
- }
74
- }
75
- function createBaseTranslator(_ref2) {
76
- var cachedFormatsByLocale = _ref2.cachedFormatsByLocale,
77
- defaultTranslationValues = _ref2.defaultTranslationValues,
78
- globalFormats = _ref2.formats,
79
- _ref2$getMessageFallb = _ref2.getMessageFallback,
80
- getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
81
- locale = _ref2.locale,
82
- messagesOrError = _ref2.messagesOrError,
83
- namespace = _ref2.namespace,
84
- onError = _ref2.onError,
85
- timeZone = _ref2.timeZone;
86
-
87
- function getFallbackFromErrorAndNotify(key, code, message) {
88
- var error = new IntlError(code, message);
89
- onError(error);
90
- return getMessageFallback({
91
- error: error,
92
- key: key,
93
- namespace: namespace
94
- });
95
- }
96
-
97
- function translateBaseFn(
98
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
99
- key,
100
- /** Key value pairs for values to interpolate into the message. */
101
- values,
102
- /** Provide custom formats for numbers, dates and times. */
103
- formats) {
104
- var _cachedFormatsByLocal;
105
-
106
- if (messagesOrError instanceof IntlError) {
107
- // We have already warned about this during render
108
- return getMessageFallback({
109
- error: messagesOrError,
110
- key: key,
111
- namespace: namespace
112
- });
113
- }
114
-
115
- var messages = messagesOrError;
116
- var message;
117
-
118
- try {
119
- message = resolvePath(messages, key, namespace);
120
- } catch (error) {
121
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
122
- }
123
-
124
- function joinPath(parts) {
125
- return parts.filter(function (part) {
126
- return part != null;
127
- }).join('.');
128
- }
129
-
130
- var cacheKey = joinPath([namespace, key, String(message)]);
131
- var messageFormat;
132
-
133
- if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
134
- messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
135
- } else {
136
- if (typeof message === 'object') {
137
- var code, errorMessage;
138
-
139
- if (Array.isArray(message)) {
140
- code = IntlErrorCode.INVALID_MESSAGE;
141
-
142
- if (process.env.NODE_ENV !== 'production') {
143
- errorMessage = "Message at `" + joinPath([namespace, key]) + "` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages";
144
- }
145
- } else {
146
- code = IntlErrorCode.INSUFFICIENT_PATH;
147
-
148
- if (process.env.NODE_ENV !== 'production') {
149
- errorMessage = "Message at `" + joinPath([namespace, key]) + "` 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";
150
- }
151
- }
152
-
153
- return getFallbackFromErrorAndNotify(key, code, errorMessage);
154
- }
155
-
156
- try {
157
- messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
158
- } catch (error) {
159
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
160
- }
161
-
162
- if (cachedFormatsByLocale) {
163
- if (!cachedFormatsByLocale[locale]) {
164
- cachedFormatsByLocale[locale] = {};
165
- }
166
-
167
- cachedFormatsByLocale[locale][cacheKey] = messageFormat;
168
- }
169
- }
170
-
171
- try {
172
- var formattedMessage = messageFormat.format( // @ts-ignore `intl-messageformat` expects a different format
173
- // for rich text elements since a recent minor update. This
174
- // needs to be evaluated in detail, possibly also in regards
175
- // to be able to format to parts.
176
- prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
177
-
178
- if (formattedMessage == null) {
179
- throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
180
- } // Limit the function signature to return strings or React elements
181
-
182
-
183
- return isValidElement(formattedMessage) || // Arrays of React elements
184
- Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
185
- } catch (error) {
186
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
187
- }
188
- }
189
-
190
- function translateFn(
191
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
192
- key,
193
- /** Key value pairs for values to interpolate into the message. */
194
- values,
195
- /** Provide custom formats for numbers, dates and times. */
196
- formats) {
197
- var result = translateBaseFn(key, values, formats);
198
-
199
- if (typeof result !== 'string') {
200
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, process.env.NODE_ENV !== 'production' ? "The message `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') + " didn't resolve to a string. If you want to format rich text, use `t.rich` instead." : undefined);
201
- }
202
-
203
- return result;
204
- }
205
-
206
- translateFn.rich = translateBaseFn;
207
-
208
- translateFn.raw = function (
209
- /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
210
- key) {
211
- if (messagesOrError instanceof IntlError) {
212
- // We have already warned about this during render
213
- return getMessageFallback({
214
- error: messagesOrError,
215
- key: key,
216
- namespace: namespace
217
- });
218
- }
219
-
220
- var messages = messagesOrError;
221
-
222
- try {
223
- return resolvePath(messages, key, namespace);
224
- } catch (error) {
225
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
226
- }
227
- };
228
-
229
- return translateFn;
1
+ /**
2
+ * For the strictly typed messages to work we have to wrap the namespace into
3
+ * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
4
+ */
5
+ function resolveNamespace(namespace, namespacePrefix) {
6
+ return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
230
7
  }
231
8
 
232
- export { createBaseTranslator as default, getMessagesOrError };
9
+ export { resolveNamespace as default };
233
10
  //# sourceMappingURL=use-intl.esm8.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm8.js","sources":["../../src/core/createBaseTranslator.tsx"],"sourcesContent":["// eslint-disable-next-line import/no-named-as-default -- False positive\nimport IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray\n} from 'react';\nimport AbstractIntlMessages from './AbstractIntlMessages';\nimport Formats from './Formats';\nimport {InitializedIntlConfig} from './IntlConfig';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\nfunction resolvePath(\n messages: AbstractIntlMessages | undefined,\n key: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages available at \\`${namespace}\\`.`\n : undefined\n );\n }\n\n let message = messages;\n\n key.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Could not resolve \\`${key}\\` in ${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }.`\n : undefined\n );\n }\n\n message = next;\n });\n\n return message;\n}\n\nfunction prepareTranslationValues(values: RichTranslationValues) {\n if (Object.keys(values).length === 0) return undefined;\n\n // Workaround for https://github.com/formatjs/formatjs/issues/1467\n const transformedValues: RichTranslationValues = {};\n Object.keys(values).forEach((key) => {\n let index = 0;\n const value = values[key];\n\n let transformed;\n if (typeof value === 'function') {\n transformed = (chunks: ReactNode) => {\n const result = value(chunks);\n\n return isValidElement(result)\n ? cloneElement(result, {key: key + index++})\n : result;\n };\n } else {\n transformed = value;\n }\n\n transformedValues[key] = transformed;\n });\n\n return transformedValues;\n}\n\nexport function getMessagesOrError<Messages extends AbstractIntlMessages>({\n messages,\n namespace,\n onError = defaultOnError\n}: {\n messages: Messages;\n namespace?: string;\n onError?(error: IntlError): void;\n}) {\n try {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages were configured on the provider.`\n : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(messages, namespace)\n : messages;\n\n if (!retrievedMessages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages for namespace \\`${namespace}\\` found.`\n : undefined\n );\n }\n\n return retrievedMessages;\n } catch (error) {\n const intlError = new IntlError(\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n onError(intlError);\n return intlError;\n }\n}\n\nexport type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {\n cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;\n defaultTranslationValues?: RichTranslationValues;\n namespace?: string;\n messagesOrError: Messages | IntlError;\n};\n\nexport default function createBaseTranslator<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>({\n cachedFormatsByLocale,\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback = defaultGetMessageFallback,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n}: CreateBaseTranslatorProps<Messages>) {\n function getFallbackFromErrorAndNotify(\n key: string,\n code: IntlErrorCode,\n message?: string\n ) {\n const error = new IntlError(code, message);\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n function translateBaseFn(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string,\n /** Key value pairs for values to interpolate into the message. */\n values?: RichTranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n let message;\n try {\n message = resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n\n function joinPath(parts: Array<string | undefined>) {\n return parts.filter((part) => part != null).join('.');\n }\n\n const cacheKey = joinPath([namespace, key, String(message)]);\n\n let messageFormat;\n if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale?.[locale][cacheKey];\n } else {\n if (typeof message === 'object') {\n let code, errorMessage;\n if (Array.isArray(message)) {\n code = IntlErrorCode.INVALID_MESSAGE;\n if (process.env.NODE_ENV !== 'production') {\n errorMessage = `Message at \\`${joinPath([\n namespace,\n key\n ])}\\` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages`;\n }\n } else {\n code = IntlErrorCode.INSUFFICIENT_PATH;\n if (process.env.NODE_ENV !== 'production') {\n errorMessage = `Message at \\`${joinPath([\n namespace,\n key\n ])}\\` 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`;\n }\n }\n\n return getFallbackFromErrorAndNotify(key, code, errorMessage);\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (cachedFormatsByLocale) {\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n }\n\n try {\n const formattedMessage = messageFormat.format(\n // @ts-ignore `intl-messageformat` expects a different format\n // for rich text elements since a recent minor update. This\n // needs to be evaluated in detail, possibly also in regards\n // to be able to format to parts.\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Unable to format \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n }`\n : undefined\n );\n }\n\n // Limit the function signature to return strings or React elements\n return isValidElement(formattedMessage) ||\n // Arrays of React elements\n Array.isArray(formattedMessage) ||\n typeof formattedMessage === 'string'\n ? formattedMessage\n : String(formattedMessage);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.FORMATTING_ERROR,\n (error as Error).message\n );\n }\n }\n\n function translateFn<\n TargetKey extends MessageKeys<\n NestedValueOf<Messages, NestedKey>,\n NestedKeyOf<NestedValueOf<Messages, NestedKey>>\n >\n >(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: TargetKey,\n /** Key value pairs for values to interpolate into the message. */\n values?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const result = translateBaseFn(key, values, formats);\n\n if (typeof result !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n process.env.NODE_ENV !== 'production'\n ? `The message \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n } didn't resolve to a string. If you want to format rich text, use \\`t.rich\\` instead.`\n : undefined\n );\n }\n\n return result;\n }\n\n translateFn.rich = translateBaseFn;\n\n translateFn.raw = (\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string\n ): any => {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n try {\n return resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n };\n\n return translateFn;\n}\n"],"names":["resolvePath","messages","key","namespace","Error","process","env","NODE_ENV","undefined","message","split","forEach","part","next","prepareTranslationValues","values","Object","keys","length","transformedValues","index","value","transformed","chunks","result","isValidElement","cloneElement","getMessagesOrError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","createBaseTranslator","cachedFormatsByLocale","defaultTranslationValues","globalFormats","formats","getMessageFallback","defaultGetMessageFallback","locale","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","joinPath","parts","filter","join","cacheKey","String","messageFormat","errorMessage","Array","isArray","INVALID_MESSAGE","INSUFFICIENT_PATH","IntlMessageFormat","convertFormatsToIntlMessageFormat","formattedMessage","format","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAoBA,SAASA,WAAT,CACEC,QADF,EAEEC,GAFF,EAGEC,SAHF,EAGoB;AAElB,EAAI,IAAA,CAACF,QAAL,EAAe;AACb,IAAA,MAAM,IAAIG,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GAAA,4BAAA,GACkCJ,SADlC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AgC;AAC9B,MAAOsB,OAAAA,6BAA6B,CAClC5C,GADkC,EAElCgC,aAAa,CAACyB,eAFoB,EAGlCtD,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GACqBL,eAAAA,GAAAA,GADrB,GAEMC,OAAAA,IAAAA,SAAS,GAAkBA,aAAAA,GAAAA,SAAlB,GAAkC,GAAA,GAAA,UAFjD,CAIIK,GAAAA,qFAAAA,GAAAA,SAP8B,CAApC,CAAA;AASD,KAAA;;AAED,IAAA,OAAOgB,MAAP,CAAA;AACD,GAAA;;AAED0C,EAAAA,WAAW,CAACC,IAAZ,GAAmBnB,eAAnB,CAAA;;AAEAkB,EAAAA,WAAW,CAACE,GAAZ,GAAkB;AAChB;AACAlE,EAAAA,GAFgB,EAGT;AACP,IAAI0C,IAAAA,eAAe,YAAYX,SAA/B,EAA0C;AACxC;AACA,MAAA,OAAOQ,kBAAkB,CAAC;AACxBV,QAAAA,KAAK,EAAEa,eADiB;AAExB1C,QAAAA,GAAG,EAAHA,GAFwB;AAGxBC,QAAAA,SAAS,EAATA,SAAAA;AAHwB,OAAD,CAAzB,CAAA;AAKD,KAAA;;AACD,IAAMF,IAAAA,QAAQ,GAAG2C,eAAjB,CAAA;;AAEA,IAAI,IAAA;AACF,MAAA,OAAO5C,WAAW,CAACC,QAAD,EAAWC,GAAX,EAAgBC,SAAhB,CAAlB,CAAA;AACD,KAFD,CAEE,OAAO4B,KAAP,EAAc;AACd,MAAOe,OAAAA,6BAA6B,CAClC5C,GADkC,EAElCgC,aAAa,CAACC,eAFoB,EAGjCJ,KAAe,CAACtB,OAHiB,CAApC,CAAA;AAKD,KAAA;AACF,GAvBD,CAAA;;AAyBA,EAAA,OAAOyD,WAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm8.js","sources":["../../src/core/resolveNamespace.tsx"],"sourcesContent":["/**\n * For the strictly typed messages to work we have to wrap the namespace into\n * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045\n */\nexport default function resolveNamespace(\n namespace: string,\n namespacePrefix: string\n) {\n return namespace === namespacePrefix\n ? undefined\n : namespace.slice((namespacePrefix + '.').length);\n}\n"],"names":["resolveNamespace","namespace","namespacePrefix","undefined","slice","length"],"mappings":"AAAA;;;AAGG;AACW,SAAUA,gBAAgBA,CACtCC,SAAiB,EACjBC,eAAuB,EAAA;AAEvB,EAAA,OAAOD,SAAS,KAAKC,eAAe,GAChCC,SAAS,GACTF,SAAS,CAACG,KAAK,CAAC,CAACF,eAAe,GAAG,GAAG,EAAEG,MAAM,CAAC,CAAA;AACrD;;;;"}
@@ -3,14 +3,12 @@ import IntlError, { IntlErrorCode } from './use-intl.esm.js';
3
3
  function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
4
4
  Object.entries(messages).forEach(function (_ref) {
5
5
  var key = _ref[0],
6
- messageOrMessages = _ref[1];
7
-
6
+ messageOrMessages = _ref[1];
8
7
  if (key.includes('.')) {
9
8
  var keyLabel = key;
10
9
  if (parentPath) keyLabel += " (at " + parentPath + ")";
11
10
  invalidKeyLabels.push(keyLabel);
12
11
  }
13
-
14
12
  if (messageOrMessages != null && typeof messageOrMessages === 'object') {
15
13
  validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
16
14
  return part != null;
@@ -18,13 +16,11 @@ function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
18
16
  }
19
17
  });
20
18
  }
21
-
22
19
  function validateMessages(messages, onError) {
23
20
  var invalidKeyLabels = [];
24
21
  validateMessagesSegment(messages, invalidKeyLabels);
25
-
26
22
  if (invalidKeyLabels.length > 0) {
27
- onError(new IntlError(IntlErrorCode.INVALID_KEY, "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ')));
23
+ onError(new IntlError(IntlErrorCode.INVALID_KEY, process.env.NODE_ENV !== 'production' ? "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ') + "\n\nIf you're migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from \"lodash\";\n\nconst input = {\n \"one.one\": \"1.1\",\n \"one.two\": \"1.2\",\n \"two.one.one\": \"2.1.1\"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// \"one\": {\n// \"one\": \"1.1\",\n// \"two\": \"1.2\"\n// },\n// \"two\": {\n// \"one\": {\n// \"one\": \"2.1.1\"\n// }\n// }\n// }\n" : undefined));
28
24
  }
29
25
  }
30
26
 
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm9.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n `Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\\n\\nInvalid ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}`\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY"],"mappings":";;AAGA,SAASA,uBAAT,CACEC,QADF,EAEEC,gBAFF,EAGEC,UAHF,EAGqB;AAEnBC,EAAAA,MAAM,CAACC,OAAP,CAAeJ,QAAf,CAAyBK,CAAAA,OAAzB,CAAiC,UAA6B,IAAA,EAAA;AAAA,IAAA,IAA3BC,GAA2B,GAAA,IAAA,CAAA,CAAA,CAAA;AAAA,QAAtBC,iBAAsB,GAAA,IAAA,CAAA,CAAA,CAAA,CAAA;;AAC5D,IAAA,IAAID,GAAG,CAACE,QAAJ,CAAa,GAAb,CAAJ,EAAuB;AACrB,MAAIC,IAAAA,QAAQ,GAAGH,GAAf,CAAA;AACA,MAAA,IAAIJ,UAAJ,EAAgBO,QAAQ,IAAA,OAAA,GAAYP,UAAZ,GAAR,GAAA,CAAA;AAChBD,MAAAA,gBAAgB,CAACS,IAAjB,CAAsBD,QAAtB,CAAA,CAAA;AACD,KAAA;;AAED,IAAIF,IAAAA,iBAAiB,IAAI,IAArB,IAA6B,OAAOA,iBAAP,KAA6B,QAA9D,EAAwE;AACtER,MAAAA,uBAAuB,CACrBQ,iBADqB,EAErBN,gBAFqB,EAGrB,CAACC,UAAD,EAAaI,GAAb,CAAA,CAAkBK,MAAlB,CAAyB,UAACC,IAAD,EAAA;AAAA,QAAUA,OAAAA,IAAI,IAAI,IAAlB,CAAA;AAAA,OAAzB,CAAiDC,CAAAA,IAAjD,CAAsD,GAAtD,CAHqB,CAAvB,CAAA;AAKD,KAAA;AACF,GAdD,CAAA,CAAA;AAeD,CAAA;;AAEa,SAAUC,gBAAV,CACZd,QADY,EAEZe,OAFY,EAEuB;AAEnC,EAAMd,IAAAA,gBAAgB,GAAkB,EAAxC,CAAA;AACAF,EAAAA,uBAAuB,CAACC,QAAD,EAAWC,gBAAX,CAAvB,CAAA;;AAEA,EAAA,IAAIA,gBAAgB,CAACe,MAAjB,GAA0B,CAA9B,EAAiC;AAC/BD,IAAAA,OAAO,CACL,IAAIE,SAAJ,CACEC,aAAa,CAACC,WADhB,EAAA,2JAAA,IAGIlB,gBAAgB,CAACe,MAAjB,KAA4B,CAA5B,GAAgC,KAAhC,GAAwC,MAH5C,CAIOf,GAAAA,IAAAA,GAAAA,gBAAgB,CAACY,IAAjB,CAAsB,IAAtB,CAJP,CADK,CAAP,CAAA;AAQD,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"use-intl.esm9.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n process.env.NODE_ENV !== 'production'\n ? `Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}\n\nIf you're migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from \"lodash\";\n\nconst input = {\n \"one.one\": \"1.1\",\n \"one.two\": \"1.2\",\n \"two.one.one\": \"2.1.1\"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// \"one\": {\n// \"one\": \"1.1\",\n// \"two\": \"1.2\"\n// },\n// \"two\": {\n// \"one\": {\n// \"one\": \"2.1.1\"\n// }\n// }\n// }\n`\n : undefined\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","_ref","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY","process","env","NODE_ENV","undefined"],"mappings":";;AAGA,SAASA,uBAAuBA,CAC9BC,QAA8B,EAC9BC,gBAA+B,EAC/BC,UAAmB,EAAA;EAEnBC,MAAM,CAACC,OAAO,CAACJ,QAAQ,CAAC,CAACK,OAAO,CAAC,UAAAC,IAAA,EAA6B;IAAA,IAA3BC,GAAG,GAAAD,IAAA,CAAA,CAAA,CAAA;AAAEE,MAAAA,iBAAiB,GAAAF,IAAA,CAAA,CAAA,CAAA,CAAA;AACvD,IAAA,IAAIC,GAAG,CAACE,QAAQ,CAAC,GAAG,CAAC,EAAE;MACrB,IAAIC,QAAQ,GAAGH,GAAG,CAAA;AAClB,MAAA,IAAIL,UAAU,EAAEQ,QAAQ,IAAA,OAAA,GAAYR,UAAU,GAAG,GAAA,CAAA;AACjDD,MAAAA,gBAAgB,CAACU,IAAI,CAACD,QAAQ,CAAC,CAAA;AAChC,KAAA;IAED,IAAIF,iBAAiB,IAAI,IAAI,IAAI,OAAOA,iBAAiB,KAAK,QAAQ,EAAE;AACtET,MAAAA,uBAAuB,CACrBS,iBAAiB,EACjBP,gBAAgB,EAChB,CAACC,UAAU,EAAEK,GAAG,CAAC,CAACK,MAAM,CAAC,UAACC,IAAI,EAAA;QAAA,OAAKA,IAAI,IAAI,IAAI,CAAA;AAAA,OAAA,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC,CAC3D,CAAA;AACF,KAAA;AACH,GAAC,CAAC,CAAA;AACJ,CAAA;AAEc,SAAUC,gBAAgBA,CACtCf,QAA8B,EAC9BgB,OAAmC,EAAA;EAEnC,IAAMf,gBAAgB,GAAkB,EAAE,CAAA;AAC1CF,EAAAA,uBAAuB,CAACC,QAAQ,EAAEC,gBAAgB,CAAC,CAAA;AAEnD,EAAA,IAAIA,gBAAgB,CAACgB,MAAM,GAAG,CAAC,EAAE;AAC/BD,IAAAA,OAAO,CACL,IAAIE,SAAS,CACXC,aAAa,CAACC,WAAW,EACzBC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,GAI/BtB,2JAAAA,IAAAA,gBAAgB,CAACgB,MAAM,KAAK,CAAC,GAAG,KAAK,GAAG,MAC1C,CAAKhB,GAAAA,IAAAA,GAAAA,gBAAgB,CAACa,IAAI,CAAC,IAAI,CAAC,GA+BhCU,4gBAAAA,GAAAA,SAAS,CACd,CACF,CAAA;AACF,GAAA;AACH;;;;"}
@@ -1,5 +1,5 @@
1
1
  import NestedValueOf from './NestedValueOf';
2
- declare type MessageKeys<ObjectType, Keys extends string> = {
2
+ type MessageKeys<ObjectType, Keys extends string> = {
3
3
  [Property in Keys]: NestedValueOf<ObjectType, Property> extends string ? Property : never;
4
4
  }[Keys];
5
5
  export default MessageKeys;
@@ -1,5 +1,5 @@
1
1
  import NestedValueOf from './NestedValueOf';
2
- declare type NamespaceKeys<ObjectType, Keys extends string> = {
2
+ type NamespaceKeys<ObjectType, Keys extends string> = {
3
3
  [Property in Keys]: NestedValueOf<ObjectType, Property> extends string ? never : Property;
4
4
  }[Keys];
5
5
  export default NamespaceKeys;
@@ -1,4 +1,4 @@
1
- declare type NestedKeyOf<ObjectType> = ObjectType extends object ? {
1
+ type NestedKeyOf<ObjectType> = ObjectType extends object ? {
2
2
  [Key in keyof ObjectType]: `${Key & string}` | `${Key & string}.${NestedKeyOf<ObjectType[Key]>}`;
3
3
  }[keyof ObjectType] : never;
4
4
  export default NestedKeyOf;
@@ -1,2 +1,2 @@
1
- declare type NestedValueOf<ObjectType, Property extends string> = Property extends `${infer Key}.${infer Rest}` ? Key extends keyof ObjectType ? NestedValueOf<ObjectType[Key], Rest> : never : Property extends keyof ObjectType ? ObjectType[Property] : never;
1
+ type NestedValueOf<ObjectType, Property extends string> = Property extends `${infer Key}.${infer Rest}` ? Key extends keyof ObjectType ? NestedValueOf<ObjectType[Key], Rest> : never : Property extends keyof ObjectType ? ObjectType[Property] : never;
2
2
  export default NestedValueOf;
@@ -1,6 +1,6 @@
1
1
  import { ReactNode } from 'react';
2
2
  import IntlConfig from '../core/IntlConfig';
3
- declare type Props = IntlConfig & {
3
+ type Props = IntlConfig & {
4
4
  children: ReactNode;
5
5
  };
6
6
  export default function IntlProvider({ children, ...props }: Props): JSX.Element;
@@ -6,8 +6,7 @@ import getInitializedConfig from './use-intl.esm9.js';
6
6
  var _excluded = ["children"];
7
7
  function IntlProvider(_ref) {
8
8
  var children = _ref.children,
9
- props = _objectWithoutPropertiesLoose(_ref, _excluded);
10
-
9
+ props = _objectWithoutPropertiesLoose(_ref, _excluded);
11
10
  return React.createElement(IntlContext.Provider, {
12
11
  value: getInitializedConfig(props)
13
12
  }, children);
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm.js","sources":["../../src/react/IntlProvider.tsx"],"sourcesContent":["import React, {ReactNode} from 'react';\nimport IntlConfig from '../core/IntlConfig';\nimport IntlContext from './IntlContext';\nimport getInitializedConfig from './getInitializedConfig';\n\ntype Props = IntlConfig & {\n children: ReactNode;\n};\n\nexport default function IntlProvider({children, ...props}: Props) {\n return (\n <IntlContext.Provider value={getInitializedConfig(props)}>\n {children}\n </IntlContext.Provider>\n );\n}\n"],"names":["IntlProvider","children","props","React","createElement","IntlContext","Provider","value","getInitializedConfig"],"mappings":";;;;;;AASc,SAAUA,YAAV,CAAkD,IAAA,EAAA;AAAA,EAA1BC,IAAAA,QAA0B,QAA1BA,QAA0B;AAAA,MAAbC,KAAa,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AAC9D,EAAA,OACEC,KAAC,CAAAC,aAAD,CAACC,WAAW,CAACC,QAAb;AAAsBC,IAAAA,KAAK,EAAEC,oBAAoB,CAACN,KAAD,CAAA;GAAjD,EACGD,QADH,CADF,CAAA;AAKD;;;;"}
1
+ {"version":3,"file":"use-intl.esm.js","sources":["../../src/react/useTranslations.tsx"],"sourcesContent":["import {ReactElement, ReactNodeArray} from 'react';\nimport Formats from '../core/Formats';\nimport TranslationValues, {\n RichTranslationValues\n} from '../core/TranslationValues';\nimport MessageKeys from '../core/utils/MessageKeys';\nimport NamespaceKeys from '../core/utils/NamespaceKeys';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport NestedValueOf from '../core/utils/NestedValueOf';\nimport useIntlContext from './useIntlContext';\nimport useTranslationsImpl from './useTranslationsImpl';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>(\n namespace?: NestedKey\n): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: RichTranslationValues,\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n const context = useIntlContext();\n const messages = context.messages as IntlMessages;\n\n // We have to wrap the actual hook so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return useTranslationsImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {'!': messages},\n // @ts-ignore\n namespace ? `!.${namespace}` : '!',\n '!'\n );\n}\n"],"names":["useTranslations","namespace","context","useIntlContext","messages","useTranslationsImpl"],"mappings":";;;AAYA;;;;;;;AAOG;AACqB,SAAAA,eAAeA,CAMrCC,SAAqB,EAAA;AA6DrB,EAAA,IAAMC,OAAO,GAAGC,cAAc,EAAE,CAAA;AAChC,EAAA,IAAMC,QAAQ,GAAGF,OAAO,CAACE,QAAwB,CAAA;AAEjD;AACA;AACA;AACA,EAAA,OAAOC,mBAAmB,CAIxB;AAAC,IAAA,GAAG,EAAED,QAAAA;GAAS;AACf;AACAH,EAAAA,SAAS,UAAQA,SAAS,GAAK,GAAG,EAClC,GAAG,CACJ,CAAA;AACH;;;;"}
@@ -3,11 +3,9 @@ import IntlContext from './use-intl.esm8.js';
3
3
 
4
4
  function useIntlContext() {
5
5
  var context = useContext(IntlContext);
6
-
7
6
  if (!context) {
8
7
  throw new Error(process.env.NODE_ENV !== 'production' ? 'No intl context found. Have you configured the provider?' : undefined);
9
8
  }
10
-
11
9
  return context;
12
10
  }
13
11
 
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm10.js","sources":["../../src/react/useIntlContext.tsx"],"sourcesContent":["import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n"],"names":["useIntlContext","context","useContext","IntlContext","Error","process","env","NODE_ENV","undefined"],"mappings":";;;AAGc,SAAUA,cAAV,GAAwB;AACpC,EAAA,IAAMC,OAAO,GAAGC,UAAU,CAACC,WAAD,CAA1B,CAAA;;AAEA,EAAI,IAAA,CAACF,OAAL,EAAc;AACZ,IAAA,MAAM,IAAIG,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GACI,0DADJ,GAEIC,SAHA,CAAN,CAAA;AAKD,GAAA;;AAED,EAAA,OAAOP,OAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm10.js","sources":["../../src/react/IntlContext.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport {InitializedIntlConfig} from '../core/IntlConfig';\n\nconst IntlContext = createContext<InitializedIntlConfig | undefined>(undefined);\n\nexport default IntlContext;\n"],"names":["IntlContext","createContext","undefined"],"mappings":";;AAGA,IAAMA,WAAW,gBAAGC,aAAa,CAAoCC,SAAS;;;;"}
@@ -1,19 +1,18 @@
1
1
  import { useRef, useMemo } from 'react';
2
- import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm8.js';
3
- import resolveNamespace from '../core/use-intl.esm7.js';
2
+ import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm7.js';
3
+ import resolveNamespace from '../core/use-intl.esm8.js';
4
4
  import useIntlContext from './use-intl.esm10.js';
5
5
 
6
6
  function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
7
7
  var _useIntlContext = useIntlContext(),
8
- defaultTranslationValues = _useIntlContext.defaultTranslationValues,
9
- globalFormats = _useIntlContext.formats,
10
- getMessageFallback = _useIntlContext.getMessageFallback,
11
- locale = _useIntlContext.locale,
12
- onError = _useIntlContext.onError,
13
- timeZone = _useIntlContext.timeZone; // The `namespacePrefix` is part of the type system.
8
+ defaultTranslationValues = _useIntlContext.defaultTranslationValues,
9
+ globalFormats = _useIntlContext.formats,
10
+ getMessageFallback = _useIntlContext.getMessageFallback,
11
+ locale = _useIntlContext.locale,
12
+ onError = _useIntlContext.onError,
13
+ timeZone = _useIntlContext.timeZone;
14
+ // The `namespacePrefix` is part of the type system.
14
15
  // See the comment in the hook invocation.
15
-
16
-
17
16
  allMessages = allMessages[namespacePrefix];
18
17
  namespace = resolveNamespace(namespace, namespacePrefix);
19
18
  var cachedFormatsByLocaleRef = useRef({});
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm11.js","sources":["../../src/react/useTranslationsImpl.tsx"],"sourcesContent":["// eslint-disable-next-line import/no-named-as-default -- False positive\nimport IntlMessageFormat from 'intl-messageformat';\nimport {useMemo, useRef} from 'react';\nimport AbstractIntlMessages from '../core/AbstractIntlMessages';\nimport createBaseTranslator, {\n getMessagesOrError\n} from '../core/createBaseTranslator';\nimport resolveNamespace from '../core/resolveNamespace';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport useIntlContext from './useIntlContext';\n\nexport default function useTranslationsImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string) {\n const {\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback,\n locale,\n onError,\n timeZone\n } = useIntlContext();\n\n // The `namespacePrefix` is part of the type system.\n // See the comment in the hook invocation.\n allMessages = allMessages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(\n () => getMessagesOrError({messages: allMessages, namespace, onError}),\n [allMessages, namespace, onError]\n );\n\n const translate = useMemo(\n () =>\n createBaseTranslator({\n cachedFormatsByLocale: cachedFormatsByLocaleRef.current,\n getMessageFallback,\n messagesOrError,\n defaultTranslationValues,\n namespace,\n onError,\n formats: globalFormats,\n locale,\n timeZone\n }),\n [\n getMessageFallback,\n messagesOrError,\n defaultTranslationValues,\n namespace,\n onError,\n globalFormats,\n locale,\n timeZone\n ]\n );\n\n return translate;\n}\n"],"names":["useTranslationsImpl","allMessages","namespace","namespacePrefix","useIntlContext","defaultTranslationValues","globalFormats","formats","getMessageFallback","locale","onError","timeZone","resolveNamespace","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","getMessagesOrError","messages","translate","createBaseTranslator","cachedFormatsByLocale","current"],"mappings":";;;;;AAWwB,SAAAA,mBAAA,CAGtBC,WAHsB,EAGCC,SAHD,EAGuBC,eAHvB,EAG8C;AACpE,EAAA,IAAA,eAAA,GAOIC,cAAc,EAPlB;AAAA,MACEC,wBADF,mBACEA,wBADF;AAAA,MAEWC,aAFX,mBAEEC,OAFF;AAAA,MAGEC,kBAHF,mBAGEA,kBAHF;AAAA,MAIEC,MAJF,mBAIEA,MAJF;AAAA,MAKEC,OALF,mBAKEA,OALF;AAAA,MAMEC,QANF,GAAA,eAAA,CAMEA,QANF,CADoE;AAWpE;;;AACAV,EAAAA,WAAW,GAAGA,WAAW,CAACE,eAAD,CAAzB,CAAA;AACAD,EAAAA,SAAS,GAAGU,gBAAgB,CAACV,SAAD,EAAYC,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMU,wBAAwB,GAAGC,MAAM,CAErC,EAFqC,CAAvC,CAAA;AAIA,EAAMC,IAAAA,eAAe,GAAGC,OAAO,CAC7B,YAAA;AAAA,IAAA,OAAMC,kBAAkB,CAAC;AAACC,MAAAA,QAAQ,EAAEjB,WAAX;AAAwBC,MAAAA,SAAS,EAATA,SAAxB;AAAmCQ,MAAAA,OAAO,EAAPA,OAAAA;AAAnC,KAAD,CAAxB,CAAA;AAAA,GAD6B,EAE7B,CAACT,WAAD,EAAcC,SAAd,EAAyBQ,OAAzB,CAF6B,CAA/B,CAAA;AAKA,EAAMS,IAAAA,SAAS,GAAGH,OAAO,CACvB,YAAA;AAAA,IAAA,OACEI,oBAAoB,CAAC;AACnBC,MAAAA,qBAAqB,EAAER,wBAAwB,CAACS,OAD7B;AAEnBd,MAAAA,kBAAkB,EAAlBA,kBAFmB;AAGnBO,MAAAA,eAAe,EAAfA,eAHmB;AAInBV,MAAAA,wBAAwB,EAAxBA,wBAJmB;AAKnBH,MAAAA,SAAS,EAATA,SALmB;AAMnBQ,MAAAA,OAAO,EAAPA,OANmB;AAOnBH,MAAAA,OAAO,EAAED,aAPU;AAQnBG,MAAAA,MAAM,EAANA,MARmB;AASnBE,MAAAA,QAAQ,EAARA,QAAAA;AATmB,KAAD,CADtB,CAAA;AAAA,GADuB,EAavB,CACEH,kBADF,EAEEO,eAFF,EAGEV,wBAHF,EAIEH,SAJF,EAKEQ,OALF,EAMEJ,aANF,EAOEG,MAPF,EAQEE,QARF,CAbuB,CAAzB,CAAA;AAyBA,EAAA,OAAOQ,SAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm11.js","sources":["../../src/react/getInitializedConfig.tsx"],"sourcesContent":["import IntlConfig from '../core/IntlConfig';\nimport {defaultGetMessageFallback, defaultOnError} from '../core/defaults';\nimport validateMessages from '../core/validateMessages';\n\n/**\n * Enhances the incoming props with defaults.\n */\nexport default function getInitializedConfig<\n // This is a generic to allow for stricter typing. E.g.\n // the RSC integration always provides a `now` value.\n Props extends Omit<IntlConfig, 'children'>\n>({getMessageFallback, messages, onError, ...rest}: Props) {\n const finalOnError = onError || defaultOnError;\n const finalGetMessageFallback =\n getMessageFallback || defaultGetMessageFallback;\n\n if (process.env.NODE_ENV !== 'production') {\n if (messages) {\n validateMessages(messages, finalOnError);\n }\n }\n\n return {\n ...rest,\n messages,\n onError: finalOnError,\n getMessageFallback: finalGetMessageFallback\n };\n}\n"],"names":["getInitializedConfig","_ref","getMessageFallback","messages","onError","rest","_objectWithoutPropertiesLoose","_excluded","finalOnError","defaultOnError","finalGetMessageFallback","defaultGetMessageFallback","process","env","NODE_ENV","validateMessages","_extends"],"mappings":";;;;;AAIA;;AAEG;AACqB,SAAAA,oBAAoBA,CAAAC,IAAA,EAIa;AAAA,EAAA,IAAtDC,kBAAkB,GAAAD,IAAA,CAAlBC,kBAAkB;IAAEC,QAAQ,GAAAF,IAAA,CAARE,QAAQ;IAAEC,OAAO,GAAAH,IAAA,CAAPG,OAAO;AAAKC,IAAAA,IAAI,GAAAC,6BAAA,CAAAL,IAAA,EAAAM,SAAA,CAAA,CAAA;AAC/C,EAAA,IAAMC,YAAY,GAAGJ,OAAO,IAAIK,cAAc,CAAA;AAC9C,EAAA,IAAMC,uBAAuB,GAC3BR,kBAAkB,IAAIS,yBAAyB,CAAA;AAEjD,EAAA,IAAIC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,EAAE;AACzC,IAAA,IAAIX,QAAQ,EAAE;AACZY,MAAAA,gBAAgB,CAACZ,QAAQ,EAAEK,YAAY,CAAC,CAAA;AACzC,KAAA;AACF,GAAA;EAED,OAAAQ,QAAA,KACKX,IAAI,EAAA;AACPF,IAAAA,QAAQ,EAARA,QAAQ;AACRC,IAAAA,OAAO,EAAEI,YAAY;AACrBN,IAAAA,kBAAkB,EAAEQ,uBAAAA;AAAuB,GAAA,CAAA,CAAA;AAE/C;;;;"}
@@ -9,16 +9,16 @@ import useTranslationsImpl from './use-intl.esm11.js';
9
9
  * The namespace can also indicate nesting by using a dot
10
10
  * (e.g. `namespace.Component`).
11
11
  */
12
-
13
12
  function useTranslations(namespace) {
14
13
  var context = useIntlContext();
15
- var messages = context.messages; // We have to wrap the actual hook so the type inference for the optional
14
+ var messages = context.messages;
15
+ // We have to wrap the actual hook so the type inference for the optional
16
16
  // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
17
17
  // The prefix ("!") is arbitrary.
18
-
19
18
  return useTranslationsImpl({
20
19
  '!': messages
21
- }, // @ts-ignore
20
+ },
21
+ // @ts-ignore
22
22
  namespace ? "!." + namespace : '!', '!');
23
23
  }
24
24
 
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm2.js","sources":["../../src/react/useTranslations.tsx"],"sourcesContent":["import {ReactElement, ReactNodeArray} from 'react';\nimport Formats from '../core/Formats';\nimport TranslationValues, {\n RichTranslationValues\n} from '../core/TranslationValues';\nimport MessageKeys from '../core/utils/MessageKeys';\nimport NamespaceKeys from '../core/utils/NamespaceKeys';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport NestedValueOf from '../core/utils/NestedValueOf';\nimport useIntlContext from './useIntlContext';\nimport useTranslationsImpl from './useTranslationsImpl';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>(\n namespace?: NestedKey\n): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: RichTranslationValues,\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n const context = useIntlContext();\n const messages = context.messages as IntlMessages;\n\n // We have to wrap the actual hook so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return useTranslationsImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {'!': messages},\n // @ts-ignore\n namespace ? `!.${namespace}` : '!',\n '!'\n );\n}\n"],"names":["useTranslations","namespace","context","useIntlContext","messages","useTranslationsImpl"],"mappings":";;;AAYA;;;;;;;AAOG;;AACqB,SAAAA,eAAA,CAMtBC,SANsB,EAMD;AA6DrB,EAAMC,IAAAA,OAAO,GAAGC,cAAc,EAA9B,CAAA;AACA,EAAA,IAAMC,QAAQ,GAAGF,OAAO,CAACE,QAAzB,CA9DqB;AAiErB;AACA;;AACA,EAAA,OAAOC,mBAAmB,CAIxB;AAAC,IAAKD,GAAAA,EAAAA,QAAAA;AAAN,GAJwB;AAMxBH,EAAAA,SAAS,GAAQA,IAAAA,GAAAA,SAAR,GAAsB,GANP,EAOxB,GAPwB,CAA1B,CAAA;AASD;;;;"}
1
+ {"version":3,"file":"use-intl.esm2.js","sources":["../../src/react/useLocale.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n"],"names":["useLocale","useIntlContext","locale"],"mappings":";;AAEc,SAAUA,SAASA,GAAA;AAC/B,EAAA,OAAOC,cAAc,EAAE,CAACC,MAAM,CAAA;AAChC;;;;"}
@@ -1,8 +1,45 @@
1
+ import { useState, useEffect } from 'react';
1
2
  import useIntlContext from './use-intl.esm10.js';
2
3
 
3
- function useLocale() {
4
- return useIntlContext().locale;
4
+ function getNow() {
5
+ return new Date();
6
+ }
7
+ /**
8
+ * Reading the current date via `new Date()` in components should be avoided, as
9
+ * it causes components to be impure and can lead to flaky tests. Instead, this
10
+ * hook can be used.
11
+ *
12
+ * By default, it returns the time when the component mounts. If `updateInterval`
13
+ * is specified, the value will be updated based on the interval.
14
+ *
15
+ * You can however also return a static value from this hook, if you
16
+ * configure the `now` parameter on the context provider. Note however,
17
+ * that if `updateInterval` is configured in this case, the component
18
+ * will initialize with the global value, but will afterwards update
19
+ * continuously based on the interval.
20
+ *
21
+ * For unit tests, this can be mocked to a constant value. For end-to-end
22
+ * testing, an environment parameter can be passed to the `now` parameter
23
+ * of the provider to mock this to a static value.
24
+ */
25
+ function useNow(options) {
26
+ var updateInterval = options == null ? void 0 : options.updateInterval;
27
+ var _useIntlContext = useIntlContext(),
28
+ globalNow = _useIntlContext.now;
29
+ var _useState = useState(globalNow || getNow()),
30
+ now = _useState[0],
31
+ setNow = _useState[1];
32
+ useEffect(function () {
33
+ if (!updateInterval) return;
34
+ var intervalId = setInterval(function () {
35
+ setNow(getNow());
36
+ }, updateInterval);
37
+ return function () {
38
+ clearInterval(intervalId);
39
+ };
40
+ }, [globalNow, updateInterval]);
41
+ return now;
5
42
  }
6
43
 
7
- export { useLocale as default };
44
+ export { useNow as default };
8
45
  //# sourceMappingURL=use-intl.esm3.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm3.js","sources":["../../src/react/useLocale.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n"],"names":["useLocale","useIntlContext","locale"],"mappings":";;AAEc,SAAUA,SAAV,GAAmB;AAC/B,EAAOC,OAAAA,cAAc,GAAGC,MAAxB,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm3.js","sources":["../../src/react/IntlProvider.tsx"],"sourcesContent":["import React, {ReactNode} from 'react';\nimport IntlConfig from '../core/IntlConfig';\nimport IntlContext from './IntlContext';\nimport getInitializedConfig from './getInitializedConfig';\n\ntype Props = IntlConfig & {\n children: ReactNode;\n};\n\nexport default function IntlProvider({children, ...props}: Props) {\n return (\n <IntlContext.Provider value={getInitializedConfig(props)}>\n {children}\n </IntlContext.Provider>\n );\n}\n"],"names":["IntlProvider","_ref","children","props","_objectWithoutPropertiesLoose","_excluded","React","createElement","IntlContext","Provider","value","getInitializedConfig"],"mappings":";;;;;;AASc,SAAUA,YAAYA,CAAAC,IAAA,EAA4B;AAAA,EAAA,IAA1BC,QAAQ,GAAAD,IAAA,CAARC,QAAQ;AAAKC,IAAAA,KAAK,GAAAC,6BAAA,CAAAH,IAAA,EAAAI,SAAA,CAAA,CAAA;AACtD,EAAA,OACEC,KAAC,CAAAC,aAAA,CAAAC,WAAW,CAACC,QAAQ;IAACC,KAAK,EAAEC,oBAAoB,CAACR,KAAK,CAAA;GAAC,EACrDD,QAAQ,CACY,CAAA;AAE3B;;;;"}