use-intl 2.7.5 → 2.7.7-alpha.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.
- package/dist/{AbstractIntlMessages.d.ts → core/AbstractIntlMessages.d.ts} +0 -0
- package/dist/{DateTimeFormatOptions.d.ts → core/DateTimeFormatOptions.d.ts} +0 -0
- package/dist/{Formats.d.ts → core/Formats.d.ts} +0 -0
- package/dist/{IntlError.d.ts → core/IntlError.d.ts} +0 -0
- package/dist/{IntlMessages.d.ts → core/IntlMessages.d.ts} +0 -0
- package/dist/{NumberFormatOptions.d.ts → core/NumberFormatOptions.d.ts} +0 -0
- package/dist/{TranslationValues.d.ts → core/TranslationValues.d.ts} +2 -2
- package/dist/core/config.d.ts +6 -0
- package/dist/{convertFormatsToIntlMessageFormat.d.ts → core/convertFormatsToIntlMessageFormat.d.ts} +0 -0
- package/dist/core/createBaseTranslator.d.ts +34 -0
- package/dist/{useIntl.d.ts → core/createIntl.d.ts} +11 -1
- package/dist/core/createTranslator.d.ts +47 -0
- package/dist/core/createTranslatorImpl.d.ts +21 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/resolveNamespace.d.ts +5 -0
- package/dist/core/use-intl.esm.js +32 -0
- package/dist/core/use-intl.esm.js.map +1 -0
- package/dist/{use-intl.esm8.js → core/use-intl.esm2.js} +2 -2
- package/dist/core/use-intl.esm2.js.map +1 -0
- package/dist/{use-intl.esm4.js → core/use-intl.esm3.js} +12 -12
- package/dist/core/use-intl.esm3.js.map +1 -0
- package/dist/core/use-intl.esm4.js +61 -0
- package/dist/core/use-intl.esm4.js.map +1 -0
- package/dist/core/use-intl.esm5.js +13 -0
- package/dist/core/use-intl.esm5.js.map +1 -0
- package/dist/{use-intl.esm10.js → core/use-intl.esm6.js} +2 -2
- package/dist/core/use-intl.esm6.js.map +1 -0
- package/dist/core/use-intl.esm7.js +213 -0
- package/dist/core/use-intl.esm7.js.map +1 -0
- package/dist/core/use-intl.esm8.js +10 -0
- package/dist/core/use-intl.esm8.js.map +1 -0
- package/dist/{use-intl.esm13.js → core/use-intl.esm9.js} +2 -2
- package/dist/core/use-intl.esm9.js.map +1 -0
- package/dist/{utils → core/utils}/MessageKeys.d.ts +0 -0
- package/dist/{utils → core/utils}/NamespaceKeys.d.ts +0 -0
- package/dist/{utils → core/utils}/NestedKeyOf.d.ts +0 -0
- package/dist/{utils → core/utils}/NestedValueOf.d.ts +0 -0
- package/dist/{validateMessages.d.ts → core/validateMessages.d.ts} +0 -0
- package/dist/index.d.ts +2 -12
- package/dist/{IntlContext.d.ts → react/IntlContext.d.ts} +4 -4
- package/dist/{IntlProvider.d.ts → react/IntlProvider.d.ts} +4 -4
- package/dist/react/index.d.ts +6 -0
- package/dist/react/use-intl.esm.js +25 -0
- package/dist/react/use-intl.esm.js.map +1 -0
- package/dist/{use-intl.esm3.js → react/use-intl.esm2.js} +4 -4
- package/dist/react/use-intl.esm2.js.map +1 -0
- package/dist/{use-intl.esm7.js → react/use-intl.esm3.js} +2 -2
- package/dist/react/use-intl.esm3.js.map +1 -0
- package/dist/{use-intl.esm6.js → react/use-intl.esm4.js} +2 -2
- package/dist/react/use-intl.esm4.js.map +1 -0
- package/dist/react/use-intl.esm5.js +36 -0
- package/dist/react/use-intl.esm5.js.map +1 -0
- package/dist/{use-intl.esm5.js → react/use-intl.esm6.js} +2 -2
- package/dist/react/use-intl.esm6.js.map +1 -0
- package/dist/{use-intl.esm11.js → react/use-intl.esm7.js} +1 -1
- package/dist/react/use-intl.esm7.js.map +1 -0
- package/dist/react/use-intl.esm8.js +44 -0
- package/dist/react/use-intl.esm8.js.map +1 -0
- package/dist/{use-intl.esm9.js → react/use-intl.esm9.js} +0 -0
- package/dist/react/use-intl.esm9.js.map +1 -0
- package/dist/react/useIntl.d.ts +5 -0
- package/dist/{useIntlContext.d.ts → react/useIntlContext.d.ts} +0 -0
- package/dist/{useLocale.d.ts → react/useLocale.d.ts} +0 -0
- package/dist/{useNow.d.ts → react/useNow.d.ts} +0 -0
- package/dist/{useTimeZone.d.ts → react/useTimeZone.d.ts} +0 -0
- package/dist/{useTranslations.d.ts → react/useTranslations.d.ts} +6 -6
- package/dist/react/useTranslationsImpl.d.ts +8 -0
- package/dist/use-intl.cjs.development.js +363 -218
- 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 +9 -7
- package/dist/use-intl.esm.js.map +1 -1
- package/package.json +3 -2
- package/src/{AbstractIntlMessages.tsx → core/AbstractIntlMessages.tsx} +0 -0
- package/src/{DateTimeFormatOptions.tsx → core/DateTimeFormatOptions.tsx} +0 -0
- package/src/{Formats.tsx → core/Formats.tsx} +0 -0
- package/src/{IntlError.tsx → core/IntlError.tsx} +0 -0
- package/src/{IntlMessages.tsx → core/IntlMessages.tsx} +0 -0
- package/src/{NumberFormatOptions.tsx → core/NumberFormatOptions.tsx} +0 -0
- package/src/{TranslationValues.tsx → core/TranslationValues.tsx} +8 -2
- package/src/core/config.tsx +15 -0
- package/src/{convertFormatsToIntlMessageFormat.tsx → core/convertFormatsToIntlMessageFormat.tsx} +0 -0
- package/src/core/createBaseTranslator.tsx +323 -0
- package/src/{useIntl.tsx → core/createIntl.tsx} +17 -4
- package/src/core/createTranslator.tsx +110 -0
- package/src/core/createTranslatorImpl.tsx +95 -0
- package/src/core/index.tsx +11 -0
- package/src/core/resolveNamespace.tsx +12 -0
- package/src/{utils → core/utils}/MessageKeys.tsx +0 -0
- package/src/{utils → core/utils}/NamespaceKeys.tsx +0 -0
- package/src/{utils → core/utils}/NestedKeyOf.tsx +0 -0
- package/src/{utils → core/utils}/NestedValueOf.tsx +0 -0
- package/src/{validateMessages.tsx → core/validateMessages.tsx} +0 -0
- package/src/index.tsx +2 -15
- package/src/{IntlContext.tsx → react/IntlContext.tsx} +4 -4
- package/src/{IntlProvider.tsx → react/IntlProvider.tsx} +6 -19
- package/src/react/index.tsx +6 -0
- package/src/react/useIntl.tsx +19 -0
- package/src/{useIntlContext.tsx → react/useIntlContext.tsx} +0 -0
- package/src/{useLocale.tsx → react/useLocale.tsx} +0 -0
- package/src/{useNow.tsx → react/useNow.tsx} +0 -0
- package/src/{useTimeZone.tsx → react/useTimeZone.tsx} +0 -0
- package/src/{useTranslations.tsx → react/useTranslations.tsx} +9 -7
- package/src/react/useTranslationsImpl.tsx +64 -0
- package/dist/use-intl.esm10.js.map +0 -1
- package/dist/use-intl.esm11.js.map +0 -1
- package/dist/use-intl.esm12.js +0 -213
- package/dist/use-intl.esm12.js.map +0 -1
- package/dist/use-intl.esm13.js.map +0 -1
- package/dist/use-intl.esm2.js +0 -48
- package/dist/use-intl.esm2.js.map +0 -1
- package/dist/use-intl.esm3.js.map +0 -1
- package/dist/use-intl.esm4.js.map +0 -1
- package/dist/use-intl.esm5.js.map +0 -1
- package/dist/use-intl.esm6.js.map +0 -1
- package/dist/use-intl.esm7.js.map +0 -1
- package/dist/use-intl.esm8.js.map +0 -1
- package/dist/use-intl.esm9.js.map +0 -1
- package/dist/useTranslationsImpl.d.ts +0 -12
- package/src/useTranslationsImpl.tsx +0 -328
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
|
-
declare type TranslationValue = string | number | boolean | Date | null | undefined;
|
|
2
|
+
export declare type TranslationValue = string | number | boolean | Date | null | undefined;
|
|
3
3
|
declare type TranslationValues = Record<string, TranslationValue>;
|
|
4
|
-
export declare type RichTranslationValues = Record<string, TranslationValue | ((
|
|
4
|
+
export declare type RichTranslationValues = Record<string, TranslationValue | ((chunks: ReactNode) => ReactNode)>;
|
|
5
5
|
export default TranslationValues;
|
package/dist/{convertFormatsToIntlMessageFormat.d.ts → core/convertFormatsToIntlMessageFormat.d.ts}
RENAMED
|
File without changes
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import IntlMessageFormat from 'intl-messageformat';
|
|
2
|
+
import { ReactElement, ReactNodeArray } from 'react';
|
|
3
|
+
import AbstractIntlMessages from './AbstractIntlMessages';
|
|
4
|
+
import Formats from './Formats';
|
|
5
|
+
import IntlError from './IntlError';
|
|
6
|
+
import TranslationValues, { RichTranslationValues } from './TranslationValues';
|
|
7
|
+
import MessageKeys from './utils/MessageKeys';
|
|
8
|
+
import NestedKeyOf from './utils/NestedKeyOf';
|
|
9
|
+
import NestedValueOf from './utils/NestedValueOf';
|
|
10
|
+
export declare function getMessagesOrError<Messages extends AbstractIntlMessages>({ messages, namespace, onError }: {
|
|
11
|
+
messages: Messages;
|
|
12
|
+
namespace?: string;
|
|
13
|
+
onError?(error: IntlError): void;
|
|
14
|
+
}): AbstractIntlMessages | IntlError;
|
|
15
|
+
export declare type CreateBaseTranslatorProps<Messages> = {
|
|
16
|
+
cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;
|
|
17
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
18
|
+
formats?: Partial<Formats>;
|
|
19
|
+
getMessageFallback?(info: {
|
|
20
|
+
error: IntlError;
|
|
21
|
+
key: string;
|
|
22
|
+
namespace?: string;
|
|
23
|
+
}): string;
|
|
24
|
+
locale: string;
|
|
25
|
+
messagesOrError: Messages | IntlError;
|
|
26
|
+
namespace?: string;
|
|
27
|
+
onError(error: IntlError): void;
|
|
28
|
+
timeZone?: string;
|
|
29
|
+
};
|
|
30
|
+
export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ cachedFormatsByLocale, defaultTranslationValues, formats: globalFormats, getMessageFallback, locale, messagesOrError, namespace, onError, timeZone }: CreateBaseTranslatorProps<Messages>): {
|
|
31
|
+
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues | undefined, formats?: Partial<Formats> | undefined): string;
|
|
32
|
+
rich: (key: string, values?: RichTranslationValues | undefined, formats?: Partial<Formats> | undefined) => string | ReactElement | ReactNodeArray;
|
|
33
|
+
raw(key: string): any;
|
|
34
|
+
};
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import DateTimeFormatOptions from './DateTimeFormatOptions';
|
|
2
|
+
import Formats from './Formats';
|
|
3
|
+
import IntlError from './IntlError';
|
|
2
4
|
import NumberFormatOptions from './NumberFormatOptions';
|
|
3
|
-
|
|
5
|
+
declare type Props = {
|
|
6
|
+
locale: string;
|
|
7
|
+
timeZone?: string;
|
|
8
|
+
onError?(error: IntlError): void;
|
|
9
|
+
formats?: Partial<Formats>;
|
|
10
|
+
now?: Date;
|
|
11
|
+
};
|
|
12
|
+
export default function createIntl({ formats, locale, now: globalNow, onError, timeZone }: Props): {
|
|
4
13
|
formatDateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions | undefined) => string;
|
|
5
14
|
formatNumber: (value: number, formatOrOptions?: string | NumberFormatOptions | undefined) => string;
|
|
6
15
|
formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
|
|
7
16
|
};
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Formats from './Formats';
|
|
2
|
+
import TranslationValues from './TranslationValues';
|
|
3
|
+
import { CreateTranslatorImplProps, CoreRichTranslationValues } from './createTranslatorImpl';
|
|
4
|
+
import MessageKeys from './utils/MessageKeys';
|
|
5
|
+
import NamespaceKeys from './utils/NamespaceKeys';
|
|
6
|
+
import NestedKeyOf from './utils/NestedKeyOf';
|
|
7
|
+
import NestedValueOf from './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 createTranslator<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>({ messages, namespace, ...rest }: Omit<CreateTranslatorImplProps<IntlMessages>, 'messagesOrError' | 'namespace' | 'namespacePrefix'> & {
|
|
17
|
+
messages: IntlMessages;
|
|
18
|
+
namespace?: NestedKey;
|
|
19
|
+
}): {
|
|
20
|
+
<TargetKey extends MessageKeys<NestedValueOf<{
|
|
21
|
+
'!': IntlMessages;
|
|
22
|
+
}, [
|
|
23
|
+
NestedKey
|
|
24
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
25
|
+
'!': IntlMessages;
|
|
26
|
+
}, [
|
|
27
|
+
NestedKey
|
|
28
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: TranslationValues, formats?: Partial<Formats>): string;
|
|
29
|
+
rich<TargetKey extends MessageKeys<NestedValueOf<{
|
|
30
|
+
'!': IntlMessages;
|
|
31
|
+
}, [
|
|
32
|
+
NestedKey
|
|
33
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
34
|
+
'!': IntlMessages;
|
|
35
|
+
}, [
|
|
36
|
+
NestedKey
|
|
37
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: CoreRichTranslationValues, formats?: Partial<Formats>): string;
|
|
38
|
+
raw<TargetKey extends MessageKeys<NestedValueOf<{
|
|
39
|
+
'!': IntlMessages;
|
|
40
|
+
}, [
|
|
41
|
+
NestedKey
|
|
42
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
43
|
+
'!': IntlMessages;
|
|
44
|
+
}, [
|
|
45
|
+
NestedKey
|
|
46
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey): any;
|
|
47
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import AbstractIntlMessages from './AbstractIntlMessages';
|
|
2
|
+
import IntlError from './IntlError';
|
|
3
|
+
import { TranslationValue } from './TranslationValues';
|
|
4
|
+
import { CreateBaseTranslatorProps } from './createBaseTranslator';
|
|
5
|
+
import NestedKeyOf from './utils/NestedKeyOf';
|
|
6
|
+
export declare type CoreRichTranslationValues = Record<string, TranslationValue | ((chunks: string) => string)>;
|
|
7
|
+
export declare type CreateTranslatorImplProps<Messages> = Omit<CreateBaseTranslatorProps<Messages>, 'messagesOrError' | 'onError' | 'getMessageFallback' | 'namespace'> & {
|
|
8
|
+
messages: Messages;
|
|
9
|
+
onError?(error: IntlError): void;
|
|
10
|
+
getMessageFallback?(info: {
|
|
11
|
+
error: IntlError;
|
|
12
|
+
key: string;
|
|
13
|
+
namespace?: string;
|
|
14
|
+
}): string;
|
|
15
|
+
namespace: string;
|
|
16
|
+
};
|
|
17
|
+
export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ messages, namespace, onError, getMessageFallback, ...rest }: CreateTranslatorImplProps<Messages>, namespacePrefix: string): {
|
|
18
|
+
(key: import("./utils/MessageKeys").default<import("./utils/NestedValueOf").default<Messages, NestedKey>, NestedKeyOf<import("./utils/NestedValueOf").default<Messages, NestedKey>>>, values?: import("./TranslationValues").default | undefined, formats?: Partial<import("./Formats").default> | undefined): string;
|
|
19
|
+
rich(key: string, values: CoreRichTranslationValues, formats?: Partial<import("./Formats").default> | undefined): string;
|
|
20
|
+
raw: (key: string) => any;
|
|
21
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { default as AbstractIntlMessages } from './AbstractIntlMessages';
|
|
2
|
+
export { default as TranslationValues, RichTranslationValues } from './TranslationValues';
|
|
3
|
+
export { default as Formats } from './Formats';
|
|
4
|
+
export { default as DateTimeFormatOptions } from './DateTimeFormatOptions';
|
|
5
|
+
export { default as NumberFormatOptions } from './NumberFormatOptions';
|
|
6
|
+
export { default as IntlError, IntlErrorCode } from './IntlError';
|
|
7
|
+
export { default as createTranslator } from './createTranslator';
|
|
8
|
+
export { default as createIntl } from './createIntl';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
+
import createTranslatorImpl from './use-intl.esm4.js';
|
|
3
|
+
|
|
4
|
+
var _excluded = ["messages", "namespace"];
|
|
5
|
+
/**
|
|
6
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
7
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
8
|
+
*
|
|
9
|
+
* If no namespace is provided, all available messages are returned.
|
|
10
|
+
* The namespace can also indicate nesting by using a dot
|
|
11
|
+
* (e.g. `namespace.Component`).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function createTranslator(_ref) {
|
|
15
|
+
var messages = _ref.messages,
|
|
16
|
+
namespace = _ref.namespace,
|
|
17
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
18
|
+
|
|
19
|
+
// We have to wrap the actual function so the type inference for the optional
|
|
20
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
21
|
+
// The prefix ("!") is arbitrary.
|
|
22
|
+
return createTranslatorImpl(_extends({}, rest, {
|
|
23
|
+
messages: {
|
|
24
|
+
'!': messages
|
|
25
|
+
},
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
namespace: namespace ? "!." + namespace : '!'
|
|
28
|
+
}), '!');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { createTranslator as default };
|
|
32
|
+
//# sourceMappingURL=use-intl.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intl.esm.js","sources":["../../src/core/createTranslator.tsx"],"sourcesContent":["import Formats from './Formats';\nimport TranslationValues from './TranslationValues';\nimport createTranslatorImpl, {\n CreateTranslatorImplProps,\n CoreRichTranslationValues\n} from './createTranslatorImpl';\nimport MessageKeys from './utils/MessageKeys';\nimport NamespaceKeys from './utils/NamespaceKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\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 createTranslator<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>({\n messages,\n namespace,\n ...rest\n}: Omit<\n CreateTranslatorImplProps<IntlMessages>,\n 'messagesOrError' | 'namespace' | 'namespacePrefix'\n> & {\n messages: IntlMessages;\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?: CoreRichTranslationValues,\n formats?: Partial<Formats>\n ): string;\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 // We have to wrap the actual function 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 createTranslatorImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {\n ...rest,\n messages: {'!': messages},\n // @ts-ignore\n namespace: namespace ? `!.${namespace}` : '!'\n },\n '!'\n );\n}\n"],"names":["createTranslator","messages","namespace","rest","createTranslatorImpl"],"mappings":";;;;AAWA;;;;;;;AAOG;;AACqB,SAAAA,gBAAA,CAevB,IAAA,EAAA;AAAA,EATCC,IAAAA,QASD,QATCA,QASD;AAAA,MARCC,SAQD,QARCA,SAQD;AAAA,MAPIC,IAOJ,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AA4DC;AACA;AACA;AACA,EAAOC,OAAAA,oBAAoB,cAKpBD,IALoB,EAAA;AAMvBF,IAAAA,QAAQ,EAAE;AAAC,MAAKA,GAAAA,EAAAA,QAAAA;AAAN,KANa;AAOvB;AACAC,IAAAA,SAAS,EAAEA,SAAS,GAAA,IAAA,GAAQA,SAAR,GAAsB,GAAA;AARnB,GAAA,CAAA,EAUzB,GAVyB,CAA3B,CAAA;AAYD;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { inheritsLoose as _inheritsLoose, wrapNativeSuper as _wrapNativeSuper } from '
|
|
1
|
+
import { inheritsLoose as _inheritsLoose, wrapNativeSuper as _wrapNativeSuper } from '../_virtual/use-intl.esm.js';
|
|
2
2
|
|
|
3
3
|
var IntlErrorCode;
|
|
4
4
|
|
|
@@ -39,4 +39,4 @@ var IntlError = /*#__PURE__*/function (_Error) {
|
|
|
39
39
|
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
40
40
|
|
|
41
41
|
export { IntlErrorCode, IntlError as default };
|
|
42
|
-
//# sourceMappingURL=use-intl.
|
|
42
|
+
//# sourceMappingURL=use-intl.esm2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intl.esm2.js","sources":["../../src/core/IntlError.tsx"],"sourcesContent":["export enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n INVALID_KEY = 'INVALID_KEY',\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"],"names":["IntlErrorCode","IntlError","code","originalMessage","message","Error"],"mappings":";;IAAYA,cAAZ;;AAAA,CAAA,UAAYA,aAAZ,EAAyB;AACvBA,EAAAA,aAAA,CAAA,iBAAA,CAAA,GAAA,iBAAA,CAAA;AACAA,EAAAA,aAAA,CAAA,gBAAA,CAAA,GAAA,gBAAA,CAAA;AACAA,EAAAA,aAAA,CAAA,mBAAA,CAAA,GAAA,mBAAA,CAAA;AACAA,EAAAA,aAAA,CAAA,iBAAA,CAAA,GAAA,iBAAA,CAAA;AACAA,EAAAA,aAAA,CAAA,aAAA,CAAA,GAAA,aAAA,CAAA;AACAA,EAAAA,aAAA,CAAA,kBAAA,CAAA,GAAA,kBAAA,CAAA;AACD,CAPD,EAAYA,aAAa,KAAbA,aAAa,GAOxB,EAPwB,CAAzB,CAAA,CAAA;;IASqBC;;;AAInB,EAAYC,SAAAA,SAAAA,CAAAA,IAAZ,EAAiCC,eAAjC,EAAyD;AAAA,IAAA,IAAA,KAAA,CAAA;;AACvD,IAAIC,IAAAA,OAAO,GAAWF,IAAtB,CAAA;;AACA,IAAA,IAAIC,eAAJ,EAAqB;AACnBC,MAAAA,OAAO,IAAI,IAAA,GAAOD,eAAlB,CAAA;AACD,KAAA;;AACD,IAAA,KAAA,GAAA,MAAA,CAAA,IAAA,CAAA,IAAA,EAAMC,OAAN,CAAA,IAAA,IAAA,CAAA;AALuD,IAAA,KAAA,CAHzCF,IAGyC,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,KAAA,CAFzCC,eAEyC,GAAA,KAAA,CAAA,CAAA;AAOvD,IAAKD,KAAAA,CAAAA,IAAL,GAAYA,IAAZ,CAAA;;AACA,IAAA,IAAIC,eAAJ,EAAqB;AACnB,MAAKA,KAAAA,CAAAA,eAAL,GAAuBA,eAAvB,CAAA;AACD,KAAA;;AAVsD,IAAA,OAAA,KAAA,CAAA;AAWxD,GAAA;;;iCAfoCE;;;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { extends as _extends } from '
|
|
2
|
-
import IntlError, { IntlErrorCode } from './use-intl.
|
|
3
|
-
import
|
|
1
|
+
import { extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
+
import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
|
|
3
|
+
import { defaultOnError } from './use-intl.esm5.js';
|
|
4
4
|
|
|
5
5
|
var MINUTE = 60;
|
|
6
6
|
var HOUR = MINUTE * 60;
|
|
@@ -44,13 +44,13 @@ function getRelativeTimeFormatConfig(seconds) {
|
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
function
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
onError =
|
|
53
|
-
timeZone =
|
|
47
|
+
function createIntl(_ref) {
|
|
48
|
+
var formats = _ref.formats,
|
|
49
|
+
locale = _ref.locale,
|
|
50
|
+
globalNow = _ref.now,
|
|
51
|
+
_ref$onError = _ref.onError,
|
|
52
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
53
|
+
timeZone = _ref.timeZone;
|
|
54
54
|
|
|
55
55
|
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
|
|
56
56
|
var options;
|
|
@@ -151,5 +151,5 @@ function useIntl() {
|
|
|
151
151
|
};
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
export {
|
|
155
|
-
//# sourceMappingURL=use-intl.
|
|
154
|
+
export { createIntl as default };
|
|
155
|
+
//# sourceMappingURL=use-intl.esm3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createIntl.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport {defaultOnError} from './config';\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\ntype Props = {\n locale: string;\n timeZone?: string;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createIntl({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone\n}: Props) {\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 | 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"],"names":["MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","value","unit","round","createIntl","formats","locale","globalNow","now","onError","defaultOnError","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","undefined","getFormattedValue","formatter","String","FORMATTING_ERROR","message","formatDateTime","dateTime","Intl","DateTimeFormat","format","formatNumber","number","NumberFormat","formatRelativeTime","date","Error","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric"],"mappings":";;;;AAMA,IAAMA,MAAM,GAAG,EAAf,CAAA;AACA,IAAMC,IAAI,GAAGD,MAAM,GAAG,EAAtB,CAAA;AACA,IAAME,GAAG,GAAGD,IAAI,GAAG,EAAnB,CAAA;AACA,IAAME,IAAI,GAAGD,GAAG,GAAG,CAAnB,CAAA;AACA,IAAME,KAAK,GAAGF,GAAG,IAAI,MAAM,EAAV,CAAjB;;AACA,IAAMG,IAAI,GAAGH,GAAG,GAAG,GAAnB,CAAA;;AAEA,SAASI,2BAAT,CAAqCC,OAArC,EAAoD;AAClD,EAAA,IAAMC,QAAQ,GAAGC,IAAI,CAACC,GAAL,CAASH,OAAT,CAAjB,CAAA;AACA,EAAA,IAAII,KAAJ,EAAWC,IAAX,CAFkD;AAKlD;;AAEA,EAAIJ,IAAAA,QAAQ,GAAGR,MAAf,EAAuB;AACrBY,IAAAA,IA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AIS,SAAJ,CAAcC,aAAa,CAACM,gBAA5B,EAA+CR,KAAe,CAACS,OAA/D,CADK,CAAP,CAAA;AAGA,MAAOF,OAAAA,MAAM,CAACY,IAAD,CAAb,CAAA;AACD,KAAA;AACF,GAAA;;AAED,EAAO,OAAA;AAACT,IAAAA,cAAc,EAAdA,cAAD;AAAiBK,IAAAA,YAAY,EAAZA,YAAjB;AAA+BG,IAAAA,kBAAkB,EAAlBA,kBAAAA;AAA/B,GAAP,CAAA;AACD;;;;"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
+
import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
|
|
3
|
+
import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm5.js';
|
|
4
|
+
import createBaseTranslator, { getMessagesOrError } from './use-intl.esm7.js';
|
|
5
|
+
import resolveNamespace from './use-intl.esm8.js';
|
|
6
|
+
|
|
7
|
+
var _excluded = ["messages", "namespace", "onError", "getMessageFallback"];
|
|
8
|
+
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
9
|
+
var messages = _ref.messages,
|
|
10
|
+
namespace = _ref.namespace,
|
|
11
|
+
_ref$onError = _ref.onError,
|
|
12
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
|
|
13
|
+
_ref$getMessageFallba = _ref.getMessageFallback,
|
|
14
|
+
getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
|
|
15
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
16
|
+
|
|
17
|
+
// The `namespacePrefix` is part of the type system.
|
|
18
|
+
// See the comment in the function invocation.
|
|
19
|
+
messages = messages[namespacePrefix];
|
|
20
|
+
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
21
|
+
var translator = createBaseTranslator(_extends({}, rest, {
|
|
22
|
+
onError: onError,
|
|
23
|
+
messagesOrError: getMessagesOrError({
|
|
24
|
+
messages: messages,
|
|
25
|
+
namespace: namespace,
|
|
26
|
+
onError: onError
|
|
27
|
+
})
|
|
28
|
+
}));
|
|
29
|
+
var originalRich = translator.rich;
|
|
30
|
+
|
|
31
|
+
function base() {
|
|
32
|
+
return translator.apply(void 0, arguments);
|
|
33
|
+
} // Augment `t.rich` to return plain strings
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
base.rich = function (key,
|
|
37
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
38
|
+
values, formats) {
|
|
39
|
+
// `chunks` is returned as a string when no React element
|
|
40
|
+
// is used, therefore it's safe to cast this type.
|
|
41
|
+
var result = originalRich(key, values, formats); // When only string chunks are provided to the parser, only strings should be returned here.
|
|
42
|
+
|
|
43
|
+
if (typeof result !== 'string') {
|
|
44
|
+
var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== "production" ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
|
|
45
|
+
onError(error);
|
|
46
|
+
return getMessageFallback({
|
|
47
|
+
error: error,
|
|
48
|
+
key: key,
|
|
49
|
+
namespace: namespace
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
base.raw = translator.raw;
|
|
57
|
+
return base;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { createTranslatorImpl as default };
|
|
61
|
+
//# sourceMappingURL=use-intl.esm4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intl.esm4.js","sources":["../../src/core/createTranslatorImpl.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport {RichTranslationValues, TranslationValue} from './TranslationValues';\nimport {defaultGetMessageFallback, defaultOnError} from './config';\nimport createBaseTranslator, {\n CreateBaseTranslatorProps,\n getMessagesOrError\n} from './createBaseTranslator';\nimport resolveNamespace from './resolveNamespace';\nimport NestedKeyOf from './utils/NestedKeyOf';\n\nexport type CoreRichTranslationValues = Record<\n string,\n TranslationValue | ((chunks: string) => string)\n>;\n\nexport type CreateTranslatorImplProps<Messages> = Omit<\n CreateBaseTranslatorProps<Messages>,\n 'messagesOrError' | 'onError' | 'getMessageFallback' | 'namespace'\n> & {\n messages: Messages;\n onError?(error: IntlError): void;\n getMessageFallback?(info: {\n error: IntlError;\n key: string;\n namespace?: string;\n }): string;\n namespace: string;\n};\n\nexport default function createTranslatorImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(\n {\n messages,\n namespace,\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n ...rest\n }: CreateTranslatorImplProps<Messages>,\n namespacePrefix: string\n) {\n // The `namespacePrefix` is part of the type system.\n // See the comment in the function invocation.\n messages = messages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const translator = createBaseTranslator<Messages, NestedKey>({\n ...rest,\n onError,\n messagesOrError: getMessagesOrError({\n messages,\n namespace,\n onError\n }) as Messages | IntlError\n });\n\n const originalRich = translator.rich;\n\n function base(...args: Parameters<typeof translator>) {\n return translator(...args);\n }\n\n // Augment `t.rich` to return plain strings\n base.rich = (\n key: Parameters<typeof originalRich>[0],\n /** Key value pairs for values to interpolate into the message. */\n values: CoreRichTranslationValues,\n formats?: Parameters<typeof originalRich>[2]\n ): string => {\n // `chunks` is returned as a string when no React element\n // is used, therefore it's safe to cast this type.\n const result = originalRich(key, values as RichTranslationValues, formats);\n\n // When only string chunks are provided to the parser, only strings should be returned here.\n if (typeof result !== 'string') {\n const error = new IntlError(\n IntlErrorCode.FORMATTING_ERROR,\n __DEV__\n ? \"`createTranslator` only accepts functions for rich text formatting that receive and return strings.\\n\\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})\"\n : undefined\n );\n\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n return result;\n };\n\n base.raw = translator.raw;\n\n return base;\n}\n"],"names":["createTranslatorImpl","namespacePrefix","messages","namespace","onError","defaultOnError","getMessageFallback","defaultGetMessageFallback","rest","resolveNamespace","translator","createBaseTranslator","messagesOrError","getMessagesOrError","originalRich","rich","base","key","values","formats","result","error","IntlError","IntlErrorCode","FORMATTING_ERROR","undefined","raw"],"mappings":";;;;;;;AA8BwB,SAAAA,oBAAA,CAWtBC,IAAAA,EAAAA,eAXsB,EAWC;AAAA,EANrBC,IAAAA,QAMqB,QANrBA,QAMqB;AAAA,MALrBC,SAKqB,QALrBA,SAKqB;AAAA,MAAA,YAAA,GAAA,IAAA,CAJrBC,OAIqB;AAAA,MAJrBA,OAIqB,6BAJXC,cAIW,GAAA,YAAA;AAAA,MAAA,qBAAA,GAAA,IAAA,CAHrBC,kBAGqB;AAAA,MAHrBA,kBAGqB,sCAHAC,yBAGA,GAAA,qBAAA;AAAA,MAFlBC,IAEkB,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AAEvB;AACA;AACAN,EAAAA,QAAQ,GAAGA,QAAQ,CAACD,eAAD,CAAnB,CAAA;AACAE,EAAAA,SAAS,GAAGM,gBAAgB,CAACN,SAAD,EAAYF,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMS,UAAU,GAAGC,oBAAoB,CAAA,QAAA,CAAA,EAAA,EAClCH,IADkC,EAAA;AAErCJ,IAAAA,OAAO,EAAPA,OAFqC;AAGrCQ,IAAAA,eAAe,EAAEC,kBAAkB,CAAC;AAClCX,MAAAA,QAAQ,EAARA,QADkC;AAElCC,MAAAA,SAAS,EAATA,SAFkC;AAGlCC,MAAAA,OAAO,EAAPA,OAAAA;AAHkC,KAAD,CAAA;AAHE,GAAvC,CAAA,CAAA,CAAA;AAUA,EAAA,IAAMU,YAAY,GAAGJ,UAAU,CAACK,IAAhC,CAAA;;AAEA,EAAA,SAASC,IAAT,GAAoD;AAClD,IAAON,OAAAA,UAAU,MAAV,CAAP,KAAA,CAAA,EAAA,SAAA,CAAA,CAAA;AACD,GArBsB;;;AAwBvBM,EAAAA,IAAI,CAACD,IAAL,GAAY,UACVE,GADU;AAEV;AACAC,EAAAA,MAHU,EAIVC,OAJU,EAKA;AACV;AACA;AACA,IAAMC,IAAAA,MAAM,GAAGN,YAAY,CAACG,GAAD,EAAMC,MAAN,EAAuCC,OAAvC,CAA3B,CAHU;;AAMV,IAAA,IAAI,OAAOC,MAAP,KAAkB,QAAtB,EAAgC;AAC9B,MAAA,IAAMC,KAAK,GAAG,IAAIC,SAAJ,CACZC,aAAa,CAACC,gBADF,EAEZ,OAAA,CAAA,GAAA,CAAA,QAAA,KAAA,YAAA,GACI,iKADJ,GAEIC,SAJQ,CAAd,CAAA;AAOArB,MAAAA,OAAO,CAACiB,KAAD,CAAP,CAAA;AACA,MAAA,OAAOf,kBAAkB,CAAC;AAACe,QAAAA,KAAK,EAALA,KAAD;AAAQJ,QAAAA,GAAG,EAAHA,GAAR;AAAad,QAAAA,SAAS,EAATA,SAAAA;AAAb,OAAD,CAAzB,CAAA;AACD,KAAA;;AAED,IAAA,OAAOiB,MAAP,CAAA;AACD,GAxBD,CAAA;;AA0BAJ,EAAAA,IAAI,CAACU,GAAL,GAAWhB,UAAU,CAACgB,GAAtB,CAAA;AAEA,EAAA,OAAOV,IAAP,CAAA;AACD;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function defaultGetMessageFallback(_ref) {
|
|
2
|
+
var key = _ref.key,
|
|
3
|
+
namespace = _ref.namespace;
|
|
4
|
+
return [namespace, key].filter(function (part) {
|
|
5
|
+
return part != null;
|
|
6
|
+
}).join('.');
|
|
7
|
+
}
|
|
8
|
+
function defaultOnError(error) {
|
|
9
|
+
console.error(error);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { defaultGetMessageFallback, defaultOnError };
|
|
13
|
+
//# sourceMappingURL=use-intl.esm5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/config.tsx"],"sourcesContent":["import IntlError from './IntlError';\n\nexport function defaultGetMessageFallback({\n key,\n namespace\n}: {\n key: string;\n namespace?: string;\n}) {\n return [namespace, key].filter((part) => part != null).join('.');\n}\n\nexport function defaultOnError(error: IntlError) {\n console.error(error);\n}\n"],"names":["defaultGetMessageFallback","key","namespace","filter","part","join","defaultOnError","error","console"],"mappings":"SAEgBA,0BAMf,IAAA,EAAA;AAAA,EALCC,IAAAA,GAKD,QALCA,GAKD;AAAA,MAJCC,SAID,QAJCA,SAID,CAAA;AACC,EAAO,OAAA,CAACA,SAAD,EAAYD,GAAZ,EAAiBE,MAAjB,CAAwB,UAACC,IAAD,EAAA;AAAA,IAAUA,OAAAA,IAAI,IAAI,IAAlB,CAAA;AAAA,GAAxB,CAAgDC,CAAAA,IAAhD,CAAqD,GAArD,CAAP,CAAA;AACD,CAAA;AAEK,SAAUC,cAAV,CAAyBC,KAAzB,EAAyC;AAC7CC,EAAAA,OAAO,CAACD,KAAR,CAAcA,KAAd,CAAA,CAAA;AACD;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import IntlError, { IntlErrorCode } from './use-intl.
|
|
1
|
+
import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
|
|
2
2
|
|
|
3
3
|
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
4
4
|
Object.entries(messages).forEach(function (_ref) {
|
|
@@ -29,4 +29,4 @@ function validateMessages(messages, onError) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export { validateMessages as default };
|
|
32
|
-
//# sourceMappingURL=use-intl.
|
|
32
|
+
//# sourceMappingURL=use-intl.esm6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n `Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\\n\\nInvalid ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}`\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY"],"mappings":";;AAGA,SAASA,uBAAT,CACEC,QADF,EAEEC,gBAFF,EAGEC,UAHF,EAGqB;AAEnBC,EAAAA,MAAM,CAACC,OAAP,CAAeJ,QAAf,CAAyBK,CAAAA,OAAzB,CAAiC,UAA6B,IAAA,EAAA;AAAA,IAAA,IAA3BC,GAA2B,GAAA,IAAA,CAAA,CAAA,CAAA;AAAA,QAAtBC,iBAAsB,GAAA,IAAA,CAAA,CAAA,CAAA,CAAA;;AAC5D,IAAA,IAAID,GAAG,CAACE,QAAJ,CAAa,GAAb,CAAJ,EAAuB;AACrB,MAAIC,IAAAA,QAAQ,GAAGH,GAAf,CAAA;AACA,MAAA,IAAIJ,UAAJ,EAAgBO,QAAQ,IAAA,OAAA,GAAYP,UAAZ,GAAR,GAAA,CAAA;AAChBD,MAAAA,gBAAgB,CAACS,IAAjB,CAAsBD,QAAtB,CAAA,CAAA;AACD,KAAA;;AAED,IAAIF,IAAAA,iBAAiB,IAAI,IAArB,IAA6B,OAAOA,iBAAP,KAA6B,QAA9D,EAAwE;AACtER,MAAAA,uBAAuB,CACrBQ,iBADqB,EAErBN,gBAFqB,EAGrB,CAACC,UAAD,EAAaI,GAAb,CAAA,CAAkBK,MAAlB,CAAyB,UAACC,IAAD,EAAA;AAAA,QAAUA,OAAAA,IAAI,IAAI,IAAlB,CAAA;AAAA,OAAzB,CAAiDC,CAAAA,IAAjD,CAAsD,GAAtD,CAHqB,CAAvB,CAAA;AAKD,KAAA;AACF,GAdD,CAAA,CAAA;AAeD,CAAA;;AAEa,SAAUC,gBAAV,CACZd,QADY,EAEZe,OAFY,EAEuB;AAEnC,EAAMd,IAAAA,gBAAgB,GAAkB,EAAxC,CAAA;AACAF,EAAAA,uBAAuB,CAACC,QAAD,EAAWC,gBAAX,CAAvB,CAAA;;AAEA,EAAA,IAAIA,gBAAgB,CAACe,MAAjB,GAA0B,CAA9B,EAAiC;AAC/BD,IAAAA,OAAO,CACL,IAAIE,SAAJ,CACEC,aAAa,CAACC,WADhB,EAAA,2JAAA,IAGIlB,gBAAgB,CAACe,MAAjB,KAA4B,CAA5B,GAAgC,KAAhC,GAAwC,MAH5C,CAIOf,GAAAA,IAAAA,GAAAA,gBAAgB,CAACY,IAAjB,CAAsB,IAAtB,CAJP,CADK,CAAP,CAAA;AAQD,GAAA;AACF;;;;"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
|
+
import IntlMessageFormat from 'intl-messageformat';
|
|
3
|
+
import { isValidElement, cloneElement } from 'react';
|
|
4
|
+
import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
|
|
5
|
+
import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm5.js';
|
|
6
|
+
import convertFormatsToIntlMessageFormat from './use-intl.esm9.js';
|
|
7
|
+
|
|
8
|
+
function resolvePath(messages, key, namespace) {
|
|
9
|
+
if (!messages) {
|
|
10
|
+
throw new Error(process.env.NODE_ENV !== "production" ? "No messages available at `" + namespace + "`." : undefined);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
var message = messages;
|
|
14
|
+
key.split('.').forEach(function (part) {
|
|
15
|
+
var next = message[part];
|
|
16
|
+
|
|
17
|
+
if (part == null || next == null) {
|
|
18
|
+
throw new Error(process.env.NODE_ENV !== "production" ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
message = next;
|
|
22
|
+
});
|
|
23
|
+
return message;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function prepareTranslationValues(values) {
|
|
27
|
+
if (Object.keys(values).length === 0) return undefined; // Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
28
|
+
|
|
29
|
+
var transformedValues = {};
|
|
30
|
+
Object.keys(values).forEach(function (key) {
|
|
31
|
+
var index = 0;
|
|
32
|
+
var value = values[key];
|
|
33
|
+
var transformed;
|
|
34
|
+
|
|
35
|
+
if (typeof value === 'function') {
|
|
36
|
+
transformed = function transformed(chunks) {
|
|
37
|
+
var result = value(chunks);
|
|
38
|
+
return isValidElement(result) ? cloneElement(result, {
|
|
39
|
+
key: key + index++
|
|
40
|
+
}) : result;
|
|
41
|
+
};
|
|
42
|
+
} else {
|
|
43
|
+
transformed = value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
transformedValues[key] = transformed;
|
|
47
|
+
});
|
|
48
|
+
return transformedValues;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getMessagesOrError(_ref) {
|
|
52
|
+
var messages = _ref.messages,
|
|
53
|
+
namespace = _ref.namespace,
|
|
54
|
+
_ref$onError = _ref.onError,
|
|
55
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
if (!messages) {
|
|
59
|
+
throw new Error(process.env.NODE_ENV !== "production" ? "No messages were configured on the provider." : undefined);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
63
|
+
|
|
64
|
+
if (!retrievedMessages) {
|
|
65
|
+
throw new Error(process.env.NODE_ENV !== "production" ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return retrievedMessages;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
var intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
71
|
+
onError(intlError);
|
|
72
|
+
return intlError;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function createBaseTranslator(_ref2) {
|
|
76
|
+
var cachedFormatsByLocale = _ref2.cachedFormatsByLocale,
|
|
77
|
+
defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
78
|
+
globalFormats = _ref2.formats,
|
|
79
|
+
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
80
|
+
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
81
|
+
locale = _ref2.locale,
|
|
82
|
+
messagesOrError = _ref2.messagesOrError,
|
|
83
|
+
namespace = _ref2.namespace,
|
|
84
|
+
onError = _ref2.onError,
|
|
85
|
+
timeZone = _ref2.timeZone;
|
|
86
|
+
|
|
87
|
+
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
88
|
+
var error = new IntlError(code, message);
|
|
89
|
+
onError(error);
|
|
90
|
+
return getMessageFallback({
|
|
91
|
+
error: error,
|
|
92
|
+
key: key,
|
|
93
|
+
namespace: namespace
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function translateBaseFn(
|
|
98
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
99
|
+
key,
|
|
100
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
101
|
+
values,
|
|
102
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
103
|
+
formats) {
|
|
104
|
+
var _cachedFormatsByLocal;
|
|
105
|
+
|
|
106
|
+
if (messagesOrError instanceof IntlError) {
|
|
107
|
+
// We have already warned about this during render
|
|
108
|
+
return getMessageFallback({
|
|
109
|
+
error: messagesOrError,
|
|
110
|
+
key: key,
|
|
111
|
+
namespace: namespace
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
var messages = messagesOrError;
|
|
116
|
+
var cacheKey = [namespace, key].filter(function (part) {
|
|
117
|
+
return part != null;
|
|
118
|
+
}).join('.');
|
|
119
|
+
var messageFormat;
|
|
120
|
+
|
|
121
|
+
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
122
|
+
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
123
|
+
} else {
|
|
124
|
+
var message;
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
message = resolvePath(messages, key, namespace);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (typeof message === 'object') {
|
|
133
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INSUFFICIENT_PATH, process.env.NODE_ENV !== "production" ? "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." : undefined);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (cachedFormatsByLocale) {
|
|
143
|
+
if (!cachedFormatsByLocale[locale]) {
|
|
144
|
+
cachedFormatsByLocale[locale] = {};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
var formattedMessage = messageFormat.format( // @ts-ignore `intl-messageformat` expects a different format
|
|
153
|
+
// for rich text elements since a recent minor update. This
|
|
154
|
+
// needs to be evaluated in detail, possibly also in regards
|
|
155
|
+
// to be able to format to parts.
|
|
156
|
+
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
157
|
+
|
|
158
|
+
if (formattedMessage == null) {
|
|
159
|
+
throw new Error(process.env.NODE_ENV !== "production" ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
160
|
+
} // Limit the function signature to return strings or React elements
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
return isValidElement(formattedMessage) || // Arrays of React elements
|
|
164
|
+
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function translateFn(
|
|
171
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
172
|
+
key,
|
|
173
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
174
|
+
values,
|
|
175
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
176
|
+
formats) {
|
|
177
|
+
var result = translateBaseFn(key, values, formats);
|
|
178
|
+
|
|
179
|
+
if (typeof result !== 'string') {
|
|
180
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, process.env.NODE_ENV !== "production" ? "The message `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') + " didn't resolve to a string. If you want to format rich text, use `t.rich` instead." : undefined);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
translateFn.rich = translateBaseFn;
|
|
187
|
+
|
|
188
|
+
translateFn.raw = function (
|
|
189
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
190
|
+
key) {
|
|
191
|
+
if (messagesOrError instanceof IntlError) {
|
|
192
|
+
// We have already warned about this during render
|
|
193
|
+
return getMessageFallback({
|
|
194
|
+
error: messagesOrError,
|
|
195
|
+
key: key,
|
|
196
|
+
namespace: namespace
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var messages = messagesOrError;
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
return resolvePath(messages, key, namespace);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return translateFn;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export { createBaseTranslator as default, getMessagesOrError };
|
|
213
|
+
//# sourceMappingURL=use-intl.esm7.js.map
|