Skip to content

Commit 15c746c

Browse files
committed
feat: added libs/RouterContextDataWrapper.js; refactored App.js;
1 parent 3c6a1de commit 15c746c

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/containers/App/App.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,17 @@ import jaLocaleData from 'react-intl/locale-data/ja'
88
addLocaleData(enLocaleData)
99
addLocaleData(jaLocaleData)
1010

11-
let translations
1211
let currentLocale
1312
let messages
1413

15-
if (!__CLIENT__) {
16-
const i18n = require('server/libs/i18n') // eslint-disable-line global-require
17-
translations = i18n.getTranslations()
18-
}
19-
2014
export default class App extends React.Component {
2115
componentWillMount () {
2216
if (__CLIENT__) {
2317
currentLocale = (__CLIENT__ ? window.App.locale : 'en-US')
2418
messages = window.App.messages
2519
} else {
26-
currentLocale = this.props.location.query.locale || 'en-US'
27-
messages = translations[ currentLocale ]
20+
currentLocale = this.context.data.i18nData.locale || 'en-US'
21+
messages = this.context.data.i18nData.messages || {}
2822
}
2923
addLocaleData({
3024
locale: currentLocale,
@@ -42,3 +36,7 @@ export default class App extends React.Component {
4236
)
4337
}
4438
}
39+
40+
App.contextTypes = {
41+
data: React.PropTypes.object
42+
}

src/containers/Home/Home.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import Transmit from 'react-transmit'
33
import { Link } from 'react-router'
4-
import { injectIntl, FormattedDate } from 'react-intl'
4+
import { FormattedDate } from 'react-intl'
55

66
import { ButtonGroup, Button } from 'react-bootstrap'
77
import { CommentBox, NavBar, Utils } from 'components'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
import { RouterContext } from 'react-router'
3+
4+
5+
export function createRouterContextDataWrapper(dataObj) {
6+
return React.createClass({
7+
childContextTypes: {
8+
data: React.PropTypes.object.isRequired
9+
},
10+
getChildContext: function() {
11+
return {
12+
data: dataObj
13+
}
14+
},
15+
render: function() {
16+
return <RouterContext { ...this.props } />
17+
}
18+
})
19+
}

src/server/renderAppRouter.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ const IGNORED_FILES = ['/favicon.ico']
1111
const INDEX_TEMPLATE_FILE = 'dist/views/index.tpl.html'
1212

1313
const indexFileContent = fs.readFileSync(INDEX_TEMPLATE_FILE).toString()
14+
15+
import { createRouterContextDataWrapper } from './libs/RouterContextDataWrapper'
1416
const i18n = require('./libs/i18n')
15-
const i18nObj = { locale: '', messages: {} }
17+
let i18nData = { locale: '', messages: {} }
1618

1719

1820
export default function renderAppRouter () {
@@ -21,8 +23,9 @@ export default function renderAppRouter () {
2123
if (IGNORED_FILES.indexOf(location) >= 0) {
2224
return next()
2325
}
24-
i18nObj.locale = ctx.query.locale || 'en-US'
25-
const messagesJsonString = JSON.stringify(i18n.getLocaleMessages(i18nObj.locale))
26+
const locale = ctx.query.locale || 'en-US'
27+
i18nData = i18n.getLocaleMessages(locale)
28+
const i18nDataString = JSON.stringify(i18nData)
2629

2730
match({ routes, location }, (error, redirectLocation, renderProps) => {
2831
if (redirectLocation) {
@@ -36,10 +39,11 @@ export default function renderAppRouter () {
3639
}
3740
else {
3841
const webserver = (__PRODUCTION__ ? '' : '//' + hostname + ':8080')
42+
var RouterContextDataWrapper = createRouterContextDataWrapper({ i18nData })
3943

40-
Transmit.renderToString(RouterContext, renderProps).then(({ reactString, reactData }) => {
44+
Transmit.renderToString(RouterContextDataWrapper, renderProps).then(({ reactString, reactData }) => {
4145
const renderedHtml = mustache.render(indexFileContent, {
42-
i18n: messagesJsonString,
46+
i18n: i18nDataString,
4347
reactString
4448
})
4549
const output = Transmit.injectIntoMarkup(renderedHtml, reactData, [`${webserver}/client.js`])

0 commit comments

Comments
 (0)