Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
redux-polyglot
Advanced tools
Toolset (actions, reducer, middleware, enhancer, selectors) to help use Polyglot with Redux.
npm install --save redux-polyglot
First of all, you need the polyglot reducer in your rootReducer :
import { createStore, combineReducers } from 'redux';
import { polyglotReducer } from 'redux-polyglot';
const rootReducer = combineReducers({
...yourReducers,
polyglot: polyglotReducer,
});
const store = createStore(rootReducer, {});
You can use redux-polyglot without his middleware, for this you need the setLanguage()
action creator :
setLanguage :: (String, Object) -> Action
Example:
import { setLanguage } from 'redux-polyglot';
store.dispatch(setLanguage('en', { yolo: 'yolo' }));
second parameter should be polyglot phrases
(see polyglot documentation)
note: if language phrases already exists, this will overwrite the corresponding object state.
The createPolyglotMiddleware()
function allow you to automatically update language and phrases by listening to specific action(s).
The middleware catches specific action(s), and find the locale in the payload, and then [asynchronously] load the polyglot phrases
(with Promise).
It takes 3 parameters and return a middleware :
actionToCatch :: String | Array<String>
getLocale :: Object -> String
getPhrases :: String -> Promise Object
setLocale
) and return a Promise of Object ( Object should be polyglot phrases
)the middleware will catch actionToCatch
import { createStore, combineReducers, applyMiddleware } from 'redux';
const polyglotMiddleware = createPolyglotMiddleware(
'ACTION_TO_CATCH',
action => action.payload.locale,
locale => new Promise(resolve => {
// perform async here
resolve({
hello: 'bonjour',
});
}),
)
const store = createStore(rootReducer, {}, applyMiddleware(polyglotMiddleware));
you can catch more than one action passing an array of action types:
const polyglotMiddleware = createPolyglotMiddleware(
['FIRST_ACTION_TO_CATCH', 'SECOND_ACTION_TO_CATCH'],
getLocale,
getPhrases,
)
note: if language has not changed, nothing happens.
You can use the getP(state)
selector.
It returns an object with 4 functions inside :
t
function)(see polyglot documentation)
there is an optional parameter to getP().
this is allow you to automatically 'aim' a scope in your phrases object using polyglotScope
property.
for example :
store.dispatch(setLanguage('en', {
some: { nested: { data: { hello: 'hello' } } }
}));
const p = getP(store.getState(), { polyglotScope: 'some.nested.data' });
console.log(p.tc('hello')) // => will return 'Hello'
getLocale(state)
selector returns current language.
You can use connect()
from react-redux
, and the getP() selector, to get the p
prop in your component.
Proptype:
p: PropTypes.shape({
t: PropTypes.func.isRequired,
tc: PropTypes.func.isRequired,
tu: PropTypes.func.isRequired,
tm: PropTypes.func.isRequired,
}),
props.p
can be also be provided easily to a component with the translate enhancer :
import translate from 'redux-polyglot/translate';
const DummyComponentWithPProps = translate(DummyComponent);
you can select a polyglotScope
with translate('scope', Component)
// all this lines return an enhanced Dummy component
translate(Dummy);
translate('catalog', Dummy); // with polyglotScope
translate()(Dummy); // curried
translate('catalog')(Dummy); // curried with polyglotScope.
You can use the getLocale()
selector inside a mapStateToProps from react-redux.
Proptype: locale: PropTypes.string,
if you want to use onMissingKey
, allowMissing
or warn
polyglot options, you can use the createGetP
function to create a custom getP
.
usage :
import { createGetP } from 'redux-polyglot';
const options = {
allowMissing: true,
}
export const getP = createGetP(options);
These folks keep the project moving and are resources for help:
FAQs
Tool for using Polyglot.js with Redux
The npm package redux-polyglot receives a total of 280 weekly downloads. As such, redux-polyglot popularity was classified as not popular.
We found that redux-polyglot demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.