@andre1502/react-utilities 1.0.6 → 1.0.7

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.
Files changed (202) hide show
  1. package/README.md +35 -51
  2. package/dist/EnvironmentEnum-CXTWJxNo.js +10 -0
  3. package/dist/EnvironmentEnum-CXTWJxNo.js.map +1 -0
  4. package/dist/Utils-CJ7ureSW.js +14 -0
  5. package/dist/Utils-CJ7ureSW.js.map +1 -0
  6. package/dist/browser-ponyfill-C-HVh7ck.js +321 -0
  7. package/dist/browser-ponyfill-C-HVh7ck.js.map +1 -0
  8. package/dist/chunk-vKJrgz-R.js +16 -0
  9. package/dist/config-cli-BIF4xuoO.js +1491 -0
  10. package/dist/config-cli-BIF4xuoO.js.map +1 -0
  11. package/dist/config-cli.js +3 -229
  12. package/dist/enums/CurrencySymbolEnum.d.ts +6 -5
  13. package/dist/enums/DeviceEnum.d.ts +5 -4
  14. package/dist/enums/EnvironmentEnum.d.ts +6 -5
  15. package/dist/format-BgweN3o5.js +65549 -0
  16. package/dist/format-BgweN3o5.js.map +1 -0
  17. package/dist/format.d.ts +26 -26
  18. package/dist/format.js +2 -55
  19. package/dist/hooks-oAipbPQ2.js +78 -0
  20. package/dist/hooks-oAipbPQ2.js.map +1 -0
  21. package/dist/hooks.js +2 -86
  22. package/dist/i18n-D1jKJPI0.js +1941 -0
  23. package/dist/i18n-D1jKJPI0.js.map +1 -0
  24. package/dist/i18n.js +4 -28
  25. package/dist/index-cli.d.ts +0 -1
  26. package/dist/index-cli.js +3 -28
  27. package/dist/index-rn.js +6 -39
  28. package/dist/index.js +7 -61
  29. package/dist/sentry-rn-DZwIel7y.js +743 -0
  30. package/dist/sentry-rn-DZwIel7y.js.map +1 -0
  31. package/dist/sentry-rn.js +2 -28
  32. package/dist/sentry.js +39 -26
  33. package/dist/sentry.js.map +1 -1
  34. package/dist/utils.js +45 -37
  35. package/dist/utils.js.map +1 -1
  36. package/package.json +56 -86
  37. package/src/Config/GoogleAuth.ts +1 -1
  38. package/src/Sentry/React.ts +2 -4
  39. package/src/Sentry/ReactNative.ts +2 -4
  40. package/src/enums/CurrencySymbolEnum.ts +8 -5
  41. package/src/enums/DeviceEnum.ts +6 -4
  42. package/src/enums/EnvironmentEnum.ts +8 -5
  43. package/src/format.ts +32 -32
  44. package/src/index-cli.ts +0 -1
  45. package/dist/Config/Config.js +0 -249
  46. package/dist/Config/Config.js.map +0 -1
  47. package/dist/Config/GoogleAuth.js +0 -68
  48. package/dist/Config/GoogleAuth.js.map +0 -1
  49. package/dist/Config/Locales.js +0 -62
  50. package/dist/Config/Locales.js.map +0 -1
  51. package/dist/Config/Output.js +0 -36
  52. package/dist/Config/Output.js.map +0 -1
  53. package/dist/Config/Sitemap.js +0 -72
  54. package/dist/Config/Sitemap.js.map +0 -1
  55. package/dist/EnvironmentEnum-BjXsfSRZ.js +0 -9
  56. package/dist/EnvironmentEnum-BjXsfSRZ.js.map +0 -1
  57. package/dist/EnvironmentEnum-UcQ6Il1O.js +0 -9
  58. package/dist/EnvironmentEnum-UcQ6Il1O.js.map +0 -1
  59. package/dist/Format/NumberFormat.js +0 -77
  60. package/dist/Format/NumberFormat.js.map +0 -1
  61. package/dist/Format/NumberParser.js +0 -51
  62. package/dist/Format/NumberParser.js.map +0 -1
  63. package/dist/Hooks/useDevice.js +0 -47
  64. package/dist/Hooks/useDevice.js.map +0 -1
  65. package/dist/Hooks/useSound.js +0 -47
  66. package/dist/Hooks/useSound.js.map +0 -1
  67. package/dist/Hooks/useWebSocket.js +0 -120
  68. package/dist/Hooks/useWebSocket.js.map +0 -1
  69. package/dist/I18n/I18n.js +0 -134
  70. package/dist/I18n/I18n.js.map +0 -1
  71. package/dist/NumberFormat-CvvBWhHc.js +0 -89
  72. package/dist/NumberFormat-CvvBWhHc.js.map +0 -1
  73. package/dist/NumberFormat-glmpbk7E.js +0 -94
  74. package/dist/NumberFormat-glmpbk7E.js.map +0 -1
  75. package/dist/React-BaJ1KfGF.js +0 -87
  76. package/dist/React-BaJ1KfGF.js.map +0 -1
  77. package/dist/React-qUl0CBmE.js +0 -109
  78. package/dist/React-qUl0CBmE.js.map +0 -1
  79. package/dist/ReactNative-B4gt8nBr.js +0 -1790
  80. package/dist/ReactNative-B4gt8nBr.js.map +0 -1
  81. package/dist/ReactNative-CDMfQFfN.js +0 -1812
  82. package/dist/ReactNative-CDMfQFfN.js.map +0 -1
  83. package/dist/Sentry/Build.d.ts +0 -9
  84. package/dist/Sentry/Build.js +0 -87
  85. package/dist/Sentry/Build.js.map +0 -1
  86. package/dist/Sentry/React.js +0 -103
  87. package/dist/Sentry/React.js.map +0 -1
  88. package/dist/Sentry/ReactNative.js +0 -113
  89. package/dist/Sentry/ReactNative.js.map +0 -1
  90. package/dist/Sentry/Utils.js +0 -24
  91. package/dist/Sentry/Utils.js.map +0 -1
  92. package/dist/Utils/Array.js +0 -26
  93. package/dist/Utils/Array.js.map +0 -1
  94. package/dist/Utils/Files.js +0 -51
  95. package/dist/Utils/Files.js.map +0 -1
  96. package/dist/Utils/Pagination.js +0 -32
  97. package/dist/Utils/Pagination.js.map +0 -1
  98. package/dist/Utils/Utils.js +0 -101
  99. package/dist/Utils/Utils.js.map +0 -1
  100. package/dist/Utils-BC1fDy39.js +0 -74
  101. package/dist/Utils-BC1fDy39.js.map +0 -1
  102. package/dist/Utils-Cq948gfa.js +0 -20
  103. package/dist/Utils-Cq948gfa.js.map +0 -1
  104. package/dist/Utils-D7KRF8VU.js +0 -64
  105. package/dist/Utils-D7KRF8VU.js.map +0 -1
  106. package/dist/Utils-Dilye04y.js +0 -22
  107. package/dist/Utils-Dilye04y.js.map +0 -1
  108. package/dist/config-cli.cjs +0 -532
  109. package/dist/config-cli.cjs.map +0 -1
  110. package/dist/config-cli.js.map +0 -1
  111. package/dist/config-cli.mjs +0 -500
  112. package/dist/config-cli.mjs.map +0 -1
  113. package/dist/enums/CurrencySymbolEnum.js +0 -15
  114. package/dist/enums/CurrencySymbolEnum.js.map +0 -1
  115. package/dist/enums/DeviceEnum.js +0 -12
  116. package/dist/enums/DeviceEnum.js.map +0 -1
  117. package/dist/enums/EnvironmentEnum.js +0 -13
  118. package/dist/enums/EnvironmentEnum.js.map +0 -1
  119. package/dist/format.cjs +0 -43
  120. package/dist/format.cjs.map +0 -1
  121. package/dist/format.js.map +0 -1
  122. package/dist/format.mjs +0 -30
  123. package/dist/format.mjs.map +0 -1
  124. package/dist/hooks.cjs +0 -13
  125. package/dist/hooks.cjs.map +0 -1
  126. package/dist/hooks.js.map +0 -1
  127. package/dist/hooks.mjs +0 -4
  128. package/dist/hooks.mjs.map +0 -1
  129. package/dist/i18n.cjs +0 -3039
  130. package/dist/i18n.cjs.map +0 -1
  131. package/dist/i18n.js.map +0 -1
  132. package/dist/i18n.mjs +0 -3032
  133. package/dist/i18n.mjs.map +0 -1
  134. package/dist/index-cli.cjs +0 -31
  135. package/dist/index-cli.cjs.map +0 -1
  136. package/dist/index-cli.js.map +0 -1
  137. package/dist/index-cli.mjs +0 -14
  138. package/dist/index-cli.mjs.map +0 -1
  139. package/dist/index-rn.cjs +0 -57
  140. package/dist/index-rn.cjs.map +0 -1
  141. package/dist/index-rn.js.map +0 -1
  142. package/dist/index-rn.mjs +0 -36
  143. package/dist/index-rn.mjs.map +0 -1
  144. package/dist/index.cjs +0 -79
  145. package/dist/index.cjs.map +0 -1
  146. package/dist/index.js.map +0 -1
  147. package/dist/index.mjs +0 -40
  148. package/dist/index.mjs.map +0 -1
  149. package/dist/interfaces/Config/ConfigOptions.js +0 -6
  150. package/dist/interfaces/Config/ConfigOptions.js.map +0 -1
  151. package/dist/interfaces/Config/EntryOptions.js +0 -6
  152. package/dist/interfaces/Config/EntryOptions.js.map +0 -1
  153. package/dist/interfaces/Config/OutputMap.js +0 -6
  154. package/dist/interfaces/Config/OutputMap.js.map +0 -1
  155. package/dist/interfaces/Config/OutputOptions.js +0 -6
  156. package/dist/interfaces/Config/OutputOptions.js.map +0 -1
  157. package/dist/interfaces/Config/SitemapMap.js +0 -6
  158. package/dist/interfaces/Config/SitemapMap.js.map +0 -1
  159. package/dist/interfaces/Config/StringMap.js +0 -6
  160. package/dist/interfaces/Config/StringMap.js.map +0 -1
  161. package/dist/interfaces/Format/FormatOptions.js +0 -6
  162. package/dist/interfaces/Format/FormatOptions.js.map +0 -1
  163. package/dist/interfaces/Sentry/InitOptions.js +0 -6
  164. package/dist/interfaces/Sentry/InitOptions.js.map +0 -1
  165. package/dist/interfaces/Sentry/InitOptionsRN.js +0 -6
  166. package/dist/interfaces/Sentry/InitOptionsRN.js.map +0 -1
  167. package/dist/interfaces/Sentry/SourceMapOptions.d.ts +0 -7
  168. package/dist/interfaces/Sentry/SourceMapOptions.js +0 -6
  169. package/dist/interfaces/Sentry/SourceMapOptions.js.map +0 -1
  170. package/dist/sentry-cli.cjs +0 -119
  171. package/dist/sentry-cli.cjs.map +0 -1
  172. package/dist/sentry-cli.d.ts +0 -9
  173. package/dist/sentry-cli.js +0 -49
  174. package/dist/sentry-cli.js.map +0 -1
  175. package/dist/sentry-cli.mjs +0 -98
  176. package/dist/sentry-cli.mjs.map +0 -1
  177. package/dist/sentry-rn.cjs +0 -11
  178. package/dist/sentry-rn.cjs.map +0 -1
  179. package/dist/sentry-rn.js.map +0 -1
  180. package/dist/sentry-rn.mjs +0 -4
  181. package/dist/sentry-rn.mjs.map +0 -1
  182. package/dist/sentry.cjs +0 -11
  183. package/dist/sentry.cjs.map +0 -1
  184. package/dist/sentry.mjs +0 -4
  185. package/dist/sentry.mjs.map +0 -1
  186. package/dist/types/Config/OptionType.js +0 -6
  187. package/dist/types/Config/OptionType.js.map +0 -1
  188. package/dist/types/Format/OptionType.js +0 -6
  189. package/dist/types/Format/OptionType.js.map +0 -1
  190. package/dist/types/Sentry/OptionType.js +0 -6
  191. package/dist/types/Sentry/OptionType.js.map +0 -1
  192. package/dist/useWebSocket-B_B6kWXM.js +0 -167
  193. package/dist/useWebSocket-B_B6kWXM.js.map +0 -1
  194. package/dist/useWebSocket-YQWaiBz9.js +0 -172
  195. package/dist/useWebSocket-YQWaiBz9.js.map +0 -1
  196. package/dist/utils.cjs +0 -59
  197. package/dist/utils.cjs.map +0 -1
  198. package/dist/utils.mjs +0 -43
  199. package/dist/utils.mjs.map +0 -1
  200. package/src/Sentry/Build.ts +0 -67
  201. package/src/interfaces/Sentry/SourceMapOptions.ts +0 -7
  202. package/src/sentry-cli.ts +0 -16
