-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
react-i18next.min.js
1 lines (1 loc) · 13.3 KB
/
react-i18next.min.js
1
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).ReactI18next={},e.React)}(this,(function(e,n){"use strict";function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var s=t({area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,link:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0}),a=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function r(e){var n={type:"tag",name:"",voidElement:!1,attrs:{},children:[]},t=e.match(/<\/?([^\s]+?)[/\s>]/);if(t&&(n.name=t[1],(s[t[1]]||"/"===e.charAt(e.length-2))&&(n.voidElement=!0),n.name.startsWith("!--"))){var r=e.indexOf("--\x3e");return{type:"comment",comment:-1!==r?e.slice(4,r):""}}for(var i=new RegExp(a),o=null;null!==(o=i.exec(e));)if(o[0].trim())if(o[1]){var l=o[1].trim(),c=[l,""];l.indexOf("=")>-1&&(c=l.split("=")),n.attrs[c[0]]=c[1],i.lastIndex--}else o[2]&&(n.attrs[o[2]]=o[3].trim().substring(1,o[3].length-1));return n}var i=/<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,o=/^\s*$/,l=Object.create(null);var c=function(e,n){n||(n={}),n.components||(n.components=l);var t,s=[],a=[],c=-1,u=!1;if(0!==e.indexOf("<")){var p=e.indexOf("<");s.push({type:"text",content:-1===p?e:e.substring(0,p)})}return e.replace(i,(function(i,l){if(u){if(i!=="</"+t.name+">")return;u=!1}var p,d="/"!==i.charAt(1),f=i.startsWith("\x3c!--"),m=l+i.length,g=e.charAt(m);if(f){var h=r(i);return c<0?(s.push(h),s):((p=a[c]).children.push(h),s)}if(d&&(c++,"tag"===(t=r(i)).type&&n.components[t.name]&&(t.type="component",u=!0),t.voidElement||u||!g||"<"===g||t.children.push({type:"text",content:e.slice(m,e.indexOf("<",m))}),0===c&&s.push(t),(p=a[c-1])&&p.children.push(t),a[c]=t),(!d||t.voidElement)&&(c>-1&&(t.voidElement||t.name===i.slice(2,-1))&&(c--,t=-1===c?s:a[c]),!u&&"<"!==g&&g)){p=-1===c?s:a[c].children;var y=e.indexOf("<",m),x=e.slice(m,-1===y?void 0:y);o.test(x)&&(x=" "),(y>-1&&c+p.length>=0||" "!==x)&&p.push({type:"text",content:x})}})),s};const u=function(e){for(var n=arguments.length,t=new Array(n>1?n-1:0),s=1;s<n;s++)t[s-1]=arguments[s];e?.services?.logger?.forward?e.services.logger.forward(t,"warn","react-i18next::",!0):e?.services?.logger?.warn?(y(t[0])&&(t[0]=`react-i18next:: ${t[0]}`),e.services.logger.warn(...t)):console?.warn&&(y(t[0])&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t))},p={},d=function(e){for(var n=arguments.length,t=new Array(n>1?n-1:0),s=1;s<n;s++)t[s-1]=arguments[s];y(t[0])&&p[t[0]]||(y(t[0])&&(p[t[0]]=new Date),u(e,...t))},f=(e,n)=>()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout((()=>{e.off("initialized",t)}),0),n()};e.on("initialized",t)}},m=(e,n,t)=>{e.loadNamespaces(n,f(e,t))},g=(e,n,t,s)=>{if(y(t)&&(t=[t]),e.options.preload&&e.options.preload.indexOf(n)>-1)return m(e,t,s);t.forEach((n=>{e.options.ns.indexOf(n)<0&&e.options.ns.push(n)})),e.loadLanguages(n,f(e,s))},h=e=>e.displayName||e.name||(y(e)&&e.length>0?e:"Unknown"),y=e=>"string"==typeof e,x=e=>"object"==typeof e&&null!==e,v=/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g,b={"&":"&","&":"&","<":"<","<":"<",">":">",">":">","'":"'","'":"'",""":'"',""":'"'," ":" "," ":" ","©":"©","©":"©","®":"®","®":"®","…":"…","…":"…","/":"/","/":"/"},E=e=>b[e];let O={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transWrapTextNodes:"",transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0,unescape:e=>e.replace(v,E)};const w=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};O={...O,...e}},N=()=>O;let $;const k=e=>{$=e},I=()=>$,S=(e,n)=>{if(!e)return!1;const t=e.props?.children??e.children;return n?t.length>0:!!t},j=e=>{if(!e)return[];const n=e.props?.children??e.children;return e.props?.i18nIsDynamicList?R(n):n},R=e=>Array.isArray(e)?e:[e],T=(e,t,s,a)=>{if(!e)return"";let r="";const i=R(e),o=t?.transSupportBasicHtmlNodes?t.transKeepBasicHtmlNodesFor??[]:[];return i.forEach(((e,i)=>{if(y(e))r+=`${e}`;else if(n.isValidElement(e)){const{props:n,type:l}=e,c=Object.keys(n).length,u=o.indexOf(l)>-1,p=n.children;if(p||!u||c)if(!p&&(!u||c)||n.i18nIsDynamicList)r+=`<${i}></${i}>`;else if(u&&1===c&&y(p))r+=`<${l}>${p}</${l}>`;else{const e=T(p,t,s,a);r+=`<${i}>${e}</${i}>`}else r+=`<${l}/>`}else if(null===e)u(s,"Trans: the passed in value is invalid - seems you passed in a null child.");else if(x(e)){const{format:n,...t}=e,i=Object.keys(t);if(1===i.length){const e=n?`${i[0]}, ${n}`:i[0];r+=`{{${e}}}`}else u(s,"react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",e,a)}else u(s,"Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",e,a)})),r},C=(e,t,s,a,r,i)=>{if(""===t)return[];const o=a.transKeepBasicHtmlNodesFor||[],l=t&&new RegExp(o.map((e=>`<${e}`)).join("|")).test(t);if(!e&&!l&&!i)return[t];const u={},p=e=>{R(e).forEach((e=>{y(e)||(S(e)?p(j(e)):x(e)&&!n.isValidElement(e)&&Object.assign(u,e))}))};p(e);const d=c(`<0>${t}</0>`),f={...u,...r},m=(e,t,s)=>{const a=j(e),r=h(a,t.children,s);return(e=>Array.isArray(e)&&e.every(n.isValidElement))(a)&&0===r.length||e.props?.i18nIsDynamicList?a:r},g=(e,t,s,a,r)=>{e.dummy?(e.children=t,s.push(n.cloneElement(e,{key:a},r?void 0:t))):s.push(...n.Children.map([e],(e=>{const s={...e.props};return delete s.i18nIsDynamicList,n.createElement(e.type,{...s,key:a,ref:e.ref},r?null:t)})))},h=(t,r,c)=>{const u=R(t);return R(r).reduce(((t,r,p)=>{const d=r.children?.[0]?.content&&s.services.interpolator.interpolate(r.children[0].content,f,s.language);if("tag"===r.type){let i=u[parseInt(r.name,10)];1!==c.length||i||(i=c[0][r.name]),i||(i={});const v=0!==Object.keys(r.attrs).length?((e,n)=>{const t={...n};return t.props=Object.assign(e.props,n.props),t})({props:r.attrs},i):i,b=n.isValidElement(v),E=b&&S(r,!0)&&!r.voidElement,O=l&&x(v)&&v.dummy&&!b,w=x(e)&&Object.hasOwnProperty.call(e,r.name);if(y(v)){const e=s.services.interpolator.interpolate(v,f,s.language);t.push(e)}else if(S(v)||E){const e=m(v,r,c);g(v,e,t,p)}else if(O){const e=h(u,r.children,c);g(v,e,t,p)}else if(Number.isNaN(parseFloat(r.name)))if(w){const e=m(v,r,c);g(v,e,t,p,r.voidElement)}else if(a.transSupportBasicHtmlNodes&&o.indexOf(r.name)>-1)if(r.voidElement)t.push(n.createElement(r.name,{key:`${r.name}-${p}`}));else{const e=h(u,r.children,c);t.push(n.createElement(r.name,{key:`${r.name}-${p}`},e))}else if(r.voidElement)t.push(`<${r.name} />`);else{const e=h(u,r.children,c);t.push(`<${r.name}>${e}</${r.name}>`)}else if(x(v)&&!b){const e=r.children[0]?d:null;e&&t.push(e)}else g(v,d,t,p,1!==r.children.length||!d)}else if("text"===r.type){const e=a.transWrapTextNodes,o=i?a.unescape(s.services.interpolator.interpolate(r.content,f,s.language)):s.services.interpolator.interpolate(r.content,f,s.language);e?t.push(n.createElement(e,{key:`${r.name}-${p}`},o)):t.push(o)}return t}),[])},v=h([{dummy:!0,children:e||[]}],d,R(e||[]));return j(v[0])},A=(e,t,s)=>{const a=e.key||t,r=n.cloneElement(e,{key:a});if(!r.props||!r.props.children||s.indexOf(`${t}/>`)<0&&s.indexOf(`${t} />`)<0)return r;return n.createElement((function(){return n.createElement(n.Fragment,null,r)}))},P=(e,n,t)=>e?Array.isArray(e)?((e,n)=>e.map(((e,t)=>A(e,t,n))))(e,n):x(e)?((e,n)=>{const t={};return Object.keys(e).forEach((s=>{Object.assign(t,{[s]:A(e[s],s,n)})})),t})(e,n):(d(t,"<Trans /> component prop expects an object or an array"),null):null;function L(e){let{children:t,count:s,parent:a,i18nKey:r,context:i,tOptions:o={},values:l,defaults:c,components:u,ns:p,i18n:f,t:m,shouldUnescape:g,...h}=e;const x=f||I();if(!x)return d(x,"You will need to pass in an i18next instance by using i18nextReactModule"),t;const v=m||x.t.bind(x)||(e=>e),b={...N(),...x.options?.react};let E=p||v.ns||x.options?.defaultNS;E=y(E)?[E]:E||["translation"];const O=T(t,b,x,r),w=c||O||b.transEmptyNodeValue||r,{hashTransKey:$}=b,k=r||($?$(O||w):O||w);x.options?.interpolation?.defaultVariables&&(l=l&&Object.keys(l).length>0?{...l,...x.options.interpolation.defaultVariables}:{...x.options.interpolation.defaultVariables});const S=l||void 0!==s&&!x.options?.interpolation?.alwaysFormat||!t?o.interpolation:{interpolation:{...o.interpolation,prefix:"#$?",suffix:"?$#"}},j={...o,context:i||o.context,count:s,...l,...S,defaultValue:w,ns:E},R=k?v(k,j):w,A=P(u,R,x),L=C(A||t,R,x,b,j,g),V=a??b.defaultTransParent;return V?n.createElement(V,h,L):L}const V={type:"3rdParty",init(e){w(e.options.react),k(e)}},z=n.createContext();class F{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach((e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)}))}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}const U=e=>async n=>({...await(e.getInitialProps?.(n))??{},...B()}),B=()=>{const e=I(),n=e.reportNamespaces?.getUsedNamespaces()??[],t={},s={};return e.languages.forEach((t=>{s[t]={},n.forEach((n=>{s[t][n]=e.getResourceBundle(t,n)||{}}))})),t.initialI18nStore=s,t.initialLanguage=e.language,t};const D=(e,n,t,s)=>e.getFixedT(n,t,s),K=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{i18n:s}=t,{i18n:a,defaultNS:r}=n.useContext(z)||{},i=s||a||I();if(i&&!i.reportNamespaces&&(i.reportNamespaces=new F),!i){d(i,"You will need to pass in an i18next instance by using initReactI18next");const e=(e,n)=>y(n)?n:x(n)&&y(n.defaultValue)?n.defaultValue:Array.isArray(e)?e[e.length-1]:e,n=[e,{},!1];return n.t=e,n.i18n={},n.ready=!1,n}i.options.react?.wait&&d(i,"It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.");const o={...N(),...i.options.react,...t},{useSuspense:l,keyPrefix:c}=o;let u=e||r||i.options?.defaultNS;u=y(u)?[u]:u||["translation"],i.reportNamespaces.addUsedNamespaces?.(u);const p=(i.isInitialized||i.initializedStoreOnce)&&u.every((e=>function(e,n){let t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return n.languages&&n.languages.length?n.hasLoadedNamespace(e,{lng:t.lng,precheck:(n,s)=>{if(t.bindI18n?.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!s(n.isLanguageChangingTo,e))return!1}}):(d(n,"i18n.languages were undefined or empty",n.languages),!0)}(e,i,o))),f=((e,t,s,a)=>n.useCallback(D(e,t,s,a),[e,t,s,a]))(i,t.lng||null,"fallback"===o.nsMode?u:u[0],c),h=()=>f,v=()=>D(i,t.lng||null,"fallback"===o.nsMode?u:u[0],c),[b,E]=n.useState(h);let O=u.join();t.lng&&(O=`${t.lng}${O}`);const w=((e,t)=>{const s=n.useRef();return n.useEffect((()=>{s.current=e}),[e,t]),s.current})(O),$=n.useRef(!0);n.useEffect((()=>{const{bindI18n:e,bindI18nStore:n}=o;$.current=!0,p||l||(t.lng?g(i,t.lng,u,(()=>{$.current&&E(v)})):m(i,u,(()=>{$.current&&E(v)}))),p&&w&&w!==O&&$.current&&E(v);const s=()=>{$.current&&E(v)};return e&&i?.on(e,s),n&&i?.store.on(n,s),()=>{$.current=!1,i&&e?.split(" ").forEach((e=>i.off(e,s))),n&&i&&n.split(" ").forEach((e=>i.store.off(e,s)))}}),[i,O]),n.useEffect((()=>{$.current&&p&&E(h)}),[i,c,p]);const k=[b,i,p];if(k.t=b,k.i18n=i,k.ready=p,p)return k;if(!p&&!l)return k;throw new Promise((e=>{t.lng?g(i,t.lng,u,(()=>e())):m(i,u,(()=>e()))}))};const W=function(e,t){let s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const{i18n:a}=s,{i18n:r}=n.useContext(z)||{},i=a||r||I();i.options?.isClone||(e&&!i.initializedStoreOnce&&(i.services.resourceStore.data=e,i.options.ns=Object.values(e).reduce(((e,n)=>(Object.keys(n).forEach((n=>{e.indexOf(n)<0&&e.push(n)})),e)),i.options.ns),i.initializedStoreOnce=!0,i.isInitialized=!0),t&&!i.initializedLanguageOnce&&(i.changeLanguage(t),i.initializedLanguageOnce=!0))};e.I18nContext=z,e.I18nextProvider=function(e){let{i18n:t,defaultNS:s,children:a}=e;const r=n.useMemo((()=>({i18n:t,defaultNS:s})),[t,s]);return n.createElement(z.Provider,{value:r},a)},e.Trans=function(e){let{children:t,count:s,parent:a,i18nKey:r,context:i,tOptions:o={},values:l,defaults:c,components:u,ns:p,i18n:d,t:f,shouldUnescape:m,...g}=e;const{i18n:h,defaultNS:y}=n.useContext(z)||{},x=d||h||I(),v=f||x?.t.bind(x);return L({children:t,count:s,parent:a,i18nKey:r,context:i,tOptions:o,values:l,defaults:c,components:u,ns:p||v?.ns||y||x?.options?.defaultNS,i18n:x,t:f,shouldUnescape:m,...g})},e.TransWithoutContext=L,e.Translation=e=>{let{ns:n,children:t,...s}=e;const[a,r,i]=K(n,s);return t(a,{i18n:r,lng:r.language},i)},e.composeInitialProps=U,e.date=()=>"",e.getDefaults=N,e.getI18n=I,e.getInitialProps=B,e.initReactI18next=V,e.number=()=>"",e.plural=()=>"",e.select=()=>"",e.selectOrdinal=()=>"",e.setDefaults=w,e.setI18n=k,e.time=()=>"",e.useSSR=W,e.useTranslation=K,e.withSSR=()=>function(e){function t(t){let{initialI18nStore:s,initialLanguage:a,...r}=t;return W(s,a),n.createElement(e,{...r})}return t.getInitialProps=U(e),t.displayName=`withI18nextSSR(${h(e)})`,t.WrappedComponent=e,t},e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(s){function a(a){let{forwardedRef:r,...i}=a;const[o,l,c]=K(e,{...i,keyPrefix:t.keyPrefix}),u={...i,t:o,i18n:l,tReady:c};return t.withRef&&r?u.ref=r:!t.withRef&&r&&(u.forwardedRef=r),n.createElement(s,u)}a.displayName=`withI18nextTranslation(${h(s)})`,a.WrappedComponent=s;return t.withRef?n.forwardRef(((e,t)=>n.createElement(a,Object.assign({},e,{forwardedRef:t})))):a}}}));