react-i18next 12.3.0 → 13.0.0

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +1 -1
  3. package/TransWithoutContext.d.ts +26 -16
  4. package/dist/amd/react-i18next.js +827 -1104
  5. package/dist/amd/react-i18next.min.js +1 -1
  6. package/dist/commonjs/I18nextProvider.js +10 -13
  7. package/dist/commonjs/Trans.js +37 -52
  8. package/dist/commonjs/TransWithoutContext.js +130 -168
  9. package/dist/commonjs/Translation.js +8 -22
  10. package/dist/commonjs/context.js +45 -76
  11. package/dist/commonjs/defaults.js +7 -16
  12. package/dist/commonjs/i18nInstance.js +2 -4
  13. package/dist/commonjs/index.js +52 -86
  14. package/dist/commonjs/initReactI18next.js +2 -5
  15. package/dist/commonjs/unescape.js +4 -11
  16. package/dist/commonjs/useSSR.js +10 -14
  17. package/dist/commonjs/useTranslation.js +57 -79
  18. package/dist/commonjs/utils.js +38 -47
  19. package/dist/commonjs/withSSR.js +9 -23
  20. package/dist/commonjs/withTranslation.js +18 -45
  21. package/dist/es/I18nextProvider.js +10 -10
  22. package/dist/es/Trans.js +36 -43
  23. package/dist/es/TransWithoutContext.js +129 -159
  24. package/dist/es/Translation.js +7 -14
  25. package/dist/es/context.js +30 -50
  26. package/dist/es/defaults.js +7 -10
  27. package/dist/es/i18nInstance.js +1 -1
  28. package/dist/es/index.js +6 -18
  29. package/dist/es/initReactI18next.js +2 -2
  30. package/dist/es/package.json +1 -1
  31. package/dist/es/unescape.js +4 -10
  32. package/dist/es/useSSR.js +10 -11
  33. package/dist/es/useTranslation.js +58 -73
  34. package/dist/es/utils.js +34 -39
  35. package/dist/es/withSSR.js +9 -15
  36. package/dist/es/withTranslation.js +17 -35
  37. package/dist/umd/react-i18next.js +828 -1105
  38. package/dist/umd/react-i18next.min.js +1 -1
  39. package/helpers.d.ts +3 -0
  40. package/icu.macro.d.ts +37 -39
  41. package/index.d.ts +44 -37
  42. package/initReactI18next.d.ts +1 -1
  43. package/package.json +36 -36
  44. package/react-i18next.js +828 -1105
  45. package/react-i18next.min.js +1 -1
  46. package/src/useTranslation.js +17 -8
  47. package/src/utils.js +27 -15
