react-i18next 12.0.0 → 12.1.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 +4 -0
- package/README.md +1 -1
- package/TransWithoutContext.d.ts +33 -0
- package/dist/amd/react-i18next.js +178 -133
- package/dist/amd/react-i18next.min.js +1 -1
- package/dist/commonjs/Trans.js +26 -243
- package/dist/commonjs/TransWithoutContext.js +288 -0
- package/dist/commonjs/context.js +39 -46
- package/dist/commonjs/defaults.js +37 -0
- package/dist/commonjs/i18nInstance.js +16 -0
- package/dist/commonjs/index.js +8 -0
- package/dist/commonjs/initReactI18next.js +19 -0
- package/dist/es/Trans.js +22 -242
- package/dist/es/TransWithoutContext.js +272 -0
- package/dist/es/context.js +5 -33
- package/dist/es/defaults.js +24 -0
- package/dist/es/i18nInstance.js +7 -0
- package/dist/es/index.js +1 -0
- package/dist/es/initReactI18next.js +9 -0
- package/dist/umd/react-i18next.js +178 -133
- package/dist/umd/react-i18next.min.js +1 -1
- package/index.d.ts +2 -32
- package/initReactI18next.d.ts +3 -0
- package/package.json +22 -4
- package/react-i18next.js +178 -133
- package/react-i18next.min.js +1 -1
- package/src/Trans.js +18 -316
- package/src/TransWithoutContext.js +343 -0
- package/src/context.js +4 -40
- package/src/defaults.js +22 -0
- package/src/i18nInstance.js +9 -0
- package/src/index.js +1 -0
- package/src/initReactI18next.js +11 -0
- package/tslint.json +2 -1
package/dist/commonjs/Trans.js
CHANGED
|
@@ -5,230 +5,30 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.nodesToString = nodesToString;
|
|
9
8
|
exports.Trans = Trans;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
Object.defineProperty(exports, "nodesToString", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function get() {
|
|
12
|
+
return _TransWithoutContext.nodesToString;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
14
15
|
|
|
15
16
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
16
17
|
|
|
18
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
19
|
+
|
|
17
20
|
var _react = require("react");
|
|
18
21
|
|
|
19
|
-
var
|
|
22
|
+
var _TransWithoutContext = require("./TransWithoutContext");
|
|
20
23
|
|
|
21
24
|
var _context = require("./context");
|
|
22
25
|
|
|
23
|
-
var
|
|
24
|
-
|
|
25
|
-
var _excluded = ["format"],
|
|
26
|
-
_excluded2 = ["children", "count", "parent", "i18nKey", "context", "tOptions", "values", "defaults", "components", "ns", "i18n", "t", "shouldUnescape"];
|
|
26
|
+
var _excluded = ["children", "count", "parent", "i18nKey", "context", "tOptions", "values", "defaults", "components", "ns", "i18n", "t", "shouldUnescape"];
|
|
27
27
|
|
|
28
28
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
29
29
|
|
|
30
30
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
31
31
|
|
|
32
|
-
function hasChildren(node, checkLength) {
|
|
33
|
-
if (!node) return false;
|
|
34
|
-
var base = node.props ? node.props.children : node.children;
|
|
35
|
-
if (checkLength) return base.length > 0;
|
|
36
|
-
return !!base;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function getChildren(node) {
|
|
40
|
-
if (!node) return [];
|
|
41
|
-
return node.props ? node.props.children : node.children;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function hasValidReactChildren(children) {
|
|
45
|
-
if (Object.prototype.toString.call(children) !== '[object Array]') return false;
|
|
46
|
-
return children.every(function (child) {
|
|
47
|
-
return (0, _react.isValidElement)(child);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getAsArray(data) {
|
|
52
|
-
return Array.isArray(data) ? data : [data];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function mergeProps(source, target) {
|
|
56
|
-
var newTarget = _objectSpread({}, target);
|
|
57
|
-
|
|
58
|
-
newTarget.props = Object.assign(source.props, target.props);
|
|
59
|
-
return newTarget;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function nodesToString(children, i18nOptions) {
|
|
63
|
-
if (!children) return '';
|
|
64
|
-
var stringNode = '';
|
|
65
|
-
var childrenArray = getAsArray(children);
|
|
66
|
-
var keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
|
|
67
|
-
childrenArray.forEach(function (child, childIndex) {
|
|
68
|
-
if (typeof child === 'string') {
|
|
69
|
-
stringNode += "".concat(child);
|
|
70
|
-
} else if ((0, _react.isValidElement)(child)) {
|
|
71
|
-
var childPropsCount = Object.keys(child.props).length;
|
|
72
|
-
var shouldKeepChild = keepArray.indexOf(child.type) > -1;
|
|
73
|
-
var childChildren = child.props.children;
|
|
74
|
-
|
|
75
|
-
if (!childChildren && shouldKeepChild && childPropsCount === 0) {
|
|
76
|
-
stringNode += "<".concat(child.type, "/>");
|
|
77
|
-
} else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
|
|
78
|
-
stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
|
|
79
|
-
} else if (child.props.i18nIsDynamicList) {
|
|
80
|
-
stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
|
|
81
|
-
} else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
|
|
82
|
-
stringNode += "<".concat(child.type, ">").concat(childChildren, "</").concat(child.type, ">");
|
|
83
|
-
} else {
|
|
84
|
-
var content = nodesToString(childChildren, i18nOptions);
|
|
85
|
-
stringNode += "<".concat(childIndex, ">").concat(content, "</").concat(childIndex, ">");
|
|
86
|
-
}
|
|
87
|
-
} else if (child === null) {
|
|
88
|
-
(0, _utils.warn)("Trans: the passed in value is invalid - seems you passed in a null child.");
|
|
89
|
-
} else if ((0, _typeof2["default"])(child) === 'object') {
|
|
90
|
-
var format = child.format,
|
|
91
|
-
clone = (0, _objectWithoutProperties2["default"])(child, _excluded);
|
|
92
|
-
var keys = Object.keys(clone);
|
|
93
|
-
|
|
94
|
-
if (keys.length === 1) {
|
|
95
|
-
var value = format ? "".concat(keys[0], ", ").concat(format) : keys[0];
|
|
96
|
-
stringNode += "{{".concat(value, "}}");
|
|
97
|
-
} else {
|
|
98
|
-
(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);
|
|
99
|
-
}
|
|
100
|
-
} else {
|
|
101
|
-
(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);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
return stringNode;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) {
|
|
108
|
-
if (targetString === '') return [];
|
|
109
|
-
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
110
|
-
var emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.join('|')).test(targetString);
|
|
111
|
-
if (!children && !emptyChildrenButNeedsHandling) return [targetString];
|
|
112
|
-
var data = {};
|
|
113
|
-
|
|
114
|
-
function getData(childs) {
|
|
115
|
-
var childrenArray = getAsArray(childs);
|
|
116
|
-
childrenArray.forEach(function (child) {
|
|
117
|
-
if (typeof child === 'string') return;
|
|
118
|
-
if (hasChildren(child)) getData(getChildren(child));else if ((0, _typeof2["default"])(child) === 'object' && !(0, _react.isValidElement)(child)) Object.assign(data, child);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
getData(children);
|
|
123
|
-
|
|
124
|
-
var ast = _htmlParseStringify["default"].parse("<0>".concat(targetString, "</0>"));
|
|
125
|
-
|
|
126
|
-
var opts = _objectSpread(_objectSpread({}, data), combinedTOpts);
|
|
127
|
-
|
|
128
|
-
function renderInner(child, node, rootReactNode) {
|
|
129
|
-
var childs = getChildren(child);
|
|
130
|
-
var mappedChildren = mapAST(childs, node.children, rootReactNode);
|
|
131
|
-
return hasValidReactChildren(childs) && mappedChildren.length === 0 ? childs : mappedChildren;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function pushTranslatedJSX(child, inner, mem, i, isVoid) {
|
|
135
|
-
if (child.dummy) child.children = inner;
|
|
136
|
-
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
137
|
-
key: i
|
|
138
|
-
}), isVoid ? undefined : inner));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function mapAST(reactNode, astNode, rootReactNode) {
|
|
142
|
-
var reactNodes = getAsArray(reactNode);
|
|
143
|
-
var astNodes = getAsArray(astNode);
|
|
144
|
-
return astNodes.reduce(function (mem, node, i) {
|
|
145
|
-
var translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
|
|
146
|
-
|
|
147
|
-
if (node.type === 'tag') {
|
|
148
|
-
var tmp = reactNodes[parseInt(node.name, 10)];
|
|
149
|
-
if (!tmp && rootReactNode.length === 1 && rootReactNode[0][node.name]) tmp = rootReactNode[0][node.name];
|
|
150
|
-
if (!tmp) tmp = {};
|
|
151
|
-
var child = Object.keys(node.attrs).length !== 0 ? mergeProps({
|
|
152
|
-
props: node.attrs
|
|
153
|
-
}, tmp) : tmp;
|
|
154
|
-
var isElement = (0, _react.isValidElement)(child);
|
|
155
|
-
var isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
|
|
156
|
-
var isEmptyTransWithHTML = emptyChildrenButNeedsHandling && (0, _typeof2["default"])(child) === 'object' && child.dummy && !isElement;
|
|
157
|
-
var isKnownComponent = (0, _typeof2["default"])(children) === 'object' && children !== null && Object.hasOwnProperty.call(children, node.name);
|
|
158
|
-
|
|
159
|
-
if (typeof child === 'string') {
|
|
160
|
-
var value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
|
|
161
|
-
mem.push(value);
|
|
162
|
-
} else if (hasChildren(child) || isValidTranslationWithChildren) {
|
|
163
|
-
var inner = renderInner(child, node, rootReactNode);
|
|
164
|
-
pushTranslatedJSX(child, inner, mem, i);
|
|
165
|
-
} else if (isEmptyTransWithHTML) {
|
|
166
|
-
var _inner = mapAST(reactNodes, node.children, rootReactNode);
|
|
167
|
-
|
|
168
|
-
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
169
|
-
key: i
|
|
170
|
-
}), _inner));
|
|
171
|
-
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
172
|
-
if (isKnownComponent) {
|
|
173
|
-
var _inner2 = renderInner(child, node, rootReactNode);
|
|
174
|
-
|
|
175
|
-
pushTranslatedJSX(child, _inner2, mem, i, node.voidElement);
|
|
176
|
-
} else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
177
|
-
if (node.voidElement) {
|
|
178
|
-
mem.push((0, _react.createElement)(node.name, {
|
|
179
|
-
key: "".concat(node.name, "-").concat(i)
|
|
180
|
-
}));
|
|
181
|
-
} else {
|
|
182
|
-
var _inner3 = mapAST(reactNodes, node.children, rootReactNode);
|
|
183
|
-
|
|
184
|
-
mem.push((0, _react.createElement)(node.name, {
|
|
185
|
-
key: "".concat(node.name, "-").concat(i)
|
|
186
|
-
}, _inner3));
|
|
187
|
-
}
|
|
188
|
-
} else if (node.voidElement) {
|
|
189
|
-
mem.push("<".concat(node.name, " />"));
|
|
190
|
-
} else {
|
|
191
|
-
var _inner4 = mapAST(reactNodes, node.children, rootReactNode);
|
|
192
|
-
|
|
193
|
-
mem.push("<".concat(node.name, ">").concat(_inner4, "</").concat(node.name, ">"));
|
|
194
|
-
}
|
|
195
|
-
} else if ((0, _typeof2["default"])(child) === 'object' && !isElement) {
|
|
196
|
-
var content = node.children[0] ? translationContent : null;
|
|
197
|
-
if (content) mem.push(content);
|
|
198
|
-
} else if (node.children.length === 1 && translationContent) {
|
|
199
|
-
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
200
|
-
key: i
|
|
201
|
-
}), translationContent));
|
|
202
|
-
} else {
|
|
203
|
-
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
204
|
-
key: i
|
|
205
|
-
})));
|
|
206
|
-
}
|
|
207
|
-
} else if (node.type === 'text') {
|
|
208
|
-
var wrapTextNodes = i18nOptions.transWrapTextNodes;
|
|
209
|
-
|
|
210
|
-
var _content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
|
|
211
|
-
|
|
212
|
-
if (wrapTextNodes) {
|
|
213
|
-
mem.push((0, _react.createElement)(wrapTextNodes, {
|
|
214
|
-
key: "".concat(node.name, "-").concat(i)
|
|
215
|
-
}, _content));
|
|
216
|
-
} else {
|
|
217
|
-
mem.push(_content);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return mem;
|
|
222
|
-
}, []);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
var result = mapAST([{
|
|
226
|
-
dummy: true,
|
|
227
|
-
children: children || []
|
|
228
|
-
}], ast, getAsArray(children || []));
|
|
229
|
-
return getChildren(result[0]);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
32
|
function Trans(_ref) {
|
|
233
33
|
var children = _ref.children,
|
|
234
34
|
count = _ref.count,
|
|
@@ -244,7 +44,7 @@ function Trans(_ref) {
|
|
|
244
44
|
i18nFromProps = _ref.i18n,
|
|
245
45
|
tFromProps = _ref.t,
|
|
246
46
|
shouldUnescape = _ref.shouldUnescape,
|
|
247
|
-
additionalProps = (0, _objectWithoutProperties2["default"])(_ref,
|
|
47
|
+
additionalProps = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
|
|
248
48
|
|
|
249
49
|
var _ref2 = (0, _react.useContext)(_context.I18nContext) || {},
|
|
250
50
|
i18nFromContext = _ref2.i18n,
|
|
@@ -252,40 +52,23 @@ function Trans(_ref) {
|
|
|
252
52
|
|
|
253
53
|
var i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
|
|
254
54
|
|
|
255
|
-
if (!i18n) {
|
|
256
|
-
(0, _utils.warnOnce)('You will need to pass in an i18next instance by using i18nextReactModule');
|
|
257
|
-
return children;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
55
|
var t = tFromProps || i18n.t.bind(i18n) || function (k) {
|
|
261
56
|
return k;
|
|
262
57
|
};
|
|
263
58
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
var combinedTOpts = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, tOptions), {}, {
|
|
281
|
-
count: count
|
|
282
|
-
}, values), interpolationOverride), {}, {
|
|
283
|
-
defaultValue: defaultValue,
|
|
284
|
-
ns: namespaces
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
var translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
288
|
-
var content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
|
|
289
|
-
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
290
|
-
return useAsParent ? (0, _react.createElement)(useAsParent, additionalProps, content) : content;
|
|
59
|
+
return (0, _TransWithoutContext.Trans)(_objectSpread({
|
|
60
|
+
children: children,
|
|
61
|
+
count: count,
|
|
62
|
+
parent: parent,
|
|
63
|
+
i18nKey: i18nKey,
|
|
64
|
+
context: context,
|
|
65
|
+
tOptions: tOptions,
|
|
66
|
+
values: values,
|
|
67
|
+
defaults: defaults,
|
|
68
|
+
components: components,
|
|
69
|
+
ns: ns || t.ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS,
|
|
70
|
+
i18n: i18n,
|
|
71
|
+
t: tFromProps,
|
|
72
|
+
shouldUnescape: shouldUnescape
|
|
73
|
+
}, additionalProps));
|
|
291
74
|
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.nodesToString = nodesToString;
|
|
9
|
+
exports.Trans = Trans;
|
|
10
|
+
|
|
11
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
12
|
+
|
|
13
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
14
|
+
|
|
15
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
16
|
+
|
|
17
|
+
var _react = require("react");
|
|
18
|
+
|
|
19
|
+
var _htmlParseStringify = _interopRequireDefault(require("html-parse-stringify"));
|
|
20
|
+
|
|
21
|
+
var _utils = require("./utils");
|
|
22
|
+
|
|
23
|
+
var _defaults = require("./defaults");
|
|
24
|
+
|
|
25
|
+
var _i18nInstance = require("./i18nInstance");
|
|
26
|
+
|
|
27
|
+
var _excluded = ["format"],
|
|
28
|
+
_excluded2 = ["children", "count", "parent", "i18nKey", "context", "tOptions", "values", "defaults", "components", "ns", "i18n", "t", "shouldUnescape"];
|
|
29
|
+
|
|
30
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
31
|
+
|
|
32
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
33
|
+
|
|
34
|
+
function hasChildren(node, checkLength) {
|
|
35
|
+
if (!node) return false;
|
|
36
|
+
var base = node.props ? node.props.children : node.children;
|
|
37
|
+
if (checkLength) return base.length > 0;
|
|
38
|
+
return !!base;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getChildren(node) {
|
|
42
|
+
if (!node) return [];
|
|
43
|
+
return node.props ? node.props.children : node.children;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function hasValidReactChildren(children) {
|
|
47
|
+
if (Object.prototype.toString.call(children) !== '[object Array]') return false;
|
|
48
|
+
return children.every(function (child) {
|
|
49
|
+
return (0, _react.isValidElement)(child);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getAsArray(data) {
|
|
54
|
+
return Array.isArray(data) ? data : [data];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function mergeProps(source, target) {
|
|
58
|
+
var newTarget = _objectSpread({}, target);
|
|
59
|
+
|
|
60
|
+
newTarget.props = Object.assign(source.props, target.props);
|
|
61
|
+
return newTarget;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function nodesToString(children, i18nOptions) {
|
|
65
|
+
if (!children) return '';
|
|
66
|
+
var stringNode = '';
|
|
67
|
+
var childrenArray = getAsArray(children);
|
|
68
|
+
var keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
|
|
69
|
+
childrenArray.forEach(function (child, childIndex) {
|
|
70
|
+
if (typeof child === 'string') {
|
|
71
|
+
stringNode += "".concat(child);
|
|
72
|
+
} else if ((0, _react.isValidElement)(child)) {
|
|
73
|
+
var childPropsCount = Object.keys(child.props).length;
|
|
74
|
+
var shouldKeepChild = keepArray.indexOf(child.type) > -1;
|
|
75
|
+
var childChildren = child.props.children;
|
|
76
|
+
|
|
77
|
+
if (!childChildren && shouldKeepChild && childPropsCount === 0) {
|
|
78
|
+
stringNode += "<".concat(child.type, "/>");
|
|
79
|
+
} else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
|
|
80
|
+
stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
|
|
81
|
+
} else if (child.props.i18nIsDynamicList) {
|
|
82
|
+
stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
|
|
83
|
+
} else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
|
|
84
|
+
stringNode += "<".concat(child.type, ">").concat(childChildren, "</").concat(child.type, ">");
|
|
85
|
+
} else {
|
|
86
|
+
var content = nodesToString(childChildren, i18nOptions);
|
|
87
|
+
stringNode += "<".concat(childIndex, ">").concat(content, "</").concat(childIndex, ">");
|
|
88
|
+
}
|
|
89
|
+
} else if (child === null) {
|
|
90
|
+
(0, _utils.warn)("Trans: the passed in value is invalid - seems you passed in a null child.");
|
|
91
|
+
} else if ((0, _typeof2["default"])(child) === 'object') {
|
|
92
|
+
var format = child.format,
|
|
93
|
+
clone = (0, _objectWithoutProperties2["default"])(child, _excluded);
|
|
94
|
+
var keys = Object.keys(clone);
|
|
95
|
+
|
|
96
|
+
if (keys.length === 1) {
|
|
97
|
+
var value = format ? "".concat(keys[0], ", ").concat(format) : keys[0];
|
|
98
|
+
stringNode += "{{".concat(value, "}}");
|
|
99
|
+
} else {
|
|
100
|
+
(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);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
(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);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return stringNode;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) {
|
|
110
|
+
if (targetString === '') return [];
|
|
111
|
+
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
112
|
+
var emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.join('|')).test(targetString);
|
|
113
|
+
if (!children && !emptyChildrenButNeedsHandling) return [targetString];
|
|
114
|
+
var data = {};
|
|
115
|
+
|
|
116
|
+
function getData(childs) {
|
|
117
|
+
var childrenArray = getAsArray(childs);
|
|
118
|
+
childrenArray.forEach(function (child) {
|
|
119
|
+
if (typeof child === 'string') return;
|
|
120
|
+
if (hasChildren(child)) getData(getChildren(child));else if ((0, _typeof2["default"])(child) === 'object' && !(0, _react.isValidElement)(child)) Object.assign(data, child);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
getData(children);
|
|
125
|
+
|
|
126
|
+
var ast = _htmlParseStringify["default"].parse("<0>".concat(targetString, "</0>"));
|
|
127
|
+
|
|
128
|
+
var opts = _objectSpread(_objectSpread({}, data), combinedTOpts);
|
|
129
|
+
|
|
130
|
+
function renderInner(child, node, rootReactNode) {
|
|
131
|
+
var childs = getChildren(child);
|
|
132
|
+
var mappedChildren = mapAST(childs, node.children, rootReactNode);
|
|
133
|
+
return hasValidReactChildren(childs) && mappedChildren.length === 0 ? childs : mappedChildren;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function pushTranslatedJSX(child, inner, mem, i, isVoid) {
|
|
137
|
+
if (child.dummy) child.children = inner;
|
|
138
|
+
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
139
|
+
key: i
|
|
140
|
+
}), isVoid ? undefined : inner));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function mapAST(reactNode, astNode, rootReactNode) {
|
|
144
|
+
var reactNodes = getAsArray(reactNode);
|
|
145
|
+
var astNodes = getAsArray(astNode);
|
|
146
|
+
return astNodes.reduce(function (mem, node, i) {
|
|
147
|
+
var translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
|
|
148
|
+
|
|
149
|
+
if (node.type === 'tag') {
|
|
150
|
+
var tmp = reactNodes[parseInt(node.name, 10)];
|
|
151
|
+
if (!tmp && rootReactNode.length === 1 && rootReactNode[0][node.name]) tmp = rootReactNode[0][node.name];
|
|
152
|
+
if (!tmp) tmp = {};
|
|
153
|
+
var child = Object.keys(node.attrs).length !== 0 ? mergeProps({
|
|
154
|
+
props: node.attrs
|
|
155
|
+
}, tmp) : tmp;
|
|
156
|
+
var isElement = (0, _react.isValidElement)(child);
|
|
157
|
+
var isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
|
|
158
|
+
var isEmptyTransWithHTML = emptyChildrenButNeedsHandling && (0, _typeof2["default"])(child) === 'object' && child.dummy && !isElement;
|
|
159
|
+
var isKnownComponent = (0, _typeof2["default"])(children) === 'object' && children !== null && Object.hasOwnProperty.call(children, node.name);
|
|
160
|
+
|
|
161
|
+
if (typeof child === 'string') {
|
|
162
|
+
var value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
|
|
163
|
+
mem.push(value);
|
|
164
|
+
} else if (hasChildren(child) || isValidTranslationWithChildren) {
|
|
165
|
+
var inner = renderInner(child, node, rootReactNode);
|
|
166
|
+
pushTranslatedJSX(child, inner, mem, i);
|
|
167
|
+
} else if (isEmptyTransWithHTML) {
|
|
168
|
+
var _inner = mapAST(reactNodes, node.children, rootReactNode);
|
|
169
|
+
|
|
170
|
+
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
171
|
+
key: i
|
|
172
|
+
}), _inner));
|
|
173
|
+
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
174
|
+
if (isKnownComponent) {
|
|
175
|
+
var _inner2 = renderInner(child, node, rootReactNode);
|
|
176
|
+
|
|
177
|
+
pushTranslatedJSX(child, _inner2, mem, i, node.voidElement);
|
|
178
|
+
} else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
179
|
+
if (node.voidElement) {
|
|
180
|
+
mem.push((0, _react.createElement)(node.name, {
|
|
181
|
+
key: "".concat(node.name, "-").concat(i)
|
|
182
|
+
}));
|
|
183
|
+
} else {
|
|
184
|
+
var _inner3 = mapAST(reactNodes, node.children, rootReactNode);
|
|
185
|
+
|
|
186
|
+
mem.push((0, _react.createElement)(node.name, {
|
|
187
|
+
key: "".concat(node.name, "-").concat(i)
|
|
188
|
+
}, _inner3));
|
|
189
|
+
}
|
|
190
|
+
} else if (node.voidElement) {
|
|
191
|
+
mem.push("<".concat(node.name, " />"));
|
|
192
|
+
} else {
|
|
193
|
+
var _inner4 = mapAST(reactNodes, node.children, rootReactNode);
|
|
194
|
+
|
|
195
|
+
mem.push("<".concat(node.name, ">").concat(_inner4, "</").concat(node.name, ">"));
|
|
196
|
+
}
|
|
197
|
+
} else if ((0, _typeof2["default"])(child) === 'object' && !isElement) {
|
|
198
|
+
var content = node.children[0] ? translationContent : null;
|
|
199
|
+
if (content) mem.push(content);
|
|
200
|
+
} else if (node.children.length === 1 && translationContent) {
|
|
201
|
+
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
202
|
+
key: i
|
|
203
|
+
}), translationContent));
|
|
204
|
+
} else {
|
|
205
|
+
mem.push((0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
|
|
206
|
+
key: i
|
|
207
|
+
})));
|
|
208
|
+
}
|
|
209
|
+
} else if (node.type === 'text') {
|
|
210
|
+
var wrapTextNodes = i18nOptions.transWrapTextNodes;
|
|
211
|
+
|
|
212
|
+
var _content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
|
|
213
|
+
|
|
214
|
+
if (wrapTextNodes) {
|
|
215
|
+
mem.push((0, _react.createElement)(wrapTextNodes, {
|
|
216
|
+
key: "".concat(node.name, "-").concat(i)
|
|
217
|
+
}, _content));
|
|
218
|
+
} else {
|
|
219
|
+
mem.push(_content);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return mem;
|
|
224
|
+
}, []);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
var result = mapAST([{
|
|
228
|
+
dummy: true,
|
|
229
|
+
children: children || []
|
|
230
|
+
}], ast, getAsArray(children || []));
|
|
231
|
+
return getChildren(result[0]);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function Trans(_ref) {
|
|
235
|
+
var children = _ref.children,
|
|
236
|
+
count = _ref.count,
|
|
237
|
+
parent = _ref.parent,
|
|
238
|
+
i18nKey = _ref.i18nKey,
|
|
239
|
+
context = _ref.context,
|
|
240
|
+
_ref$tOptions = _ref.tOptions,
|
|
241
|
+
tOptions = _ref$tOptions === void 0 ? {} : _ref$tOptions,
|
|
242
|
+
values = _ref.values,
|
|
243
|
+
defaults = _ref.defaults,
|
|
244
|
+
components = _ref.components,
|
|
245
|
+
ns = _ref.ns,
|
|
246
|
+
i18nFromProps = _ref.i18n,
|
|
247
|
+
tFromProps = _ref.t,
|
|
248
|
+
shouldUnescape = _ref.shouldUnescape,
|
|
249
|
+
additionalProps = (0, _objectWithoutProperties2["default"])(_ref, _excluded2);
|
|
250
|
+
var i18n = i18nFromProps || (0, _i18nInstance.getI18n)();
|
|
251
|
+
|
|
252
|
+
if (!i18n) {
|
|
253
|
+
(0, _utils.warnOnce)('You will need to pass in an i18next instance by using i18nextReactModule');
|
|
254
|
+
return children;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
var t = tFromProps || i18n.t.bind(i18n) || function (k) {
|
|
258
|
+
return k;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
if (context) tOptions.context = context;
|
|
262
|
+
|
|
263
|
+
var reactI18nextOptions = _objectSpread(_objectSpread({}, (0, _defaults.getDefaults)()), i18n.options && i18n.options.react);
|
|
264
|
+
|
|
265
|
+
var namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
|
|
266
|
+
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
|
|
267
|
+
var defaultValue = defaults || nodesToString(children, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue || i18nKey;
|
|
268
|
+
var hashTransKey = reactI18nextOptions.hashTransKey;
|
|
269
|
+
var key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
|
|
270
|
+
var interpolationOverride = values ? tOptions.interpolation : {
|
|
271
|
+
interpolation: _objectSpread(_objectSpread({}, tOptions.interpolation), {}, {
|
|
272
|
+
prefix: '#$?',
|
|
273
|
+
suffix: '?$#'
|
|
274
|
+
})
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
var combinedTOpts = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, tOptions), {}, {
|
|
278
|
+
count: count
|
|
279
|
+
}, values), interpolationOverride), {}, {
|
|
280
|
+
defaultValue: defaultValue,
|
|
281
|
+
ns: namespaces
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
var translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
285
|
+
var content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
|
|
286
|
+
var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
|
|
287
|
+
return useAsParent ? (0, _react.createElement)(useAsParent, additionalProps, content) : content;
|
|
288
|
+
}
|