react-i18next 11.3.3 → 11.5.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/README.md +4 -4
- package/dist/amd/react-i18next.js +94 -67
- package/dist/amd/react-i18next.min.js +1 -1
- package/dist/commonjs/I18nextProvider.js +2 -5
- package/dist/commonjs/Trans.js +61 -39
- package/dist/commonjs/context.js +2 -15
- package/dist/commonjs/useSSR.js +2 -3
- package/dist/commonjs/useTranslation.js +9 -9
- package/dist/commonjs/withSSR.js +1 -1
- package/dist/commonjs/withTranslation.js +1 -1
- package/dist/es/I18nextProvider.js +3 -4
- package/dist/es/Trans.js +61 -39
- package/dist/es/context.js +2 -11
- package/dist/es/useSSR.js +3 -4
- package/dist/es/useTranslation.js +11 -11
- package/dist/es/withSSR.js +1 -1
- package/dist/es/withTranslation.js +1 -1
- package/dist/umd/react-i18next.js +94 -67
- package/dist/umd/react-i18next.min.js +1 -1
- package/package.json +5 -3
- package/react-i18next.js +94 -67
- package/react-i18next.min.js +1 -1
- package/src/I18nextProvider.js +3 -5
- package/src/Trans.js +66 -60
- package/src/context.js +0 -9
- package/src/useSSR.js +3 -4
- package/src/useTranslation.js +9 -17
- package/.DS_Store +0 -0
- package/.vscode/settings.json +0 -2
- package/src/.DS_Store +0 -0
package/react-i18next.js
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
var React__default = 'default' in React ? React['default'] : React;
|
|
8
8
|
|
|
9
9
|
function _typeof(obj) {
|
|
10
|
+
"@babel/helpers - typeof";
|
|
11
|
+
|
|
10
12
|
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
|
11
13
|
_typeof = function (obj) {
|
|
12
14
|
return typeof obj;
|
|
@@ -76,13 +78,13 @@
|
|
|
76
78
|
var source = arguments[i] != null ? arguments[i] : {};
|
|
77
79
|
|
|
78
80
|
if (i % 2) {
|
|
79
|
-
ownKeys(source, true).forEach(function (key) {
|
|
81
|
+
ownKeys(Object(source), true).forEach(function (key) {
|
|
80
82
|
_defineProperty(target, key, source[key]);
|
|
81
83
|
});
|
|
82
84
|
} else if (Object.getOwnPropertyDescriptors) {
|
|
83
85
|
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
84
86
|
} else {
|
|
85
|
-
ownKeys(source).forEach(function (key) {
|
|
87
|
+
ownKeys(Object(source)).forEach(function (key) {
|
|
86
88
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
87
89
|
});
|
|
88
90
|
}
|
|
@@ -128,7 +130,7 @@
|
|
|
128
130
|
}
|
|
129
131
|
|
|
130
132
|
function _slicedToArray(arr, i) {
|
|
131
|
-
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
|
|
133
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
function _arrayWithHoles(arr) {
|
|
@@ -136,10 +138,7 @@
|
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
function _iterableToArrayLimit(arr, i) {
|
|
139
|
-
if (!(Symbol.iterator in Object(arr)
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
|
|
141
|
+
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
|
|
143
142
|
var _arr = [];
|
|
144
143
|
var _n = true;
|
|
145
144
|
var _d = false;
|
|
@@ -165,8 +164,25 @@
|
|
|
165
164
|
return _arr;
|
|
166
165
|
}
|
|
167
166
|
|
|
167
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
168
|
+
if (!o) return;
|
|
169
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
170
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
171
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
172
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
173
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function _arrayLikeToArray(arr, len) {
|
|
177
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
178
|
+
|
|
179
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
180
|
+
|
|
181
|
+
return arr2;
|
|
182
|
+
}
|
|
183
|
+
|
|
168
184
|
function _nonIterableRest() {
|
|
169
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
|
185
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
170
186
|
}
|
|
171
187
|
|
|
172
188
|
/**
|
|
@@ -396,14 +412,7 @@
|
|
|
396
412
|
useSuspense: true
|
|
397
413
|
};
|
|
398
414
|
var i18nInstance;
|
|
399
|
-
var hasUsedI18nextProvider;
|
|
400
415
|
var I18nContext = React__default.createContext();
|
|
401
|
-
function usedI18nextProvider(used) {
|
|
402
|
-
hasUsedI18nextProvider = used;
|
|
403
|
-
}
|
|
404
|
-
function getHasUsedI18nextProvider() {
|
|
405
|
-
return hasUsedI18nextProvider;
|
|
406
|
-
}
|
|
407
416
|
function setDefaults() {
|
|
408
417
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
409
418
|
defaultOptions = _objectSpread2({}, defaultOptions, {}, options);
|
|
@@ -411,9 +420,7 @@
|
|
|
411
420
|
function getDefaults() {
|
|
412
421
|
return defaultOptions;
|
|
413
422
|
}
|
|
414
|
-
var ReportNamespaces =
|
|
415
|
-
/*#__PURE__*/
|
|
416
|
-
function () {
|
|
423
|
+
var ReportNamespaces = /*#__PURE__*/function () {
|
|
417
424
|
function ReportNamespaces() {
|
|
418
425
|
_classCallCheck(this, ReportNamespaces);
|
|
419
426
|
|
|
@@ -596,44 +603,63 @@
|
|
|
596
603
|
return Array.isArray(data) ? data : [data];
|
|
597
604
|
}
|
|
598
605
|
|
|
599
|
-
function
|
|
606
|
+
function mergeProps(source, target) {
|
|
607
|
+
var newTarget = _objectSpread2({}, target); // overwrite source.props when target.props already set
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
newTarget.props = Object.assign(source.props, target.props);
|
|
611
|
+
return newTarget;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function nodesToString(children, i18nOptions) {
|
|
600
615
|
if (!children) return '';
|
|
601
|
-
var stringNode =
|
|
616
|
+
var stringNode = ''; // do not use `React.Children.toArray`, will fail at object children
|
|
617
|
+
|
|
602
618
|
var childrenArray = getAsArray(children);
|
|
603
|
-
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
604
|
-
childrenArray.forEach(function (child, i) {
|
|
605
|
-
var elementKey = "".concat(i);
|
|
619
|
+
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || []; // e.g. lorem <br/> ipsum {{ messageCount, format }} dolor <strong>bold</strong> amet
|
|
606
620
|
|
|
621
|
+
childrenArray.forEach(function (child, childIndex) {
|
|
607
622
|
if (typeof child === 'string') {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
if (child.props && child.props.i18nIsDynamicList) {
|
|
613
|
-
// we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
|
|
614
|
-
// the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
|
|
615
|
-
stringNode = "".concat(stringNode, "<").concat(elementTag, "></").concat(elementTag, ">");
|
|
616
|
-
} else {
|
|
617
|
-
// regular case mapping the inner children
|
|
618
|
-
stringNode = "".concat(stringNode, "<").concat(elementTag, ">").concat(nodesToString('', getChildren(child), i + 1, i18nOptions), "</").concat(elementTag, ">");
|
|
619
|
-
}
|
|
623
|
+
// actual e.g. lorem
|
|
624
|
+
// expected e.g. lorem
|
|
625
|
+
stringNode += "".concat(child);
|
|
620
626
|
} else if (React__default.isValidElement(child)) {
|
|
621
|
-
|
|
622
|
-
|
|
627
|
+
var childPropsCount = Object.keys(child.props).length;
|
|
628
|
+
var shouldKeepChild = keepArray.indexOf(child.type) > -1;
|
|
629
|
+
var childChildren = child.props.children;
|
|
630
|
+
|
|
631
|
+
if (!childChildren && shouldKeepChild && childPropsCount === 0) {
|
|
632
|
+
// actual e.g. lorem <br/> ipsum
|
|
633
|
+
// expected e.g. lorem <br/> ipsum
|
|
634
|
+
stringNode += "<".concat(child.type, "/>");
|
|
635
|
+
} else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
|
|
636
|
+
// actual e.g. lorem <hr className="test" /> ipsum
|
|
637
|
+
// expected e.g. lorem <0></0> ipsum
|
|
638
|
+
stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
|
|
639
|
+
} else if (child.props.i18nIsDynamicList) {
|
|
640
|
+
// we got a dynamic list like
|
|
641
|
+
// e.g. <ul i18nIsDynamicList>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>
|
|
642
|
+
// expected e.g. "<0></0>", not e.g. "<0><0>a</0><1>b</1></0>"
|
|
643
|
+
stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
|
|
644
|
+
} else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
|
|
645
|
+
// actual e.g. dolor <strong>bold</strong> amet
|
|
646
|
+
// expected e.g. dolor <strong>bold</strong> amet
|
|
647
|
+
stringNode += "<".concat(child.type, ">").concat(childChildren, "</").concat(child.type, ">");
|
|
623
648
|
} else {
|
|
624
|
-
|
|
649
|
+
// regular case mapping the inner children
|
|
650
|
+
var content = nodesToString(childChildren, i18nOptions);
|
|
651
|
+
stringNode += "<".concat(childIndex, ">").concat(content, "</").concat(childIndex, ">");
|
|
625
652
|
}
|
|
626
653
|
} else if (_typeof(child) === 'object') {
|
|
627
|
-
|
|
654
|
+
// e.g. lorem {{ value, format }} ipsum
|
|
655
|
+
var format = child.format,
|
|
656
|
+
clone = _objectWithoutProperties(child, ["format"]);
|
|
628
657
|
|
|
629
|
-
var format = clone.format;
|
|
630
|
-
delete clone.format;
|
|
631
658
|
var keys = Object.keys(clone);
|
|
632
659
|
|
|
633
|
-
if (
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
stringNode = "".concat(stringNode, "{{").concat(keys[0], "}}");
|
|
660
|
+
if (keys.length === 1) {
|
|
661
|
+
var value = format ? "".concat(keys[0], ", ").concat(format) : keys[0];
|
|
662
|
+
stringNode += "{{".concat(value, "}}");
|
|
637
663
|
} else {
|
|
638
664
|
// not a valid interpolation object (can only contain one value plus format)
|
|
639
665
|
warn("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.", child);
|
|
@@ -676,7 +702,10 @@
|
|
|
676
702
|
var translationContent = node.children && node.children[0] && node.children[0].content;
|
|
677
703
|
|
|
678
704
|
if (node.type === 'tag') {
|
|
679
|
-
var
|
|
705
|
+
var tmp = reactNodes[parseInt(node.name, 10)] || {};
|
|
706
|
+
var child = Object.keys(node.attrs).length !== 0 ? mergeProps({
|
|
707
|
+
props: node.attrs
|
|
708
|
+
}, tmp) : tmp;
|
|
680
709
|
var isElement = React__default.isValidElement(child);
|
|
681
710
|
|
|
682
711
|
if (typeof child === 'string') {
|
|
@@ -775,9 +804,7 @@
|
|
|
775
804
|
tFromProps = _ref.t,
|
|
776
805
|
additionalProps = _objectWithoutProperties(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
|
|
777
806
|
|
|
778
|
-
var
|
|
779
|
-
|
|
780
|
-
var _ref2 = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {},
|
|
807
|
+
var _ref2 = React.useContext(I18nContext) || {},
|
|
781
808
|
i18nFromContext = _ref2.i18n,
|
|
782
809
|
defaultNSFromContext = _ref2.defaultNS;
|
|
783
810
|
|
|
@@ -792,13 +819,12 @@
|
|
|
792
819
|
return k;
|
|
793
820
|
};
|
|
794
821
|
|
|
795
|
-
var reactI18nextOptions = _objectSpread2({}, getDefaults(), {}, i18n.options && i18n.options.react);
|
|
822
|
+
var reactI18nextOptions = _objectSpread2({}, getDefaults(), {}, i18n.options && i18n.options.react); // prepare having a namespace
|
|
796
823
|
|
|
797
|
-
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
|
|
798
824
|
|
|
799
825
|
var namespaces = ns || t.ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
|
|
800
826
|
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
801
|
-
var defaultValue = defaults || nodesToString(
|
|
827
|
+
var defaultValue = defaults || nodesToString(children, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue || i18nKey;
|
|
802
828
|
var hashTransKey = reactI18nextOptions.hashTransKey;
|
|
803
829
|
var key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
804
830
|
var interpolationOverride = values ? {} : {
|
|
@@ -816,17 +842,19 @@
|
|
|
816
842
|
});
|
|
817
843
|
|
|
818
844
|
var translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
819
|
-
|
|
820
|
-
|
|
845
|
+
var content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts); // allows user to pass `null` to `parent`
|
|
846
|
+
// and override `defaultTransParent` if is present
|
|
847
|
+
|
|
848
|
+
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
849
|
+
return useAsParent ? React__default.createElement(useAsParent, additionalProps, content) : content;
|
|
821
850
|
}
|
|
822
851
|
|
|
823
852
|
function useTranslation(ns) {
|
|
824
853
|
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
825
854
|
// assert we have the needed i18nInstance
|
|
826
855
|
var i18nFromProps = props.i18n;
|
|
827
|
-
var ReactI18nContext = React.useContext(I18nContext);
|
|
828
856
|
|
|
829
|
-
var _ref =
|
|
857
|
+
var _ref = React.useContext(I18nContext) || {},
|
|
830
858
|
i18nFromContext = _ref.i18n,
|
|
831
859
|
defaultNSFromContext = _ref.defaultNS;
|
|
832
860
|
|
|
@@ -872,20 +900,21 @@
|
|
|
872
900
|
setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
|
|
873
901
|
|
|
874
902
|
|
|
903
|
+
var isMounted = React.useRef(true);
|
|
875
904
|
React.useEffect(function () {
|
|
876
|
-
var isMounted = true;
|
|
877
905
|
var bindI18n = i18nOptions.bindI18n,
|
|
878
|
-
bindI18nStore = i18nOptions.bindI18nStore;
|
|
906
|
+
bindI18nStore = i18nOptions.bindI18nStore;
|
|
907
|
+
isMounted.current = true; // if not ready and not using suspense load the namespaces
|
|
879
908
|
// in side effect and do not call resetT if unmounted
|
|
880
909
|
|
|
881
910
|
if (!ready && !useSuspense) {
|
|
882
911
|
loadNamespaces(i18n, namespaces, function () {
|
|
883
|
-
if (isMounted) setT(getT());
|
|
912
|
+
if (isMounted.current) setT(getT());
|
|
884
913
|
});
|
|
885
914
|
}
|
|
886
915
|
|
|
887
916
|
function boundReset() {
|
|
888
|
-
if (isMounted) setT(getT());
|
|
917
|
+
if (isMounted.current) setT(getT());
|
|
889
918
|
} // bind events to trigger change, like languageChanged
|
|
890
919
|
|
|
891
920
|
|
|
@@ -893,7 +922,7 @@
|
|
|
893
922
|
if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset); // unbinding on unmount
|
|
894
923
|
|
|
895
924
|
return function () {
|
|
896
|
-
isMounted = false;
|
|
925
|
+
isMounted.current = false;
|
|
897
926
|
if (bindI18n && i18n) bindI18n.split(' ').forEach(function (e) {
|
|
898
927
|
return i18n.off(e, boundReset);
|
|
899
928
|
});
|
|
@@ -914,7 +943,7 @@
|
|
|
914
943
|
|
|
915
944
|
throw new Promise(function (resolve) {
|
|
916
945
|
loadNamespaces(i18n, namespaces, function () {
|
|
917
|
-
setT(getT());
|
|
946
|
+
if (isMounted.current) setT(getT());
|
|
918
947
|
resolve();
|
|
919
948
|
});
|
|
920
949
|
});
|
|
@@ -982,8 +1011,7 @@
|
|
|
982
1011
|
var i18n = _ref.i18n,
|
|
983
1012
|
defaultNS = _ref.defaultNS,
|
|
984
1013
|
children = _ref.children;
|
|
985
|
-
|
|
986
|
-
return React__default.createElement(I18nContext.Provider, {
|
|
1014
|
+
return React.createElement(I18nContext.Provider, {
|
|
987
1015
|
value: {
|
|
988
1016
|
i18n: i18n,
|
|
989
1017
|
defaultNS: defaultNS
|
|
@@ -994,12 +1022,11 @@
|
|
|
994
1022
|
function useSSR(initialI18nStore, initialLanguage) {
|
|
995
1023
|
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
996
1024
|
var i18nFromProps = props.i18n;
|
|
997
|
-
var ReactI18nContext = React.useContext(I18nContext);
|
|
998
1025
|
|
|
999
|
-
var _ref =
|
|
1026
|
+
var _ref = React.useContext(I18nContext) || {},
|
|
1000
1027
|
i18nFromContext = _ref.i18n;
|
|
1001
1028
|
|
|
1002
|
-
var i18n = i18nFromProps || i18nFromContext || getI18n(); // opt out if is a cloned instance, eg. created by i18next-
|
|
1029
|
+
var i18n = i18nFromProps || i18nFromContext || getI18n(); // opt out if is a cloned instance, eg. created by i18next-http-middleware on request
|
|
1003
1030
|
// -> do not set initial stuff on server side
|
|
1004
1031
|
|
|
1005
1032
|
if (i18n.options && i18n.options.isClone) return; // nextjs / SSR: getting data from next.js or other ssr stack
|
package/react-i18next.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e=e||self).ReactI18next={},e.React)}(this,(function(e,n){"use strict";var t="default"in n?n.default:n;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function a(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function o(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 c(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?o(t,!0).forEach((function(n){i(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):o(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}function u(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,n){if(!(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)))return;var t=[],r=!0,a=!1,i=void 0;try{for(var o,c=e[Symbol.iterator]();!(r=(o=c.next()).done)&&(t.push(o.value),!n||t.length!==n);r=!0);}catch(e){a=!0,i=e}finally{try{r||null==c.return||c.return()}finally{if(a)throw i}}return t}(e,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var l={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},f=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,p=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,d=Object.create?Object.create(null):{};function g(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 m(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(m,"")+"</"+n.name+">"}}var h,y,v={parse:function(e,n){n||(n={}),n.components||(n.components=d);var t,r=[],a=-1,i=[],o={},c=!1;return e.replace(p,(function(s,u){if(c){if(s!=="</"+t.name+">")return;c=!1}var p,d="/"!==s.charAt(1),m=0===s.indexOf("\x3c!--"),h=u+s.length,y=e.charAt(h);d&&!m&&(a++,"tag"===(t=function(e){var n,t=0,r=!0,a={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(f,(function(i){if("="===i)return r=!0,void t++;r?0===t?((l[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}(s)).type&&n.components[t.name]&&(t.type="component",c=!0),t.voidElement||c||!y||"<"===y||g(t.children,e,a,h,n.ignoreWhitespace),o[t.tagName]=t,0===a&&r.push(t),(p=i[a-1])&&p.children.push(t),i[a]=t),(m||!d||t.voidElement)&&(m||a--,!c&&"<"!==y&&y&&g(p=-1===a?r:i[a].children,e,a,h,n.ignoreWhitespace))})),!r.length&&e.length&&g(r,e,0,0,n.ignoreWhitespace),r},stringify:function(e){return e.reduce((function(e,n){return e+m("",n)}),"")}},b={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},O=t.createContext();function w(){return y}function j(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};b=c({},b,{},e)}function S(){return b}var E=function(){function e(){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e),this.usedNamespaces={}}var n,t,r;return n=e,(t=[{key:"addUsedNamespaces",value:function(e){var n=this;e.forEach((function(e){n.usedNamespaces[e]||(n.usedNamespaces[e]=!0)}))}},{key:"getUsedNamespaces",value:function(){return Object.keys(this.usedNamespaces)}}])&&a(n.prototype,t),r&&a(n,r),e}();function x(e){h=e}function N(){return h}var k={type:"3rdParty",init:function(e){j(e.options.react),x(e)}};function I(e){return function(n){return new Promise((function(t){var r=P();e.getInitialProps?e.getInitialProps(n).then((function(e){t(c({},e,{},r))})):t(r)}))}}function P(){var e=N(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach((function(t){r[t]={},n.forEach((function(n){r[t][n]=e.getResourceBundle(t,n)||{}}))})),t.initialI18nStore=r,t.initialLanguage=e.language,t}function R(){if(console&&console.warn){for(var e,n=arguments.length,t=new Array(n),r=0;r<n;r++)t[r]=arguments[r];"string"==typeof t[0]&&(t[0]="react-i18next:: ".concat(t[0])),(e=console).warn.apply(e,t)}}var C={};function T(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&C[n[0]]||("string"==typeof n[0]&&(C[n[0]]=new Date),R.apply(void 0,n))}function A(e,n,t){e.loadNamespaces(n,(function(){if(e.isInitialized)t();else{e.on("initialized",(function n(){setTimeout((function(){e.off("initialized",n)}),0),t()}))}}))}function L(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 D(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function B(e){return Array.isArray(e)?e:[e]}function V(e,n,a,i,o){if(""===n)return[];var s=i.transKeepBasicHtmlNodesFor||[],u=n&&new RegExp(s.join("|")).test(n);if(!e&&!u)return[n];var l={};!function e(n){B(n).forEach((function(n){"string"!=typeof n&&(z(n)?e(D(n)):"object"!==r(n)||t.isValidElement(n)||Object.assign(l,n))}))}(e);var f=a.services.interpolator.interpolate(n,c({},l,{},o),a.language);var p=function e(n,a){var o=B(n);return B(a).reduce((function(n,a,l){var f=a.children&&a.children[0]&&a.children[0].content;if("tag"===a.type){var p=o[parseInt(a.name,10)]||{},d=t.isValidElement(p);if("string"==typeof p)n.push(p);else if(z(p)){var g=D(p),m=e(g,a.children),h=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every((function(e){return t.isValidElement(e)}))}(g)&&0===m.length?g:m;p.dummy&&(p.children=h),n.push(t.cloneElement(p,c({},p.props,{key:l}),h))}else if(u&&"object"===r(p)&&p.dummy&&!d){var y=e(o,a.children);n.push(t.cloneElement(p,c({},p.props,{key:l}),y))}else if(Number.isNaN(parseFloat(a.name)))if(i.transSupportBasicHtmlNodes&&s.indexOf(a.name)>-1)if(a.voidElement)n.push(t.createElement(a.name,{key:"".concat(a.name,"-").concat(l)}));else{var v=e(o,a.children);n.push(t.createElement(a.name,{key:"".concat(a.name,"-").concat(l)},v))}else if(a.voidElement)n.push("<".concat(a.name," />"));else{var b=e(o,a.children);n.push("<".concat(a.name,">").concat(b,"</").concat(a.name,">"))}else if("object"!==r(p)||d)1===a.children.length&&f?n.push(t.cloneElement(p,c({},p.props,{key:l}),f)):n.push(t.cloneElement(p,c({},p.props,{key:l})));else{var O=a.children[0]?f:null;O&&n.push(O)}}else"text"===a.type&&n.push(a.content);return n}),[])}([{dummy:!0,children:e}],v.parse("<0>".concat(f,"</0>")));return D(p[0])}function K(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=t.i18n,a=n.useContext(O),i=w()&&a||{},o=i.i18n,s=i.defaultNS,l=r||o||N();if(l&&!l.reportNamespaces&&(l.reportNamespaces=new E),!l){T("You will need pass in an i18next instance by using initReactI18next");var f=function(e){return Array.isArray(e)?e[e.length-1]:e},p=[f,{},!1];return p.t=f,p.i18n={},p.ready=!1,p}var d=c({},S(),{},l.options.react,{},t),g=d.useSuspense,m=e||s||l.options&&l.options.defaultNS;m="string"==typeof m?[m]:m||["translation"],l.reportNamespaces.addUsedNamespaces&&l.reportNamespaces.addUsedNamespaces(m);var h=(l.isInitialized||l.initializedStoreOnce)&&m.every((function(e){return function(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!n.languages||!n.languages.length)return T("i18n.languages were undefined or empty",n.languages),!0;var r=n.languages[0],a=!!n.options&&n.options.fallbackLng,i=n.languages[n.languages.length-1];if("cimode"===r.toLowerCase())return!0;var o=function(e,t){var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!o(n.isLanguageChangingTo,e))&&(!!n.hasResourceBundle(r,e)||(!n.services.backendConnector.backend||!(!o(r,e)||a&&!o(i,e))))}(e,l,d)}));function y(){return{t:l.getFixedT(null,"fallback"===d.nsMode?m:m[0])}}var v=u(n.useState(y()),2),b=v[0],j=v[1];n.useEffect((function(){var e=!0,n=d.bindI18n,t=d.bindI18nStore;function r(){e&&j(y())}return h||g||A(l,m,(function(){e&&j(y())})),n&&l&&l.on(n,r),t&&l&&l.store.on(t,r),function(){e=!1,n&&l&&n.split(" ").forEach((function(e){return l.off(e,r)})),t&&l&&t.split(" ").forEach((function(e){return l.store.off(e,r)}))}}),[m.join()]);var x=[b.t,l,h];if(x.t=b.t,x.i18n=l,x.ready=h,h)return x;if(!h&&!g)return x;throw new Promise((function(e){A(l,m,(function(){j(y()),e()}))}))}function U(e,t){var r=(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}).i18n,a=n.useContext(O),i=(w()&&a||{}).i18n,o=r||i||N();o.options&&o.options.isClone||(e&&!o.initializedStoreOnce&&(o.services.resourceStore.data=e,o.options.ns=Object.values(e).reduce((function(e,n){return Object.keys(n).forEach((function(n){e.indexOf(n)<0&&e.push(n)})),e}),o.options.ns),o.initializedStoreOnce=!0,o.isInitialized=!0),t&&!o.initializedLanguageOnce&&(o.changeLanguage(t),o.initializedLanguageOnce=!0))}e.I18nContext=O,e.I18nextProvider=function(e){var n=e.i18n,r=e.defaultNS,a=e.children;return y=!0,t.createElement(O.Provider,{value:{i18n:n,defaultNS:r}},a)},e.Trans=function(e){var a=e.children,i=e.count,o=e.parent,u=e.i18nKey,l=e.tOptions,f=e.values,p=e.defaults,d=e.components,g=e.ns,m=e.i18n,h=e.t,y=s(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),v=n.useContext(O),b=w()&&v||{},j=b.i18n,E=b.defaultNS,x=m||j||N();if(!x)return T("You will need pass in an i18next instance by using i18nextReactModule"),a;var k=h||x.t.bind(x)||function(e){return e},I=c({},S(),{},x.options&&x.options.react),P=void 0!==o?o:I.defaultTransParent,C=g||k.ns||E||x.options&&x.options.defaultNS;C="string"==typeof C?[C]:C||["translation"];var A=p||function e(n,a,i,o){if(!a)return"";var s=n,u=B(a),l=o.transKeepBasicHtmlNodesFor||[];return u.forEach((function(n,a){var i="".concat(a);if("string"==typeof n)s="".concat(s).concat(n);else if(z(n)){var u=l.indexOf(n.type)>-1&&1===Object.keys(n.props).length&&"string"==typeof z(n)?n.type:i;s=n.props&&n.props.i18nIsDynamicList?"".concat(s,"<").concat(u,"></").concat(u,">"):"".concat(s,"<").concat(u,">").concat(e("",D(n),a+1,o),"</").concat(u,">")}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(i,"></").concat(i,">");else if("object"===r(n)){var f=c({},n),p=f.format;delete f.format;var d=Object.keys(f);p&&1===d.length?s="".concat(s,"{{").concat(d[0],", ").concat(p,"}}"):1===d.length?s="".concat(s,"{{").concat(d[0],"}}"):R("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",n)}else R("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}("",a,0,I)||I.transEmptyNodeValue,L=I.hashTransKey,K=u||(L?L(A):A),U=c({},l,{count:i},f,{},f?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:A,ns:C}),F=K?k(K,U):A;return P?t.createElement(P,y,V(d||a,F,x,I,U)):V(d||a,F,x,I,U)},e.Translation=function(e){var n=e.ns,t=e.children,r=u(K(n,s(e,["ns","children"])),3),a=r[0],i=r[1],o=r[2];return t(a,{i18n:i,lng:i.language},o)},e.composeInitialProps=I,e.getDefaults=S,e.getI18n=N,e.getInitialProps=P,e.initReactI18next=k,e.setDefaults=j,e.setI18n=x,e.useSSR=U,e.useTranslation=K,e.withSSR=function(){return function(e){function n(n){var r=n.initialI18nStore,a=n.initialLanguage,i=s(n,["initialI18nStore","initialLanguage"]);return U(r,a),t.createElement(e,c({},i))}return n.getInitialProps=I(e),n.displayName="withI18nextSSR(".concat(L(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){var i=a.forwardedRef,o=s(a,["forwardedRef"]),l=u(K(e,o),3),f=c({},o,{t:l[0],i18n:l[1],tReady:l[2]});return n.withRef&&i?f.ref=i:!n.withRef&&i&&(f.forwardedRef=i),t.createElement(r,f)}a.displayName="withI18nextTranslation(".concat(L(r),")"),a.WrappedComponent=r;return n.withRef?t.forwardRef((function(e,n){return t.createElement(a,Object.assign({},e,{forwardedRef:n}))})):a}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e=e||self).ReactI18next={},e.React)}(this,(function(e,n){"use strict";var t="default"in n?n.default:n;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function i(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function o(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 c(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?o(Object(t),!0).forEach((function(n){a(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):o(Object(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,i=function(e,n){if(null==e)return{};var t,r,i={},a=Object.keys(e);for(r=0;r<a.length;r++)t=a[r],n.indexOf(t)>=0||(i[t]=e[t]);return i}(e,n);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)t=a[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(i[t]=e[t])}return i}function u(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,n){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var t=[],r=!0,i=!1,a=void 0;try{for(var o,c=e[Symbol.iterator]();!(r=(o=c.next()).done)&&(t.push(o.value),!n||t.length!==n);r=!0);}catch(e){i=!0,a=e}finally{try{r||null==c.return||c.return()}finally{if(i)throw a}}return t}(e,n)||function(e,n){if(!e)return;if("string"==typeof e)return l(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(t);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return l(e,n)}(e,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}var f={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},p=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,d=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,g=Object.create?Object.create(null):{};function m(e,n,t,r,i){var a=n.indexOf("<",r),o=n.slice(r,-1===a?void 0:a);/^\s*$/.test(o)&&(o=" "),(!i&&a>-1&&t+e.length>=0||" "!==o)&&e.push({type:"text",content:o})}var y,h=function(e,n){n||(n={}),n.components||(n.components=g);var t,r=[],i=-1,a=[],o={},c=!1;return e.replace(d,(function(s,u){if(c){if(s!=="</"+t.name+">")return;c=!1}var l,d="/"!==s.charAt(1),g=0===s.indexOf("\x3c!--"),y=u+s.length,h=e.charAt(y);d&&!g&&(i++,"tag"===(t=function(e){var n,t=0,r=!0,i={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(p,(function(a){if("="===a)return r=!0,void t++;r?0===t?((f[a]||"/"===e.charAt(e.length-2))&&(i.voidElement=!0),i.name=a):(i.attrs[n]=a.replace(/^['"]|['"]$/g,""),n=void 0):(n&&(i.attrs[n]=n),n=a),t++,r=!1})),i}(s)).type&&n.components[t.name]&&(t.type="component",c=!0),t.voidElement||c||!h||"<"===h||m(t.children,e,i,y,n.ignoreWhitespace),o[t.tagName]=t,0===i&&r.push(t),(l=a[i-1])&&l.children.push(t),a[i]=t),(g||!d||t.voidElement)&&(g||i--,!c&&"<"!==h&&h&&m(l=-1===i?r:a[i].children,e,i,y,n.ignoreWhitespace))})),!r.length&&e.length&&m(r,e,0,0,n.ignoreWhitespace),r},v={bindI18n:"languageChanged",bindI18nStore:"",transEmptyNodeValue:"",transSupportBasicHtmlNodes:!0,transKeepBasicHtmlNodesFor:["br","strong","i","p"],useSuspense:!0},b=t.createContext();function O(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};v=c({},v,{},e)}function w(){return v}var j=function(){function e(){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e),this.usedNamespaces={}}var n,t,r;return n=e,(t=[{key:"addUsedNamespaces",value:function(e){var n=this;e.forEach((function(e){n.usedNamespaces[e]||(n.usedNamespaces[e]=!0)}))}},{key:"getUsedNamespaces",value:function(){return Object.keys(this.usedNamespaces)}}])&&i(n.prototype,t),r&&i(n,r),e}();function S(e){y=e}function E(){return y}var N={type:"3rdParty",init:function(e){O(e.options.react),S(e)}};function x(e){return function(n){return new Promise((function(t){var r=I();e.getInitialProps?e.getInitialProps(n).then((function(e){t(c({},e,{},r))})):t(r)}))}}function I(){var e=E(),n=e.reportNamespaces?e.reportNamespaces.getUsedNamespaces():[],t={},r={};return e.languages.forEach((function(t){r[t]={},n.forEach((function(n){r[t][n]=e.getResourceBundle(t,n)||{}}))})),t.initialI18nStore=r,t.initialLanguage=e.language,t}function k(){if(console&&console.warn){for(var e,n=arguments.length,t=new Array(n),r=0;r<n;r++)t[r]=arguments[r];"string"==typeof t[0]&&(t[0]="react-i18next:: ".concat(t[0])),(e=console).warn.apply(e,t)}}var P={};function R(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];"string"==typeof n[0]&&P[n[0]]||("string"==typeof n[0]&&(P[n[0]]=new Date),k.apply(void 0,n))}function C(e,n,t){e.loadNamespaces(n,(function(){if(e.isInitialized)t();else{e.on("initialized",(function n(){setTimeout((function(){e.off("initialized",n)}),0),t()}))}}))}function A(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!n.languages||!n.languages.length)return R("i18n.languages were undefined or empty",n.languages),!0;var r=n.languages[0],i=!!n.options&&n.options.fallbackLng,a=n.languages[n.languages.length-1];if("cimode"===r.toLowerCase())return!0;var o=function(e,t){var r=n.services.backendConnector.state["".concat(e,"|").concat(t)];return-1===r||2===r};return!(t.bindI18n&&t.bindI18n.indexOf("languageChanging")>-1&&n.services.backendConnector.backend&&n.isLanguageChangingTo&&!o(n.isLanguageChangingTo,e))&&(!!n.hasResourceBundle(r,e)||(!n.services.backendConnector.backend||!(!o(r,e)||i&&!o(a,e))))}function T(e){return e.displayName||e.name||("string"==typeof e&&e.length>0?e:"Unknown")}function L(e){return e&&(e.children||e.props&&e.props.children)}function z(e){return e?e&&e.children?e.children:e.props&&e.props.children:[]}function D(e){return Array.isArray(e)?e:[e]}function B(e,n,i,a,o){if(""===n)return[];var s=a.transKeepBasicHtmlNodesFor||[],u=n&&new RegExp(s.join("|")).test(n);if(!e&&!u)return[n];var l={};!function e(n){D(n).forEach((function(n){"string"!=typeof n&&(L(n)?e(z(n)):"object"!==r(n)||t.isValidElement(n)||Object.assign(l,n))}))}(e);var f=i.services.interpolator.interpolate(n,c({},l,{},o),i.language);var p=function e(n,i){var o=D(n);return D(i).reduce((function(n,i,l){var f,p,d,g=i.children&&i.children[0]&&i.children[0].content;if("tag"===i.type){var m=o[parseInt(i.name,10)]||{},y=0!==Object.keys(i.attrs).length?(f={props:i.attrs},(d=c({},p=m)).props=Object.assign(f.props,p.props),d):m,h=t.isValidElement(y);if("string"==typeof y)n.push(y);else if(L(y)){var v=z(y),b=e(v,i.children),O=function(e){return"[object Array]"===Object.prototype.toString.call(e)&&e.every((function(e){return t.isValidElement(e)}))}(v)&&0===b.length?v:b;y.dummy&&(y.children=O),n.push(t.cloneElement(y,c({},y.props,{key:l}),O))}else if(u&&"object"===r(y)&&y.dummy&&!h){var w=e(o,i.children);n.push(t.cloneElement(y,c({},y.props,{key:l}),w))}else if(Number.isNaN(parseFloat(i.name)))if(a.transSupportBasicHtmlNodes&&s.indexOf(i.name)>-1)if(i.voidElement)n.push(t.createElement(i.name,{key:"".concat(i.name,"-").concat(l)}));else{var j=e(o,i.children);n.push(t.createElement(i.name,{key:"".concat(i.name,"-").concat(l)},j))}else if(i.voidElement)n.push("<".concat(i.name," />"));else{var S=e(o,i.children);n.push("<".concat(i.name,">").concat(S,"</").concat(i.name,">"))}else if("object"!==r(y)||h)1===i.children.length&&g?n.push(t.cloneElement(y,c({},y.props,{key:l}),g)):n.push(t.cloneElement(y,c({},y.props,{key:l})));else{var E=i.children[0]?g:null;E&&n.push(E)}}else"text"===i.type&&n.push(i.content);return n}),[])}([{dummy:!0,children:e}],h("<0>".concat(f,"</0>")));return z(p[0])}function U(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=t.i18n,i=n.useContext(b)||{},a=i.i18n,o=i.defaultNS,s=r||a||E();if(s&&!s.reportNamespaces&&(s.reportNamespaces=new j),!s){R("You will need pass in an i18next instance by using initReactI18next");var l=function(e){return Array.isArray(e)?e[e.length-1]:e},f=[l,{},!1];return f.t=l,f.i18n={},f.ready=!1,f}var p=c({},w(),{},s.options.react,{},t),d=p.useSuspense,g=e||o||s.options&&s.options.defaultNS;g="string"==typeof g?[g]:g||["translation"],s.reportNamespaces.addUsedNamespaces&&s.reportNamespaces.addUsedNamespaces(g);var m=(s.isInitialized||s.initializedStoreOnce)&&g.every((function(e){return A(e,s,p)}));function y(){return{t:s.getFixedT(null,"fallback"===p.nsMode?g:g[0])}}var h=n.useState(y()),v=u(h,2),O=v[0],S=v[1],N=n.useRef(!0);n.useEffect((function(){var e=p.bindI18n,n=p.bindI18nStore;function t(){N.current&&S(y())}return N.current=!0,m||d||C(s,g,(function(){N.current&&S(y())})),e&&s&&s.on(e,t),n&&s&&s.store.on(n,t),function(){N.current=!1,e&&s&&e.split(" ").forEach((function(e){return s.off(e,t)})),n&&s&&n.split(" ").forEach((function(e){return s.store.off(e,t)}))}}),[g.join()]);var x=[O.t,s,m];if(x.t=O.t,x.i18n=s,x.ready=m,m)return x;if(!m&&!d)return x;throw new Promise((function(e){C(s,g,(function(){N.current&&S(y()),e()}))}))}function V(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=r.i18n,a=n.useContext(b)||{},o=a.i18n,c=i||o||E();c.options&&c.options.isClone||(e&&!c.initializedStoreOnce&&(c.services.resourceStore.data=e,c.options.ns=Object.values(e).reduce((function(e,n){return Object.keys(n).forEach((function(n){e.indexOf(n)<0&&e.push(n)})),e}),c.options.ns),c.initializedStoreOnce=!0,c.isInitialized=!0),t&&!c.initializedLanguageOnce&&(c.changeLanguage(t),c.initializedLanguageOnce=!0))}e.I18nContext=b,e.I18nextProvider=function(e){var t=e.i18n,r=e.defaultNS,i=e.children;return n.createElement(b.Provider,{value:{i18n:t,defaultNS:r}},i)},e.Trans=function(e){var i=e.children,a=e.count,o=e.parent,u=e.i18nKey,l=e.tOptions,f=e.values,p=e.defaults,d=e.components,g=e.ns,m=e.i18n,y=e.t,h=s(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t"]),v=n.useContext(b)||{},O=v.i18n,j=v.defaultNS,S=m||O||E();if(!S)return R("You will need pass in an i18next instance by using i18nextReactModule"),i;var N=y||S.t.bind(S)||function(e){return e},x=c({},w(),{},S.options&&S.options.react),I=g||N.ns||j||S.options&&S.options.defaultNS;I="string"==typeof I?[I]:I||["translation"];var P=p||function e(n,i){if(!n)return"";var a="",o=D(n),c=i.transKeepBasicHtmlNodesFor||[];return o.forEach((function(n,o){if("string"==typeof n)a+="".concat(n);else if(t.isValidElement(n)){var u=Object.keys(n.props).length,l=c.indexOf(n.type)>-1,f=n.props.children;if(!f&&l&&0===u)a+="<".concat(n.type,"/>");else if(f||l&&0===u)if(n.props.i18nIsDynamicList)a+="<".concat(o,"></").concat(o,">");else if(l&&1===u&&"string"==typeof f)a+="<".concat(n.type,">").concat(f,"</").concat(n.type,">");else{var p=e(f,i);a+="<".concat(o,">").concat(p,"</").concat(o,">")}else a+="<".concat(o,"></").concat(o,">")}else if("object"===r(n)){var d=n.format,g=s(n,["format"]),m=Object.keys(g);if(1===m.length){var y=d?"".concat(m[0],", ").concat(d):m[0];a+="{{".concat(y,"}}")}else k("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",n)}else k("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",n)})),a}(i,x)||x.transEmptyNodeValue||u,C=x.hashTransKey,A=u||(C?C(P):P),T=c({},l,{count:a},f,{},f?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:P,ns:I}),L=B(d||i,A?N(A,T):P,S,x,T),z=void 0!==o?o:x.defaultTransParent;return z?t.createElement(z,h,L):L},e.Translation=function(e){var n=e.ns,t=e.children,r=u(U(n,s(e,["ns","children"])),3),i=r[0],a=r[1],o=r[2];return t(i,{i18n:a,lng:a.language},o)},e.composeInitialProps=x,e.getDefaults=w,e.getI18n=E,e.getInitialProps=I,e.initReactI18next=N,e.setDefaults=O,e.setI18n=S,e.useSSR=V,e.useTranslation=U,e.withSSR=function(){return function(e){function n(n){var r=n.initialI18nStore,i=n.initialLanguage,a=s(n,["initialI18nStore","initialLanguage"]);return V(r,i),t.createElement(e,c({},a))}return n.getInitialProps=x(e),n.displayName="withI18nextSSR(".concat(T(e),")"),n.WrappedComponent=e,n}},e.withTranslation=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return function(r){function i(i){var a=i.forwardedRef,o=s(i,["forwardedRef"]),l=u(U(e,o),3),f=c({},o,{t:l[0],i18n:l[1],tReady:l[2]});return n.withRef&&a?f.ref=a:!n.withRef&&a&&(f.forwardedRef=a),t.createElement(r,f)}i.displayName="withI18nextTranslation(".concat(T(r),")"),i.WrappedComponent=r;return n.withRef?t.forwardRef((function(e,n){return t.createElement(i,Object.assign({},e,{forwardedRef:n}))})):i}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/src/I18nextProvider.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { I18nContext
|
|
1
|
+
import { createElement } from 'react';
|
|
2
|
+
import { I18nContext } from './context';
|
|
3
3
|
|
|
4
4
|
export function I18nextProvider({ i18n, defaultNS, children }) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return React.createElement(
|
|
5
|
+
return createElement(
|
|
8
6
|
I18nContext.Provider,
|
|
9
7
|
{
|
|
10
8
|
value: {
|
package/src/Trans.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { useContext } from 'react';
|
|
2
2
|
import HTML from 'html-parse-stringify2';
|
|
3
|
-
import { getI18n,
|
|
3
|
+
import { getI18n, I18nContext, getDefaults } from './context';
|
|
4
4
|
import { warn, warnOnce } from './utils';
|
|
5
5
|
|
|
6
6
|
function hasChildren(node) {
|
|
@@ -21,55 +21,62 @@ function getAsArray(data) {
|
|
|
21
21
|
return Array.isArray(data) ? data : [data];
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
function mergeProps(source, target){
|
|
25
|
+
const newTarget = {...target};
|
|
26
|
+
// overwrite source.props when target.props already set
|
|
27
|
+
newTarget.props = Object.assign(source.props, target.props);
|
|
28
|
+
return newTarget;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function nodesToString(children, i18nOptions) {
|
|
25
32
|
if (!children) return '';
|
|
26
|
-
let stringNode =
|
|
33
|
+
let stringNode = '';
|
|
27
34
|
|
|
35
|
+
// do not use `React.Children.toArray`, will fail at object children
|
|
28
36
|
const childrenArray = getAsArray(children);
|
|
29
37
|
const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
30
38
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
// e.g. lorem <br/> ipsum {{ messageCount, format }} dolor <strong>bold</strong> amet
|
|
40
|
+
childrenArray.forEach((child, childIndex) => {
|
|
34
41
|
if (typeof child === 'string') {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
keepArray.indexOf(child.type) > -1 &&
|
|
39
|
-
Object.keys(child.props).length === 1 &&
|
|
40
|
-
typeof hasChildren(child) === 'string'
|
|
41
|
-
? child.type
|
|
42
|
-
: elementKey;
|
|
43
|
-
|
|
44
|
-
if (child.props && child.props.i18nIsDynamicList) {
|
|
45
|
-
// we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
|
|
46
|
-
// the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
|
|
47
|
-
stringNode = `${stringNode}<${elementTag}></${elementTag}>`;
|
|
48
|
-
} else {
|
|
49
|
-
// regular case mapping the inner children
|
|
50
|
-
stringNode = `${stringNode}<${elementTag}>${nodesToString(
|
|
51
|
-
'',
|
|
52
|
-
getChildren(child),
|
|
53
|
-
i + 1,
|
|
54
|
-
i18nOptions,
|
|
55
|
-
)}</${elementTag}>`;
|
|
56
|
-
}
|
|
42
|
+
// actual e.g. lorem
|
|
43
|
+
// expected e.g. lorem
|
|
44
|
+
stringNode += `${child}`;
|
|
57
45
|
} else if (React.isValidElement(child)) {
|
|
58
|
-
|
|
59
|
-
|
|
46
|
+
const childPropsCount = Object.keys(child.props).length;
|
|
47
|
+
const shouldKeepChild = keepArray.indexOf(child.type) > -1;
|
|
48
|
+
const childChildren = child.props.children;
|
|
49
|
+
|
|
50
|
+
if (!childChildren && shouldKeepChild && childPropsCount === 0) {
|
|
51
|
+
// actual e.g. lorem <br/> ipsum
|
|
52
|
+
// expected e.g. lorem <br/> ipsum
|
|
53
|
+
stringNode += `<${child.type}/>`;
|
|
54
|
+
} else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
|
|
55
|
+
// actual e.g. lorem <hr className="test" /> ipsum
|
|
56
|
+
// expected e.g. lorem <0></0> ipsum
|
|
57
|
+
stringNode += `<${childIndex}></${childIndex}>`;
|
|
58
|
+
} else if (child.props.i18nIsDynamicList) {
|
|
59
|
+
// we got a dynamic list like
|
|
60
|
+
// e.g. <ul i18nIsDynamicList>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>
|
|
61
|
+
// expected e.g. "<0></0>", not e.g. "<0><0>a</0><1>b</1></0>"
|
|
62
|
+
stringNode += `<${childIndex}></${childIndex}>`;
|
|
63
|
+
} else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
|
|
64
|
+
// actual e.g. dolor <strong>bold</strong> amet
|
|
65
|
+
// expected e.g. dolor <strong>bold</strong> amet
|
|
66
|
+
stringNode += `<${child.type}>${childChildren}</${child.type}>`;
|
|
60
67
|
} else {
|
|
61
|
-
|
|
68
|
+
// regular case mapping the inner children
|
|
69
|
+
const content = nodesToString(childChildren, i18nOptions);
|
|
70
|
+
stringNode += `<${childIndex}>${content}</${childIndex}>`;
|
|
62
71
|
}
|
|
63
72
|
} else if (typeof child === 'object') {
|
|
64
|
-
|
|
65
|
-
const { format } =
|
|
66
|
-
delete clone.format;
|
|
67
|
-
|
|
73
|
+
// e.g. lorem {{ value, format }} ipsum
|
|
74
|
+
const { format, ...clone } = child;
|
|
68
75
|
const keys = Object.keys(clone);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
stringNode
|
|
76
|
+
|
|
77
|
+
if (keys.length === 1) {
|
|
78
|
+
const value = format ? `${keys[0]}, ${format}` : keys[0];
|
|
79
|
+
stringNode += `{{${value}}}`;
|
|
73
80
|
} else {
|
|
74
81
|
// not a valid interpolation object (can only contain one value plus format)
|
|
75
82
|
warn(
|
|
@@ -132,7 +139,9 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
|
|
|
132
139
|
return astNodes.reduce((mem, node, i) => {
|
|
133
140
|
const translationContent = node.children && node.children[0] && node.children[0].content;
|
|
134
141
|
if (node.type === 'tag') {
|
|
135
|
-
const
|
|
142
|
+
const tmp = reactNodes[parseInt(node.name, 10)] || {};
|
|
143
|
+
const child = Object.keys(node.attrs).length !== 0 ? mergeProps({props: node.attrs}, tmp) : tmp;
|
|
144
|
+
|
|
136
145
|
const isElement = React.isValidElement(child);
|
|
137
146
|
|
|
138
147
|
if (typeof child === 'string') {
|
|
@@ -215,11 +224,9 @@ export function Trans({
|
|
|
215
224
|
t: tFromProps,
|
|
216
225
|
...additionalProps
|
|
217
226
|
}) {
|
|
218
|
-
const
|
|
219
|
-
const { i18n: i18nFromContext, defaultNS: defaultNSFromContext } = getHasUsedI18nextProvider()
|
|
220
|
-
? ReactI18nContext || {}
|
|
221
|
-
: {};
|
|
227
|
+
const { i18n: i18nFromContext, defaultNS: defaultNSFromContext } = useContext(I18nContext) || {};
|
|
222
228
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
229
|
+
|
|
223
230
|
if (!i18n) {
|
|
224
231
|
warnOnce('You will need pass in an i18next instance by using i18nextReactModule');
|
|
225
232
|
return children;
|
|
@@ -228,7 +235,6 @@ export function Trans({
|
|
|
228
235
|
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
|
|
229
236
|
|
|
230
237
|
const reactI18nextOptions = { ...getDefaults(), ...(i18n.options && i18n.options.react) };
|
|
231
|
-
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
232
238
|
|
|
233
239
|
// prepare having a namespace
|
|
234
240
|
let namespaces = ns || t.ns || defaultNSFromContext || (i18n.options && i18n.options.defaultNS);
|
|
@@ -236,8 +242,9 @@ export function Trans({
|
|
|
236
242
|
|
|
237
243
|
const defaultValue =
|
|
238
244
|
defaults ||
|
|
239
|
-
nodesToString(
|
|
240
|
-
reactI18nextOptions.transEmptyNodeValue
|
|
245
|
+
nodesToString(children, reactI18nextOptions) ||
|
|
246
|
+
reactI18nextOptions.transEmptyNodeValue ||
|
|
247
|
+
i18nKey;
|
|
241
248
|
const { hashTransKey } = reactI18nextOptions;
|
|
242
249
|
const key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
243
250
|
const interpolationOverride = values ? {} : { interpolation: { prefix: '#$?', suffix: '?$#' } };
|
|
@@ -251,18 +258,17 @@ export function Trans({
|
|
|
251
258
|
};
|
|
252
259
|
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
253
260
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
combinedTOpts,
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
return React.createElement(
|
|
264
|
-
useAsParent,
|
|
265
|
-
additionalProps,
|
|
266
|
-
renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts),
|
|
261
|
+
const content = renderNodes(
|
|
262
|
+
components || children,
|
|
263
|
+
translation,
|
|
264
|
+
i18n,
|
|
265
|
+
reactI18nextOptions,
|
|
266
|
+
combinedTOpts,
|
|
267
267
|
);
|
|
268
|
+
|
|
269
|
+
// allows user to pass `null` to `parent`
|
|
270
|
+
// and override `defaultTransParent` if is present
|
|
271
|
+
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
272
|
+
|
|
273
|
+
return useAsParent ? React.createElement(useAsParent, additionalProps, content) : content;
|
|
268
274
|
}
|
package/src/context.js
CHANGED
|
@@ -12,18 +12,9 @@ let defaultOptions = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
let i18nInstance;
|
|
15
|
-
let hasUsedI18nextProvider;
|
|
16
15
|
|
|
17
16
|
export const I18nContext = React.createContext();
|
|
18
17
|
|
|
19
|
-
export function usedI18nextProvider(used) {
|
|
20
|
-
hasUsedI18nextProvider = used;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function getHasUsedI18nextProvider() {
|
|
24
|
-
return hasUsedI18nextProvider;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
18
|
export function setDefaults(options = {}) {
|
|
28
19
|
defaultOptions = { ...defaultOptions, ...options };
|
|
29
20
|
}
|
package/src/useSSR.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { useContext } from 'react';
|
|
2
|
-
import { getI18n,
|
|
2
|
+
import { getI18n, I18nContext } from './context';
|
|
3
3
|
|
|
4
4
|
export function useSSR(initialI18nStore, initialLanguage, props = {}) {
|
|
5
5
|
const { i18n: i18nFromProps } = props;
|
|
6
|
-
const
|
|
7
|
-
const { i18n: i18nFromContext } = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {};
|
|
6
|
+
const { i18n: i18nFromContext } = useContext(I18nContext) || {};
|
|
8
7
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
9
8
|
|
|
10
|
-
// opt out if is a cloned instance, eg. created by i18next-
|
|
9
|
+
// opt out if is a cloned instance, eg. created by i18next-http-middleware on request
|
|
11
10
|
// -> do not set initial stuff on server side
|
|
12
11
|
if (i18n.options && i18n.options.isClone) return;
|
|
13
12
|
|