use-intl 2.19.3 → 2.20.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/core/MessageFormatCache.d.ts +5 -0
- package/dist/core/createBaseTranslator.d.ts +3 -3
- package/dist/core/use-intl.esm10.js.map +1 -1
- package/dist/core/use-intl.esm2.js +2 -2
- package/dist/core/use-intl.esm3.js +1 -1
- package/dist/core/use-intl.esm5.js +50 -12
- package/dist/core/use-intl.esm5.js.map +1 -1
- package/dist/core/use-intl.esm6.js +12 -50
- package/dist/core/use-intl.esm6.js.map +1 -1
- package/dist/core/use-intl.esm7.js +22 -13
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/react/IntlContext.d.ts +11 -2
- package/dist/react/use-intl.esm.js +9 -3
- package/dist/react/use-intl.esm.js.map +1 -1
- package/dist/react/use-intl.esm10.js +1 -1
- package/dist/react/use-intl.esm12.js +4 -4
- package/dist/react/use-intl.esm12.js.map +1 -1
- package/dist/react/use-intl.esm9.js.map +1 -1
- package/dist/react/useIntlContext.d.ts +10 -1
- package/dist/src/core/MessageFormatCache.d.ts +5 -0
- package/dist/src/core/MessageFormatCache.js +2 -0
- package/dist/src/core/MessageFormatCache.js.map +1 -0
- package/dist/src/core/convertFormatsToIntlMessageFormat.js +1 -0
- package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +1 -1
- package/dist/src/core/createBaseTranslator.d.ts +3 -3
- package/dist/src/core/createBaseTranslator.js +22 -10
- package/dist/src/core/createBaseTranslator.js.map +1 -1
- package/dist/src/react/IntlContext.d.ts +11 -2
- package/dist/src/react/IntlContext.js.map +1 -1
- package/dist/src/react/IntlProvider.js +6 -2
- package/dist/src/react/IntlProvider.js.map +1 -1
- package/dist/src/react/useIntlContext.d.ts +10 -1
- package/dist/src/react/useTranslationsImpl.js +4 -4
- package/dist/src/react/useTranslationsImpl.js.map +1 -1
- package/dist/use-intl.cjs.development.js +31 -16
- 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/MessageFormatCache.tsx +10 -0
- package/src/core/convertFormatsToIntlMessageFormat.tsx +1 -0
- package/src/core/createBaseTranslator.tsx +29 -12
- package/src/react/IntlContext.tsx +7 -1
- package/src/react/IntlProvider.tsx +9 -2
- package/src/react/useTranslationsImpl.tsx +4 -8
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import IntlMessageFormat from 'intl-messageformat';
|
|
2
1
|
import { ReactElement, ReactNodeArray } from 'react';
|
|
3
2
|
import AbstractIntlMessages from './AbstractIntlMessages';
|
|
4
3
|
import Formats from './Formats';
|
|
5
4
|
import { InitializedIntlConfig } from './IntlConfig';
|
|
6
5
|
import IntlError from './IntlError';
|
|
6
|
+
import MessageFormatCache from './MessageFormatCache';
|
|
7
7
|
import TranslationValues, { RichTranslationValues } from './TranslationValues';
|
|
8
8
|
import MessageKeys from './utils/MessageKeys';
|
|
9
9
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
@@ -14,12 +14,12 @@ export declare function getMessagesOrError<Messages extends AbstractIntlMessages
|
|
|
14
14
|
onError?(error: IntlError): void;
|
|
15
15
|
}): AbstractIntlMessages | IntlError;
|
|
16
16
|
export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
|
|
17
|
-
|
|
17
|
+
messageFormatCache?: MessageFormatCache;
|
|
18
18
|
defaultTranslationValues?: RichTranslationValues;
|
|
19
19
|
namespace?: string;
|
|
20
20
|
messagesOrError: Messages | IntlError;
|
|
21
21
|
};
|
|
22
|
-
export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({
|
|
22
|
+
export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ defaultTranslationValues, formats: globalFormats, getMessageFallback, locale, messageFormatCache, messagesOrError, namespace, onError, timeZone }: CreateBaseTranslatorProps<Messages>): {
|
|
23
23
|
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues, formats?: Partial<Formats>): string;
|
|
24
24
|
rich: (key: string, values?: RichTranslationValues, formats?: Partial<Formats>) => string | ReactElement | ReactNodeArray;
|
|
25
25
|
raw(key: string): any;
|
|
@@ -69,7 +69,20 @@ export function getMessagesOrError({ messages, namespace, onError = defaultOnErr
|
|
|
69
69
|
return intlError;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
function getPlainMessage(candidate, values) {
|
|
73
|
+
if (values)
|
|
74
|
+
return undefined;
|
|
75
|
+
const unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
|
|
76
|
+
// Placeholders can be in the message if there are default values,
|
|
77
|
+
// or if the user has forgotten to provide values. In the latter
|
|
78
|
+
// case we need to compile the message to receive an error.
|
|
79
|
+
const hasPlaceholders = /<|{/.test(unescapedMessage);
|
|
80
|
+
if (!hasPlaceholders) {
|
|
81
|
+
return unescapedMessage;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
export default function createBaseTranslator({ defaultTranslationValues, formats: globalFormats, getMessageFallback = defaultGetMessageFallback, locale, messageFormatCache, messagesOrError, namespace, onError, timeZone }) {
|
|
73
86
|
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
74
87
|
const error = new IntlError(code, message);
|
|
75
88
|
onError(error);
|
|
@@ -101,10 +114,10 @@ export default function createBaseTranslator({ cachedFormatsByLocale, defaultTra
|
|
|
101
114
|
function joinPath(parts) {
|
|
102
115
|
return parts.filter((part) => part != null).join('.');
|
|
103
116
|
}
|
|
104
|
-
const cacheKey = joinPath([namespace, key, String(message)]);
|
|
117
|
+
const cacheKey = joinPath([locale, namespace, key, String(message)]);
|
|
105
118
|
let messageFormat;
|
|
106
|
-
if (
|
|
107
|
-
messageFormat =
|
|
119
|
+
if (messageFormatCache?.has(cacheKey)) {
|
|
120
|
+
messageFormat = messageFormatCache.get(cacheKey);
|
|
108
121
|
}
|
|
109
122
|
else {
|
|
110
123
|
if (typeof message === 'object') {
|
|
@@ -129,18 +142,17 @@ export default function createBaseTranslator({ cachedFormatsByLocale, defaultTra
|
|
|
129
142
|
}
|
|
130
143
|
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
131
144
|
}
|
|
145
|
+
// Hot path that avoids creating an `IntlMessageFormat` instance
|
|
146
|
+
const plainMessage = getPlainMessage(message, values);
|
|
147
|
+
if (plainMessage)
|
|
148
|
+
return plainMessage;
|
|
132
149
|
try {
|
|
133
150
|
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat({ ...globalFormats, ...formats }, timeZone));
|
|
134
151
|
}
|
|
135
152
|
catch (error) {
|
|
136
153
|
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
137
154
|
}
|
|
138
|
-
|
|
139
|
-
if (!cachedFormatsByLocale[locale]) {
|
|
140
|
-
cachedFormatsByLocale[locale] = {};
|
|
141
|
-
}
|
|
142
|
-
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
143
|
-
}
|
|
155
|
+
messageFormatCache?.set(cacheKey, messageFormat);
|
|
144
156
|
}
|
|
145
157
|
try {
|
|
146
158
|
const formattedMessage = messageFormat.format(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createBaseTranslator.js","sourceRoot":"","sources":["../../../src/core/createBaseTranslator.tsx"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,OAAO,iBAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,YAAY,EACZ,cAAc,EAIf,MAAM,OAAO,CAAC;AAIf,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"createBaseTranslator.js","sourceRoot":"","sources":["../../../src/core/createBaseTranslator.tsx"],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,GAAG,EAAE,SAAS,CAAC,CAAC;SAC9C;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,6BAA6B,CAClC,GAAG,EACH,aAAa,CAAC,eAAe,EAC5B,KAAe,CAAC,OAAO,CACzB,CAAC;SACH;IACH,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import
|
|
3
|
-
declare const IntlContext: import("react").Context<
|
|
2
|
+
import MessageFormatCache from '../core/MessageFormatCache';
|
|
3
|
+
declare const IntlContext: import("react").Context<(import("../core/IntlConfig").default<import("../core/AbstractIntlMessages").default> & {
|
|
4
|
+
onError: (error: import("..").IntlError) => void;
|
|
5
|
+
getMessageFallback: (info: {
|
|
6
|
+
error: import("..").IntlError;
|
|
7
|
+
key: string;
|
|
8
|
+
namespace?: string | undefined;
|
|
9
|
+
}) => string;
|
|
10
|
+
} & {
|
|
11
|
+
messageFormatCache?: MessageFormatCache | undefined;
|
|
12
|
+
}) | undefined>;
|
|
4
13
|
export default IntlContext;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntlContext.js","sourceRoot":"","sources":["../../../src/react/IntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"IntlContext.js","sourceRoot":"","sources":["../../../src/react/IntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;AAIpC,MAAM,WAAW,GAAG,aAAa,CAK/B,SAAS,CAAC,CAAC;AAEb,eAAe,WAAW,CAAC"}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
2
|
import IntlContext from './IntlContext';
|
|
3
3
|
import getInitializedConfig from './getInitializedConfig';
|
|
4
4
|
export default function IntlProvider({ children, ...props }) {
|
|
5
|
-
|
|
5
|
+
const [messageFormatCache] = useState(() => new Map());
|
|
6
|
+
return (React.createElement(IntlContext.Provider, { value: {
|
|
7
|
+
...getInitializedConfig(props),
|
|
8
|
+
messageFormatCache
|
|
9
|
+
} }, children));
|
|
6
10
|
}
|
|
7
11
|
//# sourceMappingURL=IntlProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntlProvider.js","sourceRoot":"","sources":["../../../src/react/IntlProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"IntlProvider.js","sourceRoot":"","sources":["../../../src/react/IntlProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAY,QAAQ,EAAC,MAAM,OAAO,CAAC;AAEjD,OAAO,WAAW,MAAM,eAAe,CAAC;AACxC,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAM1D,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,EAAC,QAAQ,EAAE,GAAG,KAAK,EAAQ;IAC9D,MAAM,CAAC,kBAAkB,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;IAEvD,OAAO,CACL,oBAAC,WAAW,CAAC,QAAQ,IACnB,KAAK,EAAE;YACL,GAAG,oBAAoB,CAAC,KAAK,CAAC;YAC9B,kBAAkB;SACnB,IAEA,QAAQ,CACY,CACxB,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1,10 @@
|
|
|
1
|
-
export default function useIntlContext(): import("../core/IntlConfig").
|
|
1
|
+
export default function useIntlContext(): import("../core/IntlConfig").default<import("../core/AbstractIntlMessages").default> & {
|
|
2
|
+
onError: (error: import("..").IntlError) => void;
|
|
3
|
+
getMessageFallback: (info: {
|
|
4
|
+
error: import("..").IntlError;
|
|
5
|
+
key: string;
|
|
6
|
+
namespace?: string | undefined;
|
|
7
|
+
}) => string;
|
|
8
|
+
} & {
|
|
9
|
+
messageFormatCache?: import("../core/MessageFormatCache").default | undefined;
|
|
10
|
+
};
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { useMemo
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
2
|
import createBaseTranslator, { getMessagesOrError } from '../core/createBaseTranslator';
|
|
3
3
|
import resolveNamespace from '../core/resolveNamespace';
|
|
4
4
|
import useIntlContext from './useIntlContext';
|
|
5
5
|
export default function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
6
|
-
const { defaultTranslationValues, formats: globalFormats, getMessageFallback, locale, onError, timeZone } = useIntlContext();
|
|
6
|
+
const { defaultTranslationValues, formats: globalFormats, getMessageFallback, locale, messageFormatCache, onError, timeZone } = useIntlContext();
|
|
7
7
|
// The `namespacePrefix` is part of the type system.
|
|
8
8
|
// See the comment in the hook invocation.
|
|
9
9
|
allMessages = allMessages[namespacePrefix];
|
|
10
10
|
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
11
|
-
const cachedFormatsByLocaleRef = useRef({});
|
|
12
11
|
const messagesOrError = useMemo(() => getMessagesOrError({ messages: allMessages, namespace, onError }), [allMessages, namespace, onError]);
|
|
13
12
|
const translate = useMemo(() => createBaseTranslator({
|
|
14
|
-
|
|
13
|
+
messageFormatCache,
|
|
15
14
|
getMessageFallback,
|
|
16
15
|
messagesOrError,
|
|
17
16
|
defaultTranslationValues,
|
|
@@ -21,6 +20,7 @@ export default function useTranslationsImpl(allMessages, namespace, namespacePre
|
|
|
21
20
|
locale,
|
|
22
21
|
timeZone
|
|
23
22
|
}), [
|
|
23
|
+
messageFormatCache,
|
|
24
24
|
getMessageFallback,
|
|
25
25
|
messagesOrError,
|
|
26
26
|
defaultTranslationValues,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTranslationsImpl.js","sourceRoot":"","sources":["../../../src/react/useTranslationsImpl.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTranslationsImpl.js","sourceRoot":"","sources":["../../../src/react/useTransl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|
|
242
242
|
}
|
|
243
|
+
function getPlainMessage(candidate, values) {
|
|
244
|
+
if (values) return undefined;
|
|
245
|
+
var unescapedMessage = candidate.replace(/'([{}])/gi, '$1');
|
|
246
|
+
// Placeholders can be in the message if there are default values,
|
|
247
|
+
// or if the user has forgotten to provide values. In the latter
|
|
248
|
+
// case we need to compile the message to receive an error.
|
|
249
|
+
var hasPlaceholders = /<|{/.test(unescapedMessage);
|
|
250
|
+
if (!hasPlaceholders) {
|
|
251
|
+
return unescapedMessage;
|
|
252
|
+
}
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
243
255
|
function createBaseTranslator(_ref2) {
|
|
244
|
-
var
|
|
245
|
-
defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
256
|
+
var defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
246
257
|
globalFormats = _ref2.formats,
|
|
247
258
|
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
248
259
|
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
249
260
|
locale = _ref2.locale,
|
|
261
|
+
messageFormatCache = _ref2.messageFormatCache,
|
|
250
262
|
messagesOrError = _ref2.messagesOrError,
|
|
251
263
|
namespace = _ref2.namespace,
|
|
252
264
|
onError = _ref2.onError,
|
|
@@ -264,7 +276,6 @@ function createBaseTranslator(_ref2) {
|
|
|
264
276
|
key, /** Key value pairs for values to interpolate into the message. */
|
|
265
277
|
values, /** Provide custom formats for numbers, dates and times. */
|
|
266
278
|
formats) {
|
|
267
|
-
var _cachedFormatsByLocal;
|
|
268
279
|
if (messagesOrError instanceof IntlError) {
|
|
269
280
|
// We have already warned about this during render
|
|
270
281
|
return getMessageFallback({
|
|
@@ -285,10 +296,10 @@ function createBaseTranslator(_ref2) {
|
|
|
285
296
|
return part != null;
|
|
286
297
|
}).join('.');
|
|
287
298
|
}
|
|
288
|
-
var cacheKey = joinPath([namespace, key, String(message)]);
|
|
299
|
+
var cacheKey = joinPath([locale, namespace, key, String(message)]);
|
|
289
300
|
var messageFormat;
|
|
290
|
-
if (
|
|
291
|
-
messageFormat =
|
|
301
|
+
if (messageFormatCache != null && messageFormatCache.has(cacheKey)) {
|
|
302
|
+
messageFormat = messageFormatCache.get(cacheKey);
|
|
292
303
|
} else {
|
|
293
304
|
if (typeof message === 'object') {
|
|
294
305
|
var code, errorMessage;
|
|
@@ -305,17 +316,15 @@ function createBaseTranslator(_ref2) {
|
|
|
305
316
|
}
|
|
306
317
|
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
307
318
|
}
|
|
319
|
+
// Hot path that avoids creating an `IntlMessageFormat` instance
|
|
320
|
+
var plainMessage = getPlainMessage(message, values);
|
|
321
|
+
if (plainMessage) return plainMessage;
|
|
308
322
|
try {
|
|
309
323
|
messageFormat = new IntlMessageFormat__default["default"](message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
310
324
|
} catch (error) {
|
|
311
325
|
return getFallbackFromErrorAndNotify(key, exports.IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
312
326
|
}
|
|
313
|
-
|
|
314
|
-
if (!cachedFormatsByLocale[locale]) {
|
|
315
|
-
cachedFormatsByLocale[locale] = {};
|
|
316
|
-
}
|
|
317
|
-
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
318
|
-
}
|
|
327
|
+
messageFormatCache == null ? void 0 : messageFormatCache.set(cacheKey, messageFormat);
|
|
319
328
|
}
|
|
320
329
|
try {
|
|
321
330
|
var formattedMessage = messageFormat.format(
|
|
@@ -646,8 +655,14 @@ var _excluded = ["children"];
|
|
|
646
655
|
function IntlProvider(_ref) {
|
|
647
656
|
var children = _ref.children,
|
|
648
657
|
props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
658
|
+
var _useState = React.useState(function () {
|
|
659
|
+
return new Map();
|
|
660
|
+
}),
|
|
661
|
+
messageFormatCache = _useState[0];
|
|
649
662
|
return React__default["default"].createElement(IntlContext.Provider, {
|
|
650
|
-
value: getInitializedConfig(props)
|
|
663
|
+
value: _extends({}, getInitializedConfig(props), {
|
|
664
|
+
messageFormatCache: messageFormatCache
|
|
665
|
+
})
|
|
651
666
|
}, children);
|
|
652
667
|
}
|
|
653
668
|
|
|
@@ -665,13 +680,13 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
665
680
|
globalFormats = _useIntlContext.formats,
|
|
666
681
|
getMessageFallback = _useIntlContext.getMessageFallback,
|
|
667
682
|
locale = _useIntlContext.locale,
|
|
683
|
+
messageFormatCache = _useIntlContext.messageFormatCache,
|
|
668
684
|
onError = _useIntlContext.onError,
|
|
669
685
|
timeZone = _useIntlContext.timeZone;
|
|
670
686
|
// The `namespacePrefix` is part of the type system.
|
|
671
687
|
// See the comment in the hook invocation.
|
|
672
688
|
allMessages = allMessages[namespacePrefix];
|
|
673
689
|
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
674
|
-
var cachedFormatsByLocaleRef = React.useRef({});
|
|
675
690
|
var messagesOrError = React.useMemo(function () {
|
|
676
691
|
return getMessagesOrError({
|
|
677
692
|
messages: allMessages,
|
|
@@ -681,7 +696,7 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
681
696
|
}, [allMessages, namespace, onError]);
|
|
682
697
|
var translate = React.useMemo(function () {
|
|
683
698
|
return createBaseTranslator({
|
|
684
|
-
|
|
699
|
+
messageFormatCache: messageFormatCache,
|
|
685
700
|
getMessageFallback: getMessageFallback,
|
|
686
701
|
messagesOrError: messagesOrError,
|
|
687
702
|
defaultTranslationValues: defaultTranslationValues,
|
|
@@ -691,7 +706,7 @@ function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
|
691
706
|
locale: locale,
|
|
692
707
|
timeZone: timeZone
|
|
693
708
|
});
|
|
694
|
-
}, [getMessageFallback, messagesOrError, defaultTranslationValues, namespace, onError, globalFormats, locale, timeZone]);
|
|
709
|
+
}, [messageFormatCache, getMessageFallback, messagesOrError, defaultTranslationValues, namespace, onError, globalFormats, locale, timeZone]);
|
|
695
710
|
return translate;
|
|
696
711
|
}
|
|
697
712
|
|