react-i18next 14.0.0 → 14.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,869 +1,860 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactI18next = {}, global.React));
5
- })(this, (function (exports, React) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactI18next = {}, global.React));
5
+ })(this, (function (exports, react) { 'use strict';
6
6
 
7
- function _extends() {
8
- _extends = Object.assign ? Object.assign.bind() : function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
- for (var key in source) {
12
- if (Object.prototype.hasOwnProperty.call(source, key)) {
13
- target[key] = source[key];
14
- }
15
- }
16
- }
17
- return target;
18
- };
19
- return _extends.apply(this, arguments);
20
- }
7
+ function getDefaultExportFromCjs (x) {
8
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
9
+ }
21
10
 
22
- function getDefaultExportFromCjs (x) {
23
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
24
- }
11
+ var voidElements = {
12
+ "area": true,
13
+ "base": true,
14
+ "br": true,
15
+ "col": true,
16
+ "embed": true,
17
+ "hr": true,
18
+ "img": true,
19
+ "input": true,
20
+ "link": true,
21
+ "meta": true,
22
+ "param": true,
23
+ "source": true,
24
+ "track": true,
25
+ "wbr": true
26
+ };
25
27
 
26
- var voidElements = {
27
- "area": true,
28
- "base": true,
29
- "br": true,
30
- "col": true,
31
- "embed": true,
32
- "hr": true,
33
- "img": true,
34
- "input": true,
35
- "link": true,
36
- "meta": true,
37
- "param": true,
38
- "source": true,
39
- "track": true,
40
- "wbr": true
41
- };
28
+ var e = /*@__PURE__*/getDefaultExportFromCjs(voidElements);
42
29
 
43
- var e = /*@__PURE__*/getDefaultExportFromCjs(voidElements);
30
+ var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
31
+ function n(n) {
32
+ var r = {
33
+ type: "tag",
34
+ name: "",
35
+ voidElement: !1,
36
+ attrs: {},
37
+ children: []
38
+ },
39
+ i = n.match(/<\/?([^\s]+?)[/\s>]/);
40
+ if (i && (r.name = i[1], (e[i[1]] || "/" === n.charAt(n.length - 2)) && (r.voidElement = !0), r.name.startsWith("!--"))) {
41
+ var s = n.indexOf("--\x3e");
42
+ return {
43
+ type: "comment",
44
+ comment: -1 !== s ? n.slice(4, s) : ""
45
+ };
46
+ }
47
+ for (var a = new RegExp(t), c = null; null !== (c = a.exec(n));) if (c[0].trim()) if (c[1]) {
48
+ var o = c[1].trim(),
49
+ l = [o, ""];
50
+ o.indexOf("=") > -1 && (l = o.split("=")), r.attrs[l[0]] = l[1], a.lastIndex--;
51
+ } else c[2] && (r.attrs[c[2]] = c[3].trim().substring(1, c[3].length - 1));
52
+ return r;
53
+ }
54
+ var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,
55
+ i = /^\s*$/,
56
+ s = Object.create(null);
57
+ function a(e, t) {
58
+ switch (t.type) {
59
+ case "text":
60
+ return e + t.content;
61
+ case "tag":
62
+ return e += "<" + t.name + (t.attrs ? function (e) {
63
+ var t = [];
64
+ for (var n in e) t.push(n + '="' + e[n] + '"');
65
+ return t.length ? " " + t.join(" ") : "";
66
+ }(t.attrs) : "") + (t.voidElement ? "/>" : ">"), t.voidElement ? e : e + t.children.reduce(a, "") + "</" + t.name + ">";
67
+ case "comment":
68
+ return e + "\x3c!--" + t.comment + "--\x3e";
69
+ }
70
+ }
71
+ var c = {
72
+ parse: function (e, t) {
73
+ t || (t = {}), t.components || (t.components = s);
74
+ var a,
75
+ c = [],
76
+ o = [],
77
+ l = -1,
78
+ m = !1;
79
+ if (0 !== e.indexOf("<")) {
80
+ var u = e.indexOf("<");
81
+ c.push({
82
+ type: "text",
83
+ content: -1 === u ? e : e.substring(0, u)
84
+ });
85
+ }
86
+ return e.replace(r, function (r, s) {
87
+ if (m) {
88
+ if (r !== "</" + a.name + ">") return;
89
+ m = !1;
90
+ }
91
+ var u,
92
+ f = "/" !== r.charAt(1),
93
+ h = r.startsWith("\x3c!--"),
94
+ p = s + r.length,
95
+ d = e.charAt(p);
96
+ if (h) {
97
+ var v = n(r);
98
+ return l < 0 ? (c.push(v), c) : ((u = o[l]).children.push(v), c);
99
+ }
100
+ if (f && (l++, "tag" === (a = n(r)).type && t.components[a.name] && (a.type = "component", m = !0), a.voidElement || m || !d || "<" === d || a.children.push({
101
+ type: "text",
102
+ content: e.slice(p, e.indexOf("<", p))
103
+ }), 0 === l && c.push(a), (u = o[l - 1]) && u.children.push(a), o[l] = a), (!f || a.voidElement) && (l > -1 && (a.voidElement || a.name === r.slice(2, -1)) && (l--, a = -1 === l ? c : o[l]), !m && "<" !== d && d)) {
104
+ u = -1 === l ? c : o[l].children;
105
+ var x = e.indexOf("<", p),
106
+ g = e.slice(p, -1 === x ? void 0 : x);
107
+ i.test(g) && (g = " "), (x > -1 && l + u.length >= 0 || " " !== g) && u.push({
108
+ type: "text",
109
+ content: g
110
+ });
111
+ }
112
+ }), c;
113
+ },
114
+ stringify: function (e) {
115
+ return e.reduce(function (e, t) {
116
+ return e + a("", t);
117
+ }, "");
118
+ }
119
+ };
44
120
 
45
- var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
46
- function n(n) {
47
- var r = {
48
- type: "tag",
49
- name: "",
50
- voidElement: !1,
51
- attrs: {},
52
- children: []
53
- },
54
- i = n.match(/<\/?([^\s]+?)[/\s>]/);
55
- if (i && (r.name = i[1], (e[i[1]] || "/" === n.charAt(n.length - 2)) && (r.voidElement = !0), r.name.startsWith("!--"))) {
56
- var s = n.indexOf("--\x3e");
57
- return {
58
- type: "comment",
59
- comment: -1 !== s ? n.slice(4, s) : ""
60
- };
61
- }
62
- for (var a = new RegExp(t), c = null; null !== (c = a.exec(n));) if (c[0].trim()) if (c[1]) {
63
- var o = c[1].trim(),
64
- l = [o, ""];
65
- o.indexOf("=") > -1 && (l = o.split("=")), r.attrs[l[0]] = l[1], a.lastIndex--;
66
- } else c[2] && (r.attrs[c[2]] = c[3].trim().substring(1, c[3].length - 1));
67
- return r;
68
- }
69
- var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g,
70
- i = /^\s*$/,
71
- s = Object.create(null);
72
- function a(e, t) {
73
- switch (t.type) {
74
- case "text":
75
- return e + t.content;
76
- case "tag":
77
- return e += "<" + t.name + (t.attrs ? function (e) {
78
- var t = [];
79
- for (var n in e) t.push(n + '="' + e[n] + '"');
80
- return t.length ? " " + t.join(" ") : "";
81
- }(t.attrs) : "") + (t.voidElement ? "/>" : ">"), t.voidElement ? e : e + t.children.reduce(a, "") + "</" + t.name + ">";
82
- case "comment":
83
- return e + "\x3c!--" + t.comment + "--\x3e";
84
- }
85
- }
86
- var c = {
87
- parse: function (e, t) {
88
- t || (t = {}), t.components || (t.components = s);
89
- var a,
90
- c = [],
91
- o = [],
92
- l = -1,
93
- m = !1;
94
- if (0 !== e.indexOf("<")) {
95
- var u = e.indexOf("<");
96
- c.push({
97
- type: "text",
98
- content: -1 === u ? e : e.substring(0, u)
99
- });
100
- }
101
- return e.replace(r, function (r, s) {
102
- if (m) {
103
- if (r !== "</" + a.name + ">") return;
104
- m = !1;
105
- }
106
- var u,
107
- f = "/" !== r.charAt(1),
108
- h = r.startsWith("\x3c!--"),
109
- p = s + r.length,
110
- d = e.charAt(p);
111
- if (h) {
112
- var v = n(r);
113
- return l < 0 ? (c.push(v), c) : ((u = o[l]).children.push(v), c);
114
- }
115
- if (f && (l++, "tag" === (a = n(r)).type && t.components[a.name] && (a.type = "component", m = !0), a.voidElement || m || !d || "<" === d || a.children.push({
116
- type: "text",
117
- content: e.slice(p, e.indexOf("<", p))
118
- }), 0 === l && c.push(a), (u = o[l - 1]) && u.children.push(a), o[l] = a), (!f || a.voidElement) && (l > -1 && (a.voidElement || a.name === r.slice(2, -1)) && (l--, a = -1 === l ? c : o[l]), !m && "<" !== d && d)) {
119
- u = -1 === l ? c : o[l].children;
120
- var x = e.indexOf("<", p),
121
- g = e.slice(p, -1 === x ? void 0 : x);
122
- i.test(g) && (g = " "), (x > -1 && l + u.length >= 0 || " " !== g) && u.push({
123
- type: "text",
124
- content: g
125
- });
126
- }
127
- }), c;
128
- },
129
- stringify: function (e) {
130
- return e.reduce(function (e, t) {
131
- return e + a("", t);
132
- }, "");
133
- }
134
- };
121
+ function warn() {
122
+ if (console && console.warn) {
123
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
124
+ args[_key] = arguments[_key];
125
+ }
126
+ if (typeof args[0] === 'string') args[0] = `react-i18next:: ${args[0]}`;
127
+ console.warn(...args);
128
+ }
129
+ }
130
+ const alreadyWarned = {};
131
+ function warnOnce() {
132
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
133
+ args[_key2] = arguments[_key2];
134
+ }
135
+ if (typeof args[0] === 'string' && alreadyWarned[args[0]]) return;
136
+ if (typeof args[0] === 'string') alreadyWarned[args[0]] = new Date();
137
+ warn(...args);
138
+ }
139
+ const loadedClb = (i18n, cb) => () => {
140
+ if (i18n.isInitialized) {
141
+ cb();
142
+ } else {
143
+ const initialized = () => {
144
+ setTimeout(() => {
145
+ i18n.off('initialized', initialized);
146
+ }, 0);
147
+ cb();
148
+ };
149
+ i18n.on('initialized', initialized);
150
+ }
151
+ };
152
+ function loadNamespaces(i18n, ns, cb) {
153
+ i18n.loadNamespaces(ns, loadedClb(i18n, cb));
154
+ }
155
+ function loadLanguages(i18n, lng, ns, cb) {
156
+ if (typeof ns === 'string') ns = [ns];
157
+ ns.forEach(n => {
158
+ if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
159
+ });
160
+ i18n.loadLanguages(lng, loadedClb(i18n, cb));
161
+ }
162
+ function oldI18nextHasLoadedNamespace(ns, i18n) {
163
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
164
+ const lng = i18n.languages[0];
165
+ const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
166
+ const lastLng = i18n.languages[i18n.languages.length - 1];
167
+ if (lng.toLowerCase() === 'cimode') return true;
168
+ const loadNotPending = (l, n) => {
169
+ const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
170
+ return loadState === -1 || loadState === 2;
171
+ };
172
+ if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
173
+ if (i18n.hasResourceBundle(lng, ns)) return true;
174
+ if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
175
+ if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
176
+ return false;
177
+ }
178
+ function hasLoadedNamespace(ns, i18n) {
179
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
180
+ if (!i18n.languages || !i18n.languages.length) {
181
+ warnOnce('i18n.languages were undefined or empty', i18n.languages);
182
+ return true;
183
+ }
184
+ const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
185
+ if (!isNewerI18next) {
186
+ return oldI18nextHasLoadedNamespace(ns, i18n, options);
187
+ }
188
+ return i18n.hasLoadedNamespace(ns, {
189
+ lng: options.lng,
190
+ precheck: (i18nInstance, loadNotPending) => {
191
+ if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
192
+ }
193
+ });
194
+ }
195
+ function getDisplayName(Component) {
196
+ return Component.displayName || Component.name || (typeof Component === 'string' && Component.length > 0 ? Component : 'Unknown');
197
+ }
135
198
 
136
- function warn() {
137
- if (console && console.warn) {
138
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
139
- args[_key] = arguments[_key];
140
- }
141
- if (typeof args[0] === 'string') args[0] = `react-i18next:: ${args[0]}`;
142
- console.warn(...args);
143
- }
144
- }
145
- const alreadyWarned = {};
146
- function warnOnce() {
147
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
148
- args[_key2] = arguments[_key2];
149
- }
150
- if (typeof args[0] === 'string' && alreadyWarned[args[0]]) return;
151
- if (typeof args[0] === 'string') alreadyWarned[args[0]] = new Date();
152
- warn(...args);
153
- }
154
- const loadedClb = (i18n, cb) => () => {
155
- if (i18n.isInitialized) {
156
- cb();
157
- } else {
158
- const initialized = () => {
159
- setTimeout(() => {
160
- i18n.off('initialized', initialized);
161
- }, 0);
162
- cb();
163
- };
164
- i18n.on('initialized', initialized);
165
- }
166
- };
167
- function loadNamespaces(i18n, ns, cb) {
168
- i18n.loadNamespaces(ns, loadedClb(i18n, cb));
169
- }
170
- function loadLanguages(i18n, lng, ns, cb) {
171
- if (typeof ns === 'string') ns = [ns];
172
- ns.forEach(n => {
173
- if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
174
- });
175
- i18n.loadLanguages(lng, loadedClb(i18n, cb));
176
- }
177
- function oldI18nextHasLoadedNamespace(ns, i18n) {
178
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
179
- const lng = i18n.languages[0];
180
- const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
181
- const lastLng = i18n.languages[i18n.languages.length - 1];
182
- if (lng.toLowerCase() === 'cimode') return true;
183
- const loadNotPending = (l, n) => {
184
- const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
185
- return loadState === -1 || loadState === 2;
186
- };
187
- if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
188
- if (i18n.hasResourceBundle(lng, ns)) return true;
189
- if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
190
- if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
191
- return false;
192
- }
193
- function hasLoadedNamespace(ns, i18n) {
194
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
195
- if (!i18n.languages || !i18n.languages.length) {
196
- warnOnce('i18n.languages were undefined or empty', i18n.languages);
197
- return true;
198
- }
199
- const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
200
- if (!isNewerI18next) {
201
- return oldI18nextHasLoadedNamespace(ns, i18n, options);
202
- }
203
- return i18n.hasLoadedNamespace(ns, {
204
- lng: options.lng,
205
- precheck: (i18nInstance, loadNotPending) => {
206
- if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
207
- }
208
- });
209
- }
210
- function getDisplayName(Component) {
211
- return Component.displayName || Component.name || (typeof Component === 'string' && Component.length > 0 ? Component : 'Unknown');
212
- }
199
+ const matchHtmlEntity = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g;
200
+ const htmlEntities = {
201
+ '&amp;': '&',
202
+ '&#38;': '&',
203
+ '&lt;': '<',
204
+ '&#60;': '<',
205
+ '&gt;': '>',
206
+ '&#62;': '>',
207
+ '&apos;': "'",
208
+ '&#39;': "'",
209
+ '&quot;': '"',
210
+ '&#34;': '"',
211
+ '&nbsp;': ' ',
212
+ '&#160;': ' ',
213
+ '&copy;': '©',
214
+ '&#169;': '©',
215
+ '&reg;': '®',
216
+ '&#174;': '®',
217
+ '&hellip;': '…',
218
+ '&#8230;': '…',
219
+ '&#x2F;': '/',
220
+ '&#47;': '/'
221
+ };
222
+ const unescapeHtmlEntity = m => htmlEntities[m];
223
+ const unescape = text => text.replace(matchHtmlEntity, unescapeHtmlEntity);
213
224
 
