use-intl 2.20.2 → 2.22.0

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.
@@ -1,10 +1,200 @@
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);
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
+ var message = messages;
13
+ key.split('.').forEach(function (part) {
14
+ var next = message[part];
15
+ if (part == null || next == null) {
16
+ throw new Error(process.env.NODE_ENV !== 'production' ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
17
+ }
18
+ message = next;
19
+ });
20
+ return message;
21
+ }
22
+ function prepareTranslationValues(values) {
23
+ if (Object.keys(values).length === 0) return undefined;
24
+ // Workaround for https://github.com/formatjs/formatjs/issues/1467
25
+ var transformedValues = {};
26
+ Object.keys(values).forEach(function (key) {
27
+ var index = 0;
28
+ var value = values[key];
29
+ var transformed;
30
+ if (typeof value === 'function') {
31
+ transformed = function transformed(chunks) {
32
+ var result = value(chunks);
33
+ return isValidElement(result) ? cloneElement(result, {
34
+ key: key + index++
35
+ }) : result;
36
+ };
37
+ } else {
38
+ transformed = value;
39
+ }
40
+ transformedValues[key] = transformed;
41
+ });
42
+ return transformedValues;
43
+ }
44
+ function getMessagesOrError(_ref) {
45
+ var messages = _ref.messages,
46
+ namespace = _ref.namespace,
47
+ _ref$onError = _ref.onError,
48
+ onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
49
+ try {
50
+ if (!messages) {
51
+ throw new Error(process.env.NODE_ENV !== 'production' ? "No messages were configured on the provider." : undefined);
52
+ }
53
+ var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
54
+ if (!retrievedMessages) {
55
+ throw new Error(process.env.NODE_ENV !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
56
+ }
57
+ return retrievedMessages;
58
+ } catch (error) {
59
+ var intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
60
+ onError(intlError);
61
+ return intlError;
62
+ }
63
+ }
64
+ function getPlainMessage(candidate, values) {
65
+ if (values) return undefined;
66
+ var unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
67
+ // Placeholders can be in the message if there are default values,
68
+ // or if the user has forgotten to provide values. In the latter
69
+ // case we need to compile the message to receive an error.
70
+ var hasPlaceholders = /<|{/.test(unescapedMessage);
71
+ if (!hasPlaceholders) {
72
+ return unescapedMessage;
73
+ }
74
+ return undefined;
75
+ }
76
+ function createBaseTranslator(_ref2) {
77
+ var 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
+ messageFormatCache = _ref2.messageFormatCache,
83
+ messagesOrError = _ref2.messagesOrError,
84
+ namespace = _ref2.namespace,
85
+ onError = _ref2.onError,
86
+ timeZone = _ref2.timeZone;
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
+ function translateBaseFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
97
+ key, /** Key value pairs for values to interpolate into the message. */
98
+ values, /** Provide custom formats for numbers, dates and times. */
99
+ formats) {
100
+ if (messagesOrError instanceof IntlError) {
101
+ // We have already warned about this during render
102
+ return getMessageFallback({
103
+ error: messagesOrError,
104
+ key: key,
105
+ namespace: namespace
106
+ });
107
+ }
108
+ var messages = messagesOrError;
109
+ var message;
110
+ try {
111
+ message = resolvePath(messages, key, namespace);
112
+ } catch (error) {
113
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
114
+ }
115
+ function joinPath(parts) {
116
+ return parts.filter(function (part) {
117
+ return part != null;
118
+ }).join('.');
119
+ }
120
+ var cacheKey = joinPath([locale, namespace, key, String(message)]);
121
+ var messageFormat;
122
+ if (messageFormatCache != null && messageFormatCache.has(cacheKey)) {
123
+ messageFormat = messageFormatCache.get(cacheKey);
124
+ } else {
125
+ if (typeof message === 'object') {
126
+ var code, errorMessage;
127
+ if (Array.isArray(message)) {
128
+ code = IntlErrorCode.INVALID_MESSAGE;
129
+ if (process.env.NODE_ENV !== 'production') {
130
+ 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";
131
+ }
132
+ } else {
133
+ code = IntlErrorCode.INSUFFICIENT_PATH;
134
+ if (process.env.NODE_ENV !== 'production') {
135
+ 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";
136
+ }
137
+ }
138
+ return getFallbackFromErrorAndNotify(key, code, errorMessage);
139
+ }
140
+ // Hot path that avoids creating an `IntlMessageFormat` instance
141
+ var plainMessage = getPlainMessage(message, values);
142
+ if (plainMessage) return plainMessage;
143
+ try {
144
+ messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
145
+ } catch (error) {
146
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
147
+ }
148
+ messageFormatCache == null || messageFormatCache.set(cacheKey, messageFormat);
149
+ }
150
+ try {
151
+ var formattedMessage = messageFormat.format(
152
+ // @ts-ignore `intl-messageformat` expects a different format
153
+ // for rich text elements since a recent minor update. This
154
+ // needs to be evaluated in detail, possibly also in regards
155
+ // to be able to format to parts.
156
+ prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
157
+ if (formattedMessage == null) {
158
+ throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
159
+ }
160
+ // Limit the function signature to return strings or React elements
161
+ return isValidElement(formattedMessage) ||
162
+ // Arrays of React elements
163
+ Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
164
+ } catch (error) {
165
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
166
+ }
167
+ }
168
+ function translateFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
169
+ key, /** Key value pairs for values to interpolate into the message. */
170
+ values, /** Provide custom formats for numbers, dates and times. */
171
+ formats) {
172
+ var result = translateBaseFn(key, values, formats);
173
+ if (typeof result !== 'string') {
174
+ 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);
175
+ }
176
+ return result;
177
+ }
178
+ translateFn.rich = translateBaseFn;
179
+ translateFn.raw = function ( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
180
+ key) {
181
+ if (messagesOrError instanceof IntlError) {
182
+ // We have already warned about this during render
183
+ return getMessageFallback({
184
+ error: messagesOrError,
185
+ key: key,
186
+ namespace: namespace
187
+ });
188
+ }
189
+ var messages = messagesOrError;
190
+ try {
191
+ return resolvePath(messages, key, namespace);
192
+ } catch (error) {
193
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
194
+ }
195
+ };
196
+ return translateFn;
7
197
  }
