use-intl 2.9.2-alpha.0 → 2.10.0-alpha.3
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/CHANGELOG.md +463 -0
- package/dist/core/use-intl.esm.js +140 -27
- package/dist/core/use-intl.esm.js.map +1 -1
- package/dist/core/use-intl.esm2.js +27 -140
- package/dist/core/use-intl.esm2.js.map +1 -1
- package/dist/core/use-intl.esm3.js +2 -2
- package/dist/core/use-intl.esm4.js +12 -56
- package/dist/core/use-intl.esm4.js.map +1 -1
- package/dist/core/use-intl.esm5.js +56 -12
- package/dist/core/use-intl.esm5.js.map +1 -1
- package/dist/core/use-intl.esm6.js +7 -29
- package/dist/core/use-intl.esm6.js.map +1 -1
- package/dist/core/use-intl.esm7.js +9 -9
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/core/use-intl.esm8.js +29 -7
- package/dist/core/use-intl.esm8.js.map +1 -1
- package/dist/react/IntlContext.d.ts +2 -19
- package/dist/react/IntlContextValue.d.ts +19 -0
- package/dist/react/IntlProvider.d.ts +2 -16
- package/dist/react/IntlProviderProps.d.ts +15 -0
- package/dist/react/getIntlContextValue.d.ts +18 -0
- package/dist/react/use-intl.esm.js +12 -21
- package/dist/react/use-intl.esm.js.map +1 -1
- package/dist/react/use-intl.esm10.js +44 -0
- package/dist/react/use-intl.esm10.js.map +1 -0
- package/dist/react/use-intl.esm2.js +2 -2
- package/dist/react/use-intl.esm3.js +20 -30
- package/dist/react/use-intl.esm3.js.map +1 -1
- package/dist/react/use-intl.esm4.js +46 -4
- package/dist/react/use-intl.esm4.js.map +1 -1
- package/dist/react/use-intl.esm5.js +1 -1
- package/dist/react/use-intl.esm6.js +4 -46
- package/dist/react/use-intl.esm6.js.map +1 -1
- package/dist/react/use-intl.esm7.js +3 -12
- package/dist/react/use-intl.esm7.js.map +1 -1
- package/dist/react/use-intl.esm8.js +30 -3
- package/dist/react/use-intl.esm8.js.map +1 -1
- package/dist/react/use-intl.esm9.js +9 -38
- package/dist/react/use-intl.esm9.js.map +1 -1
- package/dist/react/useIntlContext.d.ts +1 -1
- package/dist/src/core/AbstractIntlMessages.d.ts +9 -0
- package/dist/src/core/AbstractIntlMessages.js +2 -0
- package/dist/src/core/AbstractIntlMessages.js.map +1 -0
- package/dist/src/core/DateTimeFormatOptions.d.ts +72 -0
- package/dist/src/core/DateTimeFormatOptions.js +3 -0
- package/dist/src/core/DateTimeFormatOptions.js.map +1 -0
- package/dist/src/core/Formats.d.ts +7 -0
- package/dist/src/core/Formats.js +2 -0
- package/dist/src/core/Formats.js.map +1 -0
- package/dist/src/core/IntlConfiguration.d.ts +46 -0
- package/dist/src/core/IntlConfiguration.js +2 -0
- package/dist/src/core/IntlConfiguration.js.map +1 -0
- package/dist/src/core/IntlError.d.ts +13 -0
- package/dist/src/core/IntlError.js +23 -0
- package/dist/src/core/IntlError.js.map +1 -0
- package/dist/src/core/IntlMessages.d.ts +2 -0
- package/dist/src/core/IntlMessages.js +4 -0
- package/dist/src/core/IntlMessages.js.map +1 -0
- package/dist/src/core/NumberFormatOptions.d.ts +2 -0
- package/dist/src/core/NumberFormatOptions.js +2 -0
- package/dist/src/core/NumberFormatOptions.js.map +1 -0
- package/dist/src/core/TranslationValues.d.ts +5 -0
- package/dist/src/core/TranslationValues.js +2 -0
- package/dist/src/core/TranslationValues.js.map +1 -0
- package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +10 -0
- package/dist/src/core/convertFormatsToIntlMessageFormat.js +31 -0
- package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +1 -0
- package/dist/src/core/createBaseTranslator.d.ts +26 -0
- package/dist/src/core/createBaseTranslator.js +185 -0
- package/dist/src/core/createBaseTranslator.js.map +1 -0
- package/dist/src/core/createIntl.d.ts +17 -0
- package/dist/src/core/createIntl.js +126 -0
- package/dist/src/core/createIntl.js.map +1 -0
- package/dist/src/core/createTranslator.d.ts +48 -0
- package/dist/src/core/createTranslator.js +24 -0
- package/dist/src/core/createTranslator.js.map +1 -0
- package/dist/src/core/createTranslatorImpl.d.ts +14 -0
- package/dist/src/core/createTranslatorImpl.js +43 -0
- package/dist/src/core/createTranslatorImpl.js.map +1 -0
- package/dist/src/core/defaults.d.ts +11 -0
- package/dist/src/core/defaults.js +11 -0
- package/dist/src/core/defaults.js.map +1 -0
- package/dist/src/core/index.d.ts +8 -0
- package/dist/src/core/index.js +4 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/core/resolveNamespace.d.ts +5 -0
- package/dist/src/core/resolveNamespace.js +10 -0
- package/dist/src/core/resolveNamespace.js.map +1 -0
- package/dist/src/core/utils/MessageKeys.d.ts +5 -0
- package/dist/src/core/utils/MessageKeys.js +2 -0
- package/dist/src/core/utils/MessageKeys.js.map +1 -0
- package/dist/src/core/utils/NamespaceKeys.d.ts +5 -0
- package/dist/src/core/utils/NamespaceKeys.js +2 -0
- package/dist/src/core/utils/NamespaceKeys.js.map +1 -0
- package/dist/src/core/utils/NestedKeyOf.d.ts +4 -0
- package/dist/src/core/utils/NestedKeyOf.js +2 -0
- package/dist/src/core/utils/NestedKeyOf.js.map +1 -0
- package/dist/src/core/utils/NestedValueOf.d.ts +2 -0
- package/dist/src/core/utils/NestedValueOf.js +2 -0
- package/dist/src/core/utils/NestedValueOf.js.map +1 -0
- package/dist/src/core/validateMessages.d.ts +3 -0
- package/dist/src/core/validateMessages.js +22 -0
- package/dist/src/core/validateMessages.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/react/IntlContext.d.ts +4 -0
- package/dist/src/react/IntlContext.js +4 -0
- package/dist/src/react/IntlContext.js.map +1 -0
- package/dist/src/react/IntlContextValue.d.ts +19 -0
- package/dist/src/react/IntlContextValue.js +2 -0
- package/dist/src/react/IntlContextValue.js.map +1 -0
- package/dist/src/react/IntlProvider.d.ts +2 -0
- package/dist/src/react/IntlProvider.js +7 -0
- package/dist/src/react/IntlProvider.js.map +1 -0
- package/dist/src/react/IntlProviderProps.d.ts +15 -0
- package/dist/src/react/IntlProviderProps.js +2 -0
- package/dist/src/react/IntlProviderProps.js.map +1 -0
- package/dist/src/react/getIntlContextValue.d.ts +18 -0
- package/dist/src/react/getIntlContextValue.js +21 -0
- package/dist/src/react/getIntlContextValue.js.map +1 -0
- package/dist/src/react/index.d.ts +6 -0
- package/dist/src/react/index.js +7 -0
- package/dist/src/react/index.js.map +1 -0
- package/dist/src/react/useIntl.d.ts +5 -0
- package/dist/src/react/useIntl.js +14 -0
- package/dist/src/react/useIntl.js.map +1 -0
- package/dist/src/react/useIntlContext.d.ts +1 -0
- package/dist/src/react/useIntlContext.js +12 -0
- package/dist/src/react/useIntlContext.js.map +1 -0
- package/dist/src/react/useLocale.d.ts +1 -0
- package/dist/src/react/useLocale.js +5 -0
- package/dist/src/react/useLocale.js.map +1 -0
- package/dist/src/react/useNow.d.ts +23 -0
- package/dist/src/react/useNow.js +40 -0
- package/dist/src/react/useNow.js.map +1 -0
- package/dist/src/react/useTimeZone.d.ts +1 -0
- package/dist/src/react/useTimeZone.js +5 -0
- package/dist/src/react/useTimeZone.js.map +1 -0
- package/dist/src/react/useTranslations.d.ts +44 -0
- package/dist/src/react/useTranslations.js +21 -0
- package/dist/src/react/useTranslations.js.map +1 -0
- package/dist/src/react/useTranslationsImpl.d.ts +8 -0
- package/dist/src/react/useTranslationsImpl.js +35 -0
- package/dist/src/react/useTranslationsImpl.js.map +1 -0
- package/dist/use-intl.cjs.development.js +37 -28
- 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 +6 -6
- package/package.json +6 -5
- package/src/core/createBaseTranslator.tsx +11 -7
- package/src/core/createIntl.tsx +2 -2
- package/src/core/createTranslatorImpl.tsx +1 -1
- package/src/react/IntlContext.tsx +2 -20
- package/src/react/IntlContextValue.tsx +21 -0
- package/src/react/IntlProvider.tsx +5 -36
- package/src/react/IntlProviderProps.tsx +17 -0
- package/src/react/getIntlContextValue.tsx +30 -0
- package/src/react/useIntlContext.tsx +1 -1
- package/LICENSE +0 -21
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import createIntl from '../core/createIntl';
|
|
3
|
+
import useIntlContext from './useIntlContext';
|
|
4
|
+
export default function useIntl() {
|
|
5
|
+
const { formats, locale, now: globalNow, onError, timeZone } = useIntlContext();
|
|
6
|
+
return useMemo(() => createIntl({
|
|
7
|
+
formats,
|
|
8
|
+
locale,
|
|
9
|
+
now: globalNow,
|
|
10
|
+
onError,
|
|
11
|
+
timeZone
|
|
12
|
+
}), [formats, globalNow, locale, onError, timeZone]);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=useIntl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useIntl.js","sourceRoot":"","sources":["../../../src/react/useIntl.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,OAAO;IAC7B,MAAM,EAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,GAAG,cAAc,EAAE,CAAC;IAE9E,OAAO,OAAO,CACZ,GAAG,EAAE,CACH,UAAU,CAAC;QACT,OAAO;QACP,MAAM;QACN,GAAG,EAAE,SAAS;QACd,OAAO;QACP,QAAQ;KACT,CAAC,EACJ,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAChD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useIntlContext(): import("./IntlContextValue").default;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import IntlContext from './IntlContext';
|
|
3
|
+
export default function useIntlContext() {
|
|
4
|
+
const context = useContext(IntlContext);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error(process.env.NODE_ENV !== 'production'
|
|
7
|
+
? 'No intl context found. Have you configured the provider?'
|
|
8
|
+
: undefined);
|
|
9
|
+
}
|
|
10
|
+
return context;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=useIntlContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useIntlContext.js","sourceRoot":"","sources":["../../../src/react/useIntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,OAAO,CAAC;AACjC,OAAO,WAAW,MAAM,eAAe,CAAC;AAExC,MAAM,CAAC,OAAO,UAAU,cAAc;IACpC,MAAM,OAAO,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAExC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;YACnC,CAAC,CAAC,0DAA0D;YAC5D,CAAC,CAAC,SAAS,CACd,CAAC;KACH;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useLocale(): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLocale.js","sourceRoot":"","sources":["../../../src/react/useLocale.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,SAAS;IAC/B,OAAO,cAAc,EAAE,CAAC,MAAM,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare type Options = {
|
|
2
|
+
updateInterval?: number;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* Reading the current date via `new Date()` in components should be avoided, as
|
|
6
|
+
* it causes components to be impure and can lead to flaky tests. Instead, this
|
|
7
|
+
* hook can be used.
|
|
8
|
+
*
|
|
9
|
+
* By default, it returns the time when the component mounts. If `updateInterval`
|
|
10
|
+
* is specified, the value will be updated based on the interval.
|
|
11
|
+
*
|
|
12
|
+
* You can however also return a static value from this hook, if you
|
|
13
|
+
* configure the `now` parameter on the context provider. Note however,
|
|
14
|
+
* that if `updateInterval` is configured in this case, the component
|
|
15
|
+
* will initialize with the global value, but will afterwards update
|
|
16
|
+
* continuously based on the interval.
|
|
17
|
+
*
|
|
18
|
+
* For unit tests, this can be mocked to a constant value. For end-to-end
|
|
19
|
+
* testing, an environment parameter can be passed to the `now` parameter
|
|
20
|
+
* of the provider to mock this to a static value.
|
|
21
|
+
*/
|
|
22
|
+
export default function useNow(options?: Options): Date;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import useIntlContext from './useIntlContext';
|
|
3
|
+
function getNow() {
|
|
4
|
+
return new Date();
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Reading the current date via `new Date()` in components should be avoided, as
|
|
8
|
+
* it causes components to be impure and can lead to flaky tests. Instead, this
|
|
9
|
+
* hook can be used.
|
|
10
|
+
*
|
|
11
|
+
* By default, it returns the time when the component mounts. If `updateInterval`
|
|
12
|
+
* is specified, the value will be updated based on the interval.
|
|
13
|
+
*
|
|
14
|
+
* You can however also return a static value from this hook, if you
|
|
15
|
+
* configure the `now` parameter on the context provider. Note however,
|
|
16
|
+
* that if `updateInterval` is configured in this case, the component
|
|
17
|
+
* will initialize with the global value, but will afterwards update
|
|
18
|
+
* continuously based on the interval.
|
|
19
|
+
*
|
|
20
|
+
* For unit tests, this can be mocked to a constant value. For end-to-end
|
|
21
|
+
* testing, an environment parameter can be passed to the `now` parameter
|
|
22
|
+
* of the provider to mock this to a static value.
|
|
23
|
+
*/
|
|
24
|
+
export default function useNow(options) {
|
|
25
|
+
const updateInterval = options?.updateInterval;
|
|
26
|
+
const { now: globalNow } = useIntlContext();
|
|
27
|
+
const [now, setNow] = useState(globalNow || getNow());
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!updateInterval)
|
|
30
|
+
return;
|
|
31
|
+
const intervalId = setInterval(() => {
|
|
32
|
+
setNow(getNow());
|
|
33
|
+
}, updateInterval);
|
|
34
|
+
return () => {
|
|
35
|
+
clearInterval(intervalId);
|
|
36
|
+
};
|
|
37
|
+
}, [globalNow, updateInterval]);
|
|
38
|
+
return now;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=useNow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNow.js","sourceRoot":"","sources":["../../../src/react/useNow.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,SAAS,EAAC,MAAM,OAAO,CAAC;AAC1C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAM9C,SAAS,MAAM;IACb,OAAO,IAAI,IAAI,EAAE,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAiB;IAC9C,MAAM,cAAc,GAAG,OAAO,EAAE,cAAc,CAAC;IAE/C,MAAM,EAAC,GAAG,EAAE,SAAS,EAAC,GAAG,cAAc,EAAE,CAAC;IAC1C,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,SAAS,IAAI,MAAM,EAAE,CAAC,CAAC;IAEtD,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,cAAc;YAAE,OAAO;QAE5B,MAAM,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE;YAClC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;QACnB,CAAC,EAAE,cAAc,CAAC,CAAC;QAEnB,OAAO,GAAG,EAAE;YACV,aAAa,CAAC,UAAU,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;IAEhC,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useTimeZone(): string | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTimeZone.js","sourceRoot":"","sources":["../../../src/react/useTimeZone.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,WAAW;IACjC,OAAO,cAAc,EAAE,CAAC,QAAQ,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ReactElement, ReactNodeArray } from 'react';
|
|
2
|
+
import Formats from '../core/Formats';
|
|
3
|
+
import TranslationValues, { RichTranslationValues } from '../core/TranslationValues';
|
|
4
|
+
import MessageKeys from '../core/utils/MessageKeys';
|
|
5
|
+
import NamespaceKeys from '../core/utils/NamespaceKeys';
|
|
6
|
+
import NestedKeyOf from '../core/utils/NestedKeyOf';
|
|
7
|
+
import NestedValueOf from '../core/utils/NestedValueOf';
|
|
8
|
+
/**
|
|
9
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
10
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
11
|
+
*
|
|
12
|
+
* If no namespace is provided, all available messages are returned.
|
|
13
|
+
* The namespace can also indicate nesting by using a dot
|
|
14
|
+
* (e.g. `namespace.Component`).
|
|
15
|
+
*/
|
|
16
|
+
export default function useTranslations<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>(namespace?: NestedKey): {
|
|
17
|
+
<TargetKey extends MessageKeys<NestedValueOf<{
|
|
18
|
+
'!': IntlMessages;
|
|
19
|
+
}, [
|
|
20
|
+
NestedKey
|
|
21
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
22
|
+
'!': IntlMessages;
|
|
23
|
+
}, [
|
|
24
|
+
NestedKey
|
|
25
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: TranslationValues, formats?: Partial<Formats>): string;
|
|
26
|
+
rich<TargetKey extends MessageKeys<NestedValueOf<{
|
|
27
|
+
'!': IntlMessages;
|
|
28
|
+
}, [
|
|
29
|
+
NestedKey
|
|
30
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
31
|
+
'!': IntlMessages;
|
|
32
|
+
}, [
|
|
33
|
+
NestedKey
|
|
34
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValues, formats?: Partial<Formats>): string | ReactElement | ReactNodeArray;
|
|
35
|
+
raw<TargetKey extends MessageKeys<NestedValueOf<{
|
|
36
|
+
'!': IntlMessages;
|
|
37
|
+
}, [
|
|
38
|
+
NestedKey
|
|
39
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
40
|
+
'!': IntlMessages;
|
|
41
|
+
}, [
|
|
42
|
+
NestedKey
|
|
43
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey): any;
|
|
44
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import useIntlContext from './useIntlContext';
|
|
2
|
+
import useTranslationsImpl from './useTranslationsImpl';
|
|
3
|
+
/**
|
|
4
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
5
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
6
|
+
*
|
|
7
|
+
* If no namespace is provided, all available messages are returned.
|
|
8
|
+
* The namespace can also indicate nesting by using a dot
|
|
9
|
+
* (e.g. `namespace.Component`).
|
|
10
|
+
*/
|
|
11
|
+
export default function useTranslations(namespace) {
|
|
12
|
+
const context = useIntlContext();
|
|
13
|
+
const messages = context.messages;
|
|
14
|
+
// We have to wrap the actual hook so the type inference for the optional
|
|
15
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
16
|
+
// The prefix ("!") is arbitrary.
|
|
17
|
+
return useTranslationsImpl({ '!': messages },
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
namespace ? `!.${namespace}` : '!', '!');
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=useTranslations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTranslations.js","sourceRoot":"","sources":["../../../src/react/useTranslations.tsx"],"names":[],"mappings":"AASA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,mBAAmB,MAAM,uBAAuB,CAAC;AAExD;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CAMrC,SAAqB;IA6DrB,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;IACjC,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAwB,CAAC;IAElD,yEAAyE;IACzE,6EAA6E;IAC7E,iCAAiC;IACjC,OAAO,mBAAmB,CAIxB,EAAC,GAAG,EAAE,QAAQ,EAAC;IACf,aAAa;IACb,SAAS,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC,CAAC,CAAC,GAAG,EAClC,GAAG,CACJ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import AbstractIntlMessages from '../core/AbstractIntlMessages';
|
|
3
|
+
import NestedKeyOf from '../core/utils/NestedKeyOf';
|
|
4
|
+
export default function useTranslationsImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string): {
|
|
5
|
+
<TargetKey extends unknown>(key: TargetKey, values?: import("../core/TranslationValues").default | undefined, formats?: Partial<import("../core/Formats").default> | undefined): string;
|
|
6
|
+
rich: (key: string, values?: import("..").RichTranslationValues | undefined, formats?: Partial<import("../core/Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
|
|
7
|
+
raw(key: string): any;
|
|
8
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useMemo, useRef } from 'react';
|
|
2
|
+
import createBaseTranslator, { getMessagesOrError } from '../core/createBaseTranslator';
|
|
3
|
+
import resolveNamespace from '../core/resolveNamespace';
|
|
4
|
+
import useIntlContext from './useIntlContext';
|
|
5
|
+
export default function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
6
|
+
const { defaultTranslationValues, formats: globalFormats, getMessageFallback, locale, onError, timeZone } = useIntlContext();
|
|
7
|
+
// The `namespacePrefix` is part of the type system.
|
|
8
|
+
// See the comment in the hook invocation.
|
|
9
|
+
allMessages = allMessages[namespacePrefix];
|
|
10
|
+
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
11
|
+
const cachedFormatsByLocaleRef = useRef({});
|
|
12
|
+
const messagesOrError = useMemo(() => getMessagesOrError({ messages: allMessages, namespace, onError }), [allMessages, namespace, onError]);
|
|
13
|
+
const translate = useMemo(() => createBaseTranslator({
|
|
14
|
+
cachedFormatsByLocale: cachedFormatsByLocaleRef.current,
|
|
15
|
+
getMessageFallback,
|
|
16
|
+
messagesOrError,
|
|
17
|
+
defaultTranslationValues,
|
|
18
|
+
namespace,
|
|
19
|
+
onError,
|
|
20
|
+
formats: globalFormats,
|
|
21
|
+
locale,
|
|
22
|
+
timeZone
|
|
23
|
+
}), [
|
|
24
|
+
getMessageFallback,
|
|
25
|
+
messagesOrError,
|
|
26
|
+
defaultTranslationValues,
|
|
27
|
+
namespace,
|
|
28
|
+
onError,
|
|
29
|
+
globalFormats,
|
|
30
|
+
locale,
|
|
31
|
+
timeZone
|
|
32
|
+
]);
|
|
33
|
+
return translate;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=useTranslationsImpl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTranslationsImpl.js","sourceRoot":"","sources":["../../../src/react/useTranslationsImpl.tsx"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAE,MAAM,EAAC,MAAM,OAAO,CAAC;AAEtC,OAAO,oBAAoB,EAAE,EAC3B,kBAAkB,EACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AAExD,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAGzC,WAAqB,EAAE,SAAoB,EAAE,eAAuB;IACpE,MAAM,EACJ,wBAAwB,EACxB,OAAO,EAAE,aAAa,EACtB,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,QAAQ,EACT,GAAG,cAAc,EAAE,CAAC;IAErB,oDAAoD;IACpD,0CAA0C;IAC1C,WAAW,GAAG,WAAW,CAAC,eAAe,CAAa,CAAC;IACvD,SAAS,GAAG,gBAAgB,CAAC,SAAS,EAAE,eAAe,CAAc,CAAC;IAEtE,MAAM,wBAAwB,GAAG,MAAM,CAErC,EAAE,CAAC,CAAC;IAEN,MAAM,eAAe,GAAG,OAAO,CAC7B,GAAG,EAAE,CAAC,kBAAkB,CAAC,EAAC,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAC,CAAC,EACrE,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,CAAC,CAClC,CAAC;IAEF,MAAM,SAAS,GAAG,OAAO,CACvB,GAAG,EAAE,CACH,oBAAoB,CAAC;QACnB,qBAAqB,EAAE,wBAAwB,CAAC,OAAO;QACvD,kBAAkB;QAClB,eAAe;QACf,wBAAwB;QACxB,SAAS;QACT,OAAO;QACP,OAAO,EAAE,aAAa;QACtB,MAAM;QACN,QAAQ;KACT,CAAC,EACJ;QACE,kBAAkB;QAClB,eAAe;QACf,wBAAwB;QACxB,SAAS;QACT,OAAO;QACP,aAAa;QACb,MAAM;QACN,QAAQ;KACT,CACF,CAAC;IAEF,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -266,13 +266,13 @@ function getMessagesOrError(_ref) {
|
|
|
266
266
|
|
|
267
267
|
try {
|
|
268
268
|
if (!messages) {
|
|
269
|
-
throw new Error("development" !==
|
|
269
|
+
throw new Error("development" !== 'production' ? "No messages were configured on the provider." : undefined);
|
|
270
270
|
}
|
|
271
271
|
|
|
272
272
|
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
273
273
|
|
|
274
274
|
if (!retrievedMessages) {
|
|
275
|
-
throw new Error("development" !==
|
|
275
|
+
throw new Error("development" !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
276
276
|
}
|
|
277
277
|
|
|
278
278
|
return retrievedMessages;
|
|
@@ -366,7 +366,7 @@ function createBaseTranslator(_ref2) {
|
|
|
366
366
|
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
367
367
|
|
|
368
368
|
if (formattedMessage == null) {
|
|
369
|
-
throw new Error("development" !==
|
|
369
|
+
throw new Error("development" !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
370
370
|
} // Limit the function signature to return strings or React elements
|
|
371
371
|
|
|
372
372
|
|
|
@@ -427,13 +427,13 @@ function resolveNamespace(namespace, namespacePrefix) {
|
|
|
427
427
|
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
428
428
|
}
|
|
429
429
|
|
|
430
|
-
var _excluded$
|
|
430
|
+
var _excluded$3 = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
431
431
|
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
432
432
|
var getMessageFallback = _ref.getMessageFallback,
|
|
433
433
|
messages = _ref.messages,
|
|
434
434
|
namespace = _ref.namespace,
|
|
435
435
|
onError = _ref.onError,
|
|
436
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$
|
|
436
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$3);
|
|
437
437
|
|
|
438
438
|
// The `namespacePrefix` is part of the type system.
|
|
439
439
|
// See the comment in the function invocation.
|
|
@@ -479,7 +479,7 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
479
479
|
return base;
|
|
480
480
|
}
|
|
481
481
|
|
|
482
|
-
var _excluded$
|
|
482
|
+
var _excluded$2 = ["onError", "getMessageFallback", "messages", "namespace"];
|
|
483
483
|
/**
|
|
484
484
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
485
485
|
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
@@ -496,7 +496,7 @@ function createTranslator(_ref) {
|
|
|
496
496
|
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
497
497
|
messages = _ref.messages,
|
|
498
498
|
namespace = _ref.namespace,
|
|
499
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$
|
|
499
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
|
|
500
500
|
|
|
501
501
|
// We have to wrap the actual function so the type inference for the optional
|
|
502
502
|
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
@@ -633,7 +633,7 @@ function createIntl(_ref) {
|
|
|
633
633
|
if (globalNow) {
|
|
634
634
|
now = globalNow;
|
|
635
635
|
} else {
|
|
636
|
-
throw new Error("development" !==
|
|
636
|
+
throw new Error("development" !== 'production' ? "The `now` parameter wasn't provided to `formatRelativeTime` and there was no global fallback configured on the provider." : undefined);
|
|
637
637
|
}
|
|
638
638
|
}
|
|
639
639
|
|
|
@@ -661,6 +661,8 @@ function createIntl(_ref) {
|
|
|
661
661
|
};
|
|
662
662
|
}
|
|
663
663
|
|
|
664
|
+
var IntlContext = /*#__PURE__*/React.createContext(undefined);
|
|
665
|
+
|
|
664
666
|
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
665
667
|
Object.entries(messages).forEach(function (_ref) {
|
|
666
668
|
var key = _ref[0],
|
|
@@ -689,33 +691,40 @@ function validateMessages(messages, onError) {
|
|
|
689
691
|
}
|
|
690
692
|
}
|
|
691
693
|
|
|
692
|
-
var
|
|
694
|
+
var _excluded$1 = ["getMessageFallback", "messages", "onError"];
|
|
695
|
+
/**
|
|
696
|
+
* Enhances the incoming props with defaults.
|
|
697
|
+
*/
|
|
693
698
|
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
var children = _ref.children,
|
|
697
|
-
_ref$onError = _ref.onError,
|
|
698
|
-
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
699
|
-
_ref$getMessageFallba = _ref.getMessageFallback,
|
|
700
|
-
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
699
|
+
function getIntlContextValue(_ref) {
|
|
700
|
+
var getMessageFallback = _ref.getMessageFallback,
|
|
701
701
|
messages = _ref.messages,
|
|
702
|
-
|
|
702
|
+
onError = _ref.onError,
|
|
703
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
704
|
+
|
|
705
|
+
var finalOnError = onError || defaultOnError;
|
|
706
|
+
var finalGetMessageFallback = getMessageFallback || defaultGetMessageFallback;
|
|
703
707
|
|
|
704
708
|
{
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
validateMessages(messages, onError);
|
|
709
|
-
}
|
|
710
|
-
}, [messages, onError]);
|
|
709
|
+
if (messages) {
|
|
710
|
+
validateMessages(messages, finalOnError);
|
|
711
|
+
}
|
|
711
712
|
}
|
|
712
713
|
|
|
714
|
+
return _extends({}, rest, {
|
|
715
|
+
messages: messages,
|
|
716
|
+
onError: finalOnError,
|
|
717
|
+
getMessageFallback: finalGetMessageFallback
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
var _excluded = ["children"];
|
|
722
|
+
function IntlProvider(_ref) {
|
|
723
|
+
var children = _ref.children,
|
|
724
|
+
props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
725
|
+
|
|
713
726
|
return React__default["default"].createElement(IntlContext.Provider, {
|
|
714
|
-
value:
|
|
715
|
-
messages: messages,
|
|
716
|
-
onError: onError,
|
|
717
|
-
getMessageFallback: getMessageFallback
|
|
718
|
-
})
|
|
727
|
+
value: getIntlContextValue(props)
|
|
719
728
|
}, children);
|
|
720
729
|
}
|
|
721
730
|
|