react-i18next 10.12.4 → 10.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/amd/react-i18next.js +63 -30
- package/dist/amd/react-i18next.min.js +1 -1
- package/dist/commonjs/Trans.js +27 -23
- package/dist/commonjs/Translation.js +0 -2
- package/dist/commonjs/useSSR.js +2 -1
- package/dist/commonjs/withTranslation.js +20 -6
- package/dist/es/Trans.js +27 -23
- package/dist/es/Translation.js +0 -1
- package/dist/es/useSSR.js +2 -1
- package/dist/es/withTranslation.js +18 -6
- package/dist/umd/react-i18next.js +63 -30
- package/dist/umd/react-i18next.min.js +1 -1
- package/package.json +16 -15
- package/react-i18next.js +63 -30
- package/react-i18next.min.js +1 -1
- package/src/Trans.js +30 -22
- package/src/Translation.js +0 -1
- package/src/index.d.ts +17 -8
- package/src/useSSR.js +2 -1
- package/src/withTranslation.js +8 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
### 10.13.2
|
|
2
|
+
|
|
3
|
+
- typescript: Add t function to TransProps types [969](https://github.com/i18next/react-i18next/pull/969)
|
|
4
|
+
- lint: Fix linter errors [966](https://github.com/i18next/react-i18next/pull/966)
|
|
5
|
+
|
|
6
|
+
### 10.13.1
|
|
7
|
+
|
|
8
|
+
- avoid conditional hook call in edge case (was only issue in wrong setup useContext outside I18nextProvider) [951](https://github.com/i18next/react-i18next/pull/951)
|
|
9
|
+
|
|
10
|
+
### 10.13.0
|
|
11
|
+
|
|
12
|
+
- also use count from `values` object passed to Trans if passed - else use the one on props [947](https://github.com/i18next/react-i18next/pull/947)
|
|
13
|
+
|
|
14
|
+
### 10.12.5
|
|
15
|
+
|
|
16
|
+
- typescript: Update types for reportNamespaces [945](https://github.com/i18next/react-i18next/pull/945)
|
|
17
|
+
- typescript: Improve withSSR type definition [943](https://github.com/i18next/react-i18next/pull/943)
|
|
18
|
+
|
|
1
19
|
### 10.12.4
|
|
2
20
|
|
|
3
21
|
- ICU: Fixes macro to support count prop and expressions better [939](https://github.com/i18next/react-i18next/pull/939)
|
|
@@ -17,6 +17,24 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
17
17
|
return obj;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
function _extends() {
|
|
21
|
+
_extends = Object.assign || function (target) {
|
|
22
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
23
|
+
var source = arguments[i];
|
|
24
|
+
|
|
25
|
+
for (var key in source) {
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
27
|
+
target[key] = source[key];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return target;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return _extends.apply(this, arguments);
|
|
36
|
+
}
|
|
37
|
+
|
|
20
38
|
function ownKeys(object, enumerableOnly) {
|
|
21
39
|
var keys = Object.keys(object);
|
|
22
40
|
|
|
@@ -488,33 +506,36 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
488
506
|
return children.every(child => React__default.isValidElement(child));
|
|
489
507
|
}
|
|
490
508
|
|
|
491
|
-
function
|
|
509
|
+
function getAsArray(data) {
|
|
510
|
+
return Array.isArray(data) ? data : [data];
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function nodesToString(startingString, children, index, i18nOptions) {
|
|
492
514
|
if (!children) return '';
|
|
493
|
-
|
|
515
|
+
var stringNode = startingString;
|
|
516
|
+
var childrenArray = getAsArray(children);
|
|
494
517
|
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
495
|
-
|
|
496
|
-
// const isElement = React.isValidElement(child);
|
|
497
|
-
// const elementKey = `${index !== 0 ? index + '-' : ''}${i}:${typeof child.type === 'function' ? child.type.name : child.type || 'var'}`;
|
|
518
|
+
childrenArray.forEach((child, i) => {
|
|
498
519
|
var elementKey = "".concat(i);
|
|
499
520
|
|
|
500
521
|
if (typeof child === 'string') {
|
|
501
|
-
|
|
522
|
+
stringNode = "".concat(stringNode).concat(child);
|
|
502
523
|
} else if (hasChildren(child)) {
|
|
503
524
|
var elementTag = keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 1 && typeof hasChildren(child) === 'string' ? child.type : elementKey;
|
|
504
525
|
|
|
505
526
|
if (child.props && child.props.i18nIsDynamicList) {
|
|
506
527
|
// we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
|
|
507
528
|
// the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
|
|
508
|
-
|
|
529
|
+
stringNode = "".concat(stringNode, "<").concat(elementTag, "></").concat(elementTag, ">");
|
|
509
530
|
} else {
|
|
510
531
|
// regular case mapping the inner children
|
|
511
|
-
|
|
532
|
+
stringNode = "".concat(stringNode, "<").concat(elementTag, ">").concat(nodesToString('', getChildren(child), i + 1, i18nOptions), "</").concat(elementTag, ">");
|
|
512
533
|
}
|
|
513
534
|
} else if (React__default.isValidElement(child)) {
|
|
514
535
|
if (keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 0) {
|
|
515
|
-
|
|
536
|
+
stringNode = "".concat(stringNode, "<").concat(child.type, "/>");
|
|
516
537
|
} else {
|
|
517
|
-
|
|
538
|
+
stringNode = "".concat(stringNode, "<").concat(elementKey, "></").concat(elementKey, ">");
|
|
518
539
|
}
|
|
519
540
|
} else if (typeof child === 'object') {
|
|
520
541
|
var clone = _objectSpread2({}, child);
|
|
@@ -526,9 +547,9 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
526
547
|
var keys = Object.keys(clone);
|
|
527
548
|
|
|
528
549
|
if (format && keys.length === 1) {
|
|
529
|
-
|
|
550
|
+
stringNode = "".concat(stringNode, "{{").concat(keys[0], ", ").concat(format, "}}");
|
|
530
551
|
} else if (keys.length === 1) {
|
|
531
|
-
|
|
552
|
+
stringNode = "".concat(stringNode, "{{").concat(keys[0], "}}");
|
|
532
553
|
} else {
|
|
533
554
|
// not a valid interpolation object (can only contain one value plus format)
|
|
534
555
|
warn("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.", child);
|
|
@@ -537,7 +558,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
537
558
|
warn("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}}.", child);
|
|
538
559
|
}
|
|
539
560
|
});
|
|
540
|
-
return
|
|
561
|
+
return stringNode;
|
|
541
562
|
}
|
|
542
563
|
|
|
543
564
|
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
@@ -551,22 +572,22 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
551
572
|
var data = {};
|
|
552
573
|
|
|
553
574
|
function getData(childs) {
|
|
554
|
-
|
|
555
|
-
|
|
575
|
+
var childrenArray = getAsArray(childs);
|
|
576
|
+
childrenArray.forEach(child => {
|
|
556
577
|
if (typeof child === 'string') return;
|
|
557
578
|
if (hasChildren(child)) getData(getChildren(child));else if (typeof child === 'object' && !React__default.isValidElement(child)) Object.assign(data, child);
|
|
558
579
|
});
|
|
559
580
|
}
|
|
560
581
|
|
|
561
582
|
getData(children);
|
|
562
|
-
|
|
583
|
+
var interpolatedString = i18n.services.interpolator.interpolate(targetString, _objectSpread2({}, data, {}, combinedTOpts), i18n.language); // parse ast from string with additional wrapper tag
|
|
563
584
|
// -> avoids issues in parser removing prepending text nodes
|
|
564
585
|
|
|
565
|
-
var ast = htmlParseStringify2.parse("<0>".concat(
|
|
586
|
+
var ast = htmlParseStringify2.parse("<0>".concat(interpolatedString, "</0>"));
|
|
566
587
|
|
|
567
|
-
function mapAST(
|
|
568
|
-
|
|
569
|
-
|
|
588
|
+
function mapAST(reactNode, astNode) {
|
|
589
|
+
var reactNodes = getAsArray(reactNode);
|
|
590
|
+
var astNodes = getAsArray(astNode);
|
|
570
591
|
return astNodes.reduce((mem, node, i) => {
|
|
571
592
|
var translationContent = node.children && node.children[0] && node.children[0].content;
|
|
572
593
|
|
|
@@ -596,7 +617,7 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
596
617
|
mem.push(React__default.cloneElement(child, _objectSpread2({}, child.props, {
|
|
597
618
|
key: i
|
|
598
619
|
}), _inner));
|
|
599
|
-
} else if (isNaN(node.name)) {
|
|
620
|
+
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
600
621
|
if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
601
622
|
if (node.voidElement) {
|
|
602
623
|
mem.push(React__default.createElement(node.name, {
|
|
@@ -703,9 +724,10 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
703
724
|
}
|
|
704
725
|
};
|
|
705
726
|
|
|
706
|
-
var combinedTOpts = _objectSpread2({}, tOptions, {
|
|
727
|
+
var combinedTOpts = _objectSpread2({}, tOptions, {
|
|
728
|
+
count
|
|
729
|
+
}, values, {}, interpolationOverride, {
|
|
707
730
|
defaultValue,
|
|
708
|
-
count,
|
|
709
731
|
ns: namespaces
|
|
710
732
|
});
|
|
711
733
|
|
|
@@ -809,17 +831,22 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
809
831
|
function withTranslation(ns) {
|
|
810
832
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
811
833
|
return function Extend(WrappedComponent) {
|
|
812
|
-
function I18nextWithTranslation(
|
|
813
|
-
var
|
|
834
|
+
function I18nextWithTranslation(_ref) {
|
|
835
|
+
var {
|
|
836
|
+
forwardedRef
|
|
837
|
+
} = _ref,
|
|
838
|
+
rest = _objectWithoutProperties(_ref, ["forwardedRef"]);
|
|
839
|
+
|
|
840
|
+
var [t, i18n, ready] = useTranslation(ns, rest);
|
|
814
841
|
|
|
815
|
-
var passDownProps = _objectSpread2({},
|
|
842
|
+
var passDownProps = _objectSpread2({}, rest, {
|
|
816
843
|
t,
|
|
817
844
|
i18n,
|
|
818
845
|
tReady: ready
|
|
819
846
|
});
|
|
820
847
|
|
|
821
|
-
if (options.withRef &&
|
|
822
|
-
passDownProps.ref =
|
|
848
|
+
if (options.withRef && forwardedRef) {
|
|
849
|
+
passDownProps.ref = forwardedRef;
|
|
823
850
|
}
|
|
824
851
|
|
|
825
852
|
return React__default.createElement(WrappedComponent, passDownProps);
|
|
@@ -827,7 +854,12 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
827
854
|
|
|
828
855
|
I18nextWithTranslation.displayName = "withI18nextTranslation(".concat(getDisplayName(WrappedComponent), ")");
|
|
829
856
|
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
830
|
-
|
|
857
|
+
|
|
858
|
+
var forwardRef = (props, ref) => React__default.createElement(I18nextWithTranslation, _extends({}, props, {
|
|
859
|
+
forwardedRef: ref
|
|
860
|
+
}));
|
|
861
|
+
|
|
862
|
+
return options.withRef ? React__default.forwardRef(forwardRef) : I18nextWithTranslation;
|
|
831
863
|
};
|
|
832
864
|
}
|
|
833
865
|
|
|
@@ -865,9 +897,10 @@ define(['exports', 'react'], function (exports, React) { 'use strict';
|
|
|
865
897
|
var {
|
|
866
898
|
i18n: i18nFromProps
|
|
867
899
|
} = props;
|
|
900
|
+
var ReactI18nContext = React.useContext(I18nContext);
|
|
868
901
|
var {
|
|
869
902
|
i18n: i18nFromContext
|
|
870
|
-
} = getHasUsedI18nextProvider() ?
|
|
903
|
+
} = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {};
|
|
871
904
|
var i18n = i18nFromProps || i18nFromContext || getI18n(); // opt out if is a cloned instance, eg. created by i18next-express-middleware on request
|
|
872
905
|
// -> do not set initial stuff on server side
|
|
873
906
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
define(["exports","react"],function(e,n){"use strict";var t="default"in n?n.default:n;function r(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function a(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter(function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable})),t.push.apply(t,r)}return t}function i(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?a(t,!0).forEach(function(n){r(e,n,t[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):a(t).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))})}return e}function o(e,n){if(null==e)return{};var t,r,a=function(e,n){if(null==e)return{};var t,r,a={},i=Object.keys(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||(a[t]=e[t]);return a}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(a[t]=e[t])}return a}var c={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},s=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,l=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,u=Object.create?Object.create(null):{};function p(e,n,t,r,a){var i=n.indexOf("<",r),o=n.slice(r,-1===i?void 0:i);/^\s*$/.test(o)&&(o=" "),(!a&&i>-1&&t+e.length>=0||" "!==o)&&e.push({type:"text",content:o})}function f(e,n){switch(n.type){case"text":return e+n.content;case"tag":return e+="<"+n.name+(n.attrs?function(e){var n=[];for(var t in e)n.push(t+'="'+e[t]+'"');return n.length?" "+n.join(" "):""}(n.attrs):"")+(n.voidElement?"/>":">"),n.voidElement?e:e+n.children.reduce(f,"")+"</"+n.name+">"}}var d,g,m={parse:function(e,n){n||(n={}),n.components||(n.components=u);var t,r=[],a=-1,i=[],o={},f=!1;return e.replace(l,function(l,u){if(f){if(l!=="</"+t.name+">")return;f=!1}var d,g="/"!==l.charAt(1),m=0===l.indexOf("\x3c!--"),h=u+l.length,v=e.charAt(h);g&&!m&&(a++,"tag"===(t=function(e){var n,t=0,r=!0,a={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(s,function(i){if("="===i)return r=!0,void t++;r?0===t?((c[i]||"/"===e.charAt(e.length-2))&&(a.voidElement=!0),a.name=i):(a.attrs[n]=i.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(a.attrs[n]=n),n=i),t++,r=!1}),a}(l)).type&&n.components[t.name]&&(t.type="component",f=!0),t.voidElement||f||!v||"<"===v||p(t.children,e,a,h,n.ignoreWhitespace),o[t.tagName]=t,0===a&&r.push(t),(d=i[a-1])&&d.children.push(t),i[a]=t),(m||!g||t.voidElement)&&(m||a--,!f&&"<"!==v&&v&&p(d=-1===a?r:i[a].children,e,a,h,n.ignoreWhitespace))}),!r.length&&e.length&&p(r,e,0,0,n.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,n){return e+f("",n)},"")}},h={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},v=t.createContext();function y(){return g}function b(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};h=i({},h,{},e)}function O(){return h}class j{constructor(){this.usedNamespaces={}}addUsedNamespaces(e){e.forEach(e=>{this.usedNamespaces[e]||(this.usedNamespaces[e]=!0)})}getUsedNamespaces(){return Object.keys(this.usedNamespaces)}}function S(e){d=e}function E(){return d}var w={type:"3rdParty",init(e){b(e.options.react),S(e)}};function N(e){return n=>new Promise(t=>{var r=x();e.getInitialProps?e.getInitialProps(n).then(e=>{t(i({},e,{},r))}):t(r)})}function x(){var e=E(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach(t=>{r[t]={},n.forEach(n=>{r[t][n]=e.getResourceBundle(t,n)||{}})}),t.initialI18nStore=r,t.initialLanguage=e.language,t}function k(){if(console&&console.warn){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&(n[0]="react-i18next:: ".concat(n[0])),console.warn(...n)}}var I={};function P(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&I[n[0]]||("string"==typeof n[0]&&(I[n[0]]=new Date),k(...n))}function C(e,n,t){e.loadNamespaces(n,()=>{if(e.isInitialized)t();else{var n=()=>{setTimeout(()=>{e.off("initialized",n)},0),t()};e.on("initialized",n)}})}function 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,n,r,a,o){if(""===n)return[];var c=a.transKeepBasicHtmlNodesFor||[],s=n&&new RegExp(c.join("|")).test(n);if(!e&&!s)return[n];var l={};!function e(n){"[object Array]"!==Object.prototype.toString.call(n)&&(n=[n]),n.forEach(n=>{"string"!=typeof n&&(A(n)?e(T(n)):"object"!=typeof n||t.isValidElement(n)||Object.assign(l,n))})}(e),n=r.services.interpolator.interpolate(n,i({},l,{},o),r.language);var u=function e(n,r){return"[object Array]"!==Object.prototype.toString.call(n)&&(n=[n]),"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]),r.reduce((r,o,l)=>{var u=o.children&&o.children[0]&&o.children[0].content;if("tag"===o.type){var p=n[parseInt(o.name,10)]||{},f=t.isValidElement(p);if("string"==typeof p)r.push(p);else if(A(p)){var d=T(p),g=e(d,o.children),m=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>t.isValidElement(e))}(d)&&0===g.length?d:g;p.dummy&&(p.children=m),r.push(t.cloneElement(p,i({},p.props,{key:l}),m))}else if(s&&"object"==typeof p&&p.dummy&&!f){var h=e(n,o.children);r.push(t.cloneElement(p,i({},p.props,{key:l}),h))}else if(isNaN(o.name))if(a.transSupportBasicHtmlNodes&&c.indexOf(o.name)>-1)if(o.voidElement)r.push(t.createElement(o.name,{key:"".concat(o.name,"-").concat(l)}));else{var v=e(n,o.children);r.push(t.createElement(o.name,{key:"".concat(o.name,"-").concat(l)},v))}else if(o.voidElement)r.push("<".concat(o.name," />"));else{var y=e(n,o.children);r.push("<".concat(o.name,">").concat(y,"</").concat(o.name,">"))}else if("object"!=typeof p||f)1===o.children.length&&u?r.push(t.cloneElement(p,i({},p.props,{key:l}),u)):r.push(t.cloneElement(p,i({},p.props,{key:l})));else{var b=o.children[0]?u:null;b&&r.push(b)}}else"text"===o.type&&r.push(o.content);return r},[])}([{dummy:!0,children:e}],m.parse("<0>".concat(n,"</0>")));return T(u[0])}function L(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},{i18n:r}=t,a=n.useContext(v),{i18n:o,defaultNS:c}=y()&&a||{},s=r||o||E();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){P("You will need pass in an i18next instance by using initReactI18next");var l=[e=>e,{},!1];return l.t=e=>e,l.i18n={},l.ready=!1,l}var u=i({},O(),{},s.options.react),{useSuspense:p=u.useSuspense}=t,f=e||c||s.options&&s.options.defaultNS;f="string"==typeof f?[f]:f||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(f);var d=(s.isInitialized||s.initializedStoreOnce)&&f.every(e=>(function(e,n){if(!n.languages||!n.languages.length)return P("i18n.languages were undefined or empty",n.languages),!0;var t=n.languages[0],r=!!n.options&&n.options.fallbackLng,a=n.languages[n.languages.length-1];if("cimode"===t.toLowerCase())return!0;var i=(e,t)=>{var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!!n.hasResourceBundle(t,e)||(!n.services.backendConnector.backend||!(!i(t,e)||r&&!i(a,e)))})(e,s));function g(){return{t:s.getFixedT(null,"fallback"===u.nsMode?f:f[0])}}var[m,h]=n.useState(g());n.useEffect(()=>{var e=!0,{bindI18n:n,bindI18nStore:t}=u;function r(){e&&h(g())}return d||p||C(s,f,()=>{e&&h(g())}),n&&s&&s.on(n,r),t&&s&&s.store.on(t,r),()=>{e=!1,n&&s&&n.split(" ").forEach(e=>s.off(e,r)),t&&s&&t.split(" ").forEach(e=>s.store.off(e,r))}},[f.join()]);var b=[m.t,s,d];if(b.t=m.t,b.i18n=s,b.ready=d,d)return b;if(!d&&!p)return b;throw new Promise(e=>{C(s,f,()=>{h(g()),e()})})}function D(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},{i18n:a}=r,{i18n:i}=y()?n.useContext(v):{},o=a||i||E();o.options&&o.options.isClone||(e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.initializedStoreOnce=!0),t&&!o.initializedLanguageOnce&&(o.changeLanguage(t),o.initializedLanguageOnce=!0))}e.I18nContext=v,e.I18nextProvider=function(e){var{i18n:n,defaultNS:r,children:a}=e;return g=!0,t.createElement(v.Provider,{value:{i18n:n,defaultNS:r}},a)},e.Trans=function(e){var{children:r,count:a,parent:c,i18nKey:s,tOptions:l,values:u,defaults:p,components:f,ns:d,i18n:g,t:m}=e,h=o(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),{i18n:b,defaultNS:j}=y()&&n.useContext(v)||{},S=g||b||E();if(!S)return P("You will need pass in an i18next instance by using i18nextReactModule"),r;var w=m||S.t.bind(S)||(e=>e),N=i({},O(),{},S.options&&S.options.react),x=void 0!==c?c:N.defaultTransParent,I=d||w.ns||j||S.options&&S.options.defaultNS;I="string"==typeof I?[I]:I||["translation"];var C=p||function e(n,r,a,o){if(!r)return"";"[object Array]"!==Object.prototype.toString.call(r)&&(r=[r]);var c=o.transKeepBasicHtmlNodesFor||[];return r.forEach((r,a)=>{var s="".concat(a);if("string"==typeof r)n="".concat(n).concat(r);else if(A(r)){var l=c.indexOf(r.type)>-1&&1===Object.keys(r.props).length&&"string"==typeof A(r)?r.type:s;n=r.props&&r.props.i18nIsDynamicList?"".concat(n,"<").concat(l,"></").concat(l,">"):"".concat(n,"<").concat(l,">").concat(e("",T(r),a+1,o),"</").concat(l,">")}else if(t.isValidElement(r))n=c.indexOf(r.type)>-1&&0===Object.keys(r.props).length?"".concat(n,"<").concat(r.type,"/>"):"".concat(n,"<").concat(s,"></").concat(s,">");else if("object"==typeof r){var u=i({},r),{format:p}=u;delete u.format;var f=Object.keys(u);p&&1===f.length?n="".concat(n,"{{").concat(f[0],", ").concat(p,"}}"):1===f.length?n="".concat(n,"{{").concat(f[0],"}}"):k("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",r)}else k("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",r)}),n}("",r,0,N)||N.transEmptyNodeValue,{hashTransKey:R}=N,L=s||(R?R(C):C),D=i({},l,{},u,{},u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:C,count:a,ns:I}),B=L?w(L,D):C;return x?t.createElement(x,h,z(f||r,B,S,N,D)):z(f||r,B,S,N,D)},e.Translation=function(e){var{ns:n,children:t}=e,r=o(e,["ns","children"]),[a,i,c]=L(n,r);return t(a,{i18n:i,lng:i.language},c)},e.composeInitialProps=N,e.getDefaults=O,e.getI18n=E,e.getInitialProps=x,e.initReactI18next=w,e.setDefaults=b,e.setI18n=S,e.useSSR=D,e.useTranslation=L,e.withSSR=function(){return function(e){function n(n){var{initialI18nStore:r,initialLanguage:a}=n,c=o(n,["initialI18nStore","initialLanguage"]);return D(r,a),t.createElement(e,i({},c))}return n.getInitialProps=N(e),n.displayName="withI18nextSSR(".concat(R(e),")"),n.WrappedComponent=e,n}},e.withTranslation=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function a(a,o){var[c,s,l]=L(e,a),u=i({},a,{t:c,i18n:s,tReady:l});return n.withRef&&o&&(u.ref=o),t.createElement(r,u)}return a.displayName="withI18nextTranslation(".concat(R(r),")"),a.WrappedComponent=r,n.withRef?t.forwardRef(a):a}},Object.defineProperty(e,"__esModule",{value:!0})});
|
|
1
|
+
define(["exports","react"],(function(e,n){"use strict";var t="default"in n?n.default:n;function r(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function a(){return(a=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}function i(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,r)}return t}function o(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?i(t,!0).forEach((function(n){r(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):i(t).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function s(e,n){if(null==e)return{};var t,r,a=function(e,n){if(null==e)return{};var t,r,a={},i=Object.keys(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||(a[t]=e[t]);return a}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(a[t]=e[t])}return a}var c={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},l=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,u=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,p=Object.create?Object.create(null):{};function f(e,n,t,r,a){var i=n.indexOf("<",r),o=n.slice(r,-1===i?void 0:i);/^\s*$/.test(o)&&(o=" "),(!a&&i>-1&&t+e.length>=0||" "!==o)&&e.push({type:"text",content:o})}function d(e,n){switch(n.type){case"text":return e+n.content;case"tag":return e+="<"+n.name+(n.attrs?function(e){var n=[];for(var t in e)n.push(t+'="'+e[t]+'"');return n.length?" "+n.join(" "):""}(n.attrs):"")+(n.voidElement?"/>":">"),n.voidElement?e:e+n.children.reduce(d,"")+"</"+n.name+">"}}var g,h,m={parse:function(e,n){n||(n={}),n.components||(n.components=p);var t,r=[],a=-1,i=[],o={},s=!1;return e.replace(u,(function(u,p){if(s){if(u!=="</"+t.name+">")return;s=!1}var d,g="/"!==u.charAt(1),h=0===u.indexOf("\x3c!--"),m=p+u.length,v=e.charAt(m);g&&!h&&(a++,"tag"===(t=function(e){var n,t=0,r=!0,a={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(l,(function(i){if("="===i)return r=!0,void t++;r?0===t?((c[i]||"/"===e.charAt(e.length-2))&&(a.voidElement=!0),a.name=i):(a.attrs[n]=i.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(a.attrs[n]=n),n=i),t++,r=!1})),a}(u)).type&&n.components[t.name]&&(t.type="component",s=!0),t.voidElement||s||!v||"<"===v||f(t.children,e,a,m,n.ignoreWhitespace),o[t.tagName]=t,0===a&&r.push(t),(d=i[a-1])&&d.children.push(t),i[a]=t),(h||!g||t.voidElement)&&(h||a--,!s&&"<"!==v&&v&&f(d=-1===a?r:i[a].children,e,a,m,n.ignoreWhitespace))})),!r.length&&e.length&&f(r,e,0,0,n.ignoreWhitespace),r},stringify:function(e){return e.reduce((function(e,n){return e+d("",n)}),"")}},v={bindI18n:"languageChanging languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},y=t.createContext();function b(){return h}function O(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};v=o({},v,{},e)}function w(){return v}class E{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 N(){return g}var S={type:"3rdParty",init(e){O(e.options.react),j(e)}};function x(e){return n=>new Promise(t=>{var r=k();e.getInitialProps?e.getInitialProps(n).then(e=>{t(o({},e,{},r))}):t(r)})}function k(){var e=N(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach(t=>{r[t]={},n.forEach(n=>{r[t][n]=e.getResourceBundle(t,n)||{}})}),t.initialI18nStore=r,t.initialLanguage=e.language,t}function I(){if(console&&console.warn){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&(n[0]="react-i18next:: ".concat(n[0])),console.warn(...n)}}var P={};function R(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&P[n[0]]||("string"==typeof n[0]&&(P[n[0]]=new Date),I(...n))}function C(e,n,t){e.loadNamespaces(n,()=>{if(e.isInitialized)t();else{var n=()=>{setTimeout(()=>{e.off("initialized",n)},0),t()};e.on("initialized",n)}})}function T(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function z(e){return e&&(e.children||e.props&&e.props.children)}function L(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function A(e){return Array.isArray(e)?e:[e]}function D(e,n,r,a,i){if(""===n)return[];var s=a.transKeepBasicHtmlNodesFor||[],c=n&&new RegExp(s.join("|")).test(n);if(!e&&!c)return[n];var l={};!function e(n){A(n).forEach(n=>{"string"!=typeof n&&(z(n)?e(L(n)):"object"!=typeof n||t.isValidElement(n)||Object.assign(l,n))})}(e);var u=r.services.interpolator.interpolate(n,o({},l,{},i),r.language);var p=function e(n,r){var i=A(n);return A(r).reduce((n,r,l)=>{var u=r.children&&r.children[0]&&r.children[0].content;if("tag"===r.type){var p=i[parseInt(r.name,10)]||{},f=t.isValidElement(p);if("string"==typeof p)n.push(p);else if(z(p)){var d=L(p),g=e(d,r.children),h=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every(e=>t.isValidElement(e))}(d)&&0===g.length?d:g;p.dummy&&(p.children=h),n.push(t.cloneElement(p,o({},p.props,{key:l}),h))}else if(c&&"object"==typeof p&&p.dummy&&!f){var m=e(i,r.children);n.push(t.cloneElement(p,o({},p.props,{key:l}),m))}else if(Number.isNaN(parseFloat(r.name)))if(a.transSupportBasicHtmlNodes&&s.indexOf(r.name)>-1)if(r.voidElement)n.push(t.createElement(r.name,{key:"".concat(r.name,"-").concat(l)}));else{var v=e(i,r.children);n.push(t.createElement(r.name,{key:"".concat(r.name,"-").concat(l)},v))}else if(r.voidElement)n.push("<".concat(r.name," />"));else{var y=e(i,r.children);n.push("<".concat(r.name,">").concat(y,"</").concat(r.name,">"))}else if("object"!=typeof p||f)1===r.children.length&&u?n.push(t.cloneElement(p,o({},p.props,{key:l}),u)):n.push(t.cloneElement(p,o({},p.props,{key:l})));else{var b=r.children[0]?u:null;b&&n.push(b)}}else"text"===r.type&&n.push(r.content);return n},[])}([{dummy:!0,children:e}],m.parse("<0>".concat(u,"</0>")));return L(p[0])}function B(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},{i18n:r}=t,a=n.useContext(y),{i18n:i,defaultNS:s}=b()&&a||{},c=r||i||N();if(c&&!c.reportNamespaces&&(c.reportNamespaces=new E),!c){R("You will need pass in an i18next instance by using initReactI18next");var l=[e=>e,{},!1];return l.t=e=>e,l.i18n={},l.ready=!1,l}var u=o({},w(),{},c.options.react),{useSuspense:p=u.useSuspense}=t,f=e||s||c.options&&c.options.defaultNS;f="string"==typeof f?[f]:f||["translation"],c.reportNamespaces.addUsedNamespaces&&c.reportNamespaces.addUsedNamespaces(f);var d=(c.isInitialized||c.initializedStoreOnce)&&f.every(e=>(function(e,n){if(!n.languages||!n.languages.length)return R("i18n.languages were undefined or empty",n.languages),!0;var t=n.languages[0],r=!!n.options&&n.options.fallbackLng,a=n.languages[n.languages.length-1];if("cimode"===t.toLowerCase())return!0;var i=(e,t)=>{var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!!n.hasResourceBundle(t,e)||(!n.services.backendConnector.backend||!(!i(t,e)||r&&!i(a,e)))})(e,c));function g(){return{t:c.getFixedT(null,"fallback"===u.nsMode?f:f[0])}}var[h,m]=n.useState(g());n.useEffect(()=>{var e=!0,{bindI18n:n,bindI18nStore:t}=u;function r(){e&&m(g())}return d||p||C(c,f,()=>{e&&m(g())}),n&&c&&c.on(n,r),t&&c&&c.store.on(t,r),()=>{e=!1,n&&c&&n.split(" ").forEach(e=>c.off(e,r)),t&&c&&t.split(" ").forEach(e=>c.store.off(e,r))}},[f.join()]);var v=[h.t,c,d];if(v.t=h.t,v.i18n=c,v.ready=d,d)return v;if(!d&&!p)return v;throw new Promise(e=>{C(c,f,()=>{m(g()),e()})})}function V(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},{i18n:a}=r,i=n.useContext(y),{i18n:o}=b()&&i||{},s=a||o||N();s.options&&s.options.isClone||(e&&!s.initializedStoreOnce&&(s.services.resourceStore.data=e,s.initializedStoreOnce=!0),t&&!s.initializedLanguageOnce&&(s.changeLanguage(t),s.initializedLanguageOnce=!0))}e.I18nContext=y,e.I18nextProvider=function(e){var{i18n:n,defaultNS:r,children:a}=e;return h=!0,t.createElement(y.Provider,{value:{i18n:n,defaultNS:r}},a)},e.Trans=function(e){var{children:r,count:a,parent:i,i18nKey:c,tOptions:l,values:u,defaults:p,components:f,ns:d,i18n:g,t:h}=e,m=s(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),{i18n:v,defaultNS:O}=b()&&n.useContext(y)||{},E=g||v||N();if(!E)return R("You will need pass in an i18next instance by using i18nextReactModule"),r;var j=h||E.t.bind(E)||(e=>e),S=o({},w(),{},E.options&&E.options.react),x=void 0!==i?i:S.defaultTransParent,k=d||j.ns||O||E.options&&E.options.defaultNS;k="string"==typeof k?[k]:k||["translation"];var P=p||function e(n,r,a,i){if(!r)return"";var s=n,c=A(r),l=i.transKeepBasicHtmlNodesFor||[];return c.forEach((n,r)=>{var a="".concat(r);if("string"==typeof n)s="".concat(s).concat(n);else if(z(n)){var c=l.indexOf(n.type)>-1&&1===Object.keys(n.props).length&&"string"==typeof z(n)?n.type:a;s=n.props&&n.props.i18nIsDynamicList?"".concat(s,"<").concat(c,"></").concat(c,">"):"".concat(s,"<").concat(c,">").concat(e("",L(n),r+1,i),"</").concat(c,">")}else if(t.isValidElement(n))s=l.indexOf(n.type)>-1&&0===Object.keys(n.props).length?"".concat(s,"<").concat(n.type,"/>"):"".concat(s,"<").concat(a,"></").concat(a,">");else if("object"==typeof n){var u=o({},n),{format:p}=u;delete u.format;var f=Object.keys(u);p&&1===f.length?s="".concat(s,"{{").concat(f[0],", ").concat(p,"}}"):1===f.length?s="".concat(s,"{{").concat(f[0],"}}"):I("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",n)}else I("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",n)}),s}("",r,0,S)||S.transEmptyNodeValue,{hashTransKey:C}=S,T=c||(C?C(P):P),B=o({},l,{count:a},u,{},u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:P,ns:k}),V=T?j(T,B):P;return x?t.createElement(x,m,D(f||r,V,E,S,B)):D(f||r,V,E,S,B)},e.Translation=function(e){var{ns:n,children:t}=e,r=s(e,["ns","children"]),[a,i,o]=B(n,r);return t(a,{i18n:i,lng:i.language},o)},e.composeInitialProps=x,e.getDefaults=w,e.getI18n=N,e.getInitialProps=k,e.initReactI18next=S,e.setDefaults=O,e.setI18n=j,e.useSSR=V,e.useTranslation=B,e.withSSR=function(){return function(e){function n(n){var{initialI18nStore:r,initialLanguage:a}=n,i=s(n,["initialI18nStore","initialLanguage"]);return V(r,a),t.createElement(e,o({},i))}return n.getInitialProps=x(e),n.displayName="withI18nextSSR(".concat(T(e),")"),n.WrappedComponent=e,n}},e.withTranslation=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function i(a){var{forwardedRef:i}=a,c=s(a,["forwardedRef"]),[l,u,p]=B(e,c),f=o({},c,{t:l,i18n:u,tReady:p});return n.withRef&&i&&(f.ref=i),t.createElement(r,f)}i.displayName="withI18nextTranslation(".concat(T(r),")"),i.WrappedComponent=r;return n.withRef?t.forwardRef((e,n)=>t.createElement(i,a({},e,{forwardedRef:n}))):i}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/dist/commonjs/Trans.js
CHANGED
|
@@ -44,33 +44,36 @@ function hasValidReactChildren(children) {
|
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
function
|
|
47
|
+
function getAsArray(data) {
|
|
48
|
+
return Array.isArray(data) ? data : [data];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function nodesToString(startingString, children, index, i18nOptions) {
|
|
48
52
|
if (!children) return '';
|
|
49
|
-
|
|
53
|
+
var stringNode = startingString;
|
|
54
|
+
var childrenArray = getAsArray(children);
|
|
50
55
|
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
51
|
-
|
|
52
|
-
// const isElement = React.isValidElement(child);
|
|
53
|
-
// const elementKey = `${index !== 0 ? index + '-' : ''}${i}:${typeof child.type === 'function' ? child.type.name : child.type || 'var'}`;
|
|
56
|
+
childrenArray.forEach(function (child, i) {
|
|
54
57
|
var elementKey = "".concat(i);
|
|
55
58
|
|
|
56
59
|
if (typeof child === 'string') {
|
|
57
|
-
|
|
60
|
+
stringNode = "".concat(stringNode).concat(child);
|
|
58
61
|
} else if (hasChildren(child)) {
|
|
59
62
|
var elementTag = keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 1 && typeof hasChildren(child) === 'string' ? child.type : elementKey;
|
|
60
63
|
|
|
61
64
|
if (child.props && child.props.i18nIsDynamicList) {
|
|
62
65
|
// we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
|
|
63
66
|
// the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
|
|
64
|
-
|
|
67
|
+
stringNode = "".concat(stringNode, "<").concat(elementTag, "></").concat(elementTag, ">");
|
|
65
68
|
} else {
|
|
66
69
|
// regular case mapping the inner children
|
|
67
|
-
|
|
70
|
+
stringNode = "".concat(stringNode, "<").concat(elementTag, ">").concat(nodesToString('', getChildren(child), i + 1, i18nOptions), "</").concat(elementTag, ">");
|
|
68
71
|
}
|
|
69
72
|
} else if (_react["default"].isValidElement(child)) {
|
|
70
73
|
if (keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 0) {
|
|
71
|
-
|
|
74
|
+
stringNode = "".concat(stringNode, "<").concat(child.type, "/>");
|
|
72
75
|
} else {
|
|
73
|
-
|
|
76
|
+
stringNode = "".concat(stringNode, "<").concat(elementKey, "></").concat(elementKey, ">");
|
|
74
77
|
}
|
|
75
78
|
} else if ((0, _typeof2["default"])(child) === 'object') {
|
|
76
79
|
var clone = _objectSpread({}, child);
|
|
@@ -80,9 +83,9 @@ function nodesToString(mem, children, index, i18nOptions) {
|
|
|
80
83
|
var keys = Object.keys(clone);
|
|
81
84
|
|
|
82
85
|
if (format && keys.length === 1) {
|
|
83
|
-
|
|
86
|
+
stringNode = "".concat(stringNode, "{{").concat(keys[0], ", ").concat(format, "}}");
|
|
84
87
|
} else if (keys.length === 1) {
|
|
85
|
-
|
|
88
|
+
stringNode = "".concat(stringNode, "{{").concat(keys[0], "}}");
|
|
86
89
|
} else {
|
|
87
90
|
// not a valid interpolation object (can only contain one value plus format)
|
|
88
91
|
(0, _utils.warn)("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.", child);
|
|
@@ -91,7 +94,7 @@ function nodesToString(mem, children, index, i18nOptions) {
|
|
|
91
94
|
(0, _utils.warn)("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}}.", child);
|
|
92
95
|
}
|
|
93
96
|
});
|
|
94
|
-
return
|
|
97
|
+
return stringNode;
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
@@ -105,22 +108,22 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
105
108
|
var data = {};
|
|
106
109
|
|
|
107
110
|
function getData(childs) {
|
|
108
|
-
|
|
109
|
-
|
|
111
|
+
var childrenArray = getAsArray(childs);
|
|
112
|
+
childrenArray.forEach(function (child) {
|
|
110
113
|
if (typeof child === 'string') return;
|
|
111
114
|
if (hasChildren(child)) getData(getChildren(child));else if ((0, _typeof2["default"])(child) === 'object' && !_react["default"].isValidElement(child)) Object.assign(data, child);
|
|
112
115
|
});
|
|
113
116
|
}
|
|
114
117
|
|
|
115
118
|
getData(children);
|
|
116
|
-
|
|
119
|
+
var interpolatedString = i18n.services.interpolator.interpolate(targetString, _objectSpread({}, data, {}, combinedTOpts), i18n.language); // parse ast from string with additional wrapper tag
|
|
117
120
|
// -> avoids issues in parser removing prepending text nodes
|
|
118
121
|
|
|
119
|
-
var ast = _htmlParseStringify["default"].parse("<0>".concat(
|
|
122
|
+
var ast = _htmlParseStringify["default"].parse("<0>".concat(interpolatedString, "</0>"));
|
|
120
123
|
|
|
121
|
-
function mapAST(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
function mapAST(reactNode, astNode) {
|
|
125
|
+
var reactNodes = getAsArray(reactNode);
|
|
126
|
+
var astNodes = getAsArray(astNode);
|
|
124
127
|
return astNodes.reduce(function (mem, node, i) {
|
|
125
128
|
var translationContent = node.children && node.children[0] && node.children[0].content;
|
|
126
129
|
|
|
@@ -151,7 +154,7 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
151
154
|
mem.push(_react["default"].cloneElement(child, _objectSpread({}, child.props, {
|
|
152
155
|
key: i
|
|
153
156
|
}), _inner));
|
|
154
|
-
} else if (isNaN(node.name)) {
|
|
157
|
+
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
155
158
|
if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
156
159
|
if (node.voidElement) {
|
|
157
160
|
mem.push(_react["default"].createElement(node.name, {
|
|
@@ -256,9 +259,10 @@ function Trans(_ref) {
|
|
|
256
259
|
}
|
|
257
260
|
};
|
|
258
261
|
|
|
259
|
-
var combinedTOpts = _objectSpread({}, tOptions, {
|
|
262
|
+
var combinedTOpts = _objectSpread({}, tOptions, {
|
|
263
|
+
count: count
|
|
264
|
+
}, values, {}, interpolationOverride, {
|
|
260
265
|
defaultValue: defaultValue,
|
|
261
|
-
count: count,
|
|
262
266
|
ns: namespaces
|
|
263
267
|
});
|
|
264
268
|
|
|
@@ -11,8 +11,6 @@ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/sli
|
|
|
11
11
|
|
|
12
12
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
13
13
|
|
|
14
|
-
var _react = _interopRequireDefault(require("react"));
|
|
15
|
-
|
|
16
14
|
var _useTranslation3 = require("./useTranslation");
|
|
17
15
|
|
|
18
16
|
function Translation(props) {
|
package/dist/commonjs/useSSR.js
CHANGED
|
@@ -12,8 +12,9 @@ var _context = require("./context");
|
|
|
12
12
|
function useSSR(initialI18nStore, initialLanguage) {
|
|
13
13
|
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
14
14
|
var i18nFromProps = props.i18n;
|
|
15
|
+
var ReactI18nContext = (0, _react.useContext)(_context.I18nContext);
|
|
15
16
|
|
|
16
|
-
var _ref = (0, _context.getHasUsedI18nextProvider)() ?
|
|
17
|
+
var _ref = (0, _context.getHasUsedI18nextProvider)() ? ReactI18nContext || {} : {},
|
|
17
18
|
i18nFromContext = _ref.i18n;
|
|
18
19
|
|
|
19
20
|
var i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)(); // opt out if is a cloned instance, eg. created by i18next-express-middleware on request
|
|
@@ -7,10 +7,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.withTranslation = withTranslation;
|
|
9
9
|
|
|
10
|
+
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
11
|
+
|
|
10
12
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
13
|
|
|
12
14
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
13
15
|
|
|
16
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
17
|
+
|
|
14
18
|
var _react = _interopRequireDefault(require("react"));
|
|
15
19
|
|
|
16
20
|
var _useTranslation3 = require("./useTranslation");
|
|
@@ -24,21 +28,24 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
24
28
|
function withTranslation(ns) {
|
|
25
29
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
26
30
|
return function Extend(WrappedComponent) {
|
|
27
|
-
function I18nextWithTranslation(
|
|
28
|
-
var
|
|
31
|
+
function I18nextWithTranslation(_ref) {
|
|
32
|
+
var forwardedRef = _ref.forwardedRef,
|
|
33
|
+
rest = (0, _objectWithoutProperties2["default"])(_ref, ["forwardedRef"]);
|
|
34
|
+
|
|
35
|
+
var _useTranslation = (0, _useTranslation3.useTranslation)(ns, rest),
|
|
29
36
|
_useTranslation2 = (0, _slicedToArray2["default"])(_useTranslation, 3),
|
|
30
37
|
t = _useTranslation2[0],
|
|
31
38
|
i18n = _useTranslation2[1],
|
|
32
39
|
ready = _useTranslation2[2];
|
|
33
40
|
|
|
34
|
-
var passDownProps = _objectSpread({},
|
|
41
|
+
var passDownProps = _objectSpread({}, rest, {
|
|
35
42
|
t: t,
|
|
36
43
|
i18n: i18n,
|
|
37
44
|
tReady: ready
|
|
38
45
|
});
|
|
39
46
|
|
|
40
|
-
if (options.withRef &&
|
|
41
|
-
passDownProps.ref =
|
|
47
|
+
if (options.withRef && forwardedRef) {
|
|
48
|
+
passDownProps.ref = forwardedRef;
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
return _react["default"].createElement(WrappedComponent, passDownProps);
|
|
@@ -46,6 +53,13 @@ function withTranslation(ns) {
|
|
|
46
53
|
|
|
47
54
|
I18nextWithTranslation.displayName = "withI18nextTranslation(".concat((0, _utils.getDisplayName)(WrappedComponent), ")");
|
|
48
55
|
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
49
|
-
|
|
56
|
+
|
|
57
|
+
var forwardRef = function forwardRef(props, ref) {
|
|
58
|
+
return _react["default"].createElement(I18nextWithTranslation, (0, _extends2["default"])({}, props, {
|
|
59
|
+
forwardedRef: ref
|
|
60
|
+
}));
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return options.withRef ? _react["default"].forwardRef(forwardRef) : I18nextWithTranslation;
|
|
50
64
|
};
|
|
51
65
|
}
|
package/dist/es/Trans.js
CHANGED
|
@@ -27,33 +27,36 @@ function hasValidReactChildren(children) {
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
function getAsArray(data) {
|
|
31
|
+
return Array.isArray(data) ? data : [data];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function nodesToString(startingString, children, index, i18nOptions) {
|
|
31
35
|
if (!children) return '';
|
|
32
|
-
|
|
36
|
+
var stringNode = startingString;
|
|
37
|
+
var childrenArray = getAsArray(children);
|
|
33
38
|
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
34
|
-
|
|
35
|
-
// const isElement = React.isValidElement(child);
|
|
36
|
-
// const elementKey = `${index !== 0 ? index + '-' : ''}${i}:${typeof child.type === 'function' ? child.type.name : child.type || 'var'}`;
|
|
39
|
+
childrenArray.forEach(function (child, i) {
|
|
37
40
|
var elementKey = "".concat(i);
|
|
38
41
|
|
|
39
42
|
if (typeof child === 'string') {
|
|
40
|
-
|
|
43
|
+
stringNode = "".concat(stringNode).concat(child);
|
|
41
44
|
} else if (hasChildren(child)) {
|
|
42
45
|
var elementTag = keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 1 && typeof hasChildren(child) === 'string' ? child.type : elementKey;
|
|
43
46
|
|
|
44
47
|
if (child.props && child.props.i18nIsDynamicList) {
|
|
45
48
|
// we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
|
|
46
49
|
// the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
|
|
47
|
-
|
|
50
|
+
stringNode = "".concat(stringNode, "<").concat(elementTag, "></").concat(elementTag, ">");
|
|
48
51
|
} else {
|
|
49
52
|
// regular case mapping the inner children
|
|
50
|
-
|
|
53
|
+
stringNode = "".concat(stringNode, "<").concat(elementTag, ">").concat(nodesToString('', getChildren(child), i + 1, i18nOptions), "</").concat(elementTag, ">");
|
|
51
54
|
}
|
|
52
55
|
} else if (React.isValidElement(child)) {
|
|
53
56
|
if (keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 0) {
|
|
54
|
-
|
|
57
|
+
stringNode = "".concat(stringNode, "<").concat(child.type, "/>");
|
|
55
58
|
} else {
|
|
56
|
-
|
|
59
|
+
stringNode = "".concat(stringNode, "<").concat(elementKey, "></").concat(elementKey, ">");
|
|
57
60
|
}
|
|
58
61
|
} else if (_typeof(child) === 'object') {
|
|
59
62
|
var clone = _objectSpread({}, child);
|
|
@@ -63,9 +66,9 @@ export function nodesToString(mem, children, index, i18nOptions) {
|
|
|
63
66
|
var keys = Object.keys(clone);
|
|
64
67
|
|
|
65
68
|
if (format && keys.length === 1) {
|
|
66
|
-
|
|
69
|
+
stringNode = "".concat(stringNode, "{{").concat(keys[0], ", ").concat(format, "}}");
|
|
67
70
|
} else if (keys.length === 1) {
|
|
68
|
-
|
|
71
|
+
stringNode = "".concat(stringNode, "{{").concat(keys[0], "}}");
|
|
69
72
|
} else {
|
|
70
73
|
// not a valid interpolation object (can only contain one value plus format)
|
|
71
74
|
warn("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.", child);
|
|
@@ -74,7 +77,7 @@ export function nodesToString(mem, children, index, i18nOptions) {
|
|
|
74
77
|
warn("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}}.", child);
|
|
75
78
|
}
|
|
76
79
|
});
|
|
77
|
-
return
|
|
80
|
+
return stringNode;
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
@@ -88,22 +91,22 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
88
91
|
var data = {};
|
|
89
92
|
|
|
90
93
|
function getData(childs) {
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
var childrenArray = getAsArray(childs);
|
|
95
|
+
childrenArray.forEach(function (child) {
|
|
93
96
|
if (typeof child === 'string') return;
|
|
94
97
|
if (hasChildren(child)) getData(getChildren(child));else if (_typeof(child) === 'object' && !React.isValidElement(child)) Object.assign(data, child);
|
|
95
98
|
});
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
getData(children);
|
|
99
|
-
|
|
102
|
+
var interpolatedString = i18n.services.interpolator.interpolate(targetString, _objectSpread({}, data, {}, combinedTOpts), i18n.language); // parse ast from string with additional wrapper tag
|
|
100
103
|
// -> avoids issues in parser removing prepending text nodes
|
|
101
104
|
|
|
102
|
-
var ast = HTML.parse("<0>".concat(
|
|
105
|
+
var ast = HTML.parse("<0>".concat(interpolatedString, "</0>"));
|
|
103
106
|
|
|
104
|
-
function mapAST(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
function mapAST(reactNode, astNode) {
|
|
108
|
+
var reactNodes = getAsArray(reactNode);
|
|
109
|
+
var astNodes = getAsArray(astNode);
|
|
107
110
|
return astNodes.reduce(function (mem, node, i) {
|
|
108
111
|
var translationContent = node.children && node.children[0] && node.children[0].content;
|
|
109
112
|
|
|
@@ -133,7 +136,7 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
133
136
|
mem.push(React.cloneElement(child, _objectSpread({}, child.props, {
|
|
134
137
|
key: i
|
|
135
138
|
}), _inner));
|
|
136
|
-
} else if (isNaN(node.name)) {
|
|
139
|
+
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
137
140
|
if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
138
141
|
if (node.voidElement) {
|
|
139
142
|
mem.push(React.createElement(node.name, {
|
|
@@ -238,9 +241,10 @@ export function Trans(_ref) {
|
|
|
238
241
|
}
|
|
239
242
|
};
|
|
240
243
|
|
|
241
|
-
var combinedTOpts = _objectSpread({}, tOptions, {
|
|
244
|
+
var combinedTOpts = _objectSpread({}, tOptions, {
|
|
245
|
+
count: count
|
|
246
|
+
}, values, {}, interpolationOverride, {
|
|
242
247
|
defaultValue: defaultValue,
|
|
243
|
-
count: count,
|
|
244
248
|
ns: namespaces
|
|
245
249
|
});
|
|
246
250
|
|
package/dist/es/Translation.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
|
-
import React from 'react';
|
|
4
3
|
import { useTranslation } from './useTranslation';
|
|
5
4
|
export function Translation(props) {
|
|
6
5
|
var ns = props.ns,
|
package/dist/es/useSSR.js
CHANGED
|
@@ -3,8 +3,9 @@ import { getI18n, getHasUsedI18nextProvider, I18nContext } from './context';
|
|
|
3
3
|
export function useSSR(initialI18nStore, initialLanguage) {
|
|
4
4
|
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
5
5
|
var i18nFromProps = props.i18n;
|
|
6
|
+
var ReactI18nContext = useContext(I18nContext);
|
|
6
7
|
|
|
7
|
-
var _ref = getHasUsedI18nextProvider() ?
|
|
8
|
+
var _ref = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {},
|
|
8
9
|
i18nFromContext = _ref.i18n;
|
|
9
10
|
|
|
10
11
|
var i18n = i18nFromProps || i18nFromContext || getI18n(); // opt out if is a cloned instance, eg. created by i18next-express-middleware on request
|