Skip to content

Commit 9fd1668

Browse files
committed
Add multiple languages example
1 parent 544aa3b commit 9fd1668

File tree

10 files changed

+162
-10
lines changed

10 files changed

+162
-10
lines changed

.eslintrc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ extends:
33
- concise-esnext
44
- concise-ava
55
- concise-react
6+
rules:
7+
comma-dangle: off

examples/initial-locale/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Run this example.
22

33
```bash
4-
npm i
4+
npm install
55
npm start
6-
open http://localhost:8080/
6+
open http://localhost:3000/
77
```

examples/initial-locale/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@
1212
"react-redux": "^5.0.6",
1313
"react-scripts": "1.0.14",
1414
"redux": "^3.7.2"
15-
},
16-
"devDependencies": {
17-
"create-react-app": "^1.4.1"
1815
}
1916
}

examples/initial-locale/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ const initialState = {
2424
}
2525
const store = createStore(reducer, initialState)
2626

27-
const App = () =>
27+
const App = () => (
2828
<Provider store={store}>
2929
<IntlProvider>
3030
<p>
3131
<FormattedMessage id="app.greeting" defaultMessage="你好!" />
3232
</p>
3333
</IntlProvider>
3434
</Provider>
35+
)
3536

3637
ReactDOM.render(<App />, document.getElementById('root'))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Run this example.
2+
3+
```bash
4+
npm install
5+
npm start
6+
open http://localhost:3000/
7+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"start": "react-scripts start",
5+
"build": "react-scripts build"
6+
},
7+
"dependencies": {
8+
"intl": "^1.2.5",
9+
"prop-types": "^15.6.0",
10+
"react": "^15.6.2",
11+
"react-dom": "^15.6.2",
12+
"react-intl": "^2.4.0",
13+
"react-intl-redux": "^0.6.0",
14+
"react-redux": "^5.0.6",
15+
"react-scripts": "1.0.14",
16+
"redux": "^3.7.2"
17+
},
18+
"devDependencies": {
19+
"redux-devtools": "^3.4.0",
20+
"redux-devtools-dock-monitor": "^1.1.2",
21+
"redux-devtools-log-monitor": "^1.3.0"
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<meta name="theme-color" content="#000000">
7+
<title>React Intl Redux Example - multiple-languages</title>
8+
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en,Intl.~locale.it,Intl.~locale.zh"></script>
9+
</head>
10+
<body>
11+
<noscript>
12+
You need to enable JavaScript to run this app.
13+
</noscript>
14+
<div id="root"></div>
15+
</body>
16+
</html>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* globals document */
2+
import { addLocaleData, FormattedMessage } from 'react-intl'
3+
import { createStore, combineReducers } from 'redux'
4+
import { IntlProvider, intlReducer, updateIntl } from 'react-intl-redux'
5+
import { Provider, connect } from 'react-redux'
6+
import itLocaleData from 'react-intl/locale-data/it'
7+
import zhLocaleData from 'react-intl/locale-data/zh'
8+
import React from 'react'
9+
import ReactDOM from 'react-dom'
10+
11+
import { createDevTools } from 'redux-devtools'
12+
import DockMonitor from 'redux-devtools-dock-monitor'
13+
import LogMonitor from 'redux-devtools-log-monitor'
14+
15+
addLocaleData([...itLocaleData, ...zhLocaleData])
16+
17+
const UPDATE_LOCALES = 'UPDATE_LOCALES'
18+
19+
function localesReducer(state = {}, action) {
20+
switch (action.type) {
21+
case UPDATE_LOCALES:
22+
return {
23+
...state,
24+
...action.payload,
25+
}
26+
default:
27+
return state
28+
}
29+
}
30+
31+
const reducer = combineReducers({
32+
intl: intlReducer,
33+
locales: localesReducer,
34+
})
35+
const DevTools = createDevTools(
36+
<DockMonitor
37+
toggleVisibilityKey="ctrl-h"
38+
changePositionKey="ctrl-q"
39+
changeMonitorKey="ctrl-m"
40+
>
41+
<LogMonitor />
42+
</DockMonitor>
43+
)
44+
const store = createStore(reducer, {}, DevTools.instrument())
45+
46+
const SwitchLocale = connect((state, props) => ({
47+
currentLocale: state.intl.locale,
48+
locales: state.locales,
49+
}))(({ currentLocale, locales, onChange }) => (
50+
<select
51+
value={currentLocale}
52+
onChange={e =>
53+
store.dispatch(
54+
updateIntl({
55+
locale: e.target.value,
56+
messages: locales[e.target.value],
57+
})
58+
)}
59+
>
60+
{Object.keys(locales).map(locale => <option key={locale}>{locale}</option>)}
61+
</select>
62+
))
63+
64+
class App extends React.Component {
65+
handleLoadlLocales = () => {
66+
store.dispatch({
67+
type: UPDATE_LOCALES,
68+
payload: {
69+
en: {
70+
'app.greeting': 'Hello!',
71+
},
72+
it: {
73+
'app.greeting': 'Ciao!',
74+
},
75+
zh: {
76+
'app.greeting': '你好!',
77+
},
78+
},
79+
})
80+
}
81+
82+
render() {
83+
return (
84+
<Provider store={store}>
85+
<IntlProvider>
86+
<div>
87+
<p>
88+
<FormattedMessage id="app.greeting" defaultMessage="Hello!" />
89+
</p>
90+
<p>
91+
<button onClick={this.handleLoadlLocales}>
92+
Local locales
93+
</button>{' '}
94+
<SwitchLocale />
95+
</p>
96+
<DevTools />
97+
</div>
98+
</IntlProvider>
99+
</Provider>
100+
)
101+
}
102+
}
103+
104+
ReactDOM.render(<App />, document.getElementById('root'))

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
"babel-cli": "^6.26.0",
2525
"babel-eslint": "^8.0.1",
2626
"babel-plugin-dev-expression": "^0.2.1",
27-
"babel-plugin-transform-react-remove-prop-types": "^0.4.8",
27+
"babel-plugin-transform-react-remove-prop-types": "^0.4.9",
2828
"babel-preset-env": "^1.6.0",
2929
"babel-preset-react": "^6.24.1",
3030
"babel-preset-stage-1": "^6.24.1",
3131
"babel-register": "^6.26.0",
3232
"cross-env": "^5.0.5",
3333
"del-cli": "^1.1.0",
34-
"enzyme": "^3.0.0",
35-
"enzyme-adapter-react-16": "^1.0.0",
34+
"enzyme": "^3.1.0",
35+
"enzyme-adapter-react-16": "^1.0.1",
3636
"eslint": "^4.8.0",
3737
"eslint-config-concise": "^0.12.0",
3838
"eslint-config-concise-ava": "^0.12.0",

src/components/IntlProvider.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ function defaultSelector(state) {
88
const mapStateToProps = (state, { intlSelector = defaultSelector }) =>
99
intlSelector(state)
1010

11-
export default connect(mapStateToProps)(IntlProvider)
11+
export default connect(mapStateToProps, null, null, { pure: false })(
12+
IntlProvider
13+
)

0 commit comments

Comments
 (0)