use-intl 2.3.5 → 2.4.1-alpha.2
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/GlobalMessages.d.ts +2 -0
- package/dist/IntlContext.d.ts +2 -0
- package/dist/IntlProvider.d.ts +5 -0
- package/dist/use-intl.cjs.development.js +60 -38
- 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 +45 -27
- package/dist/use-intl.esm.js.map +1 -1
- package/dist/useTranslations.d.ts +10 -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/package.json +7 -6
- package/src/GlobalMessages.tsx +6 -0
- package/src/IntlContext.tsx +2 -0
- package/src/IntlMessages.tsx +3 -1
- package/src/IntlProvider.tsx +5 -0
- package/src/en.json +32 -0
- package/src/index.tsx +1 -0
- package/src/useTranslations.tsx +24 -300
- package/src/useTranslationsImpl.tsx +315 -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/IntlContext.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import {createContext} from 'react';
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlError from './IntlError';
|
|
4
4
|
import IntlMessages from './IntlMessages';
|
|
5
|
+
import {RichTranslationValues} from './TranslationValues';
|
|
5
6
|
|
|
6
7
|
export type IntlContextShape = {
|
|
7
8
|
messages?: IntlMessages;
|
|
@@ -15,6 +16,7 @@ export type IntlContextShape = {
|
|
|
15
16
|
namespace?: string;
|
|
16
17
|
}): string;
|
|
17
18
|
now?: Date;
|
|
19
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
const IntlContext = createContext<IntlContextShape | undefined>(undefined);
|
package/src/IntlMessages.tsx
CHANGED
package/src/IntlProvider.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import React, {ReactNode} from 'react';
|
|
|
2
2
|
import Formats from './Formats';
|
|
3
3
|
import IntlContext from './IntlContext';
|
|
4
4
|
import IntlMessages from './IntlMessages';
|
|
5
|
+
import {RichTranslationValues} from './TranslationValues';
|
|
5
6
|
import {IntlError} from '.';
|
|
6
7
|
|
|
7
8
|
type Props = {
|
|
@@ -39,6 +40,10 @@ type Props = {
|
|
|
39
40
|
* afterwards the current date will be returned continuously.
|
|
40
41
|
*/
|
|
41
42
|
now?: Date;
|
|
43
|
+
/** Global default values for translation values and rich text elements.
|
|
44
|
+
* Can be used for consistent usage or styling of rich text elements.
|
|
45
|
+
* Defaults will be overidden by locally provided values. */
|
|
46
|
+
defaultTranslationValues?: RichTranslationValues;
|
|
42
47
|
};
|
|
43
48
|
|
|
44
49
|
function defaultGetMessageFallback({
|
package/src/en.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"About": {
|
|
3
|
+
"title": "About",
|
|
4
|
+
"lastUpdated": "This example was updated {lastUpdatedRelative} ({lastUpdated, date, short}).",
|
|
5
|
+
"nested": {
|
|
6
|
+
"hello": "Hello"
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
"Index": {
|
|
10
|
+
"title": "Home",
|
|
11
|
+
"description": "<p>Only the minimum of <code>{locale}</code> messages are loaded to render this page.</p><p>These namespaces are available:</p>"
|
|
12
|
+
},
|
|
13
|
+
"Navigation": {
|
|
14
|
+
"index": "Home",
|
|
15
|
+
"about": "About",
|
|
16
|
+
"switchLocale": "Switch to {locale, select, de {German} en {English}}"
|
|
17
|
+
},
|
|
18
|
+
"NotFound": {
|
|
19
|
+
"title": "Sorry, this page could not be found."
|
|
20
|
+
},
|
|
21
|
+
"PageLayout": {
|
|
22
|
+
"pageTitle": "next-intl"
|
|
23
|
+
},
|
|
24
|
+
"Test": {
|
|
25
|
+
"nested": {
|
|
26
|
+
"hello": "Hello",
|
|
27
|
+
"another": {
|
|
28
|
+
"level": "Level"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export {default as IntlProvider} from './IntlProvider';
|
|
2
2
|
export {default as IntlMessages} from './IntlMessages';
|
|
3
|
+
// export {default as GlobalMessages} from './GlobalMessages';
|
|
3
4
|
export {default as useTranslations} from './useTranslations';
|
|
4
5
|
export {
|
|
5
6
|
default as TranslationValues,
|
package/src/useTranslations.tsx
CHANGED
|
@@ -1,79 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
cloneElement,
|
|
4
|
-
isValidElement,
|
|
5
|
-
ReactElement,
|
|
6
|
-
ReactNode,
|
|
7
|
-
ReactNodeArray,
|
|
8
|
-
useMemo,
|
|
9
|
-
useRef
|
|
10
|
-
} from 'react';
|
|
11
|
-
import Formats from './Formats';
|
|
12
|
-
import IntlError, {IntlErrorCode} from './IntlError';
|
|
13
|
-
import IntlMessages from './IntlMessages';
|
|
14
|
-
import TranslationValues, {RichTranslationValues} from './TranslationValues';
|
|
15
|
-
import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
|
|
1
|
+
// import GlobalMessages from './GlobalMessages';
|
|
16
2
|
import useIntlContext from './useIntlContext';
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
idPath: string,
|
|
21
|
-
namespace?: string
|
|
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 (!values) return values;
|
|
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
|
-
}
|
|
3
|
+
import useTranslationsImpl from './useTranslationsImpl';
|
|
4
|
+
import NamespaceKeys from './utils/NamespaceKeys';
|
|
5
|
+
import NestedKeyOf from './utils/NestedKeyOf';
|
|
77
6
|
|
|
78
7
|
/**
|
|
79
8
|
* Translates messages from the given namespace by using the ICU syntax.
|
|
@@ -83,229 +12,24 @@ function prepareTranslationValues(values?: RichTranslationValues) {
|
|
|
83
12
|
* The namespace can also indicate nesting by using a dot
|
|
84
13
|
* (e.g. `namespace.Component`).
|
|
85
14
|
*/
|
|
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
|
-
const retrievedMessages = namespace
|
|
109
|
-
? resolvePath(allMessages, namespace)
|
|
110
|
-
: allMessages;
|
|
111
|
-
|
|
112
|
-
if (!retrievedMessages) {
|
|
113
|
-
throw new Error(
|
|
114
|
-
__DEV__
|
|
115
|
-
? `No messages for namespace \`${namespace}\` found.`
|
|
116
|
-
: undefined
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return retrievedMessages;
|
|
121
|
-
} catch (error) {
|
|
122
|
-
const intlError = new IntlError(
|
|
123
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
124
|
-
(error as Error).message
|
|
125
|
-
);
|
|
126
|
-
onError(intlError);
|
|
127
|
-
return intlError;
|
|
128
|
-
}
|
|
129
|
-
}, [allMessages, namespace, onError]);
|
|
130
|
-
|
|
131
|
-
const translate = useMemo(() => {
|
|
132
|
-
function getFallbackFromErrorAndNotify(
|
|
133
|
-
key: string,
|
|
134
|
-
code: IntlErrorCode,
|
|
135
|
-
message?: string
|
|
136
|
-
) {
|
|
137
|
-
const error = new IntlError(code, message);
|
|
138
|
-
onError(error);
|
|
139
|
-
return getMessageFallback({error, key, namespace});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function translateBaseFn(
|
|
143
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
144
|
-
key: string,
|
|
145
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
146
|
-
values?: RichTranslationValues,
|
|
147
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
148
|
-
formats?: Partial<Formats>
|
|
149
|
-
): string | ReactElement | ReactNodeArray {
|
|
150
|
-
const cachedFormatsByLocale = cachedFormatsByLocaleRef.current;
|
|
151
|
-
|
|
152
|
-
if (messagesOrError instanceof IntlError) {
|
|
153
|
-
// We have already warned about this during render
|
|
154
|
-
return getMessageFallback({
|
|
155
|
-
error: messagesOrError,
|
|
156
|
-
key,
|
|
157
|
-
namespace
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
const messages = messagesOrError;
|
|
161
|
-
|
|
162
|
-
const cacheKey = [namespace, key]
|
|
163
|
-
.filter((part) => part != null)
|
|
164
|
-
.join('.');
|
|
165
|
-
|
|
166
|
-
let messageFormat;
|
|
167
|
-
if (cachedFormatsByLocale[locale]?.[cacheKey]) {
|
|
168
|
-
messageFormat = cachedFormatsByLocale[locale][cacheKey];
|
|
169
|
-
} else {
|
|
170
|
-
let message;
|
|
171
|
-
try {
|
|
172
|
-
message = resolvePath(messages, key, namespace);
|
|
173
|
-
} catch (error) {
|
|
174
|
-
return getFallbackFromErrorAndNotify(
|
|
175
|
-
key,
|
|
176
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
177
|
-
(error as Error).message
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (typeof message === 'object') {
|
|
182
|
-
return getFallbackFromErrorAndNotify(
|
|
183
|
-
key,
|
|
184
|
-
IntlErrorCode.INSUFFICIENT_PATH,
|
|
185
|
-
__DEV__
|
|
186
|
-
? `Insufficient path specified for \`${key}\` in \`${
|
|
187
|
-
namespace ? `\`${namespace}\`` : 'messages'
|
|
188
|
-
}\`.`
|
|
189
|
-
: undefined
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
try {
|
|
194
|
-
messageFormat = new IntlMessageFormat(
|
|
195
|
-
message,
|
|
196
|
-
locale,
|
|
197
|
-
convertFormatsToIntlMessageFormat(
|
|
198
|
-
{...globalFormats, ...formats},
|
|
199
|
-
timeZone
|
|
200
|
-
)
|
|
201
|
-
);
|
|
202
|
-
} catch (error) {
|
|
203
|
-
return getFallbackFromErrorAndNotify(
|
|
204
|
-
key,
|
|
205
|
-
IntlErrorCode.INVALID_MESSAGE,
|
|
206
|
-
(error as Error).message
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (!cachedFormatsByLocale[locale]) {
|
|
211
|
-
cachedFormatsByLocale[locale] = {};
|
|
212
|
-
}
|
|
213
|
-
cachedFormatsByLocale[locale][cacheKey] = messageFormat;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
try {
|
|
217
|
-
const formattedMessage = messageFormat.format(
|
|
218
|
-
prepareTranslationValues(values)
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
if (formattedMessage == null) {
|
|
222
|
-
throw new Error(
|
|
223
|
-
__DEV__
|
|
224
|
-
? `Unable to format \`${key}\` in ${
|
|
225
|
-
namespace ? `namespace \`${namespace}\`` : 'messages'
|
|
226
|
-
}`
|
|
227
|
-
: undefined
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Limit the function signature to return strings or React elements
|
|
232
|
-
return isValidElement(formattedMessage) ||
|
|
233
|
-
// Arrays of React elements
|
|
234
|
-
Array.isArray(formattedMessage) ||
|
|
235
|
-
typeof formattedMessage === 'string'
|
|
236
|
-
? formattedMessage
|
|
237
|
-
: String(formattedMessage);
|
|
238
|
-
} catch (error) {
|
|
239
|
-
return getFallbackFromErrorAndNotify(
|
|
240
|
-
key,
|
|
241
|
-
IntlErrorCode.FORMATTING_ERROR,
|
|
242
|
-
(error as Error).message
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
function translateFn(
|
|
248
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
249
|
-
key: string,
|
|
250
|
-
/** Key value pairs for values to interpolate into the message. */
|
|
251
|
-
values?: TranslationValues,
|
|
252
|
-
/** Provide custom formats for numbers, dates and times. */
|
|
253
|
-
formats?: Partial<Formats>
|
|
254
|
-
): string {
|
|
255
|
-
const message = translateBaseFn(key, values, formats);
|
|
256
|
-
|
|
257
|
-
if (typeof message !== 'string') {
|
|
258
|
-
return getFallbackFromErrorAndNotify(
|
|
259
|
-
key,
|
|
260
|
-
IntlErrorCode.INVALID_MESSAGE,
|
|
261
|
-
__DEV__
|
|
262
|
-
? `The message \`${key}\` in ${
|
|
263
|
-
namespace ? `namespace \`${namespace}\`` : 'messages'
|
|
264
|
-
} didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
|
|
265
|
-
: undefined
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return message;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
translateFn.rich = translateBaseFn;
|
|
273
|
-
|
|
274
|
-
translateFn.raw = (
|
|
275
|
-
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
276
|
-
key: string
|
|
277
|
-
): any => {
|
|
278
|
-
if (messagesOrError instanceof IntlError) {
|
|
279
|
-
// We have already warned about this during render
|
|
280
|
-
return getMessageFallback({
|
|
281
|
-
error: messagesOrError,
|
|
282
|
-
key,
|
|
283
|
-
namespace
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
const messages = messagesOrError;
|
|
287
|
-
|
|
288
|
-
try {
|
|
289
|
-
return resolvePath(messages, key, namespace);
|
|
290
|
-
} catch (error) {
|
|
291
|
-
return getFallbackFromErrorAndNotify(
|
|
292
|
-
key,
|
|
293
|
-
IntlErrorCode.MISSING_MESSAGE,
|
|
294
|
-
(error as Error).message
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
return translateFn;
|
|
300
|
-
}, [
|
|
301
|
-
getMessageFallback,
|
|
302
|
-
globalFormats,
|
|
303
|
-
locale,
|
|
304
|
-
messagesOrError,
|
|
305
|
-
namespace,
|
|
306
|
-
onError,
|
|
307
|
-
timeZone
|
|
308
|
-
]);
|
|
309
|
-
|
|
310
|
-
return translate;
|
|
15
|
+
export default function useTranslations<
|
|
16
|
+
NestedKey extends NamespaceKeys<GlobalMessages, NestedKeyOf<GlobalMessages>>
|
|
17
|
+
>(namespace?: NestedKey) {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
const messages = useIntlContext().messages as GlobalMessages;
|
|
20
|
+
if (!messages) throw new Error('TODO')
|
|
21
|
+
|
|
22
|
+
// We have to wrap the actual hook so the type inference for the optional
|
|
23
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
24
|
+
return useTranslationsImpl<
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
{__private: GlobalMessages},
|
|
27
|
+
NamespaceKeys<GlobalMessages, NestedKeyOf<GlobalMessages>> extends NestedKey
|
|
28
|
+
? '__private'
|
|
29
|
+
: `__private.${NestedKey}`
|
|
30
|
+
>(
|
|
31
|
+
{__private: messages},
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
namespace ? `__private.${namespace}` : '__private'
|
|
34
|
+
);
|
|
311
35
|
}
|