use-intl 2.10.0-alpha.4 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm3.js","sources":["../../src/react/useTranslations.tsx"],"sourcesContent":["import {ReactElement, ReactNodeArray} from 'react';\nimport Formats from '../core/Formats';\nimport TranslationValues, {\n RichTranslationValues\n} from '../core/TranslationValues';\nimport MessageKeys from '../core/utils/MessageKeys';\nimport NamespaceKeys from '../core/utils/NamespaceKeys';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport NestedValueOf from '../core/utils/NestedValueOf';\nimport useIntlContext from './useIntlContext';\nimport useTranslationsImpl from './useTranslationsImpl';\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<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>(\n namespace?: NestedKey\n): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: RichTranslationValues,\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n const context = useIntlContext();\n const messages = context.messages as IntlMessages;\n\n // We have to wrap the actual hook so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return useTranslationsImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {'!': messages},\n // @ts-ignore\n namespace ? `!.${namespace}` : '!',\n '!'\n );\n}\n"],"names":["useTranslations","namespace","context","useIntlContext","messages","useTranslationsImpl"],"mappings":";;;AAYA;;;;;;;AAOG;;AACqB,SAAAA,eAAA,CAMtBC,SANsB,EAMD;AA6DrB,EAAMC,IAAAA,OAAO,GAAGC,cAAc,EAA9B,CAAA;AACA,EAAA,IAAMC,QAAQ,GAAGF,OAAO,CAACE,QAAzB,CA9DqB;AAiErB;AACA;;AACA,EAAA,OAAOC,mBAAmB,CAIxB;AAAC,IAAKD,GAAAA,EAAAA,QAAAA;AAAN,GAJwB;AAMxBH,EAAAA,SAAS,GAAQA,IAAAA,GAAAA,SAAR,GAAsB,GANP,EAOxB,GAPwB,CAA1B,CAAA;AASD;;;;"}
1
+ {"version":3,"file":"use-intl.esm3.js","sources":["../../src/react/useIntl.tsx"],"sourcesContent":["import {useMemo} from 'react';\nimport createIntl from '../core/createIntl';\nimport useIntlContext from './useIntlContext';\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n return useMemo(\n () =>\n createIntl({\n formats,\n locale,\n now: globalNow,\n onError,\n timeZone\n }),\n [formats, globalNow, locale, onError, timeZone]\n );\n}\n"],"names":["useIntl","useIntlContext","formats","locale","globalNow","now","onError","timeZone","useMemo","createIntl"],"mappings":";;;;AAIc,SAAUA,OAAV,GAAiB;AAC7B,EAAA,IAAA,eAAA,GAA6DC,cAAc,EAA3E;AAAA,MAAOC,OAAP,mBAAOA,OAAP;AAAA,MAAgBC,MAAhB,mBAAgBA,MAAhB;AAAA,MAA6BC,SAA7B,mBAAwBC,GAAxB;AAAA,MAAwCC,OAAxC,mBAAwCA,OAAxC;AAAA,MAAiDC,QAAjD,mBAAiDA,QAAjD,CAAA;;AAEA,EAAA,OAAOC,OAAO,CACZ,YAAA;AAAA,IAAA,OACEC,UAAU,CAAC;AACTP,MAAAA,OAAO,EAAPA,OADS;AAETC,MAAAA,MAAM,EAANA,MAFS;AAGTE,MAAAA,GAAG,EAAED,SAHI;AAITE,MAAAA,OAAO,EAAPA,OAJS;AAKTC,MAAAA,QAAQ,EAARA,QAAAA;AALS,KAAD,CADZ,CAAA;AAAA,GADY,EASZ,CAACL,OAAD,EAAUE,SAAV,EAAqBD,MAArB,EAA6BG,OAA7B,EAAsCC,QAAtC,CATY,CAAd,CAAA;AAWD;;;;"}
@@ -1,25 +1,8 @@
1
- import { useMemo } from 'react';
2
- import createIntl from '../core/use-intl.esm3.js';
3
- import useIntlContext from './use-intl.esm7.js';
1
+ import useIntlContext from './use-intl.esm8.js';
4
2
 