8
198
 
9
- export { resolveNamespace as default };
199
+ export { createBaseTranslator as default, getMessagesOrError };
10
200
  //# sourceMappingURL=use-intl.esm8.js.map
@@ -1 +1 @@
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;;;;"}
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 MessageFormatCache from './MessageFormatCache';\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 messageFormatCache?: MessageFormatCache;\n defaultTranslationValues?: RichTranslationValues;\n namespace?: string;\n messagesOrError: Messages | IntlError;\n};\n\nfunction getPlainMessage(candidate: string, values?: unknown) {\n if (values) return undefined;\n\n const unescapedMessage = candidate.replace(/'([{}])/gi, '$1');\n\n // Placeholders can be in the message if there are default values,\n // or if the user has forgotten to provide values. In the latter\n // case we need to compile the message to receive an error.\n const hasPlaceholders = /<|{/.test(unescapedMessage);\n\n if (!hasPlaceholders) {\n return unescapedMessage;\n }\n\n return undefined;\n}\n\nexport default function createBaseTranslator<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>({\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback = defaultGetMessageFallback,\n locale,\n messageFormatCache,\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([locale, namespace, key, String(message)]);\n\n let messageFormat: IntlMessageFormat;\n if (messageFormatCache?.has(cacheKey)) {\n messageFormat = messageFormatCache.get(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 // Hot path that avoids creating an `IntlMessageFormat` instance\n const plainMessage = getPlainMessage(message as string, values);\n if (plainMessage) return plainMessage;\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 messageFormatCache?.set(cacheKey, messageFormat);\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","_ref","_ref$onError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","getPlainMessage","candidate","unescapedMessage","replace","hasPlaceholders","test","createBaseTranslator","_ref2","defaultTranslationValues","globalFormats","formats","_ref2$getMessageFallb","getMessageFallback","defaultGetMessageFallback","locale","messageFormatCache","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","joinPath","parts","filter","join","cacheKey","String","messageFormat","has","get","errorMessage","Array","isArray","INVALID_MESSAGE","INSUFFICIENT_PATH","plainMessage","IntlMessageFormat","convertFormatsToIntlMessageFormat","_extends","set","formattedMessage","format","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAqBA,SAASA,WAAWA,CAClBC,QAA0C,EAC1CC,GAAW,EACXC,SAAkB,EAAA;EAElB,IAAI,CAACF,QAAQ,EAAE;AACb,IAAA,MAAM,IAAIG,KAAK,CACbC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,GAAA,4BAAA,GACHJ,SAAS,GAAA,IAAA,GACvCK,SAAS,CACd,CAAA;AACF,GAAA;EAED,IAAIC,OAAO,GAAGR,QAAQ,CAAA;EAEtBC,GAAG,CAACQ,KAAK,CAAC,GAAG,CAAC,CAACC,OAAO,CAAC,UAACC,IAAI,EAAI;AAC9B,IAAA,IAAMC,IAAI,GAAIJ,OAAe,CAACG,IAAI,CAAC,CAAA;AAEnC,IAAA,IAAIA,IAAI,IAAI,IAAI,IAAIC,IAAI,IAAI,IAAI,EAAE;MAChC,MAAM,IAAIT,KAAK,CACbC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,2BACVL,GAAG,GAAA,OAAA,IACxBC,SAAS,GAAQA,GAAAA,GAAAA,SAAS,SAAO,UACnC,CAAA,GAAA,GAAA,GACAK,SAAS,CACd,CAAA;AACF,KAAA;AAEDC,IAAAA,OAAO,GAAGI,IAAI,CAAA;AAChB,GAAC,CAAC,CAAA;AAEF,EAAA,OAAOJ,OAAO,CAAA;AAChB,CAAA;AAEA,SAASK,wBAAwBA,CAACC,MAA6B,EAAA;AAC7D,EAAA,IAAIC,MAAM,CAACC,IAAI,CAACF,MAAM,CAAC,CAACG,MAAM,KAAK,CAAC,EAAE,OAAOV,SAAS,CAAA;AAE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KAAK,EAAE;MACd,OAAOuB,6BAA6B,CAClCtD,GAAG,EACHkC,aAAa,CAACC,eAAe,EAC5BJ,KAAe,CAACxB,OAAO,CACzB,CAAA;AACF,KAAA;GACF,CAAA;AAED,EAAA,OAAOwE,WAAW,CAAA;AACpB;;;;"}
@@ -1,6 +1,6 @@
1
1
  import { useMemo } from 'react';
2
- import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm7.js';
3
- import resolveNamespace from '../core/use-intl.esm8.js';
2
+ import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm8.js';
3
+ import resolveNamespace from '../core/use-intl.esm7.js';
4
4
  import useIntlContext from './use-intl.esm11.js';
5
5
 
6
6
  function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
@@ -1,6 +1,6 @@
1
1
  export default function useFormatter(): {
2
2
  dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
3
3
  number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
4
- relativeTime: (date: number | Date, now?: number | Date | undefined) => string;
4
+ relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
5
5
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
6
6
  };
@@ -2,5 +2,5 @@
2
2
  export default function useIntl(): {
3
3
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
4
4
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
5
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
6
6
  };
@@ -0,0 +1,5 @@
1
+ type RelativeTimeFormatOptions = {
2
+ now?: number | Date;
3
+ unit?: Intl.RelativeTimeFormatUnit;
4
+ };
5
+ export default RelativeTimeFormatOptions;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=RelativeTimeFormatOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RelativeTimeFormatOptions.js","sourceRoot":"","sources":["../../../src/core/RelativeTimeFormatOptions.tsx"],"names":[],"mappings":""}
@@ -2,6 +2,7 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
2
2
  import Formats from './Formats';
3
3
  import IntlError from './IntlError';
4
4
  import NumberFormatOptions from './NumberFormatOptions';
5
+ import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
5
6
  import TimeZone from './TimeZone';
6
7
  type Props = {
7
8
  locale: string;
@@ -13,7 +14,7 @@ type Props = {
13
14
  export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
14
15
  dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
15
16
  number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
16
- relativeTime: (date: number | Date, now?: number | Date) => string;
17
+ relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions) => string;
17
18
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions) => string;
18
19
  };
19
20
  export {};
@@ -1,45 +1,57 @@
1
1
  import IntlError, { IntlErrorCode } from './IntlError';
2
2
  import { defaultOnError } from './defaults';
3
- const MINUTE = 60;
3
+ const SECOND = 1;
4
+ const MINUTE = SECOND * 60;
4
5
  const HOUR = MINUTE * 60;
5
6
  const DAY = HOUR * 24;
6
7
  const WEEK = DAY * 7;
7
8
  const MONTH = DAY * (365 / 12); // Approximation
9
+ const QUARTER = MONTH * 3;
8
10
  const YEAR = DAY * 365;
9
- function getRelativeTimeFormatConfig(seconds) {
11
+ const UNIT_SECONDS = {
12
+ second: SECOND,
13
+ seconds: SECOND,
14
+ minute: MINUTE,
15
+ minutes: MINUTE,
16
+ hour: HOUR,
17
+ hours: HOUR,
18
+ day: DAY,
19
+ days: DAY,
20
+ week: WEEK,
21
+ weeks: WEEK,
22
+ month: MONTH,
23
+ months: MONTH,
24
+ quarter: QUARTER,
25
+ quarters: QUARTER,
26
+ year: YEAR,
27
+ years: YEAR
28
+ };
29
+ function resolveRelativeTimeUnit(seconds) {
10
30
  const absValue = Math.abs(seconds);
11
- let value, unit;
12
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
13
- // will include fractions like '2.1 hours ago'.
14
31
  if (absValue < MINUTE) {
15
- unit = 'second';
16
- value = Math.round(seconds);
32
+ return 'second';
17
33
  }
18
34
  else if (absValue < HOUR) {
19
- unit = 'minute';
20
- value = Math.round(seconds / MINUTE);
35
+ return 'minute';
21
36
  }
22
37
  else if (absValue < DAY) {
23
- unit = 'hour';
24
- value = Math.round(seconds / HOUR);
38
+ return 'hour';
25
39
  }
26
40
  else if (absValue < WEEK) {
27
- unit = 'day';
28
- value = Math.round(seconds / DAY);
41
+ return 'day';
29
42
  }
30
43
  else if (absValue < MONTH) {
31
- unit = 'week';
32
- value = Math.round(seconds / WEEK);
44
+ return 'week';
33
45
  }
34
46
  else if (absValue < YEAR) {
35
- unit = 'month';
36
- value = Math.round(seconds / MONTH);
37
- }
38
- else {
39
- unit = 'year';
40
- value = Math.round(seconds / YEAR);
47
+ return 'month';
41
48
  }
42
- return { value, unit };
49
+ return 'year';
50
+ }
51
+ function calculateRelativeTimeValue(seconds, unit) {
52
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
53
+ // will include fractions like '2.1 hours ago'.
54
+ return Math.round(seconds / UNIT_SECONDS[unit]);
43
55
  }
44
56
  export default function createFormatter({ formats, locale, now: globalNow, onError = defaultOnError, timeZone: globalTimeZone }) {
45
57
  function resolveFormatOrOptions(typeFormats, formatOrOptions) {
@@ -99,29 +111,41 @@ export default function createFormatter({ formats, locale, now: globalNow, onErr
99
111
  function number(value, formatOrOptions) {
100
112
  return getFormattedValue(value, formatOrOptions, formats?.number, (options) => new Intl.NumberFormat(locale, options).format(value));
101
113
  }
114
+ function getGlobalNow() {
115
+ if (globalNow) {
116
+ return globalNow;
117
+ }
118
+ else {
119
+ onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production'
120
+ ? `The \`now\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`
121
+ : undefined));
122
+ return new Date();
123
+ }
124
+ }
125
+ function extractNowDate(nowOrOptions) {
126
+ if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
127
+ return new Date(nowOrOptions);
128
+ }
129
+ if (nowOrOptions?.now !== undefined) {
130
+ return new Date(nowOrOptions.now);
131
+ }
132
+ return getGlobalNow();
133
+ }
102
134
  function relativeTime(
103
135
  /** The date time that needs to be formatted. */
104
136
  date,
105
137
  /** The reference point in time to which `date` will be formatted in relation to. */
106
- now) {
138
+ nowOrOptions) {
107
139
  try {
108
- if (!now) {
109
- if (globalNow) {
110
- now = globalNow;
111
- }
112
- else {
113
- onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production'
114
- ? `The \`now\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`
115
- : undefined));
116
- }
117
- }
118
- const dateDate = date instanceof Date ? date : new Date(date);
119
- const nowDate = now instanceof Date
120
- ? now
121
- : // @ts-expect-error -- `undefined` is fine for the `Date` constructor
122
- new Date(now);
140
+ const dateDate = new Date(date);
141
+ const nowDate = extractNowDate(nowOrOptions);
123
142
  const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
124
- const { unit, value } = getRelativeTimeFormatConfig(seconds);
143
+ const unit = typeof nowOrOptions === 'number' ||
144
+ nowOrOptions instanceof Date ||
145
+ nowOrOptions?.unit === undefined
146
+ ? resolveRelativeTimeUnit(seconds)
147
+ : nowOrOptions.unit;
148
+ const value = calculateRelativeTimeValue(seconds, unit);
125
149
  return new Intl.RelativeTimeFormat(locale, {
126
150
  numeric: 'auto'
127
151
  }).format(value, unit);
@@ -1 +1 @@
1
- {"version":3,"file":"createFormatter.js","sourceRoot":"","sources":["../../../src/core/createFormatter.tsx"],"names":[],"mappings":"AAEA,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAGrD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAC;AAE1C,MAAM,MAAM,GAAG,EAAE,CAAC;AAClB,MAAM,IAAI,GAAG,MAAM,GAAG,EAAE,CAAC;AACzB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AACtB,MAAM,IAAI,GAAG,GAAG,GAAG,CAAC,CAAC;AACrB,MAAM,KAAK,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,gBAAgB;AAChD,MAAM,IAAI,GAAG,GAAG,GAAG,GAAG,CAAC;AAEvB,SAAS,2BAA2B,CAAC,OAAe;IAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACnC,IAAI,KAAK,EAAE,IAAiC,CAAC;IAE7C,sEAAsE;IACtE,+CAA+C;IAE/C,IAAI,QAAQ,GAAG,MAAM,EAAE;QACrB,IAAI,GAAG,QAAQ,CAAC;QAChB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;KAC7B;SAAM,IAAI,QAAQ,GAAG,IAAI,EAAE;QAC1B,IAAI,GAAG,QAAQ,CAAC;QAChB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;KACtC;SAAM,IAAI,QAAQ,GAAG,GAAG,EAAE;QACzB,IAAI,GAAG,MAAM,CAAC;QACd,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI,C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,IAAI,EAAC,CAAC;AAChD,CAAC"}
@@ -3,5 +3,5 @@ import createFormatter from './createFormatter';
3
3
  export default function createIntl(...args: Parameters<typeof createFormatter>): {
4
4
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("./DateTimeFormatOptions").default | undefined) => string;
5
5
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
6
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
6
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("./RelativeTimeFormatOptions").default | undefined) => string;
7
7
  };
@@ -1,6 +1,6 @@
1
1
  export default function useFormatter(): {
2
2
  dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
3
3
  number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
4
- relativeTime: (date: number | Date, now?: number | Date | undefined) => string;
4
+ relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
5
5
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
6
6
  };
@@ -2,5 +2,5 @@
2
2
  export default function useIntl(): {
3
3
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
4
4
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
5
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
6
6
  };
@@ -460,43 +460,53 @@ function createTranslator(_ref) {
460
460
  }), '!');
