From 5215a4d9d7b7a77f454e5fe56a69b66286d45f38 Mon Sep 17 00:00:00 2001 From: Jan Muehlemann Date: Mon, 8 Mar 2021 08:10:02 +0100 Subject: [PATCH] rebuild --- CHANGELOG.md | 4 ++++ react-i18next.js | 10 +++++++++- react-i18next.min.js | 2 +- src/useTranslation.js | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35a40672..9d6931a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 11.8.9 + +- Fix allow to replace i18n in provider with useTranslation hook [1273](https://github.com/i18next/react-i18next/pull/1273) + ### 11.8.8 - typescript: Allow `TFuncKey` to be used without specifying the namespace, in the same way TFunction and useTranslation work [1262](https://github.com/i18next/react-i18next/pull/1262) diff --git a/react-i18next.js b/react-i18next.js index d62dccbf..42e2f81f 100644 --- a/react-i18next.js +++ b/react-i18next.js @@ -856,7 +856,15 @@ return i18n.store.off(e, boundReset); }); }; - }, [namespaces.join()]); + }, [i18n, namespaces.join()]); + var isInitial = React.useRef(true); + React.useEffect(function () { + if (isMounted.current && !isInitial.current) { + setT(getT()); + } + + isInitial.current = false; + }, [i18n]); var ret = [t.t, i18n, ready]; ret.t = t.t; ret.i18n = i18n; diff --git a/react-i18next.min.js b/react-i18next.min.js index e984bf7f..3a2c584a 100644 --- a/react-i18next.min.js +++ b/react-i18next.min.js @@ -1 +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=e||self).ReactI18next={},e.React)}(this,(function(e,n){"use strict";var t="default"in n?n.default:n;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function i(e,n){for(var t=0;t=0||(i[t]=e[t]);return i}(e,n);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(i[t]=e[t])}return i}function u(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,n){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var t=[],r=!0,i=!1,o=void 0;try{for(var a,c=e[Symbol.iterator]();!(r=(a=c.next()).done)&&(t.push(a.value),!n||t.length!==n);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==c.return||c.return()}finally{if(i)throw o}}return t}(e,n)||function(e,n){if(!e)return;if("string"==typeof e)return l(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return l(e,n)}(e,n)||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 l(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,g=Object.create?Object.create(null):{};function m(e,n,t,r,i){var o=n.indexOf("<",r),a=n.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&t+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}var y,h=function(e,n){n||(n={}),n.components||(n.components=g);var t,r=[],i=-1,o=[],a={},c=!1;return e.replace(d,(function(s,u){if(c){if(s!=="")return;c=!1}var l,d="/"!==s.charAt(1),g=0===s.indexOf("\x3c!--"),y=u+s.length,h=e.charAt(y);d&&!g&&(i++,"tag"===(t=function(e){var n,t=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(p,(function(o){if("="===o)return r=!0,void t++;r?0===t?((f[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[n]=o.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(i.attrs[n]=n),n=o),t++,r=!1})),i}(s)).type&&n.components[t.name]&&(t.type="component",c=!0),t.voidElement||c||!h||"<"===h||m(t.children,e,i,y,n.ignoreWhitespace),a[t.tagName]=t,0===i&&r.push(t),(l=o[i-1])&&l.children.push(t),o[i]=t),(g||!d||t.voidElement)&&(g||i--,!c&&"<"!==h&&h&&m(l=-1===i?r:o[i].children,e,i,y,n.ignoreWhitespace))})),!r.length&&e.length&&m(r,e,0,0,n.ignoreWhitespace),r},v={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},b=t.createContext();function O(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};v=c(c({},v),e)}function w(){return v}var j=function(){function e(){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e),this.usedNamespaces={}}var n,t,r;return n=e,(t=[{key:"addUsedNamespaces",value:function(e){var n=this;e.forEach((function(e){n.usedNamespaces[e]||(n.usedNamespaces[e]=!0)}))}},{key:"getUsedNamespaces",value:function(){return Object.keys(this.usedNamespaces)}}])&&i(n.prototype,t),r&&i(n,r),e}();function S(e){y=e}function E(){return y}var N={type:"3rdParty",init:function(e){O(e.options.react),S(e)}};function x(e){return function(n){return new Promise((function(t){var r=I();e.getInitialProps?e.getInitialProps(n).then((function(e){t(c(c({},e),r))})):t(r)}))}}function I(){var e=E(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach((function(t){r[t]={},n.forEach((function(n){r[t][n]=e.getResourceBundle(t,n)||{}}))})),t.initialI18nStore=r,t.initialLanguage=e.language,t}function k(){if(console&&console.warn){for(var e,n=arguments.length,t=new Array(n),r=0;r2&&void 0!==arguments[2]?arguments[2]:{};if(!n.languages||!n.languages.length)return R("i18n.languages were undefined or empty",n.languages),!0;var r=n.languages[0],i=!!n.options&&n.options.fallbackLng,o=n.languages[n.languages.length-1];if("cimode"===r.toLowerCase())return!0;var a=function(e,t){var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!a(n.isLanguageChangingTo,e))&&(!!n.hasResourceBundle(r,e)||(!n.services.backendConnector.backend||!(!a(r,e)||i&&!a(o,e))))}function T(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function L(e,n){if(!e)return!1;var t=e.props?e.props.children:e.children;return n?t.length>0:!!t}function z(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function D(e){return Array.isArray(e)?e:[e]}function B(e,n,i,o,a){if(""===n)return[];var s=o.transKeepBasicHtmlNodesFor||[],u=n&&new RegExp(s.join("|")).test(n);if(!e&&!u)return[n];var l={};!function e(n){D(n).forEach((function(n){"string"!=typeof n&&(L(n)?e(z(n)):"object"!==r(n)||t.isValidElement(n)||Object.assign(l,n))}))}(e);var f=i.services.interpolator.interpolate(n,c(c({},l),a),i.language),p=h("<0>".concat(f,""));function d(e,n,r){var i=z(e),o=m(i,n.children,r);return function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every((function(e){return t.isValidElement(e)}))}(i)&&0===o.length?i:o}function g(e,n,r,i,o){e.dummy&&(e.children=n),r.push(t.cloneElement(e,c(c({},e.props),{},{key:i}),o?void 0:n))}function m(n,i,a){var l=D(n);return D(i).reduce((function(n,i,f){var p,y,h,v=i.children&&i.children[0]&&i.children[0].content;if("tag"===i.type){var b=l[parseInt(i.name,10)];!b&&1===a.length&&a[0][i.name]&&(b=a[0][i.name]),b||(b={});var O=0!==Object.keys(i.attrs).length?(p={props:i.attrs},(h=c({},y=b)).props=Object.assign(p.props,y.props),h):b,w=t.isValidElement(O),j=w&&L(i,!0)&&!i.voidElement,S=u&&"object"===r(O)&&O.dummy&&!w,E="object"===r(e)&&null!==e&&Object.hasOwnProperty.call(e,i.name);if("string"==typeof O)n.push(O);else if(L(O)||j){g(O,d(O,i,a),n,f)}else if(S){var N=m(l,i.children,a);n.push(t.cloneElement(O,c(c({},O.props),{},{key:f}),N))}else if(Number.isNaN(parseFloat(i.name))){if(E)g(O,d(O,i,a),n,f,i.voidElement);else if(o.transSupportBasicHtmlNodes&&s.indexOf(i.name)>-1)if(i.voidElement)n.push(t.createElement(i.name,{key:"".concat(i.name,"-").concat(f)}));else{var x=m(l,i.children,a);n.push(t.createElement(i.name,{key:"".concat(i.name,"-").concat(f)},x))}else if(i.voidElement)n.push("<".concat(i.name," />"));else{var I=m(l,i.children,a);n.push("<".concat(i.name,">").concat(I,""))}}else if("object"!==r(O)||w)1===i.children.length&&v?n.push(t.cloneElement(O,c(c({},O.props),{},{key:f}),v)):n.push(t.cloneElement(O,c(c({},O.props),{},{key:f})));else{var k=i.children[0]?v:null;k&&n.push(k)}}else"text"===i.type&&n.push(i.content);return n}),[])}return z(m([{dummy:!0,children:e}],p,D(e||[]))[0])}function U(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=t.i18n,i=n.useContext(b)||{},o=i.i18n,a=i.defaultNS,s=r||o||E();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){R("You will need to pass in an i18next instance by using initReactI18next");var l=function(e){return Array.isArray(e)?e[e.length-1]:e},f=[l,{},!1];return f.t=l,f.i18n={},f.ready=!1,f}s.options.react&&void 0!==s.options.react.wait&&R("It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.");var p=c(c(c({},w()),s.options.react),t),d=p.useSuspense,g=e||a||s.options&&s.options.defaultNS;g="string"==typeof g?[g]:g||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(g);var m=(s.isInitialized||s.initializedStoreOnce)&&g.every((function(e){return A(e,s,p)}));function y(){return{t:s.getFixedT(null,"fallback"===p.nsMode?g:g[0])}}var h=n.useState(y()),v=u(h,2),O=v[0],S=v[1],N=n.useRef(!0);n.useEffect((function(){var e=p.bindI18n,n=p.bindI18nStore;function t(){N.current&&S(y())}return N.current=!0,m||d||C(s,g,(function(){N.current&&S(y())})),e&&s&&s.on(e,t),n&&s&&s.store.on(n,t),function(){N.current=!1,e&&s&&e.split(" ").forEach((function(e){return s.off(e,t)})),n&&s&&n.split(" ").forEach((function(e){return s.store.off(e,t)}))}}),[g.join()]);var x=[O.t,s,m];if(x.t=O.t,x.i18n=s,x.ready=m,m)return x;if(!m&&!d)return x;throw new Promise((function(e){C(s,g,(function(){e()}))}))}function V(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=r.i18n,o=n.useContext(b)||{},a=o.i18n,c=i||a||E();c.options&&c.options.isClone||(e&&!c.initializedStoreOnce&&(c.services.resourceStore.data=e,c.options.ns=Object.values(e).reduce((function(e,n){return Object.keys(n).forEach((function(n){e.indexOf(n)<0&&e.push(n)})),e}),c.options.ns),c.initializedStoreOnce=!0,c.isInitialized=!0),t&&!c.initializedLanguageOnce&&(c.changeLanguage(t),c.initializedLanguageOnce=!0))}e.I18nContext=b,e.I18nextProvider=function(e){var t=e.i18n,r=e.defaultNS,i=e.children,o=n.useMemo((function(){return{i18n:t,defaultNS:r}}),[t,r]);return n.createElement(b.Provider,{value:o},i)},e.Trans=function(e){var i=e.children,o=e.count,a=e.parent,u=e.i18nKey,l=e.tOptions,f=void 0===l?{}:l,p=e.values,d=e.defaults,g=e.components,m=e.ns,y=e.i18n,h=e.t,v=s(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),O=n.useContext(b)||{},j=O.i18n,S=O.defaultNS,N=y||j||E();if(!N)return R("You will need to pass in an i18next instance by using i18nextReactModule"),i;var x=h||N.t.bind(N)||function(e){return e},I=c(c({},w()),N.options&&N.options.react),P=m||x.ns||S||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];var C=d||function e(n,i){if(!n)return"";var o="",a=D(n),c=i.transKeepBasicHtmlNodesFor||[];return a.forEach((function(n,a){if("string"==typeof n)o+="".concat(n);else if(t.isValidElement(n)){var u=Object.keys(n.props).length,l=c.indexOf(n.type)>-1,f=n.props.children;if(!f&&l&&0===u)o+="<".concat(n.type,"/>");else if(f||l&&0===u)if(n.props.i18nIsDynamicList)o+="<".concat(a,">");else if(l&&1===u&&"string"==typeof f)o+="<".concat(n.type,">").concat(f,"");else{var p=e(f,i);o+="<".concat(a,">").concat(p,"")}else o+="<".concat(a,">")}else if("object"===r(n)){var d=n.format,g=s(n,["format"]),m=Object.keys(g);if(1===m.length){var y=d?"".concat(m[0],", ").concat(d):m[0];o+="{{".concat(y,"}}")}else k("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",n)}else k("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}}.",n)})),o}(i,I)||I.transEmptyNodeValue||u,A=I.hashTransKey,T=u||(A?A(C):C),L=p?f.interpolation:{interpolation:c(c({},f.interpolation),{},{prefix:"#$?",suffix:"?$#"})},z=c(c(c(c({},f),{},{count:o},p),L),{},{defaultValue:C,ns:P}),U=B(g||i,T?x(T,z):C,N,I,z),V=void 0!==a?a:I.defaultTransParent;return V?t.createElement(V,v,U):U},e.Translation=function(e){var n=e.ns,t=e.children,r=u(U(n,s(e,["ns","children"])),3),i=r[0],o=r[1],a=r[2];return t(i,{i18n:o,lng:o.language},a)},e.composeInitialProps=x,e.getDefaults=w,e.getI18n=E,e.getInitialProps=I,e.initReactI18next=N,e.setDefaults=O,e.setI18n=S,e.useSSR=V,e.useTranslation=U,e.withSSR=function(){return function(e){function n(n){var r=n.initialI18nStore,i=n.initialLanguage,o=s(n,["initialI18nStore","initialLanguage"]);return V(r,i),t.createElement(e,c({},o))}return n.getInitialProps=x(e),n.displayName="withI18nextSSR(".concat(T(e),")"),n.WrappedComponent=e,n}},e.withTranslation=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function i(i){var o=i.forwardedRef,a=s(i,["forwardedRef"]),l=u(U(e,a),3),f=l[0],p=l[1],d=l[2],g=c(c({},a),{},{t:f,i18n:p,tReady:d});return n.withRef&&o?g.ref=o:!n.withRef&&o&&(g.forwardedRef=o),t.createElement(r,g)}i.displayName="withI18nextTranslation(".concat(T(r),")"),i.WrappedComponent=r;return n.withRef?t.forwardRef((function(e,n){return t.createElement(i,Object.assign({},e,{forwardedRef:n}))})):i}},Object.defineProperty(e,"__esModule",{value:!0})})); +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e=e||self).ReactI18next={},e.React)}(this,(function(e,n){"use strict";var t="default"in n?n.default:n;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function i(e,n){for(var t=0;t=0||(i[t]=e[t]);return i}(e,n);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(i[t]=e[t])}return i}function u(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,n){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var t=[],r=!0,i=!1,o=void 0;try{for(var a,c=e[Symbol.iterator]();!(r=(a=c.next()).done)&&(t.push(a.value),!n||t.length!==n);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==c.return||c.return()}finally{if(i)throw o}}return t}(e,n)||function(e,n){if(!e)return;if("string"==typeof e)return l(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return l(e,n)}(e,n)||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 l(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,g=Object.create?Object.create(null):{};function m(e,n,t,r,i){var o=n.indexOf("<",r),a=n.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&t+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}var y,h=function(e,n){n||(n={}),n.components||(n.components=g);var t,r=[],i=-1,o=[],a={},c=!1;return e.replace(d,(function(s,u){if(c){if(s!=="")return;c=!1}var l,d="/"!==s.charAt(1),g=0===s.indexOf("\x3c!--"),y=u+s.length,h=e.charAt(y);d&&!g&&(i++,"tag"===(t=function(e){var n,t=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(p,(function(o){if("="===o)return r=!0,void t++;r?0===t?((f[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[n]=o.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(i.attrs[n]=n),n=o),t++,r=!1})),i}(s)).type&&n.components[t.name]&&(t.type="component",c=!0),t.voidElement||c||!h||"<"===h||m(t.children,e,i,y,n.ignoreWhitespace),a[t.tagName]=t,0===i&&r.push(t),(l=o[i-1])&&l.children.push(t),o[i]=t),(g||!d||t.voidElement)&&(g||i--,!c&&"<"!==h&&h&&m(l=-1===i?r:o[i].children,e,i,y,n.ignoreWhitespace))})),!r.length&&e.length&&m(r,e,0,0,n.ignoreWhitespace),r},v={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},b=t.createContext();function O(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};v=c(c({},v),e)}function w(){return v}var j=function(){function e(){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e),this.usedNamespaces={}}var n,t,r;return n=e,(t=[{key:"addUsedNamespaces",value:function(e){var n=this;e.forEach((function(e){n.usedNamespaces[e]||(n.usedNamespaces[e]=!0)}))}},{key:"getUsedNamespaces",value:function(){return Object.keys(this.usedNamespaces)}}])&&i(n.prototype,t),r&&i(n,r),e}();function S(e){y=e}function E(){return y}var N={type:"3rdParty",init:function(e){O(e.options.react),S(e)}};function x(e){return function(n){return new Promise((function(t){var r=I();e.getInitialProps?e.getInitialProps(n).then((function(e){t(c(c({},e),r))})):t(r)}))}}function I(){var e=E(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach((function(t){r[t]={},n.forEach((function(n){r[t][n]=e.getResourceBundle(t,n)||{}}))})),t.initialI18nStore=r,t.initialLanguage=e.language,t}function k(){if(console&&console.warn){for(var e,n=arguments.length,t=new Array(n),r=0;r2&&void 0!==arguments[2]?arguments[2]:{};if(!n.languages||!n.languages.length)return R("i18n.languages were undefined or empty",n.languages),!0;var r=n.languages[0],i=!!n.options&&n.options.fallbackLng,o=n.languages[n.languages.length-1];if("cimode"===r.toLowerCase())return!0;var a=function(e,t){var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!a(n.isLanguageChangingTo,e))&&(!!n.hasResourceBundle(r,e)||(!n.services.backendConnector.backend||!(!a(r,e)||i&&!a(o,e))))}function T(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function L(e,n){if(!e)return!1;var t=e.props?e.props.children:e.children;return n?t.length>0:!!t}function z(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function D(e){return Array.isArray(e)?e:[e]}function B(e,n,i,o,a){if(""===n)return[];var s=o.transKeepBasicHtmlNodesFor||[],u=n&&new RegExp(s.join("|")).test(n);if(!e&&!u)return[n];var l={};!function e(n){D(n).forEach((function(n){"string"!=typeof n&&(L(n)?e(z(n)):"object"!==r(n)||t.isValidElement(n)||Object.assign(l,n))}))}(e);var f=i.services.interpolator.interpolate(n,c(c({},l),a),i.language),p=h("<0>".concat(f,""));function d(e,n,r){var i=z(e),o=m(i,n.children,r);return function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every((function(e){return t.isValidElement(e)}))}(i)&&0===o.length?i:o}function g(e,n,r,i,o){e.dummy&&(e.children=n),r.push(t.cloneElement(e,c(c({},e.props),{},{key:i}),o?void 0:n))}function m(n,i,a){var l=D(n);return D(i).reduce((function(n,i,f){var p,y,h,v=i.children&&i.children[0]&&i.children[0].content;if("tag"===i.type){var b=l[parseInt(i.name,10)];!b&&1===a.length&&a[0][i.name]&&(b=a[0][i.name]),b||(b={});var O=0!==Object.keys(i.attrs).length?(p={props:i.attrs},(h=c({},y=b)).props=Object.assign(p.props,y.props),h):b,w=t.isValidElement(O),j=w&&L(i,!0)&&!i.voidElement,S=u&&"object"===r(O)&&O.dummy&&!w,E="object"===r(e)&&null!==e&&Object.hasOwnProperty.call(e,i.name);if("string"==typeof O)n.push(O);else if(L(O)||j){g(O,d(O,i,a),n,f)}else if(S){var N=m(l,i.children,a);n.push(t.cloneElement(O,c(c({},O.props),{},{key:f}),N))}else if(Number.isNaN(parseFloat(i.name))){if(E)g(O,d(O,i,a),n,f,i.voidElement);else if(o.transSupportBasicHtmlNodes&&s.indexOf(i.name)>-1)if(i.voidElement)n.push(t.createElement(i.name,{key:"".concat(i.name,"-").concat(f)}));else{var x=m(l,i.children,a);n.push(t.createElement(i.name,{key:"".concat(i.name,"-").concat(f)},x))}else if(i.voidElement)n.push("<".concat(i.name," />"));else{var I=m(l,i.children,a);n.push("<".concat(i.name,">").concat(I,""))}}else if("object"!==r(O)||w)1===i.children.length&&v?n.push(t.cloneElement(O,c(c({},O.props),{},{key:f}),v)):n.push(t.cloneElement(O,c(c({},O.props),{},{key:f})));else{var k=i.children[0]?v:null;k&&n.push(k)}}else"text"===i.type&&n.push(i.content);return n}),[])}return z(m([{dummy:!0,children:e}],p,D(e||[]))[0])}function U(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=t.i18n,i=n.useContext(b)||{},o=i.i18n,a=i.defaultNS,s=r||o||E();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){R("You will need to pass in an i18next instance by using initReactI18next");var l=function(e){return Array.isArray(e)?e[e.length-1]:e},f=[l,{},!1];return f.t=l,f.i18n={},f.ready=!1,f}s.options.react&&void 0!==s.options.react.wait&&R("It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.");var p=c(c(c({},w()),s.options.react),t),d=p.useSuspense,g=e||a||s.options&&s.options.defaultNS;g="string"==typeof g?[g]:g||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(g);var m=(s.isInitialized||s.initializedStoreOnce)&&g.every((function(e){return A(e,s,p)}));function y(){return{t:s.getFixedT(null,"fallback"===p.nsMode?g:g[0])}}var h=n.useState(y()),v=u(h,2),O=v[0],S=v[1],N=n.useRef(!0);n.useEffect((function(){var e=p.bindI18n,n=p.bindI18nStore;function t(){N.current&&S(y())}return N.current=!0,m||d||C(s,g,(function(){N.current&&S(y())})),e&&s&&s.on(e,t),n&&s&&s.store.on(n,t),function(){N.current=!1,e&&s&&e.split(" ").forEach((function(e){return s.off(e,t)})),n&&s&&n.split(" ").forEach((function(e){return s.store.off(e,t)}))}}),[s,g.join()]);var x=n.useRef(!0);n.useEffect((function(){N.current&&!x.current&&S(y()),x.current=!1}),[s]);var I=[O.t,s,m];if(I.t=O.t,I.i18n=s,I.ready=m,m)return I;if(!m&&!d)return I;throw new Promise((function(e){C(s,g,(function(){e()}))}))}function V(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=r.i18n,o=n.useContext(b)||{},a=o.i18n,c=i||a||E();c.options&&c.options.isClone||(e&&!c.initializedStoreOnce&&(c.services.resourceStore.data=e,c.options.ns=Object.values(e).reduce((function(e,n){return Object.keys(n).forEach((function(n){e.indexOf(n)<0&&e.push(n)})),e}),c.options.ns),c.initializedStoreOnce=!0,c.isInitialized=!0),t&&!c.initializedLanguageOnce&&(c.changeLanguage(t),c.initializedLanguageOnce=!0))}e.I18nContext=b,e.I18nextProvider=function(e){var t=e.i18n,r=e.defaultNS,i=e.children,o=n.useMemo((function(){return{i18n:t,defaultNS:r}}),[t,r]);return n.createElement(b.Provider,{value:o},i)},e.Trans=function(e){var i=e.children,o=e.count,a=e.parent,u=e.i18nKey,l=e.tOptions,f=void 0===l?{}:l,p=e.values,d=e.defaults,g=e.components,m=e.ns,y=e.i18n,h=e.t,v=s(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),O=n.useContext(b)||{},j=O.i18n,S=O.defaultNS,N=y||j||E();if(!N)return R("You will need to pass in an i18next instance by using i18nextReactModule"),i;var x=h||N.t.bind(N)||function(e){return e},I=c(c({},w()),N.options&&N.options.react),P=m||x.ns||S||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];var C=d||function e(n,i){if(!n)return"";var o="",a=D(n),c=i.transKeepBasicHtmlNodesFor||[];return a.forEach((function(n,a){if("string"==typeof n)o+="".concat(n);else if(t.isValidElement(n)){var u=Object.keys(n.props).length,l=c.indexOf(n.type)>-1,f=n.props.children;if(!f&&l&&0===u)o+="<".concat(n.type,"/>");else if(f||l&&0===u)if(n.props.i18nIsDynamicList)o+="<".concat(a,">");else if(l&&1===u&&"string"==typeof f)o+="<".concat(n.type,">").concat(f,"");else{var p=e(f,i);o+="<".concat(a,">").concat(p,"")}else o+="<".concat(a,">")}else if("object"===r(n)){var d=n.format,g=s(n,["format"]),m=Object.keys(g);if(1===m.length){var y=d?"".concat(m[0],", ").concat(d):m[0];o+="{{".concat(y,"}}")}else k("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",n)}else k("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}}.",n)})),o}(i,I)||I.transEmptyNodeValue||u,A=I.hashTransKey,T=u||(A?A(C):C),L=p?f.interpolation:{interpolation:c(c({},f.interpolation),{},{prefix:"#$?",suffix:"?$#"})},z=c(c(c(c({},f),{},{count:o},p),L),{},{defaultValue:C,ns:P}),U=B(g||i,T?x(T,z):C,N,I,z),V=void 0!==a?a:I.defaultTransParent;return V?t.createElement(V,v,U):U},e.Translation=function(e){var n=e.ns,t=e.children,r=u(U(n,s(e,["ns","children"])),3),i=r[0],o=r[1],a=r[2];return t(i,{i18n:o,lng:o.language},a)},e.composeInitialProps=x,e.getDefaults=w,e.getI18n=E,e.getInitialProps=I,e.initReactI18next=N,e.setDefaults=O,e.setI18n=S,e.useSSR=V,e.useTranslation=U,e.withSSR=function(){return function(e){function n(n){var r=n.initialI18nStore,i=n.initialLanguage,o=s(n,["initialI18nStore","initialLanguage"]);return V(r,i),t.createElement(e,c({},o))}return n.getInitialProps=x(e),n.displayName="withI18nextSSR(".concat(T(e),")"),n.WrappedComponent=e,n}},e.withTranslation=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function i(i){var o=i.forwardedRef,a=s(i,["forwardedRef"]),l=u(U(e,a),3),f=l[0],p=l[1],d=l[2],g=c(c({},a),{},{t:f,i18n:p,tReady:d});return n.withRef&&o?g.ref=o:!n.withRef&&o&&(g.forwardedRef=o),t.createElement(r,g)}i.displayName="withI18nextTranslation(".concat(T(r),")"),i.WrappedComponent=r;return n.withRef?t.forwardRef((function(e,n){return t.createElement(i,Object.assign({},e,{forwardedRef:n}))})):i}},Object.defineProperty(e,"__esModule",{value:!0})})); diff --git a/src/useTranslation.js b/src/useTranslation.js index ccbb3ecd..5ef0371c 100755 --- a/src/useTranslation.js +++ b/src/useTranslation.js @@ -74,7 +74,7 @@ export function useTranslation(ns, props = {}) { if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach((e) => i18n.store.off(e, boundReset)); }; - }, [namespaces.join()]); // re-run effect whenever list of namespaces changes + }, [i18n, namespaces.join()]); // re-run effect whenever list of namespaces changes // t is correctly initialized by useState hook. We only need to update it after i18n // instance was replaced (for example in the provider).