diff --git a/CHANGELOG.md b/CHANGELOG.md index 650faa58..ab839fe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 10.11.3 + +- only apply initial values in useSSR, withSSR on i18next instances not being a clone (eg. created by express middleware on server) ==> don't apply on serverside + ### 10.11.2 - Reload translations whenever namespaces passed to useTranslation() change [878](https://github.com/i18next/react-i18next/pull/878) diff --git a/package.json b/package.json index 7b7e84fa..67d5b551 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-i18next", - "version": "10.11.2", + "version": "10.11.3", "description": "Internationalization for react done right. Using the i18next i18n ecosystem.", "main": "dist/commonjs/index.js", "types": "src/index.d.ts", diff --git a/react-i18next.js b/react-i18next.js index a70da39d..54ed492a 100644 --- a/react-i18next.js +++ b/react-i18next.js @@ -878,7 +878,10 @@ const _ref = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {}, i18nFromContext = _ref.i18n; - const i18n = i18nFromProps || i18nFromContext || getI18n(); // nextjs / SSR: getting data from next.js or other ssr stack + const i18n = i18nFromProps || i18nFromContext || getI18n(); // opt out if is a cloned instance, eg. created by i18next-express-middleware on request + // -> do not set initial stuff on server side + + if (i18n.options && i18n.options.isClone) return; // nextjs / SSR: getting data from next.js or other ssr stack if (initialI18nStore && !i18n.initializedStoreOnce) { i18n.services.resourceStore.data = initialI18nStore; diff --git a/react-i18next.min.js b/react-i18next.min.js index 278a5101..cfb22494 100644 --- a/react-i18next.min.js +++ b/react-i18next.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactI18next={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+""}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class S{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function j(e){g=e}function w(){return g}const N={type:"3rdParty",init(e){v(e.options.react),j(e)}};function x(e){return t=>new Promise(n=>{const r=E();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function E(){const e=w(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function $(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function C(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function T(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(C(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=C(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}`));return C(u[0])}function z(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=b()?t.useContext(y):{},s=o.i18n,c=o.defaultNS,l=r||s||w();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new S),!l){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const u=i({},O(),l.options.react),p=n.useSuspense,f=void 0===p?u.useSuspense:p;let d=e||c||l.options&&l.options.defaultNS;d="string"==typeof d?[d]:d||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(d);const g=(l.isInitialized||l.initializedStoreOnce)&&d.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,l));function h(){return{t:l.getFixedT(null,"fallback"===u.nsMode?d:d[0])}}const m=a(t.useState(h()),2),v=m[0],j=m[1];t.useEffect(()=>{let e=!0;const t=u.bindI18n,n=u.bindI18nStore;function r(){e&&j(h())}return g||f||P(l,d,()=>{e&&j(h())}),t&&l&&l.on(t,r),n&&l&&l.store.on(n,r),()=>{e=!1,t&&l&&t.split(" ").forEach(e=>l.off(e,r)),n&&l&&n.split(" ").forEach(e=>l.store.off(e,r))}},[d.join()]);const N=[v.t,l,g];if(N.t=v.t,N.i18n=l,N.ready=g,g)return N;if(!g&&!f)return N;throw new Promise(e=>{P(l,d,()=>{j(h()),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||w();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=b()?t.useContext(y):{},S=v.i18n,j=v.defaultNS,N=g||S||w();if(!N)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const x=h||N.t.bind(N)||(e=>e),E=i({},O(),N.options&&N.options.react),I=void 0!==s?s:E.defaultTransParent;let P=d||x.ns||j||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];const R=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}>`:`${t}<${n}>${e("",C(r),o+1,a)}`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:$("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else $("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}}.",r)}),t}("",r,0,E)||E.transEmptyNodeValue,z=E.hashTransKey,L=c||(z?z(R):R),B=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:R,count:a,ns:P}),V=L?x(L,B):R;return I?n.createElement(I,m,T(f||r,V,N,E,B)):T(f||r,V,N,E,B)},e.useTranslation=z,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(z(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,o.WrappedComponent=r,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(z(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.defaultNS,i=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t,defaultNS:r}},i)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=x(e),t.displayName=`withI18nextSSR(${R(e)})`,t.WrappedComponent=e,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=N,e.setDefaults=v,e.getDefaults=O,e.setI18n=j,e.getI18n=w,e.composeInitialProps=x,e.getInitialProps=E,Object.defineProperty(e,"__esModule",{value:!0})}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactI18next={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+""}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class S{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function j(e){g=e}function w(){return g}const N={type:"3rdParty",init(e){v(e.options.react),j(e)}};function x(e){return t=>new Promise(n=>{const r=E();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function E(){const e=w(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function $(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function C(e){return e&&(e.children||e.props&&e.props.children)}function A(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function T(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(C(t)?e(A(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(C(u)){const t=A(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}`));return A(u[0])}function z(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=b()?t.useContext(y):{},s=o.i18n,c=o.defaultNS,l=r||s||w();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new S),!l){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const u=i({},O(),l.options.react),p=n.useSuspense,f=void 0===p?u.useSuspense:p;let d=e||c||l.options&&l.options.defaultNS;d="string"==typeof d?[d]:d||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(d);const g=(l.isInitialized||l.initializedStoreOnce)&&d.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,l));function h(){return{t:l.getFixedT(null,"fallback"===u.nsMode?d:d[0])}}const m=a(t.useState(h()),2),v=m[0],j=m[1];t.useEffect(()=>{let e=!0;const t=u.bindI18n,n=u.bindI18nStore;function r(){e&&j(h())}return g||f||P(l,d,()=>{e&&j(h())}),t&&l&&l.on(t,r),n&&l&&l.store.on(n,r),()=>{e=!1,t&&l&&t.split(" ").forEach(e=>l.off(e,r)),n&&l&&n.split(" ").forEach(e=>l.store.off(e,r))}},[d.join()]);const N=[v.t,l,g];if(N.t=v.t,N.i18n=l,N.ready=g,g)return N;if(!g&&!f)return N;throw new Promise(e=>{P(l,d,()=>{j(h()),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||w();o.options&&o.options.isClone||(e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0))}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=b()?t.useContext(y):{},S=v.i18n,j=v.defaultNS,N=g||S||w();if(!N)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const x=h||N.t.bind(N)||(e=>e),E=i({},O(),N.options&&N.options.react),I=void 0!==s?s:E.defaultTransParent;let P=d||x.ns||j||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];const R=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(C(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof C(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}>`:`${t}<${n}>${e("",A(r),o+1,a)}`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:$("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else $("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}}.",r)}),t}("",r,0,E)||E.transEmptyNodeValue,z=E.hashTransKey,L=c||(z?z(R):R),B=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:R,count:a,ns:P}),V=L?x(L,B):R;return I?n.createElement(I,m,T(f||r,V,N,E,B)):T(f||r,V,N,E,B)},e.useTranslation=z,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(z(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,o.WrappedComponent=r,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(z(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.defaultNS,i=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t,defaultNS:r}},i)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=x(e),t.displayName=`withI18nextSSR(${R(e)})`,t.WrappedComponent=e,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=N,e.setDefaults=v,e.getDefaults=O,e.setI18n=j,e.getI18n=w,e.composeInitialProps=x,e.getInitialProps=E,Object.defineProperty(e,"__esModule",{value:!0})}); diff --git a/src/useSSR.js b/src/useSSR.js index 221f358d..31c0fa03 100644 --- a/src/useSSR.js +++ b/src/useSSR.js @@ -6,6 +6,10 @@ export function useSSR(initialI18nStore, initialLanguage, props = {}) { const { i18n: i18nFromContext } = getHasUsedI18nextProvider() ? useContext(I18nContext) : {}; const i18n = i18nFromProps || i18nFromContext || getI18n(); + // opt out if is a cloned instance, eg. created by i18next-express-middleware on request + // -> do not set initial stuff on server side + if (i18n.options && i18n.options.isClone) return; + // nextjs / SSR: getting data from next.js or other ssr stack if (initialI18nStore && !i18n.initializedStoreOnce) { i18n.services.resourceStore.data = initialI18nStore;