package/dist/i18n.mjs DELETED
@@ -1,3032 +0,0 @@
1
- export { E as EnvironmentEnum } from './EnvironmentEnum-UcQ6Il1O.js';
2
- import { g as getV } from './Utils-D7KRF8VU.js';
3
-
4
- const isString = obj => typeof obj === 'string';
5
- const defer = () => {
6
- let res;
7
- let rej;
8
- const promise = new Promise((resolve, reject) => {
9
- res = resolve;
10
- rej = reject;
11
- });
12
- promise.resolve = res;
13
- promise.reject = rej;
14
- return promise;
15
- };
16
- const makeString = object => {
17
- if (object == null) return '';
18
- return '' + object;
19
- };
20
- const copy = (a, s, t) => {
21
- a.forEach(m => {
22
- if (s[m]) t[m] = s[m];
23
- });
24
- };
25
- const lastOfPathSeparatorRegExp = /###/g;
26
- const cleanKey = key => key && key.indexOf('###') > -1 ? key.replace(lastOfPathSeparatorRegExp, '.') : key;
27
- const canNotTraverseDeeper = object => !object || isString(object);
28
- const getLastOfPath = (object, path, Empty) => {
29
- const stack = !isString(path) ? path : path.split('.');
30
- let stackIndex = 0;
31
- while (stackIndex < stack.length - 1) {
32
- if (canNotTraverseDeeper(object)) return {};
33
- const key = cleanKey(stack[stackIndex]);
34
- if (!object[key] && Empty) object[key] = new Empty();
35
- if (Object.prototype.hasOwnProperty.call(object, key)) {
36
- object = object[key];
37
- } else {
38
- object = {};
39
- }
40
- ++stackIndex;
41
- }
42
- if (canNotTraverseDeeper(object)) return {};
43
- return {
44
- obj: object,
45
- k: cleanKey(stack[stackIndex])
46
- };
47
- };
48
- const setPath = (object, path, newValue) => {
49
- const {
50
- obj,
51
- k
52
- } = getLastOfPath(object, path, Object);
53
- if (obj !== undefined || path.length === 1) {
54
- obj[k] = newValue;
55
- return;
56
- }
57
- let e = path[path.length - 1];
58
- let p = path.slice(0, path.length - 1);
59
- let last = getLastOfPath(object, p, Object);
60
- while (last.obj === undefined && p.length) {
61
- e = `${p[p.length - 1]}.${e}`;
62
- p = p.slice(0, p.length - 1);
63
- last = getLastOfPath(object, p, Object);
64
- if (last?.obj && typeof last.obj[`${last.k}.${e}`] !== 'undefined') {
65
- last.obj = undefined;
66
- }
67
- }
68
- last.obj[`${last.k}.${e}`] = newValue;
69
- };
70
- const pushPath = (object, path, newValue, concat) => {
71
- const {
72
- obj,
73
- k
74
- } = getLastOfPath(object, path, Object);
75
- obj[k] = obj[k] || [];
76
- obj[k].push(newValue);
77
- };
78
- const getPath = (object, path) => {
79
- const {
80
- obj,
81
- k
82
- } = getLastOfPath(object, path);
83
- if (!obj) return undefined;
84
- if (!Object.prototype.hasOwnProperty.call(obj, k)) return undefined;
85
- return obj[k];
86
- };
87
- const getPathWithDefaults = (data, defaultData, key) => {
88
- const value = getPath(data, key);
89
- if (value !== undefined) {
90
- return value;
91
- }
92
- return getPath(defaultData, key);
93
- };
94
- const deepExtend = (target, source, overwrite) => {
95
- for (const prop in source) {
96
- if (prop !== '__proto__' && prop !== 'constructor') {
97
- if (prop in target) {
98
- if (isString(target[prop]) || target[prop] instanceof String || isString(source[prop]) || source[prop] instanceof String) {
99
- if (overwrite) target[prop] = source[prop];
100
- } else {
101
- deepExtend(target[prop], source[prop], overwrite);
102
- }
103
- } else {
104
- target[prop] = source[prop];
105
- }
106
- }
107
- }
108
- return target;
109
- };
110
- const regexEscape = str => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
111
- var _entityMap = {
112
- '&': '&amp;',
113
- '<': '&lt;',
114
- '>': '&gt;',
115
- '"': '&quot;',
116
- "'": '&#39;',
117
- '/': '&#x2F;'
118
- };
119
- const escape = data => {
120
- if (isString(data)) {
121
- return data.replace(/[&<>"'\/]/g, s => _entityMap[s]);
122
- }
123
- return data;
124
- };
125
- class RegExpCache {
126
- constructor(capacity) {
127
- this.capacity = capacity;
128
- this.regExpMap = new Map();
129
- this.regExpQueue = [];
130
- }
131
- getRegExp(pattern) {
132
- const regExpFromCache = this.regExpMap.get(pattern);
133
- if (regExpFromCache !== undefined) {
134
- return regExpFromCache;
135
- }
136
- const regExpNew = new RegExp(pattern);
137
- if (this.regExpQueue.length === this.capacity) {
138
- this.regExpMap.delete(this.regExpQueue.shift());
139
- }
140
- this.regExpMap.set(pattern, regExpNew);
141
- this.regExpQueue.push(pattern);
142
- return regExpNew;
143
- }
144
- }
145
- const chars = [' ', ',', '?', '!', ';'];
146
- const looksLikeObjectPathRegExpCache = new RegExpCache(20);
147
- const looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
148
- nsSeparator = nsSeparator || '';
149
- keySeparator = keySeparator || '';
150
- const possibleChars = chars.filter(c => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0);
151
- if (possibleChars.length === 0) return true;
152
- const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map(c => c === '?' ? '\\?' : c).join('|')})`);
153
- let matched = !r.test(key);
154
- if (!matched) {
155
- const ki = key.indexOf(keySeparator);
156
- if (ki > 0 && !r.test(key.substring(0, ki))) {
157
- matched = true;
158
- }
159
- }
160
- return matched;
161
- };
162
- const deepFind = (obj, path, keySeparator = '.') => {
163
- if (!obj) return undefined;
164
- if (obj[path]) {
165
- if (!Object.prototype.hasOwnProperty.call(obj, path)) return undefined;
166
- return obj[path];
167
- }
168
- const tokens = path.split(keySeparator);
169
- let current = obj;
170
- for (let i = 0; i < tokens.length;) {
171
- if (!current || typeof current !== 'object') {
172
- return undefined;
173
- }
174
- let next;
175
- let nextPath = '';
176
- for (let j = i; j < tokens.length; ++j) {
177
- if (j !== i) {
178
- nextPath += keySeparator;
179
- }
180
- nextPath += tokens[j];
181
- next = current[nextPath];
182
- if (next !== undefined) {
183
- if (['string', 'number', 'boolean'].indexOf(typeof next) > -1 && j < tokens.length - 1) {
184
- continue;
185
- }
186
- i += j - i + 1;
187
- break;
188
- }
189
- }
190
- current = next;
191
- }
192
- return current;
193
- };
194
- const getCleanedCode = code => code?.replace('_', '-');
195
-
196
- const consoleLogger = {
197
- type: 'logger',
198
- log(args) {
199
- this.output('log', args);
200
- },
201
- warn(args) {
202
- this.output('warn', args);
203
- },
204
- error(args) {
205
- this.output('error', args);
206
- },
207
- output(type, args) {
208
- console?.[type]?.apply?.(console, args);
209
- }
210
- };
211
- class Logger {
212
- constructor(concreteLogger, options = {}) {
213
- this.init(concreteLogger, options);
214
- }
215
- init(concreteLogger, options = {}) {
216
- this.prefix = options.prefix || 'i18next:';
217
- this.logger = concreteLogger || consoleLogger;
218
- this.options = options;
219
- this.debug = options.debug;
220
- }
221
- log(...args) {
222
- return this.forward(args, 'log', '', true);
223
- }
224
- warn(...args) {
225
- return this.forward(args, 'warn', '', true);
226
- }
227
- error(...args) {
228
- return this.forward(args, 'error', '');
229
- }
230
- deprecate(...args) {
231
- return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
232
- }
233
- forward(args, lvl, prefix, debugOnly) {
234
- if (debugOnly && !this.debug) return null;
235
- if (isString(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
236
- return this.logger[lvl](args);
237
- }
238
- create(moduleName) {
239
- return new Logger(this.logger, {
240
- ...{
241
- prefix: `${this.prefix}:${moduleName}:`
242
- },
243
- ...this.options
244
- });
245
- }
246
- clone(options) {
247
- options = options || this.options;
248
- options.prefix = options.prefix || this.prefix;
249
- return new Logger(this.logger, options);
250
- }
251
- }
252
- var baseLogger = new Logger();
253
-
254
- class EventEmitter {
255
- constructor() {
256
- this.observers = {};
257
- }
258
- on(events, listener) {
259
- events.split(' ').forEach(event => {
260
- if (!this.observers[event]) this.observers[event] = new Map();
261
- const numListeners = this.observers[event].get(listener) || 0;
262
- this.observers[event].set(listener, numListeners + 1);
263
- });
264
- return this;
265
- }
266
- off(event, listener) {
267
- if (!this.observers[event]) return;
268
- if (!listener) {
269
- delete this.observers[event];
270
- return;
271
- }
272
- this.observers[event].delete(listener);
273
- }
274
- emit(event, ...args) {
275
- if (this.observers[event]) {
276
- const cloned = Array.from(this.observers[event].entries());
277
- cloned.forEach(([observer, numTimesAdded]) => {
278
- for (let i = 0; i < numTimesAdded; i++) {
279
- observer(...args);
280
- }
281
- });
282
- }
283
- if (this.observers['*']) {
284
- const cloned = Array.from(this.observers['*'].entries());
285
- cloned.forEach(([observer, numTimesAdded]) => {
286
- for (let i = 0; i < numTimesAdded; i++) {
287
- observer.apply(observer, [event, ...args]);
288
- }
289
- });
290
- }
291
- }
292
- }
293
-
294
- class ResourceStore extends EventEmitter {
295
- constructor(data, options = {
296
- ns: ['translation'],
297
- defaultNS: 'translation'
298
- }) {
299
- super();
300
- this.data = data || {};
301
- this.options = options;
302
- if (this.options.keySeparator === undefined) {
303
- this.options.keySeparator = '.';
304
- }
305
- if (this.options.ignoreJSONStructure === undefined) {
306
- this.options.ignoreJSONStructure = true;
307
- }
308
- }
309
- addNamespaces(ns) {
310
- if (this.options.ns.indexOf(ns) < 0) {
311
- this.options.ns.push(ns);
312
- }
313
- }
314
- removeNamespaces(ns) {
315
- const index = this.options.ns.indexOf(ns);
316
- if (index > -1) {
317
- this.options.ns.splice(index, 1);
318
- }
319
- }
320
- getResource(lng, ns, key, options = {}) {
321
- const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
322
- const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
323
- let path;
324
- if (lng.indexOf('.') > -1) {
325
- path = lng.split('.');
326
- } else {
327
- path = [lng, ns];
328
- if (key) {
329
- if (Array.isArray(key)) {
330
- path.push(...key);
331
- } else if (isString(key) && keySeparator) {
332
- path.push(...key.split(keySeparator));
333
- } else {
334
- path.push(key);
335
- }
336
- }
337
- }
338
- const result = getPath(this.data, path);
339
- if (!result && !ns && !key && lng.indexOf('.') > -1) {
340
- lng = path[0];
341
- ns = path[1];
342
- key = path.slice(2).join('.');
343
- }
344
- if (result || !ignoreJSONStructure || !isString(key)) return result;
345
- return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
346
- }
347
- addResource(lng, ns, key, value, options = {
348
- silent: false
349
- }) {
350
- const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
351
- let path = [lng, ns];
352
- if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
353
- if (lng.indexOf('.') > -1) {
354
- path = lng.split('.');
355
- value = ns;
356
- ns = path[1];
357
- }
358
- this.addNamespaces(ns);
359
- setPath(this.data, path, value);
360
- if (!options.silent) this.emit('added', lng, ns, key, value);
361
- }
362
- addResources(lng, ns, resources, options = {
363
- silent: false
364
- }) {
365
- for (const m in resources) {
366
- if (isString(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
367
- silent: true
368
- });
369
- }
370
- if (!options.silent) this.emit('added', lng, ns, resources);
371
- }
372
- addResourceBundle(lng, ns, resources, deep, overwrite, options = {
373
- silent: false,
374
- skipCopy: false
375
- }) {
376
- let path = [lng, ns];
377
- if (lng.indexOf('.') > -1) {
378
- path = lng.split('.');
379
- deep = resources;
380
- resources = ns;
381
- ns = path[1];
382
- }
383
- this.addNamespaces(ns);
384
- let pack = getPath(this.data, path) || {};
385
- if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
386
- if (deep) {
387
- deepExtend(pack, resources, overwrite);
388
- } else {
389
- pack = {
390
- ...pack,
391
- ...resources
392
- };
393
- }
394
- setPath(this.data, path, pack);
395
- if (!options.silent) this.emit('added', lng, ns, resources);
396
- }
397
- removeResourceBundle(lng, ns) {
398
- if (this.hasResourceBundle(lng, ns)) {
399
- delete this.data[lng][ns];
400
- }
401
- this.removeNamespaces(ns);
402
- this.emit('removed', lng, ns);
403
- }
404
- hasResourceBundle(lng, ns) {
405
- return this.getResource(lng, ns) !== undefined;
406
- }
407
- getResourceBundle(lng, ns) {
408
- if (!ns) ns = this.options.defaultNS;
409
- return this.getResource(lng, ns);
410
- }
411
- getDataByLanguage(lng) {
412
- return this.data[lng];
413
- }
414
- hasLanguageSomeTranslations(lng) {
415
- const data = this.getDataByLanguage(lng);
416
- const n = data && Object.keys(data) || [];
417
- return !!n.find(v => data[v] && Object.keys(data[v]).length > 0);
418
- }
419
- toJSON() {
420
- return this.data;
421
- }
422
- }
423
-
424
- var postProcessor = {
425
- processors: {},
426
- addPostProcessor(module) {
427
- this.processors[module.name] = module;
428
- },
429
- handle(processors, value, key, options, translator) {
430
- processors.forEach(processor => {
431
- value = this.processors[processor]?.process(value, key, options, translator) ?? value;
432
- });
433
- return value;
434
- }
435
- };
436
-
437
- const checkedLoadedFor = {};
438
- const shouldHandleAsObject = res => !isString(res) && typeof res !== 'boolean' && typeof res !== 'number';
439
- class Translator extends EventEmitter {
440
- constructor(services, options = {}) {
441
- super();
442
- copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
443
- this.options = options;
444
- if (this.options.keySeparator === undefined) {
445
- this.options.keySeparator = '.';
446
- }
447
- this.logger = baseLogger.create('translator');
448
- }
449
- changeLanguage(lng) {
450
- if (lng) this.language = lng;
451
- }
452
- exists(key, o = {
453
- interpolation: {}
454
- }) {
455
- const opt = {
456
- ...o
457
- };
458
- if (key == null) return false;
459
- const resolved = this.resolve(key, opt);
460
- return resolved?.res !== undefined;
461
- }
462
- extractFromKey(key, opt) {
463
- let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
464
- if (nsSeparator === undefined) nsSeparator = ':';
465
- const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
466
- let namespaces = opt.ns || this.options.defaultNS || [];
467
- const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
468
- const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
469
- if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
470
- const m = key.match(this.interpolator.nestingRegexp);
471
- if (m && m.length > 0) {
472
- return {
473
- key,
474
- namespaces: isString(namespaces) ? [namespaces] : namespaces
475
- };
476
- }
477
- const parts = key.split(nsSeparator);
478
- if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
479
- key = parts.join(keySeparator);
480
- }
481
- return {
482
- key,
483
- namespaces: isString(namespaces) ? [namespaces] : namespaces
484
- };
485
- }
486
- translate(keys, o, lastKey) {
487
- let opt = typeof o === 'object' ? {
488
- ...o
489
- } : o;
490
- if (typeof opt !== 'object' && this.options.overloadTranslationOptionHandler) {
491
- opt = this.options.overloadTranslationOptionHandler(arguments);
492
- }
493
- if (typeof options === 'object') opt = {
494
- ...opt
495
- };
496
- if (!opt) opt = {};
497
- if (keys == null) return '';
498
- if (!Array.isArray(keys)) keys = [String(keys)];
499
- const returnDetails = opt.returnDetails !== undefined ? opt.returnDetails : this.options.returnDetails;
500
- const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
501
- const {
502
- key,
503
- namespaces
504
- } = this.extractFromKey(keys[keys.length - 1], opt);
505
- const namespace = namespaces[namespaces.length - 1];
506
- let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
507
- if (nsSeparator === undefined) nsSeparator = ':';
508
- const lng = opt.lng || this.language;
509
- const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
510
- if (lng?.toLowerCase() === 'cimode') {
511
- if (appendNamespaceToCIMode) {
512
- if (returnDetails) {
513
- return {
514
- res: `${namespace}${nsSeparator}${key}`,
515
- usedKey: key,
516
- exactUsedKey: key,
517
- usedLng: lng,
518
- usedNS: namespace,
519
- usedParams: this.getUsedParamsDetails(opt)
520
- };
521
- }
522
- return `${namespace}${nsSeparator}${key}`;
523
- }
524
- if (returnDetails) {
525
- return {
526
- res: key,
527
- usedKey: key,
528
- exactUsedKey: key,
529
- usedLng: lng,
530
- usedNS: namespace,
531
- usedParams: this.getUsedParamsDetails(opt)
532
- };
533
- }
534
- return key;
535
- }
536
- const resolved = this.resolve(keys, opt);
537
- let res = resolved?.res;
538
- const resUsedKey = resolved?.usedKey || key;
539
- const resExactUsedKey = resolved?.exactUsedKey || key;
540
- const noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
541
- const joinArrays = opt.joinArrays !== undefined ? opt.joinArrays : this.options.joinArrays;
542
- const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
543
- const needsPluralHandling = opt.count !== undefined && !isString(opt.count);
544
- const hasDefaultValue = Translator.hasDefaultValue(opt);
545
- const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : '';
546
- const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
547
- ordinal: false
548
- }) : '';
549
- const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
550
- const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
551
- let resForObjHndl = res;
552
- if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
553
- resForObjHndl = defaultValue;
554
- }
555
- const handleAsObject = shouldHandleAsObject(resForObjHndl);
556
- const resType = Object.prototype.toString.apply(resForObjHndl);
557
- if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && noObject.indexOf(resType) < 0 && !(isString(joinArrays) && Array.isArray(resForObjHndl))) {
558
- if (!opt.returnObjects && !this.options.returnObjects) {
559
- if (!this.options.returnedObjectHandler) {
560
- this.logger.warn('accessing an object - but returnObjects options is not enabled!');
561
- }
562
- const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
563
- ...opt,
564
- ns: namespaces
565
- }) : `key '${key} (${this.language})' returned an object instead of string.`;
566
- if (returnDetails) {
567
- resolved.res = r;
568
- resolved.usedParams = this.getUsedParamsDetails(opt);
569
- return resolved;
570
- }
571
- return r;
572
- }
573
- if (keySeparator) {
574
- const resTypeIsArray = Array.isArray(resForObjHndl);
575
- const copy = resTypeIsArray ? [] : {};
576
- const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
577
- for (const m in resForObjHndl) {
578
- if (Object.prototype.hasOwnProperty.call(resForObjHndl, m)) {
579
- const deepKey = `${newKeyToUse}${keySeparator}${m}`;
580
- if (hasDefaultValue && !res) {
581
- copy[m] = this.translate(deepKey, {
582
- ...opt,
583
- defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : undefined,
584
- ...{
585
- joinArrays: false,
586
- ns: namespaces
587
- }
588
- });
589
- } else {
590
- copy[m] = this.translate(deepKey, {
591
- ...opt,
592
- ...{
593
- joinArrays: false,
594
- ns: namespaces
595
- }
596
- });
597
- }
598
- if (copy[m] === deepKey) copy[m] = resForObjHndl[m];
599
- }
600
- }
601
- res = copy;
602
- }
603
- } else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
604
- res = res.join(joinArrays);
605
- if (res) res = this.extendTranslation(res, keys, opt, lastKey);
606
- } else {
607
- let usedDefault = false;
608
- let usedKey = false;
609
- if (!this.isValidLookup(res) && hasDefaultValue) {
610
- usedDefault = true;
611
- res = defaultValue;
612
- }
613
- if (!this.isValidLookup(res)) {
614
- usedKey = true;
615
- res = key;
616
- }
617
- const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
618
- const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
619
- const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
620
- if (usedKey || usedDefault || updateMissing) {
621
- this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? defaultValue : res);
622
- if (keySeparator) {
623
- const fk = this.resolve(key, {
624
- ...opt,
625
- keySeparator: false
626
- });
627
- if (fk && fk.res) this.logger.warn('Seems the loaded translations were in flat JSON format instead of nested. Either set keySeparator: false on init or make sure your translations are published in nested format.');
628
- }
629
- let lngs = [];
630
- const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
631
- if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
632
- for (let i = 0; i < fallbackLngs.length; i++) {
633
- lngs.push(fallbackLngs[i]);
634
- }
635
- } else if (this.options.saveMissingTo === 'all') {
636
- lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
637
- } else {
638
- lngs.push(opt.lng || this.language);
639
- }
640
- const send = (l, k, specificDefaultValue) => {
641
- const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
642
- if (this.options.missingKeyHandler) {
643
- this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
644
- } else if (this.backendConnector?.saveMissing) {
645
- this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
646
- }
647
- this.emit('missingKey', l, namespace, k, res);
648
- };
649
- if (this.options.saveMissing) {
650
- if (this.options.saveMissingPlurals && needsPluralHandling) {
651
- lngs.forEach(language => {
652
- const suffixes = this.pluralResolver.getSuffixes(language, opt);
653
- if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
654
- suffixes.push(`${this.options.pluralSeparator}zero`);
655
- }
656
- suffixes.forEach(suffix => {
657
- send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
658
- });
659
- });
660
- } else {
661
- send(lngs, key, defaultValue);
662
- }
663
- }
664
- }
665
- res = this.extendTranslation(res, keys, opt, resolved, lastKey);
666
- if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
667
- res = `${namespace}${nsSeparator}${key}`;
668
- }
669
- if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
670
- res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : undefined, opt);
671
- }
672
- }
673
- if (returnDetails) {
674
- resolved.res = res;
675
- resolved.usedParams = this.getUsedParamsDetails(opt);
676
- return resolved;
677
- }
678
- return res;
679
- }
680
- extendTranslation(res, key, opt, resolved, lastKey) {
681
- if (this.i18nFormat?.parse) {
682
- res = this.i18nFormat.parse(res, {
683
- ...this.options.interpolation.defaultVariables,
684
- ...opt
685
- }, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
686
- resolved
687
- });
688
- } else if (!opt.skipInterpolation) {
689
- if (opt.interpolation) this.interpolator.init({
690
- ...opt,
691
- ...{
692
- interpolation: {
693
- ...this.options.interpolation,
694
- ...opt.interpolation
695
- }
696
- }
697
- });
698
- const skipOnVariables = isString(res) && (opt?.interpolation?.skipOnVariables !== undefined ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
699
- let nestBef;
700
- if (skipOnVariables) {
701
- const nb = res.match(this.interpolator.nestingRegexp);
702
- nestBef = nb && nb.length;
703
- }
704
- let data = opt.replace && !isString(opt.replace) ? opt.replace : opt;
705
- if (this.options.interpolation.defaultVariables) data = {
706
- ...this.options.interpolation.defaultVariables,
707
- ...data
708
- };
709
- res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
710
- if (skipOnVariables) {
711
- const na = res.match(this.interpolator.nestingRegexp);
712
- const nestAft = na && na.length;
713
- if (nestBef < nestAft) opt.nest = false;
714
- }
715
- if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
716
- if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
717
- if (lastKey?.[0] === args[0] && !opt.context) {
718
- this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
719
- return null;
720
- }
721
- return this.translate(...args, key);
722
- }, opt);
723
- if (opt.interpolation) this.interpolator.reset();
724
- }
725
- const postProcess = opt.postProcess || this.options.postProcess;
726
- const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
727
- if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
728
- res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
729
- i18nResolved: {
730
- ...resolved,
731
- usedParams: this.getUsedParamsDetails(opt)
732
- },
733
- ...opt
734
- } : opt, this);
735
- }
736
- return res;
737
- }
738
- resolve(keys, opt = {}) {
739
- let found;
740
- let usedKey;
741
- let exactUsedKey;
742
- let usedLng;
743
- let usedNS;
744
- if (isString(keys)) keys = [keys];
745
- keys.forEach(k => {
746
- if (this.isValidLookup(found)) return;
747
- const extracted = this.extractFromKey(k, opt);
748
- const key = extracted.key;
749
- usedKey = key;
750
- let namespaces = extracted.namespaces;
751
- if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
752
- const needsPluralHandling = opt.count !== undefined && !isString(opt.count);
753
- const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
754
- const needsContextHandling = opt.context !== undefined && (isString(opt.context) || typeof opt.context === 'number') && opt.context !== '';
755
- const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
756
- namespaces.forEach(ns => {
757
- if (this.isValidLookup(found)) return;
758
- usedNS = ns;
759
- if (!checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils?.hasLoadedNamespace && !this.utils?.hasLoadedNamespace(usedNS)) {
760
- checkedLoadedFor[`${codes[0]}-${ns}`] = true;
761
- this.logger.warn(`key "${usedKey}" for languages "${codes.join(', ')}" won't get resolved as namespace "${usedNS}" was not yet loaded`, 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
762
- }
763
- codes.forEach(code => {
764
- if (this.isValidLookup(found)) return;
765
- usedLng = code;
766
- const finalKeys = [key];
767
- if (this.i18nFormat?.addLookupKeys) {
768
- this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
769
- } else {
770
- let pluralSuffix;
771
- if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
772
- const zeroSuffix = `${this.options.pluralSeparator}zero`;
773
- const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
774
- if (needsPluralHandling) {
775
- finalKeys.push(key + pluralSuffix);
776
- if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
777
- finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
778
- }
779
- if (needsZeroSuffixLookup) {
780
- finalKeys.push(key + zeroSuffix);
781
- }
782
- }
783
- if (needsContextHandling) {
784
- const contextKey = `${key}${this.options.contextSeparator}${opt.context}`;
785
- finalKeys.push(contextKey);
786
- if (needsPluralHandling) {
787
- finalKeys.push(contextKey + pluralSuffix);
788
- if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
789
- finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
790
- }
791
- if (needsZeroSuffixLookup) {
792
- finalKeys.push(contextKey + zeroSuffix);
793
- }
794
- }
795
- }
796
- }
797
- let possibleKey;
798
- while (possibleKey = finalKeys.pop()) {
799
- if (!this.isValidLookup(found)) {
800
- exactUsedKey = possibleKey;
801
- found = this.getResource(code, ns, possibleKey, opt);
802
- }
803
- }
804
- });
805
- });
806
- });
807
- return {
808
- res: found,
809
- usedKey,
810
- exactUsedKey,
811
- usedLng,
812
- usedNS
813
- };
814
- }
815
- isValidLookup(res) {
816
- return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
817
- }
818
- getResource(code, ns, key, options = {}) {
819
- if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
820
- return this.resourceStore.getResource(code, ns, key, options);
821
- }
822
- getUsedParamsDetails(options = {}) {
823
- const optionsKeys = ['defaultValue', 'ordinal', 'context', 'replace', 'lng', 'lngs', 'fallbackLng', 'ns', 'keySeparator', 'nsSeparator', 'returnObjects', 'returnDetails', 'joinArrays', 'postProcess', 'interpolation'];
824
- const useOptionsReplaceForData = options.replace && !isString(options.replace);
825
- let data = useOptionsReplaceForData ? options.replace : options;
826
- if (useOptionsReplaceForData && typeof options.count !== 'undefined') {
827
- data.count = options.count;
828
- }
829
- if (this.options.interpolation.defaultVariables) {
830
- data = {
831
- ...this.options.interpolation.defaultVariables,
832
- ...data
833
- };
834
- }
835
- if (!useOptionsReplaceForData) {
836
- data = {
837
- ...data
838
- };
839
- for (const key of optionsKeys) {
840
- delete data[key];
841
- }
842
- }
843
- return data;
844
- }
845
- static hasDefaultValue(options) {
846
- const prefix = 'defaultValue';
847
- for (const option in options) {
848
- if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && undefined !== options[option]) {
849
- return true;
850
- }
851
- }
852
- return false;
853
- }
854
- }
855
-
856
- class LanguageUtil {
857
- constructor(options) {
858
- this.options = options;
859
- this.supportedLngs = this.options.supportedLngs || false;
860
- this.logger = baseLogger.create('languageUtils');
861
- }
862
- getScriptPartFromCode(code) {
863
- code = getCleanedCode(code);
864
- if (!code || code.indexOf('-') < 0) return null;
865
- const p = code.split('-');
866
- if (p.length === 2) return null;
867
- p.pop();
868
- if (p[p.length - 1].toLowerCase() === 'x') return null;
869
- return this.formatLanguageCode(p.join('-'));
870
- }
871
- getLanguagePartFromCode(code) {
872
- code = getCleanedCode(code);
873
- if (!code || code.indexOf('-') < 0) return code;
874
- const p = code.split('-');
875
- return this.formatLanguageCode(p[0]);
876
- }
877
- formatLanguageCode(code) {
878
- if (isString(code) && code.indexOf('-') > -1) {
879
- let formattedCode;
880
- try {
881
- formattedCode = Intl.getCanonicalLocales(code)[0];
882
- } catch (e) {}
883
- if (formattedCode && this.options.lowerCaseLng) {
884
- formattedCode = formattedCode.toLowerCase();
885
- }
886
- if (formattedCode) return formattedCode;
887
- if (this.options.lowerCaseLng) {
888
- return code.toLowerCase();
889
- }
890
- return code;
891
- }
892
- return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
893
- }
894
- isSupportedCode(code) {
895
- if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
896
- code = this.getLanguagePartFromCode(code);
897
- }
898
- return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
899
- }
900
- getBestMatchFromCodes(codes) {
901
- if (!codes) return null;
902
- let found;
903
- codes.forEach(code => {
904
- if (found) return;
905
- const cleanedLng = this.formatLanguageCode(code);
906
- if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
907
- });
908
- if (!found && this.options.supportedLngs) {
909
- codes.forEach(code => {
910
- if (found) return;
911
- const lngScOnly = this.getScriptPartFromCode(code);
912
- if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
913
- const lngOnly = this.getLanguagePartFromCode(code);
914
- if (this.isSupportedCode(lngOnly)) return found = lngOnly;
915
- found = this.options.supportedLngs.find(supportedLng => {
916
- if (supportedLng === lngOnly) return supportedLng;
917
- if (supportedLng.indexOf('-') < 0 && lngOnly.indexOf('-') < 0) return;
918
- if (supportedLng.indexOf('-') > 0 && lngOnly.indexOf('-') < 0 && supportedLng.substring(0, supportedLng.indexOf('-')) === lngOnly) return supportedLng;
919
- if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
920
- });
921
- });
922
- }
923
- if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
924
- return found;
925
- }
926
- getFallbackCodes(fallbacks, code) {
927
- if (!fallbacks) return [];
928
- if (typeof fallbacks === 'function') fallbacks = fallbacks(code);
929
- if (isString(fallbacks)) fallbacks = [fallbacks];
930
- if (Array.isArray(fallbacks)) return fallbacks;
931
- if (!code) return fallbacks.default || [];
932
- let found = fallbacks[code];
933
- if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
934
- if (!found) found = fallbacks[this.formatLanguageCode(code)];
935
- if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
936
- if (!found) found = fallbacks.default;
937
- return found || [];
938
- }
939
- toResolveHierarchy(code, fallbackCode) {
940
- const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
941
- const codes = [];
942
- const addCode = c => {
943
- if (!c) return;
944
- if (this.isSupportedCode(c)) {
945
- codes.push(c);
946
- } else {
947
- this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
948
- }
949
- };
950
- if (isString(code) && (code.indexOf('-') > -1 || code.indexOf('_') > -1)) {
951
- if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code));
952
- if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code));
953
- if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code));
954
- } else if (isString(code)) {
955
- addCode(this.formatLanguageCode(code));
956
- }
957
- fallbackCodes.forEach(fc => {
958
- if (codes.indexOf(fc) < 0) addCode(this.formatLanguageCode(fc));
959
- });
960
- return codes;
961
- }
962
- }
963
-
964
- const suffixesOrder = {
965
- zero: 0,
966
- one: 1,
967
- two: 2,
968
- few: 3,
969
- many: 4,
970
- other: 5
971
- };
972
- const dummyRule = {
973
- select: count => count === 1 ? 'one' : 'other',
974
- resolvedOptions: () => ({
975
- pluralCategories: ['one', 'other']
976
- })
977
- };
978
- class PluralResolver {
979
- constructor(languageUtils, options = {}) {
980
- this.languageUtils = languageUtils;
981
- this.options = options;
982
- this.logger = baseLogger.create('pluralResolver');
983
- this.pluralRulesCache = {};
984
- }
985
- addRule(lng, obj) {
986
- this.rules[lng] = obj;
987
- }
988
- clearCache() {
989
- this.pluralRulesCache = {};
990
- }
991
- getRule(code, options = {}) {
992
- const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
993
- const type = options.ordinal ? 'ordinal' : 'cardinal';
994
- const cacheKey = JSON.stringify({
995
- cleanedCode,
996
- type
997
- });
998
- if (cacheKey in this.pluralRulesCache) {
999
- return this.pluralRulesCache[cacheKey];
1000
- }
1001
- let rule;
1002
- try {
1003
- rule = new Intl.PluralRules(cleanedCode, {
1004
- type
1005
- });
1006
- } catch (err) {
1007
- if (!Intl) {
1008
- this.logger.error('No Intl support, please use an Intl polyfill!');
1009
- return dummyRule;
1010
- }
1011
- if (!code.match(/-|_/)) return dummyRule;
1012
- const lngPart = this.languageUtils.getLanguagePartFromCode(code);
1013
- rule = this.getRule(lngPart, options);
1014
- }
1015
- this.pluralRulesCache[cacheKey] = rule;
1016
- return rule;
1017
- }
1018
- needsPlural(code, options = {}) {
1019
- let rule = this.getRule(code, options);
1020
- if (!rule) rule = this.getRule('dev', options);
1021
- return rule?.resolvedOptions().pluralCategories.length > 1;
1022
- }
1023
- getPluralFormsOfKey(code, key, options = {}) {
1024
- return this.getSuffixes(code, options).map(suffix => `${key}${suffix}`);
1025
- }
1026
- getSuffixes(code, options = {}) {
1027
- let rule = this.getRule(code, options);
1028
- if (!rule) rule = this.getRule('dev', options);
1029
- if (!rule) return [];
1030
- return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map(pluralCategory => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${pluralCategory}`);
1031
- }
1032
- getSuffix(code, count, options = {}) {
1033
- const rule = this.getRule(code, options);
1034
- if (rule) {
1035
- return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${rule.select(count)}`;
1036
- }
1037
- this.logger.warn(`no plural rule found for: ${code}`);
1038
- return this.getSuffix('dev', count, options);
1039
- }
1040
- }
1041
-
1042
- const deepFindWithDefaults = (data, defaultData, key, keySeparator = '.', ignoreJSONStructure = true) => {
1043
- let path = getPathWithDefaults(data, defaultData, key);
1044
- if (!path && ignoreJSONStructure && isString(key)) {
1045
- path = deepFind(data, key, keySeparator);
1046
- if (path === undefined) path = deepFind(defaultData, key, keySeparator);
1047
- }
1048
- return path;
1049
- };
1050
- const regexSafe = val => val.replace(/\$/g, '$$$$');
1051
- class Interpolator {
1052
- constructor(options = {}) {
1053
- this.logger = baseLogger.create('interpolator');
1054
- this.options = options;
1055
- this.format = options?.interpolation?.format || (value => value);
1056
- this.init(options);
1057
- }
1058
- init(options = {}) {
1059
- if (!options.interpolation) options.interpolation = {
1060
- escapeValue: true
1061
- };
1062
- const {
1063
- escape: escape$1,
1064
- escapeValue,
1065
- useRawValueToEscape,
1066
- prefix,
1067
- prefixEscaped,
1068
- suffix,
1069
- suffixEscaped,
1070
- formatSeparator,
1071
- unescapeSuffix,
1072
- unescapePrefix,
1073
- nestingPrefix,
1074
- nestingPrefixEscaped,
1075
- nestingSuffix,
1076
- nestingSuffixEscaped,
1077
- nestingOptionsSeparator,
1078
- maxReplaces,
1079
- alwaysFormat
1080
- } = options.interpolation;
1081
- this.escape = escape$1 !== undefined ? escape$1 : escape;
1082
- this.escapeValue = escapeValue !== undefined ? escapeValue : true;
1083
- this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
1084
- this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || '{{';
1085
- this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || '}}';
1086
- this.formatSeparator = formatSeparator || ',';
1087
- this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix || '-';
1088
- this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix || '';
1089
- this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape('$t(');
1090
- this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(')');
1091
- this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
1092
- this.maxReplaces = maxReplaces || 1000;
1093
- this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
1094
- this.resetRegExp();
1095
- }
1096
- reset() {
1097
- if (this.options) this.init(this.options);
1098
- }
1099
- resetRegExp() {
1100
- const getOrResetRegExp = (existingRegExp, pattern) => {
1101
- if (existingRegExp?.source === pattern) {
1102
- existingRegExp.lastIndex = 0;
1103
- return existingRegExp;
1104
- }
1105
- return new RegExp(pattern, 'g');
1106
- };
1107
- this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
1108
- this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
1109
- this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}(.+?)${this.nestingSuffix}`);
1110
- }
1111
- interpolate(str, data, lng, options) {
1112
- let match;
1113
- let value;
1114
- let replaces;
1115
- const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
1116
- const handleFormat = key => {
1117
- if (key.indexOf(this.formatSeparator) < 0) {
1118
- const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
1119
- return this.alwaysFormat ? this.format(path, undefined, lng, {
1120
- ...options,
1121
- ...data,
1122
- interpolationkey: key
1123
- }) : path;
1124
- }
1125
- const p = key.split(this.formatSeparator);
1126
- const k = p.shift().trim();
1127
- const f = p.join(this.formatSeparator).trim();
1128
- return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
1129
- ...options,
1130
- ...data,
1131
- interpolationkey: k
1132
- });
1133
- };
1134
- this.resetRegExp();
1135
- const missingInterpolationHandler = options?.missingInterpolationHandler || this.options.missingInterpolationHandler;
1136
- const skipOnVariables = options?.interpolation?.skipOnVariables !== undefined ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
1137
- const todos = [{
1138
- regex: this.regexpUnescape,
1139
- safeValue: val => regexSafe(val)
1140
- }, {
1141
- regex: this.regexp,
1142
- safeValue: val => this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)
1143
- }];
1144
- todos.forEach(todo => {
1145
- replaces = 0;
1146
- while (match = todo.regex.exec(str)) {
1147
- const matchedVar = match[1].trim();
1148
- value = handleFormat(matchedVar);
1149
- if (value === undefined) {
1150
- if (typeof missingInterpolationHandler === 'function') {
1151
- const temp = missingInterpolationHandler(str, match, options);
1152
- value = isString(temp) ? temp : '';
1153
- } else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
1154
- value = '';
1155
- } else if (skipOnVariables) {
1156
- value = match[0];
1157
- continue;
1158
- } else {
1159
- this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
1160
- value = '';
1161
- }
1162
- } else if (!isString(value) && !this.useRawValueToEscape) {
1163
- value = makeString(value);
1164
- }
1165
- const safeValue = todo.safeValue(value);
1166
- str = str.replace(match[0], safeValue);
1167
- if (skipOnVariables) {
1168
- todo.regex.lastIndex += value.length;
1169
- todo.regex.lastIndex -= match[0].length;
1170
- } else {
1171
- todo.regex.lastIndex = 0;
1172
- }
1173
- replaces++;
1174
- if (replaces >= this.maxReplaces) {
1175
- break;
1176
- }
1177
- }
1178
- });
1179
- return str;
1180
- }
1181
- nest(str, fc, options = {}) {
1182
- let match;
1183
- let value;
1184
- let clonedOptions;
1185
- const handleHasOptions = (key, inheritedOptions) => {
1186
- const sep = this.nestingOptionsSeparator;
1187
- if (key.indexOf(sep) < 0) return key;
1188
- const c = key.split(new RegExp(`${sep}[ ]*{`));
1189
- let optionsString = `{${c[1]}`;
1190
- key = c[0];
1191
- optionsString = this.interpolate(optionsString, clonedOptions);
1192
- const matchedSingleQuotes = optionsString.match(/'/g);
1193
- const matchedDoubleQuotes = optionsString.match(/"/g);
1194
- if ((matchedSingleQuotes?.length ?? 0) % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
1195
- optionsString = optionsString.replace(/'/g, '"');
1196
- }
1197
- try {
1198
- clonedOptions = JSON.parse(optionsString);
1199
- if (inheritedOptions) clonedOptions = {
1200
- ...inheritedOptions,
1201
- ...clonedOptions
1202
- };
1203
- } catch (e) {
1204
- this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
1205
- return `${key}${sep}${optionsString}`;
1206
- }
1207
- if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
1208
- return key;
1209
- };
1210
- while (match = this.nestingRegexp.exec(str)) {
1211
- let formatters = [];
1212
- clonedOptions = {
1213
- ...options
1214
- };
1215
- clonedOptions = clonedOptions.replace && !isString(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
1216
- clonedOptions.applyPostProcessor = false;
1217
- delete clonedOptions.defaultValue;
1218
- let doReduce = false;
1219
- if (match[0].indexOf(this.formatSeparator) !== -1 && !/{.*}/.test(match[1])) {
1220
- const r = match[1].split(this.formatSeparator).map(elem => elem.trim());
1221
- match[1] = r.shift();
1222
- formatters = r;
1223
- doReduce = true;
1224
- }
1225
- value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
1226
- if (value && match[0] === str && !isString(value)) return value;
1227
- if (!isString(value)) value = makeString(value);
1228
- if (!value) {
1229
- this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
1230
- value = '';
1231
- }
1232
- if (doReduce) {
1233
- value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
1234
- ...options,
1235
- interpolationkey: match[1].trim()
1236
- }), value.trim());
1237
- }
1238
- str = str.replace(match[0], value);
1239
- this.regexp.lastIndex = 0;
1240
- }
1241
- return str;
1242
- }
1243
- }
1244
-
1245
- const parseFormatStr = formatStr => {
1246
- let formatName = formatStr.toLowerCase().trim();
1247
- const formatOptions = {};
1248
- if (formatStr.indexOf('(') > -1) {
1249
- const p = formatStr.split('(');
1250
- formatName = p[0].toLowerCase().trim();
1251
- const optStr = p[1].substring(0, p[1].length - 1);
1252
- if (formatName === 'currency' && optStr.indexOf(':') < 0) {
1253
- if (!formatOptions.currency) formatOptions.currency = optStr.trim();
1254
- } else if (formatName === 'relativetime' && optStr.indexOf(':') < 0) {
1255
- if (!formatOptions.range) formatOptions.range = optStr.trim();
1256
- } else {
1257
- const opts = optStr.split(';');
1258
- opts.forEach(opt => {
1259
- if (opt) {
1260
- const [key, ...rest] = opt.split(':');
1261
- const val = rest.join(':').trim().replace(/^'+|'+$/g, '');
1262
- const trimmedKey = key.trim();
1263
- if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
1264
- if (val === 'false') formatOptions[trimmedKey] = false;
1265
- if (val === 'true') formatOptions[trimmedKey] = true;
1266
- if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
1267
- }
1268
- });
1269
- }
1270
- }
1271
- return {
1272
- formatName,
1273
- formatOptions
1274
- };
1275
- };
1276
- const createCachedFormatter = fn => {
1277
- const cache = {};
1278
- return (v, l, o) => {
1279
- let optForCache = o;
1280
- if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
1281
- optForCache = {
1282
- ...optForCache,
1283
- [o.interpolationkey]: undefined
1284
- };
1285
- }
1286
- const key = l + JSON.stringify(optForCache);
1287
- let frm = cache[key];
1288
- if (!frm) {
1289
- frm = fn(getCleanedCode(l), o);
1290
- cache[key] = frm;
1291
- }
1292
- return frm(v);
1293
- };
1294
- };
1295
- const createNonCachedFormatter = fn => (v, l, o) => fn(getCleanedCode(l), o)(v);
1296
- class Formatter {
1297
- constructor(options = {}) {
1298
- this.logger = baseLogger.create('formatter');
1299
- this.options = options;
1300
- this.init(options);
1301
- }
1302
- init(services, options = {
1303
- interpolation: {}
1304
- }) {
1305
- this.formatSeparator = options.interpolation.formatSeparator || ',';
1306
- const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
1307
- this.formats = {
1308
- number: cf((lng, opt) => {
1309
- const formatter = new Intl.NumberFormat(lng, {
1310
- ...opt
1311
- });
1312
- return val => formatter.format(val);
1313
- }),
1314
- currency: cf((lng, opt) => {
1315
- const formatter = new Intl.NumberFormat(lng, {
1316
- ...opt,
1317
- style: 'currency'
1318
- });
1319
- return val => formatter.format(val);
1320
- }),
1321
- datetime: cf((lng, opt) => {
1322
- const formatter = new Intl.DateTimeFormat(lng, {
1323
- ...opt
1324
- });
1325
- return val => formatter.format(val);
1326
- }),
1327
- relativetime: cf((lng, opt) => {
1328
- const formatter = new Intl.RelativeTimeFormat(lng, {
1329
- ...opt
1330
- });
1331
- return val => formatter.format(val, opt.range || 'day');
1332
- }),
1333
- list: cf((lng, opt) => {
1334
- const formatter = new Intl.ListFormat(lng, {
1335
- ...opt
1336
- });
1337
- return val => formatter.format(val);
1338
- })
1339
- };
1340
- }
1341
- add(name, fc) {
1342
- this.formats[name.toLowerCase().trim()] = fc;
1343
- }
1344
- addCached(name, fc) {
1345
- this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
1346
- }
1347
- format(value, format, lng, options = {}) {
1348
- const formats = format.split(this.formatSeparator);
1349
- if (formats.length > 1 && formats[0].indexOf('(') > 1 && formats[0].indexOf(')') < 0 && formats.find(f => f.indexOf(')') > -1)) {
1350
- const lastIndex = formats.findIndex(f => f.indexOf(')') > -1);
1351
- formats[0] = [formats[0], ...formats.splice(1, lastIndex)].join(this.formatSeparator);
1352
- }
1353
- const result = formats.reduce((mem, f) => {
1354
- const {
1355
- formatName,
1356
- formatOptions
1357
- } = parseFormatStr(f);
1358
- if (this.formats[formatName]) {
1359
- let formatted = mem;
1360
- try {
1361
- const valOptions = options?.formatParams?.[options.interpolationkey] || {};
1362
- const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
1363
- formatted = this.formats[formatName](mem, l, {
1364
- ...formatOptions,
1365
- ...options,
1366
- ...valOptions
1367
- });
1368
- } catch (error) {
1369
- this.logger.warn(error);
1370
- }
1371
- return formatted;
1372
- } else {
1373
- this.logger.warn(`there was no format function for ${formatName}`);
1374
- }
1375
- return mem;
1376
- }, value);
1377
- return result;
1378
- }
1379
- }
1380
-
1381
- const removePending = (q, name) => {
1382
- if (q.pending[name] !== undefined) {
1383
- delete q.pending[name];
1384
- q.pendingCount--;
1385
- }
1386
- };
1387
- class Connector extends EventEmitter {
1388
- constructor(backend, store, services, options = {}) {
1389
- super();
1390
- this.backend = backend;
1391
- this.store = store;
1392
- this.services = services;
1393
- this.languageUtils = services.languageUtils;
1394
- this.options = options;
1395
- this.logger = baseLogger.create('backendConnector');
1396
- this.waitingReads = [];
1397
- this.maxParallelReads = options.maxParallelReads || 10;
1398
- this.readingCalls = 0;
1399
- this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
1400
- this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
1401
- this.state = {};
1402
- this.queue = [];
1403
- this.backend?.init?.(services, options.backend, options);
1404
- }
1405
- queueLoad(languages, namespaces, options, callback) {
1406
- const toLoad = {};
1407
- const pending = {};
1408
- const toLoadLanguages = {};
1409
- const toLoadNamespaces = {};
1410
- languages.forEach(lng => {
1411
- let hasAllNamespaces = true;
1412
- namespaces.forEach(ns => {
1413
- const name = `${lng}|${ns}`;
1414
- if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
1415
- this.state[name] = 2;
1416
- } else if (this.state[name] < 0) ; else if (this.state[name] === 1) {
1417
- if (pending[name] === undefined) pending[name] = true;
1418
- } else {
1419
- this.state[name] = 1;
1420
- hasAllNamespaces = false;
1421
- if (pending[name] === undefined) pending[name] = true;
1422
- if (toLoad[name] === undefined) toLoad[name] = true;
1423
- if (toLoadNamespaces[ns] === undefined) toLoadNamespaces[ns] = true;
1424
- }
1425
- });
1426
- if (!hasAllNamespaces) toLoadLanguages[lng] = true;
1427
- });
1428
- if (Object.keys(toLoad).length || Object.keys(pending).length) {
1429
- this.queue.push({
1430
- pending,
1431
- pendingCount: Object.keys(pending).length,
1432
- loaded: {},
1433
- errors: [],
1434
- callback
1435
- });
1436
- }
1437
- return {
1438
- toLoad: Object.keys(toLoad),
1439
- pending: Object.keys(pending),
1440
- toLoadLanguages: Object.keys(toLoadLanguages),
1441
- toLoadNamespaces: Object.keys(toLoadNamespaces)
1442
- };
1443
- }
1444
- loaded(name, err, data) {
1445
- const s = name.split('|');
1446
- const lng = s[0];
1447
- const ns = s[1];
1448
- if (err) this.emit('failedLoading', lng, ns, err);
1449
- if (!err && data) {
1450
- this.store.addResourceBundle(lng, ns, data, undefined, undefined, {
1451
- skipCopy: true
1452
- });
1453
- }
1454
- this.state[name] = err ? -1 : 2;
1455
- if (err && data) this.state[name] = 0;
1456
- const loaded = {};
1457
- this.queue.forEach(q => {
1458
- pushPath(q.loaded, [lng], ns);
1459
- removePending(q, name);
1460
- if (err) q.errors.push(err);
1461
- if (q.pendingCount === 0 && !q.done) {
1462
- Object.keys(q.loaded).forEach(l => {
1463
- if (!loaded[l]) loaded[l] = {};
1464
- const loadedKeys = q.loaded[l];
1465
- if (loadedKeys.length) {
1466
- loadedKeys.forEach(n => {
1467
- if (loaded[l][n] === undefined) loaded[l][n] = true;
1468
- });
1469
- }
1470
- });
1471
- q.done = true;
1472
- if (q.errors.length) {
1473
- q.callback(q.errors);
1474
- } else {
1475
- q.callback();
1476
- }
1477
- }
1478
- });
1479
- this.emit('loaded', loaded);
1480
- this.queue = this.queue.filter(q => !q.done);
1481
- }
1482
- read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
1483
- if (!lng.length) return callback(null, {});
1484
- if (this.readingCalls >= this.maxParallelReads) {
1485
- this.waitingReads.push({
1486
- lng,
1487
- ns,
1488
- fcName,
1489
- tried,
1490
- wait,
1491
- callback
1492
- });
1493
- return;
1494
- }
1495
- this.readingCalls++;
1496
- const resolver = (err, data) => {
1497
- this.readingCalls--;
1498
- if (this.waitingReads.length > 0) {
1499
- const next = this.waitingReads.shift();
1500
- this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
1501
- }
1502
- if (err && data && tried < this.maxRetries) {
1503
- setTimeout(() => {
1504
- this.read.call(this, lng, ns, fcName, tried + 1, wait * 2, callback);
1505
- }, wait);
1506
- return;
1507
- }
1508
- callback(err, data);
1509
- };
1510
- const fc = this.backend[fcName].bind(this.backend);
1511
- if (fc.length === 2) {
1512
- try {
1513
- const r = fc(lng, ns);
1514
- if (r && typeof r.then === 'function') {
1515
- r.then(data => resolver(null, data)).catch(resolver);
1516
- } else {
1517
- resolver(null, r);
1518
- }
1519
- } catch (err) {
1520
- resolver(err);
1521
- }
1522
- return;
1523
- }
1524
- return fc(lng, ns, resolver);
1525
- }
1526
- prepareLoading(languages, namespaces, options = {}, callback) {
1527
- if (!this.backend) {
1528
- this.logger.warn('No backend was added via i18next.use. Will not load resources.');
1529
- return callback && callback();
1530
- }
1531
- if (isString(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
1532
- if (isString(namespaces)) namespaces = [namespaces];
1533
- const toLoad = this.queueLoad(languages, namespaces, options, callback);
1534
- if (!toLoad.toLoad.length) {
1535
- if (!toLoad.pending.length) callback();
1536
- return null;
1537
- }
1538
- toLoad.toLoad.forEach(name => {
1539
- this.loadOne(name);
1540
- });
1541
- }
1542
- load(languages, namespaces, callback) {
1543
- this.prepareLoading(languages, namespaces, {}, callback);
1544
- }
1545
- reload(languages, namespaces, callback) {
1546
- this.prepareLoading(languages, namespaces, {
1547
- reload: true
1548
- }, callback);
1549
- }
1550
- loadOne(name, prefix = '') {
1551
- const s = name.split('|');
1552
- const lng = s[0];
1553
- const ns = s[1];
1554
- this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
1555
- if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
1556
- if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
1557
- this.loaded(name, err, data);
1558
- });
1559
- }
1560
- saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
1561
- if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
1562
- this.logger.warn(`did not save key "${key}" as the namespace "${namespace}" was not yet loaded`, 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
1563
- return;
1564
- }
1565
- if (key === undefined || key === null || key === '') return;
1566
- if (this.backend?.create) {
1567
- const opts = {
1568
- ...options,
1569
- isUpdate
1570
- };
1571
- const fc = this.backend.create.bind(this.backend);
1572
- if (fc.length < 6) {
1573
- try {
1574
- let r;
1575
- if (fc.length === 5) {
1576
- r = fc(languages, namespace, key, fallbackValue, opts);
1577
- } else {
1578
- r = fc(languages, namespace, key, fallbackValue);
1579
- }
1580
- if (r && typeof r.then === 'function') {
1581
- r.then(data => clb(null, data)).catch(clb);
1582
- } else {
1583
- clb(null, r);
1584
- }
1585
- } catch (err) {
1586
- clb(err);
1587
- }
1588
- } else {
1589
- fc(languages, namespace, key, fallbackValue, clb, opts);
1590
- }
1591
- }
1592
- if (!languages || !languages[0]) return;
1593
- this.store.addResource(languages[0], namespace, key, fallbackValue);
1594
- }
1595
- }
1596
-
1597
- const get = () => ({
1598
- debug: false,
1599
- initAsync: true,
1600
- ns: ['translation'],
1601
- defaultNS: ['translation'],
1602
- fallbackLng: ['dev'],
1603
- fallbackNS: false,
1604
- supportedLngs: false,
1605
- nonExplicitSupportedLngs: false,
1606
- load: 'all',
1607
- preload: false,
1608
- simplifyPluralSuffix: true,
1609
- keySeparator: '.',
1610
- nsSeparator: ':',
1611
- pluralSeparator: '_',
1612
- contextSeparator: '_',
1613
- partialBundledLanguages: false,
1614
- saveMissing: false,
1615
- updateMissing: false,
1616
- saveMissingTo: 'fallback',
1617
- saveMissingPlurals: true,
1618
- missingKeyHandler: false,
1619
- missingInterpolationHandler: false,
1620
- postProcess: false,
1621
- postProcessPassResolved: false,
1622
- returnNull: false,
1623
- returnEmptyString: true,
1624
- returnObjects: false,
1625
- joinArrays: false,
1626
- returnedObjectHandler: false,
1627
- parseMissingKeyHandler: false,
1628
- appendNamespaceToMissingKey: false,
1629
- appendNamespaceToCIMode: false,
1630
- overloadTranslationOptionHandler: args => {
1631
- let ret = {};
1632
- if (typeof args[1] === 'object') ret = args[1];
1633
- if (isString(args[1])) ret.defaultValue = args[1];
1634
- if (isString(args[2])) ret.tDescription = args[2];
1635
- if (typeof args[2] === 'object' || typeof args[3] === 'object') {
1636
- const options = args[3] || args[2];
1637
- Object.keys(options).forEach(key => {
1638
- ret[key] = options[key];
1639
- });
1640
- }
1641
- return ret;
1642
- },
1643
- interpolation: {
1644
- escapeValue: true,
1645
- format: value => value,
1646
- prefix: '{{',
1647
- suffix: '}}',
1648
- formatSeparator: ',',
1649
- unescapePrefix: '-',
1650
- nestingPrefix: '$t(',
1651
- nestingSuffix: ')',
1652
- nestingOptionsSeparator: ',',
1653
- maxReplaces: 1000,
1654
- skipOnVariables: true
1655
- },
1656
- cacheInBuiltFormats: true
1657
- });
1658
- const transformOptions = options => {
1659
- if (isString(options.ns)) options.ns = [options.ns];
1660
- if (isString(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
1661
- if (isString(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
1662
- if (options.supportedLngs?.indexOf?.('cimode') < 0) {
1663
- options.supportedLngs = options.supportedLngs.concat(['cimode']);
1664
- }
1665
- if (typeof options.initImmediate === 'boolean') options.initAsync = options.initImmediate;
1666
- return options;
1667
- };
1668
-
1669
- const noop = () => {};
1670
- const bindMemberFunctions = inst => {
1671
- const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
1672
- mems.forEach(mem => {
1673
- if (typeof inst[mem] === 'function') {
1674
- inst[mem] = inst[mem].bind(inst);
1675
- }
1676
- });
1677
- };
1678
- class I18n extends EventEmitter {
1679
- constructor(options = {}, callback) {
1680
- super();
1681
- this.options = transformOptions(options);
1682
- this.services = {};
1683
- this.logger = baseLogger;
1684
- this.modules = {
1685
- external: []
1686
- };
1687
- bindMemberFunctions(this);
1688
- if (callback && !this.isInitialized && !options.isClone) {
1689
- if (!this.options.initAsync) {
1690
- this.init(options, callback);
1691
- return this;
1692
- }
1693
- setTimeout(() => {
1694
- this.init(options, callback);
1695
- }, 0);
1696
- }
1697
- }
1698
- init(options = {}, callback) {
1699
- this.isInitializing = true;
1700
- if (typeof options === 'function') {
1701
- callback = options;
1702
- options = {};
1703
- }
1704
- if (options.defaultNS == null && options.ns) {
1705
- if (isString(options.ns)) {
1706
- options.defaultNS = options.ns;
1707
- } else if (options.ns.indexOf('translation') < 0) {
1708
- options.defaultNS = options.ns[0];
1709
- }
1710
- }
1711
- const defOpts = get();
1712
- this.options = {
1713
- ...defOpts,
1714
- ...this.options,
1715
- ...transformOptions(options)
1716
- };
1717
- this.options.interpolation = {
1718
- ...defOpts.interpolation,
1719
- ...this.options.interpolation
1720
- };
1721
- if (options.keySeparator !== undefined) {
1722
- this.options.userDefinedKeySeparator = options.keySeparator;
1723
- }
1724
- if (options.nsSeparator !== undefined) {
1725
- this.options.userDefinedNsSeparator = options.nsSeparator;
1726
- }
1727
- const createClassOnDemand = ClassOrObject => {
1728
- if (!ClassOrObject) return null;
1729
- if (typeof ClassOrObject === 'function') return new ClassOrObject();
1730
- return ClassOrObject;
1731
- };
1732
- if (!this.options.isClone) {
1733
- if (this.modules.logger) {
1734
- baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
1735
- } else {
1736
- baseLogger.init(null, this.options);
1737
- }
1738
- let formatter;
1739
- if (this.modules.formatter) {
1740
- formatter = this.modules.formatter;
1741
- } else {
1742
- formatter = Formatter;
1743
- }
1744
- const lu = new LanguageUtil(this.options);
1745
- this.store = new ResourceStore(this.options.resources, this.options);
1746
- const s = this.services;
1747
- s.logger = baseLogger;
1748
- s.resourceStore = this.store;
1749
- s.languageUtils = lu;
1750
- s.pluralResolver = new PluralResolver(lu, {
1751
- prepend: this.options.pluralSeparator,
1752
- simplifyPluralSuffix: this.options.simplifyPluralSuffix
1753
- });
1754
- if (formatter && (!this.options.interpolation.format || this.options.interpolation.format === defOpts.interpolation.format)) {
1755
- s.formatter = createClassOnDemand(formatter);
1756
- s.formatter.init(s, this.options);
1757
- this.options.interpolation.format = s.formatter.format.bind(s.formatter);
1758
- }
1759
- s.interpolator = new Interpolator(this.options);
1760
- s.utils = {
1761
- hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
1762
- };
1763
- s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
1764
- s.backendConnector.on('*', (event, ...args) => {
1765
- this.emit(event, ...args);
1766
- });
1767
- if (this.modules.languageDetector) {
1768
- s.languageDetector = createClassOnDemand(this.modules.languageDetector);
1769
- if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
1770
- }
1771
- if (this.modules.i18nFormat) {
1772
- s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
1773
- if (s.i18nFormat.init) s.i18nFormat.init(this);
1774
- }
1775
- this.translator = new Translator(this.services, this.options);
1776
- this.translator.on('*', (event, ...args) => {
1777
- this.emit(event, ...args);
1778
- });
1779
- this.modules.external.forEach(m => {
1780
- if (m.init) m.init(this);
1781
- });
1782
- }
1783
- this.format = this.options.interpolation.format;
1784
- if (!callback) callback = noop;
1785
- if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
1786
- const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
1787
- if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
1788
- }
1789
- if (!this.services.languageDetector && !this.options.lng) {
1790
- this.logger.warn('init: no languageDetector is used and no lng is defined');
1791
- }
1792
- const storeApi = ['getResource', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage'];
1793
- storeApi.forEach(fcName => {
1794
- this[fcName] = (...args) => this.store[fcName](...args);
1795
- });
1796
- const storeApiChained = ['addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle'];
1797
- storeApiChained.forEach(fcName => {
1798
- this[fcName] = (...args) => {
1799
- this.store[fcName](...args);
1800
- return this;
1801
- };
1802
- });
1803
- const deferred = defer();
1804
- const load = () => {
1805
- const finish = (err, t) => {
1806
- this.isInitializing = false;
1807
- if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!');
1808
- this.isInitialized = true;
1809
- if (!this.options.isClone) this.logger.log('initialized', this.options);
1810
- this.emit('initialized', this.options);
1811
- deferred.resolve(t);
1812
- callback(err, t);
1813
- };
1814
- if (this.languages && !this.isInitialized) return finish(null, this.t.bind(this));
1815
- this.changeLanguage(this.options.lng, finish);
1816
- };
1817
- if (this.options.resources || !this.options.initAsync) {
1818
- load();
1819
- } else {
1820
- setTimeout(load, 0);
1821
- }
1822
- return deferred;
1823
- }
1824
- loadResources(language, callback = noop) {
1825
- let usedCallback = callback;
1826
- const usedLng = isString(language) ? language : this.language;
1827
- if (typeof language === 'function') usedCallback = language;
1828
- if (!this.options.resources || this.options.partialBundledLanguages) {
1829
- if (usedLng?.toLowerCase() === 'cimode' && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
1830
- const toLoad = [];
1831
- const append = lng => {
1832
- if (!lng) return;
1833
- if (lng === 'cimode') return;
1834
- const lngs = this.services.languageUtils.toResolveHierarchy(lng);
1835
- lngs.forEach(l => {
1836
- if (l === 'cimode') return;
1837
- if (toLoad.indexOf(l) < 0) toLoad.push(l);
1838
- });
1839
- };
1840
- if (!usedLng) {
1841
- const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
1842
- fallbacks.forEach(l => append(l));
1843
- } else {
1844
- append(usedLng);
1845
- }
1846
- this.options.preload?.forEach?.(l => append(l));
1847
- this.services.backendConnector.load(toLoad, this.options.ns, e => {
1848
- if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
1849
- usedCallback(e);
1850
- });
1851
- } else {
1852
- usedCallback(null);
1853
- }
1854
- }
1855
- reloadResources(lngs, ns, callback) {
1856
- const deferred = defer();
1857
- if (typeof lngs === 'function') {
1858
- callback = lngs;
1859
- lngs = undefined;
1860
- }
1861
- if (typeof ns === 'function') {
1862
- callback = ns;
1863
- ns = undefined;
1864
- }
1865
- if (!lngs) lngs = this.languages;
1866
- if (!ns) ns = this.options.ns;
1867
- if (!callback) callback = noop;
1868
- this.services.backendConnector.reload(lngs, ns, err => {
1869
- deferred.resolve();
1870
- callback(err);
1871
- });
1872
- return deferred;
1873
- }
1874
- use(module) {
1875
- if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
1876
- if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
1877
- if (module.type === 'backend') {
1878
- this.modules.backend = module;
1879
- }
1880
- if (module.type === 'logger' || module.log && module.warn && module.error) {
1881
- this.modules.logger = module;
1882
- }
1883
- if (module.type === 'languageDetector') {
1884
- this.modules.languageDetector = module;
1885
- }
1886
- if (module.type === 'i18nFormat') {
1887
- this.modules.i18nFormat = module;
1888
- }
1889
- if (module.type === 'postProcessor') {
1890
- postProcessor.addPostProcessor(module);
1891
- }
1892
- if (module.type === 'formatter') {
1893
- this.modules.formatter = module;
1894
- }
1895
- if (module.type === '3rdParty') {
1896
- this.modules.external.push(module);
1897
- }
1898
- return this;
1899
- }
1900
- setResolvedLanguage(l) {
1901
- if (!l || !this.languages) return;
1902
- if (['cimode', 'dev'].indexOf(l) > -1) return;
1903
- for (let li = 0; li < this.languages.length; li++) {
1904
- const lngInLngs = this.languages[li];
1905
- if (['cimode', 'dev'].indexOf(lngInLngs) > -1) continue;
1906
- if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
1907
- this.resolvedLanguage = lngInLngs;
1908
- break;
1909
- }
1910
- }
1911
- if (!this.resolvedLanguage && this.languages.indexOf(l) < 0 && this.store.hasLanguageSomeTranslations(l)) {
1912
- this.resolvedLanguage = l;
1913
- this.languages.unshift(l);
1914
- }
1915
- }
1916
- changeLanguage(lng, callback) {
1917
- this.isLanguageChangingTo = lng;
1918
- const deferred = defer();
1919
- this.emit('languageChanging', lng);
1920
- const setLngProps = l => {
1921
- this.language = l;
1922
- this.languages = this.services.languageUtils.toResolveHierarchy(l);
1923
- this.resolvedLanguage = undefined;
1924
- this.setResolvedLanguage(l);
1925
- };
1926
- const done = (err, l) => {
1927
- if (l) {
1928
- if (this.isLanguageChangingTo === lng) {
1929
- setLngProps(l);
1930
- this.translator.changeLanguage(l);
1931
- this.isLanguageChangingTo = undefined;
1932
- this.emit('languageChanged', l);
1933
- this.logger.log('languageChanged', l);
1934
- }
1935
- } else {
1936
- this.isLanguageChangingTo = undefined;
1937
- }
1938
- deferred.resolve((...args) => this.t(...args));
1939
- if (callback) callback(err, (...args) => this.t(...args));
1940
- };
1941
- const setLng = lngs => {
1942
- if (!lng && !lngs && this.services.languageDetector) lngs = [];
1943
- const fl = isString(lngs) ? lngs : lngs && lngs[0];
1944
- const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
1945
- if (l) {
1946
- if (!this.language) {
1947
- setLngProps(l);
1948
- }
1949
- if (!this.translator.language) this.translator.changeLanguage(l);
1950
- this.services.languageDetector?.cacheUserLanguage?.(l);
1951
- }
1952
- this.loadResources(l, err => {
1953
- done(err, l);
1954
- });
1955
- };
1956
- if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
1957
- setLng(this.services.languageDetector.detect());
1958
- } else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
1959
- if (this.services.languageDetector.detect.length === 0) {
1960
- this.services.languageDetector.detect().then(setLng);
1961
- } else {
1962
- this.services.languageDetector.detect(setLng);
1963
- }
1964
- } else {
1965
- setLng(lng);
1966
- }
1967
- return deferred;
1968
- }
1969
- getFixedT(lng, ns, keyPrefix) {
1970
- const fixedT = (key, opts, ...rest) => {
1971
- let o;
1972
- if (typeof opts !== 'object') {
1973
- o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
1974
- } else {
1975
- o = {
1976
- ...opts
1977
- };
1978
- }
1979
- o.lng = o.lng || fixedT.lng;
1980
- o.lngs = o.lngs || fixedT.lngs;
1981
- o.ns = o.ns || fixedT.ns;
1982
- if (o.keyPrefix !== '') o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
1983
- const keySeparator = this.options.keySeparator || '.';
1984
- let resultKey;
1985
- if (o.keyPrefix && Array.isArray(key)) {
1986
- resultKey = key.map(k => `${o.keyPrefix}${keySeparator}${k}`);
1987
- } else {
1988
- resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
1989
- }
1990
- return this.t(resultKey, o);
1991
- };
1992
- if (isString(lng)) {
1993
- fixedT.lng = lng;
1994
- } else {
1995
- fixedT.lngs = lng;
1996
- }
1997
- fixedT.ns = ns;
1998
- fixedT.keyPrefix = keyPrefix;
1999
- return fixedT;
2000
- }
2001
- t(...args) {
2002
- return this.translator?.translate(...args);
2003
- }
2004
- exists(...args) {
2005
- return this.translator?.exists(...args);
2006
- }
2007
- setDefaultNamespace(ns) {
2008
- this.options.defaultNS = ns;
2009
- }
2010
- hasLoadedNamespace(ns, options = {}) {
2011
- if (!this.isInitialized) {
2012
- this.logger.warn('hasLoadedNamespace: i18next was not initialized', this.languages);
2013
- return false;
2014
- }
2015
- if (!this.languages || !this.languages.length) {
2016
- this.logger.warn('hasLoadedNamespace: i18n.languages were undefined or empty', this.languages);
2017
- return false;
2018
- }
2019
- const lng = options.lng || this.resolvedLanguage || this.languages[0];
2020
- const fallbackLng = this.options ? this.options.fallbackLng : false;
2021
- const lastLng = this.languages[this.languages.length - 1];
2022
- if (lng.toLowerCase() === 'cimode') return true;
2023
- const loadNotPending = (l, n) => {
2024
- const loadState = this.services.backendConnector.state[`${l}|${n}`];
2025
- return loadState === -1 || loadState === 0 || loadState === 2;
2026
- };
2027
- if (options.precheck) {
2028
- const preResult = options.precheck(this, loadNotPending);
2029
- if (preResult !== undefined) return preResult;
2030
- }
2031
- if (this.hasResourceBundle(lng, ns)) return true;
2032
- if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
2033
- if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
2034
- return false;
2035
- }
2036
- loadNamespaces(ns, callback) {
2037
- const deferred = defer();
2038
- if (!this.options.ns) {
2039
- if (callback) callback();
2040
- return Promise.resolve();
2041
- }
2042
- if (isString(ns)) ns = [ns];
2043
- ns.forEach(n => {
2044
- if (this.options.ns.indexOf(n) < 0) this.options.ns.push(n);
2045
- });
2046
- this.loadResources(err => {
2047
- deferred.resolve();
2048
- if (callback) callback(err);
2049
- });
2050
- return deferred;
2051
- }
2052
- loadLanguages(lngs, callback) {
2053
- const deferred = defer();
2054
- if (isString(lngs)) lngs = [lngs];
2055
- const preloaded = this.options.preload || [];
2056
- const newLngs = lngs.filter(lng => preloaded.indexOf(lng) < 0 && this.services.languageUtils.isSupportedCode(lng));
2057
- if (!newLngs.length) {
2058
- if (callback) callback();
2059
- return Promise.resolve();
2060
- }
2061
- this.options.preload = preloaded.concat(newLngs);
2062
- this.loadResources(err => {
2063
- deferred.resolve();
2064
- if (callback) callback(err);
2065
- });
2066
- return deferred;
2067
- }
2068
- dir(lng) {
2069
- if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
2070
- if (!lng) return 'rtl';
2071
- const rtlLngs = ['ar', 'shu', 'sqr', 'ssh', 'xaa', 'yhd', 'yud', 'aao', 'abh', 'abv', 'acm', 'acq', 'acw', 'acx', 'acy', 'adf', 'ads', 'aeb', 'aec', 'afb', 'ajp', 'apc', 'apd', 'arb', 'arq', 'ars', 'ary', 'arz', 'auz', 'avl', 'ayh', 'ayl', 'ayn', 'ayp', 'bbz', 'pga', 'he', 'iw', 'ps', 'pbt', 'pbu', 'pst', 'prp', 'prd', 'ug', 'ur', 'ydd', 'yds', 'yih', 'ji', 'yi', 'hbo', 'men', 'xmn', 'fa', 'jpr', 'peo', 'pes', 'prs', 'dv', 'sam', 'ckb'];
2072
- const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
2073
- return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf('-arab') > 1 ? 'rtl' : 'ltr';
2074
- }
2075
- static createInstance(options = {}, callback) {
2076
- return new I18n(options, callback);
2077
- }
2078
- cloneInstance(options = {}, callback = noop) {
2079
- const forkResourceStore = options.forkResourceStore;
2080
- if (forkResourceStore) delete options.forkResourceStore;
2081
- const mergedOptions = {
2082
- ...this.options,
2083
- ...options,
2084
- ...{
2085
- isClone: true
2086
- }
2087
- };
2088
- const clone = new I18n(mergedOptions);
2089
- if (options.debug !== undefined || options.prefix !== undefined) {
2090
- clone.logger = clone.logger.clone(options);
2091
- }
2092
- const membersToCopy = ['store', 'services', 'language'];
2093
- membersToCopy.forEach(m => {
2094
- clone[m] = this[m];
2095
- });
2096
- clone.services = {
2097
- ...this.services
2098
- };
2099
- clone.services.utils = {
2100
- hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
2101
- };
2102
- if (forkResourceStore) {
2103
- const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
2104
- prev[l] = {
2105
- ...this.store.data[l]
2106
- };
2107
- prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
2108
- acc[n] = {
2109
- ...prev[l][n]
2110
- };
2111
- return acc;
2112
- }, prev[l]);
2113
- return prev;
2114
- }, {});
2115
- clone.store = new ResourceStore(clonedData, mergedOptions);
2116
- clone.services.resourceStore = clone.store;
2117
- }
2118
- clone.translator = new Translator(clone.services, mergedOptions);
2119
- clone.translator.on('*', (event, ...args) => {
2120
- clone.emit(event, ...args);
2121
- });
2122
- clone.init(mergedOptions, callback);
2123
- clone.translator.options = mergedOptions;
2124
- clone.translator.backendConnector.services.utils = {
2125
- hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
2126
- };
2127
- return clone;
2128
- }
2129
- toJSON() {
2130
- return {
2131
- options: this.options,
2132
- store: this.store,
2133
- language: this.language,
2134
- languages: this.languages,
2135
- resolvedLanguage: this.resolvedLanguage
2136
- };
2137
- }
2138
- }
2139
- const instance = I18n.createInstance();
2140
- instance.createInstance = I18n.createInstance;
2141
-
2142
- instance.createInstance;
2143
- instance.dir;
2144
- instance.init;
2145
- instance.loadResources;
2146
- instance.reloadResources;
2147
- instance.use;
2148
- instance.changeLanguage;
2149
- instance.getFixedT;
2150
- instance.t;
2151
- instance.exists;
2152
- instance.setDefaultNamespace;
2153
- instance.hasLoadedNamespace;
2154
- instance.loadNamespaces;
2155
- instance.loadLanguages;
2156
-
2157
- function _classCallCheck$1(a, n) {
2158
- if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
2159
- }
2160
-
2161
- function _typeof$3(o) {
2162
- "@babel/helpers - typeof";
2163
-
2164
- return _typeof$3 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
2165
- return typeof o;
2166
- } : function (o) {
2167
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
2168
- }, _typeof$3(o);
2169
- }
2170
-
2171
- function toPrimitive(t, r) {
2172
- if ("object" != _typeof$3(t) || !t) return t;
2173
- var e = t[Symbol.toPrimitive];
2174
- if (void 0 !== e) {
2175
- var i = e.call(t, r);
2176
- if ("object" != _typeof$3(i)) return i;
2177
- throw new TypeError("@@toPrimitive must return a primitive value.");
2178
- }
2179
- return (String )(t);
2180
- }
2181
-
2182
- function toPropertyKey(t) {
2183
- var i = toPrimitive(t, "string");
2184
- return "symbol" == _typeof$3(i) ? i : i + "";
2185
- }
2186
-
2187
- function _defineProperties$1(e, r) {
2188
- for (var t = 0; t < r.length; t++) {
2189
- var o = r[t];
2190
- o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, toPropertyKey(o.key), o);
2191
- }
2192
- }
2193
- function _createClass$1(e, r, t) {
2194
- return r && _defineProperties$1(e.prototype, r), Object.defineProperty(e, "prototype", {
2195
- writable: false
2196
- }), e;
2197
- }
2198
-
2199
- var arr = [];
2200
- var each = arr.forEach;
2201
- var slice = arr.slice;
2202
- function defaults(obj) {
2203
- each.call(slice.call(arguments, 1), function (source) {
2204
- if (source) {
2205
- for (var prop in source) {
2206
- if (obj[prop] === undefined) obj[prop] = source[prop];
2207
- }
2208
- }
2209
- });
2210
- return obj;
2211
- }
2212
- function createClassOnDemand(ClassOrObject) {
2213
- if (!ClassOrObject) return null;
2214
- if (typeof ClassOrObject === 'function') return new ClassOrObject();
2215
- return ClassOrObject;
2216
- }
2217
-
2218
- function getDefaults$2() {
2219
- return {
2220
- handleEmptyResourcesAsFailed: true,
2221
- cacheHitMode: 'none'
2222
- // reloadInterval: typeof window !== 'undefined' ? false : 60 * 60 * 1000
2223
- // refreshExpirationTime: 60 * 60 * 1000
2224
- };
2225
- }
2226
-
2227
- function handleCorrectReadFunction(backend, language, namespace, resolver) {
2228
- var fc = backend.read.bind(backend);
2229
- if (fc.length === 2) {
2230
- // no callback
2231
- try {
2232
- var r = fc(language, namespace);
2233
- if (r && typeof r.then === 'function') {
2234
- // promise
2235
- r.then(function (data) {
2236
- return resolver(null, data);
2237
- })["catch"](resolver);
2238
- } else {
2239
- // sync
2240
- resolver(null, r);
2241
- }
2242
- } catch (err) {
2243
- resolver(err);
2244
- }
2245
- return;
2246
- }
2247
-
2248
- // normal with callback
2249
- fc(language, namespace, resolver);
2250
- }
2251
- var Backend$1 = /*#__PURE__*/function () {
2252
- function Backend(services) {
2253
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2254
- var i18nextOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
2255
- _classCallCheck$1(this, Backend);
2256
- this.backends = [];
2257
- this.type = 'backend';
2258
- this.allOptions = i18nextOptions;
2259
- this.init(services, options);
2260
- }
2261
- _createClass$1(Backend, [{
2262
- key: "init",
2263
- value: function init(services) {
2264
- var _this = this;
2265
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2266
- var i18nextOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
2267
- this.services = services;
2268
- this.options = defaults(options, this.options || {}, getDefaults$2());
2269
- this.allOptions = i18nextOptions;
2270
- this.options.backends && this.options.backends.forEach(function (b, i) {
2271
- _this.backends[i] = _this.backends[i] || createClassOnDemand(b);
2272
- _this.backends[i].init(services, _this.options.backendOptions && _this.options.backendOptions[i] || {}, i18nextOptions);
2273
- });
2274
- if (this.services && this.options.reloadInterval) {
2275
- setInterval(function () {
2276
- return _this.reload();
2277
- }, this.options.reloadInterval);
2278
- }
2279
- }
2280
- }, {
2281
- key: "read",
2282
- value: function read(language, namespace, callback) {
2283
- var _this2 = this;
2284
- var bLen = this.backends.length;
2285
- var loadPosition = function loadPosition(pos) {
2286
- if (pos >= bLen) return callback(new Error('non of the backend loaded data', true)); // failed pass retry flag
2287
- var isLastBackend = pos === bLen - 1;
2288
- var lengthCheckAmount = _this2.options.handleEmptyResourcesAsFailed && !isLastBackend ? 0 : -1;
2289
- var backend = _this2.backends[pos];
2290
- if (backend.read) {
2291
- handleCorrectReadFunction(backend, language, namespace, function (err, data, savedAt) {
2292
- if (!err && data && Object.keys(data).length > lengthCheckAmount) {
2293
- callback(null, data, pos);
2294
- savePosition(pos - 1, data); // save one in front
2295
- if (backend.save && _this2.options.cacheHitMode && ['refresh', 'refreshAndUpdateStore'].indexOf(_this2.options.cacheHitMode) > -1) {
2296
- if (savedAt && _this2.options.refreshExpirationTime && savedAt + _this2.options.refreshExpirationTime > Date.now()) return;
2297
- var nextBackend = _this2.backends[pos + 1];
2298
- if (nextBackend && nextBackend.read) {
2299
- handleCorrectReadFunction(nextBackend, language, namespace, function (err, data) {
2300
- if (err) return;
2301
- if (!data) return;
2302
- if (Object.keys(data).length <= lengthCheckAmount) return;
2303
- savePosition(pos, data);
2304
- if (_this2.options.cacheHitMode !== 'refreshAndUpdateStore') return;
2305
- if (_this2.services && _this2.services.resourceStore) {
2306
- _this2.services.resourceStore.addResourceBundle(language, namespace, data);
2307
- }
2308
- });
2309
- }
2310
- }
2311
- } else {
2312
- loadPosition(pos + 1); // try load from next
2313
- }
2314
- });
2315
- } else {
2316
- loadPosition(pos + 1); // try load from next
2317
- }
2318
- };
2319
-
2320
- var savePosition = function savePosition(pos, data) {
2321
- if (pos < 0) return;
2322
- var backend = _this2.backends[pos];
2323
- if (backend.save) {
2324
- backend.save(language, namespace, data);
2325
- savePosition(pos - 1, data);
2326
- } else {
2327
- savePosition(pos - 1, data);
2328
- }
2329
- };
2330
- loadPosition(0);
2331
- }
2332
- }, {
2333
- key: "create",
2334
- value: function create(languages, namespace, key, fallbackValue) {
2335
- var clb = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : function () {};
2336
- var opts = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
2337
- this.backends.forEach(function (b) {
2338
- if (!b.create) return;
2339
- var fc = b.create.bind(b);
2340
- if (fc.length < 6) {
2341
- // no callback
2342
- try {
2343
- var r;
2344
- if (fc.length === 5) {
2345
- // future callback-less api for i18next-locize-backend
2346
- r = fc(languages, namespace, key, fallbackValue, opts);
2347
- } else {
2348
- r = fc(languages, namespace, key, fallbackValue);
2349
- }
2350
- if (r && typeof r.then === 'function') {
2351
- // promise
2352
- r.then(function (data) {
2353
- return clb(null, data);
2354
- })["catch"](clb);
2355
- } else {
2356
- // sync
2357
- clb(null, r);
2358
- }
2359
- } catch (err) {
2360
- clb(err);
2361
- }
2362
- return;
2363
- }
2364
-
2365
- // normal with callback
2366
- fc(languages, namespace, key, fallbackValue, clb /* unused callback */, opts);
2367
- });
2368
- }
2369
- }, {
2370
- key: "reload",
2371
- value: function reload() {
2372
- var _this3 = this;
2373
- var _this$services = this.services,
2374
- backendConnector = _this$services.backendConnector,
2375
- languageUtils = _this$services.languageUtils,
2376
- logger = _this$services.logger;
2377
- var currentLanguage = backendConnector.language;
2378
- if (currentLanguage && currentLanguage.toLowerCase() === 'cimode') return; // avoid loading resources for cimode
2379
-
2380
- var toLoad = [];
2381
- var append = function append(lng) {
2382
- var lngs = languageUtils.toResolveHierarchy(lng);
2383
- lngs.forEach(function (l) {
2384
- if (toLoad.indexOf(l) < 0) toLoad.push(l);
2385
- });
2386
- };
2387
- append(currentLanguage);
2388
- if (this.allOptions.preload) this.allOptions.preload.forEach(function (l) {
2389
- return append(l);
2390
- });
2391
- toLoad.forEach(function (lng) {
2392
- _this3.allOptions.ns.forEach(function (ns) {
2393
- backendConnector.read(lng, ns, 'read', null, null, function (err, data) {
2394
- if (err) logger.warn("loading namespace ".concat(ns, " for language ").concat(lng, " failed"), err);
2395
- if (!err && data) logger.log("loaded namespace ".concat(ns, " for language ").concat(lng), data);
2396
- backendConnector.loaded("".concat(lng, "|").concat(ns), err, data);
2397
- });
2398
- });
2399
- });
2400
- }
2401
- }]);
2402
- return Backend;
2403
- }();
2404
- Backend$1.type = 'backend';
2405
-
2406
- function _typeof$2(o) { "@babel/helpers - typeof"; return _typeof$2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof$2(o); }
2407
- function hasXMLHttpRequest() {
2408
- return typeof XMLHttpRequest === 'function' || (typeof XMLHttpRequest === "undefined" ? "undefined" : _typeof$2(XMLHttpRequest)) === 'object';
2409
- }
2410
- function isPromise(maybePromise) {
2411
- return !!maybePromise && typeof maybePromise.then === 'function';
2412
- }
2413
- function makePromise(maybePromise) {
2414
- if (isPromise(maybePromise)) {
2415
- return maybePromise;
2416
- }
2417
- return Promise.resolve(maybePromise);
2418
- }
2419
-
2420
- function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2421
- function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), true).forEach(function (r) { _defineProperty$2(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
2422
- function _defineProperty$2(e, r, t) { return (r = _toPropertyKey$1(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e; }
2423
- function _toPropertyKey$1(t) { var i = _toPrimitive$1(t, "string"); return "symbol" == _typeof$1(i) ? i : i + ""; }
2424
- function _toPrimitive$1(t, r) { if ("object" != _typeof$1(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r); if ("object" != _typeof$1(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
2425
- function _typeof$1(o) { "@babel/helpers - typeof"; return _typeof$1 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof$1(o); }
2426
- var fetchApi = typeof fetch === 'function' ? fetch : undefined;
2427
- if (typeof global !== 'undefined' && global.fetch) {
2428
- fetchApi = global.fetch;
2429
- } else if (typeof window !== 'undefined' && window.fetch) {
2430
- fetchApi = window.fetch;
2431
- }
2432
- var XmlHttpRequestApi;
2433
- if (hasXMLHttpRequest()) {
2434
- if (typeof global !== 'undefined' && global.XMLHttpRequest) {
2435
- XmlHttpRequestApi = global.XMLHttpRequest;
2436
- } else if (typeof window !== 'undefined' && window.XMLHttpRequest) {
2437
- XmlHttpRequestApi = window.XMLHttpRequest;
2438
- }
2439
- }
2440
- var ActiveXObjectApi;
2441
- if (typeof ActiveXObject === 'function') {
2442
- if (typeof global !== 'undefined' && global.ActiveXObject) {
2443
- ActiveXObjectApi = global.ActiveXObject;
2444
- } else if (typeof window !== 'undefined' && window.ActiveXObject) {
2445
- ActiveXObjectApi = window.ActiveXObject;
2446
- }
2447
- }
2448
- if (typeof fetchApi !== 'function') fetchApi = undefined;
2449
- if (!fetchApi && !XmlHttpRequestApi && !ActiveXObjectApi) {
2450
- try {
2451
- import('cross-fetch').then(function (mod) {
2452
- fetchApi = mod.default;
2453
- }).catch(function () {});
2454
- } catch (e) {}
2455
- }
2456
- var addQueryString = function addQueryString(url, params) {
2457
- if (params && _typeof$1(params) === 'object') {
2458
- var queryString = '';
2459
- for (var paramName in params) {
2460
- queryString += '&' + encodeURIComponent(paramName) + '=' + encodeURIComponent(params[paramName]);
2461
- }
2462
- if (!queryString) return url;
2463
- url = url + (url.indexOf('?') !== -1 ? '&' : '?') + queryString.slice(1);
2464
- }
2465
- return url;
2466
- };
2467
- var fetchIt = function fetchIt(url, fetchOptions, callback, altFetch) {
2468
- var resolver = function resolver(response) {
2469
- if (!response.ok) return callback(response.statusText || 'Error', {
2470
- status: response.status
2471
- });
2472
- response.text().then(function (data) {
2473
- callback(null, {
2474
- status: response.status,
2475
- data: data
2476
- });
2477
- }).catch(callback);
2478
- };
2479
- if (altFetch) {
2480
- var altResponse = altFetch(url, fetchOptions);
2481
- if (altResponse instanceof Promise) {
2482
- altResponse.then(resolver).catch(callback);
2483
- return;
2484
- }
2485
- }
2486
- if (typeof fetch === 'function') {
2487
- fetch(url, fetchOptions).then(resolver).catch(callback);
2488
- } else {
2489
- fetchApi(url, fetchOptions).then(resolver).catch(callback);
2490
- }
2491
- };
2492
- var omitFetchOptions = false;
2493
- var requestWithFetch = function requestWithFetch(options, url, payload, callback) {
2494
- if (options.queryStringParams) {
2495
- url = addQueryString(url, options.queryStringParams);
2496
- }
2497
- var headers = _objectSpread$2({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders);
2498
- if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) {
2499
- headers['User-Agent'] = "i18next-http-backend (node/".concat(global.process.version, "; ").concat(global.process.platform, " ").concat(global.process.arch, ")");
2500
- }
2501
- if (payload) headers['Content-Type'] = 'application/json';
2502
- var reqOptions = typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions;
2503
- var fetchOptions = _objectSpread$2({
2504
- method: payload ? 'POST' : 'GET',
2505
- body: payload ? options.stringify(payload) : undefined,
2506
- headers: headers
2507
- }, omitFetchOptions ? {} : reqOptions);
2508
- var altFetch = typeof options.alternateFetch === 'function' && options.alternateFetch.length >= 1 ? options.alternateFetch : undefined;
2509
- try {
2510
- fetchIt(url, fetchOptions, callback, altFetch);
2511
- } catch (e) {
2512
- if (!reqOptions || Object.keys(reqOptions).length === 0 || !e.message || e.message.indexOf('not implemented') < 0) {
2513
- return callback(e);
2514
- }
2515
- try {
2516
- Object.keys(reqOptions).forEach(function (opt) {
2517
- delete fetchOptions[opt];
2518
- });
2519
- fetchIt(url, fetchOptions, callback, altFetch);
2520
- omitFetchOptions = true;
2521
- } catch (err) {
2522
- callback(err);
2523
- }
2524
- }
2525
- };
2526
- var requestWithXmlHttpRequest = function requestWithXmlHttpRequest(options, url, payload, callback) {
2527
- if (payload && _typeof$1(payload) === 'object') {
2528
- payload = addQueryString('', payload).slice(1);
2529
- }
2530
- if (options.queryStringParams) {
2531
- url = addQueryString(url, options.queryStringParams);
2532
- }
2533
- try {
2534
- var x = XmlHttpRequestApi ? new XmlHttpRequestApi() : new ActiveXObjectApi('MSXML2.XMLHTTP.3.0');
2535
- x.open(payload ? 'POST' : 'GET', url, 1);
2536
- if (!options.crossDomain) {
2537
- x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
2538
- }
2539
- x.withCredentials = !!options.withCredentials;
2540
- if (payload) {
2541
- x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2542
- }
2543
- if (x.overrideMimeType) {
2544
- x.overrideMimeType('application/json');
2545
- }
2546
- var h = options.customHeaders;
2547
- h = typeof h === 'function' ? h() : h;
2548
- if (h) {
2549
- for (var i in h) {
2550
- x.setRequestHeader(i, h[i]);
2551
- }
2552
- }
2553
- x.onreadystatechange = function () {
2554
- x.readyState > 3 && callback(x.status >= 400 ? x.statusText : null, {
2555
- status: x.status,
2556
- data: x.responseText
2557
- });
2558
- };
2559
- x.send(payload);
2560
- } catch (e) {
2561
- console && console.log(e);
2562
- }
2563
- };
2564
- var request = function request(options, url, payload, callback) {
2565
- if (typeof payload === 'function') {
2566
- callback = payload;
2567
- payload = undefined;
2568
- }
2569
- callback = callback || function () {};
2570
- if (fetchApi && url.indexOf('file:') !== 0) {
2571
- return requestWithFetch(options, url, payload, callback);
2572
- }
2573
- if (hasXMLHttpRequest() || typeof ActiveXObject === 'function') {
2574
- return requestWithXmlHttpRequest(options, url, payload, callback);
2575
- }
2576
- callback(new Error('No fetch and no xhr implementation found!'));
2577
- };
2578
-
2579
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2580
- function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2581
- function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty$1(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
2582
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
2583
- function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
2584
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", { writable: false }), e; }
2585
- function _defineProperty$1(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e; }
2586
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
2587
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return (String )(t); }
2588
- var getDefaults$1 = function getDefaults() {
2589
- return {
2590
- loadPath: '/locales/{{lng}}/{{ns}}.json',
2591
- addPath: '/locales/add/{{lng}}/{{ns}}',
2592
- parse: function parse(data) {
2593
- return JSON.parse(data);
2594
- },
2595
- stringify: JSON.stringify,
2596
- parsePayload: function parsePayload(namespace, key, fallbackValue) {
2597
- return _defineProperty$1({}, key, fallbackValue || '');
2598
- },
2599
- parseLoadPayload: function parseLoadPayload(languages, namespaces) {
2600
- return undefined;
2601
- },
2602
- request: request,
2603
- reloadInterval: typeof window !== 'undefined' ? false : 60 * 60 * 1000,
2604
- customHeaders: {},
2605
- queryStringParams: {},
2606
- crossDomain: false,
2607
- withCredentials: false,
2608
- overrideMimeType: false,
2609
- requestOptions: {
2610
- mode: 'cors',
2611
- credentials: 'same-origin',
2612
- cache: 'default'
2613
- }
2614
- };
2615
- };
2616
- var Backend = function () {
2617
- function Backend(services) {
2618
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2619
- var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
2620
- _classCallCheck(this, Backend);
2621
- this.services = services;
2622
- this.options = options;
2623
- this.allOptions = allOptions;
2624
- this.type = 'backend';
2625
- this.init(services, options, allOptions);
2626
- }
2627
- return _createClass(Backend, [{
2628
- key: "init",
2629
- value: function init(services) {
2630
- var _this = this;
2631
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2632
- var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
2633
- this.services = services;
2634
- this.options = _objectSpread$1(_objectSpread$1(_objectSpread$1({}, getDefaults$1()), this.options || {}), options);
2635
- this.allOptions = allOptions;
2636
- if (this.services && this.options.reloadInterval) {
2637
- var timer = setInterval(function () {
2638
- return _this.reload();
2639
- }, this.options.reloadInterval);
2640
- if (_typeof(timer) === 'object' && typeof timer.unref === 'function') timer.unref();
2641
- }
2642
- }
2643
- }, {
2644
- key: "readMulti",
2645
- value: function readMulti(languages, namespaces, callback) {
2646
- this._readAny(languages, languages, namespaces, namespaces, callback);
2647
- }
2648
- }, {
2649
- key: "read",
2650
- value: function read(language, namespace, callback) {
2651
- this._readAny([language], language, [namespace], namespace, callback);
2652
- }
2653
- }, {
2654
- key: "_readAny",
2655
- value: function _readAny(languages, loadUrlLanguages, namespaces, loadUrlNamespaces, callback) {
2656
- var _this2 = this;
2657
- var loadPath = this.options.loadPath;
2658
- if (typeof this.options.loadPath === 'function') {
2659
- loadPath = this.options.loadPath(languages, namespaces);
2660
- }
2661
- loadPath = makePromise(loadPath);
2662
- loadPath.then(function (resolvedLoadPath) {
2663
- if (!resolvedLoadPath) return callback(null, {});
2664
- var url = _this2.services.interpolator.interpolate(resolvedLoadPath, {
2665
- lng: languages.join('+'),
2666
- ns: namespaces.join('+')
2667
- });
2668
- _this2.loadUrl(url, callback, loadUrlLanguages, loadUrlNamespaces);
2669
- });
2670
- }
2671
- }, {
2672
- key: "loadUrl",
2673
- value: function loadUrl(url, callback, languages, namespaces) {
2674
- var _this3 = this;
2675
- var lng = typeof languages === 'string' ? [languages] : languages;
2676
- var ns = typeof namespaces === 'string' ? [namespaces] : namespaces;
2677
- var payload = this.options.parseLoadPayload(lng, ns);
2678
- this.options.request(this.options, url, payload, function (err, res) {
2679
- if (res && (res.status >= 500 && res.status < 600 || !res.status)) return callback('failed loading ' + url + '; status code: ' + res.status, true);
2680
- if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url + '; status code: ' + res.status, false);
2681
- if (!res && err && err.message) {
2682
- var errorMessage = err.message.toLowerCase();
2683
- var isNetworkError = ['failed', 'fetch', 'network', 'load'].find(function (term) {
2684
- return errorMessage.indexOf(term) > -1;
2685
- });
2686
- if (isNetworkError) {
2687
- return callback('failed loading ' + url + ': ' + err.message, true);
2688
- }
2689
- }
2690
- if (err) return callback(err, false);
2691
- var ret, parseErr;
2692
- try {
2693
- if (typeof res.data === 'string') {
2694
- ret = _this3.options.parse(res.data, languages, namespaces);
2695
- } else {
2696
- ret = res.data;
2697
- }
2698
- } catch (e) {
2699
- parseErr = 'failed parsing ' + url + ' to json';
2700
- }
2701
- if (parseErr) return callback(parseErr, false);
2702
- callback(null, ret);
2703
- });
2704
- }
2705
- }, {
2706
- key: "create",
2707
- value: function create(languages, namespace, key, fallbackValue, callback) {
2708
- var _this4 = this;
2709
- if (!this.options.addPath) return;
2710
- if (typeof languages === 'string') languages = [languages];
2711
- var payload = this.options.parsePayload(namespace, key, fallbackValue);
2712
- var finished = 0;
2713
- var dataArray = [];
2714
- var resArray = [];
2715
- languages.forEach(function (lng) {
2716
- var addPath = _this4.options.addPath;
2717
- if (typeof _this4.options.addPath === 'function') {
2718
- addPath = _this4.options.addPath(lng, namespace);
2719
- }
2720
- var url = _this4.services.interpolator.interpolate(addPath, {
2721
- lng: lng,
2722
- ns: namespace
2723
- });
2724
- _this4.options.request(_this4.options, url, payload, function (data, res) {
2725
- finished += 1;
2726
- dataArray.push(data);
2727
- resArray.push(res);
2728
- if (finished === languages.length) {
2729
- if (typeof callback === 'function') callback(dataArray, resArray);
2730
- }
2731
- });
2732
- });
2733
- }
2734
- }, {
2735
- key: "reload",
2736
- value: function reload() {
2737
- var _this5 = this;
2738
- var _this$services = this.services,
2739
- backendConnector = _this$services.backendConnector,
2740
- languageUtils = _this$services.languageUtils,
2741
- logger = _this$services.logger;
2742
- var currentLanguage = backendConnector.language;
2743
- if (currentLanguage && currentLanguage.toLowerCase() === 'cimode') return;
2744
- var toLoad = [];
2745
- var append = function append(lng) {
2746
- var lngs = languageUtils.toResolveHierarchy(lng);
2747
- lngs.forEach(function (l) {
2748
- if (toLoad.indexOf(l) < 0) toLoad.push(l);
2749
- });
2750
- };
2751
- append(currentLanguage);
2752
- if (this.allOptions.preload) this.allOptions.preload.forEach(function (l) {
2753
- return append(l);
2754
- });
2755
- toLoad.forEach(function (lng) {
2756
- _this5.allOptions.ns.forEach(function (ns) {
2757
- backendConnector.read(lng, ns, 'read', null, null, function (err, data) {
2758
- if (err) logger.warn("loading namespace ".concat(ns, " for language ").concat(lng, " failed"), err);
2759
- if (!err && data) logger.log("loaded namespace ".concat(ns, " for language ").concat(lng), data);
2760
- backendConnector.loaded("".concat(lng, "|").concat(ns), err, data);
2761
- });
2762
- });
2763
- });
2764
- }
2765
- }]);
2766
- }();
2767
- Backend.type = 'backend';
2768
-
2769
- function _defineProperty(e, r, t) {
2770
- return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
2771
- value: t,
2772
- enumerable: true,
2773
- configurable: true,
2774
- writable: true
2775
- }) : e[r] = t, e;
2776
- }
2777
-
2778
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2779
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
2780
- /* eslint-disable max-classes-per-file */
2781
- var Storage = /*#__PURE__*/function () {
2782
- function Storage(options) {
2783
- _classCallCheck$1(this, Storage);
2784
- this.store = options.store;
2785
- }
2786
- _createClass$1(Storage, [{
2787
- key: "setItem",
2788
- value: function setItem(key, value) {
2789
- if (this.store) {
2790
- try {
2791
- this.store.setItem(key, value);
2792
- } catch (e) {
2793
- // f.log('failed to set value for key "' + key + '" to localStorage.');
2794
- }
2795
- }
2796
- }
2797
- }, {
2798
- key: "getItem",
2799
- value: function getItem(key, value) {
2800
- if (this.store) {
2801
- try {
2802
- return this.store.getItem(key, value);
2803
- } catch (e) {
2804
- // f.log('failed to get value for key "' + key + '" from localStorage.');
2805
- }
2806
- }
2807
- return undefined;
2808
- }
2809
- }]);
2810
- return Storage;
2811
- }();
2812
- function getDefaults() {
2813
- var store = null;
2814
- try {
2815
- store = window.localStorage;
2816
- } catch (e) {
2817
- if (typeof window !== 'undefined') {
2818
- console.log('Failed to load local storage.', e);
2819
- }
2820
- }
2821
- return {
2822
- prefix: 'i18next_res_',
2823
- expirationTime: 7 * 24 * 60 * 60 * 1000,
2824
- defaultVersion: undefined,
2825
- versions: {},
2826
- store: store
2827
- };
2828
- }
2829
- var Cache = /*#__PURE__*/function () {
2830
- function Cache(services) {
2831
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2832
- _classCallCheck$1(this, Cache);
2833
- this.init(services, options);
2834
- this.type = 'backend';
2835
- }
2836
- _createClass$1(Cache, [{
2837
- key: "init",
2838
- value: function init(services) {
2839
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2840
- this.services = services;
2841
- this.options = _objectSpread(_objectSpread(_objectSpread({}, getDefaults()), this.options), options);
2842
- this.storage = new Storage(this.options);
2843
- }
2844
- }, {
2845
- key: "read",
2846
- value: function read(language, namespace, callback) {
2847
- var nowMS = Date.now();
2848
- if (!this.storage.store) {
2849
- return callback(null, null);
2850
- }
2851
- var local = this.storage.getItem("".concat(this.options.prefix).concat(language, "-").concat(namespace));
2852
- if (local) {
2853
- local = JSON.parse(local);
2854
- var version = this.getVersion(language);
2855
- if (
2856
- // expiration field is mandatory, and should not be expired
2857
- local.i18nStamp && local.i18nStamp + this.options.expirationTime > nowMS &&
2858
- // there should be no language version set, or if it is, it should match the one in translation
2859
- version === local.i18nVersion) {
2860
- var i18nStamp = local.i18nStamp;
2861
- delete local.i18nVersion;
2862
- delete local.i18nStamp;
2863
- return callback(null, local, i18nStamp);
2864
- }
2865
- }
2866
- return callback(null, null);
2867
- }
2868
- }, {
2869
- key: "save",
2870
- value: function save(language, namespace, data) {
2871
- if (this.storage.store) {
2872
- data.i18nStamp = Date.now();
2873
-
2874
- // language version (if set)
2875
- var version = this.getVersion(language);
2876
- if (version) {
2877
- data.i18nVersion = version;
2878
- }
2879
-
2880
- // save
2881
- this.storage.setItem("".concat(this.options.prefix).concat(language, "-").concat(namespace), JSON.stringify(data));
2882
- }
2883
- }
2884
- }, {
2885
- key: "getVersion",
2886
- value: function getVersion(language) {
2887
- return this.options.versions[language] || this.options.defaultVersion;
2888
- }
2889
- }]);
2890
- return Cache;
2891
- }();
2892
- Cache.type = 'backend';
2893
-
2894
- var resourcesToBackend = function resourcesToBackend(res) {
2895
- return {
2896
- type: 'backend',
2897
- init: function init(services, backendOptions, i18nextOptions) {},
2898
- read: function read(language, namespace, callback) {
2899
- if (typeof res === 'function') {
2900
- if (res.length < 3) {
2901
- try {
2902
- var r = res(language, namespace);
2903
- if (r && typeof r.then === 'function') {
2904
- r.then(function (data) {
2905
- return callback(null, data && data.default || data);
2906
- }).catch(callback);
2907
- } else {
2908
- callback(null, r);
2909
- }
2910
- } catch (err) {
2911
- callback(err);
2912
- }
2913
- return;
2914
- }
2915
- res(language, namespace, callback);
2916
- return;
2917
- }
2918
- callback(null, res && res[language] && res[language][namespace]);
2919
- }
2920
- };
2921
- };
2922
-
2923
- const initI18n = _ref => {
2924
- let {
2925
- initReactI18next,
2926
- lang,
2927
- fallbackLang,
2928
- langCacheExpiredTimeMs,
2929
- resources,
2930
- version,
2931
- useBackend,
2932
- withLocalstorageBackend = false,
2933
- cdnUrl = '',
2934
- debug = false
2935
- } = _ref;
2936
- let backends = [];
2937
- let backendOptions = [];
2938
- const langs = Object.keys(resources);
2939
- // https://www.i18next.com/how-to/backend-fallback
2940
- if (useBackend && withLocalstorageBackend) {
2941
- const versions = {};
2942
- langs.forEach(value => Object.assign(versions, {
2943
- [value]: version
2944
- }));
2945
- backends.push(Cache);
2946
- backendOptions.push({
2947
- // prefix for stored languages
2948
- prefix: `i18next_res_`,
2949
- // expiration
2950
- expirationTime: langCacheExpiredTimeMs,
2951
- // language versions
2952
- versions: versions
2953
- });
2954
- }
2955
- if (useBackend && cdnUrl) {
2956
- backends.push(Backend);
2957
- backendOptions.push({
2958
- // load resources from url path
2959
- loadPath: `${cdnUrl}/locales/{{lng}}.json`,
2960
- // adds parameters to resource URL. 'example.com' -> 'example.com?v=1.3.5'
2961
- queryStringParams: {
2962
- v: `${version}.${getV(langCacheExpiredTimeMs)}`
2963
- },
2964
- reloadInterval: langCacheExpiredTimeMs // can be used to reload resources in a specific interval (milliseconds) (useful in server environments)
2965
- });
2966
- }
2967
- if (useBackend) {
2968
- backends.push(resourcesToBackend(resources));
2969
- }
2970
- // https://www.i18next.com/misc/creating-own-plugins#languagedetector
2971
- const languageDetector = {
2972
- type: 'languageDetector',
2973
- name: 'customDetector',
2974
- async: false,
2975
- init: () => {
2976
- /* use services and options */
2977
- },
2978
- detect: callback => {
2979
- return lang;
2980
- }
2981
- };
2982
- let config = {
2983
- debug: debug,
2984
- compatibilityJSON: 'v4',
2985
- ns: ['translation'],
2986
- defaultNS: 'translation',
2987
- lng: lang,
2988
- fallbackLng: fallbackLang,
2989
- load: 'currentOnly',
2990
- keySeparator: false,
2991
- nonExplicitSupportedLngs: true,
2992
- interpolation: {
2993
- escapeValue: false // react already safes from xss
2994
- }
2995
- };
2996
- if (useBackend) {
2997
- config = {
2998
- ...config,
2999
- backend: {
3000
- backends: backends,
3001
- backendOptions: backendOptions,
3002
- cacheHitMode: 'refreshAndUpdateStore',
3003
- reloadInterval: langCacheExpiredTimeMs,
3004
- refreshExpirationTime: langCacheExpiredTimeMs // only after determined time it should trigger a refresh if necessary
3005
- },
3006
- react: {
3007
- bindI18nStore: 'added' // this way, when the HttpBackend delivers new translations (thanks to refreshAndUpdateStore), the UI gets updated
3008
- }
3009
- };
3010
- } else {
3011
- config = {
3012
- ...config,
3013
- resources: resources
3014
- };
3015
- }
3016
- if (!instance.isInitialized) {
3017
- let i18n = instance;
3018
- if (useBackend) {
3019
- i18n = i18n.use(Backend$1);
3020
- }
3021
- i18n.use(languageDetector).use(initReactI18next) // passes i18n down to react-i18next
3022
- .init(config, (ex, t) => {
3023
- if (ex) {
3024
- console.error(`Error when i18n init`, ex);
3025
- return;
3026
- }
3027
- });
3028
- }
3029
- };
3030
-
3031
- export { getV, initI18n };
3032
- //# sourceMappingURL=i18n.mjs.map