use-intl 2.13.0 → 2.13.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/dist/core/use-intl.esm5.js +2 -2
- package/dist/core/use-intl.esm7.js +7 -210
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/core/use-intl.esm8.js +230 -7
- package/dist/core/use-intl.esm8.js.map +1 -1
- package/dist/react/getInitializedConfig.d.ts +10 -2
- package/dist/react/use-intl.esm11.js +2 -2
- package/dist/react/use-intl.esm9.js.map +1 -1
- package/dist/src/core/createBaseTranslator.js +24 -6
- package/dist/src/core/createBaseTranslator.js.map +1 -1
- package/dist/src/react/getInitializedConfig.d.ts +10 -2
- package/dist/src/react/getInitializedConfig.js.map +1 -1
- package/dist/use-intl.cjs.development.js +24 -4
- 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/package.json +2 -2
- package/src/core/createBaseTranslator.tsx +25 -12
- package/src/react/getInitializedConfig.tsx +2 -7
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
|
|
2
2
|
import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
3
|
-
import createBaseTranslator, { getMessagesOrError } from './use-intl.
|
|
4
|
-
import resolveNamespace from './use-intl.
|
|
3
|
+
import createBaseTranslator, { getMessagesOrError } from './use-intl.esm8.js';
|
|
4
|
+
import resolveNamespace from './use-intl.esm7.js';
|
|
5
5
|
|
|
6
6
|
var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
7
7
|
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
@@ -1,213 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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 message;
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
message = resolvePath(messages, key, namespace);
|
|
120
|
-
} catch (error) {
|
|
121
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
var cacheKey = [namespace, key, message].filter(function (part) {
|
|
125
|
-
return part != null;
|
|
126
|
-
}).join('.');
|
|
127
|
-
var messageFormat;
|
|
128
|
-
|
|
129
|
-
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
130
|
-
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
131
|
-
} else {
|
|
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;
|
|
1
|
+
/**
|
|
2
|
+
* For the strictly typed messages to work we have to wrap the namespace into
|
|
3
|
+
* a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
|
|
4
|
+
*/
|
|
5
|
+
function resolveNamespace(namespace, namespacePrefix) {
|
|
6
|
+
return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
|
|
210
7
|
}
|
|
211
8
|
|
|
212
|
-
export {
|
|
9
|
+
export { resolveNamespace as default };
|
|
213
10
|
//# sourceMappingURL=use-intl.esm7.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm7.js","sources":["../../src/core/createBaseTranslator.tsx"],"sourcesContent":["// eslint-disable-next-line import/no-named-as-default -- False positive\nimport IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray\n} from 'react';\nimport AbstractIntlMessages from './AbstractIntlMessages';\nimport Formats from './Formats';\nimport {InitializedIntlConfig} from './IntlConfig';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\nfunction resolvePath(\n messages: AbstractIntlMessages | undefined,\n key: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages available at \\`${namespace}\\`.`\n : undefined\n );\n }\n\n let message = messages;\n\n key.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Could not resolve \\`${key}\\` in ${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }.`\n : undefined\n );\n }\n\n message = next;\n });\n\n return message;\n}\n\nfunction prepareTranslationValues(values: RichTranslationValues) {\n if (Object.keys(values).length === 0) return undefined;\n\n // Workaround for https://github.com/formatjs/formatjs/issues/1467\n const transformedValues: RichTranslationValues = {};\n Object.keys(values).forEach((key) => {\n let index = 0;\n const value = values[key];\n\n let transformed;\n if (typeof value === 'function') {\n transformed = (chunks: ReactNode) => {\n const result = value(chunks);\n\n return isValidElement(result)\n ? cloneElement(result, {key: key + index++})\n : result;\n };\n } else {\n transformed = value;\n }\n\n transformedValues[key] = transformed;\n });\n\n return transformedValues;\n}\n\nexport function getMessagesOrError<Messages extends AbstractIntlMessages>({\n messages,\n namespace,\n onError = defaultOnError\n}: {\n messages: Messages;\n namespace?: string;\n onError?(error: IntlError): void;\n}) {\n try {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages were configured on the provider.`\n : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(messages, namespace)\n : messages;\n\n if (!retrievedMessages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages for namespace \\`${namespace}\\` found.`\n : undefined\n );\n }\n\n return retrievedMessages;\n } catch (error) {\n const intlError = new IntlError(\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n onError(intlError);\n return intlError;\n }\n}\n\nexport type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {\n cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;\n defaultTranslationValues?: RichTranslationValues;\n namespace?: string;\n messagesOrError: Messages | IntlError;\n};\n\nexport default function createBaseTranslator<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>({\n cachedFormatsByLocale,\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback = defaultGetMessageFallback,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n}: CreateBaseTranslatorProps<Messages>) {\n function getFallbackFromErrorAndNotify(\n key: string,\n code: IntlErrorCode,\n message?: string\n ) {\n const error = new IntlError(code, message);\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n function translateBaseFn(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string,\n /** Key value pairs for values to interpolate into the message. */\n values?: RichTranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n let message;\n try {\n message = resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n\n const cacheKey = [namespace, key, message]\n .filter((part) => part != null)\n .join('.');\n\n let messageFormat;\n if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale?.[locale][cacheKey];\n } else {\n if (typeof message === 'object') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INSUFFICIENT_PATH,\n process.env.NODE_ENV !== 'production'\n ? `Insufficient path specified for \\`${key}\\` in \\`${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }\\`.`\n : undefined\n );\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (cachedFormatsByLocale) {\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n }\n\n try {\n const formattedMessage = messageFormat.format(\n // @ts-ignore `intl-messageformat` expects a different format\n // for rich text elements since a recent minor update. This\n // needs to be evaluated in detail, possibly also in regards\n // to be able to format to parts.\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Unable to format \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n }`\n : undefined\n );\n }\n\n // Limit the function signature to return strings or React elements\n return isValidElement(formattedMessage) ||\n // Arrays of React elements\n Array.isArray(formattedMessage) ||\n typeof formattedMessage === 'string'\n ? formattedMessage\n : String(formattedMessage);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.FORMATTING_ERROR,\n (error as Error).message\n );\n }\n }\n\n function translateFn<\n TargetKey extends MessageKeys<\n NestedValueOf<Messages, NestedKey>,\n NestedKeyOf<NestedValueOf<Messages, NestedKey>>\n >\n >(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: TargetKey,\n /** Key value pairs for values to interpolate into the message. */\n values?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const result = translateBaseFn(key, values, formats);\n\n if (typeof result !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n process.env.NODE_ENV !== 'production'\n ? `The message \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n } didn't resolve to a string. If you want to format rich text, use \\`t.rich\\` instead.`\n : undefined\n );\n }\n\n return result;\n }\n\n translateFn.rich = translateBaseFn;\n\n translateFn.raw = (\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string\n ): any => {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n try {\n return resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n };\n\n return translateFn;\n}\n"],"names":["resolvePath","messages","key","namespace","Error","process","env","NODE_ENV","undefined","message","split","forEach","part","next","prepareTranslationValues","values","Object","keys","length","transformedValues","index","value","transformed","chunks","result","isValidElement","cloneElement","getMessagesOrError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","createBaseTranslator","cachedFormatsByLocale","defaultTranslationValues","globalFormats","formats","getMessageFallback","defaultGetMessageFallback","locale","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","cacheKey","filter","join","messageFormat","INSUFFICIENT_PATH","IntlMessageFormat","convertFormatsToIntlMessageFormat","INVALID_MESSAGE","formattedMessage","format","Array","isArray","String","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAoBA,SAASA,WAAT,CACEC,QADF,EAEEC,GAFF,EAGEC,SAHF,EAGoB;AAElB,EAAI,IAAA,CAACF,QA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GT;AACP,IAAI0C,IAAAA,eAAe,YAAYX,SAA/B,EAA0C;AACxC;AACA,MAAA,OAAOQ,kBAAkB,CAAC;AACxBV,QAAAA,KAAK,EAAEa,eADiB;AAExB1C,QAAAA,GAAG,EAAHA,GAFwB;AAGxBC,QAAAA,SAAS,EAATA,SAAAA;AAHwB,OAAD,CAAzB,CAAA;AAKD,KAAA;;AACD,IAAMF,IAAAA,QAAQ,GAAG2C,eAAjB,CAAA;;AAEA,IAAI,IAAA;AACF,MAAA,OAAO5C,WAAW,CAACC,QAAD,EAAWC,GAAX,EAAgBC,SAAhB,CAAlB,CAAA;AACD,KAFD,CAEE,OAAO4B,KAAP,EAAc;AACd,MAAOe,OAAAA,6BAA6B,CAClC5C,GADkC,EAElCgC,aAAa,CAACC,eAFoB,EAGjCJ,KAAe,CAACtB,OAHiB,CAApC,CAAA;AAKD,KAAA;AACF,GAvBD,CAAA;;AAyBA,EAAA,OAAOsD,WAAP,CAAA;AACD;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.esm7.js","sources":["../../src/core/resolveNamespace.tsx"],"sourcesContent":["/**\n * For the strictly typed messages to work we have to wrap the namespace into\n * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045\n */\nexport default function resolveNamespace(\n namespace: string,\n namespacePrefix: string\n) {\n return namespace === namespacePrefix\n ? undefined\n : namespace.slice((namespacePrefix + '.').length);\n}\n"],"names":["resolveNamespace","namespace","namespacePrefix","undefined","slice","length"],"mappings":"AAAA;;;AAGG;AACW,SAAUA,gBAAV,CACZC,SADY,EAEZC,eAFY,EAEW;AAEvB,EAAA,OAAOD,SAAS,KAAKC,eAAd,GACHC,SADG,GAEHF,SAAS,CAACG,KAAV,CAAgB,CAACF,eAAe,GAAG,GAAnB,EAAwBG,MAAxC,CAFJ,CAAA;AAGD;;;;"}
|
|
@@ -1,10 +1,233 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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.esm.js';
|
|
5
|
+
import convertFormatsToIntlMessageFormat from './use-intl.esm10.js';
|
|
6
|
+
import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm6.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 message;
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
message = resolvePath(messages, key, namespace);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function joinPath(parts) {
|
|
125
|
+
return parts.filter(function (part) {
|
|
126
|
+
return part != null;
|
|
127
|
+
}).join('.');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
var cacheKey = joinPath([namespace, key, String(message)]);
|
|
131
|
+
var messageFormat;
|
|
132
|
+
|
|
133
|
+
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
134
|
+
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
135
|
+
} else {
|
|
136
|
+
if (typeof message === 'object') {
|
|
137
|
+
var code, errorMessage;
|
|
138
|
+
|
|
139
|
+
if (Array.isArray(message)) {
|
|
140
|
+
code = IntlErrorCode.INVALID_MESSAGE;
|
|
141
|
+
|
|
142
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
143
|
+
errorMessage = "Message at `" + joinPath([namespace, key]) + "` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages";
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
code = IntlErrorCode.INSUFFICIENT_PATH;
|
|
147
|
+
|
|
148
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
149
|
+
errorMessage = "Message at `" + joinPath([namespace, key]) + "` 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";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
158
|
+
} catch (error) {
|
|
159
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (cachedFormatsByLocale) {
|
|
163
|
+
if (!cachedFormatsByLocale[locale]) {
|
|
164
|
+
cachedFormatsByLocale[locale] = {};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
var formattedMessage = messageFormat.format( // @ts-ignore `intl-messageformat` expects a different format
|
|
173
|
+
// for rich text elements since a recent minor update. This
|
|
174
|
+
// needs to be evaluated in detail, possibly also in regards
|
|
175
|
+
// to be able to format to parts.
|
|
176
|
+
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
177
|
+
|
|
178
|
+
if (formattedMessage == null) {
|
|
179
|
+
throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
180
|
+
} // Limit the function signature to return strings or React elements
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
return isValidElement(formattedMessage) || // Arrays of React elements
|
|
184
|
+
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function translateFn(
|
|
191
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
192
|
+
key,
|
|
193
|
+
/** Key value pairs for values to interpolate into the message. */
|
|
194
|
+
values,
|
|
195
|
+
/** Provide custom formats for numbers, dates and times. */
|
|
196
|
+
formats) {
|
|
197
|
+
var result = translateBaseFn(key, values, formats);
|
|
198
|
+
|
|
199
|
+
if (typeof result !== 'string') {
|
|
200
|
+
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);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
translateFn.rich = translateBaseFn;
|
|
207
|
+
|
|
208
|
+
translateFn.raw = function (
|
|
209
|
+
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
210
|
+
key) {
|
|
211
|
+
if (messagesOrError instanceof IntlError) {
|
|
212
|
+
// We have already warned about this during render
|
|
213
|
+
return getMessageFallback({
|
|
214
|
+
error: messagesOrError,
|
|
215
|
+
key: key,
|
|
216
|
+
namespace: namespace
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
var messages = messagesOrError;
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
return resolvePath(messages, key, namespace);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
return translateFn;
|
|
7
230
|
}
|
|
8
231
|
|
|
9
|
-
export {
|
|
232
|
+
export { createBaseTranslator as default, getMessagesOrError };
|
|
10
233
|
//# sourceMappingURL=use-intl.esm8.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm8.js","sources":["../../src/core/resolveNamespace.tsx"],"sourcesContent":["/**\n * For the strictly typed messages to work we have to wrap the namespace into\n * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045\n */\nexport default function resolveNamespace(\n namespace: string,\n namespacePrefix: string\n) {\n return namespace === namespacePrefix\n ? undefined\n : namespace.slice((namespacePrefix + '.').length);\n}\n"],"names":["resolveNamespace","namespace","namespacePrefix","undefined","slice","length"],"mappings":"AAAA;;;AAGG;AACW,SAAUA,gBAAV,CACZC,SADY,EAEZC,eAFY,EAEW;AAEvB,EAAA,OAAOD,SAAS,KAAKC,eAAd,GACHC,SADG,GAEHF,SAAS,CAACG,KAAV,CAAgB,CAACF,eAAe,GAAG,GAAnB,EAAwBG,MAAxC,CAFJ,CAAA;AAGD;;;;"}
|
|
1
|
+
{"version":3,"file":"use-intl.esm8.js","sources":["../../src/core/createBaseTranslator.tsx"],"sourcesContent":["// eslint-disable-next-line import/no-named-as-default -- False positive\nimport IntlMessageFormat from 'intl-messageformat';\nimport {\n cloneElement,\n isValidElement,\n ReactElement,\n ReactNode,\n ReactNodeArray\n} from 'react';\nimport AbstractIntlMessages from './AbstractIntlMessages';\nimport Formats from './Formats';\nimport {InitializedIntlConfig} from './IntlConfig';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport TranslationValues, {RichTranslationValues} from './TranslationValues';\nimport convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\nfunction resolvePath(\n messages: AbstractIntlMessages | undefined,\n key: string,\n namespace?: string\n) {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages available at \\`${namespace}\\`.`\n : undefined\n );\n }\n\n let message = messages;\n\n key.split('.').forEach((part) => {\n const next = (message as any)[part];\n\n if (part == null || next == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Could not resolve \\`${key}\\` in ${\n namespace ? `\\`${namespace}\\`` : 'messages'\n }.`\n : undefined\n );\n }\n\n message = next;\n });\n\n return message;\n}\n\nfunction prepareTranslationValues(values: RichTranslationValues) {\n if (Object.keys(values).length === 0) return undefined;\n\n // Workaround for https://github.com/formatjs/formatjs/issues/1467\n const transformedValues: RichTranslationValues = {};\n Object.keys(values).forEach((key) => {\n let index = 0;\n const value = values[key];\n\n let transformed;\n if (typeof value === 'function') {\n transformed = (chunks: ReactNode) => {\n const result = value(chunks);\n\n return isValidElement(result)\n ? cloneElement(result, {key: key + index++})\n : result;\n };\n } else {\n transformed = value;\n }\n\n transformedValues[key] = transformed;\n });\n\n return transformedValues;\n}\n\nexport function getMessagesOrError<Messages extends AbstractIntlMessages>({\n messages,\n namespace,\n onError = defaultOnError\n}: {\n messages: Messages;\n namespace?: string;\n onError?(error: IntlError): void;\n}) {\n try {\n if (!messages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages were configured on the provider.`\n : undefined\n );\n }\n\n const retrievedMessages = namespace\n ? resolvePath(messages, namespace)\n : messages;\n\n if (!retrievedMessages) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `No messages for namespace \\`${namespace}\\` found.`\n : undefined\n );\n }\n\n return retrievedMessages;\n } catch (error) {\n const intlError = new IntlError(\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n onError(intlError);\n return intlError;\n }\n}\n\nexport type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {\n cachedFormatsByLocale?: Record<string, Record<string, IntlMessageFormat>>;\n defaultTranslationValues?: RichTranslationValues;\n namespace?: string;\n messagesOrError: Messages | IntlError;\n};\n\nexport default function createBaseTranslator<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>({\n cachedFormatsByLocale,\n defaultTranslationValues,\n formats: globalFormats,\n getMessageFallback = defaultGetMessageFallback,\n locale,\n messagesOrError,\n namespace,\n onError,\n timeZone\n}: CreateBaseTranslatorProps<Messages>) {\n function getFallbackFromErrorAndNotify(\n key: string,\n code: IntlErrorCode,\n message?: string\n ) {\n const error = new IntlError(code, message);\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n function translateBaseFn(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string,\n /** Key value pairs for values to interpolate into the message. */\n values?: RichTranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n let message;\n try {\n message = resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n\n function joinPath(parts: Array<string | undefined>) {\n return parts.filter((part) => part != null).join('.');\n }\n\n const cacheKey = joinPath([namespace, key, String(message)]);\n\n let messageFormat;\n if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {\n messageFormat = cachedFormatsByLocale?.[locale][cacheKey];\n } else {\n if (typeof message === 'object') {\n let code, errorMessage;\n if (Array.isArray(message)) {\n code = IntlErrorCode.INVALID_MESSAGE;\n if (process.env.NODE_ENV !== 'production') {\n errorMessage = `Message at \\`${joinPath([\n namespace,\n key\n ])}\\` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages`;\n }\n } else {\n code = IntlErrorCode.INSUFFICIENT_PATH;\n if (process.env.NODE_ENV !== 'production') {\n errorMessage = `Message at \\`${joinPath([\n namespace,\n key\n ])}\\` 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`;\n }\n }\n\n return getFallbackFromErrorAndNotify(key, code, errorMessage);\n }\n\n try {\n messageFormat = new IntlMessageFormat(\n message,\n locale,\n convertFormatsToIntlMessageFormat(\n {...globalFormats, ...formats},\n timeZone\n )\n );\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n (error as Error).message\n );\n }\n\n if (cachedFormatsByLocale) {\n if (!cachedFormatsByLocale[locale]) {\n cachedFormatsByLocale[locale] = {};\n }\n cachedFormatsByLocale[locale][cacheKey] = messageFormat;\n }\n }\n\n try {\n const formattedMessage = messageFormat.format(\n // @ts-ignore `intl-messageformat` expects a different format\n // for rich text elements since a recent minor update. This\n // needs to be evaluated in detail, possibly also in regards\n // to be able to format to parts.\n prepareTranslationValues({...defaultTranslationValues, ...values})\n );\n\n if (formattedMessage == null) {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `Unable to format \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n }`\n : undefined\n );\n }\n\n // Limit the function signature to return strings or React elements\n return isValidElement(formattedMessage) ||\n // Arrays of React elements\n Array.isArray(formattedMessage) ||\n typeof formattedMessage === 'string'\n ? formattedMessage\n : String(formattedMessage);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.FORMATTING_ERROR,\n (error as Error).message\n );\n }\n }\n\n function translateFn<\n TargetKey extends MessageKeys<\n NestedValueOf<Messages, NestedKey>,\n NestedKeyOf<NestedValueOf<Messages, NestedKey>>\n >\n >(\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: TargetKey,\n /** Key value pairs for values to interpolate into the message. */\n values?: TranslationValues,\n /** Provide custom formats for numbers, dates and times. */\n formats?: Partial<Formats>\n ): string {\n const result = translateBaseFn(key, values, formats);\n\n if (typeof result !== 'string') {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.INVALID_MESSAGE,\n process.env.NODE_ENV !== 'production'\n ? `The message \\`${key}\\` in ${\n namespace ? `namespace \\`${namespace}\\`` : 'messages'\n } didn't resolve to a string. If you want to format rich text, use \\`t.rich\\` instead.`\n : undefined\n );\n }\n\n return result;\n }\n\n translateFn.rich = translateBaseFn;\n\n translateFn.raw = (\n /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */\n key: string\n ): any => {\n if (messagesOrError instanceof IntlError) {\n // We have already warned about this during render\n return getMessageFallback({\n error: messagesOrError,\n key,\n namespace\n });\n }\n const messages = messagesOrError;\n\n try {\n return resolvePath(messages, key, namespace);\n } catch (error) {\n return getFallbackFromErrorAndNotify(\n key,\n IntlErrorCode.MISSING_MESSAGE,\n (error as Error).message\n );\n }\n };\n\n return translateFn;\n}\n"],"names":["resolvePath","messages","key","namespace","Error","process","env","NODE_ENV","undefined","message","split","forEach","part","next","prepareTranslationValues","values","Object","keys","length","transformedValues","index","value","transformed","chunks","result","isValidElement","cloneElement","getMessagesOrError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","createBaseTranslator","cachedFormatsByLocale","defaultTranslationValues","globalFormats","formats","getMessageFallback","defaultGetMessageFallback","locale","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","joinPath","parts","filter","join","cacheKey","String","messageFormat","errorMessage","Array","isArray","INVALID_MESSAGE","INSUFFICIENT_PATH","IntlMessageFormat","convertFormatsToIntlMessageFormat","formattedMessage","format","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAoBA,SAASA,WAAT,CACEC,QADF,EAEEC,GAFF,EAGEC,SAHF,EAGoB;AAElB,EAAI,IAAA,CAACF,QAAL,EAAe;AACb,IAAA,MAAM,IAAIG,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GAAA,4BAAA,GACkCJ,SADlC,GAAA,IAAA,GAEIK,SAHA,CAAN,CAAA;AAKD,GAAA;;AAED,EAAIC,IAAAA,OAAO,GAAGR,QAAd,CAAA;AAEAC,EAAAA,GAAG,CAACQ,KAAJ,CAAU,GAAV,EAAeC,OAAf,CAAuB,UAACC,IAAD,EAAS;AAC9B,IAAA,IAAMC,IAAI,GAAIJ,OAAe,CAACG,IAAD,CAA7B,CAAA;;AAEA,IAAA,IAAIA,IAAI,IAAI,IAAR,IAAgBC,IAAI,IAAI,IAA5B,EAAkC;AAChC,MAAM,MAAA,IAAIT,KAAJ,CACJC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,2BAC2BL,GAD3B,GAAA,OAAA,IAEMC,SAAS,GAAQA,GAAAA,GAAAA,SAAR,SAAwB,UAFvC,CAAA,GAAA,GAAA,GAIIK,SALA,CAAN,CAAA;AAOD,KAAA;;AAEDC,IAAAA,OAAO,GAAGI,IAAV,CAAA;AACD,GAdD,CAAA,CAAA;AAgBA,EAAA,OAAOJ,OAAP,CAAA;AACD,CAAA;;AAED,SAASK,wBAAT,CAAkCC,MAAlC,EAA+D;AAC7D,EAAA,IAAIC,MAAM,CAACC,IAAP,CAAYF,MAAZ,CAAA,CAAoBG,MAApB,KAA+B,CAAnC,EAAsC,OAAOV,SAAP,CADuB;;AAI7D,EAAMW,IAAAA,iBAAiB,GAA0B,EAAjD,CAAA;AACAH,EAAAA,MAAM,CAACC,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4B,KAAP,EAAc;AACd,MAAOe,OAAAA,6BAA6B,CAClC5C,GADkC,EAElCgC,aAAa,CAACC,eAFoB,EAGjCJ,KAAe,CAACtB,OAHiB,CAApC,CAAA;AAKD,KAAA;AACF,GAvBD,CAAA;;AAyBA,EAAA,OAAOyD,WAAP,CAAA;AACD;;;;"}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import IntlConfig
|
|
1
|
+
import IntlConfig from '../core/IntlConfig';
|
|
2
2
|
/**
|
|
3
3
|
* Enhances the incoming props with defaults.
|
|
4
4
|
*/
|
|
5
|
-
export default function getInitializedConfig<Props extends Omit<IntlConfig, 'children'>>({ getMessageFallback, messages, onError, ...rest }: Props):
|
|
5
|
+
export default function getInitializedConfig<Props extends Omit<IntlConfig, 'children'>>({ getMessageFallback, messages, onError, ...rest }: Props): Omit<Props, "onError" | "getMessageFallback" | "messages"> & {
|
|
6
|
+
messages: import("../core/AbstractIntlMessages").default | undefined;
|
|
7
|
+
onError: (error: import("..").IntlError) => void;
|
|
8
|
+
getMessageFallback: (info: {
|
|
9
|
+
error: import("..").IntlError;
|
|
10
|
+
key: string;
|
|
11
|
+
namespace?: string | undefined;
|
|
12
|
+
}) => string;
|
|
13
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useRef, useMemo } from 'react';
|
|
2
|
-
import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.
|
|
3
|
-
import resolveNamespace from '../core/use-intl.
|
|
2
|
+
import createBaseTranslator, { getMessagesOrError } from '../core/use-intl.esm8.js';
|
|
3
|
+
import resolveNamespace from '../core/use-intl.esm7.js';
|
|
4
4
|
import useIntlContext from './use-intl.esm10.js';
|
|
5
5
|
|
|
6
6
|
function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm9.js","sources":["../../src/react/getInitializedConfig.tsx"],"sourcesContent":["import IntlConfig
|
|
1
|
+
{"version":3,"file":"use-intl.esm9.js","sources":["../../src/react/getInitializedConfig.tsx"],"sourcesContent":["import IntlConfig from '../core/IntlConfig';\nimport {defaultGetMessageFallback, defaultOnError} from '../core/defaults';\nimport validateMessages from '../core/validateMessages';\n\n/**\n * Enhances the incoming props with defaults.\n */\nexport default function getInitializedConfig<\n // This is a generic to allow for stricter typing. E.g.\n // the RSC integration always provides a `now` value.\n Props extends Omit<IntlConfig, 'children'>\n>({getMessageFallback, messages, onError, ...rest}: Props) {\n const finalOnError = onError || defaultOnError;\n const finalGetMessageFallback =\n getMessageFallback || defaultGetMessageFallback;\n\n if (process.env.NODE_ENV !== 'production') {\n if (messages) {\n validateMessages(messages, finalOnError);\n }\n }\n\n return {\n ...rest,\n messages,\n onError: finalOnError,\n getMessageFallback: finalGetMessageFallback\n };\n}\n"],"names":["getInitializedConfig","getMessageFallback","messages","onError","rest","finalOnError","defaultOnError","finalGetMessageFallback","defaultGetMessageFallback","process","env","NODE_ENV","validateMessages"],"mappings":";;;;;AAIA;;AAEG;;AACqB,SAAAA,oBAAA,CAIiC,IAAA,EAAA;AAAA,EAAtDC,IAAAA,kBAAsD,QAAtDA,kBAAsD;AAAA,MAAlCC,QAAkC,QAAlCA,QAAkC;AAAA,MAAxBC,OAAwB,QAAxBA,OAAwB;AAAA,MAAZC,IAAY,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AACvD,EAAA,IAAMC,YAAY,GAAGF,OAAO,IAAIG,cAAhC,CAAA;AACA,EAAA,IAAMC,uBAAuB,GAC3BN,kBAAkB,IAAIO,yBADxB,CAAA;;AAGA,EAAA,IAAIC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAA7B,EAA2C;AACzC,IAAA,IAAIT,QAAJ,EAAc;AACZU,MAAAA,gBAAgB,CAACV,QAAD,EAAWG,YAAX,CAAhB,CAAA;AACD,KAAA;AACF,GAAA;;AAED,EAAA,OAAA,QAAA,CAAA,EAAA,EACKD,IADL,EAAA;AAEEF,IAAAA,QAAQ,EAARA,QAFF;AAGEC,IAAAA,OAAO,EAAEE,YAHX;AAIEJ,IAAAA,kBAAkB,EAAEM,uBAAAA;AAJtB,GAAA,CAAA,CAAA;AAMD;;;;"}
|
|
@@ -98,18 +98,36 @@ export default function createBaseTranslator({ cachedFormatsByLocale, defaultTra
|
|
|
98
98
|
catch (error) {
|
|
99
99
|
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
.filter((part) => part != null)
|
|
103
|
-
|
|
101
|
+
function joinPath(parts) {
|
|
102
|
+
return parts.filter((part) => part != null).join('.');
|
|
103
|
+
}
|
|
104
|
+
const cacheKey = joinPath([namespace, key, String(message)]);
|
|
104
105
|
let messageFormat;
|
|
105
106
|
if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {
|
|
106
107
|
messageFormat = cachedFormatsByLocale?.[locale][cacheKey];
|
|
107
108
|
}
|
|
108
109
|
else {
|
|
109
110
|
if (typeof message === 'object') {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
let code, errorMessage;
|
|
112
|
+
if (Array.isArray(message)) {
|
|
113
|
+
code = IntlErrorCode.INVALID_MESSAGE;
|
|
114
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
115
|
+
errorMessage = `Message at \`${joinPath([
|
|
116
|
+
namespace,
|
|
117
|
+
key
|
|
118
|
+
])}\` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
code = IntlErrorCode.INSUFFICIENT_PATH;
|
|
123
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
124
|
+
errorMessage = `Message at \`${joinPath([
|
|
125
|
+
namespace,
|
|
126
|
+
key
|
|
127
|
+
])}\` 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`;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
113
131
|
}
|
|
114
132
|
try {
|
|
115
133
|
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat({ ...globalFormats, ...formats }, timeZone));
|