214
- const matchHtmlEntity = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g;
215
- const htmlEntities = {
216
- '&amp;': '&',
217
- '&#38;': '&',
218
- '&lt;': '<',
219
- '&#60;': '<',
220
- '&gt;': '>',
221
- '&#62;': '>',
222
- '&apos;': "'",
223
- '&#39;': "'",
224
- '&quot;': '"',
225
- '&#34;': '"',
226
- '&nbsp;': ' ',
227
- '&#160;': ' ',
228
- '&copy;': '©',
229
- '&#169;': '©',
230
- '&reg;': '®',
231
- '&#174;': '®',
232
- '&hellip;': '…',
233
- '&#8230;': '…',
234
- '&#x2F;': '/',
235
- '&#47;': '/'
236
- };
237
- const unescapeHtmlEntity = m => htmlEntities[m];
238
- const unescape = text => text.replace(matchHtmlEntity, unescapeHtmlEntity);
225
+ let defaultOptions = {
226
+ bindI18n: 'languageChanged',
227
+ bindI18nStore: '',
228
+ transEmptyNodeValue: '',
229
+ transSupportBasicHtmlNodes: true,
230
+ transWrapTextNodes: '',
231
+ transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
232
+ useSuspense: true,
233
+ unescape
234
+ };
235
+ function setDefaults() {
236
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
237
+ defaultOptions = {
238
+ ...defaultOptions,
239
+ ...options
240
+ };
241
+ }
242
+ function getDefaults() {
243
+ return defaultOptions;
244
+ }
239
245
 
240
- let defaultOptions = {
241
- bindI18n: 'languageChanged',
242
- bindI18nStore: '',
243
- transEmptyNodeValue: '',
244
- transSupportBasicHtmlNodes: true,
245
- transWrapTextNodes: '',
246
- transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
247
- useSuspense: true,
248
- unescape
249
- };
250
- function setDefaults() {
251
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
252
- defaultOptions = {
253
- ...defaultOptions,
254
- ...options
255
- };
256
- }
257
- function getDefaults() {
258
- return defaultOptions;
259
- }
246
+ let i18nInstance;
247
+ function setI18n(instance) {
248
+ i18nInstance = instance;
249
+ }
250
+ function getI18n() {
251
+ return i18nInstance;
252
+ }
260
253
 
