redux-polyglot
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -8,29 +8,17 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _actions = require('./actions'); | ||
// eslint-disable-next-line valid-typeof | ||
var is = function is(type) { | ||
return function (x) { | ||
return (typeof x === 'undefined' ? 'undefined' : _typeof(x)) === type; | ||
}; | ||
}; | ||
var _utils = require('./private/utils'); | ||
var isString = is('string'); | ||
var isFunction = is('function'); | ||
var isObject = is('object'); | ||
var isArray = Array.isArray; | ||
var checkParams = function checkParams(catchedAction, getLocale, getPhrases) { | ||
if (!catchedAction || !getLocale || !getPhrases) throw new Error('polyglotMiddleware : missing parameters.'); | ||
if (!isString(catchedAction) && !isArray(catchedAction)) { | ||
if (!(0, _utils.isString)(catchedAction) && !(0, _utils.isArray)(catchedAction)) { | ||
throw new Error('polyglotMiddleware : first parameter must be a string or an array of string.'); | ||
} | ||
if (!isFunction(getLocale)) throw new Error('polyglotMiddleware : second parameter must be a function.'); | ||
if (!isFunction(getPhrases)) throw new Error('polyglotMiddleware : third parameter must be a function.'); | ||
if (!(0, _utils.isFunction)(getLocale)) throw new Error('polyglotMiddleware : second parameter must be a function.'); | ||
if (!(0, _utils.isFunction)(getPhrases)) throw new Error('polyglotMiddleware : third parameter must be a function.'); | ||
}; | ||
var getIsValidPolyglotReducer = function getIsValidPolyglotReducer(state) { | ||
return !!(state && isObject(state.polyglot)); | ||
return !!(state && (0, _utils.isObject)(state.polyglot)); | ||
}; | ||
@@ -43,3 +31,3 @@ var checkState = function checkState(state) { | ||
checkParams(catchedAction, getLocale, getPhrases); | ||
var actions = isArray(catchedAction) ? catchedAction : [catchedAction]; | ||
var actions = (0, _utils.isArray)(catchedAction) ? catchedAction : [catchedAction]; | ||
return function (_ref) { | ||
@@ -46,0 +34,0 @@ var dispatch = _ref.dispatch; |
@@ -39,34 +39,47 @@ 'use strict'; | ||
var getPhrases = path(['polyglot', 'phrases']); | ||
var getPolyglotScope = function getPolyglotScope(state) { | ||
var polyglotScope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
return polyglotScope === '' ? '' : polyglotScope + '.'; | ||
}; | ||
var getPolyglot = (0, _reselect.createSelector)(getLocale, getPhrases, function (locale, phrases) { | ||
var p = new _nodePolyglot2.default({ | ||
return new _nodePolyglot2.default({ | ||
locale: locale, | ||
phrases: phrases | ||
}); | ||
return p; | ||
}); | ||
var getTranslation = (0, _reselect.createSelector)(getPolyglot, function (p) { | ||
return p.t.bind(p); | ||
var getTranslation = (0, _reselect.createSelector)(getPolyglot, getPolyglotScope, function (p, scope) { | ||
return function (text) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
return p.t.apply(p, [scope + text].concat(args)); | ||
}; | ||
}); | ||
var getTranslationMorphed = function getTranslationMorphed(state) { | ||
var getTranslationMorphed = function getTranslationMorphed() { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return function (f) { | ||
return (0, _redux.compose)(f, getTranslation(state)); | ||
return (0, _redux.compose)(f, getTranslation.apply(undefined, args)); | ||
}; | ||
}; | ||
var getTranslationUpperCased = function getTranslationUpperCased(state) { | ||
return getTranslationMorphed(state)(toUpper); | ||
var getTranslationUpperCased = function getTranslationUpperCased() { | ||
return getTranslationMorphed.apply(undefined, arguments)(toUpper); | ||
}; | ||
var getTranslationCapitalized = function getTranslationCapitalized(state) { | ||
return getTranslationMorphed(state)(capitalize); | ||
var getTranslationCapitalized = function getTranslationCapitalized() { | ||
return getTranslationMorphed.apply(undefined, arguments)(capitalize); | ||
}; | ||
var getP = function getP(state) { | ||
var getP = function getP(state, polyglotScope) { | ||
if (!getLocale(state) || !getPhrases(state)) return undefined; | ||
return _extends({}, getPolyglot(state), { | ||
t: getTranslation(state), | ||
tc: getTranslationCapitalized(state), | ||
tu: getTranslationUpperCased(state), | ||
tm: getTranslationMorphed(state) | ||
return _extends({}, getPolyglot(state, polyglotScope), { | ||
t: getTranslation(state, polyglotScope), | ||
tc: getTranslationCapitalized(state, polyglotScope), | ||
tu: getTranslationUpperCased(state, polyglotScope), | ||
tm: getTranslationMorphed(state, polyglotScope) | ||
}); | ||
@@ -73,0 +86,0 @@ }; |
@@ -7,2 +7,6 @@ 'use strict'; | ||
var _lodash = require('lodash.curry'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _reactRedux = require('react-redux'); | ||
@@ -12,15 +16,29 @@ | ||
var getDisplayName = function getDisplayName(WrappedComponent) { | ||
return WrappedComponent.displayName || WrappedComponent.name || 'Component'; | ||
var _utils = require('./private/utils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var getDisplayName = function getDisplayName(Component) { | ||
return Component.displayName || Component.name || 'Component'; | ||
}; | ||
var mapStateToProps = function mapStateToProps(state) { | ||
return { p: (0, _selectors.getP)(state) }; | ||
var mapPolyglotToProps = function mapPolyglotToProps(polyglotScope) { | ||
return function (state) { | ||
return { | ||
p: (0, _selectors.getP)(state, polyglotScope) | ||
}; | ||
}; | ||
}; | ||
var translate = function translate(Component) { | ||
var Connected = (0, _reactRedux.connect)(mapStateToProps)(Component); | ||
var translateEnhancer = (0, _lodash2.default)(function (polyglotScope, Component) { | ||
var Connected = (0, _reactRedux.connect)(mapPolyglotToProps(polyglotScope))(Component); | ||
Connected.displayName = 'Translated(' + getDisplayName(Connected.WrappedComponent) + ')'; | ||
return Connected; | ||
}); | ||
var translate = function translate(fstArg, sndArg) { | ||
if ((0, _utils.isFunction)(fstArg)) return translateEnhancer('', fstArg);else if ((0, _utils.isString)(fstArg) && sndArg === undefined) return translateEnhancer(fstArg); | ||
return translateEnhancer(fstArg, sndArg); | ||
}; | ||
exports.default = translate; |
{ | ||
"name": "redux-polyglot", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "Tool for using Polyglot.js with Redux", | ||
@@ -33,3 +33,3 @@ "main": "./dist/index.js", | ||
"name": "Guillaume Arm", | ||
"email": "guillaume.arm@redpelicans.com" | ||
"email": "garm@student.42.fr" | ||
}, | ||
@@ -60,2 +60,3 @@ { | ||
"dependencies": { | ||
"lodash.curry": "^4.1.1", | ||
"node-polyglot": "^2.0.0", | ||
@@ -62,0 +63,0 @@ "redux": "^3.6.0", |
@@ -98,2 +98,15 @@ # redux-polyglot | ||
there is an optional parameter to getP(), he's name is polyglotScope. | ||
this is allow you to automatically 'aim' a scope in your phrases object. | ||
for example : | ||
```js | ||
store.dispatch(setLanguage('en', { | ||
some: { nested: { data: { hello: 'hello' } } } | ||
})); | ||
const p = getP(store.getState(), 'some.nested.data'); | ||
console.log(p.tc('hello')) // => will return 'Hello' | ||
``` | ||
#### Getting current locale | ||
@@ -123,4 +136,12 @@ `getLocale(state)` selector returns current language. | ||
you can select a `polyglotScope` with `translate('scope', Component)` | ||
```js | ||
// all this lines return an enhanced Dummy component | ||
translate(Dummy); | ||
translate('catalog', Dummy); // with polyglotScope | ||
translate('catalog')(Dummy); // curried with polyglotScope. | ||
``` | ||
##### get locale in a component | ||
You can also use the `getLocale()` selector inside a [mapStateToProps](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) from react-redux. | ||
You can use the `getLocale()` selector inside a [mapStateToProps](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) from react-redux. | ||
@@ -127,0 +148,0 @@ Proptype: ````locale: PropTypes.string,```` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17154
12
242
155
6
+ Addedlodash.curry@^4.1.1
+ Addedlodash.curry@4.1.1(transitive)