Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
822cc3c8
Commit
822cc3c8
authored
Jul 21, 2018
by
Tomek
Committed by
Tim Neutkens
Jul 21, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update with-react-intl example (#4817)
parent
0d93d426
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
6 deletions
+45
-6
README.md
examples/with-react-intl/README.md
+1
-1
_app.js
examples/with-react-intl/pages/_app.js
+41
-0
about.js
examples/with-react-intl/pages/about.js
+1
-2
index.js
examples/with-react-intl/pages/index.js
+2
-3
No files found.
examples/with-react-intl/README.md
View file @
822cc3c8
...
...
@@ -46,7 +46,7 @@ This example app shows how to integrate [React Intl][] with Next.
-
Server-side language negotiation
-
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
-
Default message extraction via
`babel-plugin-react-intl`
integration
-
Translation management via build script and customized Next server
...
...
examples/with-react-intl/
components/PageWithIntl
.js
→
examples/with-react-intl/
pages/_app
.js
View file @
822cc3c8
import
React
,
{
Component
}
from
'react'
import
{
IntlProvider
,
addLocaleData
,
injectIntl
}
from
'react-intl'
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
...
...
@@ -10,35 +11,31 @@ if (typeof window !== 'undefined' && window.ReactIntlLocaleData) {
})
}
export
default
(
Page
)
=>
{
const
IntlPage
=
injectIntl
(
Page
)
export
default
class
MyApp
extends
App
{
static
async
getInitialProps
({
Component
,
router
,
ctx
})
{
let
pageProps
=
{}
return
class
PageWithIntl
extends
Component
{
static
async
getInitialProps
(
context
)
{
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
if
(
Component
.
getInitialProps
)
{
pageProps
=
await
Component
.
getInitialProps
(
ctx
)
}
// Always update the current time on page load/transition because the
// <IntlProvider> will be a new instance even with pushState routing.
const
now
=
Date
.
now
()
// 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
.
pageProps
return
{...
props
,
locale
,
messages
,
now
}
}
return
{
pageProps
,
locale
,
messages
}
}
render
()
{
const
{
locale
,
messages
,
now
,
...
props
}
=
this
.
props
return
(
render
()
{
const
{
Component
,
pageProps
,
locale
,
messages
}
=
this
.
props
const
now
=
Date
.
now
()
return
(
<
Container
>
<
IntlProvider
locale
=
{
locale
}
messages
=
{
messages
}
initialNow
=
{
now
}
>
<
IntlPage
{...
p
rops
}
/
>
<
Component
{...
pageP
rops
}
/
>
<
/IntlProvider
>
)
}
<
/Container
>
)
}
}
examples/with-react-intl/pages/about.js
View file @
822cc3c8
import
React
,
{
Component
}
from
'react'
import
{
FormattedRelative
}
from
'react-intl'
import
pageWithIntl
from
'../components/PageWithIntl'
import
Layout
from
'../components/Layout'
class
About
extends
Component
{
...
...
@@ -22,4 +21,4 @@ class About extends Component {
}
}
export
default
pageWithIntl
(
About
)
export
default
About
examples/with-react-intl/pages/index.js
View file @
822cc3c8
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
pageWithIntl
from
'../components/PageWithIntl'
import
Layout
from
'../components/Layout'
const
{
description
}
=
defineMessages
({
...
...
@@ -11,7 +10,7 @@ const {description} = defineMessages({
}
})
export
default
pageWith
Intl
(({
intl
})
=>
(
export
default
inject
Intl
(({
intl
})
=>
(
<
Layout
>
<
Head
>
<
meta
name
=
'description'
content
=
{
intl
.
formatMessage
(
description
)}
/
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment