forked from Tiqa/redux-polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.js
40 lines (30 loc) · 1.18 KB
/
translate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import curry from 'lodash.curry';
import { connect } from 'react-redux';
import { createGetP } from './selectors';
import { isFunction, isString, isObject } from './private/utils';
const getDisplayName = Component => (
Component.displayName || Component.name || 'Component'
);
const mapPolyglotToProps = (options) => () => {
const getP = createGetP();
return state => ({
p: getP(state, options),
});
};
const translateEnhancer = curry((polyglotScope, Component) => {
const Connected = connect(mapPolyglotToProps(polyglotScope), () => ({}))(Component);
Connected.displayName = `Translated(${getDisplayName(Connected.WrappedComponent)})`;
return Connected;
});
const translate = (fstArg, sndArg) => {
if (fstArg === undefined && sndArg === undefined)
return translateEnhancer({});
else if (isFunction(fstArg))
return translateEnhancer({}, fstArg);
else if (isString(fstArg) && sndArg === undefined)
return translateEnhancer({ polyglotScope: fstArg });
else if (isObject(fstArg) && sndArg === undefined)
return translateEnhancer(fstArg);
return translateEnhancer(fstArg, sndArg);
};
export default translate;