use-intl 2.13.2 → 2.14.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/_virtual/use-intl.esm.js +4 -28
- package/dist/_virtual/use-intl.esm.js.map +1 -1
- package/dist/core/AbstractIntlMessages.d.ts +1 -1
- package/dist/core/DateTimeFormatOptions.d.ts +1 -1
- package/dist/core/Formats.d.ts +1 -1
- package/dist/core/IntlConfig.d.ts +2 -2
- package/dist/core/TranslationValues.d.ts +3 -3
- package/dist/core/createBaseTranslator.d.ts +3 -3
- package/dist/core/createFormatter.d.ts +4 -4
- package/dist/core/createIntl.d.ts +1 -1
- package/dist/core/createTranslator.d.ts +1 -1
- package/dist/core/createTranslatorImpl.d.ts +2 -2
- package/dist/core/use-intl.esm.js +0 -9
- package/dist/core/use-intl.esm.js.map +1 -1
- package/dist/core/use-intl.esm10.js +2 -4
- package/dist/core/use-intl.esm10.js.map +1 -1
- package/dist/core/use-intl.esm2.js +9 -11
- package/dist/core/use-intl.esm2.js.map +1 -1
- package/dist/core/use-intl.esm3.js +12 -35
- package/dist/core/use-intl.esm3.js.map +1 -1
- package/dist/core/use-intl.esm4.js +0 -1
- package/dist/core/use-intl.esm4.js.map +1 -1
- package/dist/core/use-intl.esm5.js +9 -15
- package/dist/core/use-intl.esm5.js.map +1 -1
- package/dist/core/use-intl.esm6.js.map +1 -1
- package/dist/core/use-intl.esm7.js +27 -69
- package/dist/core/use-intl.esm7.js.map +1 -1
- package/dist/core/use-intl.esm8.js.map +1 -1
- package/dist/core/use-intl.esm9.js +1 -5
- package/dist/core/use-intl.esm9.js.map +1 -1
- package/dist/core/utils/MessageKeys.d.ts +1 -1
- package/dist/core/utils/NamespaceKeys.d.ts +1 -1
- package/dist/core/utils/NestedKeyOf.d.ts +1 -1
- package/dist/core/utils/NestedValueOf.d.ts +1 -1
- package/dist/react/IntlProvider.d.ts +1 -1
- package/dist/react/use-intl.esm.js +1 -2
- package/dist/react/use-intl.esm.js.map +1 -1
- package/dist/react/use-intl.esm10.js +0 -2
- package/dist/react/use-intl.esm10.js.map +1 -1
- package/dist/react/use-intl.esm11.js +7 -8
- package/dist/react/use-intl.esm11.js.map +1 -1
- package/dist/react/use-intl.esm2.js +4 -4
- package/dist/react/use-intl.esm2.js.map +1 -1
- package/dist/react/use-intl.esm3.js.map +1 -1
- package/dist/react/use-intl.esm4.js +3 -8
- package/dist/react/use-intl.esm4.js.map +1 -1
- package/dist/react/use-intl.esm5.js.map +1 -1
- package/dist/react/use-intl.esm6.js +5 -6
- package/dist/react/use-intl.esm6.js.map +1 -1
- package/dist/react/use-intl.esm7.js +5 -8
- package/dist/react/use-intl.esm7.js.map +1 -1
- package/dist/react/use-intl.esm8.js.map +1 -1
- package/dist/react/use-intl.esm9.js +3 -7
- package/dist/react/use-intl.esm9.js.map +1 -1
- package/dist/react/useFormatter.d.ts +1 -1
- package/dist/react/useIntl.d.ts +1 -1
- package/dist/react/useNow.d.ts +1 -1
- package/dist/src/core/AbstractIntlMessages.d.ts +1 -1
- package/dist/src/core/DateTimeFormatOptions.d.ts +1 -1
- package/dist/src/core/Formats.d.ts +1 -1
- package/dist/src/core/IntlConfig.d.ts +2 -2
- package/dist/src/core/TranslationValues.d.ts +3 -3
- package/dist/src/core/createBaseTranslator.d.ts +3 -3
- package/dist/src/core/createFormatter.d.ts +4 -4
- package/dist/src/core/createIntl.d.ts +1 -1
- package/dist/src/core/createTranslator.d.ts +1 -1
- package/dist/src/core/createTranslator.js +1 -1
- package/dist/src/core/createTranslator.js.map +1 -1
- package/dist/src/core/createTranslatorImpl.d.ts +2 -2
- package/dist/src/core/utils/MessageKeys.d.ts +1 -1
- package/dist/src/core/utils/NamespaceKeys.d.ts +1 -1
- package/dist/src/core/utils/NestedKeyOf.d.ts +1 -1
- package/dist/src/core/utils/NestedValueOf.d.ts +1 -1
- package/dist/src/react/IntlProvider.d.ts +1 -1
- package/dist/src/react/useFormatter.d.ts +1 -1
- package/dist/src/react/useIntl.d.ts +1 -1
- package/dist/src/react/useNow.d.ts +1 -1
- package/dist/use-intl.cjs.development.js +91 -221
- 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 +17 -17
- package/src/core/createTranslator.tsx +1 -1
|
@@ -6,11 +6,10 @@ import resolveNamespace from './use-intl.esm8.js';
|
|
|
6
6
|
var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
|
|
7
7
|
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
8
8
|
var getMessageFallback = _ref.getMessageFallback,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
messages = _ref.messages,
|
|
10
|
+
namespace = _ref.namespace,
|
|
11
|
+
onError = _ref.onError,
|
|
12
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
14
13
|
// The `namespacePrefix` is part of the type system.
|
|
15
14
|
// See the comment in the function invocation.
|
|
16
15
|
messages = messages[namespacePrefix];
|
|
@@ -25,19 +24,16 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
25
24
|
})
|
|
26
25
|
}));
|
|
27
26
|
var originalRich = translator.rich;
|
|
28
|
-
|
|
29
27
|
function base() {
|
|
30
28
|
return translator.apply(void 0, arguments);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
base.rich = function (key,
|
|
35
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
29
|
+
}
|
|
30
|
+
// Augment `t.rich` to return plain strings
|
|
31
|
+
base.rich = function (key, /** Key value pairs for values to interpolate into the message. */
|
|
36
32
|
values, formats) {
|
|
37
33
|
// `chunks` is returned as a string when no React element
|
|
38
34
|
// is used, therefore it's safe to cast this type.
|
|
39
|
-
var result = originalRich(key, values, formats);
|
|
40
|
-
|
|
35
|
+
var result = originalRich(key, values, formats);
|
|
36
|
+
// When only string chunks are provided to the parser, only strings should be returned here.
|
|
41
37
|
if (typeof result !== 'string') {
|
|
42
38
|
var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== 'production' ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
|
|
43
39
|
onError(error);
|
|
@@ -47,10 +43,8 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
47
43
|
namespace: namespace
|
|
48
44
|
});
|
|
49
45
|
}
|
|
50
|
-
|
|
51
46
|
return result;
|
|
52
47
|
};
|
|
53
|
-
|
|
54
48
|
base.raw = translator.raw;
|
|
55
49
|
return base;
|
|
56
50
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/
|
|
1
|
+
{"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/defaults.tsx"],"sourcesContent":["import IntlError from './IntlError';\n\n/**\n * Contains defaults that are used for all entry points into the core.\n * See also `InitializedIntlConfiguration`.\n */\n\nexport function defaultGetMessageFallback(props: {\n error: IntlError;\n key: string;\n namespace?: string;\n}) {\n return [props.namespace, props.key].filter((part) => part != null).join('.');\n}\n\nexport function defaultOnError(error: IntlError) {\n console.error(error);\n}\n"],"names":["defaultGetMessageFallback","props","namespace","key","filter","part","join","defaultOnError","error","console"],"mappings":"AAEA;;;AAGG;AAEG,SAAUA,yBAAyBA,CAACC,KAIzC,EAAA;AACC,EAAA,OAAO,CAACA,KAAK,CAACC,SAAS,EAAED,KAAK,CAACE,GAAG,CAAC,CAACC,MAAM,CAAC,UAACC,IAAI,EAAA;IAAA,OAAKA,IAAI,IAAI,IAAI,CAAA;AAAA,GAAA,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC9E,CAAA;AAEM,SAAUC,cAAcA,CAACC,KAAgB,EAAA;AAC7CC,EAAAA,OAAO,CAACD,KAAK,CAACA,KAAK,CAAC,CAAA;AACtB;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/
|
|
1
|
+
{"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/createTranslatorImpl.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport {InitializedIntlConfig} from './IntlConfig';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport {RichTranslationValues, TranslationValue} from './TranslationValues';\nimport createBaseTranslator, {getMessagesOrError} from './createBaseTranslator';\nimport resolveNamespace from './resolveNamespace';\nimport NestedKeyOf from './utils/NestedKeyOf';\n\nexport type CoreRichTranslationValues = Record<\n string,\n TranslationValue | ((chunks: string) => string)\n>;\n\nexport type CreateTranslatorImplProps<Messages> = Omit<\n InitializedIntlConfig,\n 'messages'\n> & {\n namespace: string;\n messages: Messages;\n};\n\nexport default function createTranslatorImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(\n {\n getMessageFallback,\n messages,\n namespace,\n onError,\n ...rest\n }: CreateTranslatorImplProps<Messages>,\n namespacePrefix: string\n) {\n // The `namespacePrefix` is part of the type system.\n // See the comment in the function invocation.\n messages = messages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const translator = createBaseTranslator<Messages, NestedKey>({\n ...rest,\n onError,\n getMessageFallback,\n messagesOrError: getMessagesOrError({\n messages,\n namespace,\n onError\n }) as Messages | IntlError\n });\n\n const originalRich = translator.rich;\n\n function base(...args: Parameters<typeof translator>) {\n return translator(...args);\n }\n\n // Augment `t.rich` to return plain strings\n base.rich = (\n key: Parameters<typeof originalRich>[0],\n /** Key value pairs for values to interpolate into the message. */\n values: CoreRichTranslationValues,\n formats?: Parameters<typeof originalRich>[2]\n ): string => {\n // `chunks` is returned as a string when no React element\n // is used, therefore it's safe to cast this type.\n const result = originalRich(key, values as RichTranslationValues, formats);\n\n // When only string chunks are provided to the parser, only strings should be returned here.\n if (typeof result !== 'string') {\n const error = new IntlError(\n IntlErrorCode.FORMATTING_ERROR,\n process.env.NODE_ENV !== 'production'\n ? \"`createTranslator` only accepts functions for rich text formatting that receive and return strings.\\n\\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})\"\n : undefined\n );\n\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n return result;\n };\n\n base.raw = translator.raw;\n\n return base;\n}\n"],"names":["createTranslatorImpl","_ref","namespacePrefix","getMessageFallback","messages","namespace","onError","rest","_objectWithoutPropertiesLoose","_excluded","resolveNamespace","translator","createBaseTranslator","_extends","messagesOrError","getMessagesOrError","originalRich","rich","base","apply","arguments","key","values","formats","result","error","IntlError","IntlErrorCode","FORMATTING_ERROR","process","env","NODE_ENV","undefined","raw"],"mappings":";;;;;;AAqBc,SAAUA,oBAAoBA,CAAAC,IAAA,EAW1CC,eAAuB,EAAA;AAAA,EAAA,IANrBC,kBAAkB,GAAAF,IAAA,CAAlBE,kBAAkB;IAClBC,QAAQ,GAAAH,IAAA,CAARG,QAAQ;IACRC,SAAS,GAAAJ,IAAA,CAATI,SAAS;IACTC,OAAO,GAAAL,IAAA,CAAPK,OAAO;AACJC,IAAAA,IAAI,GAAAC,6BAAA,CAAAP,IAAA,EAAAQ,SAAA,CAAA,CAAA;AAIT;AACA;AACAL,EAAAA,QAAQ,GAAGA,QAAQ,CAACF,eAAe,CAAa,CAAA;AAChDG,EAAAA,SAAS,GAAGK,gBAAgB,CAACL,SAAS,EAAEH,eAAe,CAAc,CAAA;AAErE,EAAA,IAAMS,UAAU,GAAGC,oBAAoB,CAAAC,QAAA,KAClCN,IAAI,EAAA;AACPD,IAAAA,OAAO,EAAPA,OAAO;AACPH,IAAAA,kBAAkB,EAAlBA,kBAAkB;IAClBW,eAAe,EAAEC,kBAAkB,CAAC;AAClCX,MAAAA,QAAQ,EAARA,QAAQ;AACRC,MAAAA,SAAS,EAATA,SAAS;AACTC,MAAAA,OAAO,EAAPA,OAAAA;KACD,CAAA;AAAyB,GAAA,CAC3B,CAAC,CAAA;AAEF,EAAA,IAAMU,YAAY,GAAGL,UAAU,CAACM,IAAI,CAAA;EAEpC,SAASC,IAAIA,GAAuC;AAClD,IAAA,OAAOP,UAAU,CAAAQ,KAAA,CAAA,KAAA,CAAA,EAAAC,SAAQ,CAAC,CAAA;AAC5B,GAAA;AAEA;AACAF,EAAAA,IAAI,CAACD,IAAI,GAAG,UACVI,GAAuC;EAEvCC,MAAiC,EACjCC,OAA4C,EAClC;AACV;AACA;IACA,IAAMC,MAAM,GAAGR,YAAY,CAACK,GAAG,EAAEC,MAA+B,EAAEC,OAAO,CAAC,CAAA;AAE1E;AACA,IAAA,IAAI,OAAOC,MAAM,KAAK,QAAQ,EAAE;MAC9B,IAAMC,KAAK,GAAG,IAAIC,SAAS,CACzBC,aAAa,CAACC,gBAAgB,EAC9BC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,GACjC,iKAAiK,GACjKC,SAAS,CACd,CAAA;MAED1B,OAAO,CAACmB,KAAK,CAAC,CAAA;AACd,MAAA,OAAOtB,kBAAkB,CAAC;AAACsB,QAAAA,KAAK,EAALA,KAAK;AAAEJ,QAAAA,GAAG,EAAHA,GAAG;AAAEhB,QAAAA,SAAS,EAATA,SAAAA;AAAU,OAAA,CAAC,CAAA;AACnD,KAAA;AAED,IAAA,OAAOmB,MAAM,CAAA;GACd,CAAA;AAEDN,EAAAA,IAAI,CAACe,GAAG,GAAGtB,UAAU,CAACsB,GAAG,CAAA;AAEzB,EAAA,OAAOf,IAAI,CAAA;AACb;;;;"}
|
|
@@ -9,29 +9,24 @@ function resolvePath(messages, key, namespace) {
|
|
|
9
9
|
if (!messages) {
|
|
10
10
|
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages available at `" + namespace + "`." : undefined);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
12
|
var message = messages;
|
|
14
13
|
key.split('.').forEach(function (part) {
|
|
15
14
|
var next = message[part];
|
|
16
|
-
|
|
17
15
|
if (part == null || next == null) {
|
|
18
16
|
throw new Error(process.env.NODE_ENV !== 'production' ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
message = next;
|
|
22
19
|
});
|
|
23
20
|
return message;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
function prepareTranslationValues(values) {
|
|
27
|
-
if (Object.keys(values).length === 0) return undefined;
|
|
28
|
-
|
|
23
|
+
if (Object.keys(values).length === 0) return undefined;
|
|
24
|
+
// Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
29
25
|
var transformedValues = {};
|
|
30
26
|
Object.keys(values).forEach(function (key) {
|
|
31
27
|
var index = 0;
|
|
32
28
|
var value = values[key];
|
|
33
29
|
var transformed;
|
|
34
|
-
|
|
35
30
|
if (typeof value === 'function') {
|
|
36
31
|
transformed = function transformed(chunks) {
|
|
37
32
|
var result = value(chunks);
|
|
@@ -42,29 +37,23 @@ function prepareTranslationValues(values) {
|
|
|
42
37
|
} else {
|
|
43
38
|
transformed = value;
|
|
44
39
|
}
|
|
45
|
-
|
|
46
40
|
transformedValues[key] = transformed;
|
|
47
41
|
});
|
|
48
42
|
return transformedValues;
|
|
49
43
|
}
|
|
50
|
-
|
|
51
44
|
function getMessagesOrError(_ref) {
|
|
52
45
|
var messages = _ref.messages,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
namespace = _ref.namespace,
|
|
47
|
+
_ref$onError = _ref.onError,
|
|
48
|
+
onError = _ref$onError === void 0 ? defaultOnError : _ref$onError;
|
|
57
49
|
try {
|
|
58
50
|
if (!messages) {
|
|
59
51
|
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages were configured on the provider." : undefined);
|
|
60
52
|
}
|
|
61
|
-
|
|
62
53
|
var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
|
|
63
|
-
|
|
64
54
|
if (!retrievedMessages) {
|
|
65
55
|
throw new Error(process.env.NODE_ENV !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
|
|
66
56
|
}
|
|
67
|
-
|
|
68
57
|
return retrievedMessages;
|
|
69
58
|
} catch (error) {
|
|
70
59
|
var intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
@@ -74,16 +63,15 @@ function getMessagesOrError(_ref) {
|
|
|
74
63
|
}
|
|
75
64
|
function createBaseTranslator(_ref2) {
|
|
76
65
|
var cachedFormatsByLocale = _ref2.cachedFormatsByLocale,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
66
|
+
defaultTranslationValues = _ref2.defaultTranslationValues,
|
|
67
|
+
globalFormats = _ref2.formats,
|
|
68
|
+
_ref2$getMessageFallb = _ref2.getMessageFallback,
|
|
69
|
+
getMessageFallback = _ref2$getMessageFallb === void 0 ? defaultGetMessageFallback : _ref2$getMessageFallb,
|
|
70
|
+
locale = _ref2.locale,
|
|
71
|
+
messagesOrError = _ref2.messagesOrError,
|
|
72
|
+
namespace = _ref2.namespace,
|
|
73
|
+
onError = _ref2.onError,
|
|
74
|
+
timeZone = _ref2.timeZone;
|
|
87
75
|
function getFallbackFromErrorAndNotify(key, code, message) {
|
|
88
76
|
var error = new IntlError(code, message);
|
|
89
77
|
onError(error);
|
|
@@ -93,16 +81,11 @@ function createBaseTranslator(_ref2) {
|
|
|
93
81
|
namespace: namespace
|
|
94
82
|
});
|
|
95
83
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
key,
|
|
100
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
101
|
-
values,
|
|
102
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
84
|
+
function translateBaseFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
85
|
+
key, /** Key value pairs for values to interpolate into the message. */
|
|
86
|
+
values, /** Provide custom formats for numbers, dates and times. */
|
|
103
87
|
formats) {
|
|
104
88
|
var _cachedFormatsByLocal;
|
|
105
|
-
|
|
106
89
|
if (messagesOrError instanceof IntlError) {
|
|
107
90
|
// We have already warned about this during render
|
|
108
91
|
return getMessageFallback({
|
|
@@ -111,102 +94,80 @@ function createBaseTranslator(_ref2) {
|
|
|
111
94
|
namespace: namespace
|
|
112
95
|
});
|
|
113
96
|
}
|
|
114
|
-
|
|
115
97
|
var messages = messagesOrError;
|
|
116
98
|
var message;
|
|
117
|
-
|
|
118
99
|
try {
|
|
119
100
|
message = resolvePath(messages, key, namespace);
|
|
120
101
|
} catch (error) {
|
|
121
102
|
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
122
103
|
}
|
|
123
|
-
|
|
124
104
|
function joinPath(parts) {
|
|
125
105
|
return parts.filter(function (part) {
|
|
126
106
|
return part != null;
|
|
127
107
|
}).join('.');
|
|
128
108
|
}
|
|
129
|
-
|
|
130
109
|
var cacheKey = joinPath([namespace, key, String(message)]);
|
|
131
110
|
var messageFormat;
|
|
132
|
-
|
|
133
111
|
if (cachedFormatsByLocale != null && (_cachedFormatsByLocal = cachedFormatsByLocale[locale]) != null && _cachedFormatsByLocal[cacheKey]) {
|
|
134
112
|
messageFormat = cachedFormatsByLocale == null ? void 0 : cachedFormatsByLocale[locale][cacheKey];
|
|
135
113
|
} else {
|
|
136
114
|
if (typeof message === 'object') {
|
|
137
115
|
var code, errorMessage;
|
|
138
|
-
|
|
139
116
|
if (Array.isArray(message)) {
|
|
140
117
|
code = IntlErrorCode.INVALID_MESSAGE;
|
|
141
|
-
|
|
142
118
|
if (process.env.NODE_ENV !== 'production') {
|
|
143
119
|
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
120
|
}
|
|
145
121
|
} else {
|
|
146
122
|
code = IntlErrorCode.INSUFFICIENT_PATH;
|
|
147
|
-
|
|
148
123
|
if (process.env.NODE_ENV !== 'production') {
|
|
149
124
|
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
125
|
}
|
|
151
126
|
}
|
|
152
|
-
|
|
153
127
|
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
154
128
|
}
|
|
155
|
-
|
|
156
129
|
try {
|
|
157
130
|
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats), timeZone));
|
|
158
131
|
} catch (error) {
|
|
159
132
|
return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
|
|
160
133
|
}
|
|
161
|
-
|
|
162
134
|
if (cachedFormatsByLocale) {
|
|
163
135
|
if (!cachedFormatsByLocale[locale]) {
|
|
164
136
|
cachedFormatsByLocale[locale] = {};
|
|
165
137
|
}
|
|
166
|
-
|
|
167
138
|
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
168
139
|
}
|
|
169
140
|
}
|
|
170
|
-
|
|
171
141
|
try {
|
|
172
|
-
var formattedMessage = messageFormat.format(
|
|
142
|
+
var formattedMessage = messageFormat.format(
|
|
143
|
+
// @ts-ignore `intl-messageformat` expects a different format
|
|
173
144
|
// for rich text elements since a recent minor update. This
|
|
174
145
|
// needs to be evaluated in detail, possibly also in regards
|
|
175
146
|
// to be able to format to parts.
|
|
176
147
|
prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
|
|
177
|
-
|
|
178
148
|
if (formattedMessage == null) {
|
|
179
149
|
throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
150
|
+
}
|
|
151
|
+
// Limit the function signature to return strings or React elements
|
|
152
|
+
return isValidElement(formattedMessage) ||
|
|
153
|
+
// Arrays of React elements
|
|
184
154
|
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
185
155
|
} catch (error) {
|
|
186
156
|
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
|
|
187
157
|
}
|
|
188
158
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
key,
|
|
193
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
194
|
-
values,
|
|
195
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
159
|
+
function translateFn( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
160
|
+
key, /** Key value pairs for values to interpolate into the message. */
|
|
161
|
+
values, /** Provide custom formats for numbers, dates and times. */
|
|
196
162
|
formats) {
|
|
197
163
|
var result = translateBaseFn(key, values, formats);
|
|
198
|
-
|
|
199
164
|
if (typeof result !== 'string') {
|
|
200
165
|
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
166
|
}
|
|
202
|
-
|
|
203
167
|
return result;
|
|
204
168
|
}
|
|
205
|
-
|
|
206
169
|
translateFn.rich = translateBaseFn;
|
|
207
|
-
|
|
208
|
-
translateFn.raw = function (
|
|
209
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
170
|
+
translateFn.raw = function ( /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
210
171
|
key) {
|
|
211
172
|
if (messagesOrError instanceof IntlError) {
|
|
212
173
|
// We have already warned about this during render
|
|
@@ -216,16 +177,13 @@ function createBaseTranslator(_ref2) {
|
|
|
216
177
|
namespace: namespace
|
|
217
178
|
});
|
|
218
179
|
}
|
|
219
|
-
|
|
220
180
|
var messages = messagesOrError;
|
|
221
|
-
|
|
222
181
|
try {
|
|
223
182
|
return resolvePath(messages, key, namespace);
|
|
224
183
|
} catch (error) {
|
|
225
184
|
return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
|
|
226
185
|
}
|
|
227
186
|
};
|
|
228
|
-
|
|
229
187
|
return translateFn;
|
|
230
188
|
}
|
|
231
189
|
|
|
@@ -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 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,IAA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AED,IAAA,OAAOgB,MAAP,CAAA;AACD,GAAA;;AAED0C,EAAAA,WAAW,CAACC,IAAZ,GAAmBnB,eAAnB,CAAA;;AAEAkB,EAAAA,WAAW,CAACE,GAAZ,GAAkB;AAChB;AACAlE,EAAAA,GAFgB,EAGT;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,OAAOyD,WAAP,CAAA;AACD;;;;"}
|
|
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 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","_ref","_ref$onError","onError","defaultOnError","retrievedMessages","error","intlError","IntlError","IntlErrorCode","MISSING_MESSAGE","createBaseTranslator","_ref2","cachedFormatsByLocale","defaultTranslationValues","globalFormats","formats","_ref2$getMessageFallb","getMessageFallback","defaultGetMessageFallback","locale","messagesOrError","timeZone","getFallbackFromErrorAndNotify","code","translateBaseFn","_cachedFormatsByLocal","joinPath","parts","filter","join","cacheKey","String","messageFormat","errorMessage","Array","isArray","INVALID_MESSAGE","INSUFFICIENT_PATH","IntlMessageFormat","convertFormatsToIntlMessageFormat","_extends","formattedMessage","format","FORMATTING_ERROR","translateFn","rich","raw"],"mappings":";;;;;;;AAoBA,SAASA,WAAWA,C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G,CAACC,QAAQ,KAAK,YAAY,GAChBL,eAAAA,GAAAA,GAAG,GAClBC,OAAAA,IAAAA,SAAS,GAAkBA,aAAAA,GAAAA,SAAS,GAAO,GAAA,GAAA,UAC7C,CACAK,GAAAA,qFAAAA,GAAAA,SAAS,CACd,CAAA;AACF,KAAA;AAED,IAAA,OAAOgB,MAAM,CAAA;AACf,GAAA;EAEAgD,WAAW,CAACC,IAAI,GAAGrB,eAAe,CAAA;EAElCoB,WAAW,CAACE,GAAG,GAAG;AAEhBxE,EAAAA,GAAW,EACJ;IACP,IAAI8C,eAAe,YAAYb,SAAS,EAAE;AACxC;AACA,MAAA,OAAOU,kBAAkB,CAAC;AACxBZ,QAAAA,KAAK,EAAEe,eAAe;AACtB9C,QAAAA,GAAG,EAAHA,GAAG;AACHC,QAAAA,SAAS,EAATA,SAAAA;AACD,OAAA,CAAC,CAAA;AACH,KAAA;IACD,IAAMF,QAAQ,GAAG+C,eAAe,CAAA;IAEhC,IAAI;AACF,MAAA,OAAOhD,WAAW,CAACC,QAAQ,EAAEC,GAAG,EAAEC,SAAS,CAAC,CAAA;KAC7C,CAAC,OAAO8B,KAAK,EAAE;MACd,OAAOiB,6BAA6B,CAClChD,GAAG,EACHkC,aAAa,CAACC,eAAe,EAC5BJ,KAAe,CAACxB,OAAO,CACzB,CAAA;AACF,KAAA;GACF,CAAA;AAED,EAAA,OAAO+D,WAAW,CAAA;AACpB;;;;"}
|
|
@@ -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,
|
|
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,gBAAgBA,CACtCC,SAAiB,EACjBC,eAAuB,EAAA;AAEvB,EAAA,OAAOD,SAAS,KAAKC,eAAe,GAChCC,SAAS,GACTF,SAAS,CAACG,KAAK,CAAC,CAACF,eAAe,GAAG,GAAG,EAAEG,MAAM,CAAC,CAAA;AACrD;;;;"}
|
|
@@ -3,14 +3,12 @@ import IntlError, { IntlErrorCode } from './use-intl.esm.js';
|
|
|
3
3
|
function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
4
4
|
Object.entries(messages).forEach(function (_ref) {
|
|
5
5
|
var key = _ref[0],
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
messageOrMessages = _ref[1];
|
|
8
7
|
if (key.includes('.')) {
|
|
9
8
|
var keyLabel = key;
|
|
10
9
|
if (parentPath) keyLabel += " (at " + parentPath + ")";
|
|
11
10
|
invalidKeyLabels.push(keyLabel);
|
|
12
11
|
}
|
|
13
|
-
|
|
14
12
|
if (messageOrMessages != null && typeof messageOrMessages === 'object') {
|
|
15
13
|
validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
|
|
16
14
|
return part != null;
|
|
@@ -18,11 +16,9 @@ function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
|
|
|
18
16
|
}
|
|
19
17
|
});
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
function validateMessages(messages, onError) {
|
|
23
20
|
var invalidKeyLabels = [];
|
|
24
21
|
validateMessagesSegment(messages, invalidKeyLabels);
|
|
25
|
-
|
|
26
22
|
if (invalidKeyLabels.length > 0) {
|
|
27
23
|
onError(new IntlError(IntlErrorCode.INVALID_KEY, process.env.NODE_ENV !== 'production' ? "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ') + "\n\nIf you're migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from \"lodash\";\n\nconst input = {\n \"one.one\": \"1.1\",\n \"one.two\": \"1.2\",\n \"two.one.one\": \"2.1.1\"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// \"one\": {\n// \"one\": \"1.1\",\n// \"two\": \"1.2\"\n// },\n// \"two\": {\n// \"one\": {\n// \"one\": \"2.1.1\"\n// }\n// }\n// }\n" : undefined));
|
|
28
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm9.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n process.env.NODE_ENV !== 'production'\n ? `Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}\n\nIf you're migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from \"lodash\";\n\nconst input = {\n \"one.one\": \"1.1\",\n \"one.two\": \"1.2\",\n \"two.one.one\": \"2.1.1\"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// \"one\": {\n// \"one\": \"1.1\",\n// \"two\": \"1.2\"\n// },\n// \"two\": {\n// \"one\": {\n// \"one\": \"2.1.1\"\n// }\n// }\n// }\n`\n : undefined\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY","process","env","NODE_ENV","undefined"],"mappings":";;AAGA,SAASA,
|
|
1
|
+
{"version":3,"file":"use-intl.esm9.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n process.env.NODE_ENV !== 'production'\n ? `Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}\n\nIf you're migrating from a flat structure, you can convert your messages as follows:\n\nimport {set} from \"lodash\";\n\nconst input = {\n \"one.one\": \"1.1\",\n \"one.two\": \"1.2\",\n \"two.one.one\": \"2.1.1\"\n};\n\nconst output = Object.entries(input).reduce(\n (acc, [key, value]) => set(acc, key, value),\n {}\n);\n\n// Output:\n//\n// {\n// \"one\": {\n// \"one\": \"1.1\",\n// \"two\": \"1.2\"\n// },\n// \"two\": {\n// \"one\": {\n// \"one\": \"2.1.1\"\n// }\n// }\n// }\n`\n : undefined\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","_ref","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY","process","env","NODE_ENV","undefined"],"mappings":";;AAGA,SAASA,uBAAuBA,CAC9BC,QAA8B,EAC9BC,gBAA+B,EAC/BC,UAAmB,EAAA;EAEnBC,MAAM,CAACC,OAAO,CAACJ,QAAQ,CAAC,CAACK,OAAO,CAAC,UAAAC,IAAA,EAA6B;IAAA,IAA3BC,GAAG,GAAAD,IAAA,CAAA,CAAA,CAAA;AAAEE,MAAAA,iBAAiB,GAAAF,IAAA,CAAA,CAAA,CAAA,CAAA;AACvD,IAAA,IAAIC,GAAG,CAACE,QAAQ,CAAC,GAAG,CAAC,EAAE;MACrB,IAAIC,QAAQ,GAAGH,GAAG,CAAA;AAClB,MAAA,IAAIL,UAAU,EAAEQ,QAAQ,IAAA,OAAA,GAAYR,UAAU,GAAG,GAAA,CAAA;AACjDD,MAAAA,gBAAgB,CAACU,IAAI,CAACD,QAAQ,CAAC,CAAA;AAChC,KAAA;IAED,IAAIF,iBAAiB,IAAI,IAAI,IAAI,OAAOA,iBAAiB,KAAK,QAAQ,EAAE;AACtET,MAAAA,uBAAuB,CACrBS,iBAAiB,EACjBP,gBAAgB,EAChB,CAACC,UAAU,EAAEK,GAAG,CAAC,CAACK,MAAM,CAAC,UAACC,IAAI,EAAA;QAAA,OAAKA,IAAI,IAAI,IAAI,CAAA;AAAA,OAAA,CAAC,CAACC,IAAI,CAAC,GAAG,CAAC,CAC3D,CAAA;AACF,KAAA;AACH,GAAC,CAAC,CAAA;AACJ,CAAA;AAEc,SAAUC,gBAAgBA,CACtCf,QAA8B,EAC9BgB,OAAmC,EAAA;EAEnC,IAAMf,gBAAgB,GAAkB,EAAE,CAAA;AAC1CF,EAAAA,uBAAuB,CAACC,QAAQ,EAAEC,gBAAgB,CAAC,CAAA;AAEnD,EAAA,IAAIA,gBAAgB,CAACgB,MAAM,GAAG,CAAC,EAAE;AAC/BD,IAAAA,OAAO,CACL,IAAIE,SAAS,CACXC,aAAa,CAACC,WAAW,EACzBC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,GAI/BtB,2JAAAA,IAAAA,gBAAgB,CAACgB,MAAM,KAAK,CAAC,GAAG,KAAK,GAAG,MAC1C,CAAKhB,GAAAA,IAAAA,GAAAA,gBAAgB,CAACa,IAAI,CAAC,IAAI,CAAC,GA+BhCU,4gBAAAA,GAAAA,SAAS,CACd,CACF,CAAA;AACF,GAAA;AACH;;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import NestedValueOf from './NestedValueOf';
|
|
2
|
-
|
|
2
|
+
type MessageKeys<ObjectType, Keys extends string> = {
|
|
3
3
|
[Property in Keys]: NestedValueOf<ObjectType, Property> extends string ? Property : never;
|
|
4
4
|
}[Keys];
|
|
5
5
|
export default MessageKeys;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import NestedValueOf from './NestedValueOf';
|
|
2
|
-
|
|
2
|
+
type NamespaceKeys<ObjectType, Keys extends string> = {
|
|
3
3
|
[Property in Keys]: NestedValueOf<ObjectType, Property> extends string ? never : Property;
|
|
4
4
|
}[Keys];
|
|
5
5
|
export default NamespaceKeys;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
type NestedKeyOf<ObjectType> = ObjectType extends object ? {
|
|
2
2
|
[Key in keyof ObjectType]: `${Key & string}` | `${Key & string}.${NestedKeyOf<ObjectType[Key]>}`;
|
|
3
3
|
}[keyof ObjectType] : never;
|
|
4
4
|
export default NestedKeyOf;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
type NestedValueOf<ObjectType, Property extends string> = Property extends `${infer Key}.${infer Rest}` ? Key extends keyof ObjectType ? NestedValueOf<ObjectType[Key], Rest> : never : Property extends keyof ObjectType ? ObjectType[Property] : never;
|
|
2
2
|
export default NestedValueOf;
|
|
@@ -6,8 +6,7 @@ import getInitializedConfig from './use-intl.esm9.js';
|
|
|
6
6
|
var _excluded = ["children"];
|
|
7
7
|
function IntlProvider(_ref) {
|
|
8
8
|
var children = _ref.children,
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
11
10
|
return React.createElement(IntlContext.Provider, {
|
|
12
11
|
value: getInitializedConfig(props)
|
|
13
12
|
}, children);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm.js","sources":["../../src/react/useTranslations.tsx"],"sourcesContent":["import {ReactElement, ReactNodeArray} from 'react';\nimport Formats from '../core/Formats';\nimport TranslationValues, {\n RichTranslationValues\n} from '../core/TranslationValues';\nimport MessageKeys from '../core/utils/MessageKeys';\nimport NamespaceKeys from '../core/utils/NamespaceKeys';\nimport NestedKeyOf from '../core/utils/NestedKeyOf';\nimport NestedValueOf from '../core/utils/NestedValueOf';\nimport useIntlContext from './useIntlContext';\nimport useTranslationsImpl from './useTranslationsImpl';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function useTranslations<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>(\n namespace?: NestedKey\n): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: RichTranslationValues,\n formats?: Partial<Formats>\n ): string | ReactElement | ReactNodeArray;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n const context = useIntlContext();\n const messages = context.messages as IntlMessages;\n\n // We have to wrap the actual hook so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return useTranslationsImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {'!': messages},\n // @ts-ignore\n namespace ? `!.${namespace}` : '!',\n '!'\n );\n}\n"],"names":["useTranslations","namespace","context","useIntlContext","messages","useTranslationsImpl"],"mappings":";;;AAYA;;;;;;;AAOG;AACqB,SAAAA,eAAeA,CAMrCC,SAAqB,EAAA;AA6DrB,EAAA,IAAMC,OAAO,GAAGC,cAAc,EAAE,CAAA;AAChC,EAAA,IAAMC,QAAQ,GAAGF,OAAO,CAACE,QAAwB,CAAA;AAEjD;AACA;AACA;AACA,EAAA,OAAOC,mBAAmB,CAIxB;AAAC,IAAA,GAAG,EAAED,QAAAA;GAAS;AACf;AACAH,EAAAA,SAAS,UAAQA,SAAS,GAAK,GAAG,EAClC,GAAG,CACJ,CAAA;AACH;;;;"}
|
|
@@ -3,11 +3,9 @@ import IntlContext from './use-intl.esm8.js';
|
|
|
3
3
|
|
|
4
4
|
function useIntlContext() {
|
|
5
5
|
var context = useContext(IntlContext);
|
|
6
|
-
|
|
7
6
|
if (!context) {
|
|
8
7
|
throw new Error(process.env.NODE_ENV !== 'production' ? 'No intl context found. Have you configured the provider?' : undefined);
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
return context;
|
|
12
10
|
}
|
|
13
11
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-intl.esm10.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"use-intl.esm10.js","sources":["../../src/react/IntlContext.tsx"],"sourcesContent":["import {createContext} from 'react';\nimport {InitializedIntlConfig} from '../core/IntlConfig';\n\nconst IntlContext = createContext<InitializedIntlConfig | undefined>(undefined);\n\nexport default IntlContext;\n"],"names":["IntlContext","createContext","undefined"],"mappings":";;AAGA,IAAMA,WAAW,gBAAGC,aAAa,CAAoCC,SAAS;;;;"}
|
|
@@ -5,15 +5,14 @@ import useIntlContext from './use-intl.esm10.js';
|
|
|
5
5
|
|
|
6
6
|
function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
|
|
7
7
|
var _useIntlContext = useIntlContext(),
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
defaultTranslationValues = _useIntlContext.defaultTranslationValues,
|
|
9
|
+
globalFormats = _useIntlContext.formats,
|
|
10
|
+
getMessageFallback = _useIntlContext.getMessageFallback,
|
|
11
|
+
locale = _useIntlContext.locale,
|
|
12
|
+
onError = _useIntlContext.onError,
|
|
13
|
+
timeZone = _useIntlContext.timeZone;
|
|
14
|
+
// The `namespacePrefix` is part of the type system.
|
|
14
15
|
// See the comment in the hook invocation.
|
|
15
|
-
|
|
16
|
-
|
|
17
16
|
allMessages = allMessages[namespacePrefix];
|
|
18
17
|
namespace = resolveNamespace(namespace, namespacePrefix);
|
|
19
18
|
var cachedFormatsByLocaleRef = useRef({});
|