i18n solution for react/redux
npm install redux-react-i18n




An i18n solution with plural forms support for Redux/React
!Workers of all countries, unite!
derzunov.github.io/redux-react-i18n
```
git clone https://github.com/derzunov/redux-react-i18n redux-react-i18n
cd redux-react-i18n/example
npm i`
and then`
gulp`
or`
gulp prod
#### Write ( jsx ):
`jsx``
#### Result ( html ):html`
Перевод вашего первого ключа из словаря для текущего языка
Пришла 1 кошечка
Пришли 2 кошечки
Пришло 5 кошечек
translator: ( github or npm ) for translation (translator demo)
npm i redux-react-i18n
`What's in the box
$3
- Loc ( Container Component )
- LocPresentational ( Presentational Component )$3
- setCurrentLanguage( languageCode )
- setLanguages( languageCode )
- addDictionary( languageCode, dictionary )
- setDictionaries( dictionaries )$3
- i18n
Full code demo ( complete solution for Redux ):
`jsx
import { i18nReducer, i18nActions, Loc } from 'redux-react-i18n'...
// "reducers" contains your reducers
reducers.i18n = i18nReducer
...
const store = createStore( combineReducers( reducers ) )
...
// Set dictionaries (simpliest example) -----------------------------------------------------------------------------------------------
// This dictionaries can be supported by Localization team without need to know somth about interface or project,
// and you just can fetch it to your project
const dictionaries = {
'ru-RU': {
'key_1': 'Первый дефолтный ключ',
'key_2': [ '$Count', ' ', ['штучка','штучки','штучек']], // 1 штучка, 3 штучки, пять штучек
'key_3': {
'nested_1': 'Первый вложенный ключ',
'nested_2': 'Второй вложенный ключ',
},
/ ... /
/ Other keys /
},
'en-US': {
'key_1': 'First default key',
'key_2': [ '$Count', ' ', ['thing','things']], // 1 thing, 2 things, 153 things
'key_3': {
'nested_1': 'First nested key',
'nested_2': 'Second nested key',
},
}
/ ... /
/ Other dictionaries /
}
store.dispatch( i18nActions.setDictionaries( dictionaries ) )
// / Set dictionaries (simpliest example) ---------------------------------------------------------------------------------------------
// Set languages (simpliest example) --------------------------------------------------------------------------------------------------
const languages = [
{
code: 'ru-RU',
name: 'Русский'
},
{
code: 'en-US',
name: 'English (USA)'
}
/ ... /
/ Other languages /
]
store.dispatch( i18nActions.setLanguages( languages ) )
// / Set languages (simpliest example) ------------------------------------------------------------------------------------------------
// Set current language code (you can map this action to select component or somth like this)
store.dispatch( i18nActions.setCurrentLanguage( 'ru-RU' ) )
`#### And now you can use "Loc" container component
`jsx
import { Loc } from 'redux-react-i18n'
...
// => Первый дефолтный ключ
// => 7 штучек
// => Первый вложенный ключ
// => Второй вложенный ключ
`If you don't want to use a complete solution:
#### Just use a dumb component and you can design store/actions/reducers by yourself like you want
`jsx
// Just import presentational component LocPresentational
import { LocPresentational } from 'redux-react-i18n'
...
...
...
// Then map data to props => currentLanguage, dictionary (See more in src/Loc.js):
const mapStateToProps = ( { /getting data from the state/ }, ownProps ) => ({
currentLanguage: yourCurrentLanguageFromState,
dictionary: yourDictionaryFromState
});
Loc = connect( mapStateToProps )( LocPresentational )
...
...
...
`
See more in src/\*.js
Using with React Native
The "Span Problem"
If the span tag is a big problem (in "option" tag for example), you can use translate from 'translatr' like this
`
import translate from 'translatr'
...
...
...
...
`and just a simple example of mapStateToProps as a bonus:
`
const mapStateToProps = ( {i18n: { currentLanguage, dictionaries }}, ownProps ) => ({
currentLanguage: currentLanguage,
dictionary: dictionaries[ currentLanguage ]
});
`#### Why?
With
`` you'll get:`
`
With
`translate` you'll get:
`
``PS You already have translatr as a dependency of redux-react-i18n in your node_modules