use-intl 2.4.1 → 2.7.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/README.md +1 -0
- package/dist/AbstractIntlMessages.d.ts +9 -0
- package/dist/Formats.d.ts +2 -1
- package/dist/IntlContext.d.ts +2 -2
- package/dist/IntlError.d.ts +1 -0
- package/dist/IntlMessages.d.ts +2 -4
- package/dist/IntlProvider.d.ts +3 -3
- package/dist/NumberFormatOptions.d.ts +1 -3
- package/dist/index.d.ts +1 -1
- package/dist/use-intl.cjs.development.js +137 -63
- package/dist/use-intl.cjs.development.js.map +1 -1
- package/dist/use-intl.cjs.production.min.js +1 -1
- package/dist/use-intl.cjs.production.min.js.map +1 -1
- package/dist/use-intl.esm.js +127 -56
- package/dist/use-intl.esm.js.map +1 -1
- package/dist/useIntl.d.ts +2 -1
- package/dist/useTranslations.d.ts +22 -5
- package/dist/useTranslationsImpl.d.ts +12 -0
- package/dist/utils/MessageKeys.d.ts +5 -0
- package/dist/utils/NamespaceKeys.d.ts +5 -0
- package/dist/utils/NestedKeyOf.d.ts +4 -0
- package/dist/utils/NestedValueOf.d.ts +2 -0
- package/dist/validateMessages.d.ts +3 -0
- package/package.json +17 -13
- package/src/AbstractIntlMessages.tsx +10 -0
- package/src/Formats.tsx +2 -1
- package/src/IntlContext.tsx +2 -2
- package/src/IntlError.tsx +1 -0
- package/src/IntlMessages.tsx +4 -2
- package/src/IntlProvider.tsx +15 -4
- package/src/NumberFormatOptions.tsx +1 -3
- package/src/index.tsx +1 -1
- package/src/useIntl.tsx +2 -1
- package/src/useTranslations.tsx +116 -298
- package/src/useTranslationsImpl.tsx +328 -0
- package/src/utils/MessageKeys.tsx +9 -0
- package/src/utils/NamespaceKeys.tsx +9 -0
- package/src/utils/NestedKeyOf.tsx +9 -0
- package/src/utils/NestedValueOf.tsx +12 -0
- package/src/validateMessages.tsx +42 -0
package/src/useTranslations.tsx
CHANGED
|
@@ -1,79 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
cloneElement,
|
|
4
|
-
isValidElement,
|
|
5
|
-
ReactElement,
|
|
6
|
-
ReactNode,
|
|
7
|
-
ReactNodeArray,
|
|
8
|
-
useMemo,
|
|
9
|
-
useRef
|
|
10
|
-
} from 'react';
|
|
1
|
+
import {ReactElement, ReactNodeArray} from 'react';
|
|
11
2
|
import Formats from './Formats';
|
|
12
3
|
import IntlError, {IntlErrorCode} from './IntlError';
|
|
13
|
-
import IntlMessages from './IntlMessages';
|
|
14
4
|
import TranslationValues, {RichTranslationValues} from './TranslationValues';
|
|
15
|
-
import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
|
|
16
5
|
import useIntlContext from './useIntlContext';
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
) {
|
|
23
|
-
if (!messages) {
|
|
24
|
-
throw new Error(
|
|
25
|
-
__DEV__ ? `No messages available at \`${namespace}\`.` : undefined
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let message = messages;
|
|
30
|
-
|
|
31
|
-
idPath.split('.').forEach((part) => {
|
|
32
|
-
const next = (message as any)[part];
|
|
33
|
-
|
|
34
|
-
if (part == null || next == null) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
__DEV__
|
|
37
|
-
? `Could not resolve \`${idPath}\` in ${
|
|
38
|
-
namespace ? `\`${namespace}\`` : 'messages'
|
|
39
|
-
}.`
|
|
40
|
-
: undefined
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
message = next;
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
return message;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function prepareTranslationValues(values: RichTranslationValues) {
|
|
51
|
-
if (Object.keys(values).length === 0) return undefined;
|
|
52
|
-
|
|
53
|
-
// Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
54
|
-
const transformedValues: RichTranslationValues = {};
|
|
55
|
-
Object.keys(values).forEach((key) => {
|
|
56
|
-
let index = 0;
|
|
57
|
-
const value = values[key];
|
|
58
|
-
|
|
59
|
-
let transformed;
|
|
60
|
-
if (typeof value === 'function') {
|
|
61
|
-
transformed = (children: ReactNode) => {
|
|
62
|
-
const result = value(children);
|
|
63
|
-
|
|
64
|
-
return isValidElement(result)
|
|
65
|
-
? cloneElement(result, {key: key + index++})
|
|
66
|
-
: result;
|
|
67
|
-
};
|
|
68
|
-
} else {
|
|
69
|
-
transformed = value;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
transformedValues[key] = transformed;
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
return transformedValues;
|
|
76
|
-
}
|
|
6
|
+
import useTranslationsImpl from './useTranslationsImpl';
|
|
7
|
+
import MessageKeys from './utils/MessageKeys';
|
|
8
|
+
import NamespaceKeys from './utils/NamespaceKeys';
|
|
9
|
+
import NestedKeyOf from './utils/NestedKeyOf';
|
|
10
|
+
import NestedValueOf from './utils/NestedValueOf';
|
|
77
11
|
|
|
78
12
|
/**
|
|
79
13
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
@@ -83,231 +17,115 @@ function prepareTranslationValues(values: RichTranslationValues) {
|
|
|
83
17
|
* The namespace can also indicate nesting by using a dot
|
|
84
18
|
* (e.g. `namespace.Component`).
|
|
85
19
|
*/
|
|
86
|
-
export default function useTranslations
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (typeof message === 'object') {
|
|
183
|
-
return getFallbackFromErrorAndNotify(
|
|
184
|
-
key,
|
|
185
|
-
IntlErrorCode.INSUFFICIENT_PATH,
|
|
186
|
-
__DEV__
|
|
187
|
-
? `Insufficient path specified for \`${key}\` in \`${
|
|
188
|
-
namespace ? `\`${namespace}\`` : 'messages'
|
|
189
|
-
}\`.`
|
|
190
|
-
: undefined
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
try {
|
|
195
|
-
messageFormat = new IntlMessageFormat(
|
|
196
|
-
message,
|
|
197
|
-
locale,
|
|
198
|
-
convertFormatsToIntlMessageFormat(
|
|
199
|
-
{...globalFormats, ...formats},
|
|
200
|
-
timeZone
|
|
201
|
-
)
|
|
202
|
-
);
|
|
203
|
-
} catch (error) {
|
|
204
|
-
return getFallbackFromErrorAndNotify(
|
|
205
|
-
key,
|
|
206
|
-
IntlErrorCode.INVALID_MESSAGE,
|
|
207
|
-
(error as Error).message
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (!cachedFormatsByLocale[locale]) {
|
|
212
|
-
cachedFormatsByLocale[locale] = {};
|
|
213
|
-
}
|
|
214
|
-
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
try {
|
|
218
|
-
const formattedMessage = messageFormat.format(
|
|
219
|
-
prepareTranslationValues({...defaultTranslationValues, ...values})
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
if (formattedMessage == null) {
|
|
223
|
-
throw new Error(
|
|
224
|
-
__DEV__
|
|
225
|
-
? `Unable to format \`${key}\` in ${
|
|
226
|
-
namespace ? `namespace \`${namespace}\`` : 'messages'
|
|
227
|
-
}`
|
|
228
|
-
: undefined
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// Limit the function signature to return strings or React elements
|
|
233
|
-
return isValidElement(formattedMessage) ||
|
|
234
|
-
// Arrays of React elements
|
|
235
|
-
Array.isArray(formattedMessage) ||
|
|
236
|
-
typeof formattedMessage === 'string'
|
|
237
|
-
? formattedMessage
|
|
238
|
-
: String(formattedMessage);
|
|
239
|
-
} catch (error) {
|
|
240
|
-
return getFallbackFromErrorAndNotify(
|
|
241
|
-
key,
|
|
242
|
-
IntlErrorCode.FORMATTING_ERROR,
|
|
243
|
-
(error as Error).message
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function translateFn(
|
|
249
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
250
|
-
key: string,
|
|
251
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
252
|
-
values?: TranslationValues,
|
|
253
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
254
|
-
formats?: Partial<Formats>
|
|
255
|
-
): string {
|
|
256
|
-
const message = translateBaseFn(key, values, formats);
|
|
257
|
-
|
|
258
|
-
if (typeof message !== 'string') {
|
|
259
|
-
return getFallbackFromErrorAndNotify(
|
|
260
|
-
key,
|
|
261
|
-
IntlErrorCode.INVALID_MESSAGE,
|
|
262
|
-
__DEV__
|
|
263
|
-
? `The message \`${key}\` in ${
|
|
264
|
-
namespace ? `namespace \`${namespace}\`` : 'messages'
|
|
265
|
-
} didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
|
|
266
|
-
: undefined
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return message;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
translateFn.rich = translateBaseFn;
|
|
274
|
-
|
|
275
|
-
translateFn.raw = (
|
|
276
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
277
|
-
key: string
|
|
278
|
-
): any => {
|
|
279
|
-
if (messagesOrError instanceof IntlError) {
|
|
280
|
-
// We have already warned about this during render
|
|
281
|
-
return getMessageFallback({
|
|
282
|
-
error: messagesOrError,
|
|
283
|
-
key,
|
|
284
|
-
namespace
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
const messages = messagesOrError;
|
|
288
|
-
|
|
289
|
-
try {
|
|
290
|
-
return resolvePath(messages, key, namespace);
|
|
291
|
-
} catch (error) {
|
|
292
|
-
return getFallbackFromErrorAndNotify(
|
|
293
|
-
key,
|
|
294
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
295
|
-
(error as Error).message
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
return translateFn;
|
|
301
|
-
}, [
|
|
302
|
-
getMessageFallback,
|
|
303
|
-
globalFormats,
|
|
304
|
-
locale,
|
|
305
|
-
messagesOrError,
|
|
306
|
-
namespace,
|
|
307
|
-
onError,
|
|
308
|
-
timeZone,
|
|
309
|
-
defaultTranslationValues
|
|
310
|
-
]);
|
|
20
|
+
export default function useTranslations<
|
|
21
|
+
NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>>
|
|
22
|
+
>(
|
|
23
|
+
namespace?: NestedKey
|
|
24
|
+
): // Explicitly defining the return type is necessary as TypeScript would get it wrong
|
|
25
|
+
{
|
|
26
|
+
// Default invocation
|
|
27
|
+
<
|
|
28
|
+
TargetKey extends MessageKeys<
|
|
29
|
+
NestedValueOf<
|
|
30
|
+
{'!': IntlMessages},
|
|
31
|
+
NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
|
|
32
|
+
? '!'
|
|
33
|
+
: `!.${NestedKey}`
|
|
34
|
+
>,
|
|
35
|
+
NestedKeyOf<
|
|
36
|
+
NestedValueOf<
|
|
37
|
+
{'!': IntlMessages},
|
|
38
|
+
NamespaceKeys<
|
|
39
|
+
IntlMessages,
|
|
40
|
+
NestedKeyOf<IntlMessages>
|
|
41
|
+
> extends NestedKey
|
|
42
|
+
? '!'
|
|
43
|
+
: `!.${NestedKey}`
|
|
44
|
+
>
|
|
45
|
+
>
|
|
46
|
+
>
|
|
47
|
+
>(
|
|
48
|
+
key: TargetKey,
|
|
49
|
+
values?: TranslationValues,
|
|
50
|
+
formats?: Partial<Formats>
|
|
51
|
+
): string;
|
|
52
|
+
|
|
53
|
+
// `rich`
|
|
54
|
+
rich<
|
|
55
|
+
TargetKey extends MessageKeys<
|
|
56
|
+
NestedValueOf<
|
|
57
|
+
{'!': IntlMessages},
|
|
58
|
+
NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
|
|
59
|
+
? '!'
|
|
60
|
+
: `!.${NestedKey}`
|
|
61
|
+
>,
|
|
62
|
+
NestedKeyOf<
|
|
63
|
+
NestedValueOf<
|
|
64
|
+
{'!': IntlMessages},
|
|
65
|
+
NamespaceKeys<
|
|
66
|
+
IntlMessages,
|
|
67
|
+
NestedKeyOf<IntlMessages>
|
|
68
|
+
> extends NestedKey
|
|
69
|
+
? '!'
|
|
70
|
+
: `!.${NestedKey}`
|
|
71
|
+
>
|
|
72
|
+
>
|
|
73
|
+
>
|
|
74
|
+
>(
|
|
75
|
+
key: TargetKey,
|
|
76
|
+
values?: RichTranslationValues,
|
|
77
|
+
formats?: Partial<Formats>
|
|
78
|
+
): string | ReactElement | ReactNodeArray;
|
|
79
|
+
|
|
80
|
+
// `raw`
|
|
81
|
+
raw<
|
|
82
|
+
TargetKey extends MessageKeys<
|
|
83
|
+
NestedValueOf<
|
|
84
|
+
{'!': IntlMessages},
|
|
85
|
+
NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
|
|
86
|
+
? '!'
|
|
87
|
+
: `!.${NestedKey}`
|
|
88
|
+
>,
|
|
89
|
+
NestedKeyOf<
|
|
90
|
+
NestedValueOf<
|
|
91
|
+
{'!': IntlMessages},
|
|
92
|
+
NamespaceKeys<
|
|
93
|
+
IntlMessages,
|
|
94
|
+
NestedKeyOf<IntlMessages>
|
|
95
|
+
> extends NestedKey
|
|
96
|
+
? '!'
|
|
97
|
+
: `!.${NestedKey}`
|
|
98
|
+
>
|
|
99
|
+
>
|
|
100
|
+
>
|
|
101
|
+
>(
|
|
102
|
+
key: TargetKey
|
|
103
|
+
): any;
|
|
104
|
+
} {
|
|
105
|
+
const context = useIntlContext();
|
|
106
|
+
|
|
107
|
+
const messages = context.messages as IntlMessages;
|
|
108
|
+
if (!messages) {
|
|
109
|
+
const intlError = new IntlError(
|
|
110
|
+
IntlErrorCode.MISSING_MESSAGE,
|
|
111
|
+
__DEV__ ? `No messages were configured on the provider.` : undefined
|
|
112
|
+
);
|
|
113
|
+
context.onError(intlError);
|
|
114
|
+
throw intlError;
|
|
115
|
+
}
|
|
311
116
|
|
|
312
|
-
|
|
117
|
+
// We have to wrap the actual hook so the type inference for the optional
|
|
118
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
119
|
+
// The prefix ("!"") is arbitrary, but we have to use some.
|
|
120
|
+
return useTranslationsImpl<
|
|
121
|
+
{'!': IntlMessages},
|
|
122
|
+
NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
|
|
123
|
+
? '!'
|
|
124
|
+
: `!.${NestedKey}`
|
|
125
|
+
>(
|
|
126
|
+
{'!': messages},
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
namespace ? `!.${namespace}` : '!',
|
|
129
|
+
'!'
|
|
130
|
+
);
|
|
313
131
|
}
|