redux-polyglot
Advanced tools
+16
-6
@@ -46,10 +46,17 @@ 'use strict'; | ||
| var getPhrases = path(['polyglot', 'phrases']); | ||
| var getPolyglotScope = function getPolyglotScope(state, _ref) { | ||
| var _ref$polyglotScope = _ref.polyglotScope, | ||
| polyglotScope = _ref$polyglotScope === undefined ? '' : _ref$polyglotScope; | ||
| return polyglotScope === '' ? '' : polyglotScope + '.'; | ||
| return polyglotScope !== '' ? polyglotScope + '.' : ''; | ||
| }; | ||
| var getPolyglotOptions = function getPolyglotOptions(state, _ref2) { | ||
| var polyglotOptions = _ref2.polyglotOptions; | ||
| var getPolyglotOwnPhrases = function getPolyglotOwnPhrases(state, _ref2) { | ||
| var _ref2$ownPhrases = _ref2.ownPhrases, | ||
| ownPhrases = _ref2$ownPhrases === undefined ? '' : _ref2$ownPhrases; | ||
| return ownPhrases !== '' ? ownPhrases : ''; | ||
| }; | ||
| var getPolyglotOptions = function getPolyglotOptions(state, _ref3) { | ||
| var polyglotOptions = _ref3.polyglotOptions; | ||
| return polyglotOptions; | ||
@@ -65,4 +72,4 @@ }; | ||
| var getTranslation = (0, _reselect.createSelector)(getPolyglot, getPolyglotScope, function (p, scope) { | ||
| return function (text) { | ||
| var getTranslation = (0, _reselect.createSelector)(getPolyglot, getPolyglotScope, getPolyglotOwnPhrases, function (p, polyglotScope, ownPhrases) { | ||
| return function (polyglotKey) { | ||
| for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
@@ -72,3 +79,6 @@ args[_key - 1] = arguments[_key]; | ||
| return p.t.apply(p, [scope + text].concat(args)); | ||
| var fullPath = polyglotScope + polyglotKey; | ||
| var ownPhrase = ownPhrases !== '' ? ownPhrases[fullPath] : null; | ||
| return ownPhrase || p.t.apply(p, [fullPath].concat(args)); | ||
| }; | ||
@@ -75,0 +85,0 @@ }); |
@@ -23,6 +23,6 @@ 'use strict'; | ||
| var mapPolyglotToProps = function mapPolyglotToProps(polyglotScope) { | ||
| var mapPolyglotToProps = function mapPolyglotToProps(options) { | ||
| return function (state) { | ||
| return { | ||
| p: (0, _selectors.getP)(state, { polyglotScope: polyglotScope }) | ||
| p: (0, _selectors.getP)(state, options) | ||
| }; | ||
@@ -39,3 +39,4 @@ }; | ||
| var translate = function translate(fstArg, sndArg) { | ||
| if (fstArg === undefined && sndArg === undefined) return translateEnhancer('');else if ((0, _utils.isFunction)(fstArg)) return translateEnhancer('', fstArg);else if ((0, _utils.isString)(fstArg) && sndArg === undefined) return translateEnhancer(fstArg); | ||
| if (fstArg === undefined && sndArg === undefined) return translateEnhancer({});else if ((0, _utils.isFunction)(fstArg)) return translateEnhancer({}, fstArg);else if ((0, _utils.isString)(fstArg) && sndArg === undefined) return translateEnhancer({ polyglotScope: fstArg });else if ((0, _utils.isObject)(fstArg) && sndArg === undefined) return translateEnhancer(fstArg); | ||
| return translateEnhancer(fstArg, sndArg); | ||
@@ -42,0 +43,0 @@ }; |
+1
-1
| { | ||
| "name": "redux-polyglot", | ||
| "version": "0.5.3", | ||
| "version": "0.6.0", | ||
| "description": "Tool for using Polyglot.js with Redux", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
+29
-0
@@ -143,2 +143,3 @@ # redux-polyglot | ||
| translate('catalog')(Dummy); // curried with polyglotScope. | ||
| translate({ polyglotScope : 'some.nested.data', ownPhrases: 'some.nested.data.hello': 'Hi !', ... })(Dummy); // curried with object configuration. | ||
| ``` | ||
@@ -151,2 +152,30 @@ | ||
| ### Overwrite phrases | ||
| In some case, you should be able to replace some default phrases by others phrases. | ||
| For doing this, you have to define an object which contains your overwrited phrases. | ||
| This object is composed of : ``` { 'some.nested.data': 'phrase', ... }``` where `key` is the target path you want to replace and `value` ... the new value. | ||
| ##### with _getP()_ selector | ||
| Simply add `ownPhrases` property and set the new configuration like above to overwrite : | ||
| ```js | ||
| store.dispatch(setLanguage('en', { | ||
| some: { nested: { data: { hello: 'hello' } } } | ||
| })); | ||
| const p = getP(store.getState(), { | ||
| polyglotScope: 'some.nested.data', | ||
| ownPhrases: { 'some.nested.data.hello': 'Hi !' } | ||
| }); | ||
| console.log(p.tc('hello')) // => will return 'Hi !' | ||
| ``` | ||
| ##### with _translate()_ enhancer | ||
| Instead passing only _string_ as parameter : `translate('catalog', Dummy)`, pass a plain _object_ which contains `polyglotScope` and `ownPhrases` properties : | ||
| ```js | ||
| translate({ | ||
| polyglotScope : 'some.nested.data', | ||
| ownPhrases: { 'some.nested.data.catalog': 'Cars' } | ||
| }, Dummy); | ||
| console.log(p.tc('catalog')) // => will return 'Cars' | ||
| ``` | ||
| ### Use polyglot options | ||
@@ -153,0 +182,0 @@ if you want to use `onMissingKey`, `allowMissing` or `warn` [polyglot](http://airbnb.io/polyglot.js/) options, you can use the `createGetP` function to create a custom `getP`. |
23203
8.42%306
2.68%200
16.96%