Commit 822cc3c8 authored by Tomek's avatar Tomek Committed by Tim Neutkens

update with-react-intl example (#4817)

parent 0d93d426
...@@ -46,7 +46,7 @@ This example app shows how to integrate [React Intl][] with Next. ...@@ -46,7 +46,7 @@ This example app shows how to integrate [React Intl][] with Next.
- Server-side language negotiation - Server-side language negotiation
- React Intl locale data loading via `pages/_document.js` customization - React Intl locale data loading via `pages/_document.js` customization
- React Intl integration at Next page level via `pageWithIntl()` HOC - React Intl integration with [custom App](https://github.com/zeit/next.js#custom-app) component
- `<IntlProvider>` creation with `locale`, `messages`, and `initialNow` props - `<IntlProvider>` creation with `locale`, `messages`, and `initialNow` props
- Default message extraction via `babel-plugin-react-intl` integration - Default message extraction via `babel-plugin-react-intl` integration
- Translation management via build script and customized Next server - Translation management via build script and customized Next server
......
import React, {Component} from 'react' import App, { Container } from 'next/app'
import {IntlProvider, addLocaleData, injectIntl} from 'react-intl' 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 // 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 // locale data was added to the page by `pages/_document.js`. This only happens
...@@ -10,35 +11,31 @@ if (typeof window !== 'undefined' && window.ReactIntlLocaleData) { ...@@ -10,35 +11,31 @@ if (typeof window !== 'undefined' && window.ReactIntlLocaleData) {
}) })
} }
export default (Page) => { export default class MyApp extends App {
const IntlPage = injectIntl(Page) static async getInitialProps ({ Component, router, ctx }) {
let pageProps = {}
return class PageWithIntl extends Component { if (Component.getInitialProps) {
static async getInitialProps (context) { pageProps = await Component.getInitialProps(ctx)
let props }
if (typeof Page.getInitialProps === 'function') {
props = await Page.getInitialProps(context)
}
// 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} = context
const {locale, messages} = req || window.__NEXT_DATA__.props.pageProps
// Always update the current time on page load/transition because the // Get the `locale` and `messages` from the request object on the server.
// <IntlProvider> will be a new instance even with pushState routing. // In the browser, use the same values that the server serialized.
const now = Date.now() const { req } = ctx
const { locale, messages } = req || window.__NEXT_DATA__.props.pageProps
return {...props, locale, messages, now} return { pageProps, locale, messages }
} }
render () { render () {
const {locale, messages, now, ...props} = this.props const { Component, pageProps, locale, messages } = this.props
return ( const now = Date.now()
return (
<Container>
<IntlProvider locale={locale} messages={messages} initialNow={now}> <IntlProvider locale={locale} messages={messages} initialNow={now}>
<IntlPage {...props} /> <Component {...pageProps} />
</IntlProvider> </IntlProvider>
) </Container>
} )
} }
} }
import React, {Component} from 'react' import React, {Component} from 'react'
import {FormattedRelative} from 'react-intl' import {FormattedRelative} from 'react-intl'
import pageWithIntl from '../components/PageWithIntl'
import Layout from '../components/Layout' import Layout from '../components/Layout'
class About extends Component { class About extends Component {
...@@ -22,4 +21,4 @@ class About extends Component { ...@@ -22,4 +21,4 @@ class About extends Component {
} }
} }
export default pageWithIntl(About) export default About
import React from 'react' import React from 'react'
import {FormattedMessage, FormattedNumber, defineMessages} from 'react-intl' import {FormattedMessage, FormattedNumber, defineMessages, injectIntl} from 'react-intl'
import Head from 'next/head' import Head from 'next/head'
import pageWithIntl from '../components/PageWithIntl'
import Layout from '../components/Layout' import Layout from '../components/Layout'
const {description} = defineMessages({ const {description} = defineMessages({
...@@ -11,7 +10,7 @@ const {description} = defineMessages({ ...@@ -11,7 +10,7 @@ const {description} = defineMessages({
} }
}) })
export default pageWithIntl(({intl}) => ( export default injectIntl(({intl}) => (
<Layout> <Layout>
<Head> <Head>
<meta name='description' content={intl.formatMessage(description)} /> <meta name='description' content={intl.formatMessage(description)} />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment