use-intl 3.0.0-beta.1 → 3.0.0-beta.3

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 (206) hide show
  1. package/_IntlProvider.d.ts +1 -0
  2. package/_useLocale.d.ts +1 -0
  3. package/core.d.ts +1 -1
  4. package/dist/_IntlProvider.js +7 -0
  5. package/dist/_useLocale.js +7 -0
  6. package/dist/core.js +3 -4
  7. package/dist/development/IntlContext-b5cc6be8.js +8 -0
  8. package/dist/development/_IntlProvider.js +27 -0
  9. package/dist/development/_useLocale-321e619f.js +19 -0
  10. package/dist/development/_useLocale.js +11 -0
  11. package/dist/development/core.js +92 -0
  12. package/dist/development/createIntl-dfba4462.js +421 -0
  13. package/dist/development/index.js +31 -0
  14. package/dist/development/initializeConfig-cde42612.js +119 -0
  15. package/dist/development/react.js +160 -0
  16. package/dist/esm/IntlContext-381f3ce4.js +1 -0
  17. package/dist/esm/_IntlProvider.js +1 -0
  18. package/dist/esm/_useLocale-89b32eb9.js +1 -0
  19. package/dist/esm/_useLocale.js +1 -0
  20. package/dist/esm/core.js +1 -0
  21. package/dist/esm/createIntl-164a7766.js +1 -0
  22. package/dist/esm/index.js +1 -0
  23. package/dist/esm/initializeConfig-c316f266.js +1 -0
  24. package/dist/esm/react.js +1 -0
  25. package/dist/index.js +3 -4
  26. package/dist/production/IntlContext-381f3ce4.js +1 -0
  27. package/dist/production/_IntlProvider.js +1 -0
  28. package/dist/production/_useLocale-8e23751a.js +1 -0
  29. package/dist/production/_useLocale.js +1 -0
  30. package/dist/production/core.js +1 -0
  31. package/dist/production/createIntl-e408cd5c.js +1 -0
  32. package/dist/production/index.js +1 -0
  33. package/dist/production/initializeConfig-984a566d.js +1 -0
  34. package/dist/production/react.js +1 -0
  35. package/dist/react.js +3 -4
  36. package/dist/types/src/_IntlProvider.d.ts +1 -0
  37. package/dist/types/src/_useLocale.d.ts +1 -0
  38. package/dist/{core → types/src/core}/AbstractIntlMessages.d.ts +9 -9
  39. package/dist/{core → types/src/core}/DateTimeFormatOptions.d.ts +73 -73
  40. package/dist/{core → types/src/core}/Formats.d.ts +8 -8
  41. package/dist/{core → types/src/core}/IntlConfig.d.ts +54 -54
  42. package/dist/{core → types/src/core}/IntlError.d.ts +13 -13
  43. package/dist/types/src/core/MessageFormatCache.d.ts +5 -0
  44. package/dist/{core → types/src/core}/NumberFormatOptions.d.ts +2 -2
  45. package/dist/{core → types/src/core}/TimeZone.d.ts +2 -2
  46. package/dist/{core → types/src/core}/TranslationValues.d.ts +6 -6
  47. package/dist/{core → types/src/core}/convertFormatsToIntlMessageFormat.d.ts +11 -11
  48. package/dist/{core → types/src/core}/createBaseTranslator.d.ts +21 -21
  49. package/dist/{core → types/src/core}/createFormatter.d.ts +19 -19
  50. package/dist/{core → types/src/core}/createIntl.d.ts +7 -7
  51. package/dist/{core → types/src/core}/createTranslator.d.ts +47 -47
  52. package/dist/{core → types/src/core}/createTranslatorImpl.d.ts +13 -13
  53. package/dist/{core → types/src/core}/defaults.d.ts +11 -11
  54. package/dist/{core → types/src/core}/index.d.ts +16 -16
  55. package/dist/{core → types/src/core}/initializeConfig.d.ts +14 -13
  56. package/dist/{core → types/src/core}/resolveNamespace.d.ts +5 -5
  57. package/dist/{core → types/src/core}/utils/MessageKeys.d.ts +5 -5
  58. package/dist/{core → types/src/core}/utils/NamespaceKeys.d.ts +5 -5
  59. package/dist/{core → types/src/core}/utils/NestedKeyOf.d.ts +4 -4
  60. package/dist/{core → types/src/core}/utils/NestedValueOf.d.ts +2 -2
  61. package/dist/{core → types/src/core}/validateMessages.d.ts +3 -3
  62. package/dist/{core.d.ts → types/src/core.d.ts} +1 -1
  63. package/dist/{index.d.ts → types/src/index.d.ts} +2 -2
  64. package/dist/types/src/react/IntlContext.d.ts +13 -0
  65. package/dist/{react → types/src/react}/IntlProvider.d.ts +7 -7
  66. package/dist/{react → types/src/react}/getInitializedConfig.d.ts +1 -1
  67. package/dist/{react → types/src/react}/index.d.ts +8 -8
  68. package/dist/{react → types/src/react}/useFormatter.d.ts +6 -6
  69. package/dist/{react → types/src/react}/useIntl.d.ts +6 -6
  70. package/dist/types/src/react/useIntlContext.d.ts +10 -0
  71. package/dist/{react → types/src/react}/useLocale.d.ts +1 -1
  72. package/dist/{react → types/src/react}/useMessages.d.ts +1 -1
  73. package/dist/{react → types/src/react}/useNow.d.ts +23 -23
  74. package/dist/{react → types/src/react}/useTimeZone.d.ts +1 -1
  75. package/dist/{react → types/src/react}/useTranslations.d.ts +44 -44
  76. package/dist/{react → types/src/react}/useTranslationsImpl.d.ts +8 -8
  77. package/dist/{react.d.ts → types/src/react.d.ts} +1 -1
  78. package/dist/types/test/core/createFormatter.test.d.ts +1 -0
  79. package/dist/types/test/core/createIntl.test.d.ts +1 -0
  80. package/dist/types/test/core/createTranslator.test.d.ts +1 -0
  81. package/dist/types/test/react/useFormatter.test.d.ts +1 -0
  82. package/dist/types/test/react/useIntl.test.d.ts +1 -0
  83. package/dist/types/test/react/useLocale.test.d.ts +1 -0
  84. package/dist/types/test/react/useMessages.test.d.ts +1 -0
  85. package/dist/types/test/react/useNow.test.d.ts +1 -0
  86. package/dist/types/test/react/useTimeZone.test.d.ts +1 -0
  87. package/dist/types/test/react/useTranslations.test.d.ts +1 -0
  88. package/dist/{test → types/test}/setup.d.ts +1 -1
  89. package/package.json +33 -21
  90. package/react.d.ts +1 -1
  91. package/dist/_virtual/_rollupPluginBabelHelpers.esm.js +0 -102
  92. package/dist/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
  93. package/dist/_virtual/use-intl.esm.js +0 -102
  94. package/dist/_virtual/use-intl.esm.js.map +0 -1
  95. package/dist/core/IntlError.esm.js +0 -33
  96. package/dist/core/IntlError.esm.js.map +0 -1
  97. package/dist/core/convertFormatsToIntlMessageFormat.esm.js +0 -32
  98. package/dist/core/convertFormatsToIntlMessageFormat.esm.js.map +0 -1
  99. package/dist/core/createBaseTranslator.esm.js +0 -201
  100. package/dist/core/createBaseTranslator.esm.js.map +0 -1
  101. package/dist/core/createFormatter.esm.js +0 -138
  102. package/dist/core/createFormatter.esm.js.map +0 -1
  103. package/dist/core/createIntl.esm.js +0 -14
  104. package/dist/core/createIntl.esm.js.map +0 -1
  105. package/dist/core/createTranslator.esm.js +0 -37
  106. package/dist/core/createTranslator.esm.js.map +0 -1
  107. package/dist/core/createTranslatorImpl.esm.js +0 -50
  108. package/dist/core/createTranslatorImpl.esm.js.map +0 -1
  109. package/dist/core/defaults.esm.js +0 -15
  110. package/dist/core/defaults.esm.js.map +0 -1
  111. package/dist/core/initializeConfig.esm.js +0 -29
  112. package/dist/core/initializeConfig.esm.js.map +0 -1
  113. package/dist/core/resolveNamespace.esm.js +0 -10
  114. package/dist/core/resolveNamespace.esm.js.map +0 -1
  115. package/dist/core/use-intl.esm.js +0 -37
  116. package/dist/core/use-intl.esm.js.map +0 -1
  117. package/dist/core/use-intl.esm10.js +0 -28
  118. package/dist/core/use-intl.esm10.js.map +0 -1
  119. package/dist/core/use-intl.esm11.js +0 -10
  120. package/dist/core/use-intl.esm11.js.map +0 -1
  121. package/dist/core/use-intl.esm2.js +0 -33
  122. package/dist/core/use-intl.esm2.js.map +0 -1
  123. package/dist/core/use-intl.esm3.js +0 -14
  124. package/dist/core/use-intl.esm3.js.map +0 -1
  125. package/dist/core/use-intl.esm4.js +0 -201
  126. package/dist/core/use-intl.esm4.js.map +0 -1
  127. package/dist/core/use-intl.esm5.js +0 -29
  128. package/dist/core/use-intl.esm5.js.map +0 -1
  129. package/dist/core/use-intl.esm6.js +0 -138
  130. package/dist/core/use-intl.esm6.js.map +0 -1
  131. package/dist/core/use-intl.esm7.js +0 -15
  132. package/dist/core/use-intl.esm7.js.map +0 -1
  133. package/dist/core/use-intl.esm8.js +0 -50
  134. package/dist/core/use-intl.esm8.js.map +0 -1
  135. package/dist/core/use-intl.esm9.js +0 -32
  136. package/dist/core/use-intl.esm9.js.map +0 -1
  137. package/dist/core/validateMessages.esm.js +0 -28
  138. package/dist/core/validateMessages.esm.js.map +0 -1
  139. package/dist/core.cjs.development.js +0 -93
  140. package/dist/core.cjs.development.js.map +0 -1
  141. package/dist/core.cjs.production.min.js +0 -2
  142. package/dist/core.cjs.production.min.js.map +0 -1
  143. package/dist/core.esm.js +0 -7
  144. package/dist/core.esm.js.map +0 -1
  145. package/dist/createIntl-8a2b87eb.js +0 -579
  146. package/dist/createIntl-8a2b87eb.js.map +0 -1
  147. package/dist/createIntl-f7979de1.js +0 -2
  148. package/dist/createIntl-f7979de1.js.map +0 -1
  149. package/dist/react/IntlContext.d.ts +0 -4
  150. package/dist/react/IntlContext.esm.js +0 -6
  151. package/dist/react/IntlContext.esm.js.map +0 -1
  152. package/dist/react/IntlProvider.esm.js +0 -16
  153. package/dist/react/IntlProvider.esm.js.map +0 -1
  154. package/dist/react/use-intl.esm.js +0 -26
  155. package/dist/react/use-intl.esm.js.map +0 -1
  156. package/dist/react/use-intl.esm10.js +0 -13
  157. package/dist/react/use-intl.esm10.js.map +0 -1
  158. package/dist/react/use-intl.esm11.js +0 -6
  159. package/dist/react/use-intl.esm11.js.map +0 -1
  160. package/dist/react/use-intl.esm2.js +0 -24
  161. package/dist/react/use-intl.esm2.js.map +0 -1
  162. package/dist/react/use-intl.esm3.js +0 -45
  163. package/dist/react/use-intl.esm3.js.map +0 -1
  164. package/dist/react/use-intl.esm4.js +0 -16
  165. package/dist/react/use-intl.esm4.js.map +0 -1
  166. package/dist/react/use-intl.esm5.js +0 -8
  167. package/dist/react/use-intl.esm5.js.map +0 -1
  168. package/dist/react/use-intl.esm6.js +0 -8
  169. package/dist/react/use-intl.esm6.js.map +0 -1
  170. package/dist/react/use-intl.esm7.js +0 -30
  171. package/dist/react/use-intl.esm7.js.map +0 -1
  172. package/dist/react/use-intl.esm8.js +0 -8
  173. package/dist/react/use-intl.esm8.js.map +0 -1
  174. package/dist/react/use-intl.esm9.js +0 -36
  175. package/dist/react/use-intl.esm9.js.map +0 -1
  176. package/dist/react/useFormatter.esm.js +0 -24
  177. package/dist/react/useFormatter.esm.js.map +0 -1
  178. package/dist/react/useIntl.esm.js +0 -30
  179. package/dist/react/useIntl.esm.js.map +0 -1
  180. package/dist/react/useIntlContext.d.ts +0 -1
  181. package/dist/react/useIntlContext.esm.js +0 -13
  182. package/dist/react/useIntlContext.esm.js.map +0 -1
  183. package/dist/react/useLocale.esm.js +0 -8
  184. package/dist/react/useLocale.esm.js.map +0 -1
  185. package/dist/react/useMessages.esm.js +0 -8
  186. package/dist/react/useMessages.esm.js.map +0 -1
  187. package/dist/react/useNow.esm.js +0 -45
  188. package/dist/react/useNow.esm.js.map +0 -1
  189. package/dist/react/useTimeZone.esm.js +0 -8
  190. package/dist/react/useTimeZone.esm.js.map +0 -1
  191. package/dist/react/useTranslations.esm.js +0 -26
  192. package/dist/react/useTranslations.esm.js.map +0 -1
  193. package/dist/react/useTranslationsImpl.esm.js +0 -36
  194. package/dist/react/useTranslationsImpl.esm.js.map +0 -1
  195. package/dist/react.cjs.development.js +0 -184
  196. package/dist/react.cjs.development.js.map +0 -1
  197. package/dist/react.cjs.production.min.js +0 -2
  198. package/dist/react.cjs.production.min.js.map +0 -1
  199. package/dist/react.esm.js +0 -9
  200. package/dist/react.esm.js.map +0 -1
  201. package/dist/use-intl.cjs.development.js +0 -823
  202. package/dist/use-intl.cjs.development.js.map +0 -1
  203. package/dist/use-intl.cjs.production.min.js +0 -2
  204. package/dist/use-intl.cjs.production.min.js.map +0 -1
  205. package/dist/use-intl.esm.js +0 -15
  206. package/dist/use-intl.esm.js.map +0 -1
