use-intl 2.19.1 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core.d.ts +1 -0
- package/dist/_virtual/_rollupPluginBabelHelpers.esm.js +102 -0
- package/dist/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
- package/dist/core/IntlError.esm.js +33 -0
- package/dist/core/IntlError.esm.js.map +1 -0
- package/dist/core/TranslationValues.d.ts +1 -0
- package/dist/core/convertFormatsToIntlMessageFormat.esm.js +32 -0
- package/dist/core/convertFormatsToIntlMessageFormat.esm.js.map +1 -0
- package/dist/core/createBaseTranslator.d.ts +3 -8
- package/dist/core/createBaseTranslator.esm.js +201 -0
- package/dist/core/createBaseTranslator.esm.js.map +1 -0
- package/dist/core/createFormatter.esm.js +138 -0
- package/dist/core/createFormatter.esm.js.map +1 -0
- package/dist/core/createIntl.esm.js +14 -0
- package/dist/core/createIntl.esm.js.map +1 -0
- package/dist/core/createTranslator.d.ts +2 -3
- package/dist/core/createTranslator.esm.js +37 -0
- package/dist/core/createTranslator.esm.js.map +1 -0
- package/dist/core/createTranslatorImpl.d.ts +2 -3
- package/dist/core/createTranslatorImpl.esm.js +50 -0
- package/dist/core/createTranslatorImpl.esm.js.map +1 -0
- package/dist/core/defaults.esm.js +15 -0
- package/dist/core/defaults.esm.js.map +1 -0
- package/dist/core/index.d.ts +8 -1
- package/dist/core/initializeConfig.d.ts +13 -0
- package/dist/core/initializeConfig.esm.js +29 -0
- package/dist/core/initializeConfig.esm.js.map +1 -0
- package/dist/core/resolveNamespace.esm.js +10 -0
- package/dist/core/resolveNamespace.esm.js.map +1 -0
- package/dist/core/use-intl.esm.js +34 -30
- package/dist/core/use-intl.esm.js.map +1 -1
- package/dist/core/use-intl.esm10.js +23 -27
- package/dist/core/use-intl.esm10.js.map +1 -1
- package/dist/core/use-intl.esm11.js +10 -0
- package/dist/core/use-intl.esm11.js.map +1 -0
- package/dist/core/use-intl.esm2.js +30 -34
- package/dist/core/use-intl.esm2.js.map +1 -1
- package/dist/core/use-intl.esm3.js +8 -132
- package/dist/core/use-intl.esm3.js.map +1 -1
- package/dist/core/use-intl.esm4.js +196 -9
- package/dist/core/use-intl.esm4.js.map +1 -1
- package/dist/core/use-intl.esm5.js +19 -43
- package/dist/core/use-intl.esm5.js.map +1 -1
- package/dist/core/use-intl.esm6.js +134 -11
- package/dist/core/use-intl.esm6.js.map +1 -1
- package/dist/core/use-intl.esm7.js +10 -5
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/core/use-intl.esm8.js +37 -178
- package/dist/core/use-intl.esm8.js.map +1 -1
- package/dist/core/use-intl.esm9.js +27 -23
- package/dist/core/use-intl.esm9.js.map +1 -1
- package/dist/core/validateMessages.esm.js +28 -0
- package/dist/core/validateMessages.esm.js.map +1 -0
- package/dist/core.cjs.development.js +93 -0
- package/dist/core.cjs.development.js.map +1 -0
- package/dist/core.cjs.production.min.js +2 -0
- package/dist/core.cjs.production.min.js.map +1 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.esm.js +7 -0
- package/dist/core.esm.js.map +1 -0
- package/dist/core.js +8 -0
- package/dist/createIntl-8a2b87eb.js +579 -0
- package/dist/createIntl-8a2b87eb.js.map +1 -0
- package/dist/createIntl-f7979de1.js +2 -0
- package/dist/createIntl-f7979de1.js.map +1 -0
- package/dist/react/IntlContext.esm.js +6 -0
- package/dist/react/IntlContext.esm.js.map +1 -0
- package/dist/react/IntlProvider.d.ts +1 -1
- package/dist/react/IntlProvider.esm.js +16 -0
- package/dist/react/IntlProvider.esm.js.map +1 -0
- package/dist/react/getInitializedConfig.d.ts +1 -13
- package/dist/react/use-intl.esm.js +22 -12
- package/dist/react/use-intl.esm.js.map +1 -1
- package/dist/react/use-intl.esm10.js +8 -24
- package/dist/react/use-intl.esm10.js.map +1 -1
- package/dist/react/use-intl.esm11.js +3 -10
- package/dist/react/use-intl.esm11.js.map +1 -1
- package/dist/react/use-intl.esm2.js +20 -22
- package/dist/react/use-intl.esm2.js.map +1 -1
- package/dist/react/use-intl.esm3.js +41 -4
- package/dist/react/use-intl.esm3.js.map +1 -1
- package/dist/react/use-intl.esm4.js +12 -41
- 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 +1 -1
- package/dist/react/use-intl.esm7.js +11 -5
- package/dist/react/use-intl.esm7.js.map +1 -1
- package/dist/react/use-intl.esm8.js +4 -26
- package/dist/react/use-intl.esm8.js.map +1 -1
- package/dist/react/use-intl.esm9.js +33 -3
- package/dist/react/use-intl.esm9.js.map +1 -1
- package/dist/react/useFormatter.esm.js +24 -0
- package/dist/react/useFormatter.esm.js.map +1 -0
- package/dist/react/useIntl.esm.js +30 -0
- package/dist/react/useIntl.esm.js.map +1 -0
- package/dist/react/useIntlContext.esm.js +13 -0
- package/dist/react/useIntlContext.esm.js.map +1 -0
- package/dist/react/useLocale.esm.js +8 -0
- package/dist/react/useLocale.esm.js.map +1 -0
- package/dist/react/useMessages.esm.js +8 -0
- package/dist/react/useMessages.esm.js.map +1 -0
- package/{src/react/useNow.tsx → dist/react/useNow.esm.js} +32 -36
- package/dist/react/useNow.esm.js.map +1 -0
- package/dist/react/useTimeZone.esm.js +8 -0
- package/dist/react/useTimeZone.esm.js.map +1 -0
- package/dist/react/useTranslations.esm.js +26 -0
- package/dist/react/useTranslations.esm.js.map +1 -0
- package/dist/react/{use-intl.esm12.js → useTranslationsImpl.esm.js} +6 -13
- package/dist/react/useTranslationsImpl.esm.js.map +1 -0
- package/dist/react.cjs.development.js +184 -0
- package/dist/react.cjs.development.js.map +1 -0
- package/dist/react.cjs.production.min.js +2 -0
- package/dist/react.cjs.production.min.js.map +1 -0
- package/dist/react.d.ts +1 -0
- package/dist/react.esm.js +9 -0
- package/dist/react.esm.js.map +1 -0
- package/dist/react.js +8 -0
- package/dist/test/setup.d.ts +1 -0
- package/dist/use-intl.cjs.development.js +32 -30
- 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 +12 -10
- package/dist/use-intl.esm.js.map +1 -1
- package/package.json +17 -15
- package/react.d.ts +1 -0
- package/dist/react/use-intl.esm12.js.map +0 -1
- package/dist/src/core/AbstractIntlMessages.d.ts +0 -9
- package/dist/src/core/AbstractIntlMessages.js +0 -2
- package/dist/src/core/AbstractIntlMessages.js.map +0 -1
- package/dist/src/core/DateTimeFormatOptions.d.ts +0 -73
- package/dist/src/core/DateTimeFormatOptions.js +0 -3
- package/dist/src/core/DateTimeFormatOptions.js.map +0 -1
- package/dist/src/core/Formats.d.ts +0 -8
- package/dist/src/core/Formats.js +0 -2
- package/dist/src/core/Formats.js.map +0 -1
- package/dist/src/core/IntlConfig.d.ts +0 -54
- package/dist/src/core/IntlConfig.js +0 -2
- package/dist/src/core/IntlConfig.js.map +0 -1
- package/dist/src/core/IntlError.d.ts +0 -13
- package/dist/src/core/IntlError.js +0 -23
- package/dist/src/core/IntlError.js.map +0 -1
- package/dist/src/core/NumberFormatOptions.d.ts +0 -2
- package/dist/src/core/NumberFormatOptions.js +0 -2
- package/dist/src/core/NumberFormatOptions.js.map +0 -1
- package/dist/src/core/TimeZone.d.ts +0 -2
- package/dist/src/core/TimeZone.js +0 -2
- package/dist/src/core/TimeZone.js.map +0 -1
- package/dist/src/core/TranslationValues.d.ts +0 -5
- package/dist/src/core/TranslationValues.js +0 -2
- package/dist/src/core/TranslationValues.js.map +0 -1
- package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +0 -11
- package/dist/src/core/convertFormatsToIntlMessageFormat.js +0 -31
- package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +0 -1
- package/dist/src/core/createBaseTranslator.d.ts +0 -26
- package/dist/src/core/createBaseTranslator.js +0 -206
- package/dist/src/core/createBaseTranslator.js.map +0 -1
- package/dist/src/core/createFormatter.d.ts +0 -19
- package/dist/src/core/createFormatter.js +0 -129
- package/dist/src/core/createFormatter.js.map +0 -1
- package/dist/src/core/createIntl.d.ts +0 -7
- package/dist/src/core/createIntl.js +0 -11
- package/dist/src/core/createIntl.js.map +0 -1
- package/dist/src/core/createTranslator.d.ts +0 -48
- package/dist/src/core/createTranslator.js +0 -24
- package/dist/src/core/createTranslator.js.map +0 -1
- package/dist/src/core/createTranslatorImpl.d.ts +0 -14
- package/dist/src/core/createTranslatorImpl.js +0 -43
- package/dist/src/core/createTranslatorImpl.js.map +0 -1
- package/dist/src/core/defaults.d.ts +0 -11
- package/dist/src/core/defaults.js +0 -11
- package/dist/src/core/defaults.js.map +0 -1
- package/dist/src/core/index.d.ts +0 -9
- package/dist/src/core/index.js +0 -6
- package/dist/src/core/index.js.map +0 -1
- package/dist/src/core/resolveNamespace.d.ts +0 -5
- package/dist/src/core/resolveNamespace.js +0 -10
- package/dist/src/core/resolveNamespace.js.map +0 -1
- package/dist/src/core/utils/MessageKeys.d.ts +0 -5
- package/dist/src/core/utils/MessageKeys.js +0 -2
- package/dist/src/core/utils/MessageKeys.js.map +0 -1
- package/dist/src/core/utils/NamespaceKeys.d.ts +0 -5
- package/dist/src/core/utils/NamespaceKeys.js +0 -2
- package/dist/src/core/utils/NamespaceKeys.js.map +0 -1
- package/dist/src/core/utils/NestedKeyOf.d.ts +0 -4
- package/dist/src/core/utils/NestedKeyOf.js +0 -2
- package/dist/src/core/utils/NestedKeyOf.js.map +0 -1
- package/dist/src/core/utils/NestedValueOf.d.ts +0 -2
- package/dist/src/core/utils/NestedValueOf.js +0 -2
- package/dist/src/core/utils/NestedValueOf.js.map +0 -1
- package/dist/src/core/validateMessages.d.ts +0 -3
- package/dist/src/core/validateMessages.js +0 -56
- package/dist/src/core/validateMessages.js.map +0 -1
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.js +0 -3
- package/dist/src/index.js.map +0 -1
- package/dist/src/react/IntlContext.d.ts +0 -4
- package/dist/src/react/IntlContext.js +0 -4
- package/dist/src/react/IntlContext.js.map +0 -1
- package/dist/src/react/IntlProvider.d.ts +0 -7
- package/dist/src/react/IntlProvider.js +0 -7
- package/dist/src/react/IntlProvider.js.map +0 -1
- package/dist/src/react/getInitializedConfig.d.ts +0 -13
- package/dist/src/react/getInitializedConfig.js +0 -21
- package/dist/src/react/getInitializedConfig.js.map +0 -1
- package/dist/src/react/index.d.ts +0 -8
- package/dist/src/react/index.js +0 -10
- package/dist/src/react/index.js.map +0 -1
- package/dist/src/react/useFormatter.d.ts +0 -6
- package/dist/src/react/useFormatter.js +0 -14
- package/dist/src/react/useFormatter.js.map +0 -1
- package/dist/src/react/useIntl.d.ts +0 -6
- package/dist/src/react/useIntl.js +0 -20
- package/dist/src/react/useIntl.js.map +0 -1
- package/dist/src/react/useIntlContext.d.ts +0 -1
- package/dist/src/react/useIntlContext.js +0 -12
- package/dist/src/react/useIntlContext.js.map +0 -1
- package/dist/src/react/useLocale.d.ts +0 -1
- package/dist/src/react/useLocale.js +0 -5
- package/dist/src/react/useLocale.js.map +0 -1
- package/dist/src/react/useMessages.d.ts +0 -1
- package/dist/src/react/useMessages.js +0 -5
- package/dist/src/react/useMessages.js.map +0 -1
- package/dist/src/react/useNow.d.ts +0 -23
- package/dist/src/react/useNow.js +0 -40
- package/dist/src/react/useNow.js.map +0 -1
- package/dist/src/react/useTimeZone.d.ts +0 -1
- package/dist/src/react/useTimeZone.js +0 -5
- package/dist/src/react/useTimeZone.js.map +0 -1
- package/dist/src/react/useTranslations.d.ts +0 -44
- package/dist/src/react/useTranslations.js +0 -21
- package/dist/src/react/useTranslations.js.map +0 -1
- package/dist/src/react/useTranslationsImpl.d.ts +0 -8
- package/dist/src/react/useTranslationsImpl.js +0 -35
- package/dist/src/react/useTranslationsImpl.js.map +0 -1
- package/src/core/AbstractIntlMessages.tsx +0 -10
- package/src/core/DateTimeFormatOptions.tsx +0 -130
- package/src/core/Formats.tsx +0 -10
- package/src/core/IntlConfig.tsx +0 -59
- package/src/core/IntlError.tsx +0 -26
- package/src/core/NumberFormatOptions.tsx +0 -3
- package/src/core/TimeZone.tsx +0 -426
- package/src/core/TranslationValues.tsx +0 -22
- package/src/core/convertFormatsToIntlMessageFormat.tsx +0 -46
- package/src/core/createBaseTranslator.tsx +0 -335
- package/src/core/createFormatter.tsx +0 -191
- package/src/core/createIntl.tsx +0 -13
- package/src/core/createTranslator.tsx +0 -112
- package/src/core/createTranslatorImpl.tsx +0 -87
- package/src/core/defaults.tsx +0 -18
- package/src/core/index.tsx +0 -14
- package/src/core/resolveNamespace.tsx +0 -12
- package/src/core/utils/MessageKeys.tsx +0 -9
- package/src/core/utils/NamespaceKeys.tsx +0 -9
- package/src/core/utils/NestedKeyOf.tsx +0 -9
- package/src/core/utils/NestedValueOf.tsx +0 -12
- package/src/core/validateMessages.tsx +0 -77
- package/src/index.tsx +0 -2
- package/src/react/IntlContext.tsx +0 -6
- package/src/react/IntlProvider.tsx +0 -16
- package/src/react/getInitializedConfig.tsx +0 -29
- package/src/react/index.tsx +0 -10
- package/src/react/useFormatter.tsx +0 -19
- package/src/react/useIntl.tsx +0 -29
- package/src/react/useIntlContext.tsx +0 -16
- package/src/react/useLocale.tsx +0 -5
- package/src/react/useMessages.tsx +0 -5
- package/src/react/useTimeZone.tsx +0 -5
- package/src/react/useTranslations.tsx +0 -103
- package/src/react/useTranslationsImpl.tsx +0 -65
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line import/no-named-as-default -- False positive
|
|
2
|
-
import IntlMessageFormat from 'intl-messageformat';
|
|
3
|
-
import {
|
|
4
|
-
cloneElement,
|
|
5
|
-
isValidElement,
|
|
6
|
-
ReactElement,
|
|
7
|
-
ReactNode,
|
|
8
|
-
ReactNodeArray
|
|
9
|
-
} from 'react';
|
|
10
|
-
import AbstractIntlMessages from './AbstractIntlMessages';
|
|
11
|
-
import Formats from './Formats';
|
|
12
|
-
import {InitializedIntlConfig} from './IntlConfig';
|
|
13
|
-
import IntlError, {IntlErrorCode} from './IntlError';
|
|
14
|
-
import TranslationValues, {RichTranslationValues} from './TranslationValues';
|
|
15
|
-
import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
|
|
16
|
-
import {defaultGetMessageFallback, defaultOnError} from './defaults';
|
|
17
|
-
import MessageKeys from './utils/MessageKeys';
|
|
18
|
-
import NestedKeyOf from './utils/NestedKeyOf';
|
|
19
|
-
import NestedValueOf from './utils/NestedValueOf';
|
|
20
|
-
|
|
21
|
-
function resolvePath(
|
|
22
|
-
messages: AbstractIntlMessages | undefined,
|
|
23
|
-
key: string,
|
|
24
|
-
namespace?: string
|
|
25
|
-
) {
|
|
26
|
-
if (!messages) {
|
|
27
|
-
throw new Error(
|
|
28
|
-
process.env.NODE_ENV !== 'production'
|
|
29
|
-
? `No messages available at \`${namespace}\`.`
|
|
30
|
-
: undefined
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let message = messages;
|
|
35
|
-
|
|
36
|
-
key.split('.').forEach((part) => {
|
|
37
|
-
const next = (message as any)[part];
|
|
38
|
-
|
|
39
|
-
if (part == null || next == null) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
process.env.NODE_ENV !== 'production'
|
|
42
|
-
? `Could not resolve \`${key}\` in ${
|
|
43
|
-
namespace ? `\`${namespace}\`` : 'messages'
|
|
44
|
-
}.`
|
|
45
|
-
: undefined
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
message = next;
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
return message;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function prepareTranslationValues(values: RichTranslationValues) {
|
|
56
|
-
if (Object.keys(values).length === 0) return undefined;
|
|
57
|
-
|
|
58
|
-
// Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
59
|
-
const transformedValues: RichTranslationValues = {};
|
|
60
|
-
Object.keys(values).forEach((key) => {
|
|
61
|
-
let index = 0;
|
|
62
|
-
const value = values[key];
|
|
63
|
-
|
|
64
|
-
let transformed;
|
|
65
|
-
if (typeof value === 'function') {
|
|
66
|
-
transformed = (chunks: ReactNode) => {
|
|
67
|
-
const result = value(chunks);
|
|
68
|
-
|
|
69
|
-
return isValidElement(result)
|
|
70
|
-
? cloneElement(result, {key: key + index++})
|
|
71
|
-
: result;
|
|
72
|
-
};
|
|
73
|
-
} else {
|
|
74
|
-
transformed = value;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
transformedValues[key] = transformed;
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
return transformedValues;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function getMessagesOrError<Messages extends AbstractIntlMessages>({
|
|
84
|
-
messages,
|
|
85
|
-
namespace,
|
|
86
|
-
onError = defaultOnError
|
|
87
|
-
}: {
|
|
88
|
-
messages: Messages;
|
|
89
|
-
namespace?: string;
|
|
90
|
-
onError?(error: IntlError): void;
|
|
91
|
-
}) {
|
|
92
|
-
try {
|
|
93
|
-
if (!messages) {
|
|
94
|
-
throw new Error(
|
|
95
|
-
process.env.NODE_ENV !== 'production'
|
|
96
|
-
? `No messages were configured on the provider.`
|
|
97
|
-
: undefined
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const retrievedMessages = namespace
|
|
102
|
-
? resolvePath(messages, namespace)
|
|
103
|
-
: messages;
|
|
104
|
-
|
|
105
|
-
if (!retrievedMessages) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
process.env.NODE_ENV !== 'production'
|
|
108
|
-
? `No messages for namespace \`${namespace}\` found.`
|
|
109
|
-
: undefined
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return retrievedMessages;
|
|
114
|
-
} catch (error) {
|
|
115
|
-
const intlError = new IntlError(
|
|
116
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
117
|
-
(error as Error).message
|
|
118
|
-
);
|
|
119
|
-
onError(intlError);
|
|
120
|
-
return intlError;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
|
|
125
|
-
cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;
|
|
126
|
-
defaultTranslationValues?: RichTranslationValues;
|
|
127
|
-
namespace?: string;
|
|
128
|
-
messagesOrError: Messages | IntlError;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
export default function createBaseTranslator<
|
|
132
|
-
Messages extends AbstractIntlMessages,
|
|
133
|
-
NestedKey extends NestedKeyOf<Messages>
|
|
134
|
-
>({
|
|
135
|
-
cachedFormatsByLocale,
|
|
136
|
-
defaultTranslationValues,
|
|
137
|
-
formats: globalFormats,
|
|
138
|
-
getMessageFallback = defaultGetMessageFallback,
|
|
139
|
-
locale,
|
|
140
|
-
messagesOrError,
|
|
141
|
-
namespace,
|
|
142
|
-
onError,
|
|
143
|
-
timeZone
|
|
144
|
-
}: CreateBaseTranslatorProps<Messages>) {
|
|
145
|
-
function getFallbackFromErrorAndNotify(
|
|
146
|
-
key: string,
|
|
147
|
-
code: IntlErrorCode,
|
|
148
|
-
message?: string
|
|
149
|
-
) {
|
|
150
|
-
const error = new IntlError(code, message);
|
|
151
|
-
onError(error);
|
|
152
|
-
return getMessageFallback({error, key, namespace});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function translateBaseFn(
|
|
156
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
157
|
-
key: string,
|
|
158
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
159
|
-
values?: RichTranslationValues,
|
|
160
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
161
|
-
formats?: Partial<Formats>
|
|
162
|
-
): string | ReactElement | ReactNodeArray {
|
|
163
|
-
if (messagesOrError instanceof IntlError) {
|
|
164
|
-
// We have already warned about this during render
|
|
165
|
-
return getMessageFallback({
|
|
166
|
-
error: messagesOrError,
|
|
167
|
-
key,
|
|
168
|
-
namespace
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
const messages = messagesOrError;
|
|
172
|
-
|
|
173
|
-
let message;
|
|
174
|
-
try {
|
|
175
|
-
message = resolvePath(messages, key, namespace);
|
|
176
|
-
} catch (error) {
|
|
177
|
-
return getFallbackFromErrorAndNotify(
|
|
178
|
-
key,
|
|
179
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
180
|
-
(error as Error).message
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function joinPath(parts: Array<string | undefined>) {
|
|
185
|
-
return parts.filter((part) => part != null).join('.');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const cacheKey = joinPath([namespace, key, String(message)]);
|
|
189
|
-
|
|
190
|
-
let messageFormat;
|
|
191
|
-
if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {
|
|
192
|
-
messageFormat = cachedFormatsByLocale?.[locale][cacheKey];
|
|
193
|
-
} else {
|
|
194
|
-
if (typeof message === 'object') {
|
|
195
|
-
let code, errorMessage;
|
|
196
|
-
if (Array.isArray(message)) {
|
|
197
|
-
code = IntlErrorCode.INVALID_MESSAGE;
|
|
198
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
199
|
-
errorMessage = `Message at \`${joinPath([
|
|
200
|
-
namespace,
|
|
201
|
-
key
|
|
202
|
-
])}\` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages`;
|
|
203
|
-
}
|
|
204
|
-
} else {
|
|
205
|
-
code = IntlErrorCode.INSUFFICIENT_PATH;
|
|
206
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
207
|
-
errorMessage = `Message at \`${joinPath([
|
|
208
|
-
namespace,
|
|
209
|
-
key
|
|
210
|
-
])}\` resolved to an object, but only strings are supported. Use a \`.\` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages`;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
try {
|
|
218
|
-
messageFormat = new IntlMessageFormat(
|
|
219
|
-
message,
|
|
220
|
-
locale,
|
|
221
|
-
convertFormatsToIntlMessageFormat(
|
|
222
|
-
{...globalFormats, ...formats},
|
|
223
|
-
timeZone
|
|
224
|
-
)
|
|
225
|
-
);
|
|
226
|
-
} catch (error) {
|
|
227
|
-
return getFallbackFromErrorAndNotify(
|
|
228
|
-
key,
|
|
229
|
-
IntlErrorCode.INVALID_MESSAGE,
|
|
230
|
-
(error as Error).message
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (cachedFormatsByLocale) {
|
|
235
|
-
if (!cachedFormatsByLocale[locale]) {
|
|
236
|
-
cachedFormatsByLocale[locale] = {};
|
|
237
|
-
}
|
|
238
|
-
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
try {
|
|
243
|
-
const formattedMessage = messageFormat.format(
|
|
244
|
-
// @ts-ignore `intl-messageformat` expects a different format
|
|
245
|
-
// for rich text elements since a recent minor update. This
|
|
246
|
-
// needs to be evaluated in detail, possibly also in regards
|
|
247
|
-
// to be able to format to parts.
|
|
248
|
-
prepareTranslationValues({...defaultTranslationValues, ...values})
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
if (formattedMessage == null) {
|
|
252
|
-
throw new Error(
|
|
253
|
-
process.env.NODE_ENV !== 'production'
|
|
254
|
-
? `Unable to format \`${key}\` in ${
|
|
255
|
-
namespace ? `namespace \`${namespace}\`` : 'messages'
|
|
256
|
-
}`
|
|
257
|
-
: undefined
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Limit the function signature to return strings or React elements
|
|
262
|
-
return isValidElement(formattedMessage) ||
|
|
263
|
-
// Arrays of React elements
|
|
264
|
-
Array.isArray(formattedMessage) ||
|
|
265
|
-
typeof formattedMessage === 'string'
|
|
266
|
-
? formattedMessage
|
|
267
|
-
: String(formattedMessage);
|
|
268
|
-
} catch (error) {
|
|
269
|
-
return getFallbackFromErrorAndNotify(
|
|
270
|
-
key,
|
|
271
|
-
IntlErrorCode.FORMATTING_ERROR,
|
|
272
|
-
(error as Error).message
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function translateFn<
|
|
278
|
-
TargetKey extends MessageKeys<
|
|
279
|
-
NestedValueOf<Messages, NestedKey>,
|
|
280
|
-
NestedKeyOf<NestedValueOf<Messages, NestedKey>>
|
|
281
|
-
>
|
|
282
|
-
>(
|
|
283
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
284
|
-
key: TargetKey,
|
|
285
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
286
|
-
values?: TranslationValues,
|
|
287
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
288
|
-
formats?: Partial<Formats>
|
|
289
|
-
): string {
|
|
290
|
-
const result = translateBaseFn(key, values, formats);
|
|
291
|
-
|
|
292
|
-
if (typeof result !== 'string') {
|
|
293
|
-
return getFallbackFromErrorAndNotify(
|
|
294
|
-
key,
|
|
295
|
-
IntlErrorCode.INVALID_MESSAGE,
|
|
296
|
-
process.env.NODE_ENV !== 'production'
|
|
297
|
-
? `The message \`${key}\` in ${
|
|
298
|
-
namespace ? `namespace \`${namespace}\`` : 'messages'
|
|
299
|
-
} didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
|
|
300
|
-
: undefined
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return result;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
translateFn.rich = translateBaseFn;
|
|
308
|
-
|
|
309
|
-
translateFn.raw = (
|
|
310
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
311
|
-
key: string
|
|
312
|
-
): any => {
|
|
313
|
-
if (messagesOrError instanceof IntlError) {
|
|
314
|
-
// We have already warned about this during render
|
|
315
|
-
return getMessageFallback({
|
|
316
|
-
error: messagesOrError,
|
|
317
|
-
key,
|
|
318
|
-
namespace
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
const messages = messagesOrError;
|
|
322
|
-
|
|
323
|
-
try {
|
|
324
|
-
return resolvePath(messages, key, namespace);
|
|
325
|
-
} catch (error) {
|
|
326
|
-
return getFallbackFromErrorAndNotify(
|
|
327
|
-
key,
|
|
328
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
329
|
-
(error as Error).message
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
return translateFn;
|
|
335
|
-
}
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import DateTimeFormatOptions from './DateTimeFormatOptions';
|
|
2
|
-
import Formats from './Formats';
|
|
3
|
-
import IntlError, {IntlErrorCode} from './IntlError';
|
|
4
|
-
import NumberFormatOptions from './NumberFormatOptions';
|
|
5
|
-
import TimeZone from './TimeZone';
|
|
6
|
-
import {defaultOnError} from './defaults';
|
|
7
|
-
|
|
8
|
-
const MINUTE = 60;
|
|
9
|
-
const HOUR = MINUTE * 60;
|
|
10
|
-
const DAY = HOUR * 24;
|
|
11
|
-
const WEEK = DAY * 7;
|
|
12
|
-
const MONTH = DAY * (365 / 12); // Approximation
|
|
13
|
-
const YEAR = DAY * 365;
|
|
14
|
-
|
|
15
|
-
function getRelativeTimeFormatConfig(seconds: number) {
|
|
16
|
-
const absValue = Math.abs(seconds);
|
|
17
|
-
let value, unit: Intl.RelativeTimeFormatUnit;
|
|
18
|
-
|
|
19
|
-
// We have to round the resulting values, as `Intl.RelativeTimeFormat`
|
|
20
|
-
// will include fractions like '2.1 hours ago'.
|
|
21
|
-
|
|
22
|
-
if (absValue < MINUTE) {
|
|
23
|
-
unit = 'second';
|
|
24
|
-
value = Math.round(seconds);
|
|
25
|
-
} else if (absValue < HOUR) {
|
|
26
|
-
unit = 'minute';
|
|
27
|
-
value = Math.round(seconds / MINUTE);
|
|
28
|
-
} else if (absValue < DAY) {
|
|
29
|
-
unit = 'hour';
|
|
30
|
-
value = Math.round(seconds / HOUR);
|
|
31
|
-
} else if (absValue < WEEK) {
|
|
32
|
-
unit = 'day';
|
|
33
|
-
value = Math.round(seconds / DAY);
|
|
34
|
-
} else if (absValue < MONTH) {
|
|
35
|
-
unit = 'week';
|
|
36
|
-
value = Math.round(seconds / WEEK);
|
|
37
|
-
} else if (absValue < YEAR) {
|
|
38
|
-
unit = 'month';
|
|
39
|
-
value = Math.round(seconds / MONTH);
|
|
40
|
-
} else {
|
|
41
|
-
unit = 'year';
|
|
42
|
-
value = Math.round(seconds / YEAR);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return {value, unit};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
type Props = {
|
|
49
|
-
locale: string;
|
|
50
|
-
timeZone?: TimeZone;
|
|
51
|
-
onError?(error: IntlError): void;
|
|
52
|
-
formats?: Partial<Formats>;
|
|
53
|
-
now?: Date;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export default function createFormatter({
|
|
57
|
-
formats,
|
|
58
|
-
locale,
|
|
59
|
-
now: globalNow,
|
|
60
|
-
onError = defaultOnError,
|
|
61
|
-
timeZone
|
|
62
|
-
}: Props) {
|
|
63
|
-
function resolveFormatOrOptions<Options>(
|
|
64
|
-
typeFormats: Record<string, Options> | undefined,
|
|
65
|
-
formatOrOptions?: string | Options
|
|
66
|
-
) {
|
|
67
|
-
let options;
|
|
68
|
-
if (typeof formatOrOptions === 'string') {
|
|
69
|
-
const formatName = formatOrOptions;
|
|
70
|
-
options = typeFormats?.[formatName];
|
|
71
|
-
|
|
72
|
-
if (!options) {
|
|
73
|
-
const error = new IntlError(
|
|
74
|
-
IntlErrorCode.MISSING_FORMAT,
|
|
75
|
-
process.env.NODE_ENV !== 'production'
|
|
76
|
-
? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
|
|
77
|
-
: undefined
|
|
78
|
-
);
|
|
79
|
-
onError(error);
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
options = formatOrOptions;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return options;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function getFormattedValue<Value, Options>(
|
|
90
|
-
value: Value,
|
|
91
|
-
formatOrOptions: string | Options | undefined,
|
|
92
|
-
typeFormats: Record<string, Options> | undefined,
|
|
93
|
-
formatter: (options?: Options) => string
|
|
94
|
-
) {
|
|
95
|
-
let options;
|
|
96
|
-
try {
|
|
97
|
-
options = resolveFormatOrOptions(typeFormats, formatOrOptions);
|
|
98
|
-
} catch (error) {
|
|
99
|
-
return String(value);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
return formatter(options);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
onError(
|
|
106
|
-
new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)
|
|
107
|
-
);
|
|
108
|
-
return String(value);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function dateTime(
|
|
113
|
-
/** If a number is supplied, this is interpreted as a UTC timestamp. */
|
|
114
|
-
value: Date | number,
|
|
115
|
-
/** If a time zone is supplied, the `value` is converted to that time zone.
|
|
116
|
-
* Otherwise the user time zone will be used. */
|
|
117
|
-
formatOrOptions?: string | DateTimeFormatOptions
|
|
118
|
-
) {
|
|
119
|
-
return getFormattedValue(
|
|
120
|
-
value,
|
|
121
|
-
formatOrOptions,
|
|
122
|
-
formats?.dateTime,
|
|
123
|
-
(options) => {
|
|
124
|
-
if (timeZone && !options?.timeZone) {
|
|
125
|
-
options = {...options, timeZone};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return new Intl.DateTimeFormat(locale, options).format(value);
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function number(
|
|
134
|
-
value: number | bigint,
|
|
135
|
-
formatOrOptions?: string | NumberFormatOptions
|
|
136
|
-
) {
|
|
137
|
-
return getFormattedValue(
|
|
138
|
-
value,
|
|
139
|
-
formatOrOptions,
|
|
140
|
-
formats?.number,
|
|
141
|
-
(options) => new Intl.NumberFormat(locale, options).format(value)
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function relativeTime(
|
|
146
|
-
/** The date time that needs to be formatted. */
|
|
147
|
-
date: number | Date,
|
|
148
|
-
/** The reference point in time to which `date` will be formatted in relation to. */
|
|
149
|
-
now?: number | Date
|
|
150
|
-
) {
|
|
151
|
-
try {
|
|
152
|
-
if (!now) {
|
|
153
|
-
if (globalNow) {
|
|
154
|
-
now = globalNow;
|
|
155
|
-
} else {
|
|
156
|
-
throw new Error(
|
|
157
|
-
process.env.NODE_ENV !== 'production'
|
|
158
|
-
? `The \`now\` parameter wasn't provided and there was no global fallback configured on the provider.`
|
|
159
|
-
: undefined
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const dateDate = date instanceof Date ? date : new Date(date);
|
|
165
|
-
const nowDate = now instanceof Date ? now : new Date(now);
|
|
166
|
-
|
|
167
|
-
const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
|
|
168
|
-
const {unit, value} = getRelativeTimeFormatConfig(seconds);
|
|
169
|
-
|
|
170
|
-
return new Intl.RelativeTimeFormat(locale, {
|
|
171
|
-
numeric: 'auto'
|
|
172
|
-
}).format(value, unit);
|
|
173
|
-
} catch (error) {
|
|
174
|
-
onError(
|
|
175
|
-
new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)
|
|
176
|
-
);
|
|
177
|
-
return String(date);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function list(
|
|
182
|
-
value: Iterable<string>,
|
|
183
|
-
formatOrOptions?: string | Intl.ListFormatOptions
|
|
184
|
-
) {
|
|
185
|
-
return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>
|
|
186
|
-
new Intl.ListFormat(locale, options).format(value)
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return {dateTime, number, relativeTime, list};
|
|
191
|
-
}
|
package/src/core/createIntl.tsx
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import createFormatter from './createFormatter';
|
|
2
|
-
|
|
3
|
-
/** @deprecated Switch to `createFormatter` */
|
|
4
|
-
export default function createIntl(
|
|
5
|
-
...args: Parameters<typeof createFormatter>
|
|
6
|
-
) {
|
|
7
|
-
const formatter = createFormatter(...args);
|
|
8
|
-
return {
|
|
9
|
-
formatDateTime: formatter.dateTime,
|
|
10
|
-
formatNumber: formatter.number,
|
|
11
|
-
formatRelativeTime: formatter.relativeTime
|
|
12
|
-
};
|
|
13
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import Formats from './Formats';
|
|
2
|
-
import IntlConfig from './IntlConfig';
|
|
3
|
-
import TranslationValues from './TranslationValues';
|
|
4
|
-
import createTranslatorImpl, {
|
|
5
|
-
CoreRichTranslationValues
|
|
6
|
-
} from './createTranslatorImpl';
|
|
7
|
-
import {defaultGetMessageFallback, defaultOnError} from './defaults';
|
|
8
|
-
import MessageKeys from './utils/MessageKeys';
|
|
9
|
-
import NamespaceKeys from './utils/NamespaceKeys';
|
|
10
|
-
import NestedKeyOf from './utils/NestedKeyOf';
|
|
11
|
-
import NestedValueOf from './utils/NestedValueOf';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Translates messages from the given namespace by using the ICU syntax.
|
|
15
|
-
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
16
|
-
*
|
|
17
|
-
* If no namespace is provided, all available messages are returned.
|
|
18
|
-
* The namespace can also indicate nesting by using a dot
|
|
19
|
-
* (e.g. `namespace.Component`).
|
|
20
|
-
*/
|
|
21
|
-
export default function createTranslator<
|
|
22
|
-
NestedKey extends NamespaceKeys<
|
|
23
|
-
IntlMessages,
|
|
24
|
-
NestedKeyOf<IntlMessages>
|
|
25
|
-
> = never
|
|
26
|
-
>({
|
|
27
|
-
getMessageFallback = defaultGetMessageFallback,
|
|
28
|
-
messages,
|
|
29
|
-
namespace,
|
|
30
|
-
onError = defaultOnError,
|
|
31
|
-
...rest
|
|
32
|
-
}: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {
|
|
33
|
-
messages: NonNullable<IntlConfig<IntlMessages>['messages']>;
|
|
34
|
-
namespace?: NestedKey;
|
|
35
|
-
}): // Explicitly defining the return type is necessary as TypeScript would get it wrong
|
|
36
|
-
{
|
|
37
|
-
// Default invocation
|
|
38
|
-
<
|
|
39
|
-
TargetKey extends MessageKeys<
|
|
40
|
-
NestedValueOf<
|
|
41
|
-
{'!': IntlMessages},
|
|
42
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
43
|
-
>,
|
|
44
|
-
NestedKeyOf<
|
|
45
|
-
NestedValueOf<
|
|
46
|
-
{'!': IntlMessages},
|
|
47
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
48
|
-
>
|
|
49
|
-
>
|
|
50
|
-
>
|
|
51
|
-
>(
|
|
52
|
-
key: TargetKey,
|
|
53
|
-
values?: TranslationValues,
|
|
54
|
-
formats?: Partial<Formats>
|
|
55
|
-
): string;
|
|
56
|
-
|
|
57
|
-
// `rich`
|
|
58
|
-
rich<
|
|
59
|
-
TargetKey extends MessageKeys<
|
|
60
|
-
NestedValueOf<
|
|
61
|
-
{'!': IntlMessages},
|
|
62
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
63
|
-
>,
|
|
64
|
-
NestedKeyOf<
|
|
65
|
-
NestedValueOf<
|
|
66
|
-
{'!': IntlMessages},
|
|
67
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
68
|
-
>
|
|
69
|
-
>
|
|
70
|
-
>
|
|
71
|
-
>(
|
|
72
|
-
key: TargetKey,
|
|
73
|
-
values?: CoreRichTranslationValues,
|
|
74
|
-
formats?: Partial<Formats>
|
|
75
|
-
): string;
|
|
76
|
-
|
|
77
|
-
// `raw`
|
|
78
|
-
raw<
|
|
79
|
-
TargetKey extends MessageKeys<
|
|
80
|
-
NestedValueOf<
|
|
81
|
-
{'!': IntlMessages},
|
|
82
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
83
|
-
>,
|
|
84
|
-
NestedKeyOf<
|
|
85
|
-
NestedValueOf<
|
|
86
|
-
{'!': IntlMessages},
|
|
87
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
88
|
-
>
|
|
89
|
-
>
|
|
90
|
-
>
|
|
91
|
-
>(
|
|
92
|
-
key: TargetKey
|
|
93
|
-
): any;
|
|
94
|
-
} {
|
|
95
|
-
// We have to wrap the actual function so the type inference for the optional
|
|
96
|
-
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
97
|
-
// The prefix ("!") is arbitrary.
|
|
98
|
-
return createTranslatorImpl<
|
|
99
|
-
{'!': IntlMessages},
|
|
100
|
-
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
|
|
101
|
-
>(
|
|
102
|
-
{
|
|
103
|
-
...rest,
|
|
104
|
-
onError,
|
|
105
|
-
getMessageFallback,
|
|
106
|
-
messages: {'!': messages},
|
|
107
|
-
// @ts-ignore
|
|
108
|
-
namespace: namespace ? `!.${namespace}` : '!'
|
|
109
|
-
},
|
|
110
|
-
'!'
|
|
111
|
-
);
|
|
112
|
-
}
|