-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add internationalization support
- Loading branch information
Showing
15 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const i18nActions = { | ||
CHANGE_LOCALE: 'CHANGE_LOCALE', | ||
|
||
change_locale: (locale) => ({ | ||
type: i18nActions.change_locale, | ||
payload: { | ||
locale | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { i18nActions } from './actions' | ||
export { i18nReducer } from './reducer' | ||
export { i18nSagas } from './sagas' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Record } from 'immutable' | ||
|
||
import { i18nActions } from './actions' | ||
|
||
const initialState = new Record({ | ||
locale: 'en' | ||
}) | ||
|
||
export function i18nReducer(state = initialState(), { payload, type }) { | ||
switch (type) { | ||
case i18nActions.CHANGE_LANGUAGE: | ||
return state.set('locale', payload.locale) | ||
|
||
default: | ||
return state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { takeLatest, put, fork } from 'redux-saga/effects' | ||
import i18n from 'i18next' | ||
|
||
import { localStorageAdapter } from '@core/utils' | ||
import { appActions } from '@core/app/actions' | ||
import { i18nActions } from './actions' | ||
|
||
export function* init() { | ||
const locale = localStorageAdapter.getItem('locale') | ||
if (locale) { | ||
yield put(i18nActions.change_locale(locale)) | ||
} | ||
|
||
// TODO detect user locale | ||
} | ||
|
||
export function ChangeLocale({ payload }) { | ||
localStorageAdapter.setItem('locale', payload.locale) | ||
i18n.changeLanguage(payload.locale) | ||
} | ||
|
||
//= ==================================== | ||
// WATCHERS | ||
// ------------------------------------- | ||
|
||
export function* watchInitApp() { | ||
yield takeLatest(appActions.INIT_APP, init) | ||
} | ||
|
||
export function* watchChangeLocale() { | ||
yield takeLatest(i18nActions.CHANGE_LOCALE, ChangeLocale) | ||
} | ||
|
||
//= ==================================== | ||
// ROOT | ||
// ------------------------------------- | ||
|
||
export const i18nSagas = [fork(watchInitApp), fork(watchChangeLocale)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { initReactI18next } from 'react-i18next' | ||
import i18n from 'i18next' | ||
|
||
i18n.use(initReactI18next).init({ | ||
// detection | ||
debug: true, | ||
resources: { | ||
en: { | ||
translation: { | ||
'Welcome to React': 'Welcome to React and react-i18next' | ||
} | ||
} | ||
}, | ||
lng: 'en', | ||
fallbackLng: 'en' | ||
// supportedLngs | ||
}) | ||
|
||
export default i18n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import FormControl from '@material-ui/core/FormControl' | ||
import Select from '@material-ui/core/Select' | ||
import MenuItem from '@material-ui/core/MenuItem' | ||
|
||
import './change-locale.styl' | ||
|
||
export default function ChangeLocale({ change_locale, locale }) { | ||
return ( | ||
<FormControl className='change-locale'> | ||
<Select | ||
labelId='change-locale' | ||
id='change-locale' | ||
value={locale} | ||
onChange={(event) => change_locale(event.target.value)}> | ||
<MenuItem value='en'>English</MenuItem> | ||
<MenuItem value='es'>Español</MenuItem> | ||
<MenuItem value='fr'>Français</MenuItem> | ||
<MenuItem value='it'>Italiano</MenuItem> | ||
<MenuItem value='de'>Deutsch</MenuItem> | ||
<MenuItem value='nl'>Nederlands</MenuItem> | ||
<MenuItem value='ru'>Русский</MenuItem> | ||
</Select> | ||
</FormControl> | ||
) | ||
} | ||
|
||
ChangeLocale.propTypes = { | ||
change_locale: PropTypes.func.isRequired, | ||
locale: PropTypes.string.isRequired | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { connect } from 'react-redux' | ||
import { createSelector } from 'reselect' | ||
|
||
import { i18nActions } from '@core/i18n' | ||
|
||
import ChangeLocale from './change-locale' | ||
|
||
const mapStateToProps = createSelector( | ||
(state) => state.getIn(['i18n', 'locale']), | ||
(locale) => ({ locale }) | ||
) | ||
|
||
const mapDispatchToProps = { | ||
change_locale: i18nActions.change_locale | ||
} | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(ChangeLocale) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters