react-i18next 10.8.1 → 10.11.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.
- package/CHANGELOG.md +16 -0
- package/dist/amd/react-i18next.js +30 -22
- package/dist/amd/react-i18next.min.js +1 -1
- package/dist/commonjs/I18nextProvider.js +3 -1
- package/dist/commonjs/Trans.js +7 -3
- package/dist/commonjs/context.js +1 -1
- package/dist/commonjs/useTranslation.js +17 -17
- package/dist/commonjs/withSSR.js +1 -0
- package/dist/commonjs/withTranslation.js +1 -0
- package/dist/es/I18nextProvider.js +3 -1
- package/dist/es/Trans.js +7 -3
- package/dist/es/context.js +1 -1
- package/dist/es/useTranslation.js +17 -17
- package/dist/es/withSSR.js +1 -0
- package/dist/es/withTranslation.js +1 -0
- package/dist/umd/react-i18next.js +30 -22
- package/dist/umd/react-i18next.min.js +1 -1
- package/package.json +3 -2
- package/react-i18next.js +30 -22
- package/react-i18next.min.js +1 -1
- package/src/I18nextProvider.js +2 -1
- package/src/Trans.js +8 -2
- package/src/context.js +1 -1
- package/src/useTranslation.js +15 -14
- package/src/withSSR.js +1 -0
- package/src/withTranslation.js +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
### 10.11.0
|
|
2
|
+
|
|
3
|
+
- Restore support passing the defaultNS via I18nextProvider prop [860](https://github.com/i18next/react-i18next/pull/860)
|
|
4
|
+
|
|
5
|
+
### 10.10.0
|
|
6
|
+
|
|
7
|
+
- HOC: expose wrapped component as WrappedComponent property [853](https://github.com/i18next/react-i18next/pull/853)
|
|
8
|
+
|
|
9
|
+
### 10.9.1
|
|
10
|
+
|
|
11
|
+
- Fix useEffect mount/unmount usage [852](https://github.com/i18next/react-i18next/pull/852)
|
|
12
|
+
|
|
13
|
+
### 10.9.0
|
|
14
|
+
|
|
15
|
+
- trigger suspense on languageChanging by add listening to that event too (new in i18next@15.1.0) - if you do not like this behaviour of suspending during languageChange - remove it from bindI18n
|
|
16
|
+
|
|
1
17
|
### 10.8.1
|
|
2
18
|
|
|
3
19
|
- expose context [829](https://github.com/i18next/react-i18next/pull/829)
|
|
@@ -327,7 +327,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
327
327
|
};
|
|
328
328
|
|
|
329
329
|
let defaultOptions = {
|
|
330
|
-
bindI18n: 'languageChanged',
|
|
330
|
+
bindI18n: 'languageChanging languageChanged',
|
|
331
331
|
bindI18nStore: '',
|
|
332
332
|
// nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
|
|
333
333
|
transEmptyNodeValue: '',
|
|
@@ -678,7 +678,8 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
678
678
|
additionalProps = _objectWithoutProperties(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
|
|
679
679
|
|
|
680
680
|
const _ref2 = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {},
|
|
681
|
-
i18nFromContext = _ref2.i18n
|
|
681
|
+
i18nFromContext = _ref2.i18n,
|
|
682
|
+
defaultNSFromContext = _ref2.defaultNS;
|
|
682
683
|
|
|
683
684
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
684
685
|
|
|
@@ -691,7 +692,10 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
691
692
|
|
|
692
693
|
const reactI18nextOptions = _objectSpread({}, getDefaults(), i18n.options && i18n.options.react);
|
|
693
694
|
|
|
694
|
-
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
695
|
+
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
|
|
696
|
+
|
|
697
|
+
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
698
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
695
699
|
const defaultValue = defaults || nodesToString('', children, 0, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue;
|
|
696
700
|
const hashTransKey = reactI18nextOptions.hashTransKey;
|
|
697
701
|
const key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
@@ -705,7 +709,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
705
709
|
const combinedTOpts = _objectSpread({}, tOptions, values, interpolationOverride, {
|
|
706
710
|
defaultValue,
|
|
707
711
|
count,
|
|
708
|
-
ns
|
|
712
|
+
ns: namespaces
|
|
709
713
|
});
|
|
710
714
|
|
|
711
715
|
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
@@ -719,7 +723,8 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
719
723
|
const i18nFromProps = props.i18n;
|
|
720
724
|
|
|
721
725
|
const _ref = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {},
|
|
722
|
-
i18nFromContext = _ref.i18n
|
|
726
|
+
i18nFromContext = _ref.i18n,
|
|
727
|
+
defaultNSFromContext = _ref.defaultNS;
|
|
723
728
|
|
|
724
729
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
725
730
|
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
|
|
@@ -740,27 +745,25 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
740
745
|
const _props$useSuspense = props.useSuspense,
|
|
741
746
|
useSuspense = _props$useSuspense === void 0 ? i18nOptions.useSuspense : _props$useSuspense; // prepare having a namespace
|
|
742
747
|
|
|
743
|
-
let namespaces = ns || i18n.options && i18n.options.defaultNS;
|
|
748
|
+
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
744
749
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
|
|
745
750
|
|
|
746
|
-
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty
|
|
751
|
+
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
|
|
752
|
+
|
|
753
|
+
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n)); // binding t function to namespace (acts also as rerender trigger)
|
|
747
754
|
|
|
748
|
-
|
|
755
|
+
function getT() {
|
|
756
|
+
return {
|
|
757
|
+
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
758
|
+
};
|
|
759
|
+
}
|
|
749
760
|
|
|
750
|
-
const _useState = React.useState(
|
|
751
|
-
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
752
|
-
}),
|
|
761
|
+
const _useState = React.useState(getT()),
|
|
753
762
|
_useState2 = _slicedToArray(_useState, 2),
|
|
754
763
|
t = _useState2[0],
|
|
755
764
|
setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
|
|
756
765
|
|
|
757
766
|
|
|
758
|
-
function resetT() {
|
|
759
|
-
setT({
|
|
760
|
-
t: i18n.getFixedT(null, namespaces[0])
|
|
761
|
-
});
|
|
762
|
-
}
|
|
763
|
-
|
|
764
767
|
React.useEffect(() => {
|
|
765
768
|
let isMounted = true;
|
|
766
769
|
const bindI18n = i18nOptions.bindI18n,
|
|
@@ -769,12 +772,12 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
769
772
|
|
|
770
773
|
if (!ready && !useSuspense) {
|
|
771
774
|
loadNamespaces(i18n, namespaces, () => {
|
|
772
|
-
if (isMounted)
|
|
775
|
+
if (isMounted) setT(getT());
|
|
773
776
|
});
|
|
774
777
|
}
|
|
775
778
|
|
|
776
779
|
function boundReset() {
|
|
777
|
-
if (isMounted)
|
|
780
|
+
if (isMounted) setT(getT());
|
|
778
781
|
} // bind events to trigger change, like languageChanged
|
|
779
782
|
|
|
780
783
|
|
|
@@ -786,7 +789,8 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
786
789
|
if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
|
|
787
790
|
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
|
788
791
|
};
|
|
789
|
-
});
|
|
792
|
+
}, []); // define props to trigger using effect on rerender (none here)
|
|
793
|
+
|
|
790
794
|
const ret = [t.t, i18n, ready];
|
|
791
795
|
ret.t = t.t;
|
|
792
796
|
ret.i18n = i18n;
|
|
@@ -798,7 +802,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
798
802
|
|
|
799
803
|
throw new Promise(resolve => {
|
|
800
804
|
loadNamespaces(i18n, namespaces, () => {
|
|
801
|
-
|
|
805
|
+
setT(getT());
|
|
802
806
|
resolve();
|
|
803
807
|
});
|
|
804
808
|
});
|
|
@@ -828,6 +832,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
828
832
|
}
|
|
829
833
|
|
|
830
834
|
I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
|
|
835
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
831
836
|
return options.withRef ? React__default.forwardRef(I18nextWithTranslation) : I18nextWithTranslation;
|
|
832
837
|
};
|
|
833
838
|
}
|
|
@@ -851,11 +856,13 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
851
856
|
|
|
852
857
|
function I18nextProvider(_ref) {
|
|
853
858
|
let i18n = _ref.i18n,
|
|
859
|
+
defaultNS = _ref.defaultNS,
|
|
854
860
|
children = _ref.children;
|
|
855
861
|
usedI18nextProvider(true);
|
|
856
862
|
return React__default.createElement(I18nContext.Provider, {
|
|
857
863
|
value: {
|
|
858
|
-
i18n
|
|
864
|
+
i18n,
|
|
865
|
+
defaultNS
|
|
859
866
|
}
|
|
860
867
|
}, children);
|
|
861
868
|
}
|
|
@@ -893,6 +900,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
893
900
|
|
|
894
901
|
I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
|
|
895
902
|
I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
|
|
903
|
+
I18nextWithSSR.WrappedComponent = WrappedComponent;
|
|
896
904
|
return I18nextWithSSR;
|
|
897
905
|
};
|
|
898
906
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
define(["exports","react"],function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),i.forEach(function(t){r(e,t,n[t])})}return e}function o(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+"</"+t.name+">"}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+n.name+">")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class j{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function w(e){g=e}function E(){return g}const S={type:"3rdParty",init(e){v(e.options.react),w(e)}};function $(e){return t=>new Promise(n=>{const r=x();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function x(){const e=E(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function N(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t)}}const I={};function k(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&I[t[0]]||("string"==typeof t[0]&&(I[t[0]]=new Date),N(...t))}function P(e,t,n){e.loadNamespaces(t,()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function T(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function z(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(T(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=T(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}</0>`));return T(u[0])}function C(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=(b()?t.useContext(y):{}).i18n,s=r||o||E();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const c=i({},O(),s.options.react),l=n.useSuspense,u=void 0===l?c.useSuspense:l;let p=e||s.options&&s.options.defaultNS;p="string"==typeof p?[p]:p||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(p);const f=(s.isInitialized||s.initializedStoreOnce)&&p.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,s)),d=a(t.useState({t:s.getFixedT(null,"fallback"===c.nsMode?p:p[0])}),2),g=d[0],h=d[1];function m(){h({t:s.getFixedT(null,p[0])})}t.useEffect(()=>{let e=!0;const t=c.bindI18n,n=c.bindI18nStore;function r(){e&&m()}return f||u||P(s,p,()=>{e&&m()}),t&&s&&s.on(t,r),n&&s&&s.store.on(n,r),()=>{e=!1,t&&s&&t.split(" ").forEach(e=>s.off(e,r)),n&&s&&n.split(" ").forEach(e=>s.store.off(e,r))}});const v=[g.t,s,f];if(v.t=g.t,v.i18n=s,v.ready=f,f)return v;if(!f&&!u)return v;throw new Promise(e=>{P(s,p,()=>{m(),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||E();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=(b()?t.useContext(y):{}).i18n,j=g||v||E();if(!j)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const w=h||j.t.bind(j),S=i({},O(),j.options&&j.options.react),$=void 0!==s?s:S.defaultTransParent,x=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}></${n}>`:`${t}<${n}>${e("",T(r),o+1,a)}</${n}>`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}></${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:N("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else N("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),t}("",r,0,S)||S.transEmptyNodeValue,I=S.hashTransKey,P=c||(I?I(x):x),R=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:x,count:a,ns:d}),C=P?w(P,R):x;return $?n.createElement($,m,z(f||r,C,j,S,R)):z(f||r,C,j,S,R)},e.useTranslation=C,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(C(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(C(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t}},r)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=$(e),t.displayName=`withI18nextSSR(${R(e)})`,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=S,e.setDefaults=v,e.getDefaults=O,e.setI18n=w,e.getI18n=E,e.composeInitialProps=$,e.getInitialProps=x,Object.defineProperty(e,"__esModule",{value:!0})});
|
|
1
|
+
define(["exports","react"],function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),i.forEach(function(t){r(e,t,n[t])})}return e}function o(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+"</"+t.name+">"}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+n.name+">")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class S{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function j(e){g=e}function w(){return g}const N={type:"3rdParty",init(e){v(e.options.react),j(e)}};function E(e){return t=>new Promise(n=>{const r=$();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function $(){const e=w(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function x(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t)}}const I={};function k(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&I[t[0]]||("string"==typeof t[0]&&(I[t[0]]=new Date),x(...t))}function P(e,t,n){e.loadNamespaces(t,()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function C(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function T(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(C(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=C(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}</0>`));return C(u[0])}function z(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=b()?t.useContext(y):{},s=o.i18n,c=o.defaultNS,l=r||s||w();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new S),!l){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const u=i({},O(),l.options.react),p=n.useSuspense,f=void 0===p?u.useSuspense:p;let d=e||c||l.options&&l.options.defaultNS;d="string"==typeof d?[d]:d||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(d);const g=(l.isInitialized||l.initializedStoreOnce)&&d.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,l));function h(){return{t:l.getFixedT(null,"fallback"===u.nsMode?d:d[0])}}const m=a(t.useState(h()),2),v=m[0],j=m[1];t.useEffect(()=>{let e=!0;const t=u.bindI18n,n=u.bindI18nStore;function r(){e&&j(h())}return g||f||P(l,d,()=>{e&&j(h())}),t&&l&&l.on(t,r),n&&l&&l.store.on(n,r),()=>{e=!1,t&&l&&t.split(" ").forEach(e=>l.off(e,r)),n&&l&&n.split(" ").forEach(e=>l.store.off(e,r))}},[]);const N=[v.t,l,g];if(N.t=v.t,N.i18n=l,N.ready=g,g)return N;if(!g&&!f)return N;throw new Promise(e=>{P(l,d,()=>{j(h()),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||w();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=b()?t.useContext(y):{},S=v.i18n,j=v.defaultNS,N=g||S||w();if(!N)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const E=h||N.t.bind(N),$=i({},O(),N.options&&N.options.react),I=void 0!==s?s:$.defaultTransParent;let P=d||j||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];const R=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}></${n}>`:`${t}<${n}>${e("",C(r),o+1,a)}</${n}>`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}></${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:x("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else x("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),t}("",r,0,$)||$.transEmptyNodeValue,z=$.hashTransKey,L=c||(z?z(R):R),B=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:R,count:a,ns:P}),V=L?E(L,B):R;return I?n.createElement(I,m,T(f||r,V,N,$,B)):T(f||r,V,N,$,B)},e.useTranslation=z,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(z(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,o.WrappedComponent=r,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(z(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.defaultNS,i=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t,defaultNS:r}},i)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=E(e),t.displayName=`withI18nextSSR(${R(e)})`,t.WrappedComponent=e,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=N,e.setDefaults=v,e.getDefaults=O,e.setI18n=j,e.getI18n=w,e.composeInitialProps=E,e.getInitialProps=$,Object.defineProperty(e,"__esModule",{value:!0})});
|
|
@@ -13,11 +13,13 @@ var _context = require("./context");
|
|
|
13
13
|
|
|
14
14
|
function I18nextProvider(_ref) {
|
|
15
15
|
var i18n = _ref.i18n,
|
|
16
|
+
defaultNS = _ref.defaultNS,
|
|
16
17
|
children = _ref.children;
|
|
17
18
|
(0, _context.usedI18nextProvider)(true);
|
|
18
19
|
return _react.default.createElement(_context.I18nContext.Provider, {
|
|
19
20
|
value: {
|
|
20
|
-
i18n: i18n
|
|
21
|
+
i18n: i18n,
|
|
22
|
+
defaultNS: defaultNS
|
|
21
23
|
}
|
|
22
24
|
}, children);
|
|
23
25
|
}
|
package/dist/commonjs/Trans.js
CHANGED
|
@@ -209,7 +209,8 @@ function Trans(_ref) {
|
|
|
209
209
|
additionalProps = (0, _objectWithoutProperties2.default)(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
|
|
210
210
|
|
|
211
211
|
var _ref2 = (0, _context.getHasUsedI18nextProvider)() ? (0, _react.useContext)(_context.I18nContext) : {},
|
|
212
|
-
i18nFromContext = _ref2.i18n
|
|
212
|
+
i18nFromContext = _ref2.i18n,
|
|
213
|
+
defaultNSFromContext = _ref2.defaultNS;
|
|
213
214
|
|
|
214
215
|
var i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
|
|
215
216
|
|
|
@@ -220,7 +221,10 @@ function Trans(_ref) {
|
|
|
220
221
|
|
|
221
222
|
var t = tFromProps || i18n.t.bind(i18n);
|
|
222
223
|
var reactI18nextOptions = (0, _objectSpread2.default)({}, (0, _context.getDefaults)(), i18n.options && i18n.options.react);
|
|
223
|
-
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
224
|
+
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
|
|
225
|
+
|
|
226
|
+
var namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
227
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
224
228
|
var defaultValue = defaults || nodesToString('', children, 0, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue;
|
|
225
229
|
var hashTransKey = reactI18nextOptions.hashTransKey;
|
|
226
230
|
var key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
@@ -233,7 +237,7 @@ function Trans(_ref) {
|
|
|
233
237
|
var combinedTOpts = (0, _objectSpread2.default)({}, tOptions, values, interpolationOverride, {
|
|
234
238
|
defaultValue: defaultValue,
|
|
235
239
|
count: count,
|
|
236
|
-
ns:
|
|
240
|
+
ns: namespaces
|
|
237
241
|
});
|
|
238
242
|
var translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
239
243
|
if (!useAsParent) return renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts);
|
package/dist/commonjs/context.js
CHANGED
|
@@ -24,7 +24,7 @@ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/obje
|
|
|
24
24
|
var _react = _interopRequireDefault(require("react"));
|
|
25
25
|
|
|
26
26
|
var defaultOptions = {
|
|
27
|
-
bindI18n: 'languageChanged',
|
|
27
|
+
bindI18n: 'languageChanging languageChanged',
|
|
28
28
|
bindI18nStore: '',
|
|
29
29
|
// nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
|
|
30
30
|
transEmptyNodeValue: '',
|
|
@@ -23,7 +23,8 @@ function useTranslation(ns) {
|
|
|
23
23
|
var i18nFromProps = props.i18n;
|
|
24
24
|
|
|
25
25
|
var _ref = (0, _context.getHasUsedI18nextProvider)() ? (0, _react.useContext)(_context.I18nContext) : {},
|
|
26
|
-
i18nFromContext = _ref.i18n
|
|
26
|
+
i18nFromContext = _ref.i18n,
|
|
27
|
+
defaultNSFromContext = _ref.defaultNS;
|
|
27
28
|
|
|
28
29
|
var i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
|
|
29
30
|
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new _context.ReportNamespaces();
|
|
@@ -47,29 +48,27 @@ function useTranslation(ns) {
|
|
|
47
48
|
var _props$useSuspense = props.useSuspense,
|
|
48
49
|
useSuspense = _props$useSuspense === void 0 ? i18nOptions.useSuspense : _props$useSuspense; // prepare having a namespace
|
|
49
50
|
|
|
50
|
-
var namespaces = ns || i18n.options && i18n.options.defaultNS;
|
|
51
|
+
var namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
51
52
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
|
|
52
53
|
|
|
53
|
-
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty
|
|
54
|
+
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
|
|
54
55
|
|
|
55
56
|
var ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(function (n) {
|
|
56
57
|
return (0, _utils.hasLoadedNamespace)(n, i18n);
|
|
57
|
-
}); //
|
|
58
|
+
}); // binding t function to namespace (acts also as rerender trigger)
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
function getT() {
|
|
61
|
+
return {
|
|
62
|
+
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
var _useState = (0, _react.useState)(getT()),
|
|
62
67
|
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
63
68
|
t = _useState2[0],
|
|
64
69
|
setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
|
|
65
70
|
|
|
66
71
|
|
|
67
|
-
function resetT() {
|
|
68
|
-
setT({
|
|
69
|
-
t: i18n.getFixedT(null, namespaces[0])
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
72
|
(0, _react.useEffect)(function () {
|
|
74
73
|
var isMounted = true;
|
|
75
74
|
var bindI18n = i18nOptions.bindI18n,
|
|
@@ -78,12 +77,12 @@ function useTranslation(ns) {
|
|
|
78
77
|
|
|
79
78
|
if (!ready && !useSuspense) {
|
|
80
79
|
(0, _utils.loadNamespaces)(i18n, namespaces, function () {
|
|
81
|
-
if (isMounted)
|
|
80
|
+
if (isMounted) setT(getT());
|
|
82
81
|
});
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
function boundReset() {
|
|
86
|
-
if (isMounted)
|
|
85
|
+
if (isMounted) setT(getT());
|
|
87
86
|
} // bind events to trigger change, like languageChanged
|
|
88
87
|
|
|
89
88
|
|
|
@@ -99,7 +98,8 @@ function useTranslation(ns) {
|
|
|
99
98
|
return i18n.store.off(e, boundReset);
|
|
100
99
|
});
|
|
101
100
|
};
|
|
102
|
-
});
|
|
101
|
+
}, []); // define props to trigger using effect on rerender (none here)
|
|
102
|
+
|
|
103
103
|
var ret = [t.t, i18n, ready];
|
|
104
104
|
ret.t = t.t;
|
|
105
105
|
ret.i18n = i18n;
|
|
@@ -111,7 +111,7 @@ function useTranslation(ns) {
|
|
|
111
111
|
|
|
112
112
|
throw new Promise(function (resolve) {
|
|
113
113
|
(0, _utils.loadNamespaces)(i18n, namespaces, function () {
|
|
114
|
-
|
|
114
|
+
setT(getT());
|
|
115
115
|
resolve();
|
|
116
116
|
});
|
|
117
117
|
});
|
package/dist/commonjs/withSSR.js
CHANGED
|
@@ -31,6 +31,7 @@ function withSSR() {
|
|
|
31
31
|
|
|
32
32
|
I18nextWithSSR.getInitialProps = (0, _context.composeInitialProps)(WrappedComponent);
|
|
33
33
|
I18nextWithSSR.displayName = "withI18nextSSR(".concat((0, _utils.getDisplayName)(WrappedComponent), ")");
|
|
34
|
+
I18nextWithSSR.WrappedComponent = WrappedComponent;
|
|
34
35
|
return I18nextWithSSR;
|
|
35
36
|
};
|
|
36
37
|
}
|
|
@@ -41,6 +41,7 @@ function withTranslation(ns) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
I18nextWithTranslation.displayName = "withI18nextTranslation(".concat((0, _utils.getDisplayName)(WrappedComponent), ")");
|
|
44
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
44
45
|
return options.withRef ? _react.default.forwardRef(I18nextWithTranslation) : I18nextWithTranslation;
|
|
45
46
|
};
|
|
46
47
|
}
|
|
@@ -2,11 +2,13 @@ import React from 'react';
|
|
|
2
2
|
import { I18nContext, usedI18nextProvider } from './context';
|
|
3
3
|
export function I18nextProvider(_ref) {
|
|
4
4
|
var i18n = _ref.i18n,
|
|
5
|
+
defaultNS = _ref.defaultNS,
|
|
5
6
|
children = _ref.children;
|
|
6
7
|
usedI18nextProvider(true);
|
|
7
8
|
return React.createElement(I18nContext.Provider, {
|
|
8
9
|
value: {
|
|
9
|
-
i18n: i18n
|
|
10
|
+
i18n: i18n,
|
|
11
|
+
defaultNS: defaultNS
|
|
10
12
|
}
|
|
11
13
|
}, children);
|
|
12
14
|
}
|
package/dist/es/Trans.js
CHANGED
|
@@ -191,7 +191,8 @@ export function Trans(_ref) {
|
|
|
191
191
|
additionalProps = _objectWithoutProperties(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
|
|
192
192
|
|
|
193
193
|
var _ref2 = getHasUsedI18nextProvider() ? useContext(I18nContext) : {},
|
|
194
|
-
i18nFromContext = _ref2.i18n
|
|
194
|
+
i18nFromContext = _ref2.i18n,
|
|
195
|
+
defaultNSFromContext = _ref2.defaultNS;
|
|
195
196
|
|
|
196
197
|
var i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
197
198
|
|
|
@@ -204,7 +205,10 @@ export function Trans(_ref) {
|
|
|
204
205
|
|
|
205
206
|
var reactI18nextOptions = _objectSpread({}, getDefaults(), i18n.options && i18n.options.react);
|
|
206
207
|
|
|
207
|
-
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
208
|
+
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
|
|
209
|
+
|
|
210
|
+
var namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
211
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
208
212
|
var defaultValue = defaults || nodesToString('', children, 0, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue;
|
|
209
213
|
var hashTransKey = reactI18nextOptions.hashTransKey;
|
|
210
214
|
var key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
@@ -218,7 +222,7 @@ export function Trans(_ref) {
|
|
|
218
222
|
var combinedTOpts = _objectSpread({}, tOptions, values, interpolationOverride, {
|
|
219
223
|
defaultValue: defaultValue,
|
|
220
224
|
count: count,
|
|
221
|
-
ns:
|
|
225
|
+
ns: namespaces
|
|
222
226
|
});
|
|
223
227
|
|
|
224
228
|
var translation = key ? t(key, combinedTOpts) : defaultValue;
|
package/dist/es/context.js
CHANGED
|
@@ -3,7 +3,7 @@ import _createClass from "@babel/runtime/helpers/createClass";
|
|
|
3
3
|
import _objectSpread from "@babel/runtime/helpers/objectSpread";
|
|
4
4
|
import React from 'react';
|
|
5
5
|
var defaultOptions = {
|
|
6
|
-
bindI18n: 'languageChanged',
|
|
6
|
+
bindI18n: 'languageChanging languageChanged',
|
|
7
7
|
bindI18nStore: '',
|
|
8
8
|
// nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
|
|
9
9
|
transEmptyNodeValue: '',
|
|
@@ -9,7 +9,8 @@ export function useTranslation(ns) {
|
|
|
9
9
|
var i18nFromProps = props.i18n;
|
|
10
10
|
|
|
11
11
|
var _ref = getHasUsedI18nextProvider() ? useContext(I18nContext) : {},
|
|
12
|
-
i18nFromContext = _ref.i18n
|
|
12
|
+
i18nFromContext = _ref.i18n,
|
|
13
|
+
defaultNSFromContext = _ref.defaultNS;
|
|
13
14
|
|
|
14
15
|
var i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
15
16
|
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
|
|
@@ -34,29 +35,27 @@ export function useTranslation(ns) {
|
|
|
34
35
|
var _props$useSuspense = props.useSuspense,
|
|
35
36
|
useSuspense = _props$useSuspense === void 0 ? i18nOptions.useSuspense : _props$useSuspense; // prepare having a namespace
|
|
36
37
|
|
|
37
|
-
var namespaces = ns || i18n.options && i18n.options.defaultNS;
|
|
38
|
+
var namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
38
39
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
|
|
39
40
|
|
|
40
|
-
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty
|
|
41
|
+
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
|
|
41
42
|
|
|
42
43
|
var ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(function (n) {
|
|
43
44
|
return hasLoadedNamespace(n, i18n);
|
|
44
|
-
}); //
|
|
45
|
+
}); // binding t function to namespace (acts also as rerender trigger)
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
function getT() {
|
|
48
|
+
return {
|
|
49
|
+
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var _useState = useState(getT()),
|
|
49
54
|
_useState2 = _slicedToArray(_useState, 2),
|
|
50
55
|
t = _useState2[0],
|
|
51
56
|
setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
|
|
52
57
|
|
|
53
58
|
|
|
54
|
-
function resetT() {
|
|
55
|
-
setT({
|
|
56
|
-
t: i18n.getFixedT(null, namespaces[0])
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
59
|
useEffect(function () {
|
|
61
60
|
var isMounted = true;
|
|
62
61
|
var bindI18n = i18nOptions.bindI18n,
|
|
@@ -65,12 +64,12 @@ export function useTranslation(ns) {
|
|
|
65
64
|
|
|
66
65
|
if (!ready && !useSuspense) {
|
|
67
66
|
loadNamespaces(i18n, namespaces, function () {
|
|
68
|
-
if (isMounted)
|
|
67
|
+
if (isMounted) setT(getT());
|
|
69
68
|
});
|
|
70
69
|
}
|
|
71
70
|
|
|
72
71
|
function boundReset() {
|
|
73
|
-
if (isMounted)
|
|
72
|
+
if (isMounted) setT(getT());
|
|
74
73
|
} // bind events to trigger change, like languageChanged
|
|
75
74
|
|
|
76
75
|
|
|
@@ -86,7 +85,8 @@ export function useTranslation(ns) {
|
|
|
86
85
|
return i18n.store.off(e, boundReset);
|
|
87
86
|
});
|
|
88
87
|
};
|
|
89
|
-
});
|
|
88
|
+
}, []); // define props to trigger using effect on rerender (none here)
|
|
89
|
+
|
|
90
90
|
var ret = [t.t, i18n, ready];
|
|
91
91
|
ret.t = t.t;
|
|
92
92
|
ret.i18n = i18n;
|
|
@@ -98,7 +98,7 @@ export function useTranslation(ns) {
|
|
|
98
98
|
|
|
99
99
|
throw new Promise(function (resolve) {
|
|
100
100
|
loadNamespaces(i18n, namespaces, function () {
|
|
101
|
-
|
|
101
|
+
setT(getT());
|
|
102
102
|
resolve();
|
|
103
103
|
});
|
|
104
104
|
});
|
package/dist/es/withSSR.js
CHANGED
|
@@ -17,6 +17,7 @@ export function withSSR() {
|
|
|
17
17
|
|
|
18
18
|
I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
|
|
19
19
|
I18nextWithSSR.displayName = "withI18nextSSR(".concat(getDisplayName(WrappedComponent), ")");
|
|
20
|
+
I18nextWithSSR.WrappedComponent = WrappedComponent;
|
|
20
21
|
return I18nextWithSSR;
|
|
21
22
|
};
|
|
22
23
|
}
|
|
@@ -27,6 +27,7 @@ export function withTranslation(ns) {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
I18nextWithTranslation.displayName = "withI18nextTranslation(".concat(getDisplayName(WrappedComponent), ")");
|
|
30
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
30
31
|
return options.withRef ? React.forwardRef(I18nextWithTranslation) : I18nextWithTranslation;
|
|
31
32
|
};
|
|
32
33
|
}
|
|
@@ -331,7 +331,7 @@
|
|
|
331
331
|
};
|
|
332
332
|
|
|
333
333
|
let defaultOptions = {
|
|
334
|
-
bindI18n: 'languageChanged',
|
|
334
|
+
bindI18n: 'languageChanging languageChanged',
|
|
335
335
|
bindI18nStore: '',
|
|
336
336
|
// nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
|
|
337
337
|
transEmptyNodeValue: '',
|
|
@@ -682,7 +682,8 @@
|
|
|
682
682
|
additionalProps = _objectWithoutProperties(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
|
|
683
683
|
|
|
684
684
|
const _ref2 = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {},
|
|
685
|
-
i18nFromContext = _ref2.i18n
|
|
685
|
+
i18nFromContext = _ref2.i18n,
|
|
686
|
+
defaultNSFromContext = _ref2.defaultNS;
|
|
686
687
|
|
|
687
688
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
688
689
|
|
|
@@ -695,7 +696,10 @@
|
|
|
695
696
|
|
|
696
697
|
const reactI18nextOptions = _objectSpread({}, getDefaults(), i18n.options && i18n.options.react);
|
|
697
698
|
|
|
698
|
-
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
699
|
+
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
|
|
700
|
+
|
|
701
|
+
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
702
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
699
703
|
const defaultValue = defaults || nodesToString('', children, 0, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue;
|
|
700
704
|
const hashTransKey = reactI18nextOptions.hashTransKey;
|
|
701
705
|
const key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
@@ -709,7 +713,7 @@
|
|
|
709
713
|
const combinedTOpts = _objectSpread({}, tOptions, values, interpolationOverride, {
|
|
710
714
|
defaultValue,
|
|
711
715
|
count,
|
|
712
|
-
ns
|
|
716
|
+
ns: namespaces
|
|
713
717
|
});
|
|
714
718
|
|
|
715
719
|
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
@@ -723,7 +727,8 @@
|
|
|
723
727
|
const i18nFromProps = props.i18n;
|
|
724
728
|
|
|
725
729
|
const _ref = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {},
|
|
726
|
-
i18nFromContext = _ref.i18n
|
|
730
|
+
i18nFromContext = _ref.i18n,
|
|
731
|
+
defaultNSFromContext = _ref.defaultNS;
|
|
727
732
|
|
|
728
733
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
729
734
|
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
|
|
@@ -744,27 +749,25 @@
|
|
|
744
749
|
const _props$useSuspense = props.useSuspense,
|
|
745
750
|
useSuspense = _props$useSuspense === void 0 ? i18nOptions.useSuspense : _props$useSuspense; // prepare having a namespace
|
|
746
751
|
|
|
747
|
-
let namespaces = ns || i18n.options && i18n.options.defaultNS;
|
|
752
|
+
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
748
753
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
|
|
749
754
|
|
|
750
|
-
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty
|
|
755
|
+
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
|
|
756
|
+
|
|
757
|
+
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n)); // binding t function to namespace (acts also as rerender trigger)
|
|
751
758
|
|
|
752
|
-
|
|
759
|
+
function getT() {
|
|
760
|
+
return {
|
|
761
|
+
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
762
|
+
};
|
|
763
|
+
}
|
|
753
764
|
|
|
754
|
-
const _useState = React.useState(
|
|
755
|
-
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
756
|
-
}),
|
|
765
|
+
const _useState = React.useState(getT()),
|
|
757
766
|
_useState2 = _slicedToArray(_useState, 2),
|
|
758
767
|
t = _useState2[0],
|
|
759
768
|
setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
|
|
760
769
|
|
|
761
770
|
|
|
762
|
-
function resetT() {
|
|
763
|
-
setT({
|
|
764
|
-
t: i18n.getFixedT(null, namespaces[0])
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
|
|
768
771
|
React.useEffect(() => {
|
|
769
772
|
let isMounted = true;
|
|
770
773
|
const bindI18n = i18nOptions.bindI18n,
|
|
@@ -773,12 +776,12 @@
|
|
|
773
776
|
|
|
774
777
|
if (!ready && !useSuspense) {
|
|
775
778
|
loadNamespaces(i18n, namespaces, () => {
|
|
776
|
-
if (isMounted)
|
|
779
|
+
if (isMounted) setT(getT());
|
|
777
780
|
});
|
|
778
781
|
}
|
|
779
782
|
|
|
780
783
|
function boundReset() {
|
|
781
|
-
if (isMounted)
|
|
784
|
+
if (isMounted) setT(getT());
|
|
782
785
|
} // bind events to trigger change, like languageChanged
|
|
783
786
|
|
|
784
787
|
|
|
@@ -790,7 +793,8 @@
|
|
|
790
793
|
if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
|
|
791
794
|
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
|
792
795
|
};
|
|
793
|
-
});
|
|
796
|
+
}, []); // define props to trigger using effect on rerender (none here)
|
|
797
|
+
|
|
794
798
|
const ret = [t.t, i18n, ready];
|
|
795
799
|
ret.t = t.t;
|
|
796
800
|
ret.i18n = i18n;
|
|
@@ -802,7 +806,7 @@
|
|
|
802
806
|
|
|
803
807
|
throw new Promise(resolve => {
|
|
804
808
|
loadNamespaces(i18n, namespaces, () => {
|
|
805
|
-
|
|
809
|
+
setT(getT());
|
|
806
810
|
resolve();
|
|
807
811
|
});
|
|
808
812
|
});
|
|
@@ -832,6 +836,7 @@
|
|
|
832
836
|
}
|
|
833
837
|
|
|
834
838
|
I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
|
|
839
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
835
840
|
return options.withRef ? React__default.forwardRef(I18nextWithTranslation) : I18nextWithTranslation;
|
|
836
841
|
};
|
|
837
842
|
}
|
|
@@ -855,11 +860,13 @@
|
|
|
855
860
|
|
|
856
861
|
function I18nextProvider(_ref) {
|
|
857
862
|
let i18n = _ref.i18n,
|
|
863
|
+
defaultNS = _ref.defaultNS,
|
|
858
864
|
children = _ref.children;
|
|
859
865
|
usedI18nextProvider(true);
|
|
860
866
|
return React__default.createElement(I18nContext.Provider, {
|
|
861
867
|
value: {
|
|
862
|
-
i18n
|
|
868
|
+
i18n,
|
|
869
|
+
defaultNS
|
|
863
870
|
}
|
|
864
871
|
}, children);
|
|
865
872
|
}
|
|
@@ -897,6 +904,7 @@
|
|
|
897
904
|
|
|
898
905
|
I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
|
|
899
906
|
I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
|
|
907
|
+
I18nextWithSSR.WrappedComponent = WrappedComponent;
|
|
900
908
|
return I18nextWithSSR;
|
|
901
909
|
};
|
|
902
910
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactI18next={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),i.forEach(function(t){r(e,t,n[t])})}return e}function o(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+"</"+t.name+">"}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+n.name+">")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class j{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function w(e){g=e}function x(){return g}const E={type:"3rdParty",init(e){v(e.options.react),w(e)}};function S(e){return t=>new Promise(n=>{const r=$();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function $(){const e=x(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function N(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t)}}const I={};function k(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&I[t[0]]||("string"==typeof t[0]&&(I[t[0]]=new Date),N(...t))}function P(e,t,n){e.loadNamespaces(t,()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function T(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function z(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(T(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=T(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}</0>`));return T(u[0])}function C(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=(b()?t.useContext(y):{}).i18n,s=r||o||x();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const c=i({},O(),s.options.react),l=n.useSuspense,u=void 0===l?c.useSuspense:l;let p=e||s.options&&s.options.defaultNS;p="string"==typeof p?[p]:p||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(p);const f=(s.isInitialized||s.initializedStoreOnce)&&p.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,s)),d=a(t.useState({t:s.getFixedT(null,"fallback"===c.nsMode?p:p[0])}),2),g=d[0],h=d[1];function m(){h({t:s.getFixedT(null,p[0])})}t.useEffect(()=>{let e=!0;const t=c.bindI18n,n=c.bindI18nStore;function r(){e&&m()}return f||u||P(s,p,()=>{e&&m()}),t&&s&&s.on(t,r),n&&s&&s.store.on(n,r),()=>{e=!1,t&&s&&t.split(" ").forEach(e=>s.off(e,r)),n&&s&&n.split(" ").forEach(e=>s.store.off(e,r))}});const v=[g.t,s,f];if(v.t=g.t,v.i18n=s,v.ready=f,f)return v;if(!f&&!u)return v;throw new Promise(e=>{P(s,p,()=>{m(),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||x();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=(b()?t.useContext(y):{}).i18n,j=g||v||x();if(!j)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const w=h||j.t.bind(j),E=i({},O(),j.options&&j.options.react),S=void 0!==s?s:E.defaultTransParent,$=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}></${n}>`:`${t}<${n}>${e("",T(r),o+1,a)}</${n}>`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}></${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:N("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else N("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),t}("",r,0,E)||E.transEmptyNodeValue,I=E.hashTransKey,P=c||(I?I($):$),R=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:$,count:a,ns:d}),C=P?w(P,R):$;return S?n.createElement(S,m,z(f||r,C,j,E,R)):z(f||r,C,j,E,R)},e.useTranslation=C,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(C(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(C(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t}},r)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=S(e),t.displayName=`withI18nextSSR(${R(e)})`,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=E,e.setDefaults=v,e.getDefaults=O,e.setI18n=w,e.getI18n=x,e.composeInitialProps=S,e.getInitialProps=$,Object.defineProperty(e,"__esModule",{value:!0})});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactI18next={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),i.forEach(function(t){r(e,t,n[t])})}return e}function o(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+"</"+t.name+">"}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+n.name+">")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class S{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function j(e){g=e}function w(){return g}const N={type:"3rdParty",init(e){v(e.options.react),j(e)}};function x(e){return t=>new Promise(n=>{const r=E();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function E(){const e=w(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function $(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t)}}const I={};function k(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&I[t[0]]||("string"==typeof t[0]&&(I[t[0]]=new Date),$(...t))}function P(e,t,n){e.loadNamespaces(t,()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function C(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function T(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(C(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=C(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}</0>`));return C(u[0])}function z(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=b()?t.useContext(y):{},s=o.i18n,c=o.defaultNS,l=r||s||w();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new S),!l){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const u=i({},O(),l.options.react),p=n.useSuspense,f=void 0===p?u.useSuspense:p;let d=e||c||l.options&&l.options.defaultNS;d="string"==typeof d?[d]:d||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(d);const g=(l.isInitialized||l.initializedStoreOnce)&&d.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,l));function h(){return{t:l.getFixedT(null,"fallback"===u.nsMode?d:d[0])}}const m=a(t.useState(h()),2),v=m[0],j=m[1];t.useEffect(()=>{let e=!0;const t=u.bindI18n,n=u.bindI18nStore;function r(){e&&j(h())}return g||f||P(l,d,()=>{e&&j(h())}),t&&l&&l.on(t,r),n&&l&&l.store.on(n,r),()=>{e=!1,t&&l&&t.split(" ").forEach(e=>l.off(e,r)),n&&l&&n.split(" ").forEach(e=>l.store.off(e,r))}},[]);const N=[v.t,l,g];if(N.t=v.t,N.i18n=l,N.ready=g,g)return N;if(!g&&!f)return N;throw new Promise(e=>{P(l,d,()=>{j(h()),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||w();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=b()?t.useContext(y):{},S=v.i18n,j=v.defaultNS,N=g||S||w();if(!N)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const x=h||N.t.bind(N),E=i({},O(),N.options&&N.options.react),I=void 0!==s?s:E.defaultTransParent;let P=d||j||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];const R=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}></${n}>`:`${t}<${n}>${e("",C(r),o+1,a)}</${n}>`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}></${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:$("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else $("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),t}("",r,0,E)||E.transEmptyNodeValue,z=E.hashTransKey,L=c||(z?z(R):R),B=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:R,count:a,ns:P}),V=L?x(L,B):R;return I?n.createElement(I,m,T(f||r,V,N,E,B)):T(f||r,V,N,E,B)},e.useTranslation=z,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(z(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,o.WrappedComponent=r,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(z(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.defaultNS,i=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t,defaultNS:r}},i)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=x(e),t.displayName=`withI18nextSSR(${R(e)})`,t.WrappedComponent=e,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=N,e.setDefaults=v,e.getDefaults=O,e.setI18n=j,e.getI18n=w,e.composeInitialProps=x,e.getInitialProps=E,Object.defineProperty(e,"__esModule",{value:!0})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-i18next",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.11.0",
|
|
4
4
|
"description": "Internationalization for react done right. Using the i18next i18n ecosystem.",
|
|
5
5
|
"main": "dist/commonjs/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
@@ -96,7 +96,8 @@
|
|
|
96
96
|
"pretest": "npm run test:typescript",
|
|
97
97
|
"test": "BABEL_ENV=development jest --no-cache",
|
|
98
98
|
"test:watch": "BABEL_ENV=development jest --no-cache --watch",
|
|
99
|
-
"test:coverage": "
|
|
99
|
+
"test:coverage": "BABEL_ENV=development jest --no-cache --coverage",
|
|
100
|
+
"test:coverageOldEnzymeAdapter": "enzyme-adapter-react-install 16 && BABEL_ENV=development jest --no-cache --coverage",
|
|
100
101
|
"test:lint": "./node_modules/.bin/eslint ./src",
|
|
101
102
|
"test:typescript": "tslint --project tsconfig.json",
|
|
102
103
|
"contributors:add": "all-contributors add",
|
package/react-i18next.js
CHANGED
|
@@ -331,7 +331,7 @@
|
|
|
331
331
|
};
|
|
332
332
|
|
|
333
333
|
let defaultOptions = {
|
|
334
|
-
bindI18n: 'languageChanged',
|
|
334
|
+
bindI18n: 'languageChanging languageChanged',
|
|
335
335
|
bindI18nStore: '',
|
|
336
336
|
// nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
|
|
337
337
|
transEmptyNodeValue: '',
|
|
@@ -682,7 +682,8 @@
|
|
|
682
682
|
additionalProps = _objectWithoutProperties(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
|
|
683
683
|
|
|
684
684
|
const _ref2 = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {},
|
|
685
|
-
i18nFromContext = _ref2.i18n
|
|
685
|
+
i18nFromContext = _ref2.i18n,
|
|
686
|
+
defaultNSFromContext = _ref2.defaultNS;
|
|
686
687
|
|
|
687
688
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
688
689
|
|
|
@@ -695,7 +696,10 @@
|
|
|
695
696
|
|
|
696
697
|
const reactI18nextOptions = _objectSpread({}, getDefaults(), i18n.options && i18n.options.react);
|
|
697
698
|
|
|
698
|
-
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
699
|
+
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
|
|
700
|
+
|
|
701
|
+
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
702
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
699
703
|
const defaultValue = defaults || nodesToString('', children, 0, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue;
|
|
700
704
|
const hashTransKey = reactI18nextOptions.hashTransKey;
|
|
701
705
|
const key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
@@ -709,7 +713,7 @@
|
|
|
709
713
|
const combinedTOpts = _objectSpread({}, tOptions, values, interpolationOverride, {
|
|
710
714
|
defaultValue,
|
|
711
715
|
count,
|
|
712
|
-
ns
|
|
716
|
+
ns: namespaces
|
|
713
717
|
});
|
|
714
718
|
|
|
715
719
|
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
@@ -723,7 +727,8 @@
|
|
|
723
727
|
const i18nFromProps = props.i18n;
|
|
724
728
|
|
|
725
729
|
const _ref = getHasUsedI18nextProvider() ? React.useContext(I18nContext) : {},
|
|
726
|
-
i18nFromContext = _ref.i18n
|
|
730
|
+
i18nFromContext = _ref.i18n,
|
|
731
|
+
defaultNSFromContext = _ref.defaultNS;
|
|
727
732
|
|
|
728
733
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
729
734
|
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
|
|
@@ -744,27 +749,25 @@
|
|
|
744
749
|
const _props$useSuspense = props.useSuspense,
|
|
745
750
|
useSuspense = _props$useSuspense === void 0 ? i18nOptions.useSuspense : _props$useSuspense; // prepare having a namespace
|
|
746
751
|
|
|
747
|
-
let namespaces = ns || i18n.options && i18n.options.defaultNS;
|
|
752
|
+
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
748
753
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
|
|
749
754
|
|
|
750
|
-
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty
|
|
755
|
+
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
|
|
756
|
+
|
|
757
|
+
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n)); // binding t function to namespace (acts also as rerender trigger)
|
|
751
758
|
|
|
752
|
-
|
|
759
|
+
function getT() {
|
|
760
|
+
return {
|
|
761
|
+
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
762
|
+
};
|
|
763
|
+
}
|
|
753
764
|
|
|
754
|
-
const _useState = React.useState(
|
|
755
|
-
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
|
|
756
|
-
}),
|
|
765
|
+
const _useState = React.useState(getT()),
|
|
757
766
|
_useState2 = _slicedToArray(_useState, 2),
|
|
758
767
|
t = _useState2[0],
|
|
759
768
|
setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
|
|
760
769
|
|
|
761
770
|
|
|
762
|
-
function resetT() {
|
|
763
|
-
setT({
|
|
764
|
-
t: i18n.getFixedT(null, namespaces[0])
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
|
|
768
771
|
React.useEffect(() => {
|
|
769
772
|
let isMounted = true;
|
|
770
773
|
const bindI18n = i18nOptions.bindI18n,
|
|
@@ -773,12 +776,12 @@
|
|
|
773
776
|
|
|
774
777
|
if (!ready && !useSuspense) {
|
|
775
778
|
loadNamespaces(i18n, namespaces, () => {
|
|
776
|
-
if (isMounted)
|
|
779
|
+
if (isMounted) setT(getT());
|
|
777
780
|
});
|
|
778
781
|
}
|
|
779
782
|
|
|
780
783
|
function boundReset() {
|
|
781
|
-
if (isMounted)
|
|
784
|
+
if (isMounted) setT(getT());
|
|
782
785
|
} // bind events to trigger change, like languageChanged
|
|
783
786
|
|
|
784
787
|
|
|
@@ -790,7 +793,8 @@
|
|
|
790
793
|
if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
|
|
791
794
|
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
|
792
795
|
};
|
|
793
|
-
});
|
|
796
|
+
}, []); // define props to trigger using effect on rerender (none here)
|
|
797
|
+
|
|
794
798
|
const ret = [t.t, i18n, ready];
|
|
795
799
|
ret.t = t.t;
|
|
796
800
|
ret.i18n = i18n;
|
|
@@ -802,7 +806,7 @@
|
|
|
802
806
|
|
|
803
807
|
throw new Promise(resolve => {
|
|
804
808
|
loadNamespaces(i18n, namespaces, () => {
|
|
805
|
-
|
|
809
|
+
setT(getT());
|
|
806
810
|
resolve();
|
|
807
811
|
});
|
|
808
812
|
});
|
|
@@ -832,6 +836,7 @@
|
|
|
832
836
|
}
|
|
833
837
|
|
|
834
838
|
I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
|
|
839
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
835
840
|
return options.withRef ? React__default.forwardRef(I18nextWithTranslation) : I18nextWithTranslation;
|
|
836
841
|
};
|
|
837
842
|
}
|
|
@@ -855,11 +860,13 @@
|
|
|
855
860
|
|
|
856
861
|
function I18nextProvider(_ref) {
|
|
857
862
|
let i18n = _ref.i18n,
|
|
863
|
+
defaultNS = _ref.defaultNS,
|
|
858
864
|
children = _ref.children;
|
|
859
865
|
usedI18nextProvider(true);
|
|
860
866
|
return React__default.createElement(I18nContext.Provider, {
|
|
861
867
|
value: {
|
|
862
|
-
i18n
|
|
868
|
+
i18n,
|
|
869
|
+
defaultNS
|
|
863
870
|
}
|
|
864
871
|
}, children);
|
|
865
872
|
}
|
|
@@ -897,6 +904,7 @@
|
|
|
897
904
|
|
|
898
905
|
I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
|
|
899
906
|
I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
|
|
907
|
+
I18nextWithSSR.WrappedComponent = WrappedComponent;
|
|
900
908
|
return I18nextWithSSR;
|
|
901
909
|
};
|
|
902
910
|
}
|
package/react-i18next.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactI18next={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),i.forEach(function(t){r(e,t,n[t])})}return e}function o(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+"</"+t.name+">"}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+n.name+">")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class j{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function w(e){g=e}function x(){return g}const E={type:"3rdParty",init(e){v(e.options.react),w(e)}};function S(e){return t=>new Promise(n=>{const r=$();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function $(){const e=x(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function N(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t)}}const I={};function k(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&I[t[0]]||("string"==typeof t[0]&&(I[t[0]]=new Date),N(...t))}function P(e,t,n){e.loadNamespaces(t,()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function T(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function z(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(T(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=T(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}</0>`));return T(u[0])}function C(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=(b()?t.useContext(y):{}).i18n,s=r||o||x();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const c=i({},O(),s.options.react),l=n.useSuspense,u=void 0===l?c.useSuspense:l;let p=e||s.options&&s.options.defaultNS;p="string"==typeof p?[p]:p||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(p);const f=(s.isInitialized||s.initializedStoreOnce)&&p.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,s)),d=a(t.useState({t:s.getFixedT(null,"fallback"===c.nsMode?p:p[0])}),2),g=d[0],h=d[1];function m(){h({t:s.getFixedT(null,p[0])})}t.useEffect(()=>{let e=!0;const t=c.bindI18n,n=c.bindI18nStore;function r(){e&&m()}return f||u||P(s,p,()=>{e&&m()}),t&&s&&s.on(t,r),n&&s&&s.store.on(n,r),()=>{e=!1,t&&s&&t.split(" ").forEach(e=>s.off(e,r)),n&&s&&n.split(" ").forEach(e=>s.store.off(e,r))}});const v=[g.t,s,f];if(v.t=g.t,v.i18n=s,v.ready=f,f)return v;if(!f&&!u)return v;throw new Promise(e=>{P(s,p,()=>{m(),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||x();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=(b()?t.useContext(y):{}).i18n,j=g||v||x();if(!j)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const w=h||j.t.bind(j),E=i({},O(),j.options&&j.options.react),S=void 0!==s?s:E.defaultTransParent,$=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}></${n}>`:`${t}<${n}>${e("",T(r),o+1,a)}</${n}>`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}></${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:N("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else N("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),t}("",r,0,E)||E.transEmptyNodeValue,I=E.hashTransKey,P=c||(I?I($):$),R=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:$,count:a,ns:d}),C=P?w(P,R):$;return S?n.createElement(S,m,z(f||r,C,j,E,R)):z(f||r,C,j,E,R)},e.useTranslation=C,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(C(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(C(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t}},r)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=S(e),t.displayName=`withI18nextSSR(${R(e)})`,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=E,e.setDefaults=v,e.getDefaults=O,e.setI18n=w,e.getI18n=x,e.composeInitialProps=S,e.getInitialProps=$,Object.defineProperty(e,"__esModule",{value:!0})});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactI18next={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),i.forEach(function(t){r(e,t,n[t])})}return e}function o(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var s={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},c=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,t,n,r,i){var o=t.indexOf("<",r),a=t.slice(r,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!i&&o>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function f(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(f,"")+"</"+t.name+">"}}var d={parse:function(e,t){t||(t={}),t.components||(t.components=u);var n,r=[],i=-1,o=[],a={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+n.name+">")return;f=!1}var d,g="/"!==l.charAt(1),h=0===l.indexOf("\x3c!--"),m=u+l.length,y=e.charAt(m);g&&!h&&(i++,"tag"===(n=function(e){var t,n=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(c,function(o){if("="===o)return r=!0,void n++;r?0===n?((s[o]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=o):(i.attrs[t]=o.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(i.attrs[t]=t),t=o),n++,r=!1}),i}(l)).type&&t.components[n.name]&&(n.type="component",f=!0),n.voidElement||f||!y||"<"===y||p(n.children,e,i,m,t.ignoreWhitespace),a[n.tagName]=n,0===i&&r.push(n),(d=o[i-1])&&d.children.push(n),o[i]=n),(h||!g||n.voidElement)&&(h||i--,!f&&"<"!==y&&y&&p(d=-1===i?r:o[i].children,e,i,m,t.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+f("",t)},"")}};let g,h,m={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0};const y=n.createContext();function b(){return h}function v(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};m=i({},m,e)}function O(){return m}class S{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function j(e){g=e}function w(){return g}const N={type:"3rdParty",init(e){v(e.options.react),j(e)}};function x(e){return t=>new Promise(n=>{const r=E();e.getInitialProps?e.getInitialProps(t).then(e=>{n(i({},e,r))}):n(r)})}function E(){const e=w(),t=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],n={},r={};return e.languages.forEach(n=>{r[n]={},t.forEach(t=>{r[n][t]=e.getResourceBundle(n,t)||{}})}),n.initialI18nStore=r,n.initialLanguage=e.language,n}function $(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&(t[0]=`react-i18next:: ${t[0]}`),console.warn(...t)}}const I={};function k(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];"string"==typeof t[0]&&I[t[0]]||("string"==typeof t[0]&&(I[t[0]]=new Date),$(...t))}function P(e,t,n){e.loadNamespaces(t,()=>{if(e.isInitialized)n();else{const t=()=>{setTimeout(()=>{e.off("initialized",t)},0),n()};e.on("initialized",t)}})}function R(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function A(e){return e&&(e.children||e.props&&e.props.children)}function C(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function T(e,t,r,o,a){if(""===t)return[];const s=o.transKeepBasicHtmlNodesFor||[],c=t&&new RegExp(s.join("|")).test(t);if(!e&&!c)return[t];const l={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(t=>{"string"!=typeof t&&(A(t)?e(C(t)):"object"!=typeof t||n.isValidElement(t)||Object.assign(l,t))})}(e),t=r.services.interpolator.interpolate(t,i({},l,a),r.language);const u=function e(t,r){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,a,s)=>{const l=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){const u=t[parseInt(a.name,10)]||{},p=n.isValidElement(u);if("string"==typeof u)r.push(u);else if(A(u)){const t=C(u),o=e(t,a.children),c=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>n.isValidElement(e))}(t)&&0===o.length?t:o;u.dummy&&(u.children=c),r.push(n.cloneElement(u,i({},u.props,{key:s}),c))}else if(c&&"object"==typeof u&&u.dummy&&!p){const o=e(t,a.children);r.push(n.cloneElement(u,i({},u.props,{key:s}),o))}else if(isNaN(a.name)&&o.transSupportBasicHtmlNodes)if(a.voidElement)r.push(n.createElement(a.name,{key:`${a.name}-${s}`}));else{const i=e(t,a.children);r.push(n.createElement(a.name,{key:`${a.name}-${s}`},i))}else if("object"!=typeof u||p)1===a.children.length&&l?r.push(n.cloneElement(u,i({},u.props,{key:s}),l)):r.push(u);else{const e=a.children[0]?l:null;e&&r.push(e)}}else"text"===a.type&&r.push(a.content);return r},[])}([{dummy:!0,children:e}],d.parse(`<0>${t}</0>`));return C(u[0])}function z(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=n.i18n,o=b()?t.useContext(y):{},s=o.i18n,c=o.defaultNS,l=r||s||w();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new S),!l){k("You will need pass in an i18next instance by using initReactI18next");const e=[e=>e,{},!0];return e.t=(e=>e),e.i18n={},e.ready=!0,e}const u=i({},O(),l.options.react),p=n.useSuspense,f=void 0===p?u.useSuspense:p;let d=e||c||l.options&&l.options.defaultNS;d="string"==typeof d?[d]:d||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(d);const g=(l.isInitialized||l.initializedStoreOnce)&&d.every(e=>(function(e,t){if(!t.languages||!t.languages.length)return k("i18n.languages were undefined or empty",t.languages),!0;const n=t.languages[0],r=!!t.options&&t.options.fallbackLng,i=t.languages[t.languages.length-1];if("cimode"===n.toLowerCase())return!0;const o=(e,n)=>{const r=t.services.backendConnector.state[`${e}|${n}`];return-1===r||2===r};return!!t.hasResourceBundle(n,e)||!t.services.backendConnector.backend||!(!o(n,e)||r&&!o(i,e))})(e,l));function h(){return{t:l.getFixedT(null,"fallback"===u.nsMode?d:d[0])}}const m=a(t.useState(h()),2),v=m[0],j=m[1];t.useEffect(()=>{let e=!0;const t=u.bindI18n,n=u.bindI18nStore;function r(){e&&j(h())}return g||f||P(l,d,()=>{e&&j(h())}),t&&l&&l.on(t,r),n&&l&&l.store.on(n,r),()=>{e=!1,t&&l&&t.split(" ").forEach(e=>l.off(e,r)),n&&l&&n.split(" ").forEach(e=>l.store.off(e,r))}},[]);const N=[v.t,l,g];if(N.t=v.t,N.i18n=l,N.ready=g,g)return N;if(!g&&!f)return N;throw new Promise(e=>{P(l,d,()=>{j(h()),e()})})}function L(e,n){const r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,i=(b()?t.useContext(y):{}).i18n,o=r||i||w();e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),n&&!o.initializedLanguageOnce&&(o.changeLanguage(n),o.initializedLanguageOnce=!0)}e.Trans=function(e){let r=e.children,a=e.count,s=e.parent,c=e.i18nKey,l=e.tOptions,u=e.values,p=e.defaults,f=e.components,d=e.ns,g=e.i18n,h=e.t,m=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]);const v=b()?t.useContext(y):{},S=v.i18n,j=v.defaultNS,N=g||S||w();if(!N)return k("You will need pass in an i18next instance by using i18nextReactModule"),r;const x=h||N.t.bind(N),E=i({},O(),N.options&&N.options.react),I=void 0!==s?s:E.defaultTransParent;let P=d||j||N.options&&N.options.defaultNS;P="string"==typeof P?[P]:P||["translation"];const R=p||function e(t,r,o,a){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);const s=a.transKeepBasicHtmlNodesFor||[];return r.forEach((r,o)=>{const c=`${o}`;if("string"==typeof r)t=`${t}${r}`;else if(A(r)){const n=s.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:c;t=r.props&&r.props.i18nIsDynamicList?`${t}<${n}></${n}>`:`${t}<${n}>${e("",C(r),o+1,a)}</${n}>`}else if(n.isValidElement(r))t=s.indexOf(r.type)>-1&&0===Object.keys(r.props).length?`${t}<${r.type}/>`:`${t}<${c}></${c}>`;else if("object"==typeof r){const e=i({},r),n=e.format;delete e.format;const o=Object.keys(e);n&&1===o.length?t=`${t}{{${o[0]}, ${n}}}`:1===o.length?t=`${t}{{${o[0]}}}`:$("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else $("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),t}("",r,0,E)||E.transEmptyNodeValue,z=E.hashTransKey,L=c||(z?z(R):R),B=i({},l,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:R,count:a,ns:P}),V=L?x(L,B):R;return I?n.createElement(I,m,T(f||r,V,N,E,B)):T(f||r,V,N,E,B)},e.useTranslation=z,e.withTranslation=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function o(o,s){const c=a(z(e,o),3),l=i({},o,{t:c[0],i18n:c[1],tReady:c[2]});return t.withRef&&s&&(l.ref=s),n.createElement(r,l)}return o.displayName=`withI18nextTranslation(${R(r)})`,o.WrappedComponent=r,t.withRef?n.forwardRef(o):o}},e.Translation=function(e){const t=e.ns,n=e.children,r=a(z(t,o(e,["ns","children"])),3),i=r[0],s=r[1],c=r[2];return n(i,{i18n:s,lng:s.language},c)},e.I18nextProvider=function(e){let t=e.i18n,r=e.defaultNS,i=e.children;return h=!0,n.createElement(y.Provider,{value:{i18n:t,defaultNS:r}},i)},e.withSSR=function(){return function(e){function t(t){let r=t.initialI18nStore,a=t.initialLanguage,s=o(t,["initialI18nStore","initialLanguage"]);return L(r,a),n.createElement(e,i({},s))}return t.getInitialProps=x(e),t.displayName=`withI18nextSSR(${R(e)})`,t.WrappedComponent=e,t}},e.useSSR=L,e.I18nContext=y,e.initReactI18next=N,e.setDefaults=v,e.getDefaults=O,e.setI18n=j,e.getI18n=w,e.composeInitialProps=x,e.getInitialProps=E,Object.defineProperty(e,"__esModule",{value:!0})});
|
package/src/I18nextProvider.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { I18nContext, usedI18nextProvider } from './context';
|
|
3
3
|
|
|
4
|
-
export function I18nextProvider({ i18n, children }) {
|
|
4
|
+
export function I18nextProvider({ i18n, defaultNS, children }) {
|
|
5
5
|
usedI18nextProvider(true);
|
|
6
6
|
|
|
7
7
|
return React.createElement(
|
|
@@ -9,6 +9,7 @@ export function I18nextProvider({ i18n, children }) {
|
|
|
9
9
|
{
|
|
10
10
|
value: {
|
|
11
11
|
i18n,
|
|
12
|
+
defaultNS,
|
|
12
13
|
},
|
|
13
14
|
},
|
|
14
15
|
children,
|
package/src/Trans.js
CHANGED
|
@@ -199,7 +199,9 @@ export function Trans({
|
|
|
199
199
|
t: tFromProps,
|
|
200
200
|
...additionalProps
|
|
201
201
|
}) {
|
|
202
|
-
const { i18n: i18nFromContext } = getHasUsedI18nextProvider()
|
|
202
|
+
const { i18n: i18nFromContext, defaultNS: defaultNSFromContext } = getHasUsedI18nextProvider()
|
|
203
|
+
? useContext(I18nContext)
|
|
204
|
+
: {};
|
|
203
205
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
204
206
|
if (!i18n) {
|
|
205
207
|
warnOnce('You will need pass in an i18next instance by using i18nextReactModule');
|
|
@@ -211,6 +213,10 @@ export function Trans({
|
|
|
211
213
|
const reactI18nextOptions = { ...getDefaults(), ...(i18n.options && i18n.options.react) };
|
|
212
214
|
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
213
215
|
|
|
216
|
+
// prepare having a namespace
|
|
217
|
+
let namespaces = ns || defaultNSFromContext || (i18n.options && i18n.options.defaultNS);
|
|
218
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
219
|
+
|
|
214
220
|
const defaultValue =
|
|
215
221
|
defaults ||
|
|
216
222
|
nodesToString('', children, 0, reactI18nextOptions) ||
|
|
@@ -224,7 +230,7 @@ export function Trans({
|
|
|
224
230
|
...interpolationOverride,
|
|
225
231
|
defaultValue,
|
|
226
232
|
count,
|
|
227
|
-
ns,
|
|
233
|
+
ns: namespaces,
|
|
228
234
|
};
|
|
229
235
|
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
230
236
|
|
package/src/context.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
3
|
let defaultOptions = {
|
|
4
|
-
bindI18n: 'languageChanged',
|
|
4
|
+
bindI18n: 'languageChanging languageChanged',
|
|
5
5
|
bindI18nStore: '',
|
|
6
6
|
// nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
|
|
7
7
|
transEmptyNodeValue: '',
|
package/src/useTranslation.js
CHANGED
|
@@ -11,7 +11,9 @@ import { warnOnce, loadNamespaces, hasLoadedNamespace } from './utils';
|
|
|
11
11
|
export function useTranslation(ns, props = {}) {
|
|
12
12
|
// assert we have the needed i18nInstance
|
|
13
13
|
const { i18n: i18nFromProps } = props;
|
|
14
|
-
const { i18n: i18nFromContext } = getHasUsedI18nextProvider()
|
|
14
|
+
const { i18n: i18nFromContext, defaultNS: defaultNSFromContext } = getHasUsedI18nextProvider()
|
|
15
|
+
? useContext(I18nContext)
|
|
16
|
+
: {};
|
|
15
17
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
16
18
|
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
|
|
17
19
|
if (!i18n) {
|
|
@@ -26,25 +28,24 @@ export function useTranslation(ns, props = {}) {
|
|
|
26
28
|
const { useSuspense = i18nOptions.useSuspense } = props;
|
|
27
29
|
|
|
28
30
|
// prepare having a namespace
|
|
29
|
-
let namespaces = ns || (i18n.options && i18n.options.defaultNS);
|
|
31
|
+
let namespaces = ns || defaultNSFromContext || (i18n.options && i18n.options.defaultNS);
|
|
30
32
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
31
33
|
|
|
32
34
|
// report namespaces as used
|
|
33
35
|
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
|
|
34
36
|
|
|
35
|
-
// are we ready? yes if all namespaces in first language are loaded already (either with data or empty
|
|
37
|
+
// are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
|
|
36
38
|
const ready =
|
|
37
39
|
(i18n.isInitialized || i18n.initializedStoreOnce) &&
|
|
38
40
|
namespaces.every(n => hasLoadedNamespace(n, i18n));
|
|
39
41
|
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function resetT() {
|
|
46
|
-
setT({ t: i18n.getFixedT(null, namespaces[0]) });
|
|
42
|
+
// binding t function to namespace (acts also as rerender trigger)
|
|
43
|
+
function getT() {
|
|
44
|
+
return {
|
|
45
|
+
t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0]),
|
|
46
|
+
};
|
|
47
47
|
}
|
|
48
|
+
const [t, setT] = useState(getT()); // seems we can't have functions as value -> wrap it in obj
|
|
48
49
|
|
|
49
50
|
useEffect(() => {
|
|
50
51
|
let isMounted = true;
|
|
@@ -54,12 +55,12 @@ export function useTranslation(ns, props = {}) {
|
|
|
54
55
|
// in side effect and do not call resetT if unmounted
|
|
55
56
|
if (!ready && !useSuspense) {
|
|
56
57
|
loadNamespaces(i18n, namespaces, () => {
|
|
57
|
-
if (isMounted)
|
|
58
|
+
if (isMounted) setT(getT());
|
|
58
59
|
});
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
function boundReset() {
|
|
62
|
-
if (isMounted)
|
|
63
|
+
if (isMounted) setT(getT());
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
// bind events to trigger change, like languageChanged
|
|
@@ -73,7 +74,7 @@ export function useTranslation(ns, props = {}) {
|
|
|
73
74
|
if (bindI18nStore && i18n)
|
|
74
75
|
bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
|
75
76
|
};
|
|
76
|
-
});
|
|
77
|
+
}, []); // define props to trigger using effect on rerender (none here)
|
|
77
78
|
|
|
78
79
|
const ret = [t.t, i18n, ready];
|
|
79
80
|
ret.t = t.t;
|
|
@@ -89,7 +90,7 @@ export function useTranslation(ns, props = {}) {
|
|
|
89
90
|
// not yet loaded namespaces -> load them -> and trigger suspense
|
|
90
91
|
throw new Promise(resolve => {
|
|
91
92
|
loadNamespaces(i18n, namespaces, () => {
|
|
92
|
-
|
|
93
|
+
setT(getT());
|
|
93
94
|
resolve();
|
|
94
95
|
});
|
|
95
96
|
});
|
package/src/withSSR.js
CHANGED
|
@@ -15,6 +15,7 @@ export function withSSR() {
|
|
|
15
15
|
|
|
16
16
|
I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
|
|
17
17
|
I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
|
|
18
|
+
I18nextWithSSR.WrappedComponent = WrappedComponent;
|
|
18
19
|
|
|
19
20
|
return I18nextWithSSR;
|
|
20
21
|
};
|
package/src/withTranslation.js
CHANGED
|
@@ -22,6 +22,9 @@ export function withTranslation(ns, options = {}) {
|
|
|
22
22
|
I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(
|
|
23
23
|
WrappedComponent,
|
|
24
24
|
)})`;
|
|
25
|
+
|
|
26
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
27
|
+
|
|
25
28
|
return options.withRef ? React.forwardRef(I18nextWithTranslation) : I18nextWithTranslation;
|
|
26
29
|
};
|
|
27
30
|
}
|