use-intl 3.0.0-beta.2 → 3.0.0-rc.5

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 (204) 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 +91 -0
  12. package/dist/development/createFormatter-bcc5a49f.js +430 -0
  13. package/dist/development/index.js +29 -0
  14. package/dist/development/initializeConfig-0a0b87a7.js +120 -0
  15. package/dist/development/react.js +145 -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/createFormatter-cccbd794.js +1 -0
  22. package/dist/esm/index.js +1 -0
  23. package/dist/esm/initializeConfig-29e7ba4c.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/createFormatter-8c0b8eb2.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 +14 -13
  43. package/dist/{core → types/src/core}/MessageFormatCache.d.ts +5 -5
  44. package/dist/{core → types/src/core}/NumberFormatOptions.d.ts +2 -2
  45. package/dist/types/src/core/RelativeTimeFormatOptions.d.ts +5 -0
  46. package/dist/{core → types/src/core}/TimeZone.d.ts +2 -2
  47. package/dist/{core → types/src/core}/TranslationValues.d.ts +6 -6
  48. package/dist/{core → types/src/core}/convertFormatsToIntlMessageFormat.d.ts +11 -11
  49. package/dist/{core → types/src/core}/createBaseTranslator.d.ts +21 -21
  50. package/dist/{core → types/src/core}/createFormatter.d.ts +20 -19
  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 +15 -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/{react → types/src/react}/IntlContext.d.ts +13 -13
  65. package/dist/{react → types/src/react}/IntlProvider.d.ts +7 -7
  66. package/dist/{react → types/src/react}/index.d.ts +7 -8
  67. package/dist/{react → types/src/react}/useFormatter.d.ts +6 -6
  68. package/dist/{react → types/src/react}/useIntlContext.d.ts +10 -10
  69. package/dist/{react → types/src/react}/useLocale.d.ts +1 -1
  70. package/dist/types/src/react/useMessages.d.ts +2 -0
  71. package/dist/{react → types/src/react}/useNow.d.ts +23 -23
  72. package/dist/{react → types/src/react}/useTimeZone.d.ts +1 -1
  73. package/dist/{react → types/src/react}/useTranslations.d.ts +44 -44
  74. package/dist/{react → types/src/react}/useTranslationsImpl.d.ts +8 -8
  75. package/dist/{react.d.ts → types/src/react.d.ts} +1 -1
  76. package/dist/types/test/core/createFormatter.test.d.ts +1 -0
  77. package/dist/types/test/core/createTranslator.test.d.ts +1 -0
  78. package/dist/types/test/react/useFormatter.test.d.ts +1 -0
  79. package/dist/types/test/react/useLocale.test.d.ts +1 -0
  80. package/dist/types/test/react/useMessages.test.d.ts +1 -0
  81. package/dist/types/test/react/useNow.test.d.ts +1 -0
  82. package/dist/types/test/react/useTimeZone.test.d.ts +1 -0
  83. package/dist/types/test/react/useTranslations.test.d.ts +1 -0
  84. package/dist/{test → types/test}/setup.d.ts +1 -1
  85. package/package.json +33 -21
  86. package/react.d.ts +1 -1
  87. package/dist/_virtual/_rollupPluginBabelHelpers.esm.js +0 -102
  88. package/dist/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
  89. package/dist/_virtual/use-intl.esm.js +0 -102
  90. package/dist/_virtual/use-intl.esm.js.map +0 -1
  91. package/dist/core/IntlError.esm.js +0 -33
  92. package/dist/core/IntlError.esm.js.map +0 -1
  93. package/dist/core/convertFormatsToIntlMessageFormat.esm.js +0 -37
  94. package/dist/core/convertFormatsToIntlMessageFormat.esm.js.map +0 -1
  95. package/dist/core/createBaseTranslator.esm.js +0 -210
  96. package/dist/core/createBaseTranslator.esm.js.map +0 -1
  97. package/dist/core/createFormatter.esm.js +0 -138
  98. package/dist/core/createFormatter.esm.js.map +0 -1
  99. package/dist/core/createIntl.d.ts +0 -7
  100. package/dist/core/createIntl.esm.js +0 -14
  101. package/dist/core/createIntl.esm.js.map +0 -1
  102. package/dist/core/createTranslator.esm.js +0 -37
  103. package/dist/core/createTranslator.esm.js.map +0 -1
  104. package/dist/core/createTranslatorImpl.esm.js +0 -50
  105. package/dist/core/createTranslatorImpl.esm.js.map +0 -1
  106. package/dist/core/defaults.esm.js +0 -15
  107. package/dist/core/defaults.esm.js.map +0 -1
  108. package/dist/core/initializeConfig.esm.js +0 -29
  109. package/dist/core/initializeConfig.esm.js.map +0 -1
  110. package/dist/core/resolveNamespace.esm.js +0 -10
  111. package/dist/core/resolveNamespace.esm.js.map +0 -1
  112. package/dist/core/use-intl.esm.js +0 -33
  113. package/dist/core/use-intl.esm.js.map +0 -1
  114. package/dist/core/use-intl.esm10.js +0 -28
  115. package/dist/core/use-intl.esm10.js.map +0 -1
  116. package/dist/core/use-intl.esm11.js +0 -10
  117. package/dist/core/use-intl.esm11.js.map +0 -1
  118. package/dist/core/use-intl.esm2.js +0 -210
  119. package/dist/core/use-intl.esm2.js.map +0 -1
  120. package/dist/core/use-intl.esm3.js +0 -37
  121. package/dist/core/use-intl.esm3.js.map +0 -1
  122. package/dist/core/use-intl.esm4.js +0 -14
  123. package/dist/core/use-intl.esm4.js.map +0 -1
  124. package/dist/core/use-intl.esm5.js +0 -29
  125. package/dist/core/use-intl.esm5.js.map +0 -1
  126. package/dist/core/use-intl.esm6.js +0 -138
  127. package/dist/core/use-intl.esm6.js.map +0 -1
  128. package/dist/core/use-intl.esm7.js +0 -37
  129. package/dist/core/use-intl.esm7.js.map +0 -1
  130. package/dist/core/use-intl.esm8.js +0 -15
  131. package/dist/core/use-intl.esm8.js.map +0 -1
  132. package/dist/core/use-intl.esm9.js +0 -50
  133. package/dist/core/use-intl.esm9.js.map +0 -1
  134. package/dist/core/validateMessages.esm.js +0 -28
  135. package/dist/core/validateMessages.esm.js.map +0 -1
  136. package/dist/core.cjs.development.js +0 -93
  137. package/dist/core.cjs.development.js.map +0 -1
  138. package/dist/core.cjs.production.min.js +0 -2
  139. package/dist/core.cjs.production.min.js.map +0 -1
  140. package/dist/core.esm.js +0 -7
  141. package/dist/core.esm.js.map +0 -1
  142. package/dist/createIntl-34d4281e.js +0 -592
  143. package/dist/createIntl-34d4281e.js.map +0 -1
  144. package/dist/createIntl-bc325740.js +0 -2
  145. package/dist/createIntl-bc325740.js.map +0 -1
  146. package/dist/react/IntlContext.esm.js +0 -6
  147. package/dist/react/IntlContext.esm.js.map +0 -1
  148. package/dist/react/IntlProvider.esm.js +0 -22
  149. package/dist/react/IntlProvider.esm.js.map +0 -1
  150. package/dist/react/getInitializedConfig.d.ts +0 -1
  151. package/dist/react/use-intl.esm.js +0 -26
  152. package/dist/react/use-intl.esm.js.map +0 -1
  153. package/dist/react/use-intl.esm10.js +0 -13
  154. package/dist/react/use-intl.esm10.js.map +0 -1
  155. package/dist/react/use-intl.esm11.js +0 -6
  156. package/dist/react/use-intl.esm11.js.map +0 -1
  157. package/dist/react/use-intl.esm2.js +0 -8
  158. package/dist/react/use-intl.esm2.js.map +0 -1
  159. package/dist/react/use-intl.esm3.js +0 -30
  160. package/dist/react/use-intl.esm3.js.map +0 -1
  161. package/dist/react/use-intl.esm4.js +0 -22
  162. package/dist/react/use-intl.esm4.js.map +0 -1
  163. package/dist/react/use-intl.esm5.js +0 -24
  164. package/dist/react/use-intl.esm5.js.map +0 -1
  165. package/dist/react/use-intl.esm6.js +0 -45
  166. package/dist/react/use-intl.esm6.js.map +0 -1
  167. package/dist/react/use-intl.esm7.js +0 -8
  168. package/dist/react/use-intl.esm7.js.map +0 -1
  169. package/dist/react/use-intl.esm8.js +0 -8
  170. package/dist/react/use-intl.esm8.js.map +0 -1
  171. package/dist/react/use-intl.esm9.js +0 -36
  172. package/dist/react/use-intl.esm9.js.map +0 -1
  173. package/dist/react/useFormatter.esm.js +0 -24
  174. package/dist/react/useFormatter.esm.js.map +0 -1
  175. package/dist/react/useIntl.d.ts +0 -6
  176. package/dist/react/useIntl.esm.js +0 -30
  177. package/dist/react/useIntl.esm.js.map +0 -1
  178. package/dist/react/useIntlContext.esm.js +0 -13
  179. package/dist/react/useIntlContext.esm.js.map +0 -1
  180. package/dist/react/useLocale.esm.js +0 -8
  181. package/dist/react/useLocale.esm.js.map +0 -1
  182. package/dist/react/useMessages.d.ts +0 -1
  183. package/dist/react/useMessages.esm.js +0 -8
  184. package/dist/react/useMessages.esm.js.map +0 -1
  185. package/dist/react/useNow.esm.js +0 -45
  186. package/dist/react/useNow.esm.js.map +0 -1
  187. package/dist/react/useTimeZone.esm.js +0 -8
  188. package/dist/react/useTimeZone.esm.js.map +0 -1
  189. package/dist/react/useTranslations.esm.js +0 -26
  190. package/dist/react/useTranslations.esm.js.map +0 -1
  191. package/dist/react/useTranslationsImpl.esm.js +0 -36
  192. package/dist/react/useTranslationsImpl.esm.js.map +0 -1
  193. package/dist/react.cjs.development.js +0 -190
  194. package/dist/react.cjs.development.js.map +0 -1
  195. package/dist/react.cjs.production.min.js +0 -2
  196. package/dist/react.cjs.production.min.js.map +0 -1
  197. package/dist/react.esm.js +0 -9
  198. package/dist/react.esm.js.map +0 -1
  199. package/dist/use-intl.cjs.development.js +0 -842
  200. package/dist/use-intl.cjs.development.js.map +0 -1
  201. package/dist/use-intl.cjs.production.min.js +0 -2
  202. package/dist/use-intl.cjs.production.min.js.map +0 -1
  203. package/dist/use-intl.esm.js +0 -15
  204. package/dist/use-intl.esm.js.map +0 -1
