react-localize-redux
Advanced tools
Comparing version 2.4.1 to 2.5.0
@@ -87,3 +87,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
exports.__esModule = true; | ||
exports.getTranslate = exports.getTranslationsForActiveLanguage = exports.customeEqualSelector = exports.getActiveLanguage = exports.getLanguages = exports.getTranslations = exports.setActiveLanguage = exports.setLanguages = exports.addTranslation = exports.localeReducer = exports.TRANSLATE = exports.SET_ACTIVE_LANGUAGE = exports.SET_LANGUAGES = exports.ADD_TRANSLATION = undefined; | ||
exports.getTranslate = exports.getTranslationsForActiveLanguage = exports.customeEqualSelector = exports.getActiveLanguage = exports.getLanguages = exports.getTranslations = exports.setActiveLanguage = exports.setLanguages = exports.addTranslationForLanguage = exports.addTranslation = exports.localeReducer = exports.TRANSLATE = exports.SET_ACTIVE_LANGUAGE = exports.SET_LANGUAGES = exports.ADD_TRANSLATION_FOR_LANGUGE = exports.ADD_TRANSLATION = undefined; | ||
@@ -104,2 +104,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var ADD_TRANSLATION = exports.ADD_TRANSLATION = '@@localize/ADD_TRANSLATION'; | ||
var ADD_TRANSLATION_FOR_LANGUGE = exports.ADD_TRANSLATION_FOR_LANGUGE = '@@localize/ADD_TRANSLATION_FOR_LANGUGE'; | ||
var SET_LANGUAGES = exports.SET_LANGUAGES = '@@localize/SET_LANGUAGES'; | ||
@@ -141,2 +142,16 @@ var SET_ACTIVE_LANGUAGE = exports.SET_ACTIVE_LANGUAGE = '@@localize/SET_ACTIVE_LANGUAGE'; | ||
return _extends({}, state, (0, _flat.flatten)(action.payload.translation, { safe: true })); | ||
case ADD_TRANSLATION_FOR_LANGUGE: | ||
var languageIndex = action.languageCodes.indexOf(action.payload.language); | ||
var flattenedTranslations = languageIndex >= 0 ? (0, _flat.flatten)(action.payload.translation) : {}; | ||
var languageTranslations = Object.keys(flattenedTranslations).reduce(function (prev, cur) { | ||
var _extends2; | ||
var translationValues = action.languageCodes.map(function (code, index) { | ||
var existingValues = state[cur] || []; | ||
return index === languageIndex ? flattenedTranslations[cur] : existingValues[index]; | ||
}); | ||
return _extends({}, prev, (_extends2 = {}, _extends2[cur] = translationValues, _extends2)); | ||
}, {}); | ||
return _extends({}, state, languageTranslations); | ||
default: | ||
@@ -147,4 +162,20 @@ return state; | ||
var localeReducer = exports.localeReducer = (0, _redux.combineReducers)({ languages: languages, translations: translations }); | ||
var initialState = { | ||
languages: [], | ||
translations: {} | ||
}; | ||
var localeReducer = exports.localeReducer = function localeReducer() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
var action = arguments[1]; | ||
var languageCodes = state.languages.map(function (language) { | ||
return language.code; | ||
}); | ||
return { | ||
languages: languages(state.languages, action), | ||
translations: translations(state.translations, _extends({}, action, { languageCodes: languageCodes })) | ||
}; | ||
}; | ||
/** | ||
@@ -160,2 +191,9 @@ * ACTION CREATORS | ||
var addTranslationForLanguage = exports.addTranslationForLanguage = function addTranslationForLanguage(translation, language) { | ||
return { | ||
type: ADD_TRANSLATION_FOR_LANGUGE, | ||
payload: { translation: translation, language: language } | ||
}; | ||
}; | ||
var setLanguages = exports.setLanguages = function setLanguages(languageCodes) { | ||
@@ -195,6 +233,12 @@ var activeLanguage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
// for translations data use keys for comparison | ||
// for translations data use a combination of keys and values for comparison | ||
if (isTranslationsData) { | ||
prev = Object.keys(prev).toString(); | ||
cur = Object.keys(cur).toString(); | ||
var prevKeys = Object.keys(prev).toString(); | ||
var curKeys = Object.keys(cur).toString(); | ||
var prevValues = Object.values(prev).toString(); | ||
var curValues = Object.values(cur).toString(); | ||
prev = prevKeys + ' - ' + prevValues; | ||
cur = curKeys + ' - ' + curValues; | ||
} | ||
@@ -210,5 +254,5 @@ | ||
return Object.keys(translations).reduce(function (prev, key) { | ||
var _extends2; | ||
var _extends3; | ||
return _extends({}, prev, (_extends2 = {}, _extends2[key] = translations[key][activeLanguageIndex], _extends2)); | ||
return _extends({}, prev, (_extends3 = {}, _extends3[key] = translations[key][activeLanguageIndex], _extends3)); | ||
}, {}); | ||
@@ -223,5 +267,5 @@ }); | ||
return value.reduce(function (prev, cur) { | ||
var _extends3; | ||
var _extends4; | ||
return _extends({}, prev, (_extends3 = {}, _extends3[cur] = (0, _utils.getLocalizedElement)(cur, translations, data), _extends3)); | ||
return _extends({}, prev, (_extends4 = {}, _extends4[cur] = (0, _utils.getLocalizedElement)(cur, translations, data), _extends4)); | ||
}, {}); | ||
@@ -307,2 +351,8 @@ } else { | ||
}); | ||
Object.defineProperty(exports, 'addTranslationForLanguage', { | ||
enumerable: true, | ||
get: function get() { | ||
return _locale.addTranslationForLanguage; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'setLanguages', { | ||
@@ -309,0 +359,0 @@ enumerable: true, |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("react-redux"),require("redux"),require("reselect")):"function"==typeof define&&define.amd?define(["react","react-redux","redux","reselect"],t):"object"==typeof exports?exports.ReactLocalizeRedux=t(require("react"),require("react-redux"),require("redux"),require("reselect")):e.ReactLocalizeRedux=t(e.React,e.ReactRedux,e.Redux,e.Reselect)}(this,function(e,t,n,r){return function(e){function t(r){if(n[r])return n[r].exports;var a=n[r]={i:r,l:!1,exports:{}};return e[r].call(a.exports,a,a.exports,t),a.l=!0,a.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=3)}([function(e,t,n){"use strict";function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments[1];switch(t.type){case f:var n=t.payload.languageCodes,r=t.payload.activeLanguage||n[0],a=n.indexOf(r);return n.map(function(e,t){return{code:e,active:t===a}});case g:return e.map(function(e){return e.code===t.payload.languageCode?o({},e,{active:!0}):o({},e,{active:!1})});default:return e}}function a(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments[1];switch(t.type){case s:return o({},e,(0,c.flatten)(t.payload.translation,{safe:!0}));default:return e}}t.__esModule=!0,t.getTranslate=t.getTranslationsForActiveLanguage=t.customeEqualSelector=t.getActiveLanguage=t.getLanguages=t.getTranslations=t.setActiveLanguage=t.setLanguages=t.addTranslation=t.localeReducer=t.TRANSLATE=t.SET_ACTIVE_LANGUAGE=t.SET_LANGUAGES=t.ADD_TRANSLATION=void 0;var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e};t.languages=r,t.translations=a;var u=n(8),c=n(5),i=n(9),l=n(4),s=t.ADD_TRANSLATION="@@localize/ADD_TRANSLATION",f=t.SET_LANGUAGES="@@localize/SET_LANGUAGES",g=t.SET_ACTIVE_LANGUAGE="@@localize/SET_ACTIVE_LANGUAGE",d=(t.TRANSLATE="@@localize/TRANSLATE",t.localeReducer=(0,u.combineReducers)({languages:r,translations:a}),t.addTranslation=function(e){return{type:s,payload:{translation:e}}},t.setLanguages=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return{type:f,payload:{languageCodes:e,activeLanguage:t}}},t.setActiveLanguage=function(e){return{type:g,payload:{languageCode:e}}},t.getTranslations=function(e){return e.translations}),p=t.getLanguages=function(e){return e.languages},v=t.getActiveLanguage=function(e){return p(e).find(function(e){return!0===e.active})},y=t.customeEqualSelector=(0,i.createSelectorCreator)(i.defaultMemoize,function(e,t){return!(Array.isArray(e)||"code,active"===Object.keys(e).toString())&&(t=Object.keys(t).toString(),e=Object.keys(e).toString()),t===e}),b=t.getTranslationsForActiveLanguage=y(v,p,d,function(e,t,n){var r=e.code,a=(0,l.getIndexForLanguageCode)(r,t);return Object.keys(n).reduce(function(e,t){var r;return o({},e,(r={},r[t]=n[t][a],r))},{})});t.getTranslate=(0,i.createSelector)(b,function(e){return function(t,n){if("string"==typeof t)return(0,l.getLocalizedElement)(t,e,n);if(Array.isArray(t))return t.reduce(function(t,r){var a;return o({},t,(a={},a[r]=(0,l.getLocalizedElement)(r,e,n),a))},{});throw new Error("react-localize-redux: Invalid key passed to getTranslate.")}})},function(t,n){t.exports=e},function(e,t,n){"use strict";t.__esModule=!0,t.localize=void 0;var r=n(1),a=(function(e){e&&e.__esModule}(r),n(7)),o=n(0),u=function(e){return function(t){var n=e?t[e]:t;return{currentLanguage:(0,o.getActiveLanguage)(n).code,translate:(0,o.getTranslate)(n)}}};t.localize=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return(0,a.connect)(u(t),null)(e)}},function(e,t,n){"use strict";t.__esModule=!0;var r=n(2);Object.defineProperty(t,"localize",{enumerable:!0,get:function(){return r.localize}});var a=n(0);Object.defineProperty(t,"localeReducer",{enumerable:!0,get:function(){return a.localeReducer}}),Object.defineProperty(t,"addTranslation",{enumerable:!0,get:function(){return a.addTranslation}}),Object.defineProperty(t,"setLanguages",{enumerable:!0,get:function(){return a.setLanguages}}),Object.defineProperty(t,"setActiveLanguage",{enumerable:!0,get:function(){return a.setActiveLanguage}}),Object.defineProperty(t,"getTranslate",{enumerable:!0,get:function(){return a.getTranslate}}),Object.defineProperty(t,"getActiveLanguage",{enumerable:!0,get:function(){return a.getActiveLanguage}}),Object.defineProperty(t,"getLanguages",{enumerable:!0,get:function(){return a.getLanguages}}),Object.defineProperty(t,"getTranslations",{enumerable:!0,get:function(){return a.getTranslations}})},function(e,t,n){"use strict";t.__esModule=!0,t.getIndexForLanguageCode=t.templater=t.hasHtmlTags=t.getLocalizedElement=void 0;var r=n(1),a=function(e){return e&&e.__esModule?e:{default:e}}(r),o=(t.getLocalizedElement=function(e,t,n){var r=t[e]||"Missing localized key: "+e,c=u(r,n);return o(c)?a.default.createElement("span",{dangerouslySetInnerHTML:{__html:c}}):c},t.hasHtmlTags=function(e){var t=/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+\s*|\s*)\/?>/;return e.search(t)>=0}),u=t.templater=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};for(var n in t){var r="\\${\\s*"+n+"\\s*}",a=new RegExp(r,"gmi");e=e.replace(a,t[n])}return e};t.getIndexForLanguageCode=function(e,t){return t.map(function(e){return e.code}).indexOf(e)}},function(e,t,n){function r(e,t){function n(e,c,i){i=i||1,Object.keys(e).forEach(function(l){var s=e[l],f=t.safe&&Array.isArray(s),g=Object.prototype.toString.call(s),d=o(s),p="[object Object]"===g||"[object Array]"===g,v=c?c+r+l:l;if(!f&&!d&&p&&Object.keys(s).length&&(!t.maxDepth||i<a))return n(s,v,i+1);u[v]=s})}t=t||{};var r=t.delimiter||".",a=t.maxDepth,u={};return n(e),u}function a(e,t){function n(e){var t=Number(e);return isNaN(t)||-1!==e.indexOf(".")?e:t}t=t||{};var r=t.delimiter||".",u=t.overwrite||!1,c={};return o(e)||"[object Object]"!==Object.prototype.toString.call(e)?e:(Object.keys(e).forEach(function(o){for(var i=o.split(r),l=n(i.shift()),s=n(i[0]),f=c;void 0!==s;){var g=Object.prototype.toString.call(f[l]),d="[object Object]"===g||"[object Array]"===g;if(!u&&!d&&void 0!==f[l])return;(u&&!d||!u&&null==f[l])&&(f[l]="number"!=typeof s||t.object?{}:[]),f=f[l],i.length>0&&(l=n(i.shift()),s=n(i[0]))}f[l]=a(e[o],t)}),c)}var o=n(6);e.exports=r;r.flatten=r,r.unflatten=a},function(e,t){function n(e){return!!e.constructor&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}function r(e){return"function"==typeof e.readFloatLE&&"function"==typeof e.slice&&n(e.slice(0,0))}/*! | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("react-redux"),require("redux"),require("reselect")):"function"==typeof define&&define.amd?define(["react","react-redux","redux","reselect"],t):"object"==typeof exports?exports.ReactLocalizeRedux=t(require("react"),require("react-redux"),require("redux"),require("reselect")):e.ReactLocalizeRedux=t(e.React,e.ReactRedux,e.Redux,e.Reselect)}(this,function(e,t,n,r){return function(e){function t(r){if(n[r])return n[r].exports;var a=n[r]={i:r,l:!1,exports:{}};return e[r].call(a.exports,a,a.exports,t),a.l=!0,a.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=3)}([function(e,t,n){"use strict";function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments[1];switch(t.type){case g:var n=t.payload.languageCodes,r=t.payload.activeLanguage||n[0],a=n.indexOf(r);return n.map(function(e,t){return{code:e,active:t===a}});case f:return e.map(function(e){return e.code===t.payload.languageCode?o({},e,{active:!0}):o({},e,{active:!1})});default:return e}}function a(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments[1];switch(t.type){case l:return o({},e,(0,u.flatten)(t.payload.translation,{safe:!0}));case s:var n=t.languageCodes.indexOf(t.payload.language),r=n>=0?(0,u.flatten)(t.payload.translation):{},a=Object.keys(r).reduce(function(a,u){var c,i=t.languageCodes.map(function(t,a){var o=e[u]||[];return a===n?r[u]:o[a]});return o({},a,(c={},c[u]=i,c))},{});return o({},e,a);default:return e}}t.__esModule=!0,t.getTranslate=t.getTranslationsForActiveLanguage=t.customeEqualSelector=t.getActiveLanguage=t.getLanguages=t.getTranslations=t.setActiveLanguage=t.setLanguages=t.addTranslationForLanguage=t.addTranslation=t.localeReducer=t.TRANSLATE=t.SET_ACTIVE_LANGUAGE=t.SET_LANGUAGES=t.ADD_TRANSLATION_FOR_LANGUGE=t.ADD_TRANSLATION=void 0;var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e};t.languages=r,t.translations=a;var u=(n(8),n(5)),c=n(9),i=n(4),l=t.ADD_TRANSLATION="@@localize/ADD_TRANSLATION",s=t.ADD_TRANSLATION_FOR_LANGUGE="@@localize/ADD_TRANSLATION_FOR_LANGUGE",g=t.SET_LANGUAGES="@@localize/SET_LANGUAGES",f=t.SET_ACTIVE_LANGUAGE="@@localize/SET_ACTIVE_LANGUAGE",d=(t.TRANSLATE="@@localize/TRANSLATE",{languages:[],translations:{}}),p=(t.localeReducer=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:d,t=arguments[1],n=e.languages.map(function(e){return e.code});return{languages:r(e.languages,t),translations:a(e.translations,o({},t,{languageCodes:n}))}},t.addTranslation=function(e){return{type:l,payload:{translation:e}}},t.addTranslationForLanguage=function(e,t){return{type:s,payload:{translation:e,language:t}}},t.setLanguages=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return{type:g,payload:{languageCodes:e,activeLanguage:t}}},t.setActiveLanguage=function(e){return{type:f,payload:{languageCode:e}}},t.getTranslations=function(e){return e.translations}),v=t.getLanguages=function(e){return e.languages},y=t.getActiveLanguage=function(e){return v(e).find(function(e){return!0===e.active})},A=t.customeEqualSelector=(0,c.createSelectorCreator)(c.defaultMemoize,function(e,t){if(!(Array.isArray(e)||"code,active"===Object.keys(e).toString())){var n=Object.keys(t).toString(),r=Object.keys(e).toString(),a=Object.values(t).toString(),o=Object.values(e).toString();t=n+" - "+a,e=r+" - "+o}return t===e}),L=t.getTranslationsForActiveLanguage=A(y,v,p,function(e,t,n){var r=e.code,a=(0,i.getIndexForLanguageCode)(r,t);return Object.keys(n).reduce(function(e,t){var r;return o({},e,(r={},r[t]=n[t][a],r))},{})});t.getTranslate=(0,c.createSelector)(L,function(e){return function(t,n){if("string"==typeof t)return(0,i.getLocalizedElement)(t,e,n);if(Array.isArray(t))return t.reduce(function(t,r){var a;return o({},t,(a={},a[r]=(0,i.getLocalizedElement)(r,e,n),a))},{});throw new Error("react-localize-redux: Invalid key passed to getTranslate.")}})},function(t,n){t.exports=e},function(e,t,n){"use strict";t.__esModule=!0,t.localize=void 0;var r=n(1),a=(function(e){e&&e.__esModule}(r),n(7)),o=n(0),u=function(e){return function(t){var n=e?t[e]:t;return{currentLanguage:(0,o.getActiveLanguage)(n).code,translate:(0,o.getTranslate)(n)}}};t.localize=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return(0,a.connect)(u(t),null)(e)}},function(e,t,n){"use strict";t.__esModule=!0;var r=n(2);Object.defineProperty(t,"localize",{enumerable:!0,get:function(){return r.localize}});var a=n(0);Object.defineProperty(t,"localeReducer",{enumerable:!0,get:function(){return a.localeReducer}}),Object.defineProperty(t,"addTranslation",{enumerable:!0,get:function(){return a.addTranslation}}),Object.defineProperty(t,"addTranslationForLanguage",{enumerable:!0,get:function(){return a.addTranslationForLanguage}}),Object.defineProperty(t,"setLanguages",{enumerable:!0,get:function(){return a.setLanguages}}),Object.defineProperty(t,"setActiveLanguage",{enumerable:!0,get:function(){return a.setActiveLanguage}}),Object.defineProperty(t,"getTranslate",{enumerable:!0,get:function(){return a.getTranslate}}),Object.defineProperty(t,"getActiveLanguage",{enumerable:!0,get:function(){return a.getActiveLanguage}}),Object.defineProperty(t,"getLanguages",{enumerable:!0,get:function(){return a.getLanguages}}),Object.defineProperty(t,"getTranslations",{enumerable:!0,get:function(){return a.getTranslations}})},function(e,t,n){"use strict";t.__esModule=!0,t.getIndexForLanguageCode=t.templater=t.hasHtmlTags=t.getLocalizedElement=void 0;var r=n(1),a=function(e){return e&&e.__esModule?e:{default:e}}(r),o=(t.getLocalizedElement=function(e,t,n){var r=t[e]||"Missing localized key: "+e,c=u(r,n);return o(c)?a.default.createElement("span",{dangerouslySetInnerHTML:{__html:c}}):c},t.hasHtmlTags=function(e){var t=/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+\s*|\s*)\/?>/;return e.search(t)>=0}),u=t.templater=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};for(var n in t){var r="\\${\\s*"+n+"\\s*}",a=new RegExp(r,"gmi");e=e.replace(a,t[n])}return e};t.getIndexForLanguageCode=function(e,t){return t.map(function(e){return e.code}).indexOf(e)}},function(e,t,n){function r(e,t){function n(e,c,i){i=i||1,Object.keys(e).forEach(function(l){var s=e[l],g=t.safe&&Array.isArray(s),f=Object.prototype.toString.call(s),d=o(s),p="[object Object]"===f||"[object Array]"===f,v=c?c+r+l:l;if(!g&&!d&&p&&Object.keys(s).length&&(!t.maxDepth||i<a))return n(s,v,i+1);u[v]=s})}t=t||{};var r=t.delimiter||".",a=t.maxDepth,u={};return n(e),u}function a(e,t){function n(e){var t=Number(e);return isNaN(t)||-1!==e.indexOf(".")?e:t}t=t||{};var r=t.delimiter||".",u=t.overwrite||!1,c={};return o(e)||"[object Object]"!==Object.prototype.toString.call(e)?e:(Object.keys(e).forEach(function(o){for(var i=o.split(r),l=n(i.shift()),s=n(i[0]),g=c;void 0!==s;){var f=Object.prototype.toString.call(g[l]),d="[object Object]"===f||"[object Array]"===f;if(!u&&!d&&void 0!==g[l])return;(u&&!d||!u&&null==g[l])&&(g[l]="number"!=typeof s||t.object?{}:[]),g=g[l],i.length>0&&(l=n(i.shift()),s=n(i[0]))}g[l]=a(e[o],t)}),c)}var o=n(6);e.exports=r;r.flatten=r,r.unflatten=a},function(e,t){function n(e){return!!e.constructor&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}function r(e){return"function"==typeof e.readFloatLE&&"function"==typeof e.slice&&n(e.slice(0,0))}/*! | ||
* Determine if an object is a Buffer | ||
@@ -3,0 +3,0 @@ * |
@@ -28,2 +28,8 @@ 'use strict'; | ||
}); | ||
Object.defineProperty(exports, 'addTranslationForLanguage', { | ||
enumerable: true, | ||
get: function get() { | ||
return _locale.addTranslationForLanguage; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'setLanguages', { | ||
@@ -30,0 +36,0 @@ enumerable: true, |
'use strict'; | ||
exports.__esModule = true; | ||
exports.getTranslate = exports.getTranslationsForActiveLanguage = exports.customeEqualSelector = exports.getActiveLanguage = exports.getLanguages = exports.getTranslations = exports.setActiveLanguage = exports.setLanguages = exports.addTranslation = exports.localeReducer = exports.TRANSLATE = exports.SET_ACTIVE_LANGUAGE = exports.SET_LANGUAGES = exports.ADD_TRANSLATION = undefined; | ||
exports.getTranslate = exports.getTranslationsForActiveLanguage = exports.customeEqualSelector = exports.getActiveLanguage = exports.getLanguages = exports.getTranslations = exports.setActiveLanguage = exports.setLanguages = exports.addTranslationForLanguage = exports.addTranslation = exports.localeReducer = exports.TRANSLATE = exports.SET_ACTIVE_LANGUAGE = exports.SET_LANGUAGES = exports.ADD_TRANSLATION_FOR_LANGUGE = exports.ADD_TRANSLATION = undefined; | ||
@@ -20,2 +20,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var ADD_TRANSLATION = exports.ADD_TRANSLATION = '@@localize/ADD_TRANSLATION'; | ||
var ADD_TRANSLATION_FOR_LANGUGE = exports.ADD_TRANSLATION_FOR_LANGUGE = '@@localize/ADD_TRANSLATION_FOR_LANGUGE'; | ||
var SET_LANGUAGES = exports.SET_LANGUAGES = '@@localize/SET_LANGUAGES'; | ||
@@ -57,2 +58,16 @@ var SET_ACTIVE_LANGUAGE = exports.SET_ACTIVE_LANGUAGE = '@@localize/SET_ACTIVE_LANGUAGE'; | ||
return _extends({}, state, (0, _flat.flatten)(action.payload.translation, { safe: true })); | ||
case ADD_TRANSLATION_FOR_LANGUGE: | ||
var languageIndex = action.languageCodes.indexOf(action.payload.language); | ||
var flattenedTranslations = languageIndex >= 0 ? (0, _flat.flatten)(action.payload.translation) : {}; | ||
var languageTranslations = Object.keys(flattenedTranslations).reduce(function (prev, cur) { | ||
var _extends2; | ||
var translationValues = action.languageCodes.map(function (code, index) { | ||
var existingValues = state[cur] || []; | ||
return index === languageIndex ? flattenedTranslations[cur] : existingValues[index]; | ||
}); | ||
return _extends({}, prev, (_extends2 = {}, _extends2[cur] = translationValues, _extends2)); | ||
}, {}); | ||
return _extends({}, state, languageTranslations); | ||
default: | ||
@@ -63,4 +78,20 @@ return state; | ||
var localeReducer = exports.localeReducer = (0, _redux.combineReducers)({ languages: languages, translations: translations }); | ||
var initialState = { | ||
languages: [], | ||
translations: {} | ||
}; | ||
var localeReducer = exports.localeReducer = function localeReducer() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
var action = arguments[1]; | ||
var languageCodes = state.languages.map(function (language) { | ||
return language.code; | ||
}); | ||
return { | ||
languages: languages(state.languages, action), | ||
translations: translations(state.translations, _extends({}, action, { languageCodes: languageCodes })) | ||
}; | ||
}; | ||
/** | ||
@@ -76,2 +107,9 @@ * ACTION CREATORS | ||
var addTranslationForLanguage = exports.addTranslationForLanguage = function addTranslationForLanguage(translation, language) { | ||
return { | ||
type: ADD_TRANSLATION_FOR_LANGUGE, | ||
payload: { translation: translation, language: language } | ||
}; | ||
}; | ||
var setLanguages = exports.setLanguages = function setLanguages(languageCodes) { | ||
@@ -111,6 +149,12 @@ var activeLanguage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
// for translations data use keys for comparison | ||
// for translations data use a combination of keys and values for comparison | ||
if (isTranslationsData) { | ||
prev = Object.keys(prev).toString(); | ||
cur = Object.keys(cur).toString(); | ||
var prevKeys = Object.keys(prev).toString(); | ||
var curKeys = Object.keys(cur).toString(); | ||
var prevValues = Object.values(prev).toString(); | ||
var curValues = Object.values(cur).toString(); | ||
prev = prevKeys + ' - ' + prevValues; | ||
cur = curKeys + ' - ' + curValues; | ||
} | ||
@@ -126,5 +170,5 @@ | ||
return Object.keys(translations).reduce(function (prev, key) { | ||
var _extends2; | ||
var _extends3; | ||
return _extends({}, prev, (_extends2 = {}, _extends2[key] = translations[key][activeLanguageIndex], _extends2)); | ||
return _extends({}, prev, (_extends3 = {}, _extends3[key] = translations[key][activeLanguageIndex], _extends3)); | ||
}, {}); | ||
@@ -139,5 +183,5 @@ }); | ||
return value.reduce(function (prev, cur) { | ||
var _extends3; | ||
var _extends4; | ||
return _extends({}, prev, (_extends3 = {}, _extends3[cur] = (0, _utils.getLocalizedElement)(cur, translations, data), _extends3)); | ||
return _extends({}, prev, (_extends4 = {}, _extends4[cur] = (0, _utils.getLocalizedElement)(cur, translations, data), _extends4)); | ||
}, {}); | ||
@@ -144,0 +188,0 @@ } else { |
@@ -28,2 +28,8 @@ 'use strict'; | ||
}); | ||
Object.defineProperty(exports, 'addTranslationForLanguage', { | ||
enumerable: true, | ||
get: function get() { | ||
return _locale.addTranslationForLanguage; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'setLanguages', { | ||
@@ -30,0 +36,0 @@ enumerable: true, |
'use strict'; | ||
exports.__esModule = true; | ||
exports.getTranslate = exports.getTranslationsForActiveLanguage = exports.customeEqualSelector = exports.getActiveLanguage = exports.getLanguages = exports.getTranslations = exports.setActiveLanguage = exports.setLanguages = exports.addTranslation = exports.localeReducer = exports.TRANSLATE = exports.SET_ACTIVE_LANGUAGE = exports.SET_LANGUAGES = exports.ADD_TRANSLATION = undefined; | ||
exports.getTranslate = exports.getTranslationsForActiveLanguage = exports.customeEqualSelector = exports.getActiveLanguage = exports.getLanguages = exports.getTranslations = exports.setActiveLanguage = exports.setLanguages = exports.addTranslationForLanguage = exports.addTranslation = exports.localeReducer = exports.TRANSLATE = exports.SET_ACTIVE_LANGUAGE = exports.SET_LANGUAGES = exports.ADD_TRANSLATION_FOR_LANGUGE = exports.ADD_TRANSLATION = undefined; | ||
@@ -20,2 +20,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var ADD_TRANSLATION = exports.ADD_TRANSLATION = '@@localize/ADD_TRANSLATION'; | ||
var ADD_TRANSLATION_FOR_LANGUGE = exports.ADD_TRANSLATION_FOR_LANGUGE = '@@localize/ADD_TRANSLATION_FOR_LANGUGE'; | ||
var SET_LANGUAGES = exports.SET_LANGUAGES = '@@localize/SET_LANGUAGES'; | ||
@@ -57,2 +58,16 @@ var SET_ACTIVE_LANGUAGE = exports.SET_ACTIVE_LANGUAGE = '@@localize/SET_ACTIVE_LANGUAGE'; | ||
return _extends({}, state, (0, _flat.flatten)(action.payload.translation, { safe: true })); | ||
case ADD_TRANSLATION_FOR_LANGUGE: | ||
var languageIndex = action.languageCodes.indexOf(action.payload.language); | ||
var flattenedTranslations = languageIndex >= 0 ? (0, _flat.flatten)(action.payload.translation) : {}; | ||
var languageTranslations = Object.keys(flattenedTranslations).reduce(function (prev, cur) { | ||
var _extends2; | ||
var translationValues = action.languageCodes.map(function (code, index) { | ||
var existingValues = state[cur] || []; | ||
return index === languageIndex ? flattenedTranslations[cur] : existingValues[index]; | ||
}); | ||
return _extends({}, prev, (_extends2 = {}, _extends2[cur] = translationValues, _extends2)); | ||
}, {}); | ||
return _extends({}, state, languageTranslations); | ||
default: | ||
@@ -63,4 +78,20 @@ return state; | ||
var localeReducer = exports.localeReducer = (0, _redux.combineReducers)({ languages: languages, translations: translations }); | ||
var initialState = { | ||
languages: [], | ||
translations: {} | ||
}; | ||
var localeReducer = exports.localeReducer = function localeReducer() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
var action = arguments[1]; | ||
var languageCodes = state.languages.map(function (language) { | ||
return language.code; | ||
}); | ||
return { | ||
languages: languages(state.languages, action), | ||
translations: translations(state.translations, _extends({}, action, { languageCodes: languageCodes })) | ||
}; | ||
}; | ||
/** | ||
@@ -76,2 +107,9 @@ * ACTION CREATORS | ||
var addTranslationForLanguage = exports.addTranslationForLanguage = function addTranslationForLanguage(translation, language) { | ||
return { | ||
type: ADD_TRANSLATION_FOR_LANGUGE, | ||
payload: { translation: translation, language: language } | ||
}; | ||
}; | ||
var setLanguages = exports.setLanguages = function setLanguages(languageCodes) { | ||
@@ -111,6 +149,12 @@ var activeLanguage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
// for translations data use keys for comparison | ||
// for translations data use a combination of keys and values for comparison | ||
if (isTranslationsData) { | ||
prev = Object.keys(prev).toString(); | ||
cur = Object.keys(cur).toString(); | ||
var prevKeys = Object.keys(prev).toString(); | ||
var curKeys = Object.keys(cur).toString(); | ||
var prevValues = Object.values(prev).toString(); | ||
var curValues = Object.values(cur).toString(); | ||
prev = prevKeys + ' - ' + prevValues; | ||
cur = curKeys + ' - ' + curValues; | ||
} | ||
@@ -126,5 +170,5 @@ | ||
return Object.keys(translations).reduce(function (prev, key) { | ||
var _extends2; | ||
var _extends3; | ||
return _extends({}, prev, (_extends2 = {}, _extends2[key] = translations[key][activeLanguageIndex], _extends2)); | ||
return _extends({}, prev, (_extends3 = {}, _extends3[key] = translations[key][activeLanguageIndex], _extends3)); | ||
}, {}); | ||
@@ -139,5 +183,5 @@ }); | ||
return value.reduce(function (prev, cur) { | ||
var _extends3; | ||
var _extends4; | ||
return _extends({}, prev, (_extends3 = {}, _extends3[cur] = (0, _utils.getLocalizedElement)(cur, translations, data), _extends3)); | ||
return _extends({}, prev, (_extends4 = {}, _extends4[cur] = (0, _utils.getLocalizedElement)(cur, translations, data), _extends4)); | ||
}, {}); | ||
@@ -144,0 +188,0 @@ } else { |
{ | ||
"name": "react-localize-redux", | ||
"version": "2.4.1", | ||
"description": "Dead simple localization for your React/Redux components", | ||
"version": "2.5.0", | ||
"description": "Localization library for React/Redux", | ||
"main": "lib/index", | ||
@@ -24,3 +24,4 @@ "jsnext:main": "es/index", | ||
"test": "jest", | ||
"test:dev": "jest --watch" | ||
"test:dev": "jest --watch", | ||
"deploy:docs": "mkdocs gh-deploy" | ||
}, | ||
@@ -68,3 +69,3 @@ "repository": { | ||
"cross-env": "^3.1.4", | ||
"enzyme": "^2.7.0", | ||
"enzyme": "^2.9.1", | ||
"express": "^4.14.0", | ||
@@ -75,7 +76,8 @@ "html-webpack-plugin": "^2.24.1", | ||
"progress-bar-webpack-plugin": "^1.9.1", | ||
"react": "^15.4.1", | ||
"react-addons-test-utils": "^15.4.1", | ||
"react-dom": "^15.4.1", | ||
"react": "^15.6.1", | ||
"react-addons-test-utils": "^15.6.0", | ||
"react-dom": "^15.6.1", | ||
"react-redux": "^5.0.1", | ||
"react-router": "^3.0.0", | ||
"react-test-renderer": "^15.6.1", | ||
"redux": "^3.6.0", | ||
@@ -82,0 +84,0 @@ "redux-devtools": "^3.3.1", |
393
README.md
@@ -1,6 +0,12 @@ | ||
![React Localize Redux](https://cdn-images-1.medium.com/max/600/1*3Hg5LMvLmWCmEg-ICeUtjg.png) | ||
<p align="center"> | ||
<a href="https://ryandrewjohnson.github.io/react-localize-redux/getting-started/"> | ||
<img alt="React Localize Redux" src="https://cdn-images-1.medium.com/max/600/1*3Hg5LMvLmWCmEg-ICeUtjg.png"> | ||
</a> | ||
</p> | ||
A collection of helpers for managing localized content in your React/Redux application. | ||
<p align="center"> | ||
Localization library for <a href="https://facebook.github.io/react">React</a>/<a href="http://redux.js.org/">Redux</a>. | ||
</p> | ||
<p> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/react-localize-redux"><img src="https://img.shields.io/npm/dm/react-localize-redux.svg?style=flat-square"></a> | ||
@@ -10,12 +16,2 @@ <a href="https://travis-ci.org/ryandrewjohnson/react-localize-redux"><img src="https://img.shields.io/travis/ryandrewjohnson/react-localize-redux/master.svg?style=flat-square"></a> | ||
## Table of Contents | ||
- [Installation](#installation) | ||
- [Getting Started](#getting-started) | ||
- [Features](#features) | ||
- [API](#api) | ||
- [Dead simple localization for your React components](https://medium.com/@ryandrewjohnson/adding-multi-language-support-to-your-react-redux-app-cf6e64250050) | ||
- [Migrating from v1 to v2?](MIGRATING.md) | ||
## Installation | ||
@@ -27,373 +23,24 @@ | ||
## Getting Started | ||
## Docs | ||
### 1. Add `localeReducer` to redux store. | ||
- [Getting Started](https://ryandrewjohnson.github.io/react-localize-redux/getting-started/) | ||
- [Formatting Translation Data](https://ryandrewjohnson.github.io/react-localize-redux/formatting-translation-data/) | ||
- [Features](https://ryandrewjohnson.github.io/react-localize-redux/features/) | ||
- API | ||
- [Action Creators](https://ryandrewjohnson.github.io/react-localize-redux/api/action-creators/) | ||
- [Selectors](https://ryandrewjohnson.github.io/react-localize-redux/api/selectors/) | ||
- [Higher Order Component](https://ryandrewjohnson.github.io/react-localize-redux/api/higher-order-component/) | ||
> NOTE: Although the example uses the `locale` prop for the reducer, you can name this prop whatever you like. Just ensure that you use the correct prop name when passing the state to selectors. | ||
## Articles | ||
```javascript | ||
... | ||
import { createStore, combineReducers } from 'redux'; | ||
import { localeReducer } from 'react-localize-redux'; | ||
- [Dead simple localization for your React components](https://medium.com/@ryandrewjohnson/adding-multi-language-support-to-your-react-redux-app-cf6e64250050) | ||
- [Migrating from v1 to v2?](MIGRATING.md) | ||
const store = createStore(combineReducers({ | ||
locale: localeReducer | ||
})); | ||
const App = props => { | ||
return ( | ||
<Provider store={ store }> | ||
... | ||
</Provider> | ||
); | ||
}; | ||
``` | ||
### 2. Set the supported languages | ||
Dispatch [setLanguages](#setlanguageslanguages-defaultactivelanguage) action creator and pass in the languages for your app. By default the first language in the array will be set as the active language. | ||
```javascript | ||
import { setLanguages } from 'react-localize-redux'; | ||
const languages = ['en', 'fr', 'es']; | ||
store.dispatch(setLanguages(languages)); | ||
``` | ||
To set a different default active language pass in the `language`. | ||
```javascript | ||
const languages = ['en', 'fr', 'es']; | ||
store.dispatch(setLanguages(languages, 'fr')); | ||
``` | ||
### 3. Add localized translation data | ||
Typically you will store your translation data in json files, but the data can also be a vanilla JS object. Once your translation data is in the correct format use the [addTranslation](#addtranslationdata) action creator. | ||
> NOTE: The following assumes you are using [webpack](https://webpack.github.io/) to bundle json | ||
```javascript | ||
import { addTranslation } from 'react-localize-redux'; | ||
const json = require('global.locale.json'); | ||
store.dispatch(addTranslation(json)); | ||
``` | ||
The json data should enforce the following format, where each translation string is a represented by a `{ key: value }` pair. | ||
The `value` is an array that should enforce the following... | ||
* Include a translation for each language your app supports. | ||
* The translation order matches the order of the languages in `setLanguages`. | ||
```json | ||
{ | ||
"greeting": [ | ||
"Hello", (en) | ||
"Bonjour", (fr) | ||
"Hola", (es) | ||
], | ||
"farwell": [ | ||
"Goodbye", (en) | ||
"Au revoir", (fr) | ||
"Adiós" (es) | ||
] | ||
} | ||
``` | ||
### 4. Change the current language | ||
Dispatch [setActiveLanguage](#setactivelanguagelanguage) action creator and pass the language. | ||
```javascript | ||
import { setActiveLanguage } from 'react-localize-redux'; | ||
store.dispatch(setActiveLanguage('fr')); | ||
``` | ||
### 5. Translate components | ||
If you have a component that is already using `connect` you can use the [getTranslate](#gettranslatestate) selector that returns the `translate` function. This function will return the localized string based on active language. | ||
```javascript | ||
import { getTranslate } from 'react-localize-redux'; | ||
const Greeting = ({ translate, currentLanguage }) => ( | ||
<div> | ||
<h1>{ translate('greeting') }</h1> | ||
<button>{ translate('farwell') }</button> | ||
</div> | ||
); | ||
const mapStateToProps = state => ({ | ||
translate: getTranslate(state.locale), | ||
currentLanguage: getActiveLanguage(state.locale).code | ||
}); | ||
export default connect(mapStateToProps)(Greeting); | ||
``` | ||
For components not already using `connect` instead use [localize](#localizecomponent-slice). This will automatically connect your component with the `translate` function and `currentLanguage` prop. | ||
```javascript | ||
import { localize } from 'react-localize-redux'; | ||
const Greeting = ({ translate, currentLanguage }) => ( | ||
<div> | ||
<h1>{ translate('greeting') }</h1> | ||
<button>{ translate('farwell') }</button> | ||
</div> | ||
); | ||
export default localize(Greeting, 'locale'); | ||
``` | ||
## Features | ||
### Include HTML in translations | ||
Include HTML in your translation strings and it will be rendered in your component. | ||
```json | ||
{ | ||
"google-link": [ | ||
"<a href='https://www.google.en/'>Google</a>", | ||
"<a href='https://www.google.fr/'>Google</a>" | ||
] | ||
} | ||
``` | ||
### Add dynamic content to translations | ||
You can insert dynamic content into your translation strings by inserting placeholders with the following format `${ placeholder }`. | ||
```json | ||
{ | ||
"greeting": [ | ||
"Hello ${ name }", | ||
"Bonjour ${ name }" | ||
] | ||
} | ||
``` | ||
Then pass in the data you want to swap in for placeholders to the `translate` function. | ||
```javascript | ||
<h1>{ translate('greeting', { name: 'Testy McTest' }) }</h1> | ||
``` | ||
### Supports nested translation data to avoid naming collisions | ||
```json | ||
{ | ||
"welcome": { | ||
"greeting": [ | ||
"Hello ${ name }!", | ||
"Bonjour ${ name }!" | ||
] | ||
}, | ||
"info": { | ||
"greeting": [ | ||
"Hello", | ||
"Bonjour" | ||
] | ||
} | ||
} | ||
``` | ||
```javascript | ||
<h1>{ translate('welcome.greeting', { name: 'Testy McTest' }) }</h1> | ||
<h1>{ translate('info.greeting') }</h1> | ||
``` | ||
### Pass multiple translations to child components | ||
A parent component that has added the `translate` function by using [getTranslate](#gettranslatestate) or [localize](#localizecomponent-slice) can easily pass multiple translations down to it's child components. Just pass the `translate` function an array of translation keys instead of a single key. | ||
```json | ||
{ | ||
"heading": ["Heading", "Heading French"], | ||
"article": { | ||
"title": ["Title", "Title French"], | ||
"author": ["By ${ name }", "By French ${ name }"], | ||
"desc": ["Description", "Description French"] | ||
} | ||
} | ||
``` | ||
```javascript | ||
const Article = props => ( | ||
<div> | ||
<h2>{ props['article.title'] }</h2> | ||
<h3>{ props['article.author'] }</h3> | ||
<p>{ props['article.desc'] }</p> | ||
</div> | ||
); | ||
const Page = ({ translate }) => ( | ||
<div> | ||
<h1>{ translate('heading') }</h1> | ||
<Article { ...translate(['article.title', 'article.author', 'article.desc'], { name: 'Ted' }) } /> | ||
</div> | ||
); | ||
``` | ||
### Load translation data on demand | ||
If you have a larger app you may want to break your translation data up into multiple files, or maybe your translation data is being loaded from a service. Either way you can call [addTranslation](#addtranslationdata) for each new translation file/service, and the new translation data will be merged with any existing data. | ||
Also If you are using a tool like [webpack](https://webpack.js.org) for bundling, then you can use [async code-splitting](https://webpack.js.org/guides/code-splitting-async/) to split translations across bundles, and async load them when you need them. | ||
## API | ||
### `getTranslate(state)` | ||
A selector that takes the localeReducer slice of your `state` and returns the translate function. This function will have access to any and all translations that were added with [addTranslation](#addtranslationdata). | ||
For single translation: | ||
returns `(key: string) => LocalizedElement | string` | ||
For multiple translations: | ||
returns `(key: []string) => { [key: string]: LocalizedElement | string` | ||
> Note: If a translation contains HTML a LocalizedElement will be used to render. In all other cases a string will be used. | ||
* `key: string|array` = A translation key or an array of translation keys. | ||
* `data: object` = Pass data to your [dynamic translation](#add-dynamic-content-to-translations) string. | ||
#### Usage (single translation): | ||
```javascript | ||
const Greeting = ({ translate }) => <h1>{ translate('greeting', { name: 'Testy McTest' }) }</h1> | ||
const mapStateToProps = state => ({ translate: getTranslate(state.locale) }); | ||
export default connect(mapStateToProps)(Greeting); | ||
``` | ||
#### Usage (multiple translations): | ||
See [Pass multiple translations to child components](#pass-multiple-translations-to-child-components). | ||
### `getActiveLanguage(state)` | ||
A selector that takes the localeReducer slice of your `state` and returns the currently active language object. | ||
returns `{ code: 'en', active: true }`; | ||
#### Usage: | ||
```javascript | ||
const Greeting = ({ currentLanguage }) => <h1>My language is: { currentLanguage }</h1> | ||
const mapStateToProps = state => ({ currentLanguage: getActiveLanguage(state.locale).code }); | ||
export default connect(mapStateToProps)(Greeting); | ||
``` | ||
### `getLanguages(state)` | ||
A selector that takes the localeReducer slice of your `state` and returns the languages you set. | ||
returns `[{ code: 'en', active: true }, { code: 'fr', active: false }]`; | ||
#### Usage: | ||
```javascript | ||
const LanguageSelector = ({ languages }) => ( | ||
<ul> | ||
{ languages.map(language => | ||
<a href={ `/${ language.code }` }>{ language.code }</a> | ||
)} | ||
</ul> | ||
) | ||
const mapStateToProps = state => ({ languages: getLanguages(state.locale) }); | ||
export default connect(mapStateToProps)(Greeting); | ||
``` | ||
### `getLanguages(state)` | ||
A selector that takes your redux `state` and returns the languages you set. | ||
returns `[{ code: 'en', active: true }, { code: 'fr', active: false }]`; | ||
#### Usage: | ||
```javascript | ||
const LanguageSelector = ({ languages }) => ( | ||
<ul> | ||
{ languages.map(language => | ||
<li><a href={ `/${ language.code }` }>{ language.code }</a></li> | ||
)} | ||
</ul> | ||
) | ||
const mapStateToProps = state => ({ languages: getLanguages(state) }); | ||
export default connect(mapStateToProps)(LanguageSelector); | ||
``` | ||
### `getTranslations(state)` | ||
A selector that takes your redux `state` and returns the raw translation data. | ||
### `localize(Component)` | ||
If you have a Component that is not using `connect` you can wrap it with `localize` to automatically add the `translate` function and `currentLanguage` prop. When using `combineReducers` to add `localeReducer` you must pass the `slice` param to `localize`, where `slice` is the name of the prop you used with `combineReducers` (e.g. locale). | ||
#### Usage: | ||
```javascript | ||
const Greeting = ({ translate, currentLanguage }) => ( | ||
<span> | ||
<h1>languageCode: { currentLanguage }</h1> | ||
<h2>{ translate('greeting', { name: 'Testy McTest' }) }</h2> | ||
</span> | ||
); | ||
export default localize(Greeting, 'locale'); | ||
``` | ||
### `setLanguages(languages, defaultActiveLanguage)` | ||
**Redux action creator** to set which languages you are supporting in your translations. If `defaultActiveLanguage` is not passed then the first language in the `languages` array will be used. | ||
#### Usage: | ||
```javascript | ||
const languages = ['en', 'fr', 'es']; | ||
store.dispatch(setLanguages(languages)); | ||
// if you wanted 'fr' to be default language instead of 'en' | ||
store.dispatch(setLanguages(languages, 'fr')); | ||
``` | ||
### `addTranslation(data)` | ||
**Redux action creator** to add new translation data to your redux store. Typically this data will be loaded from a json file, but can also be a plain JS object as long as it's structured properly. | ||
>**IMPORTANT:** The order of the translation strings in the array matters! The order **MUST** follow the order of the languages array passed to [setLanguages](#setlanguageslanguages-defaultactivelanguage). | ||
#### Usage: | ||
```javascript | ||
// assuming your app has set languages ['en', 'fr'] | ||
const welcomePageTranslations = { | ||
greeting: ['Hi!', 'Bonjour!'], | ||
farwell: ['Bye!', 'Au revoir!'] | ||
}; | ||
store.dispatch(addTranslation(welcomePageTranslations)); | ||
``` | ||
### `setActiveLanguage(language)` | ||
**Redux action creator** to change the current language being used. | ||
#### Usage: | ||
```javascript | ||
// assuming your app has set languages ['en', 'fr'] | ||
store.dispatch(setActiveLanguage('fr')); | ||
``` |
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
1069
56738
30
14
45