redux-pagan
managing internationalization via redux
(see react-pagan
demo)
Setup redux store
Include i18n reducer in redux:
; const createStoreWithMiddleware = createStore; const rootReducer = ; const store = ;...
Loading lang data
; { // here we use promise-loader to load lang data by demand return ;} @ { thisprops; } { return <select onChange=thishandleLocaleChange value=thispropslocale> <option value='en-US'>en-US</option> <option value='ru-RU'>ru-RU</option> <option value='fi-FI'>fi-FI</option> </select> ; } { thisprops; }
Using lang data
i18n.get(...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:
i18n'lang' 'string'
Every call is memoized. To receive string value from last call, use .toString()
or .s
property (it's also smart enough to be used as React element without calling .toString()
or .s
- NB: for this to work, you'll need a Symbol
polyfill)
// in this case lang data looks like:// {// "app": {// "some": {// "text": "foobar"// },// "element": {// "something": {// "else": "foobar"// }// }// }// } ;; @ { // string methods are proxied, no need to call .toString() either const str = thispropslang'some' 'text'; return <div> thispropslang'some' 'text' <Element lang=thispropslang'element' /> </div> ; } { return <div> thispropslang'something' 'else' </div> ; }