@@ -1,592 +0,0 @@
1
- 'use strict';
2
-
3
- var IntlMessageFormat = require('intl-messageformat');
4
- var React = require('react');
5
-
6
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
-
8
- var IntlMessageFormat__default = /*#__PURE__*/_interopDefaultLegacy(IntlMessageFormat);
9
-
10
- function _extends() {
11
- _extends = Object.assign ? Object.assign.bind() : function (target) {
12
- for (var i = 1; i < arguments.length; i++) {
13
- var source = arguments[i];
14
- for (var key in source) {
15
- if (Object.prototype.hasOwnProperty.call(source, key)) {
16
- target[key] = source[key];
17
- }
18
- }
19
- }
20
- return target;
21
- };
22
- return _extends.apply(this, arguments);
23
- }
24
- function _inheritsLoose(subClass, superClass) {
25
- subClass.prototype = Object.create(superClass.prototype);
26
- subClass.prototype.constructor = subClass;
27
- _setPrototypeOf(subClass, superClass);
28
- }
29
- function _getPrototypeOf(o) {
30
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
31
- return o.__proto__ || Object.getPrototypeOf(o);
32
- };
33
- return _getPrototypeOf(o);
34
- }
35
- function _setPrototypeOf(o, p) {
36
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
37
- o.__proto__ = p;
38
- return o;
39
- };
40
- return _setPrototypeOf(o, p);
41
- }
42
- function _isNativeReflectConstruct() {
43
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
44
- if (Reflect.construct.sham) return false;
45
- if (typeof Proxy === "function") return true;
46
- try {
47
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
48
- return true;
49
- } catch (e) {
50
- return false;
51
- }
52
- }
53
- function _construct(Parent, args, Class) {
54
- if (_isNativeReflectConstruct()) {
55
- _construct = Reflect.construct.bind();
56
- } else {
57
- _construct = function _construct(Parent, args, Class) {
58
- var a = [null];
59
- a.push.apply(a, args);
60
- var Constructor = Function.bind.apply(Parent, a);
61
- var instance = new Constructor();
62
- if (Class) _setPrototypeOf(instance, Class.prototype);
63
- return instance;
64
- };
65
- }
66
- return _construct.apply(null, arguments);
67
- }
68
- function _isNativeFunction(fn) {
69
- return Function.toString.call(fn).indexOf("[native code]") !== -1;
70
- }
71
- function _wrapNativeSuper(Class) {
72
- var _cache = typeof Map === "function" ? new Map() : undefined;
73
- _wrapNativeSuper = function _wrapNativeSuper(Class) {
74
- if (Class === null || !_isNativeFunction(Class)) return Class;
75
- if (typeof Class !== "function") {
76
- throw new TypeError("Super expression must either be null or a function");
77
- }
78
- if (typeof _cache !== "undefined") {
79
- if (_cache.has(Class)) return _cache.get(Class);
80
- _cache.set(Class, Wrapper);
81
- }
82
- function Wrapper() {
83
- return _construct(Class, arguments, _getPrototypeOf(this).constructor);
84
- }
85
- Wrapper.prototype = Object.create(Class.prototype, {
86
- constructor: {
87
- value: Wrapper,
88
- enumerable: false,
89
- writable: true,
90
- configurable: true
91
- }
92
- });
93
- return _setPrototypeOf(Wrapper, Class);
94
- };
95
- return _wrapNativeSuper(Class);
96
- }
97
- function _objectWithoutPropertiesLoose(source, excluded) {
98
- if (source == null) return {};
99
- var target = {};
100
- var sourceKeys = Object.keys(source);
101
- var key, i;
102
- for (i = 0; i < sourceKeys.length; i++) {
103
- key = sourceKeys[i];
104
- if (excluded.indexOf(key) >= 0) continue;
105
- target[key] = source[key];
106
- }
107
- return target;
108
- }
109
-
110
- exports.IntlErrorCode = void 0;
111
- (function (IntlErrorCode) {
112
- IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
113
- IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
114
- IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
115
- IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
116
- IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
117
- IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
118
- })(exports.IntlErrorCode || (exports.IntlErrorCode = {}));
119
- var IntlError = /*#__PURE__*/function (_Error) {
120
- _inheritsLoose(IntlError, _Error);
121
- function IntlError(code, originalMessage) {
122
- var _this;
123
- var message = code;
124
- if (originalMessage) {
125
- message += ': ' + originalMessage;
126
- }
127
- _this = _Error.call(this, message) || this;
128
- _this.code = void 0;
129
- _this.originalMessage = void 0;
130
- _this.code = code;
131
- if (originalMessage) {
132
- _this.originalMessage = originalMessage;
133
- }
134
- return _this;
135
- }
136
- return IntlError;
137
- }( /*#__PURE__*/_wrapNativeSuper(Error));
138
-
139
- function setTimeZoneInFormats(formats, timeZone) {
140
- if (!formats) return formats;
141
- // The only way to set a time zone with `intl-messageformat` is to merge it into the formats
142
- // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15
143
- return Object.keys(formats).reduce(function (acc, key) {
144
- acc[key] = _extends({
145
- timeZone: timeZone
146
- }, formats[key]);
147
- return acc;
148
- }, {});
149
- }
150
- /**
151
- * `intl-messageformat` uses separate keys for `date` and `time`, but there's
152
- * only one native API: `Intl.DateTimeFormat`. Additionally you might want to
153
- * include both a time and a date in a value, therefore the separation doesn't
154
- * seem so useful. We offer a single `dateTime` namespace instead, but we have
155
- * to convert the format before `intl-messageformat` can be used.
156
- */
157
- function convertFormatsToIntlMessageFormat(formats, timeZone) {
158
- var formatsWithTimeZone = timeZone ? _extends({}, formats, {
159
- dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)
160
- }) : formats;
161
- var mfDateDefaults = IntlMessageFormat__default["default"].formats.date;
162
- var defaultDateFormats = timeZone ? setTimeZoneInFormats(mfDateDefaults, timeZone) : mfDateDefaults;
163
- var mfTimeDefaults = IntlMessageFormat__default["default"].formats.time;
164
- var defaultTimeFormats = timeZone ? setTimeZoneInFormats(mfTimeDefaults, timeZone) : mfTimeDefaults;
165
- return _extends({}, formatsWithTimeZone, {
166
- date: _extends({}, defaultDateFormats, formatsWithTimeZone == null ? void 0 : formatsWithTimeZone.dateTime),
167
- time: _extends({}, defaultTimeFormats, formatsWithTimeZone == null ? void 0 : formatsWithTimeZone.dateTime)
168
- });
169
- }
170
-
171
- /**
172
- * Contains defaults that are used for all entry points into the core.
173
- * See also `InitializedIntlConfiguration`.
174
- */
175
- function defaultGetMessageFallback(props) {
176
- return [props.namespace, props.key].filter(function (part) {
177
- return part != null;
178
- }).join('.');
179
- }
180
- function defaultOnError(error) {
181
- console.error(error);
182
- }
183
-
184
- function resolvePath(messages, key, namespace) {
185
- if (!messages) {
186
- throw new Error("No messages available at `" + namespace + "`." );
187
- }
188
- var message = messages;
189
- key.split('.').forEach(function (part) {
190
- var next = message[part];
191
- if (part == null || next == null) {
192
- throw new Error("Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." );
193
- }
194
- message = next;
195
- });
196
- return message;
197
- }
198
- function prepareTranslationValues(values) {
199
- if (Object.keys(values).length === 0) return undefined;
200
- // Workaround for https://github.com/formatjs/formatjs/issues/1467
201
- var transformedValues = {};
202
- Object.keys(values).forEach(function (key) {
203
- var index = 0;
204
- var value = values[key];
205
- var transformed;
206
- if (typeof value === 'function') {
207
- transformed = function transformed(chunks) {
208
- var result = value(chunks);
209
- return React.isValidElement(result) ? React.cloneElement(result, {
210
- key: key + index++
211
- }) : result;
212
- };
213
- } else {
214
- transformed = value;
215
- }
216
- transformedValues[key] = transformed;
217
- });
218
- return transformedValues;
219
- }
220
- function getMessagesOrError(_ref) {
221
- var messages = _ref.messages,
222
- namespace = _ref.namespace,
223
- _ref$onError = _ref.onError,
224
- onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
225
- try {
226
- if (!messages) {
227
- throw new Error("development" !== 'production' ? "No messages were configured on the provider." : undefined);
228
- }
229
- var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
230
- if (!retrievedMessages) {
231
- throw new Error("development" !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
232
- }
233
- return retrievedMessages;
234
- } catch (error) {
235
- var intlError = new IntlError(exports.IntlErrorCode.MISSING_MESSAGE, error.message);
236
- onError(intlError);
237
- return intlError;
238
- }
239
- }
240
- function getPlainMessage(candidate, values) {
241
- if (values) return undefined;
242
- var unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
243
- // Placeholders can be in the message if there are default values,
244
- // or if the user has forgotten to provide values. In the latter
245
- // case we need to compile the message to receive an error.
246
- var hasPlaceholders = /<|{/.test(unescapedMessage);
247
- if (!hasPlaceholders) {
248
- return unescapedMessage;
249
- }
250
- return undefined;
251
- }
252
- function createBaseTranslator(config) {
253
- var messagesOrError = getMessagesOrError({
254
- messages: config.messages,
255
- namespace: config.namespace,
256
- onError: config.onError
257
- });
258
- return createBaseTranslatorImpl(_extends({}, config, {
259
- messagesOrError: messagesOrError
260
- }));
261
- }
262
- function createBaseTranslatorImpl(_ref2) {
263
- var defaultTranslationValues = _ref2.defaultTranslationValues,
264
- globalFormats = _ref2.formats,
265
- _ref2$getMessageFallb = _ref2.getMessageFallback,
266
- getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
267
- locale = _ref2.locale,
268
- messageFormatCache = _ref2.messageFormatCache,
269
- messagesOrError = _ref2.messagesOrError,
270
- namespace = _ref2.namespace,
271
- onError = _ref2.onError,
272
- timeZone = _ref2.timeZone;
273
- function getFallbackFromErrorAndNotify(key, code, message) {
274
- var error = new IntlError(code, message);
275
- onError(error);
276
- return getMessageFallback({
277
- error: error,
278
- key: key,
279
- namespace: namespace
280
- });
281
- }
282
- function translateBaseFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
283
- key, /** Key value pairs for values to interpolate into the message. */
284
- values, /** Provide custom formats for numbers, dates and times. */
285
- formats) {
286
- if (messagesOrError instanceof IntlError) {
287
- // We have already warned about this during render
288
- return getMessageFallback({
289
- error: messagesOrError,
290
- key: key,
291
- namespace: namespace
292
- });
293
- }
294
- var messages = messagesOrError;
295
- var message;
296
- try {
297
- message = resolvePath(messages, key, namespace);
298
- } catch (error) {
299
- return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
300
- }
301
- function joinPath(parts) {
302
- return parts.filter(function (part) {
303
- return part != null;
304
- }).join('.');
305
- }
306
- var cacheKey = joinPath([locale, namespace, key, String(message)]);
307
- var messageFormat;
308
- if (messageFormatCache != null && messageFormatCache.has(cacheKey)) {
309
- messageFormat = messageFormatCache.get(cacheKey);
310
- } else {
311
- if (typeof message === 'object') {
312
- var code, errorMessage;
313
- if (Array.isArray(message)) {
314
- code = exports.IntlErrorCode.INVALID_MESSAGE;
315
- {
316
- 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";
317
- }
318
- } else {
319
- code = exports.IntlErrorCode.INSUFFICIENT_PATH;
320
- {
321
- 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";
322
- }
323
- }
324
- return getFallbackFromErrorAndNotify(key, code, errorMessage);
325
- }
326
- // Hot path that avoids creating an `IntlMessageFormat` instance
327
- var plainMessage = getPlainMessage(message, values);
328
- if (plainMessage) return plainMessage;
329
- try {
330
- messageFormat = new IntlMessageFormat__default["default"](message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
331
- } catch (error) {
332
- return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, error.message);
333
- }
334
- messageFormatCache == null ? void 0 : messageFormatCache.set(cacheKey, messageFormat);
335
- }
336
- try {
337
- var formattedMessage = messageFormat.format(
338
- // @ts-ignore `intl-messageformat` expects a different format
339
- // for rich text elements since a recent minor update. This
340
- // needs to be evaluated in detail, possibly also in regards
341
- // to be able to format to parts.
342
- prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
343
- if (formattedMessage == null) {
344
- throw new Error("development" !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
345
- }
346
- // Limit the function signature to return strings or React elements
347
- return React.isValidElement(formattedMessage) ||
348
- // Arrays of React elements
349
- Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
350
- } catch (error) {
351
- return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.FORMATTING_ERROR, error.message);
352
- }
353
- }
354
- function translateFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
355
- key, /** Key value pairs for values to interpolate into the message. */
356
- values, /** Provide custom formats for numbers, dates and times. */
357
- formats) {
358
- var result = translateBaseFn(key, values, formats);
359
- if (typeof result !== 'string') {
360
- return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, "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." );
361
- }
362
- return result;
363
- }
364
- translateFn.rich = translateBaseFn;
365
- translateFn.raw = function ( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
366
- key) {
367
- if (messagesOrError instanceof IntlError) {
368
- // We have already warned about this during render
369
- return getMessageFallback({
370
- error: messagesOrError,
371
- key: key,
372
- namespace: namespace
373
- });
374
- }
375
- var messages = messagesOrError;
376
- try {
377
- return resolvePath(messages, key, namespace);
378
- } catch (error) {
379
- return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.MISSING_MESSAGE, error.message);
380
- }
381
- };
382
- return translateFn;
383
- }
384
-
385
- /**
386
- * For the strictly typed messages to work we have to wrap the namespace into
387
- * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
388
- */
389
- function resolveNamespace(namespace, namespacePrefix) {
390
- return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
391
- }
392
-
393
- var MINUTE = 60;
394
- var HOUR = MINUTE * 60;
395
- var DAY = HOUR * 24;
396
- var WEEK = DAY * 7;
397
- var MONTH = DAY * (365 / 12); // Approximation
398
- var YEAR = DAY * 365;
399
- function getRelativeTimeFormatConfig(seconds) {
400
- var absValue = Math.abs(seconds);
401
- var value, unit;
402
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
403
- // will include fractions like '2.1 hours ago'.
404
- if (absValue < MINUTE) {
405
- unit = 'second';
406
- value = Math.round(seconds);
407
- } else if (absValue < HOUR) {
408
- unit = 'minute';
409
- value = Math.round(seconds / MINUTE);
410
- } else if (absValue < DAY) {
411
- unit = 'hour';
412
- value = Math.round(seconds / HOUR);
413
- } else if (absValue < WEEK) {
414
- unit = 'day';
415
- value = Math.round(seconds / DAY);
416
- } else if (absValue < MONTH) {
417
- unit = 'week';
418
- value = Math.round(seconds / WEEK);
419
- } else if (absValue < YEAR) {
420
- unit = 'month';
421
- value = Math.round(seconds / MONTH);
422
- } else {
423
- unit = 'year';
424
- value = Math.round(seconds / YEAR);
425
- }
426
- return {
427
- value: value,
428
- unit: unit
429
- };
430
- }
431
- function createFormatter(_ref) {
432
- var formats = _ref.formats,
433
- locale = _ref.locale,
434
- globalNow = _ref.now,
435
- _ref$onError = _ref.onError,
436
- onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
437
- timeZone = _ref.timeZone;
438
- function resolveFormatOrOptions(typeFormats, formatOrOptions) {
439
- var options;
440
- if (typeof formatOrOptions === 'string') {
441
- var formatName = formatOrOptions;
442
- options = typeFormats == null ? void 0 : typeFormats[formatName];
443
- if (!options) {
444
- var error = new IntlError(exports.IntlErrorCode.MISSING_FORMAT, "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." );
445
- onError(error);
446
- throw error;
447
- }
448
- } else {
449
- options = formatOrOptions;
450
- }
451
- return options;
452
- }
453
- function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
454
- var options;
455
- try {
456
- options = resolveFormatOrOptions(typeFormats, formatOrOptions);
457
- } catch (error) {
458
- return String(value);
459
- }
460
- try {
461
- return formatter(options);
462
- } catch (error) {
463
- onError(new IntlError(exports.IntlErrorCode.FORMATTING_ERROR, error.message));
464
- return String(value);
465
- }
466
- }
467
- function dateTime( /** If a number is supplied, this is interpreted as a UTC timestamp. */
468
- value,
469
- /** If a time zone is supplied, the `value` is converted to that time zone.
470
- * Otherwise the user time zone will be used. */
471
- formatOrOptions) {
472
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
473
- var _options;
474
- if (timeZone && !((_options = options) != null && _options.timeZone)) {
475
- options = _extends({}, options, {
476
- timeZone: timeZone
477
- });
478
- }
479
- return new Intl.DateTimeFormat(locale, options).format(value);
480
- });
481
- }
482
- function number(value, formatOrOptions) {
483
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
484
- return new Intl.NumberFormat(locale, options).format(value);
485
- });
486
- }
487
- function relativeTime( /** The date time that needs to be formatted. */
488
- date, /** The reference point in time to which `date` will be formatted in relation to. */
489
- now) {
490
- try {
491
- if (!now) {
492
- if (globalNow) {
493
- now = globalNow;
494
- } else {
495
- throw new Error("development" !== 'production' ? "The `now` parameter wasn't provided and there was no global fallback configured on the provider." : undefined);
496
- }
497
- }
498
- var dateDate = date instanceof Date ? date : new Date(date);
499
- var nowDate = now instanceof Date ? now : new Date(now);
500
- var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
501
- var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
502
- unit = _getRelativeTimeForma.unit,
503
- value = _getRelativeTimeForma.value;
504
- return new Intl.RelativeTimeFormat(locale, {
505
- numeric: 'auto'
506
- }).format(value, unit);
507
- } catch (error) {
508
- onError(new IntlError(exports.IntlErrorCode.FORMATTING_ERROR, error.message));
509
- return String(date);
510
- }
511
- }
512
- function list(value, formatOrOptions) {
513
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.list, function (options) {
514
- return new Intl.ListFormat(locale, options).format(value);
515
- });
516
- }
517
- return {
518
- dateTime: dateTime,
519
- number: number,
520
- relativeTime: relativeTime,
521
- list: list
522
- };
523
- }
524
-
525
- function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
526
- Object.entries(messages).forEach(function (_ref) {
527
- var key = _ref[0],
528
- messageOrMessages = _ref[1];
529
- if (key.includes('.')) {
530
- var keyLabel = key;
531
- if (parentPath) keyLabel += " (at " + parentPath + ")";
532
- invalidKeyLabels.push(keyLabel);
533
- }
534
- if (messageOrMessages != null && typeof messageOrMessages === 'object') {
535
- validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
536
- return part != null;
537
- }).join('.'));
538
- }
539
- });
540
- }
541
- function validateMessages(messages, onError) {
542
- var invalidKeyLabels = [];
543
- validateMessagesSegment(messages, invalidKeyLabels);
544
- if (invalidKeyLabels.length > 0) {
545
- onError(new IntlError(exports.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(', ') + "\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" ));
546
- }
547
- }
548
-
549
- var _excluded = ["getMessageFallback", "messages", "onError"];
550
- /**
551
- * Enhances the incoming props with defaults.
552
- */
553
- function initializeConfig(_ref) {
554
- var getMessageFallback = _ref.getMessageFallback,
555
- messages = _ref.messages,
556
- onError = _ref.onError,
557
- rest = _objectWithoutPropertiesLoose(_ref, _excluded);
558
- var finalOnError = onError || defaultOnError;
559
- var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
560
- {
561
- if (messages) {
562
- validateMessages(messages, finalOnError);
563
- }
564
- }
565
- return _extends({}, rest, {
566
- messages: messages,
567
- onError: finalOnError,
568
- getMessageFallback: finalGetMessageFallback
569
- });
570
- }
571
-
572
- /** @deprecated Switch to `createFormatter` */
573
- function createIntl() {
574
- var formatter = createFormatter.apply(void 0, arguments);
575
- return {
576
- formatDateTime: formatter.dateTime,
577
- formatNumber: formatter.number,
578
- formatRelativeTime: formatter.relativeTime
579
- };
580
- }
581
-
582
- exports.IntlError = IntlError;
583
- exports._extends = _extends;
584
- exports._objectWithoutPropertiesLoose = _objectWithoutPropertiesLoose;
585
- exports.createBaseTranslator = createBaseTranslator;
586
- exports.createFormatter = createFormatter;
587
- exports.createIntl = createIntl;
588
- exports.defaultGetMessageFallback = defaultGetMessageFallback;
589
- exports.defaultOnError = defaultOnError;
590
- exports.initializeConfig = initializeConfig;
591
- exports.resolveNamespace = resolveNamespace;
592
- //# sourceMappingURL=createIntl-34d4281e.js.map