5
- function useIntl() {
6
- var _useIntlContext = useIntlContext(),
7
- formats = _useIntlContext.formats,
8
- locale = _useIntlContext.locale,
9
- globalNow = _useIntlContext.now,
10
- onError = _useIntlContext.onError,
11
- timeZone = _useIntlContext.timeZone;
12
-
13
- return useMemo(function () {
14
- return createIntl({
15
- formats: formats,
16
- locale: locale,
17
- now: globalNow,
18
- onError: onError,
19
- timeZone: timeZone
20
- });
21
- }, [formats, globalNow, locale, onError, timeZone]);
3
+ function useLocale() {
4
+ return useIntlContext().locale;
22
5
  }
23
6
 
24
- export { useIntl as default };
7
+ export { useLocale as default };
25
8
  //# sourceMappingURL=use-intl.esm4.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm4.js","sources":["../../src/react/useIntl.tsx"],"sourcesContent":["import {useMemo} from 'react';\nimport createIntl from '../core/createIntl';\nimport useIntlContext from './useIntlContext';\n\nexport default function useIntl() {\n const {formats, locale, now: globalNow, onError, timeZone} = useIntlContext();\n\n return useMemo(\n () =>\n createIntl({\n formats,\n locale,\n now: globalNow,\n onError,\n timeZone\n }),\n [formats, globalNow, locale, onError, timeZone]\n );\n}\n"],"names":["useIntl","useIntlContext","formats","locale","globalNow","now","onError","timeZone","useMemo","createIntl"],"mappings":";;;;AAIc,SAAUA,OAAV,GAAiB;AAC7B,EAAA,IAAA,eAAA,GAA6DC,cAAc,EAA3E;AAAA,MAAOC,OAAP,mBAAOA,OAAP;AAAA,MAAgBC,MAAhB,mBAAgBA,MAAhB;AAAA,MAA6BC,SAA7B,mBAAwBC,GAAxB;AAAA,MAAwCC,OAAxC,mBAAwCA,OAAxC;AAAA,MAAiDC,QAAjD,mBAAiDA,QAAjD,CAAA;;AAEA,EAAA,OAAOC,OAAO,CACZ,YAAA;AAAA,IAAA,OACEC,UAAU,CAAC;AACTP,MAAAA,OAAO,EAAPA,OADS;AAETC,MAAAA,MAAM,EAANA,MAFS;AAGTE,MAAAA,GAAG,EAAED,SAHI;AAITE,MAAAA,OAAO,EAAPA,OAJS;AAKTC,MAAAA,QAAQ,EAARA,QAAAA;AALS,KAAD,CADZ,CAAA;AAAA,GADY,EASZ,CAACL,OAAD,EAAUE,SAAV,EAAqBD,MAArB,EAA6BG,OAA7B,EAAsCC,QAAtC,CATY,CAAd,CAAA;AAWD;;;;"}
1
+ {"version":3,"file":"use-intl.esm4.js","sources":["../../src/react/useLocale.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n"],"names":["useLocale","useIntlContext","locale"],"mappings":";;AAEc,SAAUA,SAAV,GAAmB;AAC/B,EAAOC,OAAAA,cAAc,GAAGC,MAAxB,CAAA;AACD;;;;"}
@@ -1,8 +1,50 @@
1
- import useIntlContext from './use-intl.esm7.js';
1
+ import { useState, useEffect } from 'react';
2
+ import useIntlContext from './use-intl.esm8.js';
2
3
 
3
- function useTimeZone() {
4
- return useIntlContext().timeZone;
4
+ function getNow() {
5
+ return new Date();
5
6
  }
7
+ /**
8
+ * Reading the current date via `new Date()` in components should be avoided, as
9
+ * it causes components to be impure and can lead to flaky tests. Instead, this
10
+ * hook can be used.
11
+ *
12
+ * By default, it returns the time when the component mounts. If `updateInterval`
13
+ * is specified, the value will be updated based on the interval.
14
+ *
15
+ * You can however also return a static value from this hook, if you
16
+ * configure the `now` parameter on the context provider. Note however,
17
+ * that if `updateInterval` is configured in this case, the component
18
+ * will initialize with the global value, but will afterwards update
19
+ * continuously based on the interval.
20
+ *
21
+ * For unit tests, this can be mocked to a constant value. For end-to-end
22
+ * testing, an environment parameter can be passed to the `now` parameter
23
+ * of the provider to mock this to a static value.
24
+ */
6
25
 
7
- export { useTimeZone as default };
26
+
27
+ function useNow(options) {
28
+ var updateInterval = options == null ? void 0 : options.updateInterval;
29
+
30
+ var _useIntlContext = useIntlContext(),
31
+ globalNow = _useIntlContext.now;
32
+
33
+ var _useState = useState(globalNow || getNow()),
34
+ now = _useState[0],
35
+ setNow = _useState[1];
36
+
37
+ useEffect(function () {
38
+ if (!updateInterval) return;
39
+ var intervalId = setInterval(function () {
40
+ setNow(getNow());
41
+ }, updateInterval);
42
+ return function () {
43
+ clearInterval(intervalId);
44
+ };
45
+ }, [globalNow, updateInterval]);
46
+ return now;
47
+ }
48
+
49
+ export { useNow as default };
8
50
  //# sourceMappingURL=use-intl.esm5.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm5.js","sources":["../../src/react/useTimeZone.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["useTimeZone","useIntlContext","timeZone"],"mappings":";;AAEc,SAAUA,WAAV,GAAqB;AACjC,EAAOC,OAAAA,cAAc,GAAGC,QAAxB,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm5.js","sources":["../../src/react/useNow.tsx"],"sourcesContent":["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"],"names":["getNow","Date","useNow","options","updateInterval","useIntlContext","globalNow","now","useState","setNow","useEffect","intervalId","setInterval","clearInterval"],"mappings":";;;AAOA,SAASA,MAAT,GAAe;AACb,EAAO,OAAA,IAAIC,IAAJ,EAAP,CAAA;AACD,CAAA;AAED;;;;;;;;;;;;;;;;;AAiBG;;;AACqB,SAAAC,MAAA,CAAOC,OAAP,EAAwB;AAC9C,EAAA,IAAMC,cAAc,GAAGD,OAAH,IAAGA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,OAAO,CAAEC,cAAhC,CAAA;;AAEA,EAAA,IAAA,eAAA,GAAyBC,cAAc,EAAvC;AAAA,MAAYC,SAAZ,mBAAOC,GAAP,CAAA;;AACA,EAAA,IAAA,SAAA,GAAsBC,QAAQ,CAACF,SAAS,IAAIN,MAAM,EAApB,CAA9B;AAAA,MAAOO,GAAP,GAAA,SAAA,CAAA,CAAA,CAAA;AAAA,MAAYE,MAAZ,GAAA,SAAA,CAAA,CAAA,CAAA,CAAA;;AAEAC,EAAAA,SAAS,CAAC,YAAK;AACb,IAAI,IAAA,CAACN,cAAL,EAAqB,OAAA;AAErB,IAAA,IAAMO,UAAU,GAAGC,WAAW,CAAC,YAAK;AAClCH,MAAAA,MAAM,CAACT,MAAM,EAAP,CAAN,CAAA;AACD,KAF6B,EAE3BI,cAF2B,CAA9B,CAAA;AAIA,IAAA,OAAO,YAAK;AACVS,MAAAA,aAAa,CAACF,UAAD,CAAb,CAAA;AACD,KAFD,CAAA;AAGD,GAVQ,EAUN,CAACL,SAAD,EAAYF,cAAZ,CAVM,CAAT,CAAA;AAYA,EAAA,OAAOG,GAAP,CAAA;AACD;;;;"}
@@ -1,8 +1,8 @@
1
- import useIntlContext from './use-intl.esm7.js';
1
+ import useIntlContext from './use-intl.esm8.js';
2
2
 
3
- function useLocale() {
4
- return useIntlContext().locale;
3
+ function useTimeZone() {
4
+ return useIntlContext().timeZone;
5
5
  }
6
6
 
7
- export { useLocale as default };
7
+ export { useTimeZone as default };
8
8
  //# sourceMappingURL=use-intl.esm6.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm6.js","sources":["../../src/react/useLocale.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useLocale() {\n return useIntlContext().locale;\n}\n"],"names":["useLocale","useIntlContext","locale"],"mappings":";;AAEc,SAAUA,SAAV,GAAmB;AAC/B,EAAOC,OAAAA,cAAc,GAAGC,MAAxB,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm6.js","sources":["../../src/react/useTimeZone.tsx"],"sourcesContent":["import useIntlContext from './useIntlContext';\n\nexport default function useTimeZone() {\n return useIntlContext().timeZone;\n}\n"],"names":["useTimeZone","useIntlContext","timeZone"],"mappings":";;AAEc,SAAUA,WAAV,GAAqB;AACjC,EAAOC,OAAAA,cAAc,GAAGC,QAAxB,CAAA;AACD;;;;"}
@@ -1,15 +1,6 @@
1
- import { useContext } from 'react';
2
- import IntlContext from './use-intl.esm9.js';
1
+ import { createContext } from 'react';
3
2
 
4
- function useIntlContext() {
5
- var context = useContext(IntlContext);
3
+ var IntlContext = /*#__PURE__*/createContext(undefined);
6
4
 
7
- if (!context) {
8
- throw new Error(process.env.NODE_ENV !== "production" ? 'No intl context found. Have you configured the provider?' : undefined);
9
- }
10
-
11
- return context;
12
- }
13
-
14
- export { useIntlContext as default };
5
+ export { IntlContext as default };
15
6
  //# sourceMappingURL=use-intl.esm7.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm7.js","sources":["../../src/react/useIntlContext.tsx"],"sourcesContent":["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"],"names":["useIntlContext","context","useContext","IntlContext","Error","undefined"],"mappings":";;;AAGc,SAAUA,cAAV,GAAwB;AACpC,EAAA,IAAMC,OAAO,GAAGC,UAAU,CAACC,WAAD,CAA1B,CAAA;;AAEA,EAAI,IAAA,CAACF,OAAL,EAAc;AACZ,IAAA,MAAM,IAAIG,KAAJ,CACJ,wCACI,0DADJ,GAEIC,SAHA,CAAN,CAAA;AAKD,GAAA;;AAED,EAAA,OAAOJ,OAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm7.js","sources":["../../src/react/IntlContext.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport AbstractIntlMessages from '../core/AbstractIntlMessages';\nimport Formats from '../core/Formats';\nimport IntlError from '../core/IntlError';\nimport {RichTranslationValues} from '../core/TranslationValues';\n\nexport type IntlContextShape = {\n messages?: AbstractIntlMessages;\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"],"names":["IntlContext","createContext","undefined"],"mappings":";;AAqBA,IAAMA,WAAW,gBAAGC,aAAa,CAA+BC,SAA/B;;;;"}
@@ -1,44 +1,15 @@
1
- import { useRef, useMemo } from 'react';
2
- import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm7.js';
3
- import resolveNamespace from '../core/use-intl.esm8.js';
4
- import useIntlContext from './use-intl.esm7.js';
1
+ import { useContext } from 'react';
2
+ import IntlContext from './use-intl.esm7.js';
5
3
 
6
- function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
7
- var _useIntlContext = useIntlContext(),
8
- defaultTranslationValues = _useIntlContext.defaultTranslationValues,
9
- globalFormats = _useIntlContext.formats,
10
- getMessageFallback = _useIntlContext.getMessageFallback,
11
- locale = _useIntlContext.locale,
12
- onError = _useIntlContext.onError,
13
- timeZone = _useIntlContext.timeZone; // The `namespacePrefix` is part of the type system.
14
- // See the comment in the hook invocation.
4
+ function useIntlContext() {
5
+ var context = useContext(IntlContext);
15
6
 
7
+ if (!context) {
8
+ throw new Error(process.env.NODE_ENV !== "production" ? 'No intl context found. Have you configured the provider?' : undefined);
9
+ }
16
10
 
17
- allMessages = allMessages[namespacePrefix];
18
- namespace = resolveNamespace(namespace, namespacePrefix);
19
- var cachedFormatsByLocaleRef = useRef({});
20
- var messagesOrError = useMemo(function () {
21
- return getMessagesOrError({
22
- messages: allMessages,
23
- namespace: namespace,
24
- onError: onError
25
- });
26
- }, [allMessages, namespace, onError]);
27
- var translate = useMemo(function () {
28
- return createBaseTranslator({
29
- cachedFormatsByLocale: cachedFormatsByLocaleRef.current,
30
- getMessageFallback: getMessageFallback,
31
- messagesOrError: messagesOrError,
32
- defaultTranslationValues: defaultTranslationValues,
33
- namespace: namespace,
34
- onError: onError,
35
- formats: globalFormats,
36
- locale: locale,
37
- timeZone: timeZone
38
- });
39
- }, [getMessageFallback, messagesOrError, defaultTranslationValues, namespace, onError, globalFormats, locale, timeZone]);
40
- return translate;
11
+ return context;
41
12
  }
42
13
 
43
- export { useTranslationsImpl as default };
14
+ export { useIntlContext as default };
44
15
  //# sourceMappingURL=use-intl.esm8.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm8.js","sources":["../../src/react/useTranslationsImpl.tsx"],"sourcesContent":["import {IntlMessageFormat} from 'intl-messageformat';\nimport {useMemo, useRef} from 'react';\nimport AbstractIntlMessages from '../core/AbstractIntlMessages';\nimport createBaseTranslator, {\n getMessagesOrError\n} from '../core/createBaseTranslator';\nimport resolveNamespace from '../core/resolveNamespace';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport useIntlContext from './useIntlContext';\n\nexport default function useTranslationsImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string) {\n const {\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback,\n locale,\n onError,\n timeZone\n } = useIntlContext();\n\n // The `namespacePrefix` is part of the type system.\n // See the comment in the hook invocation.\n allMessages = allMessages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(\n () => getMessagesOrError({messages: allMessages, namespace, onError}),\n [allMessages, namespace, onError]\n );\n\n const translate = useMemo(\n () =>\n createBaseTranslator({\n cachedFormatsByLocale: cachedFormatsByLocaleRef.current,\n getMessageFallback,\n messagesOrError,\n defaultTranslationValues,\n namespace,\n onError,\n formats: globalFormats,\n locale,\n timeZone\n }),\n [\n getMessageFallback,\n messagesOrError,\n defaultTranslationValues,\n namespace,\n onError,\n globalFormats,\n locale,\n timeZone\n ]\n );\n\n return translate;\n}\n"],"names":["useTranslationsImpl","allMessages","namespace","namespacePrefix","useIntlContext","defaultTranslationValues","globalFormats","formats","getMessageFallback","locale","onError","timeZone","resolveNamespace","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","getMessagesOrError","messages","translate","createBaseTranslator","cachedFormatsByLocale","current"],"mappings":";;;;;AAUwB,SAAAA,mBAAA,CAGtBC,WAHsB,EAGCC,SAHD,EAGuBC,eAHvB,EAG8C;AACpE,EAAA,IAAA,eAAA,GAOIC,cAAc,EAPlB;AAAA,MACEC,wBADF,mBACEA,wBADF;AAAA,MAEWC,aAFX,mBAEEC,OAFF;AAAA,MAGEC,kBAHF,mBAGEA,kBAHF;AAAA,MAIEC,MAJF,mBAIEA,MAJF;AAAA,MAKEC,OALF,mBAKEA,OALF;AAAA,MAMEC,QANF,GAAA,eAAA,CAMEA,QANF,CADoE;AAWpE;;;AACAV,EAAAA,WAAW,GAAGA,WAAW,CAACE,eAAD,CAAzB,CAAA;AACAD,EAAAA,SAAS,GAAGU,gBAAgB,CAACV,SAAD,EAAYC,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMU,wBAAwB,GAAGC,MAAM,CAErC,EAFqC,CAAvC,CAAA;AAIA,EAAMC,IAAAA,eAAe,GAAGC,OAAO,CAC7B,YAAA;AAAA,IAAA,OAAMC,kBAAkB,CAAC;AAACC,MAAAA,QAAQ,EAAEjB,WAAX;AAAwBC,MAAAA,SAAS,EAATA,SAAxB;AAAmCQ,MAAAA,OAAO,EAAPA,OAAAA;AAAnC,KAAD,CAAxB,CAAA;AAAA,GAD6B,EAE7B,CAACT,WAAD,EAAcC,SAAd,EAAyBQ,OAAzB,CAF6B,CAA/B,CAAA;AAKA,EAAMS,IAAAA,SAAS,GAAGH,OAAO,CACvB,YAAA;AAAA,IAAA,OACEI,oBAAoB,CAAC;AACnBC,MAAAA,qBAAqB,EAAER,wBAAwB,CAACS,OAD7B;AAEnBd,MAAAA,kBAAkB,EAAlBA,kBAFmB;AAGnBO,MAAAA,eAAe,EAAfA,eAHmB;AAInBV,MAAAA,wBAAwB,EAAxBA,wBAJmB;AAKnBH,MAAAA,SAAS,EAATA,SALmB;AAMnBQ,MAAAA,OAAO,EAAPA,OANmB;AAOnBH,MAAAA,OAAO,EAAED,aAPU;AAQnBG,MAAAA,MAAM,EAANA,MARmB;AASnBE,MAAAA,QAAQ,EAARA,QAAAA;AATmB,KAAD,CADtB,CAAA;AAAA,GADuB,EAavB,CACEH,kBADF,EAEEO,eAFF,EAGEV,wBAHF,EAIEH,SAJF,EAKEQ,OALF,EAMEJ,aANF,EAOEG,MAPF,EAQEE,QARF,CAbuB,CAAzB,CAAA;AAyBA,EAAA,OAAOQ,SAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm8.js","sources":["../../src/react/useIntlContext.tsx"],"sourcesContent":["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"],"names":["useIntlContext","context","useContext","IntlContext","Error","undefined"],"mappings":";;;AAGc,SAAUA,cAAV,GAAwB;AACpC,EAAA,IAAMC,OAAO,GAAGC,UAAU,CAACC,WAAD,CAA1B,CAAA;;AAEA,EAAI,IAAA,CAACF,OAAL,EAAc;AACZ,IAAA,MAAM,IAAIG,KAAJ,CACJ,wCACI,0DADJ,GAEIC,SAHA,CAAN,CAAA;AAKD,GAAA;;AAED,EAAA,OAAOJ,OAAP,CAAA;AACD;;;;"}
@@ -1,6 +1,44 @@
1
- import { createContext } from 'react';
1
+ import { useRef, useMemo } from 'react';
2
+ import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm7.js';
3
+ import resolveNamespace from '../core/use-intl.esm8.js';
4
+ import useIntlContext from './use-intl.esm8.js';
2
5
 
3
- var IntlContext = /*#__PURE__*/createContext(undefined);
6
+ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
7
+ var _useIntlContext = useIntlContext(),
8
+ defaultTranslationValues = _useIntlContext.defaultTranslationValues,
9
+ globalFormats = _useIntlContext.formats,
10
+ getMessageFallback = _useIntlContext.getMessageFallback,
11
+ locale = _useIntlContext.locale,
12
+ onError = _useIntlContext.onError,
13
+ timeZone = _useIntlContext.timeZone; // The `namespacePrefix` is part of the type system.
14
+ // See the comment in the hook invocation.
4
15
 
5
- export { IntlContext as default };
16
+
17
+ allMessages = allMessages[namespacePrefix];
18
+ namespace = resolveNamespace(namespace, namespacePrefix);
19
+ var cachedFormatsByLocaleRef = useRef({});
20
+ var messagesOrError = useMemo(function () {
21
+ return getMessagesOrError({
22
+ messages: allMessages,
23
+ namespace: namespace,
24
+ onError: onError
25
+ });
26
+ }, [allMessages, namespace, onError]);
27
+ var translate = useMemo(function () {
28
+ return createBaseTranslator({
29
+ cachedFormatsByLocale: cachedFormatsByLocaleRef.current,
30
+ getMessageFallback: getMessageFallback,
31
+ messagesOrError: messagesOrError,
32
+ defaultTranslationValues: defaultTranslationValues,
33
+ namespace: namespace,
34
+ onError: onError,
35
+ formats: globalFormats,
36
+ locale: locale,
37
+ timeZone: timeZone
38
+ });
39
+ }, [getMessageFallback, messagesOrError, defaultTranslationValues, namespace, onError, globalFormats, locale, timeZone]);
40
+ return translate;
41
+ }
42
+
43
+ export { useTranslationsImpl as default };
6
44
  //# sourceMappingURL=use-intl.esm9.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm9.js","sources":["../../src/react/IntlContext.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport AbstractIntlMessages from '../core/AbstractIntlMessages';\nimport Formats from '../core/Formats';\nimport IntlError from '../core/IntlError';\nimport {RichTranslationValues} from '../core/TranslationValues';\n\nexport type IntlContextShape = {\n messages?: AbstractIntlMessages;\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"],"names":["IntlContext","createContext","undefined"],"mappings":";;AAqBA,IAAMA,WAAW,gBAAGC,aAAa,CAA+BC,SAA/B;;;;"}
1
+ {"version":3,"file":"use-intl.esm9.js","sources":["../../src/react/useTranslationsImpl.tsx"],"sourcesContent":["import {IntlMessageFormat} from 'intl-messageformat';\nimport {useMemo, useRef} from 'react';\nimport AbstractIntlMessages from '../core/AbstractIntlMessages';\nimport createBaseTranslator, {\n getMessagesOrError\n} from '../core/createBaseTranslator';\nimport resolveNamespace from '../core/resolveNamespace';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport useIntlContext from './useIntlContext';\n\nexport default function useTranslationsImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string) {\n const {\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback,\n locale,\n onError,\n timeZone\n } = useIntlContext();\n\n // The `namespacePrefix` is part of the type system.\n // See the comment in the hook invocation.\n allMessages = allMessages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const cachedFormatsByLocaleRef = useRef<\n Record<string, Record<string, IntlMessageFormat>>\n >({});\n\n const messagesOrError = useMemo(\n () => getMessagesOrError({messages: allMessages, namespace, onError}),\n [allMessages, namespace, onError]\n );\n\n const translate = useMemo(\n () =>\n createBaseTranslator({\n cachedFormatsByLocale: cachedFormatsByLocaleRef.current,\n getMessageFallback,\n messagesOrError,\n defaultTranslationValues,\n namespace,\n onError,\n formats: globalFormats,\n locale,\n timeZone\n }),\n [\n getMessageFallback,\n messagesOrError,\n defaultTranslationValues,\n namespace,\n onError,\n globalFormats,\n locale,\n timeZone\n ]\n );\n\n return translate;\n}\n"],"names":["useTranslationsImpl","allMessages","namespace","namespacePrefix","useIntlContext","defaultTranslationValues","globalFormats","formats","getMessageFallback","locale","onError","timeZone","resolveNamespace","cachedFormatsByLocaleRef","useRef","messagesOrError","useMemo","getMessagesOrError","messages","translate","createBaseTranslator","cachedFormatsByLocale","current"],"mappings":";;;;;AAUwB,SAAAA,mBAAA,CAGtBC,WAHsB,EAGCC,SAHD,EAGuBC,eAHvB,EAG8C;AACpE,EAAA,IAAA,eAAA,GAOIC,cAAc,EAPlB;AAAA,MACEC,wBADF,mBACEA,wBADF;AAAA,MAEWC,aAFX,mBAEEC,OAFF;AAAA,MAGEC,kBAHF,mBAGEA,kBAHF;AAAA,MAIEC,MAJF,mBAIEA,MAJF;AAAA,MAKEC,OALF,mBAKEA,OALF;AAAA,MAMEC,QANF,GAAA,eAAA,CAMEA,QANF,CADoE;AAWpE;;;AACAV,EAAAA,WAAW,GAAGA,WAAW,CAACE,eAAD,CAAzB,CAAA;AACAD,EAAAA,SAAS,GAAGU,gBAAgB,CAACV,SAAD,EAAYC,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMU,wBAAwB,GAAGC,MAAM,CAErC,EAFqC,CAAvC,CAAA;AAIA,EAAMC,IAAAA,eAAe,GAAGC,OAAO,CAC7B,YAAA;AAAA,IAAA,OAAMC,kBAAkB,CAAC;AAACC,MAAAA,QAAQ,EAAEjB,WAAX;AAAwBC,MAAAA,SAAS,EAATA,SAAxB;AAAmCQ,MAAAA,OAAO,EAAPA,OAAAA;AAAnC,KAAD,CAAxB,CAAA;AAAA,GAD6B,EAE7B,CAACT,WAAD,EAAcC,SAAd,EAAyBQ,OAAzB,CAF6B,CAA/B,CAAA;AAKA,EAAMS,IAAAA,SAAS,GAAGH,OAAO,CACvB,YAAA;AAAA,IAAA,OACEI,oBAAoB,CAAC;AACnBC,MAAAA,qBAAqB,EAAER,wBAAwB,CAACS,OAD7B;AAEnBd,MAAAA,kBAAkB,EAAlBA,kBAFmB;AAGnBO,MAAAA,eAAe,EAAfA,eAHmB;AAInBV,MAAAA,wBAAwB,EAAxBA,wBAJmB;AAKnBH,MAAAA,SAAS,EAATA,SALmB;AAMnBQ,MAAAA,OAAO,EAAPA,OANmB;AAOnBH,MAAAA,OAAO,EAAED,aAPU;AAQnBG,MAAAA,MAAM,EAANA,MARmB;AASnBE,MAAAA,QAAQ,EAARA,QAAAA;AATmB,KAAD,CADtB,CAAA;AAAA,GADuB,EAavB,CACEH,kBADF,EAEEO,eAFF,EAGEV,wBAHF,EAIEH,SAJF,EAKEQ,OALF,EAMEJ,aANF,EAOEG,MAPF,EAQEE,QARF,CAbuB,CAAzB,CAAA;AAyBA,EAAA,OAAOQ,SAAP,CAAA;AACD;;;;"}
@@ -1,10 +1,10 @@
1
- export { default as IntlError, IntlErrorCode } from './core/use-intl.esm2.js';
2
- export { default as createTranslator } from './core/use-intl.esm.js';
1
+ export { default as IntlError, IntlErrorCode } from './core/use-intl.esm.js';
2
+ export { default as createTranslator } from './core/use-intl.esm2.js';
3
3
  export { default as createIntl } from './core/use-intl.esm3.js';
4
4
  export { default as IntlProvider } from './react/use-intl.esm.js';
5
- export { default as useTranslations } from './react/use-intl.esm3.js';
6
- export { default as useIntl } from './react/use-intl.esm4.js';
7
- export { default as useLocale } from './react/use-intl.esm6.js';
8
- export { default as useNow } from './react/use-intl.esm2.js';
9
- export { default as useTimeZone } from './react/use-intl.esm5.js';
5
+ export { default as useTranslations } from './react/use-intl.esm2.js';
6
+ export { default as useIntl } from './react/use-intl.esm3.js';
7
+ export { default as useLocale } from './react/use-intl.esm4.js';
8
+ export { default as useNow } from './react/use-intl.esm5.js';
9
+ export { default as useTimeZone } from './react/use-intl.esm6.js';
10
10
  //# sourceMappingURL=use-intl.esm.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-intl",
3
- "version": "2.10.0-alpha.4",
3
+ "version": "2.10.0",
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.",
@@ -61,5 +61,6 @@
61
61
  },
62
62
  "engines": {
63
63
  "node": ">=10"
64
- }
64
+ },
65
+ "gitHead": "cda1f5415724c973500f4b693f208ba1145baa43"
65
66
  }