@@ -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";function t(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 r(e){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{};n%2?t(Object(r),!0).forEach((function(n){o(e,n,r[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):t(Object(r)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(r,n))}))}return e}function a(e){return(a="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<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function o(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function s(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}function c(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,n){var t=e&&("undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"]);if(null==t)return;var r,a,i=[],o=!0,s=!1;try{for(t=t.call(e);!(o=(r=t.next()).done)&&(i.push(r.value),!n||i.length!==n);o=!0);}catch(e){s=!0,a=e}finally{try{o||null==t.return||t.return()}finally{if(s)throw a}}return i}(e,n)||function(e,n){if(!e)return;if("string"==typeof e)return u(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 u(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 u(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}var l={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},f=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function p(e){var n={type:"tag",name:"",voidElement:!1,attrs:{},children:[]},t=e.match(/<\/?([^\s]+?)[/\s>]/);if(t&&(n.name=t[1],(l[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 a=new RegExp(f),i=null;null!==(i=a.exec(e));)if(i[0].trim())if(i[1]){var o=i[1].trim(),s=[o,""];o.indexOf("=")>-1&&(s=o.split("=")),n.attrs[s[0]]=s[1],a.lastIndex--}else i[2]&&(n.attrs[i[2]]=i[3].trim().substring(1,i[3].length-1));return n}var d=/<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,g=/^\s*$/,m=Object.create(null);var h=function(e,n){n||(n={}),n.components||(n.components=m);var t,r=[],a=[],i=-1,o=!1;if(0!==e.indexOf("<")){var s=e.indexOf("<");r.push({type:"text",content:-1===s?e:e.substring(0,s)})}return e.replace(d,(function(s,c){if(o){if(s!=="</"+t.name+">")return;o=!1}var u,l="/"!==s.charAt(1),f=s.startsWith("\x3c!--"),d=c+s.length,m=e.charAt(d);if(f){var h=p(s);return i<0?(r.push(h),r):((u=a[i]).children.push(h),r)}if(l&&(i++,"tag"===(t=p(s)).type&&n.components[t.name]&&(t.type="component",o=!0),t.voidElement||o||!m||"<"===m||t.children.push({type:"text",content:e.slice(d,e.indexOf("<",d))}),0===i&&r.push(t),(u=a[i-1])&&u.children.push(t),a[i]=t),(!l||t.voidElement)&&(i>-1&&(t.voidElement||t.name===s.slice(2,-1))&&(i--,t=-1===i?r:a[i]),!o&&"<"!==m&&m)){u=-1===i?r:a[i].children;var v=e.indexOf("<",d),y=e.slice(d,-1===v?void 0:v);g.test(y)&&(y=" "),(v>-1&&i+u.length>=0||" "!==y)&&u.push({type:"text",content:y})}})),r};function v(){if(console&&console.warn){for(var e,n=arguments.length,t=new Array(n),r=0;r<n;r++)t[r]=arguments[r];"string"==typeof t[0]&&(t[0]="react-i18next:: ".concat(t[0])),(e=console).warn.apply(e,t)}}var y={};function b(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&y[n[0]]||("string"==typeof n[0]&&(y[n[0]]=new Date),v.apply(void 0,n))}function O(e,n,t){e.loadNamespaces(n,(function(){if(e.isInitialized)t();else{e.on("initialized",(function n(){setTimeout((function(){e.off("initialized",n)}),0),t()}))}}))}function x(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r=n.languages[0],a=!!n.options&&n.options.fallbackLng,i=n.languages[n.languages.length-1];if("cimode"===r.toLowerCase())return!0;var o=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&&!o(n.isLanguageChangingTo,e))&&(!!n.hasResourceBundle(r,e)||(!(n.services.backendConnector.backend&&(!n.options.resources||n.options.partialBundledLanguages))||!(!o(r,e)||a&&!o(i,e))))}function w(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!n.languages||!n.languages.length)return b("i18n.languages were undefined or empty",n.languages),!0;var r=void 0!==n.options.ignoreJSONStructure;return r?n.hasLoadedNamespace(e,{precheck:function(n,r){if(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!r(n.isLanguageChangingTo,e))return!1}}):x(e,n,t)}function j(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}var S,E=/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g,N={"&amp;":"&","&#38;":"&","&lt;":"<","&#60;":"<","&gt;":">","&#62;":">","&apos;":"'","&#39;":"'","&quot;":'"',"&#34;":'"',"&nbsp;":" ","&#160;":" ","&copy;":"©","&#169;":"©","&reg;":"®","&#174;":"®","&hellip;":"…","&#8230;":"…","&#x2F;":"/","&#47;":"/"},k=function(e){return N[e]},I={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transWrapTextNodes:"",transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0,unescape:function(e){return e.replace(E,k)}};function P(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};I=r(r({},I),e)}function R(){return I}function C(e){S=e}function T(){return S}var A=["format"],L=["children","count","parent","i18nKey","context","tOptions","values","defaults","components","ns","i18n","t","shouldUnescape"];function U(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.props?e.props.children:e.children:[]}function B(e){return Array.isArray(e)?e:[e]}function K(e,t,i,o,s,c){if(""===t)return[];var u=o.transKeepBasicHtmlNodesFor||[],l=t&&new RegExp(u.join("|")).test(t);if(!e&&!l)return[t];var f={};!function e(t){B(t).forEach((function(t){"string"!=typeof t&&(U(t)?e(z(t)):"object"!==a(t)||n.isValidElement(t)||Object.assign(f,t))}))}(e);var p=h("<0>".concat(t,"</0>")),d=r(r({},f),s);function g(e,t,r){var a=z(e),i=v(a,t.children,r);return function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every((function(e){return n.isValidElement(e)}))}(a)&&0===i.length?a:i}function m(e,t,a,i,o){e.dummy&&(e.children=t),a.push(n.cloneElement(e,r(r({},e.props),{},{key:i}),o?void 0:t))}function v(t,s,f){var p=B(t);return B(s).reduce((function(t,s,h){var y,b,O,x=s.children&&s.children[0]&&s.children[0].content&&i.services.interpolator.interpolate(s.children[0].content,d,i.language);if("tag"===s.type){var w=p[parseInt(s.name,10)];!w&&1===f.length&&f[0][s.name]&&(w=f[0][s.name]),w||(w={});var j=0!==Object.keys(s.attrs).length?(y={props:s.attrs},(O=r({},b=w)).props=Object.assign(y.props,b.props),O):w,S=n.isValidElement(j),E=S&&U(s,!0)&&!s.voidElement,N=l&&"object"===a(j)&&j.dummy&&!S,k="object"===a(e)&&null!==e&&Object.hasOwnProperty.call(e,s.name);if("string"==typeof j){var I=i.services.interpolator.interpolate(j,d,i.language);t.push(I)}else if(U(j)||E){m(j,g(j,s,f),t,h)}else if(N){var P=v(p,s.children,f);t.push(n.cloneElement(j,r(r({},j.props),{},{key:h}),P))}else if(Number.isNaN(parseFloat(s.name))){if(k)m(j,g(j,s,f),t,h,s.voidElement);else if(o.transSupportBasicHtmlNodes&&u.indexOf(s.name)>-1)if(s.voidElement)t.push(n.createElement(s.name,{key:"".concat(s.name,"-").concat(h)}));else{var R=v(p,s.children,f);t.push(n.createElement(s.name,{key:"".concat(s.name,"-").concat(h)},R))}else if(s.voidElement)t.push("<".concat(s.name," />"));else{var C=v(p,s.children,f);t.push("<".concat(s.name,">").concat(C,"</").concat(s.name,">"))}}else if("object"!==a(j)||S)1===s.children.length&&x?t.push(n.cloneElement(j,r(r({},j.props),{},{key:h}),x)):t.push(n.cloneElement(j,r(r({},j.props),{},{key:h})));else{var T=s.children[0]?x:null;T&&t.push(T)}}else if("text"===s.type){var A=o.transWrapTextNodes,L=c?o.unescape(i.services.interpolator.interpolate(s.content,d,i.language)):i.services.interpolator.interpolate(s.content,d,i.language);A?t.push(n.createElement(A,{key:"".concat(s.name,"-").concat(h)},L)):t.push(L)}return t}),[])}return z(v([{dummy:!0,children:e||[]}],p,B(e||[]))[0])}function V(e){var t=e.children,i=e.count,o=e.parent,c=e.i18nKey,u=e.context,l=e.tOptions,f=void 0===l?{}:l,p=e.values,d=e.defaults,g=e.components,m=e.ns,h=e.i18n,y=e.t,O=e.shouldUnescape,x=s(e,L),w=h||T();if(!w)return b("You will need to pass in an i18next instance by using i18nextReactModule"),t;var j=y||w.t.bind(w)||function(e){return e};u&&(f.context=u);var S=r(r({},R()),w.options&&w.options.react),E=m||j.ns||w.options&&w.options.defaultNS;E="string"==typeof E?[E]:E||["translation"];var N=d||function e(t,r){if(!t)return"";var i="",o=B(t),c=r.transSupportBasicHtmlNodes&&r.transKeepBasicHtmlNodesFor?r.transKeepBasicHtmlNodesFor:[];return o.forEach((function(t,o){if("string"==typeof t)i+="".concat(t);else if(n.isValidElement(t)){var u=Object.keys(t.props).length,l=c.indexOf(t.type)>-1,f=t.props.children;if(!f&&l&&0===u)i+="<".concat(t.type,"/>");else if(f||l&&0===u)if(t.props.i18nIsDynamicList)i+="<".concat(o,"></").concat(o,">");else if(l&&1===u&&"string"==typeof f)i+="<".concat(t.type,">").concat(f,"</").concat(t.type,">");else{var p=e(f,r);i+="<".concat(o,">").concat(p,"</").concat(o,">")}else i+="<".concat(o,"></").concat(o,">")}else if(null===t)v("Trans: the passed in value is invalid - seems you passed in a null child.");else if("object"===a(t)){var d=t.format,g=s(t,A),m=Object.keys(g);if(1===m.length){var h=d?"".concat(m[0],", ").concat(d):m[0];i+="{{".concat(h,"}}")}else v("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",t)}else v("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}}.",t)})),i}(t,S)||S.transEmptyNodeValue||c,k=S.hashTransKey,I=c||(k?k(N):N),P=p?f.interpolation:{interpolation:r(r({},f.interpolation),{},{prefix:"#$?",suffix:"?$#"})},C=r(r(r(r({},f),{},{count:i},p),P),{},{defaultValue:N,ns:E}),U=K(g||t,I?j(I,C):N,w,S,C,O),z=void 0!==o?o:S.defaultTransParent;return z?n.createElement(z,x,U):U}var D={type:"3rdParty",init:function(e){P(e.options.react),C(e)}},F=n.createContext(),H=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 W(e){return function(n){return new Promise((function(t){var a=M();e.getInitialProps?e.getInitialProps(n).then((function(e){t(r(r({},e),a))})):t(a)}))}}function M(){var e=T(),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}var $=["children","count","parent","i18nKey","context","tOptions","values","defaults","components","ns","i18n","t","shouldUnescape"];var q=function(e,t){var r=n.useRef();return n.useEffect((function(){r.current=t?r.current:e}),[e,t]),r.current};function Y(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=t.i18n,o=n.useContext(F)||{},s=o.i18n,u=o.defaultNS,l=i||s||T();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new H),!l){b("You will need to pass in an i18next instance by using initReactI18next");var f=function(e,n){return"string"==typeof n?n:n&&"object"===a(n)&&"string"==typeof n.defaultValue?n.defaultValue:Array.isArray(e)?e[e.length-1]:e},p=[f,{},!1];return p.t=f,p.i18n={},p.ready=!1,p}l.options.react&&void 0!==l.options.react.wait&&b("It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.");var d=r(r(r({},R()),l.options.react),t),g=d.useSuspense,m=d.keyPrefix,h=e||u||l.options&&l.options.defaultNS;h="string"==typeof h?[h]:h||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(h);var v=(l.isInitialized||l.initializedStoreOnce)&&h.every((function(e){return w(e,l,d)}));function y(){return l.getFixedT(t.lng||null,"fallback"===d.nsMode?h:h[0],m)}var x=n.useState(y),j=c(x,2),S=j[0],E=j[1],N=h.join(),k=q(N),I=n.useRef(!0);n.useEffect((function(){var e=d.bindI18n,n=d.bindI18nStore;function t(){I.current&&E(y)}return I.current=!0,v||g||O(l,h,(function(){I.current&&E(y)})),v&&k&&k!==N&&I.current&&E(y),e&&l&&l.on(e,t),n&&l&&l.store.on(n,t),function(){I.current=!1,e&&l&&e.split(" ").forEach((function(e){return l.off(e,t)})),n&&l&&n.split(" ").forEach((function(e){return l.store.off(e,t)}))}}),[l,N]);var P=n.useRef(!0);n.useEffect((function(){I.current&&!P.current&&E(y),P.current=!1}),[l,m]);var C=[S,l,v];if(C.t=S,C.i18n=l,C.ready=v,v)return C;if(!v&&!g)return C;throw new Promise((function(e){O(l,h,(function(){e()}))}))}var _=["forwardedRef"];var J=["ns","children"];function Z(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=r.i18n,i=n.useContext(F)||{},o=i.i18n,s=a||o||T();s.options&&s.options.isClone||(e&&!s.initializedStoreOnce&&(s.services.resourceStore.data=e,s.options.ns=Object.values(e).reduce((function(e,n){return Object.keys(n).forEach((function(n){e.indexOf(n)<0&&e.push(n)})),e}),s.options.ns),s.initializedStoreOnce=!0,s.isInitialized=!0),t&&!s.initializedLanguageOnce&&(s.changeLanguage(t),s.initializedLanguageOnce=!0))}var G=["initialI18nStore","initialLanguage"];e.I18nContext=F,e.I18nextProvider=function(e){var t=e.i18n,r=e.defaultNS,a=e.children,i=n.useMemo((function(){return{i18n:t,defaultNS:r}}),[t,r]);return n.createElement(F.Provider,{value:i},a)},e.Trans=function(e){var t=e.children,a=e.count,i=e.parent,o=e.i18nKey,c=e.context,u=e.tOptions,l=void 0===u?{}:u,f=e.values,p=e.defaults,d=e.components,g=e.ns,m=e.i18n,h=e.t,v=e.shouldUnescape,y=s(e,$),b=n.useContext(F)||{},O=b.i18n,x=b.defaultNS,w=m||O||T(),j=h||w&&w.t.bind(w);return V(r({children:t,count:a,parent:i,i18nKey:o,context:c,tOptions:l,values:f,defaults:p,components:d,ns:g||j&&j.ns||x||w&&w.options&&w.options.defaultNS,i18n:w,t:h,shouldUnescape:v},y))},e.TransWithoutContext=V,e.Translation=function(e){var n=e.ns,t=e.children,r=c(Y(n,s(e,J)),3),a=r[0],i=r[1],o=r[2];return t(a,{i18n:i,lng:i.language},o)},e.composeInitialProps=W,e.date=function(){return""},e.getDefaults=R,e.getI18n=T,e.getInitialProps=M,e.initReactI18next=D,e.number=function(){return""},e.plural=function(){return""},e.select=function(){return""},e.selectOrdinal=function(){return""},e.setDefaults=P,e.setI18n=C,e.time=function(){return""},e.useSSR=Z,e.useTranslation=Y,e.withSSR=function(){return function(e){function t(t){var a=t.initialI18nStore,i=t.initialLanguage,o=s(t,G);return Z(a,i),n.createElement(e,r({},o))}return t.getInitialProps=W(e),t.displayName="withI18nextSSR(".concat(j(e),")"),t.WrappedComponent=e,t}},e.withTranslation=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(a){function i(i){var o=i.forwardedRef,u=s(i,_),l=c(Y(e,r(r({},u),{},{keyPrefix:t.keyPrefix})),3),f=l[0],p=l[1],d=l[2],g=r(r({},u),{},{t:f,i18n:p,tReady:d});return t.withRef&&o?g.ref=o:!t.withRef&&o&&(g.forwardedRef=o),n.createElement(a,g)}i.displayName="withI18nextTranslation(".concat(j(a),")"),i.WrappedComponent=a;return t.withRef?n.forwardRef((function(e,t){return n.createElement(i,Object.assign({},e,{forwardedRef:t}))})):i}},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="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}),i=/\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 o=new RegExp(i),a=null;null!==(a=o.exec(e));)if(a[0].trim())if(a[1]){var c=a[1].trim(),l=[c,""];c.indexOf("=")>-1&&(l=c.split("=")),n.attrs[l[0]]=l[1],o.lastIndex--}else a[2]&&(n.attrs[a[2]]=a[3].trim().substring(1,a[3].length-1));return n}var o=/<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,a=/^\s*$/,c=Object.create(null);function l(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(l,"")+"</"+n.name+">";case"comment":return e+"\x3c!--"+n.comment+"--\x3e"}}var u={parse:function(e,n){n||(n={}),n.components||(n.components=c);var t,s=[],i=[],l=-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(o,(function(o,c){if(u){if(o!=="</"+t.name+">")return;u=!1}var p,d="/"!==o.charAt(1),f=o.startsWith("\x3c!--"),g=c+o.length,h=e.charAt(g);if(f){var m=r(o);return l<0?(s.push(m),s):((p=i[l]).children.push(m),s)}if(d&&(l++,"tag"===(t=r(o)).type&&n.components[t.name]&&(t.type="component",u=!0),t.voidElement||u||!h||"<"===h||t.children.push({type:"text",content:e.slice(g,e.indexOf("<",g))}),0===l&&s.push(t),(p=i[l-1])&&p.children.push(t),i[l]=t),(!d||t.voidElement)&&(l>-1&&(t.voidElement||t.name===o.slice(2,-1))&&(l--,t=-1===l?s:i[l]),!u&&"<"!==h&&h)){p=-1===l?s:i[l].children;var y=e.indexOf("<",g),v=e.slice(g,-1===y?void 0:y);a.test(v)&&(v=" "),(y>-1&&l+p.length>=0||" "!==v)&&p.push({type:"text",content:v})}})),s},stringify:function(e){return e.reduce((function(e,n){return e+l("",n)}),"")}};function p(){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:: ${n[0]}`),console.warn(...n)}}const d={};function f(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&d[n[0]]||("string"==typeof n[0]&&(d[n[0]]=new Date),p(...n))}const g=(e,n)=>()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout((()=>{e.off("initialized",t)}),0),n()};e.on("initialized",t)}};function h(e,n,t){e.loadNamespaces(n,g(e,t))}function m(e,n,t,s){"string"==typeof t&&(t=[t]),t.forEach((n=>{e.options.ns.indexOf(n)<0&&e.options.ns.push(n)})),e.loadLanguages(n,g(e,s))}function y(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}const v=/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g,b={"&amp;":"&","&#38;":"&","&lt;":"<","&#60;":"<","&gt;":">","&#62;":">","&apos;":"'","&#39;":"'","&quot;":'"',"&#34;":'"',"&nbsp;":" ","&#160;":" ","&copy;":"©","&#169;":"©","&reg;":"®","&#174;":"®","&hellip;":"…","&#8230;":"…","&#x2F;":"/","&#47;":"/"},x=e=>b[e];let E,N={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transWrapTextNodes:"",transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0,unescape:e=>e.replace(v,x)};function O(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};N={...N,...e}}function $(){return N}function w(e){E=e}function k(){return E}function I(e,n){if(!e)return!1;const t=e.props?e.props.children:e.children;return n?t.length>0:!!t}function S(e){return e?e.props?e.props.children:e.children:[]}function j(e){return Array.isArray(e)?e:[e]}function R(e,t){if(!e)return"";let s="";const i=j(e),r=t.transSupportBasicHtmlNodes&&t.transKeepBasicHtmlNodesFor?t.transKeepBasicHtmlNodesFor:[];return i.forEach(((e,i)=>{if("string"==typeof e)s+=`${e}`;else if(n.isValidElement(e)){const n=Object.keys(e.props).length,o=r.indexOf(e.type)>-1,a=e.props.children;if(!a&&o&&0===n)s+=`<${e.type}/>`;else if(a||o&&0===n)if(e.props.i18nIsDynamicList)s+=`<${i}></${i}>`;else if(o&&1===n&&"string"==typeof a)s+=`<${e.type}>${a}</${e.type}>`;else{const e=R(a,t);s+=`<${i}>${e}</${i}>`}else s+=`<${i}></${i}>`}else if(null===e)p("Trans: the passed in value is invalid - seems you passed in a null child.");else if("object"==typeof e){const{format:n,...t}=e,i=Object.keys(t);if(1===i.length){const e=n?`${i[0]}, ${n}`:i[0];s+=`{{${e}}}`}else p("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",e)}else p("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)})),s}function C(e,t,s,i,r,o){if(""===t)return[];const a=i.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(a.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){j(t).forEach((t=>{"string"!=typeof t&&(I(t)?e(S(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))}))}(e);const p=u.parse(`<0>${t}</0>`),d={...l,...r};function f(e,t,s){const i=S(e),r=h(i,t.children,s);return function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every((e=>n.isValidElement(e)))}(i)&&0===r.length?i:r}function g(e,t,s,i,r){e.dummy&&(e.children=t),s.push(n.cloneElement(e,{...e.props,key:i},r?void 0:t))}function h(t,r,l){const u=j(t);return j(r).reduce(((t,r,p)=>{const m=r.children&&r.children[0]&&r.children[0].content&&s.services.interpolator.interpolate(r.children[0].content,d,s.language);if("tag"===r.type){let o=u[parseInt(r.name,10)];!o&&1===l.length&&l[0][r.name]&&(o=l[0][r.name]),o||(o={});const y=0!==Object.keys(r.attrs).length?function(e,n){const t={...n};return t.props=Object.assign(e.props,n.props),t}({props:r.attrs},o):o,v=n.isValidElement(y),b=v&&I(r,!0)&&!r.voidElement,x=c&&"object"==typeof y&&y.dummy&&!v,E="object"==typeof e&&null!==e&&Object.hasOwnProperty.call(e,r.name);if("string"==typeof y){const e=s.services.interpolator.interpolate(y,d,s.language);t.push(e)}else if(I(y)||b){g(y,f(y,r,l),t,p)}else if(x){const e=h(u,r.children,l);t.push(n.cloneElement(y,{...y.props,key:p},e))}else if(Number.isNaN(parseFloat(r.name)))if(E){g(y,f(y,r,l),t,p,r.voidElement)}else if(i.transSupportBasicHtmlNodes&&a.indexOf(r.name)>-1)if(r.voidElement)t.push(n.createElement(r.name,{key:`${r.name}-${p}`}));else{const e=h(u,r.children,l);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,l);t.push(`<${r.name}>${e}</${r.name}>`)}else if("object"!=typeof y||v)1===r.children.length&&m?t.push(n.cloneElement(y,{...y.props,key:p},m)):t.push(n.cloneElement(y,{...y.props,key:p}));else{const e=r.children[0]?m:null;e&&t.push(e)}}else if("text"===r.type){const e=i.transWrapTextNodes,a=o?i.unescape(s.services.interpolator.interpolate(r.content,d,s.language)):s.services.interpolator.interpolate(r.content,d,s.language);e?t.push(n.createElement(e,{key:`${r.name}-${p}`},a)):t.push(a)}return t}),[])}return S(h([{dummy:!0,children:e||[]}],p,j(e||[]))[0])}function T(e){let{children:t,count:s,parent:i,i18nKey:r,context:o,tOptions:a={},values:c,defaults:l,components:u,ns:p,i18n:d,t:g,shouldUnescape:h,...m}=e;const y=d||k();if(!y)return f("You will need to pass in an i18next instance by using i18nextReactModule"),t;const v=g||y.t.bind(y)||(e=>e);o&&(a.context=o);const b={...$(),...y.options&&y.options.react};let x=p||v.ns||y.options&&y.options.defaultNS;x="string"==typeof x?[x]:x||["translation"];const E=l||R(t,b)||b.transEmptyNodeValue||r,{hashTransKey:N}=b,O=r||(N?N(E):E),w=c?a.interpolation:{interpolation:{...a.interpolation,prefix:"#$?",suffix:"?$#"}},I={...a,count:s,...c,...w,defaultValue:E,ns:x},S=C(u||t,O?v(O,I):E,y,b,I,h),j=void 0!==i?i:b.defaultTransParent;return j?n.createElement(j,m,S):S}const P={type:"3rdParty",init(e){O(e.options.react),w(e)}},L=n.createContext();class z{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach((e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)}))}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function A(e){return n=>new Promise((t=>{const s=B();e.getInitialProps?e.getInitialProps(n).then((e=>{t({...e,...s})})):t(s)}))}function B(){const e=k(),n=e.reportNamespaces?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 U=(e,t)=>{const s=n.useRef();return n.useEffect((()=>{s.current=t?s.current:e}),[e,t]),s.current};function V(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{i18n:s}=t,{i18n:i,defaultNS:r}=n.useContext(L)||{},o=s||i||k();if(o&&!o.reportNamespaces&&(o.reportNamespaces=new z),!o){f("You will need to pass in an i18next instance by using initReactI18next");const e=(e,n)=>"string"==typeof n?n:n&&"object"==typeof n&&"string"==typeof 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}o.options.react&&void 0!==o.options.react.wait&&f("It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.");const a={...$(),...o.options.react,...t},{useSuspense:c,keyPrefix:l}=a;let u=e||r||o.options&&o.options.defaultNS;u="string"==typeof u?[u]:u||["translation"],o.reportNamespaces.addUsedNamespaces&&o.reportNamespaces.addUsedNamespaces(u);const p=(o.isInitialized||o.initializedStoreOnce)&&u.every((e=>function(e,n){let t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return n.languages&&n.languages.length?void 0!==n.options.ignoreJSONStructure?n.hasLoadedNamespace(e,{lng:t.lng,precheck:(n,s)=>{if(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!s(n.isLanguageChangingTo,e))return!1}}):function(e,n){let t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const s=n.languages[0],i=!!n.options&&n.options.fallbackLng,r=n.languages[n.languages.length-1];if("cimode"===s.toLowerCase())return!0;const o=(e,t)=>{const s=n.services.backendConnector.state[`${e}|${t}`];return-1===s||2===s};return!(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!o(n.isLanguageChangingTo,e)||!n.hasResourceBundle(s,e)&&n.services.backendConnector.backend&&(!n.options.resources||n.options.partialBundledLanguages)&&(!o(s,e)||i&&!o(r,e)))}(e,n,t):(f("i18n.languages were undefined or empty",n.languages),!0)}(e,o,a)));function d(){return o.getFixedT(t.lng||null,"fallback"===a.nsMode?u:u[0],l)}const[g,y]=n.useState(d);let v=u.join();t.lng&&(v=`${t.lng}${v}`);const b=U(v),x=n.useRef(!0);n.useEffect((()=>{const{bindI18n:e,bindI18nStore:n}=a;function s(){x.current&&y(d)}return x.current=!0,p||c||(t.lng?m(o,t.lng,u,(()=>{x.current&&y(d)})):h(o,u,(()=>{x.current&&y(d)}))),p&&b&&b!==v&&x.current&&y(d),e&&o&&o.on(e,s),n&&o&&o.store.on(n,s),()=>{x.current=!1,e&&o&&e.split(" ").forEach((e=>o.off(e,s))),n&&o&&n.split(" ").forEach((e=>o.store.off(e,s)))}}),[o,v]);const E=n.useRef(!0);n.useEffect((()=>{x.current&&!E.current&&y(d),E.current=!1}),[o,l]);const N=[g,o,p];if(N.t=g,N.i18n=o,N.ready=p,p)return N;if(!p&&!c)return N;throw new Promise((e=>{t.lng?m(o,t.lng,u,(()=>e())):h(o,u,(()=>e()))}))}function F(e,t){let s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const{i18n:i}=s,{i18n:r}=n.useContext(L)||{},o=i||r||k();o.options&&o.options.isClone||(e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.options.ns=Object.values(e).reduce(((e,n)=>(Object.keys(n).forEach((n=>{e.indexOf(n)<0&&e.push(n)})),e)),o.options.ns),o.initializedStoreOnce=!0,o.isInitialized=!0),t&&!o.initializedLanguageOnce&&(o.changeLanguage(t),o.initializedLanguageOnce=!0))}e.I18nContext=L,e.I18nextProvider=function(e){let{i18n:t,defaultNS:s,children:i}=e;const r=n.useMemo((()=>({i18n:t,defaultNS:s})),[t,s]);return n.createElement(L.Provider,{value:r},i)},e.Trans=function(e){let{children:t,count:s,parent:i,i18nKey:r,context:o,tOptions:a={},values:c,defaults:l,components:u,ns:p,i18n:d,t:f,shouldUnescape:g,...h}=e;const{i18n:m,defaultNS:y}=n.useContext(L)||{},v=d||m||k(),b=f||v&&v.t.bind(v);return T({children:t,count:s,parent:i,i18nKey:r,context:o,tOptions:a,values:c,defaults:l,components:u,ns:p||b&&b.ns||y||v&&v.options&&v.options.defaultNS,i18n:v,t:f,shouldUnescape:g,...h})},e.TransWithoutContext=T,e.Translation=function(e){const{ns:n,children:t,...s}=e,[i,r,o]=V(n,s);return t(i,{i18n:r,lng:r.language},o)},e.composeInitialProps=A,e.date=()=>"",e.getDefaults=$,e.getI18n=k,e.getInitialProps=B,e.initReactI18next=P,e.number=()=>"",e.plural=()=>"",e.select=()=>"",e.selectOrdinal=()=>"",e.setDefaults=O,e.setI18n=w,e.time=()=>"",e.useSSR=F,e.useTranslation=V,e.withSSR=function(){return function(e){function t(t){let{initialI18nStore:s,initialLanguage:i,...r}=t;return F(s,i),n.createElement(e,{...r})}return t.getInitialProps=A(e),t.displayName=`withI18nextSSR(${y(e)})`,t.WrappedComponent=e,t}},e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(s){function i(i){let{forwardedRef:r,...o}=i;const[a,c,l]=V(e,{...o,keyPrefix:t.keyPrefix}),u={...o,t:a,i18n:c,tReady:l};return t.withRef&&r?u.ref=r:!t.withRef&&r&&(u.forwardedRef=r),n.createElement(s,u)}i.displayName=`withI18nextTranslation(${y(s)})`,i.WrappedComponent=s;return t.withRef?n.forwardRef(((e,t)=>n.createElement(i,Object.assign({},e,{forwardedRef:t})))):i}}}));
package/helpers.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Internal Helpers
2
+ export type $Tuple<T> = readonly [T?, ...T[]];
3
+ export type $Subtract<T extends K, K> = Omit<T, keyof K>;
package/icu.macro.d.ts CHANGED
@@ -1,46 +1,47 @@
1
- import React from 'react';
1
+ import { ReactElement } from 'react';
2
2
  import { Trans } from './';
