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