261
- let i18nInstance;
262
- function setI18n(instance) {
263
- i18nInstance = instance;
264
- }
265
- function getI18n() {
266
- return i18nInstance;
267
- }
254
+ function hasChildren(node, checkLength) {
255
+ if (!node) return false;
256
+ const base = node.props ? node.props.children : node.children;
257
+ if (checkLength) return base.length > 0;
258
+ return !!base;
259
+ }
260
+ function getChildren(node) {
261
+ if (!node) return [];
262
+ const children = node.props ? node.props.children : node.children;
263
+ return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children;
264
+ }
265
+ function hasValidReactChildren(children) {
266
+ if (Object.prototype.toString.call(children) !== '[object Array]') return false;
267
+ return children.every(child => react.isValidElement(child));
268
+ }
269
+ function getAsArray(data) {
270
+ return Array.isArray(data) ? data : [data];
271
+ }
272
+ function mergeProps(source, target) {
273
+ const newTarget = {
274
+ ...target
275
+ };
276
+ newTarget.props = Object.assign(source.props, target.props);
277
+ return newTarget;
278
+ }
279
+ function nodesToString(children, i18nOptions) {
280
+ if (!children) return '';
281
+ let stringNode = '';
282
+ const childrenArray = getAsArray(children);
283
+ const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
284
+ childrenArray.forEach((child, childIndex) => {
285
+ if (typeof child === 'string') {
286
+ stringNode += `${child}`;
287
+ } else if (react.isValidElement(child)) {
288
+ const childPropsCount = Object.keys(child.props).length;
289
+ const shouldKeepChild = keepArray.indexOf(child.type) > -1;
290
+ const childChildren = child.props.children;
291
+ if (!childChildren && shouldKeepChild && childPropsCount === 0) {
292
+ stringNode += `<${child.type}/>`;
293
+ } else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
294
+ stringNode += `<${childIndex}></${childIndex}>`;
295
+ } else if (child.props.i18nIsDynamicList) {
296
+ stringNode += `<${childIndex}></${childIndex}>`;
297
+ } else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
298
+ stringNode += `<${child.type}>${childChildren}</${child.type}>`;
299
+ } else {
300
+ const content = nodesToString(childChildren, i18nOptions);
301
+ stringNode += `<${childIndex}>${content}</${childIndex}>`;
302
+ }
303
+ } else if (child === null) {
304
+ warn(`Trans: the passed in value is invalid - seems you passed in a null child.`);
305
+ } else if (typeof child === 'object') {
306
+ const {
307
+ format,
308
+ ...clone
309
+ } = child;
310
+ const keys = Object.keys(clone);
311
+ if (keys.length === 1) {
312
+ const value = format ? `${keys[0]}, ${format}` : keys[0];
313
+ stringNode += `{{${value}}}`;
314
+ } else {
315
+ warn(`react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.`, child);
316
+ }
317
+ } else {
318
+ 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);
319
+ }
320
+ });
321
+ return stringNode;
322
+ }
323
+ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) {
324
+ if (targetString === '') return [];
325
+ const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
326
+ const emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.map(keep => `<${keep}`).join('|')).test(targetString);
327
+ if (!children && !emptyChildrenButNeedsHandling && !shouldUnescape) return [targetString];
328
+ const data = {};
329
+ function getData(childs) {
330
+ const childrenArray = getAsArray(childs);
331
+ childrenArray.forEach(child => {
332
+ if (typeof child === 'string') return;
333
+ if (hasChildren(child)) getData(getChildren(child));else if (typeof child === 'object' && !react.isValidElement(child)) Object.assign(data, child);
334
+ });
335
+ }
336
+ getData(children);
337
+ const ast = c.parse(`<0>${targetString}</0>`);
338
+ const opts = {
339
+ ...data,
340
+ ...combinedTOpts
341
+ };
342
+ function renderInner(child, node, rootReactNode) {
343
+ const childs = getChildren(child);
344
+ const mappedChildren = mapAST(childs, node.children, rootReactNode);
345
+ return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props && child.props.i18nIsDynamicList ? childs : mappedChildren;
346
+ }
347
+ function pushTranslatedJSX(child, inner, mem, i, isVoid) {
348
+ if (child.dummy) {
349
+ child.children = inner;
350
+ mem.push(react.cloneElement(child, {
351
+ key: i
352
+ }, isVoid ? undefined : inner));
353
+ } else {
354
+ mem.push(...react.Children.map([child], c => {
355
+ const props = {
356
+ ...c.props
357
+ };
358
+ delete props.i18nIsDynamicList;
359
+ return react.createElement(c.type, {
360
+ ...props,
361
+ key: i,
362
+ ref: c.ref
363
+ }, isVoid ? null : inner);
364
+ }));
365
+ }
366
+ }
367
+ function mapAST(reactNode, astNode, rootReactNode) {
368
+ const reactNodes = getAsArray(reactNode);
369
+ const astNodes = getAsArray(astNode);
370
+ return astNodes.reduce((mem, node, i) => {
371
+ const translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
372
+ if (node.type === 'tag') {
373
+ let tmp = reactNodes[parseInt(node.name, 10)];
374
+ if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
375
+ if (!tmp) tmp = {};
376
+ const child = Object.keys(node.attrs).length !== 0 ? mergeProps({
377
+ props: node.attrs
378
+ }, tmp) : tmp;
379
+ const isElement = react.isValidElement(child);
380
+ const isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
381
+ const isEmptyTransWithHTML = emptyChildrenButNeedsHandling && typeof child === 'object' && child.dummy && !isElement;
382
+ const isKnownComponent = typeof children === 'object' && children !== null && Object.hasOwnProperty.call(children, node.name);
383
+ if (typeof child === 'string') {
384
+ const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
385
+ mem.push(value);
386
+ } else if (hasChildren(child) || isValidTranslationWithChildren) {
387
+ const inner = renderInner(child, node, rootReactNode);
388
+ pushTranslatedJSX(child, inner, mem, i);
389
+ } else if (isEmptyTransWithHTML) {
390
+ const inner = mapAST(reactNodes, node.children, rootReactNode);
391
+ pushTranslatedJSX(child, inner, mem, i);
392
+ } else if (Number.isNaN(parseFloat(node.name))) {
393
+ if (isKnownComponent) {
394
+ const inner = renderInner(child, node, rootReactNode);
395
+ pushTranslatedJSX(child, inner, mem, i, node.voidElement);
396
+ } else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
397
+ if (node.voidElement) {
398
+ mem.push(react.createElement(node.name, {
399
+ key: `${node.name}-${i}`
400
+ }));
401
+ } else {
402
+ const inner = mapAST(reactNodes, node.children, rootReactNode);
403
+ mem.push(react.createElement(node.name, {
404
+ key: `${node.name}-${i}`
405
+ }, inner));
406
+ }
407
+ } else if (node.voidElement) {
408
+ mem.push(`<${node.name} />`);
409
+ } else {
410
+ const inner = mapAST(reactNodes, node.children, rootReactNode);
411
+ mem.push(`<${node.name}>${inner}</${node.name}>`);
412
+ }
413
+ } else if (typeof child === 'object' && !isElement) {
414
+ const content = node.children[0] ? translationContent : null;
415
+ if (content) mem.push(content);
416
+ } else {
417
+ pushTranslatedJSX(child, translationContent, mem, i, node.children.length !== 1 || !translationContent);
418
+ }
419
+ } else if (node.type === 'text') {
420
+ const wrapTextNodes = i18nOptions.transWrapTextNodes;
421
+ const content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
422
+ if (wrapTextNodes) {
423
+ mem.push(react.createElement(wrapTextNodes, {
424
+ key: `${node.name}-${i}`
425
+ }, content));
426
+ } else {
427
+ mem.push(content);
428
+ }
429
+ }
430
+ return mem;
431
+ }, []);
432
+ }
433
+ const result = mapAST([{
434
+ dummy: true,
435
+ children: children || []
436
+ }], ast, getAsArray(children || []));
437
+ return getChildren(result[0]);
438
+ }
439
+ function Trans$1(_ref) {
440
+ let {
441
+ children,
442
+ count,
443
+ parent,
444
+ i18nKey,
445
+ context,
446
+ tOptions = {},
447
+ values,
448
+ defaults,
449
+ components,
450
+ ns,
451
+ i18n: i18nFromProps,
452
+ t: tFromProps,
453
+ shouldUnescape,
454
+ ...additionalProps
455
+ } = _ref;
456
+ const i18n = i18nFromProps || getI18n();
457
+ if (!i18n) {
458
+ warnOnce('You will need to pass in an i18next instance by using i18nextReactModule');
459
+ return children;
460
+ }
461
+ const t = tFromProps || i18n.t.bind(i18n) || (k => k);
462
+ if (context) tOptions.context = context;
463
+ const reactI18nextOptions = {
464
+ ...getDefaults(),
465
+ ...(i18n.options && i18n.options.react)
466
+ };
467
+ let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
468
+ namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
469
+ const nodeAsString = nodesToString(children, reactI18nextOptions);
470
+ const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
471
+ const {
472
+ hashTransKey
473
+ } = reactI18nextOptions;
474
+ const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
475
+ if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
476
+ values = values && Object.keys(values).length > 0 ? {
477
+ ...values,
478
+ ...i18n.options.interpolation.defaultVariables
479
+ } : {
480
+ ...i18n.options.interpolation.defaultVariables
481
+ };
482
+ }
483
+ const interpolationOverride = values ? tOptions.interpolation : {
484
+ interpolation: {
485
+ ...tOptions.interpolation,
486
+ prefix: '#$?',
487
+ suffix: '?$#'
488
+ }
489
+ };
490
+ const combinedTOpts = {
491
+ ...tOptions,
492
+ count,
493
+ ...values,
494
+ ...interpolationOverride,
495
+ defaultValue,
496
+ ns: namespaces
497
+ };
498
+ const translation = key ? t(key, combinedTOpts) : defaultValue;
499
+ if (components) {
500
+ Object.keys(components).forEach(c => {
501
+ const comp = components[c];
502
+ if (typeof comp.type === 'function' || !comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return;
503
+ function Componentized() {
504
+ return react.createElement(react.Fragment, null, comp);
505
+ }
506
+ components[c] = react.createElement(Componentized);
507
+ });
508
+ }
509
+ const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
510
+ const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
511
+ return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
512
+ }
268
513
 
