-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.ts
47 lines (42 loc) · 1.38 KB
/
request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {getRequestConfig} from 'next-intl/server';
import {routing} from './routing';
import {getLocalePath} from './settings';
export default getRequestConfig(async ({requestLocale}) => {
// This typically corresponds to the `[locale]` segment
let locale = await requestLocale;
// Ensure that the incoming `locale` is valid
if (!locale || !routing.locales.includes(locale as any)) {
locale = routing.defaultLocale;
}
const messages = {} as Record<string, string>;
try {
const parts = locale.split('-');
let localeLang = parts[0];
if (parts.length === 3) {
localeLang = parts.slice(0, 2).join('-');
}
const rawMessages = await fetch(getLocalePath(localeLang), {
next: {tags: ['i18n', localeLang]}
});
// const defaultMessages = await fetch(getLocalePath(localeLang), {
// next: {tags: ['i18n', defaultLocale]}
// });
const localeJson = await rawMessages.json();
// const defaultLocaleJson = await defaultMessages.json();
console.log('localeJson', Object.keys(localeJson).length);
// eslint-disable-next-line no-restricted-syntax
for (const k in localeJson) {
if (!k.includes('.')) {
const key = k.replace(/^t_/g, '');
messages[key] = localeJson[k];
}
}
} catch (e) {
console.error('i18n getRequestConfig', e);
// captureException(e);
}
return {
locale,
messages
};
});