react-i18next 11.4.0 → 11.7.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/dist/es/Trans.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
2
  import _typeof from "@babel/runtime/helpers/typeof";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
4
 
5
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; }
6
6
 
@@ -8,11 +8,14 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
8
8
 
9
9
  import React, { useContext } from 'react';
10
10
  import HTML from 'html-parse-stringify2';
11
- import { getI18n, getHasUsedI18nextProvider, I18nContext, getDefaults } from './context';
11
+ import { getI18n, I18nContext, getDefaults } from './context';
12
12
  import { warn, warnOnce } from './utils';
13
13
 
14
- function hasChildren(node) {
15
- return node && (node.children || node.props && node.props.children);
14
+ function hasChildren(node, checkLength) {
15
+ if (!node) return false;
16
+ var base = node.props ? node.props.children : node.children;
17
+ if (checkLength) return base.length > 0;
18
+ return !!base;
16
19
  }
17
20
 
18
21
  function getChildren(node) {
@@ -31,46 +34,48 @@ function getAsArray(data) {
31
34
  return Array.isArray(data) ? data : [data];
32
35
  }
33
36
 
34
- export function nodesToString(startingString, children, index, i18nOptions) {
37
+ function mergeProps(source, target) {
38
+ var newTarget = _objectSpread({}, target);
39
+
40
+ newTarget.props = Object.assign(source.props, target.props);
41
+ return newTarget;
42
+ }
43
+
44
+ export function nodesToString(children, i18nOptions) {
35
45
  if (!children) return '';
36
- var stringNode = startingString;
46
+ var stringNode = '';
37
47
  var childrenArray = getAsArray(children);
38
48
  var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
39
- childrenArray.forEach(function (child, i) {
40
- var elementKey = "".concat(i);
41
-
49
+ childrenArray.forEach(function (child, childIndex) {
42
50
  if (typeof child === 'string') {
43
- stringNode = "".concat(stringNode).concat(child);
44
- } else if (hasChildren(child)) {
45
- var elementTag = keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 1 && typeof hasChildren(child) === 'string' ? child.type : elementKey;
46
-
47
- if (child.props && child.props.i18nIsDynamicList) {
48
- // we got a dynamic list like "<ul>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>""
49
- // the result should be "<0></0>" and not "<0><0>a</0><1>b</1></0>"
50
- stringNode = "".concat(stringNode, "<").concat(elementTag, "></").concat(elementTag, ">");
51
- } else {
52
- // regular case mapping the inner children
53
- stringNode = "".concat(stringNode, "<").concat(elementTag, ">").concat(nodesToString('', getChildren(child), i + 1, i18nOptions), "</").concat(elementTag, ">");
54
- }
51
+ stringNode += "".concat(child);
55
52
  } else if (React.isValidElement(child)) {
56
- if (keepArray.indexOf(child.type) > -1 && Object.keys(child.props).length === 0) {
57
- stringNode = "".concat(stringNode, "<").concat(child.type, "/>");
53
+ var childPropsCount = Object.keys(child.props).length;
54
+ var shouldKeepChild = keepArray.indexOf(child.type) > -1;
55
+ var childChildren = child.props.children;
56
+
57
+ if (!childChildren && shouldKeepChild && childPropsCount === 0) {
58
+ stringNode += "<".concat(child.type, "/>");
59
+ } else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
60
+ stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
61
+ } else if (child.props.i18nIsDynamicList) {
62
+ stringNode += "<".concat(childIndex, "></").concat(childIndex, ">");
63
+ } else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
64
+ stringNode += "<".concat(child.type, ">").concat(childChildren, "</").concat(child.type, ">");
58
65
  } else {
59
- stringNode = "".concat(stringNode, "<").concat(elementKey, "></").concat(elementKey, ">");
66
+ var content = nodesToString(childChildren, i18nOptions);
67
+ stringNode += "<".concat(childIndex, ">").concat(content, "</").concat(childIndex, ">");
60
68
  }
61
69
  } else if (_typeof(child) === 'object') {
62
- var clone = _objectSpread({}, child);
70
+ var format = child.format,
71
+ clone = _objectWithoutProperties(child, ["format"]);
63
72
 
64
- var format = clone.format;
65
- delete clone.format;
66
73
  var keys = Object.keys(clone);
67
74
 
68
- if (format && keys.length === 1) {
69
- stringNode = "".concat(stringNode, "{{").concat(keys[0], ", ").concat(format, "}}");
70
- } else if (keys.length === 1) {
71
- stringNode = "".concat(stringNode, "{{").concat(keys[0], "}}");
75
+ if (keys.length === 1) {
76
+ var value = format ? "".concat(keys[0], ", ").concat(format) : keys[0];
77
+ stringNode += "{{".concat(value, "}}");
72
78
  } else {
73
- // not a valid interpolation object (can only contain one value plus format)
74
79
  warn("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.", child);
75
80
  }
76
81
  } else {
@@ -81,13 +86,10 @@ export function nodesToString(startingString, children, index, i18nOptions) {
81
86
  }
82
87
 
83
88
  function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
84
- if (targetString === '') return []; // check if contains tags we need to replace from html string to react nodes
85
-
89
+ if (targetString === '') return [];
86
90
  var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
87
- var emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.join('|')).test(targetString); // no need to replace tags in the targetstring
88
-
89
- if (!children && !emptyChildrenButNeedsHandling) return [targetString]; // v2 -> interpolates upfront no need for "some <0>{{var}}</0>"" -> will be just "some {{var}}" in translation file
90
-
91
+ var emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.join('|')).test(targetString);
92
+ if (!children && !emptyChildrenButNeedsHandling) return [targetString];
91
93
  var data = {};
92
94
 
93
95
  function getData(childs) {
@@ -99,77 +101,79 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
99
101
  }
100
102
 
101
103
  getData(children);
102
- var interpolatedString = i18n.services.interpolator.interpolate(targetString, _objectSpread({}, data, {}, combinedTOpts), i18n.language); // parse ast from string with additional wrapper tag
103
- // -> avoids issues in parser removing prepending text nodes
104
-
104
+ var interpolatedString = i18n.services.interpolator.interpolate(targetString, _objectSpread({}, data, {}, combinedTOpts), i18n.language);
105
105
  var ast = HTML.parse("<0>".concat(interpolatedString, "</0>"));
106
106
 
107
- function mapAST(reactNode, astNode) {
107
+ function renderInner(child, node, rootReactNode) {
108
+ var childs = getChildren(child);
109
+ var mappedChildren = mapAST(childs, node.children, rootReactNode);
110
+ return hasValidReactChildren(childs) && mappedChildren.length === 0 ? childs : mappedChildren;
111
+ }
112
+
113
+ function pushTranslatedJSX(child, inner, mem, i) {
114
+ if (child.dummy) child.children = inner;
115
+ mem.push(React.cloneElement(child, _objectSpread({}, child.props, {
116
+ key: i
117
+ }), inner));
118
+ }
119
+
120
+ function mapAST(reactNode, astNode, rootReactNode) {
108
121
  var reactNodes = getAsArray(reactNode);
109
122
  var astNodes = getAsArray(astNode);
110
123
  return astNodes.reduce(function (mem, node, i) {
111
124
  var translationContent = node.children && node.children[0] && node.children[0].content;
112
125
 
113
126
  if (node.type === 'tag') {
114
- var child = reactNodes[parseInt(node.name, 10)] || {};
127
+ var tmp = reactNodes[parseInt(node.name, 10)];
128
+ if (!tmp && rootReactNode.length === 1 && rootReactNode[0][node.name]) tmp = rootReactNode[0][node.name];
129
+ if (!tmp) tmp = {};
130
+ var child = Object.keys(node.attrs).length !== 0 ? mergeProps({
131
+ props: node.attrs
132
+ }, tmp) : tmp;
115
133
  var isElement = React.isValidElement(child);
134
+ var isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
135
+ var isEmptyTransWithHTML = emptyChildrenButNeedsHandling && _typeof(child) === 'object' && child.dummy && !isElement;
136
+ var isKnownComponent = _typeof(children) === 'object' && children !== null && Object.hasOwnProperty.call(children, node.name);
116
137
 
117
138
  if (typeof child === 'string') {
118
139
  mem.push(child);
119
- } else if (hasChildren(child)) {
120
- var childs = getChildren(child);
121
- var mappedChildren = mapAST(childs, node.children);
122
- var inner = hasValidReactChildren(childs) && mappedChildren.length === 0 ? childs : mappedChildren;
123
- if (child.dummy) child.children = inner; // needed on preact!
124
-
125
- mem.push(React.cloneElement(child, _objectSpread({}, child.props, {
126
- key: i
127
- }), inner));
128
- } else if (emptyChildrenButNeedsHandling && _typeof(child) === 'object' && child.dummy && !isElement) {
129
- // we have a empty Trans node (the dummy element) with a targetstring that contains html tags needing
130
- // conversion to react nodes
131
- // so we just need to map the inner stuff
132
- var _inner = mapAST(reactNodes
133
- /* wrong but we need something */
134
- , node.children);
140
+ } else if (hasChildren(child) || isValidTranslationWithChildren) {
141
+ var inner = renderInner(child, node, rootReactNode);
142
+ pushTranslatedJSX(child, inner, mem, i);
143
+ } else if (isEmptyTransWithHTML) {
144
+ var _inner = mapAST(reactNodes, node.children, rootReactNode);
135
145
 
136
146
  mem.push(React.cloneElement(child, _objectSpread({}, child.props, {
137
147
  key: i
138
148
  }), _inner));
139
149
  } else if (Number.isNaN(parseFloat(node.name))) {
140
- if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
150
+ if (isKnownComponent) {
151
+ var _inner2 = renderInner(child, node, rootReactNode);
152
+
153
+ pushTranslatedJSX(child, _inner2, mem, i);
154
+ } else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
141
155
  if (node.voidElement) {
142
156
  mem.push(React.createElement(node.name, {
143
157
  key: "".concat(node.name, "-").concat(i)
144
158
  }));
145
159
  } else {
146
- var _inner2 = mapAST(reactNodes
147
- /* wrong but we need something */
148
- , node.children);
160
+ var _inner3 = mapAST(reactNodes, node.children, rootReactNode);
149
161
 
150
162
  mem.push(React.createElement(node.name, {
151
163
  key: "".concat(node.name, "-").concat(i)
152
- }, _inner2));
164
+ }, _inner3));
153
165
  }
154
166
  } else if (node.voidElement) {
155
167
  mem.push("<".concat(node.name, " />"));
156
168
  } else {
157
- var _inner3 = mapAST(reactNodes
158
- /* wrong but we need something */
159
- , node.children);
169
+ var _inner4 = mapAST(reactNodes, node.children, rootReactNode);
160
170
 
161
- mem.push("<".concat(node.name, ">").concat(_inner3, "</").concat(node.name, ">"));
171
+ mem.push("<".concat(node.name, ">").concat(_inner4, "</").concat(node.name, ">"));
162
172
  }
163
173
  } else if (_typeof(child) === 'object' && !isElement) {
164
- var content = node.children[0] ? translationContent : null; // v1
165
- // as interpolation was done already we just have a regular content node
166
- // in the translation AST while having an object in reactNodes
167
- // -> push the content no need to interpolate again
168
-
174
+ var content = node.children[0] ? translationContent : null;
169
175
  if (content) mem.push(content);
170
176
  } else if (node.children.length === 1 && translationContent) {
171
- // If component does not have children, but translation - has
172
- // with this in component could be components={[<span class='make-beautiful'/>]} and in translation - 'some text <0>some highlighted message</0>'
173
177
  mem.push(React.cloneElement(child, _objectSpread({}, child.props, {
174
178
  key: i
175
179
  }), translationContent));
@@ -184,15 +188,12 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) {
184
188
 
185
189
  return mem;
186
190
  }, []);
187
- } // call mapAST with having react nodes nested into additional node like
188
- // we did for the string ast from translation
189
- // return the children of that extra node to get expected result
190
-
191
+ }
191
192
 
192
193
  var result = mapAST([{
193
194
  dummy: true,
194
195
  children: children
195
- }], ast);
196
+ }], ast, getAsArray(children || []));
196
197
  return getChildren(result[0]);
197
198
  }
198
199
 
@@ -210,9 +211,7 @@ export function Trans(_ref) {
210
211
  tFromProps = _ref.t,
211
212
  additionalProps = _objectWithoutProperties(_ref, ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t"]);
212
213
 
213
- var ReactI18nContext = useContext(I18nContext);
214
-
215
- var _ref2 = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {},
214
+ var _ref2 = useContext(I18nContext) || {},
216
215
  i18nFromContext = _ref2.i18n,
217
216
  defaultNSFromContext = _ref2.defaultNS;
218
217
 
@@ -229,11 +228,9 @@ export function Trans(_ref) {
229
228
 
230
229
  var reactI18nextOptions = _objectSpread({}, getDefaults(), {}, i18n.options && i18n.options.react);
231
230
 
232
- var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; // prepare having a namespace
233
-
234
231
  var namespaces = ns || t.ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
235
232
  namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
236
- var defaultValue = defaults || nodesToString('', children, 0, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue || i18nKey;
233
+ var defaultValue = defaults || nodesToString(children, reactI18nextOptions) || reactI18nextOptions.transEmptyNodeValue || i18nKey;
237
234
  var hashTransKey = reactI18nextOptions.hashTransKey;
238
235
  var key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
239
236
  var interpolationOverride = values ? {} : {
@@ -251,6 +248,7 @@ export function Trans(_ref) {
251
248
  });
252
249
 
253
250
  var translation = key ? t(key, combinedTOpts) : defaultValue;
254
- if (!useAsParent) return renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts);
255
- return React.createElement(useAsParent, additionalProps, renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts));
251
+ var content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts);
252
+ var useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
253
+ return useAsParent ? React.createElement(useAsParent, additionalProps, content) : content;
256
254
  }
@@ -10,22 +10,13 @@ import React from 'react';
10
10
  var defaultOptions = {
11
11
  bindI18n: 'languageChanged',
12
12
  bindI18nStore: '',
13
- // nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
14
13
  transEmptyNodeValue: '',
15
14
  transSupportBasicHtmlNodes: true,
16
15
  transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
17
- // hashTransKey: key => key // calculate a key for Trans component based on defaultValue
18
16
  useSuspense: true
19
17
  };
20
18
  var i18nInstance;
21
- var hasUsedI18nextProvider;
22
19
  export var I18nContext = React.createContext();
23
- export function usedI18nextProvider(used) {
24
- hasUsedI18nextProvider = used;
25
- }
26
- export function getHasUsedI18nextProvider() {
27
- return hasUsedI18nextProvider;
28
- }
29
20
  export function setDefaults() {
30
21
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
31
22
  defaultOptions = _objectSpread({}, defaultOptions, {}, options);
@@ -33,9 +24,7 @@ export function setDefaults() {
33
24
  export function getDefaults() {
34
25
  return defaultOptions;
35
26
  }
36
- export var ReportNamespaces =
37
- /*#__PURE__*/
38
- function () {
27
+ export var ReportNamespaces = function () {
39
28
  function ReportNamespaces() {
40
29
  _classCallCheck(this, ReportNamespaces);
41
30
 
@@ -86,17 +75,7 @@ export function composeInitialProps(ForComponent) {
86
75
  resolve(i18nInitialProps);
87
76
  }
88
77
  });
89
- }; // Avoid async for now - so we do not need to pull in regenerator
90
- // return async ctx => {
91
- // const componentsInitialProps = ForComponent.getInitialProps
92
- // ? await ForComponent.getInitialProps(ctx)
93
- // : {};
94
- // const i18nInitialProps = getInitialProps();
95
- // return {
96
- // ...componentsInitialProps,
97
- // ...i18nInitialProps,
98
- // };
99
- // };
78
+ };
100
79
  }
101
80
  export function getInitialProps() {
102
81
  var i18n = getI18n();
package/dist/es/useSSR.js CHANGED
@@ -1,21 +1,17 @@
1
1
  import { useContext } from 'react';
2
- import { getI18n, getHasUsedI18nextProvider, I18nContext } from './context';
2
+ import { getI18n, I18nContext } from './context';
3
3
  export function useSSR(initialI18nStore, initialLanguage) {
4
4
  var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
5
5
  var i18nFromProps = props.i18n;
6
- var ReactI18nContext = useContext(I18nContext);
7
6
 
8
- var _ref = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {},
7
+ var _ref = useContext(I18nContext) || {},
9
8
  i18nFromContext = _ref.i18n;
10
9
 
11
- var i18n = i18nFromProps || i18nFromContext || getI18n(); // opt out if is a cloned instance, eg. created by i18next-http-middleware on request
12
- // -> do not set initial stuff on server side
13
-
14
- if (i18n.options && i18n.options.isClone) return; // nextjs / SSR: getting data from next.js or other ssr stack
10
+ var i18n = i18nFromProps || i18nFromContext || getI18n();
11
+ if (i18n.options && i18n.options.isClone) return;
15
12
 
16
13
  if (initialI18nStore && !i18n.initializedStoreOnce) {
17
- i18n.services.resourceStore.data = initialI18nStore; // add namespaces to the config - so a languageChange call loads all namespaces needed
18
-
14
+ i18n.services.resourceStore.data = initialI18nStore;
19
15
  i18n.options.ns = Object.values(initialI18nStore).reduce(function (mem, lngResources) {
20
16
  Object.keys(lngResources).forEach(function (ns) {
21
17
  if (mem.indexOf(ns) < 0) mem.push(ns);
@@ -6,15 +6,13 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
6
6
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
7
7
 
8
8
  import { useState, useEffect, useContext, useRef } from 'react';
9
- import { getI18n, getDefaults, ReportNamespaces, getHasUsedI18nextProvider, I18nContext } from './context';
9
+ import { getI18n, getDefaults, ReportNamespaces, I18nContext } from './context';
10
10
  import { warnOnce, loadNamespaces, hasLoadedNamespace } from './utils';
11
11
  export function useTranslation(ns) {
12
12
  var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
13
- // assert we have the needed i18nInstance
14
13
  var i18nFromProps = props.i18n;
15
- var ReactI18nContext = useContext(I18nContext);
16
14
 
17
- var _ref = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {},
15
+ var _ref = useContext(I18nContext) || {},
18
16
  i18nFromContext = _ref.i18n,
19
17
  defaultNSFromContext = _ref.defaultNS;
20
18
 
@@ -37,16 +35,13 @@ export function useTranslation(ns) {
37
35
 
38
36
  var i18nOptions = _objectSpread({}, getDefaults(), {}, i18n.options.react, {}, props);
39
37
 
40
- var useSuspense = i18nOptions.useSuspense; // prepare having a namespace
41
-
38
+ var useSuspense = i18nOptions.useSuspense;
42
39
  var namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
43
- namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
44
-
45
- if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
46
-
40
+ namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
41
+ if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
47
42
  var ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(function (n) {
48
43
  return hasLoadedNamespace(n, i18n, i18nOptions);
49
- }); // binding t function to namespace (acts also as rerender trigger)
44
+ });
50
45
 
51
46
  function getT() {
52
47
  return {
@@ -57,15 +52,13 @@ export function useTranslation(ns) {
57
52
  var _useState = useState(getT()),
58
53
  _useState2 = _slicedToArray(_useState, 2),
59
54
  t = _useState2[0],
60
- setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
61
-
55
+ setT = _useState2[1];
62
56
 
63
57
  var isMounted = useRef(true);
64
58
  useEffect(function () {
65
59
  var bindI18n = i18nOptions.bindI18n,
66
60
  bindI18nStore = i18nOptions.bindI18nStore;
67
- isMounted.current = true; // if not ready and not using suspense load the namespaces
68
- // in side effect and do not call resetT if unmounted
61
+ isMounted.current = true;
69
62
 
70
63
  if (!ready && !useSuspense) {
71
64
  loadNamespaces(i18n, namespaces, function () {
@@ -75,12 +68,10 @@ export function useTranslation(ns) {
75
68
 
76
69
  function boundReset() {
77
70
  if (isMounted.current) setT(getT());
78
- } // bind events to trigger change, like languageChanged
79
-
71
+ }
80
72
 
81
73
  if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
82
- if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset); // unbinding on unmount
83
-
74
+ if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
84
75
  return function () {
85
76
  isMounted.current = false;
86
77
  if (bindI18n && i18n) bindI18n.split(' ').forEach(function (e) {
@@ -90,17 +81,13 @@ export function useTranslation(ns) {
90
81
  return i18n.store.off(e, boundReset);
91
82
  });
92
83
  };
93
- }, [namespaces.join()]); // re-run effect whenever list of namespaces changes
94
-
84
+ }, [namespaces.join()]);
95
85
  var ret = [t.t, i18n, ready];
96
86
  ret.t = t.t;
97
87
  ret.i18n = i18n;
98
- ret.ready = ready; // return hook stuff if ready
99
-
100
- if (ready) return ret; // not yet loaded namespaces -> load them -> and return if useSuspense option set false
101
-
102
- if (!ready && !useSuspense) return ret; // not yet loaded namespaces -> load them -> and trigger suspense
103
-
88
+ ret.ready = ready;
89
+ if (ready) return ret;
90
+ if (!ready && !useSuspense) return ret;
104
91
  throw new Promise(function (resolve) {
105
92
  loadNamespaces(i18n, namespaces, function () {
106
93
  if (isMounted.current) setT(getT());
package/dist/es/utils.js CHANGED
@@ -20,23 +20,13 @@ export function warnOnce() {
20
20
  if (typeof args[0] === 'string' && alreadyWarned[args[0]]) return;
21
21
  if (typeof args[0] === 'string') alreadyWarned[args[0]] = new Date();
22
22
  warn.apply(void 0, args);
23
- } // not needed right now
24
- //
25
- // export function deprecated(...args) {
26
- // if (process && process.env && (!process.env.NODE_ENV || process.env.NODE_ENV === 'development')) {
27
- // if (typeof args[0] === 'string') args[0] = `deprecation warning -> ${args[0]}`;
28
- // warnOnce(...args);
29
- // }
30
- // }
31
-
23
+ }
32
24
  export function loadNamespaces(i18n, ns, cb) {
33
25
  i18n.loadNamespaces(ns, function () {
34
- // delay ready if not yet initialized i18n instance
35
26
  if (i18n.isInitialized) {
36
27
  cb();
37
28
  } else {
38
29
  var initialized = function initialized() {
39
- // due to emitter removing issue in i18next we need to delay remove
40
30
  setTimeout(function () {
41
31
  i18n.off('initialized', initialized);
42
32
  }, 0);
@@ -57,24 +47,17 @@ export function hasLoadedNamespace(ns, i18n) {
57
47
 
58
48
  var lng = i18n.languages[0];
59
49
  var fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
60
- var lastLng = i18n.languages[i18n.languages.length - 1]; // we're in cimode so this shall pass
61
-
50
+ var lastLng = i18n.languages[i18n.languages.length - 1];
62
51
  if (lng.toLowerCase() === 'cimode') return true;
63
52
 
64
53
  var loadNotPending = function loadNotPending(l, n) {
65
54
  var loadState = i18n.services.backendConnector.state["".concat(l, "|").concat(n)];
66
55
  return loadState === -1 || loadState === 2;
67
- }; // bound to trigger on event languageChanging
68
- // so set ready to false while we are changing the language
69
- // and namespace pending (depends on having a backend)
70
-
71
-
72
- if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false; // loaded -> SUCCESS
73
-
74
- if (i18n.hasResourceBundle(lng, ns)) return true; // were not loading at all -> SEMI SUCCESS
75
-
76
- if (!i18n.services.backendConnector.backend) return true; // failed loading ns - but at least fallback is not pending -> SEMI SUCCESS
56
+ };
77
57
 
58
+ if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
59
+ if (i18n.hasResourceBundle(lng, ns)) return true;
60
+ if (!i18n.services.backendConnector.backend) return true;
78
61
  if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
79
62
  return false;
80
63
  }