269
- function hasChildren(node, checkLength) {
270
- if (!node) return false;
271
- const base = node.props ? node.props.children : node.children;
272
- if (checkLength) return base.length > 0;
273
- return !!base;
274
- }
275
- function getChildren(node) {
276
- if (!node) return [];
277
- const children = node.props ? node.props.children : node.children;
278
- return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children;
279
- }
280
- function hasValidReactChildren(children) {
281
- if (Object.prototype.toString.call(children) !== '[object Array]') return false;
282
- return children.every(child => React.isValidElement(child));
283
- }
284
- function getAsArray(data) {
285
- return Array.isArray(data) ? data : [data];
286
- }
287
- function mergeProps(source, target) {
288
- const newTarget = {
289
- ...target
290
- };
291
- newTarget.props = Object.assign(source.props, target.props);
292
- return newTarget;
293
- }
294
- function nodesToString(children, i18nOptions) {
295
- if (!children) return '';
296
- let stringNode = '';
297
- const childrenArray = getAsArray(children);
298
- const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
299
- childrenArray.forEach((child, childIndex) => {
300
- if (typeof child === 'string') {
301
- stringNode += `${child}`;
302
- } else if (React.isValidElement(child)) {
303
- const childPropsCount = Object.keys(child.props).length;
304
- const shouldKeepChild = keepArray.indexOf(child.type) > -1;
305
- const childChildren = child.props.children;
306
- if (!childChildren && shouldKeepChild && childPropsCount === 0) {
307
- stringNode += `<${child.type}/>`;
308
- } else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
309
- stringNode += `<${childIndex}></${childIndex}>`;
310
- } else if (child.props.i18nIsDynamicList) {
311
- stringNode += `<${childIndex}></${childIndex}>`;
312
- } else if (shouldKeepChild && childPropsCount === 1 && typeof childChildren === 'string') {
313
- stringNode += `<${child.type}>${childChildren}</${child.type}>`;
314
- } else {
315
- const content = nodesToString(childChildren, i18nOptions);
316
- stringNode += `<${childIndex}>${content}</${childIndex}>`;
317
- }
318
- } else if (child === null) {
319
- warn(`Trans: the passed in value is invalid - seems you passed in a null child.`);
320
- } else if (typeof child === 'object') {
321
- const {
322
- format,
323
- ...clone
324
- } = child;
325
- const keys = Object.keys(clone);
326
- if (keys.length === 1) {
327
- const value = format ? `${keys[0]}, ${format}` : keys[0];
328
- stringNode += `{{${value}}}`;
329
- } else {
330
- warn(`react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.`, child);
331
- }
332
- } else {
333
- 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);
334
- }
335
- });
336
- return stringNode;
337
- }
338
- function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) {
339
- if (targetString === '') return [];
340
- const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
341
- const emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.map(keep => `<${keep}`).join('|')).test(targetString);
342
- if (!children && !emptyChildrenButNeedsHandling && !shouldUnescape) return [targetString];
343
- const data = {};
344
- function getData(childs) {
345
- const childrenArray = getAsArray(childs);
346
- childrenArray.forEach(child => {
347
- if (typeof child === 'string') return;
348
- if (hasChildren(child)) getData(getChildren(child));else if (typeof child === 'object' && !React.isValidElement(child)) Object.assign(data, child);
349
- });
350
- }
351
- getData(children);
352
- const ast = c.parse(`<0>${targetString}</0>`);
353
- const opts = {
354
- ...data,
355
- ...combinedTOpts
356
- };
357
- function renderInner(child, node, rootReactNode) {
358
- const childs = getChildren(child);
359
- const mappedChildren = mapAST(childs, node.children, rootReactNode);
360
- return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props && child.props.i18nIsDynamicList ? childs : mappedChildren;
361
- }
362
- function pushTranslatedJSX(child, inner, mem, i, isVoid) {
363
- if (child.dummy) {
364
- child.children = inner;
365
- mem.push(React.cloneElement(child, {
366
- key: i
367
- }, isVoid ? undefined : inner));
368
- } else {
369
- mem.push(...React.Children.map([child], c => {
370
- const props = {
371
- ...c.props
372
- };
373
- delete props.i18nIsDynamicList;
374
- return React.createElement(c.type, _extends({}, props, {
375
- key: i,
376
- ref: c.ref
377
- }, isVoid ? {} : {
378
- children: inner
379
- }));
380
- }));
381
- }
382
- }
383
- function mapAST(reactNode, astNode, rootReactNode) {
384
- const reactNodes = getAsArray(reactNode);
385
- const astNodes = getAsArray(astNode);
386
- return astNodes.reduce((mem, node, i) => {
387
- const translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
388
- if (node.type === 'tag') {
389
- let tmp = reactNodes[parseInt(node.name, 10)];
390
- if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
391
- if (!tmp) tmp = {};
392
- const child = Object.keys(node.attrs).length !== 0 ? mergeProps({
393
- props: node.attrs
394
- }, tmp) : tmp;
395
- const isElement = React.isValidElement(child);
396
- const isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
397
- const isEmptyTransWithHTML = emptyChildrenButNeedsHandling && typeof child === 'object' && child.dummy && !isElement;
398
- const isKnownComponent = typeof children === 'object' && children !== null && Object.hasOwnProperty.call(children, node.name);
399
- if (typeof child === 'string') {
400
- const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
401
- mem.push(value);
402
- } else if (hasChildren(child) || isValidTranslationWithChildren) {
403
- const inner = renderInner(child, node, rootReactNode);
404
- pushTranslatedJSX(child, inner, mem, i);
405
- } else if (isEmptyTransWithHTML) {
406
- const inner = mapAST(reactNodes, node.children, rootReactNode);
407
- pushTranslatedJSX(child, inner, mem, i);
408
- } else if (Number.isNaN(parseFloat(node.name))) {
409
- if (isKnownComponent) {
410
- const inner = renderInner(child, node, rootReactNode);
411
- pushTranslatedJSX(child, inner, mem, i, node.voidElement);
412
- } else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
413
- if (node.voidElement) {
414
- mem.push(React.createElement(node.name, {
415
- key: `${node.name}-${i}`
416
- }));
417
- } else {
418
- const inner = mapAST(reactNodes, node.children, rootReactNode);
419
- mem.push(React.createElement(node.name, {
420
- key: `${node.name}-${i}`
421
- }, inner));
422
- }
423
- } else if (node.voidElement) {
424
- mem.push(`<${node.name} />`);
425
- } else {
426
- const inner = mapAST(reactNodes, node.children, rootReactNode);
427
- mem.push(`<${node.name}>${inner}</${node.name}>`);
428
- }
429
- } else if (typeof child === 'object' && !isElement) {
430
- const content = node.children[0] ? translationContent : null;
431
- if (content) mem.push(content);
432
- } else {
433
- pushTranslatedJSX(child, translationContent, mem, i, node.children.length !== 1 || !translationContent);
434
- }
435
- } else if (node.type === 'text') {
436
- const wrapTextNodes = i18nOptions.transWrapTextNodes;
437
- const content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
438
- if (wrapTextNodes) {
439
- mem.push(React.createElement(wrapTextNodes, {
440
- key: `${node.name}-${i}`
441
- }, content));
442
- } else {
443
- mem.push(content);
444
- }
445
- }
446
- return mem;
447
- }, []);
448
- }
449
- const result = mapAST([{
450
- dummy: true,
451
- children: children || []
452
- }], ast, getAsArray(children || []));
453
- return getChildren(result[0]);
454
- }
455
- function Trans$1(_ref) {
456
- let {
457
- children,
458
- count,
459
- parent,
460
- i18nKey,
461
- context,
462
- tOptions = {},
463
- values,
464
- defaults,
465
- components,
466
- ns,
467
- i18n: i18nFromProps,
468
- t: tFromProps,
469
- shouldUnescape,
470
- ...additionalProps
471
- } = _ref;
472
- const i18n = i18nFromProps || getI18n();
473
- if (!i18n) {
474
- warnOnce('You will need to pass in an i18next instance by using i18nextReactModule');
475
- return children;
476
- }
477
- const t = tFromProps || i18n.t.bind(i18n) || (k => k);
478
- if (context) tOptions.context = context;
479
- const reactI18nextOptions = {
480
- ...getDefaults(),
481
- ...(i18n.options && i18n.options.react)
482
- };
483
- let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
484
- namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
485
- const nodeAsString = nodesToString(children, reactI18nextOptions);
486
- const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
487
- const {
488
- hashTransKey
489
- } = reactI18nextOptions;
490
- const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
491
- if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
492
- values = values && Object.keys(values).length > 0 ? {
493
- ...values,
494
- ...i18n.options.interpolation.defaultVariables
495
- } : {
496
- ...i18n.options.interpolation.defaultVariables
497
- };
498
- }
499
- const interpolationOverride = values ? tOptions.interpolation : {
500
- interpolation: {
501
- ...tOptions.interpolation,
502
- prefix: '#$?',
503
- suffix: '?$#'
504
- }
505
- };
506
- const combinedTOpts = {
507
- ...tOptions,
508
- count,
509
- ...values,
510
- ...interpolationOverride,
511
- defaultValue,
512
- ns: namespaces
513
- };
514
- const translation = key ? t(key, combinedTOpts) : defaultValue;
515
- if (components) {
516
- Object.keys(components).forEach(c => {
517
- const comp = components[c];
518
- if (typeof comp.type === 'function' || !comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return;
519
- function Componentized() {
520
- return React.createElement(React.Fragment, null, comp);
521
- }
522
- components[c] = React.createElement(Componentized, null);
523
- });
524
- }
525
- const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
526
- const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
527
- return useAsParent ? React.createElement(useAsParent, additionalProps, content) : content;
528
- }
514
+ const initReactI18next = {
515
+ type: '3rdParty',
516
+ init(instance) {
517
+ setDefaults(instance.options.react);
518
+ setI18n(instance);
519
+ }
520
+ };
529
521
 
