-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
_app.tsx
43 lines (36 loc) · 1.18 KB
/
_app.tsx
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
import * as React from 'react';
import {IntlProvider} from 'react-intl';
import {polyfill} from '../polyfills';
import App from 'next/app';
function MyApp({Component, pageProps, locale, messages}) {
return (
<IntlProvider locale={locale} messages={messages}>
<Component {...pageProps} />
</IntlProvider>
);
}
// We need to load and expose the translations on the request for the user's
// locale. These will only be used in production, in dev the `defaultMessage` in
// each message description in the source code will be used.
const getMessages = (locale: string = 'en') => {
switch (locale) {
default:
return import('../compiled-lang/en.json');
case 'fr':
return import('../compiled-lang/fr.json');
}
};
const getInitialProps: typeof App.getInitialProps = async appContext => {
const {
ctx: {req},
} = appContext;
const locale = (req as any)?.locale || (window as any).LOCALE || 'en';
const [appProps, messages] = await Promise.all([
polyfill(locale),
getMessages(locale),
App.getInitialProps(appContext),
]);
return {...(appProps as any), locale, messages};
};
MyApp.getInitialProps = getInitialProps;
export default MyApp;