1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-react-intl/pages/_app.js
Ivan Starkov 954fcb9674 Fix react-intl example (#4840)
react-intl does not work otherwise
2018-09-02 23:53:43 +02:00

43 lines
1.3 KiB
JavaScript

import App, { Container } from 'next/app'
import React from 'react'
import { IntlProvider, addLocaleData } from 'react-intl'
// Register React Intl's locale data for the user's locale in the browser. This
// locale data was added to the page by `pages/_document.js`. This only happens
// once, on initial page load in the browser.
if (typeof window !== 'undefined' && window.ReactIntlLocaleData) {
Object.keys(window.ReactIntlLocaleData).forEach((lang) => {
addLocaleData(window.ReactIntlLocaleData[lang])
})
}
export default class MyApp extends App {
static async getInitialProps ({ Component, router, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
// Get the `locale` and `messages` from the request object on the server.
// In the browser, use the same values that the server serialized.
const { req } = ctx
const { locale, messages } = req || window.__NEXT_DATA__.props
return { pageProps, locale, messages }
}
render () {
const { Component, pageProps, locale, messages } = this.props
const now = Date.now()
return (
<Container>
<IntlProvider locale={locale} messages={messages} initialNow={now}>
<Component {...pageProps} />
</IntlProvider>
</Container>
)
}
}