530
- const initReactI18next = {
531
- type: '3rdParty',
532
- init(instance) {
533
- setDefaults(instance.options.react);
534
- setI18n(instance);
535
- }
536
- };
522
+ const I18nContext = react.createContext();
523
+ class ReportNamespaces {
524
+ constructor() {
525
+ this.usedNamespaces = {};
526
+ }
527
+ addUsedNamespaces(namespaces) {
528
+ namespaces.forEach(ns => {
529
+ if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
530
+ });
531
+ }
532
+ getUsedNamespaces() {
533
+ return Object.keys(this.usedNamespaces);
534
+ }
535
+ }
536
+ function composeInitialProps(ForComponent) {
537
+ return ctx => new Promise(resolve => {
538
+ const i18nInitialProps = getInitialProps();
539
+ if (ForComponent.getInitialProps) {
540
+ ForComponent.getInitialProps(ctx).then(componentsInitialProps => {
541
+ resolve({
542
+ ...componentsInitialProps,
543
+ ...i18nInitialProps
544
+ });
545
+ });
546
+ } else {
547
+ resolve(i18nInitialProps);
548
+ }
549
+ });
550
+ }
551
+ function getInitialProps() {
552
+ const i18n = getI18n();
553
+ const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
554
+ const ret = {};
555
+ const initialI18nStore = {};
556
+ i18n.languages.forEach(l => {
557
+ initialI18nStore[l] = {};
558
+ namespaces.forEach(ns => {
559
+ initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
560
+ });
561
+ });
562
+ ret.initialI18nStore = initialI18nStore;
563
+ ret.initialLanguage = i18n.language;
564
+ return ret;
565
+ }
537
566
 
538
- const I18nContext = React.createContext();
539
- class ReportNamespaces {
540
- constructor() {
541
- this.usedNamespaces = {};
542
- }
543
- addUsedNamespaces(namespaces) {
544
- namespaces.forEach(ns => {
545
- if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
546
- });
547
- }
548
- getUsedNamespaces() {
549
- return Object.keys(this.usedNamespaces);
550
- }
551
- }
552
- function composeInitialProps(ForComponent) {
553
- return ctx => new Promise(resolve => {
554
- const i18nInitialProps = getInitialProps();
555
- if (ForComponent.getInitialProps) {
556
- ForComponent.getInitialProps(ctx).then(componentsInitialProps => {
557
- resolve({
558
- ...componentsInitialProps,
559
- ...i18nInitialProps
560
- });
561
- });
562
- } else {
563
- resolve(i18nInitialProps);
564
- }
565
- });
566
- }
567
- function getInitialProps() {
568
- const i18n = getI18n();
569
- const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
570
- const ret = {};
571
- const initialI18nStore = {};
572
- i18n.languages.forEach(l => {
573
- initialI18nStore[l] = {};
574
- namespaces.forEach(ns => {
575
- initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
576
- });
577
- });
578
- ret.initialI18nStore = initialI18nStore;
579
- ret.initialLanguage = i18n.language;
580
- return ret;
581
- }
567
+ function Trans(_ref) {
568
+ let {
569
+ children,
570
+ count,
571
+ parent,
572
+ i18nKey,
573
+ context,
574
+ tOptions = {},
575
+ values,
576
+ defaults,
577
+ components,
578
+ ns,
579
+ i18n: i18nFromProps,
580
+ t: tFromProps,
581
+ shouldUnescape,
582
+ ...additionalProps
583
+ } = _ref;
584
+ const {
585
+ i18n: i18nFromContext,
586
+ defaultNS: defaultNSFromContext
587
+ } = react.useContext(I18nContext) || {};
588
+ const i18n = i18nFromProps || i18nFromContext || getI18n();
589
+ const t = tFromProps || i18n && i18n.t.bind(i18n);
590
+ return Trans$1({
591
+ children,
592
+ count,
593
+ parent,
594
+ i18nKey,
595
+ context,
596
+ tOptions,
597
+ values,
598
+ defaults,
599
+ components,
600
+ ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS,
601
+ i18n,
602
+ t: tFromProps,
603
+ shouldUnescape,
604
+ ...additionalProps
605
+ });
606
+ }
582
607
 
583
- function Trans(_ref) {
584
- let {
585
- children,
586
- count,
587
- parent,
588
- i18nKey,
589
- context,
590
- tOptions = {},
591
- values,
592
- defaults,
593
- components,
594
- ns,
595
- i18n: i18nFromProps,
596
- t: tFromProps,
597
- shouldUnescape,
598
- ...additionalProps
599
- } = _ref;
600
- const {
601
- i18n: i18nFromContext,
602
- defaultNS: defaultNSFromContext
603
- } = React.useContext(I18nContext) || {};
604
- const i18n = i18nFromProps || i18nFromContext || getI18n();
605
- const t = tFromProps || i18n && i18n.t.bind(i18n);
606
- return Trans$1({
607
- children,
608
- count,
609
- parent,
610
- i18nKey,
611
- context,
612
- tOptions,
613
- values,
614
- defaults,
615
- components,
616
- ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS,
617
- i18n,
618
- t: tFromProps,
619
- shouldUnescape,
620
- ...additionalProps
621
- });
622
- }
608
+ const usePrevious = (value, ignore) => {
609
+ const ref = react.useRef();
610
+ react.useEffect(() => {
611
+ ref.current = ignore ? ref.current : value;
612
+ }, [value, ignore]);
613
+ return ref.current;
614
+ };
615
+ function alwaysNewT(i18n, language, namespace, keyPrefix) {
616
+ return i18n.getFixedT(language, namespace, keyPrefix);
617
+ }
618
+ function useMemoizedT(i18n, language, namespace, keyPrefix) {
619
+ return react.useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
620
+ }
621
+ function useTranslation(ns) {
622
+ let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
623
+ const {
624
+ i18n: i18nFromProps
625
+ } = props;
626
+ const {
627
+ i18n: i18nFromContext,
628
+ defaultNS: defaultNSFromContext
629
+ } = react.useContext(I18nContext) || {};
630
+ const i18n = i18nFromProps || i18nFromContext || getI18n();
631
+ if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
632
+ if (!i18n) {
633
+ warnOnce('You will need to pass in an i18next instance by using initReactI18next');
634
+ const notReadyT = (k, optsOrDefaultValue) => {
635
+ if (typeof optsOrDefaultValue === 'string') return optsOrDefaultValue;
636
+ if (optsOrDefaultValue && typeof optsOrDefaultValue === 'object' && typeof optsOrDefaultValue.defaultValue === 'string') return optsOrDefaultValue.defaultValue;
637
+ return Array.isArray(k) ? k[k.length - 1] : k;
638
+ };
639
+ const retNotReady = [notReadyT, {}, false];
640
+ retNotReady.t = notReadyT;
641
+ retNotReady.i18n = {};
642
+ retNotReady.ready = false;
643
+ return retNotReady;
644
+ }
645
+ if (i18n.options.react && i18n.options.react.wait !== undefined) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
646
+ const i18nOptions = {
647
+ ...getDefaults(),
648
+ ...i18n.options.react,
649
+ ...props
650
+ };
651
+ const {
652
+ useSuspense,
653
+ keyPrefix
654
+ } = i18nOptions;
655
+ let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
656
+ namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
657
+ if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
658
+ const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
659
+ const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
660
+ const getT = () => memoGetT;
661
+ const [t, setT] = react.useState(getT);
662
+ let joinedNS = namespaces.join();
663
+ if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
664
+ const previousJoinedNS = usePrevious(joinedNS);
665
+ const isMounted = react.useRef(true);
666
+ react.useEffect(() => {
667
+ const {
668
+ bindI18n,
669
+ bindI18nStore
670
+ } = i18nOptions;
671
+ isMounted.current = true;
672
+ if (!ready && !useSuspense) {
673
+ if (props.lng) {
674
+ loadLanguages(i18n, props.lng, namespaces, () => {
675
+ if (isMounted.current) setT(getT);
676
+ });
677
+ } else {
678
+ loadNamespaces(i18n, namespaces, () => {
679
+ if (isMounted.current) {
680
+ setT(() => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix));
681
+ }
682
+ });
683
+ }
684
+ }
685
+ if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
686
+ setT(getT);
687
+ }
688
+ function boundReset() {
689
+ if (isMounted.current) setT(getT);
690
+ }
691
+ if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
692
+ if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
693
+ return () => {
694
+ isMounted.current = false;
695
+ if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
696
+ if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
697
+ };
698
+ }, [i18n, joinedNS]);
699
+ const isInitial = react.useRef(true);
700
+ react.useEffect(() => {
701
+ if (isMounted.current && !isInitial.current) {
702
+ setT(getT);
703
+ }
704
+ isInitial.current = false;
705
+ }, [i18n, keyPrefix]);
706
+ const ret = [t, i18n, ready];
707
+ ret.t = t;
708
+ ret.i18n = i18n;
709
+ ret.ready = ready;
710
+ if (ready) return ret;
711
+ if (!ready && !useSuspense) return ret;
712
+ throw new Promise(resolve => {
713
+ if (props.lng) {
714
+ loadLanguages(i18n, props.lng, namespaces, () => resolve());
715
+ } else {
716
+ loadNamespaces(i18n, namespaces, () => resolve());
717
+ }
718
+ });
719
+ }
623
720
 
