Created
January 15, 2018 02:14
-
-
Save thanhtungdp/9bd12936ca149d5c6bd7e190e3d4d489 to your computer and use it in GitHub Desktop.
Language
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
// hoc/create-lang.js | |
/*eslint-disable*/ | |
import React from 'react' | |
import PropTypes from 'prop-types' | |
import objectPath from 'object-path' | |
import { connectAutoDispatch } from 'redux/connect' | |
import { changeLanguage } from 'redux/actions/languageAction' | |
import { autobind } from 'core-decorators' | |
import dot from 'dot' | |
// eslint-disable-next-line | |
export const langPropTypes = PropTypes.shape({ | |
t: PropTypes.func, | |
changeLanguage: PropTypes.func | |
}) | |
export function translate(key, params = {}, isParse = true) { | |
const languageData = (typeof window !== "undefined" ? window.currentLanguage : global.currentLanguage) | |
let translated = objectPath.get(languageData, key) | |
if (translated && isParse) { | |
return dot.template(translated)(params).toString() | |
} else return translated ? translated : '' | |
} | |
const createLanguageHoc = Component => { | |
@connectAutoDispatch( | |
state => ({ | |
languageData: state.language.data[state.language.locale], | |
languageLocale: state.language.locale | |
}), | |
{ changeLanguage } | |
) | |
@autobind | |
class LanguageHoc extends React.PureComponent { | |
static propTypes = { | |
changeLanguage: PropTypes.func | |
} | |
static getInitialProps = Component.getInitialProps | |
constructor(props) { | |
super(props) | |
} | |
translate(key, params = {}, isParse = true) { | |
let translated = objectPath.get(this.props.languageData, key) | |
if (translated && isParse) { | |
return dot.template(translated)(params) | |
} else return translated ? translated : '' | |
} | |
changeLanguage(lang) { | |
this.props.changeLanguage(lang) | |
} | |
render() { | |
const langProps = { | |
t: this.translate, | |
locale: this.props.languageLocale, | |
changeLanguage: this.changeLanguage | |
} | |
return <Component {...this.props} ref={this.props.ref} lang={langProps} /> | |
} | |
} | |
return LanguageHoc | |
} | |
export default createLanguageHoc |
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
// redux/reducers/language.js | |
import update from 'react-addons-update' | |
import { CHANGE_LANGUAGE } from '../actions/languageAction' | |
import languages from 'languages' | |
const initialState = { | |
locale: 'en', | |
data: languages | |
} | |
export default function createReducer (state = initialState, action) { | |
switch (action.type) { | |
case CHANGE_LANGUAGE: | |
return changeLanguage(state, action) | |
default: | |
return state | |
} | |
} | |
export function changeLanguage (state, { locale }) { | |
return update(state, { | |
locale: { | |
$set: 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
// redux/reducers/lang.js | |
export const CHANGE_LANGUAGE = 'LANGUAGE/change-language' | |
export function changeLanguage (locale = 'en') { | |
return { | |
type: CHANGE_LANGUAGE, | |
locale | |
} | |
} |
Author
thanhtungdp
commented
Jan 15, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment