React / Redux localization tool
npm install redux-lexicon --save
currentLang reducer
update currentLang action creator
HOC - add lexicon context to App component
mapStateToLang - currentLang selector
mapStateToLexicon - lexicon selector
defLang - default lang
HOC - add lex prop to WrappedComponent
componentKey - optional, get only componentKey fields from lexicon
withLex()(YouCmp)
withLex('YouCmp')(YouCmp)
withLex('YouCmp', 'OtherCmp')(YouComp)
withLex('YouCmp', 'FerstCmp', 'SecondCmp')(YouComp)
- connect lexicon and currentLang reducers to redux store
You can take currentLang reducer and setLang action from this package
{
en: {
component1: {
key1: "en translate",
key2: "en translate"
},
component2: {
key1: "en translate"
}
},
ru: {
component1: {
key1: "ru translate",
key2: "ru translate"
},
component2: {
key1: " ru translate"
}
}
}
- wrap you root component withLexContext
import { withLexContext } from 'redux-lexicon'
class App extends Component {
...
}
const enhanceLexContext = withLexContext(
state => state.lang,
state => state.lexicon
)
export default enhanceLexContext(App)
- wrap you translate component withLex
import { withLex } from 'redux-lexicon'
class YouComponent extends Component {
...
}
const enhanceLex = withLex('youComponent')
export default enhanceLex(YouComponent)
- get translate strings from lex prop in YouComponent