624
- const usePrevious = (value, ignore) => {
625
- const ref = React.useRef();
626
- React.useEffect(() => {
627
- ref.current = ignore ? ref.current : value;
628
- }, [value, ignore]);
629
- return ref.current;
630
- };
631
- function useTranslation(ns) {
632
- let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
633
- const {
634
- i18n: i18nFromProps
635
- } = props;
636
- const {
637
- i18n: i18nFromContext,
638
- defaultNS: defaultNSFromContext
639
- } = React.useContext(I18nContext) || {};
640
- const i18n = i18nFromProps || i18nFromContext || getI18n();
641
- if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
642
- if (!i18n) {
643
- warnOnce('You will need to pass in an i18next instance by using initReactI18next');
644
- const notReadyT = (k, optsOrDefaultValue) => {
645
- if (typeof optsOrDefaultValue === 'string') return optsOrDefaultValue;
646
- if (optsOrDefaultValue && typeof optsOrDefaultValue === 'object' && typeof optsOrDefaultValue.defaultValue === 'string') return optsOrDefaultValue.defaultValue;
647
- return Array.isArray(k) ? k[k.length - 1] : k;
648
- };
649
- const retNotReady = [notReadyT, {}, false];
650
- retNotReady.t = notReadyT;
651
- retNotReady.i18n = {};
652
- retNotReady.ready = false;
653
- return retNotReady;
654
- }
655
- if (i18n.options.react && i18n.options.react.wait !== undefined) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
656
- const i18nOptions = {
657
- ...getDefaults(),
658
- ...i18n.options.react,
659
- ...props
660
- };
661
- const {
662
- useSuspense,
663
- keyPrefix
664
- } = i18nOptions;
665
- let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
666
- namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];
667
- if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
668
- const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
669
- function getT() {
670
- return i18n.getFixedT(props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
671
- }
672
- const [t, setT] = React.useState(getT);
673
- let joinedNS = namespaces.join();
674
- if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
675
- const previousJoinedNS = usePrevious(joinedNS);
676
- const isMounted = React.useRef(true);
677
- React.useEffect(() => {
678
- const {
679
- bindI18n,
680
- bindI18nStore
681
- } = i18nOptions;
682
- isMounted.current = true;
683
- if (!ready && !useSuspense) {
684
- if (props.lng) {
685
- loadLanguages(i18n, props.lng, namespaces, () => {
686
- if (isMounted.current) setT(getT);
687
- });
688
- } else {
689
- loadNamespaces(i18n, namespaces, () => {
690
- if (isMounted.current) setT(getT);
691
- });
692
- }
693
- }
694
- if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
695
- setT(getT);
696
- }
697
- function boundReset() {
698
- if (isMounted.current) setT(getT);
699
- }
700
- if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
701
- if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
702
- return () => {
703
- isMounted.current = false;
704
- if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
705
- if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
706
- };
707
- }, [i18n, joinedNS]);
708
- const isInitial = React.useRef(true);
709
- React.useEffect(() => {
710
- if (isMounted.current && !isInitial.current) {
711
- setT(getT);
712
- }
713
- isInitial.current = false;
714
- }, [i18n, keyPrefix]);
715
- const ret = [t, i18n, ready];
716
- ret.t = t;
717
- ret.i18n = i18n;
718
- ret.ready = ready;
719
- if (ready) return ret;
720
- if (!ready && !useSuspense) return ret;
721
- throw new Promise(resolve => {
722
- if (props.lng) {
723
- loadLanguages(i18n, props.lng, namespaces, () => resolve());
724
- } else {
725
- loadNamespaces(i18n, namespaces, () => resolve());
726
- }
727
- });
728
- }
721
+ function withTranslation(ns) {
722
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
723
+ return function Extend(WrappedComponent) {
724
+ function I18nextWithTranslation(_ref) {
725
+ let {
726
+ forwardedRef,
727
+ ...rest
728
+ } = _ref;
729
+ const [t, i18n, ready] = useTranslation(ns, {
730
+ ...rest,
731
+ keyPrefix: options.keyPrefix
732
+ });
733
+ const passDownProps = {
734
+ ...rest,
735
+ t,
736
+ i18n,
737
+ tReady: ready
738
+ };
739
+ if (options.withRef && forwardedRef) {
740
+ passDownProps.ref = forwardedRef;
741
+ } else if (!options.withRef && forwardedRef) {
742
+ passDownProps.forwardedRef = forwardedRef;
743
+ }
744
+ return react.createElement(WrappedComponent, passDownProps);
745
+ }
746
+ I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
747
+ I18nextWithTranslation.WrappedComponent = WrappedComponent;
748
+ const forwardRef = (props, ref) => react.createElement(I18nextWithTranslation, Object.assign({}, props, {
749
+ forwardedRef: ref
750
+ }));
751
+ return options.withRef ? react.forwardRef(forwardRef) : I18nextWithTranslation;
752
+ };
753
+ }
729
754
 
