multilingual-translate
Version:
Add multilingual support to your react app with 4 easy steps. Built with React hooks and love.
1 lines • 3.85 kB
JavaScript
module.exports=function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=1)}([function(t,e){t.exports=require("react")},function(t,e,n){"use strict";n.r(e),n.d(e,"useTranslate",(function(){return l}));var r=n(0),o=n.n(r);function a(t,e,n,r,o,a,u){try{var i=t[a](u),c=i.value}catch(t){return void n(t)}i.done?e(c):Promise.resolve(c).then(r,o)}function u(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],r=!0,o=!1,a=void 0;try{for(var u,i=t[Symbol.iterator]();!(r=(u=i.next()).done)&&(n.push(u.value),!e||n.length!==e);r=!0);}catch(t){o=!0,a=t}finally{try{r||null==i.return||i.return()}finally{if(o)throw a}}return n}(t,e)||function(t,e){if(!t)return;if("string"==typeof t)return i(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return i(t,e)}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var c=Object(r.createContext)(),l=function(){var t=Object(r.useContext)(c);return[function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(0===Object.keys(t.translation).length)return"";var r=t.translation,o=e.split(".");return o.forEach((function(t){r=r[t]})),"string"!=typeof r&&"number"!=typeof r&&"boolean"!=typeof r?n||"No translation found":r},t.onChangeTranslationLanguage]};e.default=function(t){var e=t.children,n=t.defLang,i=t.folderURL,l=u(Object(r.useState)({}),2),f=l[0],s=l[1],d=u(Object(r.useState)(!0),2),p=d[0],v=d[1],y=u(Object(r.useState)(localStorage.getItem("defLang")||n||"en"),2),g=y[0],b=y[1],h=i||"translations/";Object(r.useEffect)((function(){m()}),[]);var m=function(){var t,e=(t=regeneratorRuntime.mark((function t(){var e,n,r,o=arguments;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=o.length>0&&void 0!==o[0]?o[0]:g,v(!0),t.prev=2,t.next=5,fetch("".concat(h,"/").concat(e,".json"));case 5:return n=t.sent,t.next=8,n.json();case 8:r=t.sent,s(r),b(e),v(!1),localStorage.setItem("defLang",e),t.next=20;break;case 15:t.prev=15,t.t0=t.catch(2),v(!1),console.error("Error in fetching/parsing the translations/".concat(e,".json translation file")),console.error(t.t0);case 20:case"end":return t.stop()}}),t,null,[[2,15]])})),function(){var e=this,n=arguments;return new Promise((function(r,o){var u=t.apply(e,n);function i(t){a(u,r,o,i,c,"next",t)}function c(t){a(u,r,o,i,c,"throw",t)}i(void 0)}))});return function(){return e.apply(this,arguments)}}();return o.a.createElement(c.Provider,{value:{translation:f,isTranslationLoading:p,translationLanguage:g,onChangeTranslationLanguage:m}},e)}}]);