Skip to content

Commit

Permalink
Integration with i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Digvijay Upadhyay committed Aug 30, 2018
1 parent eed332c commit d5a282c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-intl": "^2.4.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"redux": "^4.0.0"
Expand Down
19 changes: 16 additions & 3 deletions src/Pages/Home.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { textChange } from '../Actions';
import { FormattedMessage } from 'react-intl'

class Home extends Component {
render() {
let input
return (
<div>
<h2>
Home Page
<FormattedMessage
id="Home.title"
defaultMessage="Home Page"
/>
</h2>
<div>Write a name and check it in SomePage Link</div>
<div>
<FormattedMessage
id="Home.input"
defaultMessage="Write a name and check it in SomePage Link"
/>
</div>
<input
type="text"
onChange={e =>
Expand All @@ -23,7 +32,11 @@ class Home extends Component {
}}
/>
<div>
You typed: {this.props.text}
<FormattedMessage
id="Home.stateText"
defaultMessage="You typed: {text}"
values={{text: <b>{this.props.text}</b>}}
/>
</div>
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/Translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Home.title": "Startseite",
"Home.input": "Schreiben Sie einen Namen und überprüfen Sie es in SomePage Link.",
"Home.stateText": "Du hast getippt: {text}"
}
5 changes: 5 additions & 0 deletions src/Translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Home.title": "Home Page (loaded from en.json)",
"Home.input": "Write a name and check it in SomePage Link.(loaded from en.json)",
"Home.stateText": "You typed: {text} (loaded from en.json)"
}
17 changes: 16 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@ import { createStore } from 'redux'
import rootReducer from './Reducers'
import App from './App';
import { HashRouter } from 'react-router-dom'
import { IntlProvider, addLocaleData } from 'react-intl';
import locale_en from 'react-intl/locale-data/en';
import locale_de from 'react-intl/locale-data/de';
import messages_de from "./translations/de.json";
import messages_en from "./translations/en.json";

const messages = {
'de': messages_de,
'en': messages_en
};

addLocaleData([...locale_en, ...locale_de]);
const language = navigator.language.split(/[-_]/)[0]; // language without region code

const store = createStore(rootReducer)

const render = () =>
ReactDOM.render(
<Provider store={store}>
<HashRouter>
<App/>
<IntlProvider locale={language} messages={messages[language]}>
<App/>
</IntlProvider>
</HashRouter>
</Provider>,
document.getElementById('index'));
Expand Down

0 comments on commit d5a282c

Please sign in to comment.