730
- function withTranslation(ns) {
731
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
732
- return function Extend(WrappedComponent) {
733
- function I18nextWithTranslation(_ref) {
734
- let {
735
- forwardedRef,
736
- ...rest
737
- } = _ref;
738
- const [t, i18n, ready] = useTranslation(ns, {
739
- ...rest,
740
- keyPrefix: options.keyPrefix
741
- });
742
- const passDownProps = {
743
- ...rest,
744
- t,
745
- i18n,
746
- tReady: ready
747
- };
748
- if (options.withRef && forwardedRef) {
749
- passDownProps.ref = forwardedRef;
750
- } else if (!options.withRef && forwardedRef) {
751
- passDownProps.forwardedRef = forwardedRef;
752
- }
753
- return React.createElement(WrappedComponent, passDownProps);
754
- }
755
- I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
756
- I18nextWithTranslation.WrappedComponent = WrappedComponent;
757
- const forwardRef = (props, ref) => React.createElement(I18nextWithTranslation, Object.assign({}, props, {
758
- forwardedRef: ref
759
- }));
760
- return options.withRef ? React.forwardRef(forwardRef) : I18nextWithTranslation;
761
- };
762
- }
755
+ function Translation(props) {
756
+ const {
757
+ ns,
758
+ children,
759
+ ...options
760
+ } = props;
761
+ const [t, i18n, ready] = useTranslation(ns, options);
762
+ return children(t, {
763
+ i18n,
764
+ lng: i18n.language
765
+ }, ready);
766
+ }
763
767
 
764
- function Translation(props) {
765
- const {
766
- ns,
767
- children,
768
- ...options
769
- } = props;
770
- const [t, i18n, ready] = useTranslation(ns, options);
771
- return children(t, {
772
- i18n,
773
- lng: i18n.language
774
- }, ready);
775
- }
768
+ function I18nextProvider(_ref) {
769
+ let {
770
+ i18n,
771
+ defaultNS,
772
+ children
773
+ } = _ref;
774
+ const value = react.useMemo(() => ({
775
+ i18n,
776
+ defaultNS
777
+ }), [i18n, defaultNS]);
778
+ return react.createElement(I18nContext.Provider, {
779
+ value
780
+ }, children);
781
+ }
776
782
 
777
- function I18nextProvider(_ref) {
778
- let {
779
- i18n,
780
- defaultNS,
781
- children
782
- } = _ref;
783
- const value = React.useMemo(() => ({
784
- i18n,
785
- defaultNS
786
- }), [i18n, defaultNS]);
787
- return React.createElement(I18nContext.Provider, {
788
- value
789
- }, children);
790
- }
783
+ function useSSR(initialI18nStore, initialLanguage) {
784
+ let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
785
+ const {
786
+ i18n: i18nFromProps
787
+ } = props;
788
+ const {
789
+ i18n: i18nFromContext
790
+ } = react.useContext(I18nContext) || {};
791
+ const i18n = i18nFromProps || i18nFromContext || getI18n();
792
+ if (i18n.options && i18n.options.isClone) return;
793
+ if (initialI18nStore && !i18n.initializedStoreOnce) {
794
+ i18n.services.resourceStore.data = initialI18nStore;
795
+ i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
796
+ Object.keys(lngResources).forEach(ns => {
797
+ if (mem.indexOf(ns) < 0) mem.push(ns);
798
+ });
799
+ return mem;
800
+ }, i18n.options.ns);
801
+ i18n.initializedStoreOnce = true;
802
+ i18n.isInitialized = true;
803
+ }
804
+ if (initialLanguage && !i18n.initializedLanguageOnce) {
805
+ i18n.changeLanguage(initialLanguage);
806
+ i18n.initializedLanguageOnce = true;
807
+ }
808
+ }
791
809
 
792
- function useSSR(initialI18nStore, initialLanguage) {
793
- let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
794
- const {
795
- i18n: i18nFromProps
796
- } = props;
797
- const {
798
- i18n: i18nFromContext
799
- } = React.useContext(I18nContext) || {};
800
- const i18n = i18nFromProps || i18nFromContext || getI18n();
801
- if (i18n.options && i18n.options.isClone) return;
802
- if (initialI18nStore && !i18n.initializedStoreOnce) {
803
- i18n.services.resourceStore.data = initialI18nStore;
804
- i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
805
- Object.keys(lngResources).forEach(ns => {
806
- if (mem.indexOf(ns) < 0) mem.push(ns);
807
- });
808
- return mem;
809
- }, i18n.options.ns);
810
- i18n.initializedStoreOnce = true;
811
- i18n.isInitialized = true;
812
- }
813
- if (initialLanguage && !i18n.initializedLanguageOnce) {
814
- i18n.changeLanguage(initialLanguage);
815
- i18n.initializedLanguageOnce = true;
816
- }
817
- }
810
+ function withSSR() {
811
+ return function Extend(WrappedComponent) {
812
+ function I18nextWithSSR(_ref) {
813
+ let {
814
+ initialI18nStore,
815
+ initialLanguage,
816
+ ...rest
817
+ } = _ref;
818
+ useSSR(initialI18nStore, initialLanguage);
819
+ return react.createElement(WrappedComponent, {
820
+ ...rest
821
+ });
822
+ }
823
+ I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
824
+ I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
825
+ I18nextWithSSR.WrappedComponent = WrappedComponent;
826
+ return I18nextWithSSR;
827
+ };
828
+ }
818
829
 
819
- function withSSR() {
820
- return function Extend(WrappedComponent) {
821
- function I18nextWithSSR(_ref) {
822
- let {
823
- initialI18nStore,
824
- initialLanguage,
825
- ...rest
826
- } = _ref;
827
- useSSR(initialI18nStore, initialLanguage);
828
- return React.createElement(WrappedComponent, {
829
- ...rest
830
- });
831
- }
832
- I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
833
- I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;
834
- I18nextWithSSR.WrappedComponent = WrappedComponent;
835
- return I18nextWithSSR;
836
- };
837
- }
830
+ const date = () => '';
831
+ const time = () => '';
832
+ const number = () => '';
833
+ const select = () => '';
834
+ const plural = () => '';
835
+ const selectOrdinal = () => '';
838
836
 
839
- const date = () => '';
840
- const time = () => '';
841
- const number = () => '';
842
- const select = () => '';
843
- const plural = () => '';
844
- const selectOrdinal = () => '';
845
-
846
- exports.I18nContext = I18nContext;
847
- exports.I18nextProvider = I18nextProvider;
848
- exports.Trans = Trans;
849
- exports.TransWithoutContext = Trans$1;
850
- exports.Translation = Translation;
851
- exports.composeInitialProps = composeInitialProps;
852
- exports.date = date;
853
- exports.getDefaults = getDefaults;
854
- exports.getI18n = getI18n;
855
- exports.getInitialProps = getInitialProps;
856
- exports.initReactI18next = initReactI18next;
857
- exports.number = number;
858
- exports.plural = plural;
859
- exports.select = select;
860
- exports.selectOrdinal = selectOrdinal;
861
- exports.setDefaults = setDefaults;
862
- exports.setI18n = setI18n;
863
- exports.time = time;
864
- exports.useSSR = useSSR;
865
- exports.useTranslation = useTranslation;
866
- exports.withSSR = withSSR;
867
- exports.withTranslation = withTranslation;
837
+ exports.I18nContext = I18nContext;
838
+ exports.I18nextProvider = I18nextProvider;
839
+ exports.Trans = Trans;
840
+ exports.TransWithoutContext = Trans$1;
841
+ exports.Translation = Translation;
842
+ exports.composeInitialProps = composeInitialProps;
843
+ exports.date = date;
844
+ exports.getDefaults = getDefaults;
845
+ exports.getI18n = getI18n;
846
+ exports.getInitialProps = getInitialProps;
847
+ exports.initReactI18next = initReactI18next;
848
+ exports.number = number;
849
+ exports.plural = plural;
850
+ exports.select = select;
851
+ exports.selectOrdinal = selectOrdinal;
852
+ exports.setDefaults = setDefaults;
853
+ exports.setI18n = setI18n;
854
+ exports.time = time;
855
+ exports.useSSR = useSSR;
856
+ exports.useTranslation = useTranslation;
857
+ exports.withSSR = withSSR;
858
+ exports.withTranslation = withTranslation;
868
859
 
869
860
  }));