项目作者: iamolegga

项目描述 :
translations utils for react-redux apps
高级语言: TypeScript
项目地址: git://github.com/iamolegga/redux-translations.git
创建时间: 2017-06-30T16:57:16Z
项目社区:https://github.com/iamolegga/redux-translations

开源协议:MIT License

下载


redux-translations

Translations utils for react-redux apps

Greenkeeper badge
npm
npm
Travis
Codecov
license
Commitizen friendly
Libraries.io for GitHub

Install

  1. npm i redux-translations

Usage

Translations middleware

Create and config translations middleware and apply it.

  1. import { createTranslationsMiddleware } from 'redux-translations';
  2. // Change this function to yours
  3. const functionThatReturnsPromiseWithDictionary = language =>
  4. Promise.resolve(
  5. language === 'en' ? { hello: 'Hello!' } : { hello: 'Привет!' }
  6. );
  7. const translationsMiddleware = createTranslationsMiddleware(
  8. functionThatReturnsPromiseWithDictionary
  9. );
  10. const store = createStore(rootReducer, applyMiddleware(translationsMiddleware));

Translations props

Wrap component with withTranslations function:

  1. import withTranslations from 'redux-translations';
  2. const MyComponent = ({
  3. dictionary,
  4. currentLang,
  5. loadingLang,
  6. switchLang,
  7. }) =>
  8. <div>
  9. Translated text: { dictionary['hello'] }
  10. <br />
  11. Current language: { currentLang }
  12. <br />
  13. Loading language: { loadingLang }
  14. <button
  15. onClick={ () => switchLang('en') }
  16. >English</button>
  17. <br />
  18. <button
  19. onClick={ () => switchLang('ru') }
  20. >Russian</button>
  21. </div>
  22. const MyComponentTranslated = withTranslations(MyComponent);

You can change language not only in react-component:

  1. import { switchLangActionCreator } from 'redux-translations';
  2. store.dispatch(switchLangActionCreator('en'));

API

createTranslationsMiddleware(getDictionary, [options], [initialState])

Function, that creates redux-middleware for translations. Has next arguments:

  1. getDictionary (Function) - function with one argument type of string - language, that user is switching to, and returns promise with dictionary object.

  2. [options] (Object) - options object with next optional fields:

    • cache (Boolean) - should cache results of getDictionary, and do not call it if dictionary is already loaded. Default true.
    • updateCacheOnSwitch (Boolean) - when cache is true, should switch immediately to cached dictionary, but load dictionary in background one more time and replace old with the new one. Default false.
    • startSwitchCallback (Function) - callback for every language switching start. Run exactly in switch event, without waiting for fetching dictionary. Takes next arguments: loadingLang (String) and store. Default undefined.
    • endSwitchCallback (Function) - callback for every language switching end. Run exactly after fetching dictionary. Takes next arguments: loadedLang (String), dictionary (Object) and store. Default undefined.
  3. [initialState] (Object) - initial inner state object with next optional fields:

    • dictionaries (Object) - hash-table of dictionaries, where key is language name and value is dictionary. Default {}.
    • currentLang (String) - current language with fetched dictionary. Default null.
    • loadingLang (String) - language that user is switching to, but not fetched dictionary yet. Default null.

withTranslations(Component, [copyStaticMethods])

React component class wrapper that adds next props to wrapping component class (actually it returns new component class):

  1. currentLang (String) - language, which dictionary is currently using.

  2. loadingLang (String) - language, which dictionary is currently loading.

  3. dictionary (Object) - object, that is returned by getDictionary.

  4. switchLang (Function) - function, that switch language to passed one.

Arguments:

  1. Component (Function) - component that depends on props, listed above.

  2. copyStaticMethods (Boolean) - whether to copy static methods of Component or not. Default true.

switchLangActionCreator(language)

Redux action creator - function with one argument type of string, returns flux standard action (FSA), that you can dispatch whereever in your app (for example, when initialising your app).

patchState(changes, [updateComponents])

Patch translations inner state without dispatching redux action. Could be useful for server-side rendering or another cases where store.dispatch function is unreachable. Returns Promise, resolved when all components are updated (if updateComponents === true) or immediately. Has next arguments:

  1. changes (Object) - partial initial inner state object with next optional fields:

    • dictionaries (Object) - hash-table of dictionaries, where key is language name and value is dictionary.
    • currentLang (String) - current language with fetched dictionary.
    • loadingLang (String) - language that user is switching to, but not fetched dictionary yet.
  2. updateComponents (Boolean) - whether to update components or not.