461
461
  }
462
462
 
463
- var MINUTE = 60;
463
+ var SECOND = 1;
464
+ var MINUTE = SECOND * 60;
464
465
  var HOUR = MINUTE * 60;
465
466
  var DAY = HOUR * 24;
466
467
  var WEEK = DAY * 7;
467
468
  var MONTH = DAY * (365 / 12); // Approximation
469
+ var QUARTER = MONTH * 3;
468
470
  var YEAR = DAY * 365;
469
- function getRelativeTimeFormatConfig(seconds) {
471
+ var UNIT_SECONDS = {
472
+ second: SECOND,
473
+ seconds: SECOND,
474
+ minute: MINUTE,
475
+ minutes: MINUTE,
476
+ hour: HOUR,
477
+ hours: HOUR,
478
+ day: DAY,
479
+ days: DAY,
480
+ week: WEEK,
481
+ weeks: WEEK,
482
+ month: MONTH,
483
+ months: MONTH,
484
+ quarter: QUARTER,
485
+ quarters: QUARTER,
486
+ year: YEAR,
487
+ years: YEAR
488
+ };
489
+ function resolveRelativeTimeUnit(seconds) {
470
490
  var absValue = Math.abs(seconds);
471
- var value, unit;
472
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
473
- // will include fractions like '2.1 hours ago'.
474
491
  if (absValue < MINUTE) {
475
- unit = 'second';
476
- value = Math.round(seconds);
492
+ return 'second';
477
493
  } else if (absValue < HOUR) {
478
- unit = 'minute';
479
- value = Math.round(seconds / MINUTE);
494
+ return 'minute';
480
495
  } else if (absValue < DAY) {
481
- unit = 'hour';
482
- value = Math.round(seconds / HOUR);
496
+ return 'hour';
483
497
  } else if (absValue < WEEK) {
484
- unit = 'day';
485
- value = Math.round(seconds / DAY);
498
+ return 'day';
486
499
  } else if (absValue < MONTH) {
487
- unit = 'week';
488
- value = Math.round(seconds / WEEK);
500
+ return 'week';
489
501
  } else if (absValue < YEAR) {
490
- unit = 'month';
491
- value = Math.round(seconds / MONTH);
492
- } else {
493
- unit = 'year';
494
- value = Math.round(seconds / YEAR);
502
+ return 'month';
495
503
  }
496
- return {
497
- value: value,
498
- unit: unit
499
- };
504
+ return 'year';
505
+ }
506
+ function calculateRelativeTimeValue(seconds, unit) {
507
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
508
+ // will include fractions like '2.1 hours ago'.
509
+ return Math.round(seconds / UNIT_SECONDS[unit]);
500
510
  }
501
511
  function createFormatter(_ref) {
502
512
  var formats = _ref.formats,
@@ -558,25 +568,32 @@ function createFormatter(_ref) {
558
568
  return new Intl.NumberFormat(locale, options).format(value);
559
569
  });
560
570
  }
