Skip to content
SDK for a React and Redux app architecture to easily connect to a Polyglot project.
JavaScript
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
package-lock.json
package.json
yarn.lock

README.md

Polyglot SDK for React+Redux

SDK for a React and Redux app architecture to easily connect to a Polyglot project.

Getting started

yarn add polyglot-react-redux-sdk

Requirements

  • React is a dependency for this package.
  • Redux is a dependency for this package.
  • A kind of redux thunk middleware is needed for the async actions to work (e.g. redux-thunk)

Usage

The following modules can be imported

reducer

The polyglot reducer that should be combined with the existing app reducers

actionTypes

The reducer action types:

  • SET_LOCALE
  • FETCH_PHRASES
  • FETCH_PHRASES_SUCCESS
  • FETCH_PHRASES_FAIL

actions

The reducer actions:

  • setLocale(locale)
  • fetchPhrases(url)

selectors

The reducer selectors:

  • getLocale
  • getPhrases
  • getPhrasesLoading
  • getPhrasesLoaded
  • getPhrasesError

usePolyglot

React hook to manage the phrases. It will fetch from the url if it's passed as an argument.

const {
  locale,
  phrases,
  phrasesLoading,
  phrasesLoaded,
  phrasesError,
  fetchPhrases,
  setLocale,
} = usePolyglot(defaultUrl, defaultLocale)

useTranslate

React hook to expose the a function to get a string (uses the polyglot.js counterpart under the hood). It can be used with or without a prefix.

const t = useTranslate()
<h1>{t('home.title')}</h1>

const t = useTranslate('home')
<h1>{t('title')}</h1>

Example

/*  store.js */

import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { reducer as polyglotReducer } from 'polyglot-react-redux-sdk'

const rootReducer = combineReducers({
  ...
  polyglot: polyglotReducer,
  ...
})

const initializeStore = () => (
  createStore(rootReducer, applyMiddleware(thunk));
)

export default initializeStore
...
/*  App.js */

import React from 'react'
import { usePolyglot } from 'polyglot-react-redux-sdk'

import Home from 'pages/Home'

const polyglotProjectUrl = 'https://d8wn722pvlrac.cloudfront.net/c81e728d1124862c/all.json'

const App = () => {
  const { phrasesLoaded } = usePolyglot(polyglotProjectUrl, 'en')

  return (
    phrasesLoaded ?
      <Home />
      :
      <h1>Phrases loading...</h1>
  );
}

export default App
/*  Home.js */

import React from 'react'

import { useTranslate } from 'polyglot-react-redux-sdk'

const Home = () => {
  const t = useTranslate('home')

  return (
    <div className="home-page">
	<h1 className="title">{t('title')}</h1>
    </div>
  )
}

export default Home

Credits

This is an adaptation of node-polyglot inspired by redux-polyglot. Thanks to both!

License

MIT

You can’t perform that action at this time.