@@ -0,0 +1 @@
1
+ export * from './dist/types/src/_IntlProvider';
@@ -0,0 +1 @@
1
+ export * from './dist/types/src/_useLocale';
package/core.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './dist/core';
1
+ export * from './dist/types/src/core';
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./production/_IntlProvider.js');
5
+ } else {
6
+ module.exports = require('./development/_IntlProvider.js');
7
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./production/_useLocale.js');
5
+ } else {
6
+ module.exports = require('./development/_useLocale.js');
7
+ }
package/dist/core.js CHANGED
@@ -1,8 +1,7 @@
1
-
2
- 'use strict'
1
+ 'use strict';
3
2
 
4
3
  if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./core.cjs.production.min.js')
4
+ module.exports = require('./production/core.js');
6
5
  } else {
7
- module.exports = require('./core.cjs.development.js')
6
+ module.exports = require('./development/core.js');
8
7
  }
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ const IntlContext = /*#__PURE__*/React.createContext(undefined);
6
+ var IntlContext$1 = IntlContext;
7
+
8
+ exports.IntlContext = IntlContext$1;
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var React = require('react');
6
+ var initializeConfig = require('./initializeConfig-cde42612.js');
7
+ var IntlContext = require('./IntlContext-b5cc6be8.js');
8
+
9
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
+
11
+ var React__default = /*#__PURE__*/_interopDefault(React);
12
+
13
+ function IntlProvider(_ref) {
14
+ let {
15
+ children,
16
+ ...config
17
+ } = _ref;
18
+ const [messageFormatCache] = React.useState(() => new Map());
19
+ return /*#__PURE__*/React__default.default.createElement(IntlContext.IntlContext.Provider, {
20
+ value: {
21
+ ...initializeConfig.initializeConfig(config),
22
+ messageFormatCache
23
+ }
24
+ }, children);
25
+ }
26
+
27
+ exports.IntlProvider = IntlProvider;
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var IntlContext = require('./IntlContext-b5cc6be8.js');
5
+
6
+ function useIntlContext() {
7
+ const context = React.useContext(IntlContext.IntlContext);
8
+ if (!context) {
9
+ throw new Error('No intl context found. Have you configured the provider?' );
10
+ }
11
+ return context;
12
+ }
13
+
14
+ function useLocale() {
15
+ return useIntlContext().locale;
16
+ }
17
+
18
+ exports.useIntlContext = useIntlContext;
19
+ exports.useLocale = useLocale;
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _useLocale = require('./_useLocale-321e619f.js');
6
+ require('react');
7
+ require('./IntlContext-b5cc6be8.js');
8
+
9
+
10
+
11
+ exports.useLocale = _useLocale.useLocale;
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var initializeConfig = require('./initializeConfig-cde42612.js');
6
+ var createIntl = require('./createIntl-dfba4462.js');
7
+ require('intl-messageformat');
8
+ require('react');
9
+
10
+ function createTranslatorImpl(_ref, namespacePrefix) {
11
+ let {
12
+ getMessageFallback,
13
+ messages,
14
+ namespace,
15
+ onError,
16
+ ...rest
17
+ } = _ref;
18
+ // The `namespacePrefix` is part of the type system.
19
+ // See the comment in the function invocation.
20
+ messages = messages[namespacePrefix];
21
+ namespace = createIntl.resolveNamespace(namespace, namespacePrefix);
22
+ const translator = createIntl.createBaseTranslator({
23
+ ...rest,
24
+ onError,
25
+ getMessageFallback,
26
+ messages,
27
+ namespace
28
+ });
29
+ const originalRich = translator.rich;
30
+ function base() {
31
+ return translator(...arguments);
32
+ }
33
+
34
+ // Augment `t.rich` to return plain strings
35
+ base.rich = (key, values, formats) => {
36
+ // `chunks` is returned as a string when no React element
37
+ // is used, therefore it's safe to cast this type.
38
+ const result = originalRich(key, values, formats);
39
+
40
+ // When only string chunks are provided to the parser, only strings should be returned here.
41
+ if (typeof result !== 'string') {
42
+ const error = new initializeConfig.IntlError(initializeConfig.IntlErrorCode.FORMATTING_ERROR, "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" );
43
+ onError(error);
44
+ return getMessageFallback({
45
+ error,
46
+ key,
47
+ namespace
48
+ });
49
+ }
50
+ return result;
51
+ };
52
+ base.raw = translator.raw;
53
+ return base;
54
+ }
55
+
56
+ /**
57
+ * Translates messages from the given namespace by using the ICU syntax.
58
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
59
+ *
60
+ * If no namespace is provided, all available messages are returned.
61
+ * The namespace can also indicate nesting by using a dot
62
+ * (e.g. `namespace.Component`).
63
+ */
64
+ function createTranslator(_ref) {
65
+ let {
66
+ getMessageFallback = initializeConfig.defaultGetMessageFallback,
67
+ messages,
68
+ namespace,
69
+ onError = initializeConfig.defaultOnError,
70
+ ...rest
71
+ } = _ref;
72
+ // We have to wrap the actual function so the type inference for the optional
73
+ // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
74
+ // The prefix ("!") is arbitrary.
75
+ return createTranslatorImpl({
76
+ ...rest,
77
+ onError,
78
+ getMessageFallback,
79
+ messages: {
80
+ '!': messages
81
+ },
82
+ namespace: namespace ? "!.".concat(namespace) : '!'
83
+ }, '!');
84
+ }
85
+
86
+ exports.IntlError = initializeConfig.IntlError;
87
+ exports.IntlErrorCode = initializeConfig.IntlErrorCode;
88
+ exports.initializeConfig = initializeConfig.initializeConfig;
89
+ exports.createBaseTranslator = createIntl.createBaseTranslator;
90
+ exports.createFormatter = createIntl.createFormatter;
91
+ exports.createIntl = createIntl.createIntl;
92
+ exports.createTranslator = createTranslator;
@@ -0,0 +1,421 @@
1
+ 'use strict';
2
+
3
+ var IntlMessageFormat = require('intl-messageformat');
4
+ var React = require('react');
5
+ var initializeConfig = require('./initializeConfig-cde42612.js');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
+
9
+ var IntlMessageFormat__default = /*#__PURE__*/_interopDefault(IntlMessageFormat);
10
+
11
+ // eslint-disable-next-line import/no-named-as-default -- False positive
12
+ function setTimeZoneInFormats(formats, timeZone) {
13
+ if (!formats) return formats;
14
+
15
+ // The only way to set a time zone with `intl-messageformat` is to merge it into the formats
16
+ // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15
17
+ return Object.keys(formats).reduce((acc, key) => {
18
+ acc[key] = {
19
+ timeZone,
20
+ ...formats[key]
21
+ };
22
+ return acc;
23
+ }, {});
24
+ }
25
+
26
+ /**
27
+ * `intl-messageformat` uses separate keys for `date` and `time`, but there's
28
+ * only one native API: `Intl.DateTimeFormat`. Additionally you might want to
29
+ * include both a time and a date in a value, therefore the separation doesn't
30
+ * seem so useful. We offer a single `dateTime` namespace instead, but we have
31
+ * to convert the format before `intl-messageformat` can be used.
32
+ */
33
+ function convertFormatsToIntlMessageFormat(formats, timeZone) {
34
+ const formatsWithTimeZone = timeZone ? {
35
+ ...formats,
36
+ dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)
37
+ } : formats;
38
+ const mfDateDefaults = IntlMessageFormat__default.default.formats.date;
39
+ const defaultDateFormats = timeZone ? setTimeZoneInFormats(mfDateDefaults, timeZone) : mfDateDefaults;
40
+ const mfTimeDefaults = IntlMessageFormat__default.default.formats.time;
41
+ const defaultTimeFormats = timeZone ? setTimeZoneInFormats(mfTimeDefaults, timeZone) : mfTimeDefaults;
42
+ return {
43
+ ...formatsWithTimeZone,
44
+ date: {
45
+ ...defaultDateFormats,
46
+ ...(formatsWithTimeZone === null || formatsWithTimeZone === void 0 ? void 0 : formatsWithTimeZone.dateTime)
47
+ },
48
+ time: {
49
+ ...defaultTimeFormats,
50
+ ...(formatsWithTimeZone === null || formatsWithTimeZone === void 0 ? void 0 : formatsWithTimeZone.dateTime)
51
+ }
52
+ };
53
+ }
54
+
55
+ // eslint-disable-next-line import/no-named-as-default -- False positive
56
+ function resolvePath(messages, key, namespace) {
57
+ if (!messages) {
58
+ throw new Error("No messages available at `".concat(namespace, "`.") );
59
+ }
60
+ let message = messages;
61
+ key.split('.').forEach(part => {
62
+ const next = message[part];
63
+ if (part == null || next == null) {
64
+ throw new Error("Could not resolve `".concat(key, "` in ").concat(namespace ? "`".concat(namespace, "`") : 'messages', ".") );
65
+ }
66
+ message = next;
67
+ });
68
+ return message;
69
+ }
70
+ function prepareTranslationValues(values) {
71
+ if (Object.keys(values).length === 0) return undefined;
72
+
73
+ // Workaround for https://github.com/formatjs/formatjs/issues/1467
74
+ const transformedValues = {};
75
+ Object.keys(values).forEach(key => {
76
+ let index = 0;
77
+ const value = values[key];
78
+ let transformed;
79
+ if (typeof value === 'function') {
80
+ transformed = chunks => {
81
+ const result = value(chunks);
82
+ return /*#__PURE__*/React.isValidElement(result) ? /*#__PURE__*/React.cloneElement(result, {
83
+ key: key + index++
84
+ }) : result;
85
+ };
86
+ } else {
87
+ transformed = value;
88
+ }
89
+ transformedValues[key] = transformed;
90
+ });
91
+ return transformedValues;
92
+ }
93
+ function getMessagesOrError(_ref) {
94
+ let {
95
+ messages,
96
+ namespace,
97
+ onError = initializeConfig.defaultOnError
98
+ } = _ref;
99
+ try {
100
+ if (!messages) {
101
+ throw new Error("No messages were configured on the provider." );
102
+ }
103
+ const retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
104
+ if (!retrievedMessages) {
105
+ throw new Error("No messages for namespace `".concat(namespace, "` found.") );
106
+ }
107
+ return retrievedMessages;
108
+ } catch (error) {
109
+ const intlError = new initializeConfig.IntlError(initializeConfig.IntlErrorCode.MISSING_MESSAGE, error.message);
110
+ onError(intlError);
111
+ return intlError;
112
+ }
113
+ }
114
+ function getPlainMessage(candidate, values) {
115
+ if (values) return undefined;
116
+ const unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
117
+
118
+ // Placeholders can be in the message if there are default values,
119
+ // or if the user has forgotten to provide values. In the latter
120
+ // case we need to compile the message to receive an error.
121
+ const hasPlaceholders = /<|{/.test(unescapedMessage);
122
+ if (!hasPlaceholders) {
123
+ return unescapedMessage;
124
+ }
125
+ return undefined;
126
+ }
127
+ function createBaseTranslator(config) {
128
+ const messagesOrError = getMessagesOrError({
129
+ messages: config.messages,
130
+ namespace: config.namespace,
131
+ onError: config.onError
132
+ });
133
+ return createBaseTranslatorImpl({
134
+ ...config,
135
+ messagesOrError
136
+ });
137
+ }
138
+ function createBaseTranslatorImpl(_ref2) {
139
+ let {
140
+ defaultTranslationValues,
141
+ formats: globalFormats,
142
+ getMessageFallback = initializeConfig.defaultGetMessageFallback,
143
+ locale,
144
+ messageFormatCache,
145
+ messagesOrError,
146
+ namespace,
147
+ onError,
148
+ timeZone
149
+ } = _ref2;
150
+ function getFallbackFromErrorAndNotify(key, code, message) {
151
+ const error = new initializeConfig.IntlError(code, message);
152
+ onError(error);
153
+ return getMessageFallback({
154
+ error,
155
+ key,
156
+ namespace
157
+ });
158
+ }
159
+ function translateBaseFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
160
+ key, /** Key value pairs for values to interpolate into the message. */
161
+ values, /** Provide custom formats for numbers, dates and times. */
162
+ formats) {
163
+ if (messagesOrError instanceof initializeConfig.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
+ let message;
173
+ try {
174
+ message = resolvePath(messages, key, namespace);
175
+ } catch (error) {
176
+ return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.MISSING_MESSAGE, error.message);
177
+ }
178
+ function joinPath(parts) {
179
+ return parts.filter(part => part != null).join('.');
180
+ }
181
+ const cacheKey = joinPath([locale, namespace, key, String(message)]);
182
+ let messageFormat;
183
+ if (messageFormatCache !== null && messageFormatCache !== void 0 && messageFormatCache.has(cacheKey)) {
184
+ messageFormat = messageFormatCache.get(cacheKey);
185
+ } else {
186
+ if (typeof message === 'object') {
187
+ let code, errorMessage;
188
+ if (Array.isArray(message)) {
189
+ code = initializeConfig.IntlErrorCode.INVALID_MESSAGE;
190
+ {
191
+ errorMessage = "Message at `".concat(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");
192
+ }
193
+ } else {
194
+ code = initializeConfig.IntlErrorCode.INSUFFICIENT_PATH;
195
+ {
196
+ errorMessage = "Message at `".concat(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");
197
+ }
198
+ }
199
+ return getFallbackFromErrorAndNotify(key, code, errorMessage);
200
+ }
201
+
202
+ // Hot path that avoids creating an `IntlMessageFormat` instance
203
+ const plainMessage = getPlainMessage(message, values);
204
+ if (plainMessage) return plainMessage;
205
+ try {
206
+ messageFormat = new IntlMessageFormat__default.default(message, locale, convertFormatsToIntlMessageFormat({
207
+ ...globalFormats,
208
+ ...formats
209
+ }, timeZone));
210
+ } catch (error) {
211
+ return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.INVALID_MESSAGE, error.message);
212
+ }
213
+ messageFormatCache === null || messageFormatCache === void 0 || messageFormatCache.set(cacheKey, messageFormat);
214
+ }
215
+ try {
216
+ const formattedMessage = messageFormat.format(
217
+ // @ts-expect-error `intl-messageformat` expects a different format
218
+ // for rich text elements since a recent minor update. This
219
+ // needs to be evaluated in detail, possibly also in regards
220
+ // to be able to format to parts.
221
+ prepareTranslationValues({
222
+ ...defaultTranslationValues,
223
+ ...values
224
+ }));
225
+ if (formattedMessage == null) {
226
+ throw new Error("Unable to format `".concat(key, "` in ").concat(namespace ? "namespace `".concat(namespace, "`") : 'messages') );
227
+ }
228
+
229
+ // Limit the function signature to return strings or React elements
230
+ return /*#__PURE__*/React.isValidElement(formattedMessage) ||
231
+ // Arrays of React elements
232
+ Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
233
+ } catch (error) {
234
+ return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.FORMATTING_ERROR, error.message);
235
+ }
236
+ }
237
+ function translateFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
238
+ key, /** Key value pairs for values to interpolate into the message. */
239
+ values, /** Provide custom formats for numbers, dates and times. */
240
+ formats) {
241
+ const result = translateBaseFn(key, values, formats);
242
+ if (typeof result !== 'string') {
243
+ return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.INVALID_MESSAGE, "The message `".concat(key, "` in ").concat(namespace ? "namespace `".concat(namespace, "`") : 'messages', " didn't resolve to a string. If you want to format rich text, use `t.rich` instead.") );
244
+ }
245
+ return result;
246
+ }
247
+ translateFn.rich = translateBaseFn;
248
+ translateFn.raw = key => {
249
+ if (messagesOrError instanceof initializeConfig.IntlError) {
250
+ // We have already warned about this during render
251
+ return getMessageFallback({
252
+ error: messagesOrError,
253
+ key,
254
+ namespace
255
+ });
256
+ }
257
+ const messages = messagesOrError;
258
+ try {
259
+ return resolvePath(messages, key, namespace);
260
+ } catch (error) {
261
+ return getFallbackFromErrorAndNotify(key, initializeConfig.IntlErrorCode.MISSING_MESSAGE, error.message);
262
+ }
263
+ };
264
+ return translateFn;
265
+ }
266
+
267
+ /**
268
+ * For the strictly typed messages to work we have to wrap the namespace into
269
+ * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
270
+ */
271
+ function resolveNamespace(namespace, namespacePrefix) {
272
+ return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
273
+ }
274
+
275
+ const MINUTE = 60;
276
+ const HOUR = MINUTE * 60;
277
+ const DAY = HOUR * 24;
278
+ const WEEK = DAY * 7;
279
+ const MONTH = DAY * (365 / 12); // Approximation
280
+ const YEAR = DAY * 365;
281
+ function getRelativeTimeFormatConfig(seconds) {
282
+ const absValue = Math.abs(seconds);
283
+ let value, unit;
284
+
285
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
286
+ // will include fractions like '2.1 hours ago'.
287
+
288
+ if (absValue < MINUTE) {
289
+ unit = 'second';
290
+ value = Math.round(seconds);
291
+ } else if (absValue < HOUR) {
292
+ unit = 'minute';
293
+ value = Math.round(seconds / MINUTE);
294
+ } else if (absValue < DAY) {
295
+ unit = 'hour';
296
+ value = Math.round(seconds / HOUR);
297
+ } else if (absValue < WEEK) {
298
+ unit = 'day';
299
+ value = Math.round(seconds / DAY);
300
+ } else if (absValue < MONTH) {
301
+ unit = 'week';
302
+ value = Math.round(seconds / WEEK);
303
+ } else if (absValue < YEAR) {
304
+ unit = 'month';
305
+ value = Math.round(seconds / MONTH);
306
+ } else {
307
+ unit = 'year';
308
+ value = Math.round(seconds / YEAR);
309
+ }
310
+ return {
311
+ value,
312
+ unit
313
+ };
314
+ }
315
+ function createFormatter(_ref) {
316
+ let {
317
+ formats,
318
+ locale,
319
+ now: globalNow,
320
+ onError = initializeConfig.defaultOnError,
321
+ timeZone
322
+ } = _ref;
323
+ function resolveFormatOrOptions(typeFormats, formatOrOptions) {
324
+ let options;
325
+ if (typeof formatOrOptions === 'string') {
326
+ const formatName = formatOrOptions;
327
+ options = typeFormats === null || typeFormats === void 0 ? void 0 : typeFormats[formatName];
328
+ if (!options) {
329
+ const error = new initializeConfig.IntlError(initializeConfig.IntlErrorCode.MISSING_FORMAT, "Format `".concat(formatName, "` is not available. You can configure it on the provider or provide custom options.") );
330
+ onError(error);
331
+ throw error;
332
+ }
333
+ } else {
334
+ options = formatOrOptions;
335
+ }
336
+ return options;
337
+ }
338
+ function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
339
+ let options;
340
+ try {
341
+ options = resolveFormatOrOptions(typeFormats, formatOrOptions);
342
+ } catch (error) {
343
+ return String(value);
344
+ }
345
+ try {
346
+ return formatter(options);
347
+ } catch (error) {
348
+ onError(new initializeConfig.IntlError(initializeConfig.IntlErrorCode.FORMATTING_ERROR, error.message));
349
+ return String(value);
350
+ }
351
+ }
352
+ function dateTime( /** If a number is supplied, this is interpreted as a UTC timestamp. */
353
+ value,
354
+ /** If a time zone is supplied, the `value` is converted to that time zone.
355
+ * Otherwise the user time zone will be used. */
356
+ formatOrOptions) {
357
+ return getFormattedValue(value, formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.dateTime, options => {
358
+ var _options;
359
+ if (timeZone && !((_options = options) !== null && _options !== void 0 && _options.timeZone)) {
360
+ options = {
361
+ ...options,
362
+ timeZone
363
+ };
364
+ }
365
+ return new Intl.DateTimeFormat(locale, options).format(value);
366
+ });
367
+ }
368
+ function number(value, formatOrOptions) {
369
+ return getFormattedValue(value, formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.number, options => new Intl.NumberFormat(locale, options).format(value));
370
+ }
371
+ function relativeTime( /** The date time that needs to be formatted. */
372
+ date, /** The reference point in time to which `date` will be formatted in relation to. */
373
+ now) {
374
+ try {
375
+ if (!now) {
376
+ if (globalNow) {
377
+ now = globalNow;
378
+ } else {
379
+ throw new Error("The `now` parameter wasn't provided and there was no global fallback configured on the provider." );
380
+ }
381
+ }
382
+ const dateDate = date instanceof Date ? date : new Date(date);
383
+ const nowDate = now instanceof Date ? now : new Date(now);
384
+ const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
385
+ const {
386
+ unit,
387
+ value
388
+ } = getRelativeTimeFormatConfig(seconds);
389
+ return new Intl.RelativeTimeFormat(locale, {
390
+ numeric: 'auto'
391
+ }).format(value, unit);
392
+ } catch (error) {
393
+ onError(new initializeConfig.IntlError(initializeConfig.IntlErrorCode.FORMATTING_ERROR, error.message));
394
+ return String(date);
395
+ }
396
+ }
397
+ function list(value, formatOrOptions) {
398
+ return getFormattedValue(value, formatOrOptions, formats === null || formats === void 0 ? void 0 : formats.list, options => new Intl.ListFormat(locale, options).format(value));
399
+ }
400
+ return {
401
+ dateTime,
402
+ number,
403
+ relativeTime,
404
+ list
405
+ };
406
+ }
407
+
408
+ /** @deprecated Switch to `createFormatter` */
409
+ function createIntl() {
410
+ const formatter = createFormatter(...arguments);
411
+ return {
412
+ formatDateTime: formatter.dateTime,
413
+ formatNumber: formatter.number,
414
+ formatRelativeTime: formatter.relativeTime
415
+ };
416
+ }
417
+
418
+ exports.createBaseTranslator = createBaseTranslator;
419
+ exports.createFormatter = createFormatter;
420
+ exports.createIntl = createIntl;
421
+ exports.resolveNamespace = resolveNamespace;
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var initializeConfig = require('./initializeConfig-cde42612.js');
6
+ var core = require('./core.js');
7
+ var createIntl = require('./createIntl-dfba4462.js');
8
+ var _IntlProvider = require('./_IntlProvider.js');
9
+ var react = require('./react.js');
10
+ var _useLocale = require('./_useLocale-321e619f.js');
11
+ require('intl-messageformat');
12
+ require('react');
13
+ require('./IntlContext-b5cc6be8.js');
14
+
15
+
16
+
17
+ exports.IntlError = initializeConfig.IntlError;
18
+ exports.IntlErrorCode = initializeConfig.IntlErrorCode;
19
+ exports.initializeConfig = initializeConfig.initializeConfig;
20
+ exports.createTranslator = core.createTranslator;
21
+ exports.createBaseTranslator = createIntl.createBaseTranslator;
22
+ exports.createFormatter = createIntl.createFormatter;
23
+ exports.createIntl = createIntl.createIntl;
24
+ exports.IntlProvider = _IntlProvider.IntlProvider;
25
+ exports.useFormatter = react.useFormatter;
26
+ exports.useIntl = react.useIntl;
27
+ exports.useMessages = react.useMessages;
28
+ exports.useNow = react.useNow;
29
+ exports.useTimeZone = react.useTimeZone;
30
+ exports.useTranslations = react.useTranslations;
31
+ exports.useLocale = _useLocale.useLocale;