You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{loadLang}from'redux-pagan';functiongetLangData(locale){// here we use promise-loader to load lang data by demandreturnrequire('promise?global,[name].i18n!json!./i18n/'+locale+'.i18n.json');}
@connect(...)classAppextendsComponent{componentDidMount(){this.props.dispatch(loadLang(cookie.get('lang')||'en-US',getLangData));}render(){return(<selectonChange={this.handleLocaleChange}value={this.props.locale}><optionvalue='en-US'>en-US</option><optionvalue='ru-RU'>ru-RU</option><optionvalue='fi-FI'>fi-FI</option></select>);}handleLocaleChange=e=>{this.props.dispatch(loadLang(e.target.value,getLangData));}}
Using lang data
getLang(state.i18n, ...path) is a special method that safely obtains strings from lang data. If no strings were found on stated path, last key is returned. This method's calling is chainable:
Every call is memoized. To receive string value from last call, use .toString() or .s property.
// in this case lang data looks like:// {// "app": {// "some": {// "text": "foobar"// },// "element": {// "something": {// "else": "foobar"// }// }// }// }import{connect}from'react-redux';import{getLang}from'redux-pagan';
@connect(state=>({lang: getLang(state.i18n,'app'),locale: state.i18n.locale}))classAppextendsComponent{render(){// string methods are proxied, no need to call .toString() eitherconststr=this.props.lang('some','text').replace('foo','bar');return(<div>{this.props.lang('some','text').s}<Elementlang={this.props.lang('element').s}/></div>);}}classElementextendsComponent{render(){return(<div>{this.props.lang('something','else').s}</div>);}}