react-i18next 10.12.4 → 10.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/amd/react-i18next.js +63 -30
- package/dist/amd/react-i18next.min.js +1 -1
- package/dist/commonjs/Trans.js +27 -23
- package/dist/commonjs/Translation.js +0 -2
- package/dist/commonjs/useSSR.js +2 -1
- package/dist/commonjs/withTranslation.js +20 -6
- package/dist/es/Trans.js +27 -23
- package/dist/es/Translation.js +0 -1
- package/dist/es/useSSR.js +2 -1
- package/dist/es/withTranslation.js +18 -6
- package/dist/umd/react-i18next.js +63 -30
- package/dist/umd/react-i18next.min.js +1 -1
- package/package.json +16 -15
- package/react-i18next.js +63 -30
- package/react-i18next.min.js +1 -1
- package/src/Trans.js +30 -22
- package/src/Translation.js +0 -1
- package/src/index.d.ts +17 -8
- package/src/useSSR.js +2 -1
- package/src/withTranslation.js +8 -6
package/react-i18next.min.js
CHANGED
|
@@ -1 +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,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function a(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter(function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable})),t.push.apply(t,r)}return t}function i(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?a(t,!0).forEach(function(n){r(e,n,t[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):a(t).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))})}return e}function o(e,n){if(null==e)return{};var t,r,a=function(e,n){if(null==e)return{};var t,r,a={},i=Object.keys(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||(a[t]=e[t]);return a}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(a[t]=e[t])}return a}var c={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},s=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,n,t,r,a){var i=n.indexOf("<",r),o=n.slice(r,-1===i?void 0:i);/^\s*$/.test(o)&&(o=" "),(!a&&i>-1&&t+e.length>=0||" "!==o)&&e.push({type:"text",content:o})}function f(e,n){switch(n.type){case"text":return e+n.content;case"tag":return e+="<"+n.name+(n.attrs?function(e){var n=[];for(var t in e)n.push(t+'="'+e[t]+'"');return n.length?" "+n.join(" "):""}(n.attrs):"")+(n.voidElement?"/>":">"),n.voidElement?e:e+n.children.reduce(f,"")+"</"+n.name+">"}}var d,g,m={parse:function(e,n){n||(n={}),n.components||(n.components=u);var t,r=[],a=-1,i=[],o={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+t.name+">")return;f=!1}var d,g="/"!==l.charAt(1),m=0===l.indexOf("\x3c!--"),h=u+l.length,y=e.charAt(h);g&&!m&&(a++,"tag"===(t=function(e){var n,t=0,r=!0,a={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(s,function(i){if("="===i)return r=!0,void t++;r?0===t?((c[i]||"/"===e.charAt(e.length-2))&&(a.voidElement=!0),a.name=i):(a.attrs[n]=i.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(a.attrs[n]=n),n=i),t++,r=!1}),a}(l)).type&&n.components[t.name]&&(t.type="component",f=!0),t.voidElement||f||!y||"<"===y||p(t.children,e,a,h,n.ignoreWhitespace),o[t.tagName]=t,0===a&&r.push(t),(d=i[a-1])&&d.children.push(t),i[a]=t),(m||!g||t.voidElement)&&(m||a--,!f&&"<"!==y&&y&&p(d=-1===a?r:i[a].children,e,a,h,n.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,n.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,n){return e+f("",n)},"")}},h={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},y=t.createContext();function v(){return g}function b(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};h=i({},h,{},e)}function O(){return h}class j{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function S(e){d=e}function E(){return d}var w={type:"3rdParty",init(e){b(e.options.react),S(e)}};function N(e){return n=>new Promise(t=>{var r=x();e.getInitialProps?e.getInitialProps(n).then(e=>{t(i({},e,{},r))}):t(r)})}function x(){var e=E(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach(t=>{r[t]={},n.forEach(n=>{r[t][n]=e.getResourceBundle(t,n)||{}})}),t.initialI18nStore=r,t.initialLanguage=e.language,t}function I(){if(console&&console.warn){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&(n[0]="react-i18next:: ".concat(n[0])),console.warn(...n)}}var k={};function P(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&k[n[0]]||("string"==typeof n[0]&&(k[n[0]]=new Date),I(...n))}function R(e,n,t){e.loadNamespaces(n,()=>{if(e.isInitialized)t();else{var n=()=>{setTimeout(()=>{e.off("initialized",n)},0),t()};e.on("initialized",n)}})}function C(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 T(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function z(e,n,r,a,o){if(""===n)return[];var c=a.transKeepBasicHtmlNodesFor||[],s=n&&new RegExp(c.join("|")).test(n);if(!e&&!s)return[n];var l={};!function e(n){"[object Array]"!==Object.prototype.toString.call(n)&&(n=[n]),n.forEach(n=>{"string"!=typeof n&&(A(n)?e(T(n)):"object"!=typeof n||t.isValidElement(n)||Object.assign(l,n))})}(e),n=r.services.interpolator.interpolate(n,i({},l,{},o),r.language);var u=function e(n,r){return"[object Array]"!==Object.prototype.toString.call(n)&&(n=[n]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,o,l)=>{var u=o.children&&o.children[0]&&o.children[0].content;if("tag"===o.type){var p=n[parseInt(o.name,10)]||{},f=t.isValidElement(p);if("string"==typeof p)r.push(p);else if(A(p)){var d=T(p),g=e(d,o.children),m=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>t.isValidElement(e))}(d)&&0===g.length?d:g;p.dummy&&(p.children=m),r.push(t.cloneElement(p,i({},p.props,{key:l}),m))}else if(s&&"object"==typeof p&&p.dummy&&!f){var h=e(n,o.children);r.push(t.cloneElement(p,i({},p.props,{key:l}),h))}else if(isNaN(o.name))if(a.transSupportBasicHtmlNodes&&c.indexOf(o.name)>-1)if(o.voidElement)r.push(t.createElement(o.name,{key:"".concat(o.name,"-").concat(l)}));else{var y=e(n,o.children);r.push(t.createElement(o.name,{key:"".concat(o.name,"-").concat(l)},y))}else if(o.voidElement)r.push("<".concat(o.name," />"));else{var v=e(n,o.children);r.push("<".concat(o.name,">").concat(v,"</").concat(o.name,">"))}else if("object"!=typeof p||f)1===o.children.length&&u?r.push(t.cloneElement(p,i({},p.props,{key:l}),u)):r.push(t.cloneElement(p,i({},p.props,{key:l})));else{var b=o.children[0]?u:null;b&&r.push(b)}}else"text"===o.type&&r.push(o.content);return r},[])}([{dummy:!0,children:e}],m.parse("<0>".concat(n,"</0>")));return T(u[0])}function L(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},{i18n:r}=t,a=n.useContext(y),{i18n:o,defaultNS:c}=v()&&a||{},s=r||o||E();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){P("You will need pass in an i18next instance by using initReactI18next");var l=[e=>e,{},!1];return l.t=e=>e,l.i18n={},l.ready=!1,l}var u=i({},O(),{},s.options.react),{useSuspense:p=u.useSuspense}=t,f=e||c||s.options&&s.options.defaultNS;f="string"==typeof f?[f]:f||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(f);var d=(s.isInitialized||s.initializedStoreOnce)&&f.every(e=>(function(e,n){if(!n.languages||!n.languages.length)return P("i18n.languages were undefined or empty",n.languages),!0;var t=n.languages[0],r=!!n.options&&n.options.fallbackLng,a=n.languages[n.languages.length-1];if("cimode"===t.toLowerCase())return!0;var i=(e,t)=>{var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!!n.hasResourceBundle(t,e)||(!n.services.backendConnector.backend||!(!i(t,e)||r&&!i(a,e)))})(e,s));function g(){return{t:s.getFixedT(null,"fallback"===u.nsMode?f:f[0])}}var[m,h]=n.useState(g());n.useEffect(()=>{var e=!0,{bindI18n:n,bindI18nStore:t}=u;function r(){e&&h(g())}return d||p||R(s,f,()=>{e&&h(g())}),n&&s&&s.on(n,r),t&&s&&s.store.on(t,r),()=>{e=!1,n&&s&&n.split(" ").forEach(e=>s.off(e,r)),t&&s&&t.split(" ").forEach(e=>s.store.off(e,r))}},[f.join()]);var b=[m.t,s,d];if(b.t=m.t,b.i18n=s,b.ready=d,d)return b;if(!d&&!p)return b;throw new Promise(e=>{R(s,f,()=>{h(g()),e()})})}function D(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},{i18n:a}=r,{i18n:i}=v()?n.useContext(y):{},o=a||i||E();o.options&&o.options.isClone||(e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),t&&!o.initializedLanguageOnce&&(o.changeLanguage(t),o.initializedLanguageOnce=!0))}e.I18nContext=y,e.I18nextProvider=function(e){var{i18n:n,defaultNS:r,children:a}=e;return g=!0,t.createElement(y.Provider,{value:{i18n:n,defaultNS:r}},a)},e.Trans=function(e){var{children:r,count:a,parent:c,i18nKey:s,tOptions:l,values:u,defaults:p,components:f,ns:d,i18n:g,t:m}=e,h=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),{i18n:b,defaultNS:j}=v()&&n.useContext(y)||{},S=g||b||E();if(!S)return P("You will need pass in an i18next instance by using i18nextReactModule"),r;var w=m||S.t.bind(S)||(e=>e),N=i({},O(),{},S.options&&S.options.react),x=void 0!==c?c:N.defaultTransParent,k=d||w.ns||j||S.options&&S.options.defaultNS;k="string"==typeof k?[k]:k||["translation"];var R=p||function e(n,r,a,o){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);var c=o.transKeepBasicHtmlNodesFor||[];return r.forEach((r,a)=>{var s="".concat(a);if("string"==typeof r)n="".concat(n).concat(r);else if(A(r)){var l=c.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:s;n=r.props&&r.props.i18nIsDynamicList?"".concat(n,"<").concat(l,"></").concat(l,">"):"".concat(n,"<").concat(l,">").concat(e("",T(r),a+1,o),"</").concat(l,">")}else if(t.isValidElement(r))n=c.indexOf(r.type)>-1&&0===Object.keys(r.props).length?"".concat(n,"<").concat(r.type,"/>"):"".concat(n,"<").concat(s,"></").concat(s,">");else if("object"==typeof r){var u=i({},r),{format:p}=u;delete u.format;var f=Object.keys(u);p&&1===f.length?n="".concat(n,"{{").concat(f[0],", ").concat(p,"}}"):1===f.length?n="".concat(n,"{{").concat(f[0],"}}"):I("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else I("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)}),n}("",r,0,N)||N.transEmptyNodeValue,{hashTransKey:C}=N,L=s||(C?C(R):R),D=i({},l,{},u,{},u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:R,count:a,ns:k}),B=L?w(L,D):R;return x?t.createElement(x,h,z(f||r,B,S,N,D)):z(f||r,B,S,N,D)},e.Translation=function(e){var{ns:n,children:t}=e,r=o(e,["ns","children"]),[a,i,c]=L(n,r);return t(a,{i18n:i,lng:i.language},c)},e.composeInitialProps=N,e.getDefaults=O,e.getI18n=E,e.getInitialProps=x,e.initReactI18next=w,e.setDefaults=b,e.setI18n=S,e.useSSR=D,e.useTranslation=L,e.withSSR=function(){return function(e){function n(n){var{initialI18nStore:r,initialLanguage:a}=n,c=o(n,["initialI18nStore","initialLanguage"]);return D(r,a),t.createElement(e,i({},c))}return n.getInitialProps=N(e),n.displayName="withI18nextSSR(".concat(C(e),")"),n.WrappedComponent=e,n}},e.withTranslation=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function a(a,o){var[c,s,l]=L(e,a),u=i({},a,{t:c,i18n:s,tReady:l});return n.withRef&&o&&(u.ref=o),t.createElement(r,u)}return a.displayName="withI18nextTranslation(".concat(C(r),")"),a.WrappedComponent=r,n.withRef?t.forwardRef(a):a}},Object.defineProperty(e,"__esModule",{value:!0})});
|
|
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,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function a(){return(a=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}function i(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,r)}return t}function o(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?i(t,!0).forEach((function(n){r(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):i(t).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function c(e,n){if(null==e)return{};var t,r,a=function(e,n){if(null==e)return{};var t,r,a={},i=Object.keys(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||(a[t]=e[t]);return a}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(a[t]=e[t])}return a}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},l=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,u=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,p=Object.create?Object.create(null):{};function f(e,n,t,r,a){var i=n.indexOf("<",r),o=n.slice(r,-1===i?void 0:i);/^\s*$/.test(o)&&(o=" "),(!a&&i>-1&&t+e.length>=0||" "!==o)&&e.push({type:"text",content:o})}function d(e,n){switch(n.type){case"text":return e+n.content;case"tag":return e+="<"+n.name+(n.attrs?function(e){var n=[];for(var t in e)n.push(t+'="'+e[t]+'"');return n.length?" "+n.join(" "):""}(n.attrs):"")+(n.voidElement?"/>":">"),n.voidElement?e:e+n.children.reduce(d,"")+"</"+n.name+">"}}var g,m,h={parse:function(e,n){n||(n={}),n.components||(n.components=p);var t,r=[],a=-1,i=[],o={},c=!1;return e.replace(u,(function(u,p){if(c){if(u!=="</"+t.name+">")return;c=!1}var d,g="/"!==u.charAt(1),m=0===u.indexOf("\x3c!--"),h=p+u.length,v=e.charAt(h);g&&!m&&(a++,"tag"===(t=function(e){var n,t=0,r=!0,a={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(l,(function(i){if("="===i)return r=!0,void t++;r?0===t?((s[i]||"/"===e.charAt(e.length-2))&&(a.voidElement=!0),a.name=i):(a.attrs[n]=i.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(a.attrs[n]=n),n=i),t++,r=!1})),a}(u)).type&&n.components[t.name]&&(t.type="component",c=!0),t.voidElement||c||!v||"<"===v||f(t.children,e,a,h,n.ignoreWhitespace),o[t.tagName]=t,0===a&&r.push(t),(d=i[a-1])&&d.children.push(t),i[a]=t),(m||!g||t.voidElement)&&(m||a--,!c&&"<"!==v&&v&&f(d=-1===a?r:i[a].children,e,a,h,n.ignoreWhitespace))})),!r.length&&e.length&&f(r,e,0,0,n.ignoreWhitespace),r},stringify:function(e){return e.reduce((function(e,n){return e+d("",n)}),"")}},v={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},y=t.createContext();function b(){return m}function O(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};v=o({},v,{},e)}function w(){return v}class j{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function E(e){g=e}function N(){return g}var x={type:"3rdParty",init(e){O(e.options.react),E(e)}};function S(e){return n=>new Promise(t=>{var r=I();e.getInitialProps?e.getInitialProps(n).then(e=>{t(o({},e,{},r))}):t(r)})}function I(){var e=N(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach(t=>{r[t]={},n.forEach(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=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&(n[0]="react-i18next:: ".concat(n[0])),console.warn(...n)}}var P={};function R(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&P[n[0]]||("string"==typeof n[0]&&(P[n[0]]=new Date),k(...n))}function C(e,n,t){e.loadNamespaces(n,()=>{if(e.isInitialized)t();else{var n=()=>{setTimeout(()=>{e.off("initialized",n)},0),t()};e.on("initialized",n)}})}function T(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function z(e){return e&&(e.children||e.props&&e.props.children)}function L(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function A(e){return Array.isArray(e)?e:[e]}function D(e,n,r,a,i){if(""===n)return[];var c=a.transKeepBasicHtmlNodesFor||[],s=n&&new RegExp(c.join("|")).test(n);if(!e&&!s)return[n];var l={};!function e(n){A(n).forEach(n=>{"string"!=typeof n&&(z(n)?e(L(n)):"object"!=typeof n||t.isValidElement(n)||Object.assign(l,n))})}(e);var u=r.services.interpolator.interpolate(n,o({},l,{},i),r.language);var p=function e(n,r){var i=A(n);return A(r).reduce((n,r,l)=>{var u=r.children&&r.children[0]&&r.children[0].content;if("tag"===r.type){var p=i[parseInt(r.name,10)]||{},f=t.isValidElement(p);if("string"==typeof p)n.push(p);else if(z(p)){var d=L(p),g=e(d,r.children),m=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>t.isValidElement(e))}(d)&&0===g.length?d:g;p.dummy&&(p.children=m),n.push(t.cloneElement(p,o({},p.props,{key:l}),m))}else if(s&&"object"==typeof p&&p.dummy&&!f){var h=e(i,r.children);n.push(t.cloneElement(p,o({},p.props,{key:l}),h))}else if(Number.isNaN(parseFloat(r.name)))if(a.transSupportBasicHtmlNodes&&c.indexOf(r.name)>-1)if(r.voidElement)n.push(t.createElement(r.name,{key:"".concat(r.name,"-").concat(l)}));else{var v=e(i,r.children);n.push(t.createElement(r.name,{key:"".concat(r.name,"-").concat(l)},v))}else if(r.voidElement)n.push("<".concat(r.name," />"));else{var y=e(i,r.children);n.push("<".concat(r.name,">").concat(y,"</").concat(r.name,">"))}else if("object"!=typeof p||f)1===r.children.length&&u?n.push(t.cloneElement(p,o({},p.props,{key:l}),u)):n.push(t.cloneElement(p,o({},p.props,{key:l})));else{var b=r.children[0]?u:null;b&&n.push(b)}}else"text"===r.type&&n.push(r.content);return n},[])}([{dummy:!0,children:e}],h.parse("<0>".concat(u,"</0>")));return L(p[0])}function B(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},{i18n:r}=t,a=n.useContext(y),{i18n:i,defaultNS:c}=b()&&a||{},s=r||i||N();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){R("You will need pass in an i18next instance by using initReactI18next");var l=[e=>e,{},!1];return l.t=e=>e,l.i18n={},l.ready=!1,l}var u=o({},w(),{},s.options.react),{useSuspense:p=u.useSuspense}=t,f=e||c||s.options&&s.options.defaultNS;f="string"==typeof f?[f]:f||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(f);var d=(s.isInitialized||s.initializedStoreOnce)&&f.every(e=>(function(e,n){if(!n.languages||!n.languages.length)return R("i18n.languages were undefined or empty",n.languages),!0;var t=n.languages[0],r=!!n.options&&n.options.fallbackLng,a=n.languages[n.languages.length-1];if("cimode"===t.toLowerCase())return!0;var i=(e,t)=>{var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!!n.hasResourceBundle(t,e)||(!n.services.backendConnector.backend||!(!i(t,e)||r&&!i(a,e)))})(e,s));function g(){return{t:s.getFixedT(null,"fallback"===u.nsMode?f:f[0])}}var[m,h]=n.useState(g());n.useEffect(()=>{var e=!0,{bindI18n:n,bindI18nStore:t}=u;function r(){e&&h(g())}return d||p||C(s,f,()=>{e&&h(g())}),n&&s&&s.on(n,r),t&&s&&s.store.on(t,r),()=>{e=!1,n&&s&&n.split(" ").forEach(e=>s.off(e,r)),t&&s&&t.split(" ").forEach(e=>s.store.off(e,r))}},[f.join()]);var v=[m.t,s,d];if(v.t=m.t,v.i18n=s,v.ready=d,d)return v;if(!d&&!p)return v;throw new Promise(e=>{C(s,f,()=>{h(g()),e()})})}function V(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},{i18n:a}=r,i=n.useContext(y),{i18n:o}=b()&&i||{},c=a||o||N();c.options&&c.options.isClone||(e&&!c.initializedStoreOnce&&(c.services.resourceStore.data=e,c.initializedStoreOnce=!0),t&&!c.initializedLanguageOnce&&(c.changeLanguage(t),c.initializedLanguageOnce=!0))}e.I18nContext=y,e.I18nextProvider=function(e){var{i18n:n,defaultNS:r,children:a}=e;return m=!0,t.createElement(y.Provider,{value:{i18n:n,defaultNS:r}},a)},e.Trans=function(e){var{children:r,count:a,parent:i,i18nKey:s,tOptions:l,values:u,defaults:p,components:f,ns:d,i18n:g,t:m}=e,h=c(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),{i18n:v,defaultNS:O}=b()&&n.useContext(y)||{},j=g||v||N();if(!j)return R("You will need pass in an i18next instance by using i18nextReactModule"),r;var E=m||j.t.bind(j)||(e=>e),x=o({},w(),{},j.options&&j.options.react),S=void 0!==i?i:x.defaultTransParent,I=d||E.ns||O||j.options&&j.options.defaultNS;I="string"==typeof I?[I]:I||["translation"];var P=p||function e(n,r,a,i){if(!r)return"";var c=n,s=A(r),l=i.transKeepBasicHtmlNodesFor||[];return s.forEach((n,r)=>{var a="".concat(r);if("string"==typeof n)c="".concat(c).concat(n);else if(z(n)){var s=l.indexOf(n.type)>-1&&1===Object.keys(n.props).length&&"string"==typeof z(n)?n.type:a;c=n.props&&n.props.i18nIsDynamicList?"".concat(c,"<").concat(s,"></").concat(s,">"):"".concat(c,"<").concat(s,">").concat(e("",L(n),r+1,i),"</").concat(s,">")}else if(t.isValidElement(n))c=l.indexOf(n.type)>-1&&0===Object.keys(n.props).length?"".concat(c,"<").concat(n.type,"/>"):"".concat(c,"<").concat(a,"></").concat(a,">");else if("object"==typeof n){var u=o({},n),{format:p}=u;delete u.format;var f=Object.keys(u);p&&1===f.length?c="".concat(c,"{{").concat(f[0],", ").concat(p,"}}"):1===f.length?c="".concat(c,"{{").concat(f[0],"}}"):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)}),c}("",r,0,x)||x.transEmptyNodeValue,{hashTransKey:C}=x,T=s||(C?C(P):P),B=o({},l,{count:a},u,{},u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:P,ns:I}),V=T?E(T,B):P;return S?t.createElement(S,h,D(f||r,V,j,x,B)):D(f||r,V,j,x,B)},e.Translation=function(e){var{ns:n,children:t}=e,r=c(e,["ns","children"]),[a,i,o]=B(n,r);return t(a,{i18n:i,lng:i.language},o)},e.composeInitialProps=S,e.getDefaults=w,e.getI18n=N,e.getInitialProps=I,e.initReactI18next=x,e.setDefaults=O,e.setI18n=E,e.useSSR=V,e.useTranslation=B,e.withSSR=function(){return function(e){function n(n){var{initialI18nStore:r,initialLanguage:a}=n,i=c(n,["initialI18nStore","initialLanguage"]);return V(r,a),t.createElement(e,o({},i))}return n.getInitialProps=S(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(a){var{forwardedRef:i}=a,s=c(a,["forwardedRef"]),[l,u,p]=B(e,s),f=o({},s,{t:l,i18n:u,tReady:p});return n.withRef&&i&&(f.ref=i),t.createElement(r,f)}i.displayName="withI18nextTranslation(".concat(T(r),")"),i.WrappedComponent=r;return n.withRef?t.forwardRef((e,n)=>t.createElement(i,a({},e,{forwardedRef:n}))):i}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/src/Trans.js
CHANGED
|
@@ -17,18 +17,22 @@ function hasValidReactChildren(children) {
|
|
|
17
17
|
return children.every(child => React.isValidElement(child));
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
function getAsArray(data) {
|
|
21
|
+
return Array.isArray(data) ? data : [data];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function nodesToString(startingString, children, index, i18nOptions) {
|
|
21
25
|
if (!children) return '';
|
|
22
|
-
|
|
26
|
+
let stringNode = startingString;
|
|
27
|
+
|
|
28
|
+
const childrenArray = getAsArray(children);
|
|
23
29
|
const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
// const isElement = React.isValidElement(child);
|
|
27
|
-
// const elementKey = `${index !== 0 ? index + '-' : ''}${i}:${typeof child.type === 'function' ? child.type.name : child.type || 'var'}`;
|
|
31
|
+
childrenArray.forEach((child, i) => {
|
|
28
32
|
const elementKey = `${i}`;
|
|
29
33
|
|
|
30
34
|
if (typeof child === 'string') {
|
|
31
|
-
|
|
35
|
+
stringNode = `${stringNode}${child}`;
|
|
32
36
|
} else if (hasChildren(child)) {
|
|
33
37
|
const elementTag =
|
|
34
38
|
keepArray.indexOf(child.type) > -1 &&
|
|
@@ -40,10 +44,10 @@ export function nodesToString(mem, children, index, i18nOptions) {
|
|
|
40
44
|
if (child.props && child.props.i18nIsDynamicList) {
|
|
41
45
|
// we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
|
|
42
46
|
// the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
|
|
43
|
-
|
|
47
|
+
stringNode = `${stringNode}<${elementTag}></${elementTag}>`;
|
|
44
48
|
} else {
|
|
45
49
|
// regular case mapping the inner children
|
|
46
|
-
|
|
50
|
+
stringNode = `${stringNode}<${elementTag}>${nodesToString(
|
|
47
51
|
'',
|
|
48
52
|
getChildren(child),
|
|
49
53
|
i + 1,
|
|
@@ -52,9 +56,9 @@ export function nodesToString(mem, children, index, i18nOptions) {
|
|
|
52
56
|
}
|
|
53
57
|
} else if (React.isValidElement(child)) {
|
|
54
58
|
if (keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 0) {
|
|
55
|
-
|
|
59
|
+
stringNode = `${stringNode}<${child.type}/>`;
|
|
56
60
|
} else {
|
|
57
|
-
|
|
61
|
+
stringNode = `${stringNode}<${elementKey}></${elementKey}>`;
|
|
58
62
|
}
|
|
59
63
|
} else if (typeof child === 'object') {
|
|
60
64
|
const clone = { ...child };
|
|
@@ -63,9 +67,9 @@ export function nodesToString(mem, children, index, i18nOptions) {
|
|
|
63
67
|
|
|
64
68
|
const keys = Object.keys(clone);
|
|
65
69
|
if (format && keys.length === 1) {
|
|
66
|
-
|
|
70
|
+
stringNode = `${stringNode}{{${keys[0]}, ${format}}}`;
|
|
67
71
|
} else if (keys.length === 1) {
|
|
68
|
-
|
|
72
|
+
stringNode = `${stringNode}{{${keys[0]}}}`;
|
|
69
73
|
} else {
|
|
70
74
|
// not a valid interpolation object (can only contain one value plus format)
|
|
71
75
|
warn(
|
|
@@ -81,7 +85,7 @@ export function nodesToString(mem, children, index, i18nOptions) {
|
|
|
81
85
|
}
|
|
82
86
|
});
|
|
83
87
|
|
|
84
|
-
return
|
|
88
|
+
return stringNode;
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
@@ -97,17 +101,21 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
97
101
|
|
|
98
102
|
// v2 -> interpolates upfront no need for "some <0>{{var}}</0>"" -> will be just "some {{var}}" in translation file
|
|
99
103
|
const data = {};
|
|
104
|
+
|
|
100
105
|
function getData(childs) {
|
|
101
|
-
|
|
102
|
-
|
|
106
|
+
const childrenArray = getAsArray(childs);
|
|
107
|
+
|
|
108
|
+
childrenArray.forEach(child => {
|
|
103
109
|
if (typeof child === 'string') return;
|
|
104
110
|
if (hasChildren(child)) getData(getChildren(child));
|
|
105
111
|
else if (typeof child === 'object' && !React.isValidElement(child))
|
|
106
112
|
Object.assign(data, child);
|
|
107
113
|
});
|
|
108
114
|
}
|
|
115
|
+
|
|
109
116
|
getData(children);
|
|
110
|
-
|
|
117
|
+
|
|
118
|
+
const interpolatedString = i18n.services.interpolator.interpolate(
|
|
111
119
|
targetString,
|
|
112
120
|
{ ...data, ...combinedTOpts },
|
|
113
121
|
i18n.language,
|
|
@@ -115,11 +123,11 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
115
123
|
|
|
116
124
|
// parse ast from string with additional wrapper tag
|
|
117
125
|
// -> avoids issues in parser removing prepending text nodes
|
|
118
|
-
const ast = HTML.parse(`<0>${
|
|
126
|
+
const ast = HTML.parse(`<0>${interpolatedString}</0>`);
|
|
119
127
|
|
|
120
|
-
function mapAST(
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
function mapAST(reactNode, astNode) {
|
|
129
|
+
const reactNodes = getAsArray(reactNode);
|
|
130
|
+
const astNodes = getAsArray(astNode);
|
|
123
131
|
|
|
124
132
|
return astNodes.reduce((mem, node, i) => {
|
|
125
133
|
const translationContent = node.children && node.children[0] && node.children[0].content;
|
|
@@ -148,7 +156,7 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
148
156
|
// so we just need to map the inner stuff
|
|
149
157
|
const inner = mapAST(reactNodes /* wrong but we need something */, node.children);
|
|
150
158
|
mem.push(React.cloneElement(child, { ...child.props, key: i }, inner));
|
|
151
|
-
} else if (isNaN(node.name)) {
|
|
159
|
+
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
152
160
|
if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
153
161
|
if (node.voidElement) {
|
|
154
162
|
mem.push(React.createElement(node.name, { key: `${node.name}-${i}` }));
|
|
@@ -234,10 +242,10 @@ export function Trans({
|
|
|
234
242
|
const interpolationOverride = values ? {} : { interpolation: { prefix: '#$?', suffix: '?$#' } };
|
|
235
243
|
const combinedTOpts = {
|
|
236
244
|
...tOptions,
|
|
245
|
+
count,
|
|
237
246
|
...values,
|
|
238
247
|
...interpolationOverride,
|
|
239
248
|
defaultValue,
|
|
240
|
-
count,
|
|
241
249
|
ns: namespaces,
|
|
242
250
|
};
|
|
243
251
|
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
package/src/Translation.js
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -7,8 +7,6 @@ export type Namespace = string | string[];
|
|
|
7
7
|
|
|
8
8
|
export function setDefaults(options: i18next.ReactOptions): void;
|
|
9
9
|
export function getDefaults(): i18next.ReactOptions;
|
|
10
|
-
export function addUsedNamespaces(namespaces: Namespace[]): void;
|
|
11
|
-
export function getUsedNamespaces(): string[];
|
|
12
10
|
export function setI18n(instance: i18next.i18n): void;
|
|
13
11
|
export function getI18n(): i18next.i18n;
|
|
14
12
|
export const initReactI18next: i18next.ThirdPartyModule;
|
|
@@ -20,6 +18,17 @@ export function getInitialProps(): {
|
|
|
20
18
|
initialLanguage: string;
|
|
21
19
|
};
|
|
22
20
|
|
|
21
|
+
export interface ReportNamespaces {
|
|
22
|
+
addUsedNamespaces(namespaces: Namespace[]): void;
|
|
23
|
+
getUsedNamespaces(): string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
declare module 'i18next' {
|
|
27
|
+
interface i18n {
|
|
28
|
+
reportNamespaces: ReportNamespaces;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
23
32
|
export interface TransProps extends Partial<i18next.WithT> {
|
|
24
33
|
children?: React.ReactNode;
|
|
25
34
|
components?: React.ReactNode[];
|
|
@@ -31,6 +40,7 @@ export interface TransProps extends Partial<i18next.WithT> {
|
|
|
31
40
|
parent?: React.ReactNode;
|
|
32
41
|
tOptions?: {};
|
|
33
42
|
values?: {};
|
|
43
|
+
t?: i18next.TFunction;
|
|
34
44
|
}
|
|
35
45
|
export function Trans(props: TransProps): any;
|
|
36
46
|
|
|
@@ -51,18 +61,17 @@ export function useTranslation(
|
|
|
51
61
|
): UseTranslationResponse;
|
|
52
62
|
|
|
53
63
|
// Need to see usage to improve this
|
|
54
|
-
export function withSSR(): (
|
|
55
|
-
WrappedComponent: React.
|
|
64
|
+
export function withSSR(): <Props>(
|
|
65
|
+
WrappedComponent: React.ComponentType<Props>,
|
|
56
66
|
) => {
|
|
57
67
|
({
|
|
58
68
|
initialI18nStore,
|
|
59
69
|
initialLanguage,
|
|
60
70
|
...rest
|
|
61
71
|
}: {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}): React.ComponentElement<{}, React.Component<{}, any, any>>;
|
|
72
|
+
initialI18nStore: i18next.Resource;
|
|
73
|
+
initialLanguage: string;
|
|
74
|
+
} & Props): React.FunctionComponentElement<Props>;
|
|
66
75
|
getInitialProps: (ctx: unknown) => Promise<any>;
|
|
67
76
|
};
|
|
68
77
|
|
package/src/useSSR.js
CHANGED
|
@@ -3,7 +3,8 @@ import { getI18n, getHasUsedI18nextProvider, I18nContext } from './context';
|
|
|
3
3
|
|
|
4
4
|
export function useSSR(initialI18nStore, initialLanguage, props = {}) {
|
|
5
5
|
const { i18n: i18nFromProps } = props;
|
|
6
|
-
const
|
|
6
|
+
const ReactI18nContext = useContext(I18nContext);
|
|
7
|
+
const { i18n: i18nFromContext } = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {};
|
|
7
8
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
8
9
|
|
|
9
10
|
// opt out if is a cloned instance, eg. created by i18next-express-middleware on request
|
package/src/withTranslation.js
CHANGED
|
@@ -4,17 +4,17 @@ import { getDisplayName } from './utils';
|
|
|
4
4
|
|
|
5
5
|
export function withTranslation(ns, options = {}) {
|
|
6
6
|
return function Extend(WrappedComponent) {
|
|
7
|
-
function I18nextWithTranslation(
|
|
8
|
-
const [t, i18n, ready] = useTranslation(ns,
|
|
7
|
+
function I18nextWithTranslation({ forwardedRef, ...rest }) {
|
|
8
|
+
const [t, i18n, ready] = useTranslation(ns, rest);
|
|
9
9
|
|
|
10
10
|
const passDownProps = {
|
|
11
|
-
...
|
|
11
|
+
...rest,
|
|
12
12
|
t,
|
|
13
13
|
i18n,
|
|
14
14
|
tReady: ready,
|
|
15
15
|
};
|
|
16
|
-
if (options.withRef &&
|
|
17
|
-
passDownProps.ref =
|
|
16
|
+
if (options.withRef && forwardedRef) {
|
|
17
|
+
passDownProps.ref = forwardedRef;
|
|
18
18
|
}
|
|
19
19
|
return React.createElement(WrappedComponent, passDownProps);
|
|
20
20
|
}
|
|
@@ -25,6 +25,8 @@ export function withTranslation(ns, options = {}) {
|
|
|
25
25
|
|
|
26
26
|
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
const forwardRef = (props, ref) => <I18nextWithTranslation {...props} forwardedRef={ref} />;
|
|
29
|
+
|
|
30
|
+
return options.withRef ? React.forwardRef(forwardRef) : I18nextWithTranslation;
|
|
29
31
|
};
|
|
30
32
|
}
|