use-intl 2.3.5 → 2.4.1
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 +5 -5
- package/dist/IntlContext.d.ts +2 -0
- package/dist/IntlProvider.d.ts +5 -0
- package/dist/use-intl.cjs.development.js +4 -3
- package/dist/use-intl.cjs.development.js.map +1 -1
- package/dist/use-intl.cjs.production.min.js +1 -1
- package/dist/use-intl.cjs.production.min.js.map +1 -1
- package/dist/use-intl.esm.js +4 -3
- package/dist/use-intl.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/IntlContext.tsx +2 -0
- package/src/IntlProvider.tsx +5 -0
- package/src/useTranslations.tsx +6 -4
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# use-intl
|
|
1
|
+
# 🌐 use-intl
|
|
2
2
|
|
|
3
3
|
  
|
|
4
4
|
|
|
@@ -46,9 +46,9 @@ function LatestFollower({user}) {
|
|
|
46
46
|
```jsx
|
|
47
47
|
import {IntlProvider} from 'use-intl';
|
|
48
48
|
|
|
49
|
-
// You can get the messages from anywhere you like. You can also
|
|
50
|
-
// them from within a component and then render the provider
|
|
51
|
-
// your app once you have the messages.
|
|
49
|
+
// You can get the messages from anywhere you like. You can also
|
|
50
|
+
// fetch them from within a component and then render the provider
|
|
51
|
+
// along with your app once you have the messages.
|
|
52
52
|
const messages = {
|
|
53
53
|
App: {
|
|
54
54
|
hello: 'Hello'
|
|
@@ -59,7 +59,7 @@ ReactDOM.render(
|
|
|
59
59
|
<IntlProvider messages={messages} locale="en">
|
|
60
60
|
<App />
|
|
61
61
|
</IntlProvider>
|
|
62
|
-
)
|
|
62
|
+
);
|
|
63
63
|
```
|
|
64
64
|
3. Based on the features you need and the browsers you support, you might have to provide [polyfills](https://formatjs.io/docs/polyfills).
|
|
65
65
|
4. Use translations in your components!
|
package/dist/IntlContext.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlError from './IntlError';
|
|
4
4
|
import IntlMessages from './IntlMessages';
|
|
5
|
+
import { RichTranslationValues } from './TranslationValues';
|
|
5
6
|
export declare type IntlContextShape = {
|
|
6
7
|
messages?: IntlMessages;
|
|
7
8
|
locale: string;
|
|
@@ -14,6 +15,7 @@ export declare type IntlContextShape = {
|
|
|
14
15
|
namespace?: string;
|
|
15
16
|
}): string;
|
|
16
17
|
now?: Date;
|
|
18
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
17
19
|
};
|
|
18
20
|
declare const IntlContext: import("react").Context<IntlContextShape | undefined>;
|
|
19
21
|
export default IntlContext;
|
package/dist/IntlProvider.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlMessages from './IntlMessages';
|
|
4
|
+
import { RichTranslationValues } from './TranslationValues';
|
|
4
5
|
import { IntlError } from '.';
|
|
5
6
|
declare type Props = {
|
|
6
7
|
/** All messages that will be available in your components. */
|
|
@@ -37,6 +38,10 @@ declare type Props = {
|
|
|
37
38
|
* afterwards the current date will be returned continuously.
|
|
38
39
|
*/
|
|
39
40
|
now?: Date;
|
|
41
|
+
/** Global default values for translation values and rich text elements.
|
|
42
|
+
* Can be used for consistent usage or styling of rich text elements.
|
|
43
|
+
* Defaults will be overidden by locally provided values. */
|
|
44
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
40
45
|
};
|
|
41
46
|
export default function IntlProvider({ children, onError, getMessageFallback, ...contextValues }: Props): JSX.Element;
|
|
42
47
|
export {};
|
|
@@ -253,7 +253,7 @@ function resolvePath(messages, idPath, namespace) {
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
function prepareTranslationValues(values) {
|
|
256
|
-
if (
|
|
256
|
+
if (Object.keys(values).length === 0) return undefined; // Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
257
257
|
|
|
258
258
|
var transformedValues = {};
|
|
259
259
|
Object.keys(values).forEach(function (key) {
|
|
@@ -288,6 +288,7 @@ function prepareTranslationValues(values) {
|
|
|
288
288
|
|
|
289
289
|
function useTranslations(namespace) {
|
|
290
290
|
var _useIntlContext = useIntlContext(),
|
|
291
|
+
defaultTranslationValues = _useIntlContext.defaultTranslationValues,
|
|
291
292
|
globalFormats = _useIntlContext.formats,
|
|
292
293
|
getMessageFallback = _useIntlContext.getMessageFallback,
|
|
293
294
|
locale = _useIntlContext.locale,
|
|
@@ -381,7 +382,7 @@ function useTranslations(namespace) {
|
|
|
381
382
|
}
|
|
382
383
|
|
|
383
384
|
try {
|
|
384
|
-
var formattedMessage = messageFormat.format(prepareTranslationValues(values));
|
|
385
|
+
var formattedMessage = messageFormat.format(prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
385
386
|
|
|
386
387
|
if (formattedMessage == null) {
|
|
387
388
|
throw new Error("development" !== "production" ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
@@ -435,7 +436,7 @@ function useTranslations(namespace) {
|
|
|
435
436
|
};
|
|
436
437
|
|
|
437
438
|
return translateFn;
|
|
438
|
-
}, [getMessageFallback, globalFormats, locale, messagesOrError, namespace, onError, timeZone]);
|
|
439
|
+
}, [getMessageFallback, globalFormats, locale, messagesOrError, namespace, onError, timeZone, defaultTranslationValues]);
|
|
439
440
|
return translate;
|
|
440
441
|
}
|
|
441
442
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.cjs.development.js","sources":["../src/IntlContext.tsx","../src/IntlProvider.tsx","../src/IntlError.tsx","../src/convertFormatsToIntlMessageFormat.tsx","../src/useIntlContext.tsx","../src/useTranslations.tsx","../src/useIntl.tsx","../src/useLocale.tsx","../src/useNow.tsx","../src/useTimeZone.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport Formats from './Formats';\nimport IntlError from './IntlError';\nimport IntlMessages from './IntlMessages';\n\nexport type IntlContextShape = {\n messages?: IntlMessages;\n locale: string;\n formats?: Partial<Formats>;\n timeZone?: string;\n onError(error: IntlError): void;\n getMessageFallback(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n now?: Date;\n};\n\nconst IntlContext = createContext<IntlContextShape | undefined>(undefined);\n\nexport default IntlContext;\n","import React, {ReactNode} from 'react';\nimport Formats from './Formats';\nimport IntlContext from './IntlContext';\nimport IntlMessages from './IntlMessages';\nimport {IntlError} from '.';\n\ntype Props = {\n /** All messages that will be available in your components. */\n messages?: IntlMessages;\n /** A valid Unicode locale tag (e.g. \"en\" or \"en-GB\"). */\n locale: string;\n /** Global formats can be provided to achieve consistent\n * formatting across components. */\n formats?: Partial<Formats>;\n /** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */\n timeZone?: string;\n /** This callback will be invoked when an error is encountered during\n * resolving a message or formatting it. This defaults to `console.error` to\n * keep your app running. You can customize the handling by taking\n * `error.code` into account. */\n onError?(error: IntlError): void;\n /** Will be called when a message couldn't be resolved or formatting it led to\n * an error. This defaults to `${namespace}.${key}` You can use this to\n * customize what will be rendered in this case. */\n getMessageFallback?(info: {\n namespace?: string;\n key: string;\n error: IntlError;\n }): string;\n /** All components that use the provided hooks should be within this tree. */\n children: ReactNode;\n /**\n * Providing this value will have two effects:\n * 1. It will be used as the default for the `now` argument of\n * `useIntl().formatRelativeTime` if no explicit value is provided.\n * 2. It will be returned as a static value from the `useNow` hook. Note\n * however that when `updateInterval` is configured on the `useNow` hook,\n * the global `now` value will only be used for the initial render, but\n * afterwards the current date will be returned continuously.\n */\n now?: Date;\n};\n\nfunction defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nfunction defaultOnError(error: IntlError) {\n console.error(error);\n}\n\nexport default function IntlProvider({\n children,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...contextValues\n}: Props) {\n return (\n <IntlContext.Provider\n value={{...contextValues, onError, getMessageFallback}}\n >\n {children}\n </IntlContext.Provider>\n );\n}\n","export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n","import {Formats as IntlFormats} from 'intl-messageformat';\nimport DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\n\nfunction setTimeZoneInFormats(\n formats: Record<string, DateTimeFormatOptions> | undefined,\n timeZone: string\n) {\n if (!formats) return formats;\n\n // The only way to set a time zone with `intl-messageformat` is to merge it into the formats\n // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15\n return Object.keys(formats).reduce(\n (acc: Record<string, DateTimeFormatOptions>, key) => {\n acc[key] = {\n timeZone,\n ...formats[key]\n };\n return acc;\n },\n {}\n );\n}\n\n/**\n * `intl-messageformat` uses separate keys for `date` and `time`, but there's\n * only one native API: `Intl.DateTimeFormat`. Additionally you might want to\n * include both a time and a date in a value, therefore the separation doesn't\n * seem so useful. We offer a single `dateTime` namespace instead, but we have\n * to convert the format before `intl-messageformat` can be used.\n */\nexport default function convertFormatsToIntlMessageFormat(\n formats: Partial<Formats>,\n timeZone?: string\n): Partial<IntlFormats> {\n const formatsWithTimeZone = timeZone\n ? {...formats, dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)}\n : formats;\n\n return {\n ...formatsWithTimeZone,\n date: formatsWithTimeZone?.dateTime,\n time: formatsWithTimeZone?.dateTime\n };\n}\n","import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n __DEV__\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n","import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray,\n useMemo,\n useRef\n} from 'react';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport useIntlContext from './useIntlContext';\n\nfunction resolvePath(\n messages: IntlMessages | undefined,\n idPath: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n __DEV__ ? `No messages available at \\`${namespace}\\`.` : undefined\n );\n }\n\n let message = messages;\n\n idPath.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n __DEV__\n ? `Could not resolve \\`${idPath}\\` 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 (!values) return values;\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 = (children: ReactNode) => {\n const result = value(children);\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\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations(namespace?: string) {\n const {\n formats: globalFormats,\n getMessageFallback,\n locale,\n messages: allMessages,\n onError,\n timeZone\n } = useIntlContext();\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(() => {\n try {\n if (!allMessages) {\n throw new Error(\n __DEV__ ? `No messages were configured on the provider.` : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(allMessages, namespace)\n : allMessages;\n\n if (!retrievedMessages) {\n throw new Error(\n __DEV__\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 }, [allMessages, namespace, onError]);\n\n const translate = useMemo(() => {\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 const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;\n\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 const cacheKey = [namespace, key]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale[locale][cacheKey];\n } else {\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 if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n __DEV__\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n\n try {\n const formattedMessage = messageFormat.format(\n prepareTranslationValues(values)\n );\n\n if (formattedMessage == null) {\n throw new Error(\n __DEV__\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 /** 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?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const message = translateBaseFn(key, values, formats);\n\n if (typeof message !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n __DEV__\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 message;\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 getMessageFallback,\n globalFormats,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n ]);\n\n return translate;\n}\n","import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport useIntlContext from './useIntlContext';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | Intl.NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n","import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["IntlContext","createContext","undefined","defaultGetMessageFallback","key","namespace","filter","part","join","defaultOnError","error","console","IntlProvider","children","onError","getMessageFallback","contextValues","React","Provider","value","IntlErrorCode","IntlError","code","originalMessage","message","Error","setTimeZoneInFormats","formats","timeZone","Object","keys","reduce","acc","convertFormatsToIntlMessageFormat","formatsWithTimeZone","dateTime","date","time","useIntlContext","context","useContext","resolvePath","messages","idPath","split","forEach","next","prepareTranslationValues","values","transformedValues","index","transformed","result","isValidElement","cloneElement","useTranslations","globalFormats","locale","allMessages","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","retrievedMessages","intlError","MISSING_MESSAGE","translate","getFallbackFromErrorAndNotify","translateBaseFn","cachedFormatsByLocale","current","cacheKey","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw","MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","unit","round","useIntl","globalNow","now","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","MISSING_FORMAT","getFormattedValue","formatter","formatDateTime","Intl","DateTimeFormat","formatNumber","number","NumberFormat","formatRelativeTime","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric","useLocale","getNow","useNow","updateInterval","useState","setNow","useEffect","intervalId","setInterval","clearInterval","useTimeZone"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,IAAMA,WAAW,gBAAGC,mBAAa,CAA+BC,SAA/B,CAAjC;;ACwBA,SAASC,yBAAT;MACEC,WAAAA;MACAC,iBAAAA;AAKA,SAAO,CAACA,SAAD,EAAYD,GAAZ,EAAiBE,MAAjB,CAAwB,UAACC,IAAD;AAAA,WAAUA,IAAI,IAAI,IAAlB;AAAA,GAAxB,EAAgDC,IA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,CAAP;AAGA,aAAOwD,MAAM,CAAC5C,IAAD,CAAb;AACD;AACF;;AAED,SAAO;AAACyE,IAAAA,cAAc,EAAdA,cAAD;AAAiBG,IAAAA,YAAY,EAAZA,YAAjB;AAA+BG,IAAAA,kBAAkB,EAAlBA;AAA/B,GAAP;AACD;;SCpKuBO;AACtB,SAAOpF,cAAc,GAAGmB,MAAxB;AACD;;ACGD,SAASkE,MAAT;AACE,SAAO,IAAIN,IAAJ,EAAP;AACD;AAED;;;;;;;;;;;;;;;;;;;;AAkBA,SAAwBO,OAAOpB;AAC7B,MAAMqB,cAAc,GAAGrB,OAAH,oBAAGA,OAAO,CAAEqB,cAAhC;;wBAEyBvF,cAAc;MAA3B6D,4BAALC;;kBACe0B,cAAQ,CAAC3B,SAAS,IAAIwB,MAAM,EAApB;MAAvBvB;MAAK2B;;AAEZC,EAAAA,eAAS,CAAC;AACR,QAAI,CAACH,cAAL,EAAqB;AAErB,QAAMI,UAAU,GAAGC,WAAW,CAAC;AAC7BH,MAAAA,MAAM,CAACJ,MAAM,EAAP,CAAN;AACD,KAF6B,EAE3BE,cAF2B,CAA9B;AAIA,WAAO;AACLM,MAAAA,aAAa,CAACF,UAAD,CAAb;AACD,KAFD;AAGD,GAVQ,EAUN,CAAC9B,SAAD,EAAY0B,cAAZ,CAVM,CAAT;AAYA,SAAOzB,GAAP;AACD;;SC9CuBgC;AACtB,SAAO9F,cAAc,GAAGV,QAAxB;AACD;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.cjs.development.js","sources":["../src/IntlContext.tsx","../src/IntlProvider.tsx","../src/IntlError.tsx","../src/convertFormatsToIntlMessageFormat.tsx","../src/useIntlContext.tsx","../src/useTranslations.tsx","../src/useIntl.tsx","../src/useLocale.tsx","../src/useNow.tsx","../src/useTimeZone.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport Formats from './Formats';\nimport IntlError from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport {RichTranslationValues} from './TranslationValues';\n\nexport type IntlContextShape = {\n messages?: IntlMessages;\n locale: string;\n formats?: Partial<Formats>;\n timeZone?: string;\n onError(error: IntlError): void;\n getMessageFallback(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n now?: Date;\n defaultTranslationValues?: RichTranslationValues;\n};\n\nconst IntlContext = createContext<IntlContextShape | undefined>(undefined);\n\nexport default IntlContext;\n","import React, {ReactNode} from 'react';\nimport Formats from './Formats';\nimport IntlContext from './IntlContext';\nimport IntlMessages from './IntlMessages';\nimport {RichTranslationValues} from './TranslationValues';\nimport {IntlError} from '.';\n\ntype Props = {\n /** All messages that will be available in your components. */\n messages?: IntlMessages;\n /** A valid Unicode locale tag (e.g. \"en\" or \"en-GB\"). */\n locale: string;\n /** Global formats can be provided to achieve consistent\n * formatting across components. */\n formats?: Partial<Formats>;\n /** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */\n timeZone?: string;\n /** This callback will be invoked when an error is encountered during\n * resolving a message or formatting it. This defaults to `console.error` to\n * keep your app running. You can customize the handling by taking\n * `error.code` into account. */\n onError?(error: IntlError): void;\n /** Will be called when a message couldn't be resolved or formatting it led to\n * an error. This defaults to `${namespace}.${key}` You can use this to\n * customize what will be rendered in this case. */\n getMessageFallback?(info: {\n namespace?: string;\n key: string;\n error: IntlError;\n }): string;\n /** All components that use the provided hooks should be within this tree. */\n children: ReactNode;\n /**\n * Providing this value will have two effects:\n * 1. It will be used as the default for the `now` argument of\n * `useIntl().formatRelativeTime` if no explicit value is provided.\n * 2. It will be returned as a static value from the `useNow` hook. Note\n * however that when `updateInterval` is configured on the `useNow` hook,\n * the global `now` value will only be used for the initial render, but\n * afterwards the current date will be returned continuously.\n */\n now?: Date;\n /** Global default values for translation values and rich text elements.\n * Can be used for consistent usage or styling of rich text elements.\n * Defaults will be overidden by locally provided values. */\n defaultTranslationValues?: RichTranslationValues;\n};\n\nfunction defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nfunction defaultOnError(error: IntlError) {\n console.error(error);\n}\n\nexport default function IntlProvider({\n children,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...contextValues\n}: Props) {\n return (\n <IntlContext.Provider\n value={{...contextValues, onError, getMessageFallback}}\n >\n {children}\n </IntlContext.Provider>\n );\n}\n","export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n","import {Formats as IntlFormats} from 'intl-messageformat';\nimport DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\n\nfunction setTimeZoneInFormats(\n formats: Record<string, DateTimeFormatOptions> | undefined,\n timeZone: string\n) {\n if (!formats) return formats;\n\n // The only way to set a time zone with `intl-messageformat` is to merge it into the formats\n // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15\n return Object.keys(formats).reduce(\n (acc: Record<string, DateTimeFormatOptions>, key) => {\n acc[key] = {\n timeZone,\n ...formats[key]\n };\n return acc;\n },\n {}\n );\n}\n\n/**\n * `intl-messageformat` uses separate keys for `date` and `time`, but there's\n * only one native API: `Intl.DateTimeFormat`. Additionally you might want to\n * include both a time and a date in a value, therefore the separation doesn't\n * seem so useful. We offer a single `dateTime` namespace instead, but we have\n * to convert the format before `intl-messageformat` can be used.\n */\nexport default function convertFormatsToIntlMessageFormat(\n formats: Partial<Formats>,\n timeZone?: string\n): Partial<IntlFormats> {\n const formatsWithTimeZone = timeZone\n ? {...formats, dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)}\n : formats;\n\n return {\n ...formatsWithTimeZone,\n date: formatsWithTimeZone?.dateTime,\n time: formatsWithTimeZone?.dateTime\n };\n}\n","import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n __DEV__\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n","import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray,\n useMemo,\n useRef\n} from 'react';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport useIntlContext from './useIntlContext';\n\nfunction resolvePath(\n messages: IntlMessages | undefined,\n idPath: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n __DEV__ ? `No messages available at \\`${namespace}\\`.` : undefined\n );\n }\n\n let message = messages;\n\n idPath.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n __DEV__\n ? `Could not resolve \\`${idPath}\\` 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 = (children: ReactNode) => {\n const result = value(children);\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\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations(namespace?: string) {\n const {\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback,\n locale,\n messages: allMessages,\n onError,\n timeZone\n } = useIntlContext();\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(() => {\n try {\n if (!allMessages) {\n throw new Error(\n __DEV__ ? `No messages were configured on the provider.` : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(allMessages, namespace)\n : allMessages;\n\n if (!retrievedMessages) {\n throw new Error(\n __DEV__\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 }, [allMessages, namespace, onError]);\n\n const translate = useMemo(() => {\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 const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;\n\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 const cacheKey = [namespace, key]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale[locale][cacheKey];\n } else {\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 if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n __DEV__\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n\n try {\n const formattedMessage = messageFormat.format(\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n __DEV__\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 /** 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?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const message = translateBaseFn(key, values, formats);\n\n if (typeof message !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n __DEV__\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 message;\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 getMessageFallback,\n globalFormats,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone,\n defaultTranslationValues\n ]);\n\n return translate;\n}\n","import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport useIntlContext from './useIntlContext';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | Intl.NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n","import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["IntlContext","createContext","undefined","defaultGetMessageFallback","key","namespace","filter","part","join","defaultOnError","error","console","IntlProvider","children","onError","getMessageFallback","contextValues","React","Provider","value","IntlErrorCode","IntlError","code","originalMessage","message","Error","setTimeZoneInFormats","formats","timeZone","Object","keys","reduce","acc","convertFormatsToIntlMessageFormat","formatsWithTimeZone","dateTime","date","time","useIntlContext","context","useContext","resolvePath","messages","idPath","split","forEach","next","prepareTranslationValues","values","length","transformedValues","index","transformed","result","isValidElement","cloneElement","useTranslations","defaultTranslationValues","globalFormats","locale","allMessages","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","retrievedMessages","intlError","MISSING_MESSAGE","translate","getFallbackFromErrorAndNotify","translateBaseFn","cachedFormatsByLocale","current","cacheKey","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw","MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","unit","round","useIntl","globalNow","now","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","MISSING_FORMAT","getFormattedValue","formatter","formatDateTime","Intl","DateTimeFormat","formatNumber","number","NumberFormat","formatRelativeTime","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric","useLocale","getNow","useNow","updateInterval","useState","setNow","useEffect","intervalId","setInterval","clearInterval","useTimeZone"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AGV,QAAxB;AACD;;;;;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=r(e),n=r(require("intl-messageformat"));function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function u(r){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function i(r,e){return(i=Object.setPrototypeOf||function(r,e){return r.__proto__=e,r})(r,e)}function a(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(r){return!1}}function c(r,e,t){return(c=a()?Reflect.construct:function(r,e,t){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(r,n));return t&&i(o,t.prototype),o}).apply(null,arguments)}function f(r){var e="function"==typeof Map?new Map:void 0;return(f=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(r))return e.get(r);e.set(r,t)}function t(){return c(r,arguments,u(this).constructor)}return t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),i(t,r)})(r)}var l,s=e.createContext(void 0);function p(r){return[r.namespace,r.key].filter((function(r){return null!=r})).join(".")}function v(r){console.error(r)}(l=exports.IntlErrorCode||(exports.IntlErrorCode={})).MISSING_MESSAGE="MISSING_MESSAGE",l.MISSING_FORMAT="MISSING_FORMAT",l.INSUFFICIENT_PATH="INSUFFICIENT_PATH",l.INVALID_MESSAGE="INVALID_MESSAGE",l.FORMATTING_ERROR="FORMATTING_ERROR";var d=function(r){var e,t;function n(e,t){var n,o=e;return t&&(o+=": "+t),(n=r.call(this,o)||this).code=e,t&&(n.originalMessage=t),n}return t=r,(e=n).prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t,n}(f(Error));function m(r,e){return r?Object.keys(r).reduce((function(t,n){return t[n]=o({timeZone:e},r[n]),t}),{}):r}function E(){var r=e.useContext(s);if(!r)throw new Error(void 0);return r}function I(r,e,t){if(!r)throw new Error(void 0);var n=r;return e.split(".").forEach((function(r){var e=n[r];if(null==r||null==e)throw new Error(void 0);n=e})),n}function y(){return new Date}exports.IntlError=d,exports.IntlProvider=function(r){var e=r.children,n=r.onError,u=void 0===n?v:n,i=r.getMessageFallback,a=void 0===i?p:i,c=function(r,e){if(null==r)return{};var t,n,o={},u=Object.keys(r);for(n=0;n<u.length;n++)e.indexOf(t=u[n])>=0||(o[t]=r[t]);return o}(r,["children","onError","getMessageFallback"]);return t.createElement(s.Provider,{value:o({},c,{onError:u,getMessageFallback:a})},e)},exports.useIntl=function(){var r=E(),e=r.formats,t=r.locale,n=r.now,u=r.onError,i=r.timeZone;function a(r,e,t,n){var o;try{o=function(r,e){var t;if("string"==typeof e){if(!(t=null==r?void 0:r[e])){var n=new d(exports.IntlErrorCode.MISSING_FORMAT,void 0);throw u(n),n}}else t=e;return t}(t,e)}catch(e){return String(r)}try{return n(o)}catch(e){return u(new d(exports.IntlErrorCode.FORMATTING_ERROR,e.message)),String(r)}}return{formatDateTime:function(r,n){return a(r,n,null==e?void 0:e.dateTime,(function(e){var n;return!i||null!=(n=e)&&n.timeZone||(e=o({},e,{timeZone:i})),new Intl.DateTimeFormat(t,e).format(r)}))},formatNumber:function(r,n){return a(r,n,null==e?void 0:e.number,(function(e){return new Intl.NumberFormat(t,e).format(r)}))},formatRelativeTime:function(r,e){try{if(!e){if(!n)throw new Error(void 0);e=n}var o=r instanceof Date?r:new Date(r),i=e instanceof Date?e:new Date(e),a=function(r){var e,t,n=Math.abs(r);return n<60?(t="second",e=Math.round(r)):n<3600?(t="minute",e=Math.round(r/60)):n<86400?(t="hour",e=Math.round(r/3600)):n<604800?(t="day",e=Math.round(r/86400)):n<2628e3?(t="week",e=Math.round(r/604800)):n<31536e3?(t="month",e=Math.round(r/2628e3)):(t="year",e=Math.round(r/31536e3)),{value:e,unit:t}}((o.getTime()-i.getTime())/1e3),c=a.unit,f=a.value;return new Intl.RelativeTimeFormat(t,{numeric:"auto"}).format(f,c)}catch(e){return u(new d(exports.IntlErrorCode.FORMATTING_ERROR,e.message)),String(r)}}}},exports.useLocale=function(){return E().locale},exports.useNow=function(r){var t=null==r?void 0:r.updateInterval,n=E().now,o=e.useState(n||y()),u=o[0],i=o[1];return e.useEffect((function(){if(t){var r=setInterval((function(){i(y())}),t);return function(){clearInterval(r)}}}),[n,t]),u},exports.useTimeZone=function(){return E().timeZone},exports.useTranslations=function(r){var t=E(),u=t.
|
|
1
|
+
"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=r(e),n=r(require("intl-messageformat"));function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function u(r){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function i(r,e){return(i=Object.setPrototypeOf||function(r,e){return r.__proto__=e,r})(r,e)}function a(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(r){return!1}}function c(r,e,t){return(c=a()?Reflect.construct:function(r,e,t){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(r,n));return t&&i(o,t.prototype),o}).apply(null,arguments)}function f(r){var e="function"==typeof Map?new Map:void 0;return(f=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(r))return e.get(r);e.set(r,t)}function t(){return c(r,arguments,u(this).constructor)}return t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),i(t,r)})(r)}var l,s=e.createContext(void 0);function p(r){return[r.namespace,r.key].filter((function(r){return null!=r})).join(".")}function v(r){console.error(r)}(l=exports.IntlErrorCode||(exports.IntlErrorCode={})).MISSING_MESSAGE="MISSING_MESSAGE",l.MISSING_FORMAT="MISSING_FORMAT",l.INSUFFICIENT_PATH="INSUFFICIENT_PATH",l.INVALID_MESSAGE="INVALID_MESSAGE",l.FORMATTING_ERROR="FORMATTING_ERROR";var d=function(r){var e,t;function n(e,t){var n,o=e;return t&&(o+=": "+t),(n=r.call(this,o)||this).code=e,t&&(n.originalMessage=t),n}return t=r,(e=n).prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t,n}(f(Error));function m(r,e){return r?Object.keys(r).reduce((function(t,n){return t[n]=o({timeZone:e},r[n]),t}),{}):r}function E(){var r=e.useContext(s);if(!r)throw new Error(void 0);return r}function I(r,e,t){if(!r)throw new Error(void 0);var n=r;return e.split(".").forEach((function(r){var e=n[r];if(null==r||null==e)throw new Error(void 0);n=e})),n}function y(){return new Date}exports.IntlError=d,exports.IntlProvider=function(r){var e=r.children,n=r.onError,u=void 0===n?v:n,i=r.getMessageFallback,a=void 0===i?p:i,c=function(r,e){if(null==r)return{};var t,n,o={},u=Object.keys(r);for(n=0;n<u.length;n++)e.indexOf(t=u[n])>=0||(o[t]=r[t]);return o}(r,["children","onError","getMessageFallback"]);return t.createElement(s.Provider,{value:o({},c,{onError:u,getMessageFallback:a})},e)},exports.useIntl=function(){var r=E(),e=r.formats,t=r.locale,n=r.now,u=r.onError,i=r.timeZone;function a(r,e,t,n){var o;try{o=function(r,e){var t;if("string"==typeof e){if(!(t=null==r?void 0:r[e])){var n=new d(exports.IntlErrorCode.MISSING_FORMAT,void 0);throw u(n),n}}else t=e;return t}(t,e)}catch(e){return String(r)}try{return n(o)}catch(e){return u(new d(exports.IntlErrorCode.FORMATTING_ERROR,e.message)),String(r)}}return{formatDateTime:function(r,n){return a(r,n,null==e?void 0:e.dateTime,(function(e){var n;return!i||null!=(n=e)&&n.timeZone||(e=o({},e,{timeZone:i})),new Intl.DateTimeFormat(t,e).format(r)}))},formatNumber:function(r,n){return a(r,n,null==e?void 0:e.number,(function(e){return new Intl.NumberFormat(t,e).format(r)}))},formatRelativeTime:function(r,e){try{if(!e){if(!n)throw new Error(void 0);e=n}var o=r instanceof Date?r:new Date(r),i=e instanceof Date?e:new Date(e),a=function(r){var e,t,n=Math.abs(r);return n<60?(t="second",e=Math.round(r)):n<3600?(t="minute",e=Math.round(r/60)):n<86400?(t="hour",e=Math.round(r/3600)):n<604800?(t="day",e=Math.round(r/86400)):n<2628e3?(t="week",e=Math.round(r/604800)):n<31536e3?(t="month",e=Math.round(r/2628e3)):(t="year",e=Math.round(r/31536e3)),{value:e,unit:t}}((o.getTime()-i.getTime())/1e3),c=a.unit,f=a.value;return new Intl.RelativeTimeFormat(t,{numeric:"auto"}).format(f,c)}catch(e){return u(new d(exports.IntlErrorCode.FORMATTING_ERROR,e.message)),String(r)}}}},exports.useLocale=function(){return E().locale},exports.useNow=function(r){var t=null==r?void 0:r.updateInterval,n=E().now,o=e.useState(n||y()),u=o[0],i=o[1];return e.useEffect((function(){if(t){var r=setInterval((function(){i(y())}),t);return function(){clearInterval(r)}}}),[n,t]),u},exports.useTimeZone=function(){return E().timeZone},exports.useTranslations=function(r){var t=E(),u=t.defaultTranslationValues,i=t.formats,a=t.getMessageFallback,c=t.locale,f=t.messages,l=t.onError,s=t.timeZone,p=e.useRef({}),v=e.useMemo((function(){try{if(!f)throw new Error(void 0);var e=r?I(f,r):f;if(!e)throw new Error(void 0);return e}catch(r){var t=new d(exports.IntlErrorCode.MISSING_MESSAGE,r.message);return l(t),t}}),[f,r,l]);return e.useMemo((function(){function t(e,t,n){var o=new d(t,n);return l(o),a({error:o,key:e,namespace:r})}function f(f,l,E){var y,h=p.current;if(v instanceof d)return a({error:v,key:f,namespace:r});var S,M=v,g=[r,f].filter((function(r){return null!=r})).join(".");if(null!=(y=h[c])&&y[g])S=h[c][g];else{var w;try{w=I(M,f)}catch(r){return t(f,exports.IntlErrorCode.MISSING_MESSAGE,r.message)}if("object"==typeof w)return t(f,exports.IntlErrorCode.INSUFFICIENT_PATH,void 0);try{S=new n(w,c,function(r,e){var t=e?o({},r,{dateTime:m(r.dateTime,e)}):r;return o({},t,{date:null==t?void 0:t.dateTime,time:null==t?void 0:t.dateTime})}(o({},i,E),s))}catch(r){return t(f,exports.IntlErrorCode.INVALID_MESSAGE,r.message)}h[c]||(h[c]={}),h[c][g]=S}try{var T=S.format(function(r){if(0!==Object.keys(r).length){var t={};return Object.keys(r).forEach((function(n){var o=0,u=r[n];t[n]="function"==typeof u?function(r){var t=u(r);return e.isValidElement(t)?e.cloneElement(t,{key:n+o++}):t}:u})),t}}(o({},u,l)));if(null==T)throw new Error(void 0);return e.isValidElement(T)||Array.isArray(T)||"string"==typeof T?T:String(T)}catch(r){return t(f,exports.IntlErrorCode.FORMATTING_ERROR,r.message)}}function E(r,e,n){var o=f(r,e,n);return"string"!=typeof o?t(r,exports.IntlErrorCode.INVALID_MESSAGE,void 0):o}return E.rich=f,E.raw=function(e){if(v instanceof d)return a({error:v,key:e,namespace:r});var n=v;try{return I(n,e)}catch(r){return t(e,exports.IntlErrorCode.MISSING_MESSAGE,r.message)}},E}),[a,i,c,v,r,l,s,u])};
|
|
2
2
|
//# sourceMappingURL=use-intl.cjs.production.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.cjs.production.min.js","sources":["../src/IntlContext.tsx","../src/IntlError.tsx","../src/IntlProvider.tsx","../src/convertFormatsToIntlMessageFormat.tsx","../src/useIntlContext.tsx","../src/useTranslations.tsx","../src/useNow.tsx","../src/useIntl.tsx","../src/useLocale.tsx","../src/useTimeZone.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport Formats from './Formats';\nimport IntlError from './IntlError';\nimport IntlMessages from './IntlMessages';\n\nexport type IntlContextShape = {\n messages?: IntlMessages;\n locale: string;\n formats?: Partial<Formats>;\n timeZone?: string;\n onError(error: IntlError): void;\n getMessageFallback(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n now?: Date;\n};\n\nconst IntlContext = createContext<IntlContextShape | undefined>(undefined);\n\nexport default IntlContext;\n","export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n","import React, {ReactNode} from 'react';\nimport Formats from './Formats';\nimport IntlContext from './IntlContext';\nimport IntlMessages from './IntlMessages';\nimport {IntlError} from '.';\n\ntype Props = {\n /** All messages that will be available in your components. */\n messages?: IntlMessages;\n /** A valid Unicode locale tag (e.g. \"en\" or \"en-GB\"). */\n locale: string;\n /** Global formats can be provided to achieve consistent\n * formatting across components. */\n formats?: Partial<Formats>;\n /** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */\n timeZone?: string;\n /** This callback will be invoked when an error is encountered during\n * resolving a message or formatting it. This defaults to `console.error` to\n * keep your app running. You can customize the handling by taking\n * `error.code` into account. */\n onError?(error: IntlError): void;\n /** Will be called when a message couldn't be resolved or formatting it led to\n * an error. This defaults to `${namespace}.${key}` You can use this to\n * customize what will be rendered in this case. */\n getMessageFallback?(info: {\n namespace?: string;\n key: string;\n error: IntlError;\n }): string;\n /** All components that use the provided hooks should be within this tree. */\n children: ReactNode;\n /**\n * Providing this value will have two effects:\n * 1. It will be used as the default for the `now` argument of\n * `useIntl().formatRelativeTime` if no explicit value is provided.\n * 2. It will be returned as a static value from the `useNow` hook. Note\n * however that when `updateInterval` is configured on the `useNow` hook,\n * the global `now` value will only be used for the initial render, but\n * afterwards the current date will be returned continuously.\n */\n now?: Date;\n};\n\nfunction defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nfunction defaultOnError(error: IntlError) {\n console.error(error);\n}\n\nexport default function IntlProvider({\n children,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...contextValues\n}: Props) {\n return (\n <IntlContext.Provider\n value={{...contextValues, onError, getMessageFallback}}\n >\n {children}\n </IntlContext.Provider>\n );\n}\n","import {Formats as IntlFormats} from 'intl-messageformat';\nimport DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\n\nfunction setTimeZoneInFormats(\n formats: Record<string, DateTimeFormatOptions> | undefined,\n timeZone: string\n) {\n if (!formats) return formats;\n\n // The only way to set a time zone with `intl-messageformat` is to merge it into the formats\n // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15\n return Object.keys(formats).reduce(\n (acc: Record<string, DateTimeFormatOptions>, key) => {\n acc[key] = {\n timeZone,\n ...formats[key]\n };\n return acc;\n },\n {}\n );\n}\n\n/**\n * `intl-messageformat` uses separate keys for `date` and `time`, but there's\n * only one native API: `Intl.DateTimeFormat`. Additionally you might want to\n * include both a time and a date in a value, therefore the separation doesn't\n * seem so useful. We offer a single `dateTime` namespace instead, but we have\n * to convert the format before `intl-messageformat` can be used.\n */\nexport default function convertFormatsToIntlMessageFormat(\n formats: Partial<Formats>,\n timeZone?: string\n): Partial<IntlFormats> {\n const formatsWithTimeZone = timeZone\n ? {...formats, dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)}\n : formats;\n\n return {\n ...formatsWithTimeZone,\n date: formatsWithTimeZone?.dateTime,\n time: formatsWithTimeZone?.dateTime\n };\n}\n","import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n __DEV__\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n","import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray,\n useMemo,\n useRef\n} from 'react';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport useIntlContext from './useIntlContext';\n\nfunction resolvePath(\n messages: IntlMessages | undefined,\n idPath: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n __DEV__ ? `No messages available at \\`${namespace}\\`.` : undefined\n );\n }\n\n let message = messages;\n\n idPath.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n __DEV__\n ? `Could not resolve \\`${idPath}\\` 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 (!values) return values;\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 = (children: ReactNode) => {\n const result = value(children);\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\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations(namespace?: string) {\n const {\n formats: globalFormats,\n getMessageFallback,\n locale,\n messages: allMessages,\n onError,\n timeZone\n } = useIntlContext();\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(() => {\n try {\n if (!allMessages) {\n throw new Error(\n __DEV__ ? `No messages were configured on the provider.` : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(allMessages, namespace)\n : allMessages;\n\n if (!retrievedMessages) {\n throw new Error(\n __DEV__\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 }, [allMessages, namespace, onError]);\n\n const translate = useMemo(() => {\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 const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;\n\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 const cacheKey = [namespace, key]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale[locale][cacheKey];\n } else {\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 if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n __DEV__\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n\n try {\n const formattedMessage = messageFormat.format(\n prepareTranslationValues(values)\n );\n\n if (formattedMessage == null) {\n throw new Error(\n __DEV__\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 /** 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?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const message = translateBaseFn(key, values, formats);\n\n if (typeof message !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n __DEV__\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 message;\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 getMessageFallback,\n globalFormats,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n ]);\n\n return translate;\n}\n","import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n","import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport useIntlContext from './useIntlContext';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | Intl.NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["IntlErrorCode","IntlContext","createContext","undefined","defaultGetMessageFallback","namespace","key","filter","part","join","defaultOnError","error","console","IntlError","code","originalMessage","message","Error","setTimeZoneInFormats","formats","timeZone","Object","keys","reduce","acc","useIntlContext","context","useContext","resolvePath","messages","idPath","split","forEach","next","getNow","Date","children","onError","getMessageFallback","contextValues","React","Provider","value","locale","globalNow","now","getFormattedValue","formatOrOptions","typeFormats","formatter","options","MISSING_FORMAT","resolveFormatOrOptions","String","FORMATTING_ERROR","formatDateTime","dateTime","_options","Intl","DateTimeFormat","format","formatNumber","number","NumberFormat","formatRelativeTime","date","dateDate","nowDate","seconds","unit","absValue","Math","abs","round","MINUTE","HOUR","DAY","getRelativeTimeFormatConfig","getTime","RelativeTimeFormat","numeric","updateInterval","useState","setNow","useEffect","intervalId","setInterval","clearInterval","globalFormats","allMessages","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","retrievedMessages","intlError","MISSING_MESSAGE","getFallbackFromErrorAndNotify","translateBaseFn","values","cachedFormatsByLocale","current","messageFormat","cacheKey","_cachedFormatsByLocal","INSUFFICIENT_PATH","IntlMessageFormat","formatsWithTimeZone","time","convertFormatsToIntlMessageFormat","INVALID_MESSAGE","formattedMessage","transformedValues","index","result","isValidElement","cloneElement","prepareTranslationValues","Array","isArray","translateFn","rich","raw"],"mappings":"khDAmBA,ICnBYA,EDmBNC,EAAcC,qBAA4CC,GEwBhE,SAASC,WAOA,GALPC,YADAC,KAMwBC,QAAO,SAACC,UAAiB,MAARA,KAAcC,KAAK,KAG9D,SAASC,EAAeC,GACtBC,QAAQD,MAAMA,IDtDJX,EAAAA,wBAAAA,6DAEVA,kCACAA,wCACAA,oCACAA,0CAGmBa,iCAIPC,EAAqBC,SAC3BC,EAAkBF,SAClBC,IACFC,GAAW,KAAOD,kBAEdC,UAEDF,KAAOA,EAC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AAgBK,WAKhBwG,IACN,CACDlF,EACAkD,EACA7C,EACAiD,EACAvF,EACAgC,EACAjB"}
|
|
1
|
+
{"version":3,"file":"use-intl.cjs.production.min.js","sources":["../src/IntlContext.tsx","../src/IntlError.tsx","../src/IntlProvider.tsx","../src/convertFormatsToIntlMessageFormat.tsx","../src/useIntlContext.tsx","../src/useTranslations.tsx","../src/useNow.tsx","../src/useIntl.tsx","../src/useLocale.tsx","../src/useTimeZone.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport Formats from './Formats';\nimport IntlError from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport {RichTranslationValues} from './TranslationValues';\n\nexport type IntlContextShape = {\n messages?: IntlMessages;\n locale: string;\n formats?: Partial<Formats>;\n timeZone?: string;\n onError(error: IntlError): void;\n getMessageFallback(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n now?: Date;\n defaultTranslationValues?: RichTranslationValues;\n};\n\nconst IntlContext = createContext<IntlContextShape | undefined>(undefined);\n\nexport default IntlContext;\n","export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n","import React, {ReactNode} from 'react';\nimport Formats from './Formats';\nimport IntlContext from './IntlContext';\nimport IntlMessages from './IntlMessages';\nimport {RichTranslationValues} from './TranslationValues';\nimport {IntlError} from '.';\n\ntype Props = {\n /** All messages that will be available in your components. */\n messages?: IntlMessages;\n /** A valid Unicode locale tag (e.g. \"en\" or \"en-GB\"). */\n locale: string;\n /** Global formats can be provided to achieve consistent\n * formatting across components. */\n formats?: Partial<Formats>;\n /** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */\n timeZone?: string;\n /** This callback will be invoked when an error is encountered during\n * resolving a message or formatting it. This defaults to `console.error` to\n * keep your app running. You can customize the handling by taking\n * `error.code` into account. */\n onError?(error: IntlError): void;\n /** Will be called when a message couldn't be resolved or formatting it led to\n * an error. This defaults to `${namespace}.${key}` You can use this to\n * customize what will be rendered in this case. */\n getMessageFallback?(info: {\n namespace?: string;\n key: string;\n error: IntlError;\n }): string;\n /** All components that use the provided hooks should be within this tree. */\n children: ReactNode;\n /**\n * Providing this value will have two effects:\n * 1. It will be used as the default for the `now` argument of\n * `useIntl().formatRelativeTime` if no explicit value is provided.\n * 2. It will be returned as a static value from the `useNow` hook. Note\n * however that when `updateInterval` is configured on the `useNow` hook,\n * the global `now` value will only be used for the initial render, but\n * afterwards the current date will be returned continuously.\n */\n now?: Date;\n /** Global default values for translation values and rich text elements.\n * Can be used for consistent usage or styling of rich text elements.\n * Defaults will be overidden by locally provided values. */\n defaultTranslationValues?: RichTranslationValues;\n};\n\nfunction defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nfunction defaultOnError(error: IntlError) {\n console.error(error);\n}\n\nexport default function IntlProvider({\n children,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...contextValues\n}: Props) {\n return (\n <IntlContext.Provider\n value={{...contextValues, onError, getMessageFallback}}\n >\n {children}\n </IntlContext.Provider>\n );\n}\n","import {Formats as IntlFormats} from 'intl-messageformat';\nimport DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\n\nfunction setTimeZoneInFormats(\n formats: Record<string, DateTimeFormatOptions> | undefined,\n timeZone: string\n) {\n if (!formats) return formats;\n\n // The only way to set a time zone with `intl-messageformat` is to merge it into the formats\n // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15\n return Object.keys(formats).reduce(\n (acc: Record<string, DateTimeFormatOptions>, key) => {\n acc[key] = {\n timeZone,\n ...formats[key]\n };\n return acc;\n },\n {}\n );\n}\n\n/**\n * `intl-messageformat` uses separate keys for `date` and `time`, but there's\n * only one native API: `Intl.DateTimeFormat`. Additionally you might want to\n * include both a time and a date in a value, therefore the separation doesn't\n * seem so useful. We offer a single `dateTime` namespace instead, but we have\n * to convert the format before `intl-messageformat` can be used.\n */\nexport default function convertFormatsToIntlMessageFormat(\n formats: Partial<Formats>,\n timeZone?: string\n): Partial<IntlFormats> {\n const formatsWithTimeZone = timeZone\n ? {...formats, dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)}\n : formats;\n\n return {\n ...formatsWithTimeZone,\n date: formatsWithTimeZone?.dateTime,\n time: formatsWithTimeZone?.dateTime\n };\n}\n","import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n __DEV__\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n","import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray,\n useMemo,\n useRef\n} from 'react';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport useIntlContext from './useIntlContext';\n\nfunction resolvePath(\n messages: IntlMessages | undefined,\n idPath: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n __DEV__ ? `No messages available at \\`${namespace}\\`.` : undefined\n );\n }\n\n let message = messages;\n\n idPath.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n __DEV__\n ? `Could not resolve \\`${idPath}\\` 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 = (children: ReactNode) => {\n const result = value(children);\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\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations(namespace?: string) {\n const {\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback,\n locale,\n messages: allMessages,\n onError,\n timeZone\n } = useIntlContext();\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(() => {\n try {\n if (!allMessages) {\n throw new Error(\n __DEV__ ? `No messages were configured on the provider.` : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(allMessages, namespace)\n : allMessages;\n\n if (!retrievedMessages) {\n throw new Error(\n __DEV__\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 }, [allMessages, namespace, onError]);\n\n const translate = useMemo(() => {\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 const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;\n\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 const cacheKey = [namespace, key]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale[locale][cacheKey];\n } else {\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 if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n __DEV__\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n\n try {\n const formattedMessage = messageFormat.format(\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n __DEV__\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 /** 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?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const message = translateBaseFn(key, values, formats);\n\n if (typeof message !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n __DEV__\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 message;\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 getMessageFallback,\n globalFormats,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone,\n defaultTranslationValues\n ]);\n\n return translate;\n}\n","import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n","import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport useIntlContext from './useIntlContext';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | Intl.NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["IntlErrorCode","IntlContext","createContext","undefined","defaultGetMessageFallback","namespace","key","filter","part","join","defaultOnError","error","console","IntlError","code","originalMessage","message","Error","setTimeZoneInFormats","formats","timeZone","Object","keys","reduce","acc","useIntlContext","context","useContext","resolvePath","messages","idPath","split","forEach","next","getNow","Date","children","onError","getMessageFallback","contextValues","React","Provider","value","locale","globalNow","now","getFormattedValue","formatOrOptions","typeFormats","formatter","options","MISSING_FORMAT","resolveFormatOrOptions","String","FORMATTING_ERROR","formatDateTime","dateTime","_options","Intl","DateTimeFormat","format","formatNumber","number","NumberFormat","formatRelativeTime","date","dateDate","nowDate","seconds","unit","absValue","Math","abs","round","MINUTE","HOUR","DAY","getRelativeTimeFormatConfig","getTime","RelativeTimeFormat","numeric","updateInterval","useState","setNow","useEffect","intervalId","setInterval","clearInterval","defaultTranslationValues","globalFormats","allMessages","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","retrievedMessages","intlError","MISSING_MESSAGE","getFallbackFromErrorAndNotify","translateBaseFn","values","cachedFormatsByLocale","current","messageFormat","cacheKey","_cachedFormatsByLocal","INSUFFICIENT_PATH","IntlMessageFormat","formatsWithTimeZone","time","convertFormatsToIntlMessageFormat","INVALID_MESSAGE","formattedMessage","length","transformedValues","index","result","isValidElement","cloneElement","prepareTranslationValues","Array","isArray","translateFn","rich","raw"],"mappings":"khDAqBA,ICrBYA,EDqBNC,EAAcC,qBAA4CC,GE2BhE,SAASC,WAOA,GALPC,YADAC,KAMwBC,QAAO,SAACC,UAAiB,MAARA,KAAcC,KAAK,KAG9D,SAASC,EAAeC,GACtBC,QAAQD,MAAMA,ID3DJX,EAAAA,wBAAAA,6DAEVA,kCACAA,wCACAA,oCACAA,0CAGmBa,iCAIPC,EAAqBC,SAC3BC,EAAkBF,SAClBC,IACFC,GAAW,KAAOD,kBAEdC,UAEDF,KAAOA,EACRC,MACGA,gBAAkBA,wGAbUE,QEJvC,SAASC,EACPC,EACAC,UAEKD,EAIEE,OAAOC,KAAKH,GAASI,QAC1B,SAACC,EAA4ClB,UAC3CkB,EAAIlB,MACFc,SAAAA,GACGD,EAAQb,IAENkB,IAET,IAZmBL,WCLCM,QAChBC,EAAUC,aAAW1B,OAEtByB,QACG,IAAIT,WAGJd,UAIDuB,WCGAE,EACPC,EACAC,EACAzB,OAEKwB,QACG,IAAIZ,WACiDd,OAIzDa,EAAUa,SAEdC,EAAOC,MAAM,KAAKC,SAAQ,SAACxB,OACnByB,EAAQjB,EAAgBR,MAElB,MAARA,GAAwB,MAARyB,QACZ,IAAIhB,WAKJd,GAIRa,EAAUiB,KAGLjB,WCvCAkB,WACA,IAAIC,8DJuDXC,IAAAA,aACAC,QAAAA,aAAU3B,QACV4B,mBAAAA,aAAqBlC,IAClBmC,2LAGDC,gBAACvC,EAAYwC,UACXC,WAAWH,GAAeF,QAAAA,EAASC,mBAAAA,KAElCF,oBK5BP,iBAC+DX,IAAtDN,IAAAA,QAASwB,IAAAA,OAAaC,IAALC,IAAgBR,IAAAA,QAASjB,IAAAA,kBA4BxC0B,EACP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AA6BY,QAGpC,MAApBY,QACI,IAAI/F,WAKJd,UAKDkH,iBAAeL,IAEpBQ,MAAMC,QAAQT,IACc,iBAArBA,EACLA,EACA3D,OAAO2D,GACX,MAAOrG,UACAuF,EACL5F,EACAN,sBAAcsD,iBACb3C,EAAgBK,mBAKd0G,EAEPpH,EAEA8F,EAEAjF,OAEMH,EAAUmF,EAAgB7F,EAAK8F,EAAQjF,SAEtB,iBAAZH,EACFkF,EACL5F,EACAN,sBAAc+G,qBAKV5G,GAIDa,SAGT0G,EAAYC,KAAOxB,EAEnBuB,EAAYE,IAAM,SAEhBtH,MAEIuF,aAA2BhF,SAEtByB,EAAmB,CACxB3B,MAAOkF,EACPvF,IAAAA,EACAD,UAAAA,QAGEwB,EAAWgE,aAGRjE,EAAYC,EAAUvB,GAC7B,MAAOK,UACAuF,EACL5F,EACAN,sBAAciG,gBACbtF,EAAgBK,WAKhB0G,IACN,CACDpF,EACAmD,EACA9C,EACAkD,EACAxF,EACAgC,EACAjB,EACAoE"}
|
package/dist/use-intl.esm.js
CHANGED
|
@@ -248,7 +248,7 @@ function resolvePath(messages, idPath, namespace) {
|
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
function prepareTranslationValues(values) {
|
|
251
|
-
if (
|
|
251
|
+
if (Object.keys(values).length === 0) return undefined; // Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
252
252
|
|
|
253
253
|
var transformedValues = {};
|
|
254
254
|
Object.keys(values).forEach(function (key) {
|
|
@@ -283,6 +283,7 @@ function prepareTranslationValues(values) {
|
|
|
283
283
|
|
|
284
284
|
function useTranslations(namespace) {
|
|
285
285
|
var _useIntlContext = useIntlContext(),
|
|
286
|
+
defaultTranslationValues = _useIntlContext.defaultTranslationValues,
|
|
286
287
|
globalFormats = _useIntlContext.formats,
|
|
287
288
|
getMessageFallback = _useIntlContext.getMessageFallback,
|
|
288
289
|
locale = _useIntlContext.locale,
|
|
@@ -376,7 +377,7 @@ function useTranslations(namespace) {
|
|
|
376
377
|
}
|
|
377
378
|
|
|
378
379
|
try {
|
|
379
|
-
var formattedMessage = messageFormat.format(prepareTranslationValues(values));
|
|
380
|
+
var formattedMessage = messageFormat.format(prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
380
381
|
|
|
381
382
|
if (formattedMessage == null) {
|
|
382
383
|
throw new Error(process.env.NODE_ENV !== "production" ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
@@ -430,7 +431,7 @@ function useTranslations(namespace) {
|
|
|
430
431
|
};
|
|
431
432
|
|
|
432
433
|
return translateFn;
|
|
433
|
-
}, [getMessageFallback, globalFormats, locale, messagesOrError, namespace, onError, timeZone]);
|
|
434
|
+
}, [getMessageFallback, globalFormats, locale, messagesOrError, namespace, onError, timeZone, defaultTranslationValues]);
|
|
434
435
|
return translate;
|
|
435
436
|
}
|
|
436
437
|
|
package/dist/use-intl.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm.js","sources":["../src/IntlContext.tsx","../src/IntlProvider.tsx","../src/IntlError.tsx","../src/convertFormatsToIntlMessageFormat.tsx","../src/useIntlContext.tsx","../src/useTranslations.tsx","../src/useIntl.tsx","../src/useLocale.tsx","../src/useNow.tsx","../src/useTimeZone.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport Formats from './Formats';\nimport IntlError from './IntlError';\nimport IntlMessages from './IntlMessages';\n\nexport type IntlContextShape = {\n messages?: IntlMessages;\n locale: string;\n formats?: Partial<Formats>;\n timeZone?: string;\n onError(error: IntlError): void;\n getMessageFallback(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n now?: Date;\n};\n\nconst IntlContext = createContext<IntlContextShape | undefined>(undefined);\n\nexport default IntlContext;\n","import React, {ReactNode} from 'react';\nimport Formats from './Formats';\nimport IntlContext from './IntlContext';\nimport IntlMessages from './IntlMessages';\nimport {IntlError} from '.';\n\ntype Props = {\n /** All messages that will be available in your components. */\n messages?: IntlMessages;\n /** A valid Unicode locale tag (e.g. \"en\" or \"en-GB\"). */\n locale: string;\n /** Global formats can be provided to achieve consistent\n * formatting across components. */\n formats?: Partial<Formats>;\n /** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */\n timeZone?: string;\n /** This callback will be invoked when an error is encountered during\n * resolving a message or formatting it. This defaults to `console.error` to\n * keep your app running. You can customize the handling by taking\n * `error.code` into account. */\n onError?(error: IntlError): void;\n /** Will be called when a message couldn't be resolved or formatting it led to\n * an error. This defaults to `${namespace}.${key}` You can use this to\n * customize what will be rendered in this case. */\n getMessageFallback?(info: {\n namespace?: string;\n key: string;\n error: IntlError;\n }): string;\n /** All components that use the provided hooks should be within this tree. */\n children: ReactNode;\n /**\n * Providing this value will have two effects:\n * 1. It will be used as the default for the `now` argument of\n * `useIntl().formatRelativeTime` if no explicit value is provided.\n * 2. It will be returned as a static value from the `useNow` hook. Note\n * however that when `updateInterval` is configured on the `useNow` hook,\n * the global `now` value will only be used for the initial render, but\n * afterwards the current date will be returned continuously.\n */\n now?: Date;\n};\n\nfunction defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nfunction defaultOnError(error: IntlError) {\n console.error(error);\n}\n\nexport default function IntlProvider({\n children,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...contextValues\n}: Props) {\n return (\n <IntlContext.Provider\n value={{...contextValues, onError, getMessageFallback}}\n >\n {children}\n </IntlContext.Provider>\n );\n}\n","export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n","import {Formats as IntlFormats} from 'intl-messageformat';\nimport DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\n\nfunction setTimeZoneInFormats(\n formats: Record<string, DateTimeFormatOptions> | undefined,\n timeZone: string\n) {\n if (!formats) return formats;\n\n // The only way to set a time zone with `intl-messageformat` is to merge it into the formats\n // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15\n return Object.keys(formats).reduce(\n (acc: Record<string, DateTimeFormatOptions>, key) => {\n acc[key] = {\n timeZone,\n ...formats[key]\n };\n return acc;\n },\n {}\n );\n}\n\n/**\n * `intl-messageformat` uses separate keys for `date` and `time`, but there's\n * only one native API: `Intl.DateTimeFormat`. Additionally you might want to\n * include both a time and a date in a value, therefore the separation doesn't\n * seem so useful. We offer a single `dateTime` namespace instead, but we have\n * to convert the format before `intl-messageformat` can be used.\n */\nexport default function convertFormatsToIntlMessageFormat(\n formats: Partial<Formats>,\n timeZone?: string\n): Partial<IntlFormats> {\n const formatsWithTimeZone = timeZone\n ? {...formats, dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)}\n : formats;\n\n return {\n ...formatsWithTimeZone,\n date: formatsWithTimeZone?.dateTime,\n time: formatsWithTimeZone?.dateTime\n };\n}\n","import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n __DEV__\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n","import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray,\n useMemo,\n useRef\n} from 'react';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport useIntlContext from './useIntlContext';\n\nfunction resolvePath(\n messages: IntlMessages | undefined,\n idPath: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n __DEV__ ? `No messages available at \\`${namespace}\\`.` : undefined\n );\n }\n\n let message = messages;\n\n idPath.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n __DEV__\n ? `Could not resolve \\`${idPath}\\` 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 (!values) return values;\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 = (children: ReactNode) => {\n const result = value(children);\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\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations(namespace?: string) {\n const {\n formats: globalFormats,\n getMessageFallback,\n locale,\n messages: allMessages,\n onError,\n timeZone\n } = useIntlContext();\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(() => {\n try {\n if (!allMessages) {\n throw new Error(\n __DEV__ ? `No messages were configured on the provider.` : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(allMessages, namespace)\n : allMessages;\n\n if (!retrievedMessages) {\n throw new Error(\n __DEV__\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 }, [allMessages, namespace, onError]);\n\n const translate = useMemo(() => {\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 const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;\n\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 const cacheKey = [namespace, key]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale[locale][cacheKey];\n } else {\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 if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n __DEV__\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n\n try {\n const formattedMessage = messageFormat.format(\n prepareTranslationValues(values)\n );\n\n if (formattedMessage == null) {\n throw new Error(\n __DEV__\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 /** 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?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const message = translateBaseFn(key, values, formats);\n\n if (typeof message !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n __DEV__\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 message;\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 getMessageFallback,\n globalFormats,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n ]);\n\n return translate;\n}\n","import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport useIntlContext from './useIntlContext';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | Intl.NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n","import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["IntlContext","createContext","undefined","defaultGetMessageFallback","key","namespace","filter","part","join","defaultOnError","error","console","IntlProvider","children","onError","getMessageFallback","contextValues","React","Provider","value","IntlErrorCode","IntlError","code","originalMessage","message","Error","setTimeZoneInFormats","formats","timeZone","Object","keys","reduce","acc","convertFormatsToIntlMessageFormat","formatsWithTimeZone","dateTime","date","time","useIntlContext","context","useContext","resolvePath","messages","idPath","split","forEach","next","prepareTranslationValues","values","transformedValues","index","transformed","result","isValidElement","cloneElement","useTranslations","globalFormats","locale","allMessages","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","retrievedMessages","intlError","MISSING_MESSAGE","translate","getFallbackFromErrorAndNotify","translateBaseFn","cachedFormatsByLocale","current","cacheKey","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw","MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","unit","round","useIntl","globalNow","now","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","MISSING_FORMAT","getFormattedValue","formatter","formatDateTime","Intl","DateTimeFormat","formatNumber","number","NumberFormat","formatRelativeTime","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric","useLocale","getNow","useNow","updateInterval","useState","setNow","useEffect","intervalId","setInterval","clearInterval","useTimeZone"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,IAAMA,WAAW,gBAAGC,aAAa,CAA+BC,SAA/B,CAAjC;;ACwBA,SAASC,yBAAT;MACEC,WAAAA;MACAC,iBAAAA;AAKA,SAAO,CAACA,SAAD,EAAYD,GAAZ,EAAiBE,MAAjB,CAAwB,UAACC,IAAD;AAAA,WAAUA,IAAI,IAAI,IAAlB;AAAA,GAAxB,EAAgDC,IAAhD,CAAqD,GAArD,CAAP;AACD;;AAED,SAASC,cAAT,CAAwBC,KAAxB;AACEC,EAAAA,OAAO,CAACD,KAAR,CAAcA,KAAd;AACD;;AAED,SAAwBE;MACtBC,iBAAAA;4BACAC;MAAAA,qCAAUL;oCACVM;MAAAA,wDAAqBZ;MAClBa;;AAEH,SACEC,mBAAA,CAACjB,WAAW,CAACkB,QAAb;AACEC,IAAAA,KAAK,eAAMH,aAAN;AAAqBF,MAAAA,OAAO,EAAPA,OAArB;AAA8BC,MAAAA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AAIN,IAAJ,EAAP;AACD;AAED;;;;;;;;;;;;;;;;;;;;AAkBA,SAAwBO,OAAOpB;AAC7B,MAAMqB,cAAc,GAAGrB,OAAH,oBAAGA,OAAO,CAAEqB,cAAhC;;wBAEyBvF,cAAc;MAA3B6D,4BAALC;;kBACe0B,QAAQ,CAAC3B,SAAS,IAAIwB,MAAM,EAApB;MAAvBvB;MAAK2B;;AAEZC,EAAAA,SAAS,CAAC;AACR,QAAI,CAACH,cAAL,EAAqB;AAErB,QAAMI,UAAU,GAAGC,WAAW,CAAC;AAC7BH,MAAAA,MAAM,CAACJ,MAAM,EAAP,CAAN;AACD,KAF6B,EAE3BE,cAF2B,CAA9B;AAIA,WAAO;AACLM,MAAAA,aAAa,CAACF,UAAD,CAAb;AACD,KAFD;AAGD,GAVQ,EAUN,CAAC9B,SAAD,EAAY0B,cAAZ,CAVM,CAAT;AAYA,SAAOzB,GAAP;AACD;;SC9CuBgC;AACtB,SAAO9F,cAAc,GAAGV,QAAxB;AACD;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.esm.js","sources":["../src/IntlContext.tsx","../src/IntlProvider.tsx","../src/IntlError.tsx","../src/convertFormatsToIntlMessageFormat.tsx","../src/useIntlContext.tsx","../src/useTranslations.tsx","../src/useIntl.tsx","../src/useLocale.tsx","../src/useNow.tsx","../src/useTimeZone.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport Formats from './Formats';\nimport IntlError from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport {RichTranslationValues} from './TranslationValues';\n\nexport type IntlContextShape = {\n messages?: IntlMessages;\n locale: string;\n formats?: Partial<Formats>;\n timeZone?: string;\n onError(error: IntlError): void;\n getMessageFallback(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n now?: Date;\n defaultTranslationValues?: RichTranslationValues;\n};\n\nconst IntlContext = createContext<IntlContextShape | undefined>(undefined);\n\nexport default IntlContext;\n","import React, {ReactNode} from 'react';\nimport Formats from './Formats';\nimport IntlContext from './IntlContext';\nimport IntlMessages from './IntlMessages';\nimport {RichTranslationValues} from './TranslationValues';\nimport {IntlError} from '.';\n\ntype Props = {\n /** All messages that will be available in your components. */\n messages?: IntlMessages;\n /** A valid Unicode locale tag (e.g. \"en\" or \"en-GB\"). */\n locale: string;\n /** Global formats can be provided to achieve consistent\n * formatting across components. */\n formats?: Partial<Formats>;\n /** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */\n timeZone?: string;\n /** This callback will be invoked when an error is encountered during\n * resolving a message or formatting it. This defaults to `console.error` to\n * keep your app running. You can customize the handling by taking\n * `error.code` into account. */\n onError?(error: IntlError): void;\n /** Will be called when a message couldn't be resolved or formatting it led to\n * an error. This defaults to `${namespace}.${key}` You can use this to\n * customize what will be rendered in this case. */\n getMessageFallback?(info: {\n namespace?: string;\n key: string;\n error: IntlError;\n }): string;\n /** All components that use the provided hooks should be within this tree. */\n children: ReactNode;\n /**\n * Providing this value will have two effects:\n * 1. It will be used as the default for the `now` argument of\n * `useIntl().formatRelativeTime` if no explicit value is provided.\n * 2. It will be returned as a static value from the `useNow` hook. Note\n * however that when `updateInterval` is configured on the `useNow` hook,\n * the global `now` value will only be used for the initial render, but\n * afterwards the current date will be returned continuously.\n */\n now?: Date;\n /** Global default values for translation values and rich text elements.\n * Can be used for consistent usage or styling of rich text elements.\n * Defaults will be overidden by locally provided values. */\n defaultTranslationValues?: RichTranslationValues;\n};\n\nfunction defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nfunction defaultOnError(error: IntlError) {\n console.error(error);\n}\n\nexport default function IntlProvider({\n children,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...contextValues\n}: Props) {\n return (\n <IntlContext.Provider\n value={{...contextValues, onError, getMessageFallback}}\n >\n {children}\n </IntlContext.Provider>\n );\n}\n","export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n FORMATTING_ERROR = 'FORMATTING_ERROR'\n}\n\nexport default class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += ': ' + originalMessage;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n","import {Formats as IntlFormats} from 'intl-messageformat';\nimport DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\n\nfunction setTimeZoneInFormats(\n formats: Record<string, DateTimeFormatOptions> | undefined,\n timeZone: string\n) {\n if (!formats) return formats;\n\n // The only way to set a time zone with `intl-messageformat` is to merge it into the formats\n // https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15\n return Object.keys(formats).reduce(\n (acc: Record<string, DateTimeFormatOptions>, key) => {\n acc[key] = {\n timeZone,\n ...formats[key]\n };\n return acc;\n },\n {}\n );\n}\n\n/**\n * `intl-messageformat` uses separate keys for `date` and `time`, but there's\n * only one native API: `Intl.DateTimeFormat`. Additionally you might want to\n * include both a time and a date in a value, therefore the separation doesn't\n * seem so useful. We offer a single `dateTime` namespace instead, but we have\n * to convert the format before `intl-messageformat` can be used.\n */\nexport default function convertFormatsToIntlMessageFormat(\n formats: Partial<Formats>,\n timeZone?: string\n): Partial<IntlFormats> {\n const formatsWithTimeZone = timeZone\n ? {...formats, dateTime: setTimeZoneInFormats(formats.dateTime, timeZone)}\n : formats;\n\n return {\n ...formatsWithTimeZone,\n date: formatsWithTimeZone?.dateTime,\n time: formatsWithTimeZone?.dateTime\n };\n}\n","import {useContext} from 'react';\nimport IntlContext from './IntlContext';\n\nexport default function useIntlContext() {\n const context = useContext(IntlContext);\n\n if (!context) {\n throw new Error(\n __DEV__\n ? 'No intl context found. Have you configured the provider?'\n : undefined\n );\n }\n\n return context;\n}\n","import IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray,\n useMemo,\n useRef\n} from 'react';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport IntlMessages from './IntlMessages';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport useIntlContext from './useIntlContext';\n\nfunction resolvePath(\n messages: IntlMessages | undefined,\n idPath: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n __DEV__ ? `No messages available at \\`${namespace}\\`.` : undefined\n );\n }\n\n let message = messages;\n\n idPath.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n __DEV__\n ? `Could not resolve \\`${idPath}\\` 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 = (children: ReactNode) => {\n const result = value(children);\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\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations(namespace?: string) {\n const {\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback,\n locale,\n messages: allMessages,\n onError,\n timeZone\n } = useIntlContext();\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(() => {\n try {\n if (!allMessages) {\n throw new Error(\n __DEV__ ? `No messages were configured on the provider.` : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(allMessages, namespace)\n : allMessages;\n\n if (!retrievedMessages) {\n throw new Error(\n __DEV__\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 }, [allMessages, namespace, onError]);\n\n const translate = useMemo(() => {\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 const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;\n\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 const cacheKey = [namespace, key]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale[locale][cacheKey];\n } else {\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 if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n __DEV__\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n\n try {\n const formattedMessage = messageFormat.format(\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n __DEV__\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 /** 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?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const message = translateBaseFn(key, values, formats);\n\n if (typeof message !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n __DEV__\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 message;\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 getMessageFallback,\n globalFormats,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone,\n defaultTranslationValues\n ]);\n\n return translate;\n}\n","import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport useIntlContext from './useIntlContext';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | Intl.NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n","import {useState, useEffect} from 'react';\nimport useIntlContext from './useIntlContext';\n\ntype Options = {\n updateInterval?: number;\n};\n\nfunction getNow() {\n return new Date();\n}\n\n/**\n * Reading the current date via `new Date()` in components should be avoided, as\n * it causes components to be impure and can lead to flaky tests. Instead, this\n * hook can be used.\n *\n * By default, it returns the time when the component mounts. If `updateInterval`\n * is specified, the value will be updated based on the interval.\n *\n * You can however also return a static value from this hook, if you\n * configure the `now` parameter on the context provider. Note however,\n * that if `updateInterval` is configured in this case, the component\n * will initialize with the global value, but will afterwards update\n * continuously based on the interval.\n *\n * For unit tests, this can be mocked to a constant value. For end-to-end\n * testing, an environment parameter can be passed to the `now` parameter\n * of the provider to mock this to a static value.\n */\nexport default function useNow(options?: Options) {\n const updateInterval = options?.updateInterval;\n\n const {now: globalNow} = useIntlContext();\n const [now, setNow] = useState(globalNow || getNow());\n\n useEffect(() => {\n if (!updateInterval) return;\n\n const intervalId = setInterval(() => {\n setNow(getNow());\n }, updateInterval);\n\n return () => {\n clearInterval(intervalId);\n };\n }, [globalNow, updateInterval]);\n\n return now;\n}\n","import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["IntlContext","createContext","undefined","defaultGetMessageFallback","key","namespace","filter","part","join","defaultOnError","error","console","IntlProvider","children","onError","getMessageFallback","contextValues","React","Provider","value","IntlErrorCode","IntlError","code","originalMessage","message","Error","setTimeZoneInFormats","formats","timeZone","Object","keys","reduce","acc","convertFormatsToIntlMessageFormat","formatsWithTimeZone","dateTime","date","time","useIntlContext","context","useContext","resolvePath","messages","idPath","split","forEach","next","prepareTranslationValues","values","length","transformedValues","index","transformed","result","isValidElement","cloneElement","useTranslations","defaultTranslationValues","globalFormats","locale","allMessages","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","retrievedMessages","intlError","MISSING_MESSAGE","translate","getFallbackFromErrorAndNotify","translateBaseFn","cachedFormatsByLocale","current","cacheKey","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw","MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","unit","round","useIntl","globalNow","now","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","MISSING_FORMAT","getFormattedValue","formatter","formatDateTime","Intl","DateTimeFormat","formatNumber","number","NumberFormat","formatRelativeTime","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric","useLocale","getNow","useNow","updateInterval","useState","setNow","useEffect","intervalId","setInterval","clearInterval","useTimeZone"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,IAAMA,WAAW,gBAAGC,aAAa,CAA+BC,SAA/B,CAAjC;;AC2BA,SAASC,yBAAT;MACEC,WAAAA;MACAC,iBAAAA;AAKA,SAAO,CAACA,SAAD,EAAYD,GAAZ,EAAiBE,MAAjB,CAAwB,UAACC,IAAD;AAAA,WAAUA,IAAI,IAAI,IAAlB;AAAA,GAAxB,EAAgDC,IAAhD,CAAqD,GAArD,CAAP;AACD;;AAED,SAASC,cAAT,CA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AAc;AACdI,MAAAA,OAAO,CACL,IAAIO,SAAJ,CAAcD,aAAa,CAAC+D,gBAA5B,EAA+CzE,KAAe,CAACc,OAA/D,CADK,CAAP;AAGA,aAAO0D,MAAM,CAAC9C,IAAD,CAAb;AACD;AACF;;AAED,SAAO;AAAC2E,IAAAA,cAAc,EAAdA,cAAD;AAAiBG,IAAAA,YAAY,EAAZA,YAAjB;AAA+BG,IAAAA,kBAAkB,EAAlBA;AAA/B,GAAP;AACD;;SCpKuBO;AACtB,SAAOtF,cAAc,GAAGqB,MAAxB;AACD;;ACGD,SAASkE,MAAT;AACE,SAAO,IAAIN,IAAJ,EAAP;AACD;AAED;;;;;;;;;;;;;;;;;;;;AAkBA,SAAwBO,OAAOpB;AAC7B,MAAMqB,cAAc,GAAGrB,OAAH,oBAAGA,OAAO,CAAEqB,cAAhC;;wBAEyBzF,cAAc;MAA3B+D,4BAALC;;kBACe0B,QAAQ,CAAC3B,SAAS,IAAIwB,MAAM,EAApB;MAAvBvB;MAAK2B;;AAEZC,EAAAA,SAAS,CAAC;AACR,QAAI,CAACH,cAAL,EAAqB;AAErB,QAAMI,UAAU,GAAGC,WAAW,CAAC;AAC7BH,MAAAA,MAAM,CAACJ,MAAM,EAAP,CAAN;AACD,KAF6B,EAE3BE,cAF2B,CAA9B;AAIA,WAAO;AACLM,MAAAA,aAAa,CAACF,UAAD,CAAb;AACD,KAFD;AAGD,GAVQ,EAUN,CAAC9B,SAAD,EAAY0B,cAAZ,CAVM,CAAT;AAYA,SAAOzB,GAAP;AACD;;SC9CuBgC;AACtB,SAAOhG,cAAc,GAAGV,QAAxB;AACD;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-intl",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "Jan Amann <jan@amann.me>",
|
|
6
6
|
"description": "Minimal, but complete solution for managing internationalization in React apps.",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"engines": {
|
|
56
56
|
"node": ">=10"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "f410247a382e52e5d62cda538f285cb92120719e"
|
|
59
59
|
}
|
package/src/IntlContext.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import {createContext} from 'react';
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlError from './IntlError';
|
|
4
4
|
import IntlMessages from './IntlMessages';
|
|
5
|
+
import {RichTranslationValues} from './TranslationValues';
|
|
5
6
|
|
|
6
7
|
export type IntlContextShape = {
|
|
7
8
|
messages?: IntlMessages;
|
|
@@ -15,6 +16,7 @@ export type IntlContextShape = {
|
|
|
15
16
|
namespace?: string;
|
|
16
17
|
}): string;
|
|
17
18
|
now?: Date;
|
|
19
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
const IntlContext = createContext<IntlContextShape | undefined>(undefined);
|
package/src/IntlProvider.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import React, {ReactNode} from 'react';
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlContext from './IntlContext';
|
|
4
4
|
import IntlMessages from './IntlMessages';
|
|
5
|
+
import {RichTranslationValues} from './TranslationValues';
|
|
5
6
|
import {IntlError} from '.';
|
|
6
7
|
|
|
7
8
|
type Props = {
|
|
@@ -39,6 +40,10 @@ type Props = {
|
|
|
39
40
|
* afterwards the current date will be returned continuously.
|
|
40
41
|
*/
|
|
41
42
|
now?: Date;
|
|
43
|
+
/** Global default values for translation values and rich text elements.
|
|
44
|
+
* Can be used for consistent usage or styling of rich text elements.
|
|
45
|
+
* Defaults will be overidden by locally provided values. */
|
|
46
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
42
47
|
};
|
|
43
48
|
|
|
44
49
|
function defaultGetMessageFallback({
|
package/src/useTranslations.tsx
CHANGED
|
@@ -47,8 +47,8 @@ function resolvePath(
|
|
|
47
47
|
return message;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
function prepareTranslationValues(values
|
|
51
|
-
if (
|
|
50
|
+
function prepareTranslationValues(values: RichTranslationValues) {
|
|
51
|
+
if (Object.keys(values).length === 0) return undefined;
|
|
52
52
|
|
|
53
53
|
// Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
54
54
|
const transformedValues: RichTranslationValues = {};
|
|
@@ -85,6 +85,7 @@ function prepareTranslationValues(values?: RichTranslationValues) {
|
|
|
85
85
|
*/
|
|
86
86
|
export default function useTranslations(namespace?: string) {
|
|
87
87
|
const {
|
|
88
|
+
defaultTranslationValues,
|
|
88
89
|
formats: globalFormats,
|
|
89
90
|
getMessageFallback,
|
|
90
91
|
locale,
|
|
@@ -215,7 +216,7 @@ export default function useTranslations(namespace?: string) {
|
|
|
215
216
|
|
|
216
217
|
try {
|
|
217
218
|
const formattedMessage = messageFormat.format(
|
|
218
|
-
prepareTranslationValues(values)
|
|
219
|
+
prepareTranslationValues({...defaultTranslationValues, ...values})
|
|
219
220
|
);
|
|
220
221
|
|
|
221
222
|
if (formattedMessage == null) {
|
|
@@ -304,7 +305,8 @@ export default function useTranslations(namespace?: string) {
|
|
|
304
305
|
messagesOrError,
|
|
305
306
|
namespace,
|
|
306
307
|
onError,
|
|
307
|
-
timeZone
|
|
308
|
+
timeZone,
|
|
309
|
+
defaultTranslationValues
|
|
308
310
|
]);
|
|
309
311
|
|
|
310
312
|
return translate;
|