571
+ function getGlobalNow() {
572
+ if (globalNow) {
573
+ return globalNow;
574
+ } else {
575
+ onError(new IntlError(exports.IntlErrorCode.ENVIRONMENT_FALLBACK, "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" ));
576
+ return new Date();
577
+ }
578
+ }
579
+ function extractNowDate(nowOrOptions) {
580
+ if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
581
+ return new Date(nowOrOptions);
582
+ }
583
+ if ((nowOrOptions == null ? void 0 : nowOrOptions.now) !== undefined) {
584
+ return new Date(nowOrOptions.now);
585
+ }
586
+ return getGlobalNow();
587
+ }
561
588
  function relativeTime( /** The date time that needs to be formatted. */
562
589
  date, /** The reference point in time to which `date` will be formatted in relation to. */
563
- now) {
590
+ nowOrOptions) {
564
591
  try {
565
- if (!now) {
566
- if (globalNow) {
567
- now = globalNow;
568
- } else {
569
- onError(new IntlError(exports.IntlErrorCode.ENVIRONMENT_FALLBACK, "development" !== 'production' ? "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" : undefined));
570
- }
571
- }
572
- var dateDate = date instanceof Date ? date : new Date(date);
573
- var nowDate = now instanceof Date ? now :
574
- // @ts-expect-error -- `undefined` is fine for the `Date` constructor
575
- new Date(now);
592
+ var dateDate = new Date(date);
593
+ var nowDate = extractNowDate(nowOrOptions);
576
594
  var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
577
- var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
578
- unit = _getRelativeTimeForma.unit,
579
- value = _getRelativeTimeForma.value;
595
+ var unit = typeof nowOrOptions === 'number' || nowOrOptions instanceof Date || (nowOrOptions == null ? void 0 : nowOrOptions.unit) === undefined ? resolveRelativeTimeUnit(seconds) : nowOrOptions.unit;
596
+ var value = calculateRelativeTimeValue(seconds, unit);
580
597
  return new Intl.RelativeTimeFormat(locale, {
581
598
  numeric: 'auto'
582
599
  }).format(value, unit);