3
- import { Namespace, DefaultNamespace, TFuncKey, i18n } from 'i18next';
3
+ import type { Namespace, TypeOptions, i18n, ParseKeys } from 'i18next';
4
4
 
5
5
  export { Trans };
6
6
 
7
+ type _DefaultNamespace = TypeOptions['defaultNS'];
7
8
  declare module 'react-i18next/icu.macro' {
8
9
  export interface PluralSubProps<
9
- K extends TFuncKey<N> extends infer A ? A : never,
10
- N extends Namespace = DefaultNamespace
10
+ Key extends ParseKeys<Ns, {}, ''>,
11
+ Ns extends Namespace = _DefaultNamespace,
11
12
  > {
12
13
  children?: never;
13
- i18nKey?: K;
14
+ i18nKey?: Key;
14
15
  i18n?: i18n;
15
- ns?: N;
16
+ ns?: Ns;
16
17
  count: number;
17
18
  values?: {};
18
- zero?: string | React.ReactElement;
19
- one?: string | React.ReactElement;
20
- two?: string | React.ReactElement;
21
- few?: string | React.ReactElement;
22
- many?: string | React.ReactElement;
23
- other: string | React.ReactElement;
19
+ zero?: string | ReactElement;
20
+ one?: string | ReactElement;
21
+ two?: string | ReactElement;
22
+ few?: string | ReactElement;
23
+ many?: string | ReactElement;
24
+ other: string | ReactElement;
24
25
  }
25
26
 
26
27
  type PluralProps<
27
28
  T,
28
- K extends TFuncKey<N> extends infer A ? A : never,
29
- N extends Namespace = DefaultNamespace
29
+ Key extends ParseKeys<Ns, {}, ''>,
30
+ Ns extends Namespace = _DefaultNamespace,
30
31
  > = {
31
- [P in keyof T]: P extends keyof PluralSubProps<K, N>
32
+ [P in keyof T]: P extends keyof PluralSubProps<Key, Ns>
32
33
  ? // support the standard properties of Plural
33
- PluralSubProps<K, N>[P]
34
+ PluralSubProps<Key, Ns>[P]
34
35
  : // this supports infinite $0={..} or $123={..}
35
36
  // technically it also supports $-1={..} and $2.3={..} but we don't need to
36
37
  // worry since that's invalid syntax.
37
38
  P extends `$${number}`
38
- ? string | React.ReactElement
39
+ ? string | ReactElement
39
40
  : never;
40
41
  };
41
42
 
42
43
  interface SelectSubProps {
43
- [key: string]: string | React.ReactElement;
44
+ [key: string]: string | ReactElement;
44
45
  }
45
46
 
46
47
  interface NoChildren {
@@ -48,44 +49,41 @@ declare module 'react-i18next/icu.macro' {
48
49
  }
49
50
 
50
51
  interface SelectRequiredProps<
51
- K extends TFuncKey<N> extends infer A ? A : never,
52
- N extends Namespace = DefaultNamespace
52
+ Key extends ParseKeys<Ns, {}, ''>,
53
+ Ns extends Namespace = _DefaultNamespace,
53
54
  > extends NoChildren {
54
- i18nKey?: K;
55
+ i18nKey?: Key;
55
56
  i18n?: i18n;
56
- ns?: N;
57
- other: string | React.ReactElement;
57
+ ns?: Ns;
58
+ other: string | ReactElement;
58
59
  }
59
60
 
60
61
  // defining it this way ensures that `other` is always defined, but allows
61
62
  // unlimited other select types.
62
63
  type SelectProps<
63
- K extends TFuncKey<N> extends infer A ? A : never,
64
- N extends Namespace = DefaultNamespace
65
- > = SelectSubProps & SelectRequiredProps<K, N>;
64
+ Key extends ParseKeys<Ns, {}, ''>,
65
+ Ns extends Namespace = _DefaultNamespace,
66
+ > = SelectSubProps & SelectRequiredProps<Key, Ns>;
66
67
 
67
- function Plural<
68
- T,
69
- K extends TFuncKey<N> extends infer A ? A : never,
70
- N extends Namespace = DefaultNamespace
71
- >(props: PluralProps<T, K, N> & NoChildren): React.ReactElement;
68
+ function Plural<T, Key extends ParseKeys<Ns, {}, ''>, Ns extends Namespace = _DefaultNamespace>(
69
+ props: PluralProps<T, Key, Ns> & NoChildren,
70
+ ): ReactElement;
72
71
 
73
72
  function SelectOrdinal<
74
73
  T,
75
- K extends TFuncKey<N> extends infer A ? A : never,
76
- N extends Namespace = DefaultNamespace
77
- >(props: PluralProps<T, K, N> & NoChildren): React.ReactElement;
74
+ Key extends ParseKeys<Ns, {}, ''>,
75
+ Ns extends Namespace = _DefaultNamespace,
76
+ >(props: PluralProps<T, Key, Ns> & NoChildren): ReactElement;
78
77
 
79
- function Select<
80
- K extends TFuncKey<N> extends infer A ? A : never,
81
- N extends Namespace = DefaultNamespace
82
- >(props: SelectProps<K, N>): React.ReactElement;
78
+ function Select<Key extends ParseKeys<Ns, {}, ''>, Ns extends Namespace = _DefaultNamespace>(
79
+ props: SelectProps<Key, Ns>,
80
+ ): ReactElement;
83
81
 
84
82
  function date(strings: TemplateStringsArray, variable: Date): string;
85
83
  function time(strings: TemplateStringsArray, variable: Date): string;
86
84
  function number(strings: TemplateStringsArray, variable: number): string;
87
85
 
88
- type ValidInterpolations = React.ReactElement | string;
86
+ type ValidInterpolations = ReactElement | string;
89
87
 
90
88
  function plural(
91
89
  strings: TemplateStringsArray,
package/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import i18next, {
1
+ import type { $Subtract, $Tuple } from './helpers';
2
+ import type {
2
3
  ReactOptions,
3
4
  i18n,
4
5
  Resource,
6
+ FlatNamespace,
5
7
  Namespace,
6
8
  TypeOptions,
7
9
  TFunction,
@@ -11,8 +13,6 @@ import * as React from 'react';
11
13
  export { Trans, TransProps } from './TransWithoutContext';
12
14
  export { initReactI18next } from './initReactI18next';
13
15
 
14
- type Subtract<T extends K, K> = Omit<T, keyof K>;
15
-
16
16
  export function setDefaults(options: ReactOptions): void;
17
17
  export function getDefaults(): ReactOptions;
18
18
  export function setI18n(instance: i18n): void;
@@ -48,42 +48,49 @@ declare module 'react' {
48
48
  }
49
49
  }
50
50
 
51
- type DefaultNamespace = TypeOptions['defaultNS'];
51
+ type _DefaultNamespace = TypeOptions['defaultNS'];
52
52
 
53
53
  export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;
54
54
 
55
- export interface UseTranslationOptions<TKPrefix = undefined> {
55
+ export interface UseTranslationOptions<KPrefix> {
56
56
  i18n?: i18n;
57
57
  useSuspense?: boolean;
58
- keyPrefix?: TKPrefix;
58
+ keyPrefix?: KPrefix;
59
59
  bindI18n?: string | false;
60
60
  nsMode?: 'fallback' | 'default';
61
61
  lng?: string;
62
62
  // other of these options might also work: https://github.com/i18next/i18next/blob/master/index.d.ts#L127
63
63
  }
64
64
 
65
- export type UseTranslationResponse<N extends Namespace, TKPrefix = undefined> = [
66
- TFunction<N, TKPrefix>,
67
- i18n,
68
- boolean,
65
+ export type UseTranslationResponse<Ns extends Namespace, KPrefix> = [
66
+ t: TFunction<Ns, KPrefix>,
67
+ i18n: i18n,
68
+ ready: boolean,
69
69
  ] & {
70
- t: TFunction<N, TKPrefix>;
70
+ t: TFunction<Ns, KPrefix>;
71
71
  i18n: i18n;
72
72
  ready: boolean;
73
73
  };
74
74
 
75
+ // Workaround to make code completion to work when suggesting namespaces.
76
+ // This is a typescript limitation when using generics with default values,
77
+ // it'll be addressed in this issue: https://github.com/microsoft/TypeScript/issues/52516
78
+ export type FallbackNs<Ns> = Ns extends undefined
79
+ ? _DefaultNamespace
80
+ : Ns extends Namespace
81
+ ? Ns
82
+ : _DefaultNamespace;
83
+
75
84
  export function useTranslation<
76
- N extends Namespace = DefaultNamespace,
77
- TKPrefix extends KeyPrefix<N> = undefined
85
+ Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
86
+ KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
78
87
  >(
79
- ns?: N | Readonly<N>,
80
- options?: UseTranslationOptions<TKPrefix>,
81
- ): UseTranslationResponse<N, TKPrefix>;
88
+ ns?: Ns,
89
+ options?: UseTranslationOptions<KPrefix>,
90
+ ): UseTranslationResponse<FallbackNs<Ns>, KPrefix>;
82
91
 
83
92
  // Need to see usage to improve this
84
- export function withSSR(): <Props>(
85
- WrappedComponent: React.ComponentType<Props>,
86
- ) => {
93
+ export function withSSR(): <Props>(WrappedComponent: React.ComponentType<Props>) => {
87
94
  ({
88
95
  initialI18nStore,
89
96
  initialLanguage,
@@ -96,10 +103,10 @@ export function withSSR(): <Props>(
96
103
  };
97
104
 
98
105
  export interface WithTranslation<
99
- N extends Namespace = DefaultNamespace,
100
- TKPrefix extends KeyPrefix<N> = undefined
106
+ Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
107
+ KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
101
108
  > {
102
- t: TFunction<N, TKPrefix>;
109
+ t: TFunction<FallbackNs<Ns>, KPrefix>;
103
110
  i18n: i18n;
104
111
  tReady: boolean;
105
112
  }
@@ -110,23 +117,23 @@ export interface WithTranslationProps {
110
117
  }
111
118
 
112
119
  export function withTranslation<
113
- N extends Namespace = DefaultNamespace,
114
- TKPrefix extends KeyPrefix<N> = undefined
120
+ Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
121
+ KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
115
122
  >(
116
- ns?: N,
123
+ ns?: Ns,
117
124
  options?: {
118
125
  withRef?: boolean;
119
- keyPrefix?: TKPrefix;
126
+ keyPrefix?: KPrefix;
120
127
  },
121
128
  ): <
122
129
  C extends React.ComponentType<React.ComponentProps<any> & WithTranslationProps>,
123
130
  ResolvedProps = JSX.LibraryManagedAttributes<
124
131
  C,
125
- Subtract<React.ComponentProps<C>, WithTranslationProps>
126
- >
132
+ $Subtract<React.ComponentProps<C>, WithTranslationProps>
133
+ >,
127
134
  >(
128
135
  component: C,
129
- ) => React.ComponentType<Omit<ResolvedProps, keyof WithTranslation<N>> & WithTranslationProps>;
136
+ ) => React.ComponentType<Omit<ResolvedProps, keyof WithTranslation<Ns>> & WithTranslationProps>;
130
137
 
131
138
  export interface I18nextProviderProps {
132
139
  children?: React.ReactNode;
@@ -138,25 +145,25 @@ export const I18nextProvider: React.FunctionComponent<I18nextProviderProps>;
138
145
  export const I18nContext: React.Context<{ i18n: i18n }>;
139
146
 
140
147
  export interface TranslationProps<
141
- N extends Namespace = DefaultNamespace,
142
- TKPrefix extends KeyPrefix<N> = undefined
148
+ Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
149
+ KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
143
150
  > {
144
151
  children: (
145
- t: TFunction<N, TKPrefix>,
152
+ t: TFunction<FallbackNs<Ns>, KPrefix>,
146
153
  options: {
147
154
  i18n: i18n;
148
155
  lng: string;
149
156
  },
150
157
  ready: boolean,
151
158
  ) => React.ReactNode;
152
- ns?: N;
159
+ ns?: Ns;
153
160
  i18n?: i18n;
154
161
  useSuspense?: boolean;
155
- keyPrefix?: TKPrefix;
162
+ keyPrefix?: KPrefix;
156
163
  nsMode?: 'fallback' | 'default';
157
164
  }
158
165
 
159
166
  export function Translation<
160
- N extends Namespace = DefaultNamespace,
161
- TKPrefix extends KeyPrefix<N> = undefined
162
- >(props: TranslationProps<N, TKPrefix>): any;
167
+ Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
168
+ KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
169
+ >(props: TranslationProps<Ns, KPrefix>): any;
@@ -1,3 +1,3 @@
1
- import { ThirdPartyModule } from 'i18next';
1
+ import type { ThirdPartyModule } from 'i18next';
2
2
 
3
3
  export const initReactI18next: ThirdPartyModule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-i18next",
3
- "version": "12.3.0",
3
+ "version": "13.0.0",
4
4
  "description": "Internationalization for react done right. Using the i18next i18n ecosystem.",
5
5
  "main": "dist/commonjs/index.js",
6
6
  "types": "./index.d.ts",
@@ -48,65 +48,65 @@
48
48
  "url": "https://github.com/i18next/react-i18next.git"
49
49
  },
50
50
  "dependencies": {
51
- "@babel/runtime": "^7.20.6",
51
+ "@babel/runtime": "^7.22.5",
52
52
  "html-parse-stringify": "^3.0.1"
53
53
  },
54
54
  "devDependencies": {
55
- "@babel/cli": "^7.14.5",
56
- "@babel/core": "^7.14.5",
57
- "@babel/plugin-proposal-async-generator-functions": "^7.14.5",
58
- "@babel/plugin-proposal-object-rest-spread": "^7.14.5",
59
- "@babel/plugin-transform-modules-commonjs": "^7.14.5",
60
- "@babel/plugin-transform-runtime": "^7.14.5",
55
+ "@babel/cli": "^7.22.5",
56
+ "@babel/core": "^7.22.5",
57
+ "@babel/plugin-proposal-async-generator-functions": "^7.20.5",
58
+ "@babel/plugin-proposal-object-rest-spread": "^7.20.5",
59
+ "@babel/plugin-transform-modules-commonjs": "^7.22.5",
60
+ "@babel/plugin-transform-runtime": "^7.22.5",
61
61
  "@babel/polyfill": "^7.2.5",
62
- "@babel/preset-env": "^7.14.5",
63
- "@babel/preset-react": "^7.14.5",
64
- "@babel/register": "^7.14.5",
65
- "@testing-library/jest-dom": "^5.11.6",
62
+ "@babel/preset-env": "^7.22.5",
63
+ "@babel/preset-react": "^7.22.5",
64
+ "@babel/register": "^7.22.5",
65
+ "@rollup/plugin-babel": "^6.0.3",
66
+ "@rollup/plugin-commonjs": "^25.0.1",
67
+ "@rollup/plugin-node-resolve": "^15.1.0",
68
+ "@rollup/plugin-terser": "0.4.3",
69
+ "@rollup/plugin-replace": "^5.0.2",
70
+ "@testing-library/jest-dom": "^5.16.5",
66
71
  "@testing-library/react": "^11.2.7",
67
72
  "@testing-library/react-hooks": "^3.4.2",
68
- "all-contributors-cli": "^6.20.0",
73
+ "all-contributors-cli": "^6.26.0",
69
74
  "babel-core": "^7.0.0-bridge.0",
70
75
  "babel-eslint": "^10.0.3",
71
76
  "babel-jest": "^24.8.0",
72
77
  "babel-plugin-macros": "^2.5.0",
73
78
  "babel-plugin-tester": "^6.0.0",
74
- "coveralls": "^3.1.0",
79
+ "coveralls": "^3.1.1",
75
80
  "cp-cli": "^2.0.0",
76
81
  "cross-env": "^7.0.3",
77
- "dtslint": "^4.1.0",
78
- "eslint": "^7.28.0",
79
- "eslint-config-airbnb": "^18.2.1",
80
- "eslint-config-prettier": "6.4.0",
81
- "eslint-plugin-import": "^2.23.4",
82
- "eslint-plugin-jest-dom": "^3.6.3",
83
- "eslint-plugin-jsx-a11y": "^6.4.1",
84
- "eslint-plugin-react": "^7.16.0",
85
- "eslint-plugin-testing-library": "^3.10.1",
82
+ "dtslint": "^4.2.1",
83
+ "eslint": "8.42.0",
84
+ "eslint-config-airbnb": "19.0.4",
85
+ "eslint-config-prettier": "^8.8.0",
86
+ "eslint-plugin-import": "^2.27.5",
87
+ "eslint-plugin-jest-dom": "^5.0.1",
88
+ "eslint-plugin-jsx-a11y": "^6.7.1",
89
+ "eslint-plugin-react": "^7.32.2",
90
+ "eslint-plugin-testing-library": "^5.11.0",
86
91
  "husky": "^3.0.3",
87
- "i18next": "^22.4.3",
92
+ "i18next": "^23.0.1",
88
93
  "jest": "^24.8.0",
89
94
  "jest-cli": "^24.8.4",
90
95
  "lint-staged": "^8.1.3",
91
96
  "mkdirp": "^1.0.4",
92
- "prettier": "2.2.1",
97
+ "prettier": "2.8.8",
93
98
  "react": "^16.14.0",
94
99
  "react-dom": "^16.14.0",
95
100
  "react-test-renderer": "^16.14.0",
96
101
  "rimraf": "2.6.3",
97
- "rollup": "1.19.4",
98
- "rollup-plugin-babel": "^4.3.2",
99
- "rollup-plugin-commonjs": "^10.0.2",
100
- "rollup-plugin-node-resolve": "^5.2.0",
101
- "rollup-plugin-replace": "^2.1.0",
102
- "rollup-plugin-terser": "^5.1.1",
102
+ "rollup": "3.25.1",
103
103
  "sinon": "^7.2.3",
104
104
  "tslint": "^6.1.3",
105
105
  "typescript": "4.6.2",
106
- "yargs": "^13.3.0"
106
+ "yargs": "^17.7.2"
107
107
  },
108
108
  "peerDependencies": {
109
- "i18next": ">= 19.0.0",
109
+ "i18next": ">= 23.0.1",
110
110
  "react": ">= 16.8.0"
111
111
  },
112
112
  "peerDependenciesMeta": {
@@ -122,9 +122,9 @@
122
122
  "copy": "cp-cli ./dist/umd/react-i18next.min.js ./react-i18next.min.js && cp-cli ./dist/umd/react-i18next.js ./react-i18next.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
123
123
  "build:es": "cross-env BABEL_ENV=jsnext babel src --out-dir dist/es",
124
124
  "build:cjs": "babel src --out-dir dist/commonjs",
125
- "build:umd": "rollup -c rollup.config.js --format umd && rollup -c rollup.config.js --format umd --uglify",
126
- "build:amd": "rollup -c rollup.config.js --format amd && rollup -c rollup.config.js --format amd --uglify",
127
- "build:iife": "rollup -c rollup.config.js --format iife && rollup -c rollup.config.js --format iife --uglify",
125
+ "build:umd": "rollup -c rollup.config.mjs --format umd && rollup -c rollup.config.mjs --format umd --uglify",
126
+ "build:amd": "rollup -c rollup.config.mjs --format amd && rollup -c rollup.config.mjs --format amd --uglify",
127
+ "build:iife": "rollup -c rollup.config.mjs --format iife && rollup -c rollup.config.mjs --format iife --uglify",
128
128
  "build": "npm run clean && npm run build:cjs && npm run build:es && npm run build:umd && npm run build:amd && npm run copy",
129
129
  "fix_dist_package": "node -e 'console.log(`{\"type\":\"module\",\"version\":\"${process.env.npm_package_version}\"}`)' > dist/es/package.json",
130
130
  "preversion": "npm run build && git push",