use-intl 0.0.0-canary-92d8a58 → 0.0.0-canary-2b3a9ce
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.
- package/README.md +1 -1
- package/dist/esm/development/core.js +6 -2
- package/dist/esm/development/index.js +2 -2
- package/dist/esm/development/{initializeConfig-CRD6euuK.js → initializeConfig-CIDVMS2E.js} +48 -42
- package/dist/esm/development/react.js +48 -2
- package/dist/esm/production/core.js +1 -1
- package/dist/esm/production/index.js +1 -1
- package/dist/esm/production/initializeConfig-CzP0yD8_.js +1 -0
- package/dist/esm/production/react.js +1 -1
- package/dist/types/core/createBaseTranslator.d.ts +3 -3
- package/dist/types/core/createTranslator.d.ts +12 -8
- package/dist/types/core/createTranslatorImpl.d.ts +3 -3
- package/dist/types/core/index.d.ts +2 -0
- package/dist/types/react/index.d.ts +2 -0
- package/dist/types/react/useExtracted.d.ts +42 -0
- package/dist/types/react/useTranslationsImpl.d.ts +3 -3
- package/package.json +3 -3
- package/dist/esm/production/initializeConfig-zfMDfl5R.js +0 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Internationalization (i18n) is an essential part of the user experience, therefo
|
|
|
10
10
|
- 📅 **Dates, times & numbers**: Apply appropriate formatting without worrying about server/client differences like time zones.
|
|
11
11
|
- ✅ **Type-safe**: Speed up development with autocompletion for message keys and catch typos early with compile-time checks.
|
|
12
12
|
- 💡 **Hooks-based API**: Learn a single API that can be used across your code base to turn translations into plain strings or rich text.
|
|
13
|
-
-
|
|
13
|
+
- 🛠️ **Standards-based**: Use the best parts of built-in JavaScript APIs and supplemental lower-level APIs from Format.JS.
|
|
14
14
|
|
|
15
15
|
## What does it look like?
|
|
16
16
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { r as resolveNamespace, e as createBaseTranslator, f as defaultGetMessageFallback, b as createIntlFormatters, d as createCache, g as defaultOnError } from './initializeConfig-
|
|
2
|
-
export { I as IntlError, a as IntlErrorCode, c as createFormatter, i as initializeConfig } from './initializeConfig-
|
|
1
|
+
import { r as resolveNamespace, e as createBaseTranslator, f as defaultGetMessageFallback, b as createIntlFormatters, d as createCache, g as defaultOnError } from './initializeConfig-CIDVMS2E.js';
|
|
2
|
+
export { I as IntlError, a as IntlErrorCode, c as createFormatter, i as initializeConfig } from './initializeConfig-CIDVMS2E.js';
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
|
|
@@ -23,6 +23,10 @@ function createTranslatorImpl({
|
|
|
23
23
|
// This type is slightly more loose than `AbstractIntlMessages`
|
|
24
24
|
// in order to avoid a type error.
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* @private Not intended for direct use.
|
|
28
|
+
*/
|
|
29
|
+
|
|
26
30
|
/**
|
|
27
31
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
28
32
|
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { I as IntlError, a as IntlErrorCode, d as _createCache, b as _createIntlFormatters, c as createFormatter, i as initializeConfig } from './initializeConfig-
|
|
1
|
+
export { I as IntlError, a as IntlErrorCode, d as _createCache, b as _createIntlFormatters, c as createFormatter, i as initializeConfig } from './initializeConfig-CIDVMS2E.js';
|
|
2
2
|
export { createTranslator, hasLocale } from './core.js';
|
|
3
|
-
export { IntlProvider, useFormatter, useLocale, useMessages, useNow, useTimeZone, useTranslations } from './react.js';
|
|
3
|
+
export { IntlProvider, _useExtracted, useFormatter, useLocale, useMessages, useNow, useTimeZone, useTranslations } from './react.js';
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
@@ -186,7 +186,7 @@ function prepareTranslationValues(values) {
|
|
|
186
186
|
});
|
|
187
187
|
return transformedValues;
|
|
188
188
|
}
|
|
189
|
-
function getMessagesOrError(locale, messages, namespace
|
|
189
|
+
function getMessagesOrError(locale, messages, namespace) {
|
|
190
190
|
try {
|
|
191
191
|
if (!messages) {
|
|
192
192
|
throw new Error(`No messages were configured.` );
|
|
@@ -200,27 +200,23 @@ function getMessagesOrError(locale, messages, namespace, onError = defaultOnErro
|
|
|
200
200
|
return retrievedMessages;
|
|
201
201
|
} catch (error) {
|
|
202
202
|
const intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
203
|
-
onError(intlError);
|
|
204
203
|
return intlError;
|
|
205
204
|
}
|
|
206
205
|
}
|
|
207
206
|
function getPlainMessage(candidate, values) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
//
|
|
213
|
-
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return unescapedMessage;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
207
|
+
// To improve runtime performance, only compile message if:
|
|
208
|
+
return (
|
|
209
|
+
// 1. Values are provided
|
|
210
|
+
values ||
|
|
211
|
+
// 2. There are escaped braces (e.g. "'{name'}")
|
|
212
|
+
/'[{}]/.test(candidate) ||
|
|
213
|
+
// 3. There are missing arguments or tags (dev-only error handling)
|
|
214
|
+
/<|{/.test(candidate) ? undefined // Compile
|
|
215
|
+
: candidate // Don't compile
|
|
216
|
+
);
|
|
221
217
|
}
|
|
222
218
|
function createBaseTranslator(config) {
|
|
223
|
-
const messagesOrError = getMessagesOrError(config.locale, config.messages, config.namespace
|
|
219
|
+
const messagesOrError = getMessagesOrError(config.locale, config.messages, config.namespace);
|
|
224
220
|
return createBaseTranslatorImpl({
|
|
225
221
|
...config,
|
|
226
222
|
messagesOrError
|
|
@@ -238,10 +234,10 @@ function createBaseTranslatorImpl({
|
|
|
238
234
|
timeZone
|
|
239
235
|
}) {
|
|
240
236
|
const hasMessagesError = messagesOrError instanceof IntlError;
|
|
241
|
-
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
237
|
+
function getFallbackFromErrorAndNotify(key, code, message, fallback) {
|
|
242
238
|
const error = new IntlError(code, message);
|
|
243
239
|
onError(error);
|
|
244
|
-
return getMessageFallback({
|
|
240
|
+
return fallback ?? getMessageFallback({
|
|
245
241
|
error,
|
|
246
242
|
key,
|
|
247
243
|
namespace
|
|
@@ -250,33 +246,43 @@ function createBaseTranslatorImpl({
|
|
|
250
246
|
function translateBaseFn(/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
251
247
|
key, /** Key value pairs for values to interpolate into the message. */
|
|
252
248
|
values, /** Provide custom formats for numbers, dates and times. */
|
|
253
|
-
formats) {
|
|
254
|
-
|
|
255
|
-
// We have already warned about this during render
|
|
256
|
-
return getMessageFallback({
|
|
257
|
-
error: messagesOrError,
|
|
258
|
-
key,
|
|
259
|
-
namespace
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
const messages = messagesOrError;
|
|
249
|
+
formats, _fallback) {
|
|
250
|
+
const fallback = _fallback;
|
|
263
251
|
let message;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
252
|
+
if (hasMessagesError) {
|
|
253
|
+
if (fallback) {
|
|
254
|
+
message = fallback;
|
|
255
|
+
} else {
|
|
256
|
+
onError(messagesOrError);
|
|
257
|
+
return getMessageFallback({
|
|
258
|
+
error: messagesOrError,
|
|
259
|
+
key,
|
|
260
|
+
namespace
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
const messages = messagesOrError;
|
|
265
|
+
try {
|
|
266
|
+
message = resolvePath(locale, messages, key, namespace);
|
|
267
|
+
} catch (error) {
|
|
268
|
+
if (fallback) {
|
|
269
|
+
message = fallback;
|
|
270
|
+
} else {
|
|
271
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message, fallback);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
268
274
|
}
|
|
269
275
|
if (typeof message === 'object') {
|
|
270
276
|
let code, errorMessage;
|
|
271
277
|
if (Array.isArray(message)) {
|
|
272
278
|
code = IntlErrorCode.INVALID_MESSAGE;
|
|
273
279
|
{
|
|
274
|
-
errorMessage = `Message at \`${joinPath(namespace, key)}\` resolved to an array, but only strings are supported. See https://next-intl.dev/docs/usage/
|
|
280
|
+
errorMessage = `Message at \`${joinPath(namespace, key)}\` resolved to an array, but only strings are supported. See https://next-intl.dev/docs/usage/translations#arrays-of-messages`;
|
|
275
281
|
}
|
|
276
282
|
} else {
|
|
277
283
|
code = IntlErrorCode.INSUFFICIENT_PATH;
|
|
278
284
|
{
|
|
279
|
-
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.dev/docs/usage/
|
|
285
|
+
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.dev/docs/usage/translations#structuring-messages`;
|
|
280
286
|
}
|
|
281
287
|
}
|
|
282
288
|
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
@@ -307,7 +313,7 @@ function createBaseTranslatorImpl({
|
|
|
307
313
|
});
|
|
308
314
|
} catch (error) {
|
|
309
315
|
const thrownError = error;
|
|
310
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, thrownError.message + ('originalMessage' in thrownError ? ` (${thrownError.originalMessage})` : '') );
|
|
316
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, thrownError.message + ('originalMessage' in thrownError ? ` (${thrownError.originalMessage})` : '') , fallback);
|
|
311
317
|
}
|
|
312
318
|
try {
|
|
313
319
|
const formattedMessage = messageFormat.format(
|
|
@@ -325,14 +331,14 @@ function createBaseTranslatorImpl({
|
|
|
325
331
|
// Arrays of React elements
|
|
326
332
|
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
327
333
|
} catch (error) {
|
|
328
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
334
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message, fallback);
|
|
329
335
|
}
|
|
330
336
|
}
|
|
331
337
|
function translateFn(/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
332
338
|
key, /** Key value pairs for values to interpolate into the message. */
|
|
333
|
-
values, /**
|
|
334
|
-
formats) {
|
|
335
|
-
const result = translateBaseFn(key, values, formats);
|
|
339
|
+
values, /** Custom formats for numbers, dates and times. */
|
|
340
|
+
formats, _fallback) {
|
|
341
|
+
const result = translateBaseFn(key, values, formats, _fallback);
|
|
336
342
|
if (typeof result !== 'string') {
|
|
337
343
|
return getFallbackFromErrorAndNotify(key, 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.` );
|
|
338
344
|
}
|
|
@@ -341,11 +347,11 @@ function createBaseTranslatorImpl({
|
|
|
341
347
|
translateFn.rich = translateBaseFn;
|
|
342
348
|
|
|
343
349
|
// Augment `translateBaseFn` to return plain strings
|
|
344
|
-
translateFn.markup = (key, values, formats) => {
|
|
350
|
+
translateFn.markup = (key, values, formats, _fallback) => {
|
|
345
351
|
const result = translateBaseFn(key,
|
|
346
352
|
// @ts-expect-error -- `MarkupTranslationValues` is practically a sub type
|
|
347
353
|
// of `RichTranslationValues` but TypeScript isn't smart enough here.
|
|
348
|
-
values, formats);
|
|
354
|
+
values, formats, _fallback);
|
|
349
355
|
if (typeof result !== 'string') {
|
|
350
356
|
const error = new IntlError(IntlErrorCode.FORMATTING_ERROR, "`t.markup` only accepts functions for formatting that receive and return strings.\n\nE.g. t.markup('markup', {b: (chunks) => `<b>${chunks}</b>`})");
|
|
351
357
|
onError(error);
|
|
@@ -359,7 +365,7 @@ function createBaseTranslatorImpl({
|
|
|
359
365
|
};
|
|
360
366
|
translateFn.raw = key => {
|
|
361
367
|
if (hasMessagesError) {
|
|
362
|
-
|
|
368
|
+
onError(messagesOrError);
|
|
363
369
|
return getMessageFallback({
|
|
364
370
|
error: messagesOrError,
|
|
365
371
|
key,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createContext, useContext, useMemo, useState, useEffect } from 'react';
|
|
2
|
-
import { d as createCache, b as createIntlFormatters, i as initializeConfig, r as resolveNamespace, I as IntlError, a as IntlErrorCode, e as createBaseTranslator, c as createFormatter } from './initializeConfig-
|
|
2
|
+
import { d as createCache, b as createIntlFormatters, i as initializeConfig, r as resolveNamespace, I as IntlError, a as IntlErrorCode, e as createBaseTranslator, c as createFormatter } from './initializeConfig-CIDVMS2E.js';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
|
5
5
|
|
|
@@ -181,4 +181,50 @@ function useFormatter() {
|
|
|
181
181
|
}), [formats, formatters, globalNow, locale, onError, timeZone]);
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
function getArgs(messageOrParams, ...rest) {
|
|
185
|
+
let message, values, formats;
|
|
186
|
+
if (typeof messageOrParams === 'string') {
|
|
187
|
+
message = messageOrParams;
|
|
188
|
+
values = rest[0];
|
|
189
|
+
formats = rest[1];
|
|
190
|
+
} else {
|
|
191
|
+
message = messageOrParams.message;
|
|
192
|
+
values = messageOrParams.values;
|
|
193
|
+
formats = messageOrParams.formats;
|
|
194
|
+
// `description` is is not used at runtime
|
|
195
|
+
}
|
|
196
|
+
// @ts-expect-error -- Secret fallback parameter
|
|
197
|
+
return [undefined,
|
|
198
|
+
// Always use fallback if not compiled
|
|
199
|
+
values, formats, message ];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Note: This API is usually compiled into `useTranslations`,
|
|
203
|
+
// but there is some fallback handling which allows this hook
|
|
204
|
+
// to still work when not being compiled.
|
|
205
|
+
//
|
|
206
|
+
// This is relevant for:
|
|
207
|
+
// - Isolated environments like tests, Storybook, etc.
|
|
208
|
+
// - Fallbacks in case an extracted message is not yet available
|
|
209
|
+
function useExtracted(namespace) {
|
|
210
|
+
const t = useTranslations(namespace);
|
|
211
|
+
function translateFn(...params) {
|
|
212
|
+
// @ts-expect-error -- Passing `undefined` as an ID is secretly allowed here
|
|
213
|
+
return t(...getArgs(...params));
|
|
214
|
+
}
|
|
215
|
+
translateFn.rich = (...params) =>
|
|
216
|
+
// @ts-expect-error -- Passing `undefined` as an ID is secretly allowed here
|
|
217
|
+
t.rich(...getArgs(...params));
|
|
218
|
+
translateFn.markup = (...params) =>
|
|
219
|
+
// @ts-expect-error -- Passing `undefined` as an ID is secretly allowed here
|
|
220
|
+
t.markup(...getArgs(...params));
|
|
221
|
+
translateFn.has = function translateHasFn(
|
|
222
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
223
|
+
message) {
|
|
224
|
+
// Not really something better we can do here
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
return translateFn;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export { IntlProvider, useExtracted as _useExtracted, useFormatter, useLocale, useMessages, useNow, useTimeZone, useTranslations };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{r as e,e as s,f as r,b as t,d as o,g as n}from"./initializeConfig-
|
|
1
|
+
import{r as e,e as s,f as r,b as t,d as o,g as n}from"./initializeConfig-CzP0yD8_.js";export{I as IntlError,a as IntlErrorCode,c as createFormatter,i as initializeConfig}from"./initializeConfig-CzP0yD8_.js";function m({_cache:a=o(),_formatters:i=t(a),getMessageFallback:m=r,messages:c,namespace:f,onError:g=n,...l}){return function({messages:a,namespace:r,...t},o){return a=a[o],r=e(r,o),s({...t,messages:a,namespace:r})}({...l,onError:g,cache:a,formatters:i,getMessageFallback:m,messages:{"!":c},namespace:f?`!.${f}`:"!"},"!")}function f(e,a){return e.includes(a)}export{o as _createCache,t as _createIntlFormatters,m as createTranslator,f as hasLocale};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export{I as IntlError,a as IntlErrorCode,d as _createCache,b as _createIntlFormatters,c as createFormatter,i as initializeConfig}from"./initializeConfig-
|
|
1
|
+
export{I as IntlError,a as IntlErrorCode,d as _createCache,b as _createIntlFormatters,c as createFormatter,i as initializeConfig}from"./initializeConfig-CzP0yD8_.js";export{createTranslator,hasLocale}from"./core.js";export{IntlProvider,_useExtracted,useFormatter,useLocale,useMessages,useNow,useTimeZone,useTranslations}from"./react.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{IntlMessageFormat as e}from"intl-messageformat";import{isValidElement as t,cloneElement as r}from"react";import{memoize as n,strategies as o}from"@formatjs/fast-memoize";class a extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),this.code=e,t&&(this.originalMessage=t)}}var s=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}(s||{});function i(...e){return e.filter(Boolean).join(".")}function u(e){return i(e.namespace,e.key)}function c(e){console.error(e)}function m(){return{dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}}}function f(e,t){return n(e,{cache:(r=t,{create:()=>({get:e=>r[e],set(e,t){r[e]=t}})}),strategy:o.variadic});var r}function l(e,t){return f(((...t)=>new e(...t)),t)}function g(e){return{getDateTimeFormat:l(Intl.DateTimeFormat,e.dateTime),getNumberFormat:l(Intl.NumberFormat,e.number),getPluralRules:l(Intl.PluralRules,e.pluralRules),getRelativeTimeFormat:l(Intl.RelativeTimeFormat,e.relativeTime),getListFormat:l(Intl.ListFormat,e.list),getDisplayNames:l(Intl.DisplayNames,e.displayNames)}}function I(e,t,r,n){const o=i(n,r);if(!t)throw new Error(o);let a=t;return r.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(o+` (${e})`);a=r})),a}function E(n){const o=function(e,t,r){try{if(!t)throw new Error(void 0);const n=r?I(e,t,r):t;if(!n)throw new Error(r);return n}catch(e){return new a(s.MISSING_MESSAGE,e.message)}}(n.locale,n.messages,n.namespace);return function({cache:n,formats:o,formatters:i,getMessageFallback:c=u,locale:m,messagesOrError:l,namespace:g,onError:E,timeZone:S}){const T=l instanceof a;function N(e,t,r,n){const o=new a(t,r);return E(o),n??c({error:o,key:e,namespace:g})}function y(a,u,y,A){const M=A;let F,R;if(T){if(!M)return E(l),c({error:l,key:a,namespace:g});F=M}else{const e=l;try{F=I(m,e,a,g)}catch(e){if(!M)return N(a,s.MISSING_MESSAGE,e.message,M);F=M}}if("object"==typeof F){let e,t;return e=Array.isArray(F)?s.INVALID_MESSAGE:s.INSUFFICIENT_PATH,N(a,e,t)}const d=function(e,t){return t||/'[{}]/.test(e)?void 0:e}(F,u);if(d)return d;i.getMessageFormat||(i.getMessageFormat=function(t,r){return f(((...t)=>new e(t[0],t[1],t[2],{formatters:r,...t[3]})),t.message)}(n,i));try{R=i.getMessageFormat(F,m,function(t,r,n){const o=e.formats.date,a=e.formats.time,s={...t?.dateTime,...r?.dateTime},i={date:{...o,...s},time:{...a,...s},number:{...t?.number,...r?.number}};return n&&["date","time"].forEach((e=>{const t=i[e];for(const[e,r]of Object.entries(t))t[e]={timeZone:n,...r}})),i}(o,y,S),{formatters:{...i,getDateTimeFormat:(e,t)=>i.getDateTimeFormat(e,{timeZone:S,...t})}})}catch(e){const t=e;return N(a,s.INVALID_MESSAGE,t.message,M)}try{const e=R.format(u?function(e){const n={};return Object.keys(e).forEach((o=>{let a=0;const s=e[o];let i;i="function"==typeof s?e=>{const n=s(e);return t(n)?r(n,{key:o+a++}):n}:s,n[o]=i})),n}(u):u);if(null==e)throw new Error(void 0);return t(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(e){return N(a,s.FORMATTING_ERROR,e.message,M)}}function A(e,t,r,n){const o=y(e,t,r,n);return"string"!=typeof o?N(e,s.INVALID_MESSAGE,void 0):o}return A.rich=y,A.markup=(e,t,r,n)=>y(e,t,r,n),A.raw=e=>{if(T)return E(l),c({error:l,key:e,namespace:g});const t=l;try{return I(m,t,e,g)}catch(t){return N(e,s.MISSING_MESSAGE,t.message)}},A.has=e=>{if(T)return!1;try{return I(m,l,e,g),!0}catch{return!1}},A}({...n,messagesOrError:o})}function S(e,t){return e===t?void 0:e.slice((t+".").length)}const T=3600,N=86400,y=7*N,A=2628e3,M=7884e3,F=365*N,R={second:1,seconds:1,minute:60,minutes:60,hour:T,hours:T,day:N,days:N,week:y,weeks:y,month:A,months:A,quarter:M,quarters:M,year:F,years:F};function d(e){const{_cache:t=m(),_formatters:r=g(t),formats:n,locale:o,onError:i=c,timeZone:u}=e;function f(e){return e?.timeZone||(u?e={...e,timeZone:u}:i(new a(s.ENVIRONMENT_FALLBACK,void 0))),e}function l(e,t,r,n,o){let u;try{u=function(e,t,r){let n;if("string"==typeof t){const r=t;if(n=e?.[r],!n){const e=new a(s.MISSING_FORMAT,void 0);throw i(e),e}}else n=t;return r&&(n={...n,...r}),n}(r,e,t)}catch{return o()}try{return n(u)}catch(e){return i(new a(s.FORMATTING_ERROR,e.message)),o()}}function I(e,t,a){return l(t,a,n?.dateTime,(t=>(t=f(t),r.getDateTimeFormat(o,t).format(e))),(()=>String(e)))}function E(){return e.now?e.now:(i(new a(s.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:I,number:function(e,t,a){return l(t,a,n?.number,(t=>r.getNumberFormat(o,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let n,a;const s={};t instanceof Date||"number"==typeof t?n=new Date(t):t&&(n=null!=t.now?new Date(t.now):E(),a=t.unit,s.style=t.style,s.numberingSystem=t.numberingSystem),n||(n=E());const i=(new Date(e).getTime()-n.getTime())/1e3;a||(a=function(e){const t=Math.abs(e);return t<60?"second":t<T?"minute":t<N?"hour":t<y?"day":t<A?"week":t<F?"month":"year"}(i)),s.numeric="second"===a?"auto":"always";const u=function(e,t){return Math.round(e/R[t])}(i,a);return r.getRelativeTimeFormat(o,s).format(u,a)}catch(t){return i(new a(s.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,t,a){const s=[],i=new Map;let u=0;for(const t of e){let e;"object"==typeof t?(e=String(u),i.set(e,t)):e=String(t),s.push(e),u++}return l(t,a,n?.list,(e=>{const t=r.getListFormat(o,e).formatToParts(s).map((e=>"literal"===e.type?e.value:i.get(e.value)||e.value));return i.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,t,a,s){return l(a,s,n?.dateTime,(n=>(n=f(n),r.getDateTimeFormat(o,n).formatRange(e,t))),(()=>[I(e),I(t)].join(" – ")))}}}function h({formats:e,getMessageFallback:t,messages:r,onError:n,...o}){return{...o,formats:e||void 0,messages:r||void 0,onError:n||c,getMessageFallback:t||u}}export{a as I,s as a,g as b,d as c,m as d,E as e,u as f,c as g,h as i,S as r};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createContext as e,useContext as r,useMemo as
|
|
1
|
+
import{createContext as e,useContext as r,useMemo as t,useState as o,useEffect as n}from"react";import{d as a,b as s,i as c,r as i,I as m,a as f,e as u,c as l}from"./initializeConfig-CzP0yD8_.js";import{jsx as g}from"react/jsx-runtime";const d=e(void 0);function v({children:e,formats:o,getMessageFallback:n,locale:i,messages:m,now:f,onError:u,timeZone:l}){const v=r(d),w=t((()=>v?.cache||a()),[i,v?.cache]),h=t((()=>v?.formatters||s(w)),[w,v?.formatters]),p=t((()=>({...c({locale:i,formats:void 0===o?v?.formats:o,getMessageFallback:n||v?.getMessageFallback,messages:void 0===m?v?.messages:m,now:f||v?.now,onError:u||v?.onError,timeZone:l||v?.timeZone}),formatters:h,cache:w})),[w,o,h,n,i,m,f,u,v,l]);return g(d.Provider,{value:p,children:e})}function w(){const e=r(d);if(!e)throw new Error(void 0);return e}let h=!1;const p="undefined"==typeof window;function E(e){return function(e,r,o){const{cache:n,formats:a,formatters:s,getMessageFallback:c,locale:l,onError:g,timeZone:d}=w(),v=e[o],E=i(r,o);return d||h||!p||(h=!0,g(new m(f.ENVIRONMENT_FALLBACK,void 0))),t((()=>u({cache:n,formatters:s,getMessageFallback:c,messages:v,namespace:E,onError:g,formats:a,locale:l,timeZone:d})),[n,s,c,v,E,g,a,l,d])}({"!":w().messages},e?`!.${e}`:"!","!")}function Z(){return w().locale}function k(){return new Date}function b(e){const r=e?.updateInterval,{now:t}=w(),[a,s]=o(t||k());return n((()=>{if(!r)return;const e=setInterval((()=>{s(k())}),r);return()=>{clearInterval(e)}}),[t,r]),null==r&&t?t:a}function F(){return w().timeZone}function M(){const e=w();if(!e.messages)throw new Error(void 0);return e.messages}function I(){const{formats:e,formatters:r,locale:o,now:n,onError:a,timeZone:s}=w();return t((()=>l({formats:e,locale:o,now:n,onError:a,timeZone:s,_formatters:r})),[e,r,n,o,a,s])}function j(e,...r){let t,o;return"string"==typeof e?(t=r[0],o=r[1]):(t=e.values,o=e.formats),[void 0,t,o,void 0]}function x(e){const r=E(e);function t(...e){return r(...j(...e))}return t.rich=(...e)=>r.rich(...j(...e)),t.markup=(...e)=>r.markup(...j(...e)),t.has=function(e){return!0},t}export{v as IntlProvider,x as _useExtracted,I as useFormatter,Z as useLocale,M as useMessages,b as useNow,F as useTimeZone,E as useTranslations};
|
|
@@ -13,9 +13,9 @@ export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
|
|
|
13
13
|
messagesOrError: Messages | IntlError;
|
|
14
14
|
};
|
|
15
15
|
export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(config: Omit<CreateBaseTranslatorProps<Messages>, 'messagesOrError'>): {
|
|
16
|
-
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues, formats?: Formats): string;
|
|
17
|
-
rich: (key: string, values?: RichTranslationValues, formats?: Formats) => ReactNode;
|
|
18
|
-
markup(key: Parameters<(key: string, values?: RichTranslationValues, formats?: Formats) => ReactNode>[0], values: MarkupTranslationValues, formats?: Parameters<(key: string, values?: RichTranslationValues, formats?: Formats) => ReactNode>[2]): string;
|
|
16
|
+
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues, formats?: Formats, _fallback?: never): string;
|
|
17
|
+
rich: (key: string, values?: RichTranslationValues, formats?: Formats, _fallback?: never) => ReactNode;
|
|
18
|
+
markup(key: Parameters<(key: string, values?: RichTranslationValues, formats?: Formats, _fallback?: never) => ReactNode>[0], values: MarkupTranslationValues, formats?: Parameters<(key: string, values?: RichTranslationValues, formats?: Formats, _fallback?: never) => ReactNode>[2], _fallback?: never): string;
|
|
19
19
|
raw(key: string): any;
|
|
20
20
|
has(key: string): boolean;
|
|
21
21
|
};
|
|
@@ -13,7 +13,7 @@ type ICUArgsWithTags<MessageString extends string, TagsFn extends RichTagsFuncti
|
|
|
13
13
|
ICUDateArgument: Date;
|
|
14
14
|
}> & ([TagsFn] extends [never] ? {} : ICUTags<MessageString, TagsFn>);
|
|
15
15
|
type OnlyOptional<T> = Partial<T> extends T ? true : false;
|
|
16
|
-
type TranslateArgs<Value extends string, TagsFn extends RichTagsFunction | MarkupTagsFunction = never> = string extends Value ? [
|
|
16
|
+
export type TranslateArgs<Value extends string, TagsFn extends RichTagsFunction | MarkupTagsFunction = never> = string extends Value ? [
|
|
17
17
|
values?: Record<string, TranslationValues[string] | TagsFn>,
|
|
18
18
|
formats?: Formats
|
|
19
19
|
] : (Value extends any ? (key: ICUArgsWithTags<Value, TagsFn>) => void : never) extends (key: infer Args) => void ? OnlyOptional<Args> extends true ? [values?: undefined, formats?: Formats] : [values: Prettify<Args>, formats?: Formats] : never;
|
|
@@ -30,6 +30,16 @@ type NamespacedMessageKeys<TranslatorMessages extends IntlMessages, Namespace ex
|
|
|
30
30
|
type NamespacedValue<TranslatorMessages extends IntlMessages, Namespace extends NamespaceKeys<TranslatorMessages, NestedKeyOf<TranslatorMessages>>, TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>> = NestedValueOf<TranslatorMessages, [
|
|
31
31
|
Namespace
|
|
32
32
|
] extends [never] ? TargetKey : `${Namespace}.${TargetKey}`>;
|
|
33
|
+
/**
|
|
34
|
+
* @private Not intended for direct use.
|
|
35
|
+
*/
|
|
36
|
+
export type Translator<TranslatorMessages extends IntlMessages = IntlMessages, Namespace extends NamespaceKeys<TranslatorMessages, NestedKeyOf<TranslatorMessages>> = never> = {
|
|
37
|
+
<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey, ...args: TranslateArgs<NamespacedValue<TranslatorMessages, Namespace, TargetKey>>): string;
|
|
38
|
+
rich<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey, ...args: TranslateArgs<NamespacedValue<TranslatorMessages, Namespace, TargetKey>, RichTagsFunction>): ReactNode;
|
|
39
|
+
markup<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey, ...args: TranslateArgs<NamespacedValue<TranslatorMessages, Namespace, TargetKey>, MarkupTagsFunction>): string;
|
|
40
|
+
raw<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey): any;
|
|
41
|
+
has<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey): boolean;
|
|
42
|
+
};
|
|
33
43
|
/**
|
|
34
44
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
35
45
|
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
@@ -45,11 +55,5 @@ export default function createTranslator<const TranslatorMessages extends IntlMe
|
|
|
45
55
|
_formatters?: Formatters;
|
|
46
56
|
/** @private */
|
|
47
57
|
_cache?: IntlCache;
|
|
48
|
-
}):
|
|
49
|
-
<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey, ...args: TranslateArgs<NamespacedValue<TranslatorMessages, Namespace, TargetKey>>): string;
|
|
50
|
-
rich<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey, ...args: TranslateArgs<NamespacedValue<TranslatorMessages, Namespace, TargetKey>, RichTagsFunction>): ReactNode;
|
|
51
|
-
markup<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey, ...args: TranslateArgs<NamespacedValue<TranslatorMessages, Namespace, TargetKey>, MarkupTagsFunction>): string;
|
|
52
|
-
raw<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey): any;
|
|
53
|
-
has<TargetKey extends NamespacedMessageKeys<TranslatorMessages, Namespace>>(key: TargetKey): boolean;
|
|
54
|
-
};
|
|
58
|
+
}): Translator<TranslatorMessages, Namespace>;
|
|
55
59
|
export {};
|
|
@@ -9,9 +9,9 @@ export type CreateTranslatorImplProps<Messages> = Omit<InitializedIntlConfig, 'm
|
|
|
9
9
|
cache: IntlCache;
|
|
10
10
|
};
|
|
11
11
|
export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ messages, namespace, ...rest }: CreateTranslatorImplProps<Messages>, namespacePrefix: string): {
|
|
12
|
-
<TargetKey extends import("./MessageKeys.js").MessageKeys<import("./MessageKeys.js").NestedValueOf<Messages, NestedKey>, NestedKeyOf<import("./MessageKeys.js").NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: import("./TranslationValues.js").TranslationValues, formats?: import("./Formats.js").default): string;
|
|
13
|
-
rich: (key: string, values?: import("./TranslationValues.js").RichTranslationValues, formats?: import("./Formats.js").default) => import("react").ReactNode;
|
|
14
|
-
markup(key: Parameters<(key: string, values?: import("./TranslationValues.js").RichTranslationValues, formats?: import("./Formats.js").default) => import("react").ReactNode>[0], values: import("./TranslationValues.js").MarkupTranslationValues, formats?: Parameters<(key: string, values?: import("./TranslationValues.js").RichTranslationValues, formats?: import("./Formats.js").default) => import("react").ReactNode>[2]): string;
|
|
12
|
+
<TargetKey extends import("./MessageKeys.js").MessageKeys<import("./MessageKeys.js").NestedValueOf<Messages, NestedKey>, NestedKeyOf<import("./MessageKeys.js").NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: import("./TranslationValues.js").TranslationValues, formats?: import("./Formats.js").default, _fallback?: never): string;
|
|
13
|
+
rich: (key: string, values?: import("./TranslationValues.js").RichTranslationValues, formats?: import("./Formats.js").default, _fallback?: never) => import("react").ReactNode;
|
|
14
|
+
markup(key: Parameters<(key: string, values?: import("./TranslationValues.js").RichTranslationValues, formats?: import("./Formats.js").default, _fallback?: never) => import("react").ReactNode>[0], values: import("./TranslationValues.js").MarkupTranslationValues, formats?: Parameters<(key: string, values?: import("./TranslationValues.js").RichTranslationValues, formats?: import("./Formats.js").default, _fallback?: never) => import("react").ReactNode>[2], _fallback?: never): string;
|
|
15
15
|
raw(key: string): any;
|
|
16
16
|
has(key: string): boolean;
|
|
17
17
|
};
|
|
@@ -18,3 +18,5 @@ export type { default as RelativeTimeFormatOptions } from './RelativeTimeFormatO
|
|
|
18
18
|
export type { default as Timezone } from './TimeZone.js';
|
|
19
19
|
export type { default as ICUArgs } from './ICUArgs.js';
|
|
20
20
|
export type { default as ICUTags } from './ICUTags.js';
|
|
21
|
+
/** @private -- Only for type portability */
|
|
22
|
+
export type { Translator as _Translator } from './createTranslator.js';
|
|
@@ -5,3 +5,5 @@ export { default as useNow } from './useNow.js';
|
|
|
5
5
|
export { default as useTimeZone } from './useTimeZone.js';
|
|
6
6
|
export { default as useMessages } from './useMessages.js';
|
|
7
7
|
export { default as useFormatter } from './useFormatter.js';
|
|
8
|
+
/** @private -- Only for usage in `next-intl` currently */
|
|
9
|
+
export { default as _useExtracted } from './useExtracted.js';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { MarkupTagsFunction, RichTagsFunction } from '../core/TranslationValues.js';
|
|
3
|
+
import type { TranslateArgs } from '../core/createTranslator.js';
|
|
4
|
+
type TranslateArgsObject<Value extends string, TagsFn extends RichTagsFunction | MarkupTagsFunction = never> = TranslateArgs<Value, TagsFn> extends readonly [any?, any?] ? undefined extends TranslateArgs<Value, TagsFn>[0] ? {
|
|
5
|
+
values?: TranslateArgs<Value, TagsFn>[0];
|
|
6
|
+
formats?: TranslateArgs<Value, TagsFn>[1];
|
|
7
|
+
} : {
|
|
8
|
+
values: TranslateArgs<Value, TagsFn>[0];
|
|
9
|
+
formats?: TranslateArgs<Value, TagsFn>[1];
|
|
10
|
+
} : never;
|
|
11
|
+
export default function useExtracted(namespace?: string): {
|
|
12
|
+
<Message extends string>(message: Message, ...[values, formats]: TranslateArgs<Message>): string;
|
|
13
|
+
<Message extends string>(params: {
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Inline ICU message in the source locale. */
|
|
16
|
+
message: Message;
|
|
17
|
+
/** Description for translators and tooling. */
|
|
18
|
+
description?: string;
|
|
19
|
+
} & TranslateArgsObject<Message>): string;
|
|
20
|
+
rich: {
|
|
21
|
+
<Message extends string>(message: Message, ...[values, formats]: TranslateArgs<Message, RichTagsFunction>): ReactNode;
|
|
22
|
+
<Message extends string>(params: {
|
|
23
|
+
id?: string;
|
|
24
|
+
/** Inline ICU message in the source locale. */
|
|
25
|
+
message: Message;
|
|
26
|
+
/** Description for translators and tooling. */
|
|
27
|
+
description?: string;
|
|
28
|
+
} & TranslateArgsObject<Message, RichTagsFunction>): ReactNode;
|
|
29
|
+
};
|
|
30
|
+
markup: {
|
|
31
|
+
<Message extends string>(message: Message, ...[values, formats]: TranslateArgs<Message, MarkupTagsFunction>): string;
|
|
32
|
+
<Message extends string>(params: {
|
|
33
|
+
id?: string;
|
|
34
|
+
/** Inline ICU message in the source locale. */
|
|
35
|
+
message: Message;
|
|
36
|
+
/** Description for translators and tooling. */
|
|
37
|
+
description?: string;
|
|
38
|
+
} & TranslateArgsObject<Message, MarkupTagsFunction>): string;
|
|
39
|
+
};
|
|
40
|
+
has<Message extends string>(message: Message): boolean;
|
|
41
|
+
};
|
|
42
|
+
export {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type AbstractIntlMessages from '../core/AbstractIntlMessages.js';
|
|
2
2
|
import type { NestedKeyOf } from '../core/MessageKeys.js';
|
|
3
3
|
export default function useTranslationsImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(allMessagesPrefixed: Messages, namespacePrefixed: NestedKey, namespacePrefix: string): {
|
|
4
|
-
<TargetKey extends unknown>(key: TargetKey, values?: import("../core.js").TranslationValues, formats?: import("../core.js").Formats): string;
|
|
5
|
-
rich: (key: string, values?: import("../core.js").RichTranslationValues, formats?: import("../core.js").Formats) => import("react").ReactNode;
|
|
6
|
-
markup(key: Parameters<(key: string, values?: import("../core.js").RichTranslationValues, formats?: import("../core.js").Formats) => import("react").ReactNode>[0], values: import("../core.js").MarkupTranslationValues, formats?: Parameters<(key: string, values?: import("../core.js").RichTranslationValues, formats?: import("../core.js").Formats) => import("react").ReactNode>[2]): string;
|
|
4
|
+
<TargetKey extends unknown>(key: TargetKey, values?: import("../core.js").TranslationValues, formats?: import("../core.js").Formats, _fallback?: never): string;
|
|
5
|
+
rich: (key: string, values?: import("../core.js").RichTranslationValues, formats?: import("../core.js").Formats, _fallback?: never) => import("react").ReactNode;
|
|
6
|
+
markup(key: Parameters<(key: string, values?: import("../core.js").RichTranslationValues, formats?: import("../core.js").Formats, _fallback?: never) => import("react").ReactNode>[0], values: import("../core.js").MarkupTranslationValues, formats?: Parameters<(key: string, values?: import("../core.js").RichTranslationValues, formats?: import("../core.js").Formats, _fallback?: never) => import("react").ReactNode>[2], _fallback?: never): string;
|
|
7
7
|
raw(key: string): any;
|
|
8
8
|
has(key: string): boolean;
|
|
9
9
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-intl",
|
|
3
|
-
"version": "0.0.0-canary-
|
|
3
|
+
"version": "0.0.0-canary-2b3a9ce",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "Jan Amann <jan@amann.work>",
|
|
6
6
|
"description": "Internationalization (i18n) for React",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"homepage": "https://next-intl.dev",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/amannn/next-intl
|
|
11
|
+
"url": "git+https://github.com/amannn/next-intl.git"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "rm -rf dist && rollup -c",
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"react": "^17.0.0 || ^18.0.0 || >=19.0.0-rc <19.0.0 || ^19.0.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "d97078ef204ddacf1742f9526d38c50794baef1d"
|
|
68
68
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{IntlMessageFormat as e}from"intl-messageformat";import{isValidElement as t,cloneElement as r}from"react";import{memoize as n,strategies as o}from"@formatjs/fast-memoize";class a extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),this.code=e,t&&(this.originalMessage=t)}}var s=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}(s||{});function i(...e){return e.filter(Boolean).join(".")}function u(e){return i(e.namespace,e.key)}function c(e){console.error(e)}function m(){return{dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}}}function f(e,t){return n(e,{cache:(r=t,{create:()=>({get:e=>r[e],set(e,t){r[e]=t}})}),strategy:o.variadic});var r}function l(e,t){return f(((...t)=>new e(...t)),t)}function g(e){return{getDateTimeFormat:l(Intl.DateTimeFormat,e.dateTime),getNumberFormat:l(Intl.NumberFormat,e.number),getPluralRules:l(Intl.PluralRules,e.pluralRules),getRelativeTimeFormat:l(Intl.RelativeTimeFormat,e.relativeTime),getListFormat:l(Intl.ListFormat,e.list),getDisplayNames:l(Intl.DisplayNames,e.displayNames)}}function I(e,t,r,n){const o=i(n,r);if(!t)throw new Error(o);let a=t;return r.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(o+` (${e})`);a=r})),a}function E(n){const o=function(e,t,r,n=c){try{if(!t)throw new Error(void 0);const n=r?I(e,t,r):t;if(!n)throw new Error(r);return n}catch(e){const t=new a(s.MISSING_MESSAGE,e.message);return n(t),t}}(n.locale,n.messages,n.namespace,n.onError);return function({cache:n,formats:o,formatters:i,getMessageFallback:c=u,locale:m,messagesOrError:l,namespace:g,onError:E,timeZone:S}){const T=l instanceof a;function N(e,t,r){const n=new a(t,r);return E(n),c({error:n,key:e,namespace:g})}function y(a,u,E){if(T)return c({error:l,key:a,namespace:g});const y=l;let A,M;try{A=I(m,y,a,g)}catch(e){return N(a,s.MISSING_MESSAGE,e.message)}if("object"==typeof A){let e,t;return e=Array.isArray(A)?s.INVALID_MESSAGE:s.INSUFFICIENT_PATH,N(a,e,t)}const F=function(e,t){return t?void 0:e}(A,u);if(F)return F;i.getMessageFormat||(i.getMessageFormat=function(t,r){return f(((...t)=>new e(t[0],t[1],t[2],{formatters:r,...t[3]})),t.message)}(n,i));try{M=i.getMessageFormat(A,m,function(t,r,n){const o=e.formats.date,a=e.formats.time,s={...t?.dateTime,...r?.dateTime},i={date:{...o,...s},time:{...a,...s},number:{...t?.number,...r?.number}};return n&&["date","time"].forEach((e=>{const t=i[e];for(const[e,r]of Object.entries(t))t[e]={timeZone:n,...r}})),i}(o,E,S),{formatters:{...i,getDateTimeFormat:(e,t)=>i.getDateTimeFormat(e,{timeZone:S,...t})}})}catch(e){const t=e;return N(a,s.INVALID_MESSAGE,t.message)}try{const e=M.format(u?function(e){const n={};return Object.keys(e).forEach((o=>{let a=0;const s=e[o];let i;i="function"==typeof s?e=>{const n=s(e);return t(n)?r(n,{key:o+a++}):n}:s,n[o]=i})),n}(u):u);if(null==e)throw new Error(void 0);return t(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(e){return N(a,s.FORMATTING_ERROR,e.message)}}function A(e,t,r){const n=y(e,t,r);return"string"!=typeof n?N(e,s.INVALID_MESSAGE,void 0):n}return A.rich=y,A.markup=(e,t,r)=>y(e,t,r),A.raw=e=>{if(T)return c({error:l,key:e,namespace:g});const t=l;try{return I(m,t,e,g)}catch(t){return N(e,s.MISSING_MESSAGE,t.message)}},A.has=e=>{if(T)return!1;try{return I(m,l,e,g),!0}catch{return!1}},A}({...n,messagesOrError:o})}function S(e,t){return e===t?void 0:e.slice((t+".").length)}const T=3600,N=86400,y=7*N,A=2628e3,M=7884e3,F=365*N,R={second:1,seconds:1,minute:60,minutes:60,hour:T,hours:T,day:N,days:N,week:y,weeks:y,month:A,months:A,quarter:M,quarters:M,year:F,years:F};function d(e){const{_cache:t=m(),_formatters:r=g(t),formats:n,locale:o,onError:i=c,timeZone:u}=e;function f(e){return e?.timeZone||(u?e={...e,timeZone:u}:i(new a(s.ENVIRONMENT_FALLBACK,void 0))),e}function l(e,t,r,n,o){let u;try{u=function(e,t,r){let n;if("string"==typeof t){const r=t;if(n=e?.[r],!n){const e=new a(s.MISSING_FORMAT,void 0);throw i(e),e}}else n=t;return r&&(n={...n,...r}),n}(r,e,t)}catch{return o()}try{return n(u)}catch(e){return i(new a(s.FORMATTING_ERROR,e.message)),o()}}function I(e,t,a){return l(t,a,n?.dateTime,(t=>(t=f(t),r.getDateTimeFormat(o,t).format(e))),(()=>String(e)))}function E(){return e.now?e.now:(i(new a(s.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:I,number:function(e,t,a){return l(t,a,n?.number,(t=>r.getNumberFormat(o,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let n,a;const s={};t instanceof Date||"number"==typeof t?n=new Date(t):t&&(n=null!=t.now?new Date(t.now):E(),a=t.unit,s.style=t.style,s.numberingSystem=t.numberingSystem),n||(n=E());const i=(new Date(e).getTime()-n.getTime())/1e3;a||(a=function(e){const t=Math.abs(e);return t<60?"second":t<T?"minute":t<N?"hour":t<y?"day":t<A?"week":t<F?"month":"year"}(i)),s.numeric="second"===a?"auto":"always";const u=function(e,t){return Math.round(e/R[t])}(i,a);return r.getRelativeTimeFormat(o,s).format(u,a)}catch(t){return i(new a(s.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,t,a){const s=[],i=new Map;let u=0;for(const t of e){let e;"object"==typeof t?(e=String(u),i.set(e,t)):e=String(t),s.push(e),u++}return l(t,a,n?.list,(e=>{const t=r.getListFormat(o,e).formatToParts(s).map((e=>"literal"===e.type?e.value:i.get(e.value)||e.value));return i.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,t,a,s){return l(a,s,n?.dateTime,(n=>(n=f(n),r.getDateTimeFormat(o,n).formatRange(e,t))),(()=>[I(e),I(t)].join(" – ")))}}}function h({formats:e,getMessageFallback:t,messages:r,onError:n,...o}){return{...o,formats:e||void 0,messages:r||void 0,onError:n||c,getMessageFallback:t||u}}export{a as I,s as a,g as b,d as c,m as d,E as e,u as f,c as g,h as i,S as r};
|