@developer-overheid-nl/don-register-components 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundled-licenses.json +530 -0
- package/dist/chunk-CejOibrJ.js +4221 -0
- package/dist/chunk-t31k1LNs.js +4220 -0
- package/dist/client.cjs +4 -0
- package/dist/client.js +4 -0
- package/dist/i18n.cjs +2223 -0
- package/dist/i18n.js +2224 -0
- package/dist/index.cjs +16522 -0
- package/dist/index.js +16523 -0
- package/dist/packages/components/src/alert/Alert.d.ts +8 -0
- package/dist/packages/components/src/alert/Alert.d.ts.map +1 -0
- package/dist/packages/components/src/alignBox/AlignBox.d.ts +14 -0
- package/dist/packages/components/src/alignBox/AlignBox.d.ts.map +1 -0
- package/dist/packages/components/src/article/Article.d.ts +4 -0
- package/dist/packages/components/src/article/Article.d.ts.map +1 -0
- package/dist/packages/components/src/block/Block.d.ts +10 -0
- package/dist/packages/components/src/block/Block.d.ts.map +1 -0
- package/dist/packages/components/src/cardAsLink/CardAsLink.d.ts +6 -0
- package/dist/packages/components/src/cardAsLink/CardAsLink.d.ts.map +1 -0
- package/dist/packages/components/src/cardsList/CardsList.d.ts +22 -0
- package/dist/packages/components/src/cardsList/CardsList.d.ts.map +1 -0
- package/dist/packages/components/src/client.d.ts +2 -0
- package/dist/packages/components/src/client.d.ts.map +1 -0
- package/dist/packages/components/src/container/Container.d.ts +2 -0
- package/dist/packages/components/src/container/Container.d.ts.map +1 -0
- package/dist/packages/components/src/copyButton/CopyButton.d.ts +6 -0
- package/dist/packages/components/src/copyButton/CopyButton.d.ts.map +1 -0
- package/dist/packages/components/src/dataBadgeLink/DataBadgeLink.d.ts +13 -0
- package/dist/packages/components/src/dataBadgeLink/DataBadgeLink.d.ts.map +1 -0
- package/dist/packages/components/src/dataSummary/DataSummary.d.ts +7 -0
- package/dist/packages/components/src/dataSummary/DataSummary.d.ts.map +1 -0
- package/dist/packages/components/src/dataSummary/DataSummaryItem.d.ts +6 -0
- package/dist/packages/components/src/dataSummary/DataSummaryItem.d.ts.map +1 -0
- package/dist/packages/components/src/filters/Filters.d.ts +12 -0
- package/dist/packages/components/src/filters/Filters.d.ts.map +1 -0
- package/dist/packages/components/src/footer/Footer.d.ts +14 -0
- package/dist/packages/components/src/footer/Footer.d.ts.map +1 -0
- package/dist/packages/components/src/header/Header.d.ts +31 -0
- package/dist/packages/components/src/header/Header.d.ts.map +1 -0
- package/dist/packages/components/src/heading/Heading.d.ts +4 -0
- package/dist/packages/components/src/heading/Heading.d.ts.map +1 -0
- package/dist/packages/components/src/headingGroup/HeadingGroup.d.ts +12 -0
- package/dist/packages/components/src/headingGroup/HeadingGroup.d.ts.map +1 -0
- package/dist/packages/components/src/i18n.d.ts +4 -0
- package/dist/packages/components/src/i18n.d.ts.map +1 -0
- package/dist/packages/components/src/iconBadge/IconBadge.d.ts +7 -0
- package/dist/packages/components/src/iconBadge/IconBadge.d.ts.map +1 -0
- package/dist/packages/components/src/iconBadge/getAppearance.d.ts +5 -0
- package/dist/packages/components/src/iconBadge/getAppearance.d.ts.map +1 -0
- package/dist/packages/components/src/iconsSprite/Icon.d.ts +2 -0
- package/dist/packages/components/src/iconsSprite/Icon.d.ts.map +1 -0
- package/dist/packages/components/src/iconsSprite/IconsSprite.d.ts +2 -0
- package/dist/packages/components/src/iconsSprite/IconsSprite.d.ts.map +1 -0
- package/dist/packages/components/src/index.d.ts +31 -0
- package/dist/packages/components/src/index.d.ts.map +1 -0
- package/dist/packages/components/src/link/Link.d.ts +4 -0
- package/dist/packages/components/src/link/Link.d.ts.map +1 -0
- package/dist/packages/components/src/markdown/Markdown.d.ts +9 -0
- package/dist/packages/components/src/markdown/Markdown.d.ts.map +1 -0
- package/dist/packages/components/src/pagination/Pagination.d.ts +23 -0
- package/dist/packages/components/src/pagination/Pagination.d.ts.map +1 -0
- package/dist/packages/components/src/paragraph/Paragraph.d.ts +4 -0
- package/dist/packages/components/src/paragraph/Paragraph.d.ts.map +1 -0
- package/dist/packages/components/src/pillBadge/PillBadge.d.ts +12 -0
- package/dist/packages/components/src/pillBadge/PillBadge.d.ts.map +1 -0
- package/dist/packages/components/src/pillBadge/PillBadge.examples.d.ts +3 -0
- package/dist/packages/components/src/pillBadge/PillBadge.examples.d.ts.map +1 -0
- package/dist/packages/components/src/readOnlyTextInput/ReadOnlyTextInput.d.ts +4 -0
- package/dist/packages/components/src/readOnlyTextInput/ReadOnlyTextInput.d.ts.map +1 -0
- package/dist/packages/components/src/scoreBadge/ScoreBadge.d.ts +18 -0
- package/dist/packages/components/src/scoreBadge/ScoreBadge.d.ts.map +1 -0
- package/dist/packages/components/src/search/Search.d.ts +10 -0
- package/dist/packages/components/src/search/Search.d.ts.map +1 -0
- package/dist/packages/components/src/siteLogo/SiteLogo.d.ts +5 -0
- package/dist/packages/components/src/siteLogo/SiteLogo.d.ts.map +1 -0
- package/dist/packages/components/src/topNavigation/TopNavigation.d.ts +16 -0
- package/dist/packages/components/src/topNavigation/TopNavigation.d.ts.map +1 -0
- package/dist/proprietary/icons/index.d.ts +5 -0
- package/dist/proprietary/icons/index.d.ts.map +1 -0
- package/dist/proprietary/icons/ro/react/IconDeltaNaarLinksInline.d.ts +17 -0
- package/dist/proprietary/icons/ro/react/IconDeltaNaarLinksInline.d.ts.map +1 -0
- package/dist/proprietary/icons/ro/react/IconDeltaNaarRechtsInline.d.ts +17 -0
- package/dist/proprietary/icons/ro/react/IconDeltaNaarRechtsInline.d.ts.map +1 -0
- package/dist/proprietary/icons/ro/react/IconZoekInline.d.ts +17 -0
- package/dist/proprietary/icons/ro/react/IconZoekInline.d.ts.map +1 -0
- package/dist/proprietary/icons/ro/react/Sprite.d.ts +2 -0
- package/dist/proprietary/icons/ro/react/Sprite.d.ts.map +1 -0
- package/dist/proprietary/index.d.ts +3 -0
- package/dist/proprietary/index.d.ts.map +1 -0
- package/dist/proprietary/logos/don/react/Lint/index.d.ts +4 -0
- package/dist/proprietary/logos/don/react/Lint/index.d.ts.map +1 -0
- package/dist/proprietary/logos/don/react/Logo/index.d.ts +4 -0
- package/dist/proprietary/logos/don/react/Logo/index.d.ts.map +1 -0
- package/dist/styles.css +944 -0
- package/package.json +63 -0
- package/src/index.ts +76 -0
package/dist/i18n.cjs
ADDED
|
@@ -0,0 +1,2223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const isString = (obj) => typeof obj === "string";
|
|
3
|
+
const defer = () => {
|
|
4
|
+
let res;
|
|
5
|
+
let rej;
|
|
6
|
+
const promise = new Promise((resolve, reject) => {
|
|
7
|
+
res = resolve;
|
|
8
|
+
rej = reject;
|
|
9
|
+
});
|
|
10
|
+
promise.resolve = res;
|
|
11
|
+
promise.reject = rej;
|
|
12
|
+
return promise;
|
|
13
|
+
};
|
|
14
|
+
const makeString = (object) => {
|
|
15
|
+
if (object == null) return "";
|
|
16
|
+
return "" + object;
|
|
17
|
+
};
|
|
18
|
+
const copy = (a, s, t) => {
|
|
19
|
+
a.forEach((m) => {
|
|
20
|
+
if (s[m]) t[m] = s[m];
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
const lastOfPathSeparatorRegExp = /###/g;
|
|
24
|
+
const cleanKey = (key) => key && key.indexOf("###") > -1 ? key.replace(lastOfPathSeparatorRegExp, ".") : key;
|
|
25
|
+
const canNotTraverseDeeper = (object) => !object || isString(object);
|
|
26
|
+
const getLastOfPath = (object, path, Empty) => {
|
|
27
|
+
const stack = !isString(path) ? path : path.split(".");
|
|
28
|
+
let stackIndex = 0;
|
|
29
|
+
while (stackIndex < stack.length - 1) {
|
|
30
|
+
if (canNotTraverseDeeper(object)) return {};
|
|
31
|
+
const key = cleanKey(stack[stackIndex]);
|
|
32
|
+
if (!object[key] && Empty) object[key] = new Empty();
|
|
33
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
34
|
+
object = object[key];
|
|
35
|
+
} else {
|
|
36
|
+
object = {};
|
|
37
|
+
}
|
|
38
|
+
++stackIndex;
|
|
39
|
+
}
|
|
40
|
+
if (canNotTraverseDeeper(object)) return {};
|
|
41
|
+
return {
|
|
42
|
+
obj: object,
|
|
43
|
+
k: cleanKey(stack[stackIndex])
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
const setPath = (object, path, newValue) => {
|
|
47
|
+
const {
|
|
48
|
+
obj,
|
|
49
|
+
k
|
|
50
|
+
} = getLastOfPath(object, path, Object);
|
|
51
|
+
if (obj !== void 0 || path.length === 1) {
|
|
52
|
+
obj[k] = newValue;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
let e = path[path.length - 1];
|
|
56
|
+
let p = path.slice(0, path.length - 1);
|
|
57
|
+
let last = getLastOfPath(object, p, Object);
|
|
58
|
+
while (last.obj === void 0 && p.length) {
|
|
59
|
+
e = `${p[p.length - 1]}.${e}`;
|
|
60
|
+
p = p.slice(0, p.length - 1);
|
|
61
|
+
last = getLastOfPath(object, p, Object);
|
|
62
|
+
if (last?.obj && typeof last.obj[`${last.k}.${e}`] !== "undefined") {
|
|
63
|
+
last.obj = void 0;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
last.obj[`${last.k}.${e}`] = newValue;
|
|
67
|
+
};
|
|
68
|
+
const pushPath = (object, path, newValue, concat) => {
|
|
69
|
+
const {
|
|
70
|
+
obj,
|
|
71
|
+
k
|
|
72
|
+
} = getLastOfPath(object, path, Object);
|
|
73
|
+
obj[k] = obj[k] || [];
|
|
74
|
+
obj[k].push(newValue);
|
|
75
|
+
};
|
|
76
|
+
const getPath = (object, path) => {
|
|
77
|
+
const {
|
|
78
|
+
obj,
|
|
79
|
+
k
|
|
80
|
+
} = getLastOfPath(object, path);
|
|
81
|
+
if (!obj) return void 0;
|
|
82
|
+
if (!Object.prototype.hasOwnProperty.call(obj, k)) return void 0;
|
|
83
|
+
return obj[k];
|
|
84
|
+
};
|
|
85
|
+
const getPathWithDefaults = (data, defaultData, key) => {
|
|
86
|
+
const value = getPath(data, key);
|
|
87
|
+
if (value !== void 0) {
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
return getPath(defaultData, key);
|
|
91
|
+
};
|
|
92
|
+
const deepExtend = (target, source, overwrite) => {
|
|
93
|
+
for (const prop in source) {
|
|
94
|
+
if (prop !== "__proto__" && prop !== "constructor") {
|
|
95
|
+
if (prop in target) {
|
|
96
|
+
if (isString(target[prop]) || target[prop] instanceof String || isString(source[prop]) || source[prop] instanceof String) {
|
|
97
|
+
if (overwrite) target[prop] = source[prop];
|
|
98
|
+
} else {
|
|
99
|
+
deepExtend(target[prop], source[prop], overwrite);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
target[prop] = source[prop];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return target;
|
|
107
|
+
};
|
|
108
|
+
const regexEscape = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
|
109
|
+
var _entityMap = {
|
|
110
|
+
"&": "&",
|
|
111
|
+
"<": "<",
|
|
112
|
+
">": ">",
|
|
113
|
+
'"': """,
|
|
114
|
+
"'": "'",
|
|
115
|
+
"/": "/"
|
|
116
|
+
};
|
|
117
|
+
const escape = (data) => {
|
|
118
|
+
if (isString(data)) {
|
|
119
|
+
return data.replace(/[&<>"'\/]/g, (s) => _entityMap[s]);
|
|
120
|
+
}
|
|
121
|
+
return data;
|
|
122
|
+
};
|
|
123
|
+
class RegExpCache {
|
|
124
|
+
constructor(capacity) {
|
|
125
|
+
this.capacity = capacity;
|
|
126
|
+
this.regExpMap = /* @__PURE__ */ new Map();
|
|
127
|
+
this.regExpQueue = [];
|
|
128
|
+
}
|
|
129
|
+
getRegExp(pattern) {
|
|
130
|
+
const regExpFromCache = this.regExpMap.get(pattern);
|
|
131
|
+
if (regExpFromCache !== void 0) {
|
|
132
|
+
return regExpFromCache;
|
|
133
|
+
}
|
|
134
|
+
const regExpNew = new RegExp(pattern);
|
|
135
|
+
if (this.regExpQueue.length === this.capacity) {
|
|
136
|
+
this.regExpMap.delete(this.regExpQueue.shift());
|
|
137
|
+
}
|
|
138
|
+
this.regExpMap.set(pattern, regExpNew);
|
|
139
|
+
this.regExpQueue.push(pattern);
|
|
140
|
+
return regExpNew;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const chars = [" ", ",", "?", "!", ";"];
|
|
144
|
+
const looksLikeObjectPathRegExpCache = new RegExpCache(20);
|
|
145
|
+
const looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
|
|
146
|
+
nsSeparator = nsSeparator || "";
|
|
147
|
+
keySeparator = keySeparator || "";
|
|
148
|
+
const possibleChars = chars.filter((c) => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0);
|
|
149
|
+
if (possibleChars.length === 0) return true;
|
|
150
|
+
const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map((c) => c === "?" ? "\\?" : c).join("|")})`);
|
|
151
|
+
let matched = !r.test(key);
|
|
152
|
+
if (!matched) {
|
|
153
|
+
const ki = key.indexOf(keySeparator);
|
|
154
|
+
if (ki > 0 && !r.test(key.substring(0, ki))) {
|
|
155
|
+
matched = true;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return matched;
|
|
159
|
+
};
|
|
160
|
+
const deepFind = (obj, path, keySeparator = ".") => {
|
|
161
|
+
if (!obj) return void 0;
|
|
162
|
+
if (obj[path]) {
|
|
163
|
+
if (!Object.prototype.hasOwnProperty.call(obj, path)) return void 0;
|
|
164
|
+
return obj[path];
|
|
165
|
+
}
|
|
166
|
+
const tokens = path.split(keySeparator);
|
|
167
|
+
let current = obj;
|
|
168
|
+
for (let i = 0; i < tokens.length; ) {
|
|
169
|
+
if (!current || typeof current !== "object") {
|
|
170
|
+
return void 0;
|
|
171
|
+
}
|
|
172
|
+
let next;
|
|
173
|
+
let nextPath = "";
|
|
174
|
+
for (let j = i; j < tokens.length; ++j) {
|
|
175
|
+
if (j !== i) {
|
|
176
|
+
nextPath += keySeparator;
|
|
177
|
+
}
|
|
178
|
+
nextPath += tokens[j];
|
|
179
|
+
next = current[nextPath];
|
|
180
|
+
if (next !== void 0) {
|
|
181
|
+
if (["string", "number", "boolean"].indexOf(typeof next) > -1 && j < tokens.length - 1) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
i += j - i + 1;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
current = next;
|
|
189
|
+
}
|
|
190
|
+
return current;
|
|
191
|
+
};
|
|
192
|
+
const getCleanedCode = (code) => code?.replace("_", "-");
|
|
193
|
+
const consoleLogger = {
|
|
194
|
+
type: "logger",
|
|
195
|
+
log(args) {
|
|
196
|
+
this.output("log", args);
|
|
197
|
+
},
|
|
198
|
+
warn(args) {
|
|
199
|
+
this.output("warn", args);
|
|
200
|
+
},
|
|
201
|
+
error(args) {
|
|
202
|
+
this.output("error", args);
|
|
203
|
+
},
|
|
204
|
+
output(type, args) {
|
|
205
|
+
console?.[type]?.apply?.(console, args);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
class Logger {
|
|
209
|
+
constructor(concreteLogger, options = {}) {
|
|
210
|
+
this.init(concreteLogger, options);
|
|
211
|
+
}
|
|
212
|
+
init(concreteLogger, options = {}) {
|
|
213
|
+
this.prefix = options.prefix || "i18next:";
|
|
214
|
+
this.logger = concreteLogger || consoleLogger;
|
|
215
|
+
this.options = options;
|
|
216
|
+
this.debug = options.debug;
|
|
217
|
+
}
|
|
218
|
+
log(...args) {
|
|
219
|
+
return this.forward(args, "log", "", true);
|
|
220
|
+
}
|
|
221
|
+
warn(...args) {
|
|
222
|
+
return this.forward(args, "warn", "", true);
|
|
223
|
+
}
|
|
224
|
+
error(...args) {
|
|
225
|
+
return this.forward(args, "error", "");
|
|
226
|
+
}
|
|
227
|
+
deprecate(...args) {
|
|
228
|
+
return this.forward(args, "warn", "WARNING DEPRECATED: ", true);
|
|
229
|
+
}
|
|
230
|
+
forward(args, lvl, prefix, debugOnly) {
|
|
231
|
+
if (debugOnly && !this.debug) return null;
|
|
232
|
+
if (isString(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
|
|
233
|
+
return this.logger[lvl](args);
|
|
234
|
+
}
|
|
235
|
+
create(moduleName) {
|
|
236
|
+
return new Logger(this.logger, {
|
|
237
|
+
...{
|
|
238
|
+
prefix: `${this.prefix}:${moduleName}:`
|
|
239
|
+
},
|
|
240
|
+
...this.options
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
clone(options) {
|
|
244
|
+
options = options || this.options;
|
|
245
|
+
options.prefix = options.prefix || this.prefix;
|
|
246
|
+
return new Logger(this.logger, options);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
var baseLogger = new Logger();
|
|
250
|
+
class EventEmitter {
|
|
251
|
+
constructor() {
|
|
252
|
+
this.observers = {};
|
|
253
|
+
}
|
|
254
|
+
on(events, listener) {
|
|
255
|
+
events.split(" ").forEach((event) => {
|
|
256
|
+
if (!this.observers[event]) this.observers[event] = /* @__PURE__ */ new Map();
|
|
257
|
+
const numListeners = this.observers[event].get(listener) || 0;
|
|
258
|
+
this.observers[event].set(listener, numListeners + 1);
|
|
259
|
+
});
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
262
|
+
off(event, listener) {
|
|
263
|
+
if (!this.observers[event]) return;
|
|
264
|
+
if (!listener) {
|
|
265
|
+
delete this.observers[event];
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
this.observers[event].delete(listener);
|
|
269
|
+
}
|
|
270
|
+
emit(event, ...args) {
|
|
271
|
+
if (this.observers[event]) {
|
|
272
|
+
const cloned = Array.from(this.observers[event].entries());
|
|
273
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
|
274
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
|
275
|
+
observer(...args);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (this.observers["*"]) {
|
|
280
|
+
const cloned = Array.from(this.observers["*"].entries());
|
|
281
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
|
282
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
|
283
|
+
observer.apply(observer, [event, ...args]);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
class ResourceStore extends EventEmitter {
|
|
290
|
+
constructor(data, options = {
|
|
291
|
+
ns: ["translation"],
|
|
292
|
+
defaultNS: "translation"
|
|
293
|
+
}) {
|
|
294
|
+
super();
|
|
295
|
+
this.data = data || {};
|
|
296
|
+
this.options = options;
|
|
297
|
+
if (this.options.keySeparator === void 0) {
|
|
298
|
+
this.options.keySeparator = ".";
|
|
299
|
+
}
|
|
300
|
+
if (this.options.ignoreJSONStructure === void 0) {
|
|
301
|
+
this.options.ignoreJSONStructure = true;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
addNamespaces(ns) {
|
|
305
|
+
if (this.options.ns.indexOf(ns) < 0) {
|
|
306
|
+
this.options.ns.push(ns);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
removeNamespaces(ns) {
|
|
310
|
+
const index = this.options.ns.indexOf(ns);
|
|
311
|
+
if (index > -1) {
|
|
312
|
+
this.options.ns.splice(index, 1);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
getResource(lng, ns, key, options = {}) {
|
|
316
|
+
const keySeparator = options.keySeparator !== void 0 ? options.keySeparator : this.options.keySeparator;
|
|
317
|
+
const ignoreJSONStructure = options.ignoreJSONStructure !== void 0 ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
318
|
+
let path;
|
|
319
|
+
if (lng.indexOf(".") > -1) {
|
|
320
|
+
path = lng.split(".");
|
|
321
|
+
} else {
|
|
322
|
+
path = [lng, ns];
|
|
323
|
+
if (key) {
|
|
324
|
+
if (Array.isArray(key)) {
|
|
325
|
+
path.push(...key);
|
|
326
|
+
} else if (isString(key) && keySeparator) {
|
|
327
|
+
path.push(...key.split(keySeparator));
|
|
328
|
+
} else {
|
|
329
|
+
path.push(key);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const result = getPath(this.data, path);
|
|
334
|
+
if (!result && !ns && !key && lng.indexOf(".") > -1) {
|
|
335
|
+
lng = path[0];
|
|
336
|
+
ns = path[1];
|
|
337
|
+
key = path.slice(2).join(".");
|
|
338
|
+
}
|
|
339
|
+
if (result || !ignoreJSONStructure || !isString(key)) return result;
|
|
340
|
+
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
|
341
|
+
}
|
|
342
|
+
addResource(lng, ns, key, value, options = {
|
|
343
|
+
silent: false
|
|
344
|
+
}) {
|
|
345
|
+
const keySeparator = options.keySeparator !== void 0 ? options.keySeparator : this.options.keySeparator;
|
|
346
|
+
let path = [lng, ns];
|
|
347
|
+
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
348
|
+
if (lng.indexOf(".") > -1) {
|
|
349
|
+
path = lng.split(".");
|
|
350
|
+
value = ns;
|
|
351
|
+
ns = path[1];
|
|
352
|
+
}
|
|
353
|
+
this.addNamespaces(ns);
|
|
354
|
+
setPath(this.data, path, value);
|
|
355
|
+
if (!options.silent) this.emit("added", lng, ns, key, value);
|
|
356
|
+
}
|
|
357
|
+
addResources(lng, ns, resources2, options = {
|
|
358
|
+
silent: false
|
|
359
|
+
}) {
|
|
360
|
+
for (const m in resources2) {
|
|
361
|
+
if (isString(resources2[m]) || Array.isArray(resources2[m])) this.addResource(lng, ns, m, resources2[m], {
|
|
362
|
+
silent: true
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
if (!options.silent) this.emit("added", lng, ns, resources2);
|
|
366
|
+
}
|
|
367
|
+
addResourceBundle(lng, ns, resources2, deep, overwrite, options = {
|
|
368
|
+
silent: false,
|
|
369
|
+
skipCopy: false
|
|
370
|
+
}) {
|
|
371
|
+
let path = [lng, ns];
|
|
372
|
+
if (lng.indexOf(".") > -1) {
|
|
373
|
+
path = lng.split(".");
|
|
374
|
+
deep = resources2;
|
|
375
|
+
resources2 = ns;
|
|
376
|
+
ns = path[1];
|
|
377
|
+
}
|
|
378
|
+
this.addNamespaces(ns);
|
|
379
|
+
let pack = getPath(this.data, path) || {};
|
|
380
|
+
if (!options.skipCopy) resources2 = JSON.parse(JSON.stringify(resources2));
|
|
381
|
+
if (deep) {
|
|
382
|
+
deepExtend(pack, resources2, overwrite);
|
|
383
|
+
} else {
|
|
384
|
+
pack = {
|
|
385
|
+
...pack,
|
|
386
|
+
...resources2
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
setPath(this.data, path, pack);
|
|
390
|
+
if (!options.silent) this.emit("added", lng, ns, resources2);
|
|
391
|
+
}
|
|
392
|
+
removeResourceBundle(lng, ns) {
|
|
393
|
+
if (this.hasResourceBundle(lng, ns)) {
|
|
394
|
+
delete this.data[lng][ns];
|
|
395
|
+
}
|
|
396
|
+
this.removeNamespaces(ns);
|
|
397
|
+
this.emit("removed", lng, ns);
|
|
398
|
+
}
|
|
399
|
+
hasResourceBundle(lng, ns) {
|
|
400
|
+
return this.getResource(lng, ns) !== void 0;
|
|
401
|
+
}
|
|
402
|
+
getResourceBundle(lng, ns) {
|
|
403
|
+
if (!ns) ns = this.options.defaultNS;
|
|
404
|
+
return this.getResource(lng, ns);
|
|
405
|
+
}
|
|
406
|
+
getDataByLanguage(lng) {
|
|
407
|
+
return this.data[lng];
|
|
408
|
+
}
|
|
409
|
+
hasLanguageSomeTranslations(lng) {
|
|
410
|
+
const data = this.getDataByLanguage(lng);
|
|
411
|
+
const n = data && Object.keys(data) || [];
|
|
412
|
+
return !!n.find((v) => data[v] && Object.keys(data[v]).length > 0);
|
|
413
|
+
}
|
|
414
|
+
toJSON() {
|
|
415
|
+
return this.data;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
var postProcessor = {
|
|
419
|
+
processors: {},
|
|
420
|
+
addPostProcessor(module2) {
|
|
421
|
+
this.processors[module2.name] = module2;
|
|
422
|
+
},
|
|
423
|
+
handle(processors, value, key, options, translator) {
|
|
424
|
+
processors.forEach((processor) => {
|
|
425
|
+
value = this.processors[processor]?.process(value, key, options, translator) ?? value;
|
|
426
|
+
});
|
|
427
|
+
return value;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
const PATH_KEY = /* @__PURE__ */ Symbol("i18next/PATH_KEY");
|
|
431
|
+
function createProxy() {
|
|
432
|
+
const state = [];
|
|
433
|
+
const handler = /* @__PURE__ */ Object.create(null);
|
|
434
|
+
let proxy;
|
|
435
|
+
handler.get = (target, key) => {
|
|
436
|
+
proxy?.revoke?.();
|
|
437
|
+
if (key === PATH_KEY) return state;
|
|
438
|
+
state.push(key);
|
|
439
|
+
proxy = Proxy.revocable(target, handler);
|
|
440
|
+
return proxy.proxy;
|
|
441
|
+
};
|
|
442
|
+
return Proxy.revocable(/* @__PURE__ */ Object.create(null), handler).proxy;
|
|
443
|
+
}
|
|
444
|
+
function keysFromSelector(selector, opts) {
|
|
445
|
+
const {
|
|
446
|
+
[PATH_KEY]: path
|
|
447
|
+
} = selector(createProxy());
|
|
448
|
+
return path.join(opts?.keySeparator ?? ".");
|
|
449
|
+
}
|
|
450
|
+
const checkedLoadedFor = {};
|
|
451
|
+
const shouldHandleAsObject = (res) => !isString(res) && typeof res !== "boolean" && typeof res !== "number";
|
|
452
|
+
class Translator extends EventEmitter {
|
|
453
|
+
constructor(services, options = {}) {
|
|
454
|
+
super();
|
|
455
|
+
copy(["resourceStore", "languageUtils", "pluralResolver", "interpolator", "backendConnector", "i18nFormat", "utils"], services, this);
|
|
456
|
+
this.options = options;
|
|
457
|
+
if (this.options.keySeparator === void 0) {
|
|
458
|
+
this.options.keySeparator = ".";
|
|
459
|
+
}
|
|
460
|
+
this.logger = baseLogger.create("translator");
|
|
461
|
+
}
|
|
462
|
+
changeLanguage(lng) {
|
|
463
|
+
if (lng) this.language = lng;
|
|
464
|
+
}
|
|
465
|
+
exists(key, o = {
|
|
466
|
+
interpolation: {}
|
|
467
|
+
}) {
|
|
468
|
+
const opt = {
|
|
469
|
+
...o
|
|
470
|
+
};
|
|
471
|
+
if (key == null) return false;
|
|
472
|
+
const resolved = this.resolve(key, opt);
|
|
473
|
+
if (resolved?.res === void 0) return false;
|
|
474
|
+
const isObject = shouldHandleAsObject(resolved.res);
|
|
475
|
+
if (opt.returnObjects === false && isObject) {
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
extractFromKey(key, opt) {
|
|
481
|
+
let nsSeparator = opt.nsSeparator !== void 0 ? opt.nsSeparator : this.options.nsSeparator;
|
|
482
|
+
if (nsSeparator === void 0) nsSeparator = ":";
|
|
483
|
+
const keySeparator = opt.keySeparator !== void 0 ? opt.keySeparator : this.options.keySeparator;
|
|
484
|
+
let namespaces = opt.ns || this.options.defaultNS || [];
|
|
485
|
+
const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
|
|
486
|
+
const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
|
|
487
|
+
if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
|
|
488
|
+
const m = key.match(this.interpolator.nestingRegexp);
|
|
489
|
+
if (m && m.length > 0) {
|
|
490
|
+
return {
|
|
491
|
+
key,
|
|
492
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
const parts = key.split(nsSeparator);
|
|
496
|
+
if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
|
|
497
|
+
key = parts.join(keySeparator);
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
key,
|
|
501
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
translate(keys, o, lastKey) {
|
|
505
|
+
let opt = typeof o === "object" ? {
|
|
506
|
+
...o
|
|
507
|
+
} : o;
|
|
508
|
+
if (typeof opt !== "object" && this.options.overloadTranslationOptionHandler) {
|
|
509
|
+
opt = this.options.overloadTranslationOptionHandler(arguments);
|
|
510
|
+
}
|
|
511
|
+
if (typeof opt === "object") opt = {
|
|
512
|
+
...opt
|
|
513
|
+
};
|
|
514
|
+
if (!opt) opt = {};
|
|
515
|
+
if (keys == null) return "";
|
|
516
|
+
if (typeof keys === "function") keys = keysFromSelector(keys, {
|
|
517
|
+
...this.options,
|
|
518
|
+
...opt
|
|
519
|
+
});
|
|
520
|
+
if (!Array.isArray(keys)) keys = [String(keys)];
|
|
521
|
+
const returnDetails = opt.returnDetails !== void 0 ? opt.returnDetails : this.options.returnDetails;
|
|
522
|
+
const keySeparator = opt.keySeparator !== void 0 ? opt.keySeparator : this.options.keySeparator;
|
|
523
|
+
const {
|
|
524
|
+
key,
|
|
525
|
+
namespaces
|
|
526
|
+
} = this.extractFromKey(keys[keys.length - 1], opt);
|
|
527
|
+
const namespace = namespaces[namespaces.length - 1];
|
|
528
|
+
let nsSeparator = opt.nsSeparator !== void 0 ? opt.nsSeparator : this.options.nsSeparator;
|
|
529
|
+
if (nsSeparator === void 0) nsSeparator = ":";
|
|
530
|
+
const lng = opt.lng || this.language;
|
|
531
|
+
const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
|
|
532
|
+
if (lng?.toLowerCase() === "cimode") {
|
|
533
|
+
if (appendNamespaceToCIMode) {
|
|
534
|
+
if (returnDetails) {
|
|
535
|
+
return {
|
|
536
|
+
res: `${namespace}${nsSeparator}${key}`,
|
|
537
|
+
usedKey: key,
|
|
538
|
+
exactUsedKey: key,
|
|
539
|
+
usedLng: lng,
|
|
540
|
+
usedNS: namespace,
|
|
541
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
return `${namespace}${nsSeparator}${key}`;
|
|
545
|
+
}
|
|
546
|
+
if (returnDetails) {
|
|
547
|
+
return {
|
|
548
|
+
res: key,
|
|
549
|
+
usedKey: key,
|
|
550
|
+
exactUsedKey: key,
|
|
551
|
+
usedLng: lng,
|
|
552
|
+
usedNS: namespace,
|
|
553
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return key;
|
|
557
|
+
}
|
|
558
|
+
const resolved = this.resolve(keys, opt);
|
|
559
|
+
let res = resolved?.res;
|
|
560
|
+
const resUsedKey = resolved?.usedKey || key;
|
|
561
|
+
const resExactUsedKey = resolved?.exactUsedKey || key;
|
|
562
|
+
const noObject = ["[object Number]", "[object Function]", "[object RegExp]"];
|
|
563
|
+
const joinArrays = opt.joinArrays !== void 0 ? opt.joinArrays : this.options.joinArrays;
|
|
564
|
+
const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
|
|
565
|
+
const needsPluralHandling = opt.count !== void 0 && !isString(opt.count);
|
|
566
|
+
const hasDefaultValue = Translator.hasDefaultValue(opt);
|
|
567
|
+
const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : "";
|
|
568
|
+
const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
|
|
569
|
+
ordinal: false
|
|
570
|
+
}) : "";
|
|
571
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
|
572
|
+
const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
|
|
573
|
+
let resForObjHndl = res;
|
|
574
|
+
if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
|
|
575
|
+
resForObjHndl = defaultValue;
|
|
576
|
+
}
|
|
577
|
+
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
|
578
|
+
const resType = Object.prototype.toString.apply(resForObjHndl);
|
|
579
|
+
if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && noObject.indexOf(resType) < 0 && !(isString(joinArrays) && Array.isArray(resForObjHndl))) {
|
|
580
|
+
if (!opt.returnObjects && !this.options.returnObjects) {
|
|
581
|
+
if (!this.options.returnedObjectHandler) {
|
|
582
|
+
this.logger.warn("accessing an object - but returnObjects options is not enabled!");
|
|
583
|
+
}
|
|
584
|
+
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
585
|
+
...opt,
|
|
586
|
+
ns: namespaces
|
|
587
|
+
}) : `key '${key} (${this.language})' returned an object instead of string.`;
|
|
588
|
+
if (returnDetails) {
|
|
589
|
+
resolved.res = r;
|
|
590
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
591
|
+
return resolved;
|
|
592
|
+
}
|
|
593
|
+
return r;
|
|
594
|
+
}
|
|
595
|
+
if (keySeparator) {
|
|
596
|
+
const resTypeIsArray = Array.isArray(resForObjHndl);
|
|
597
|
+
const copy2 = resTypeIsArray ? [] : {};
|
|
598
|
+
const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
|
|
599
|
+
for (const m in resForObjHndl) {
|
|
600
|
+
if (Object.prototype.hasOwnProperty.call(resForObjHndl, m)) {
|
|
601
|
+
const deepKey = `${newKeyToUse}${keySeparator}${m}`;
|
|
602
|
+
if (hasDefaultValue && !res) {
|
|
603
|
+
copy2[m] = this.translate(deepKey, {
|
|
604
|
+
...opt,
|
|
605
|
+
defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : void 0,
|
|
606
|
+
...{
|
|
607
|
+
joinArrays: false,
|
|
608
|
+
ns: namespaces
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
} else {
|
|
612
|
+
copy2[m] = this.translate(deepKey, {
|
|
613
|
+
...opt,
|
|
614
|
+
...{
|
|
615
|
+
joinArrays: false,
|
|
616
|
+
ns: namespaces
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
if (copy2[m] === deepKey) copy2[m] = resForObjHndl[m];
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
res = copy2;
|
|
624
|
+
}
|
|
625
|
+
} else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
|
|
626
|
+
res = res.join(joinArrays);
|
|
627
|
+
if (res) res = this.extendTranslation(res, keys, opt, lastKey);
|
|
628
|
+
} else {
|
|
629
|
+
let usedDefault = false;
|
|
630
|
+
let usedKey = false;
|
|
631
|
+
if (!this.isValidLookup(res) && hasDefaultValue) {
|
|
632
|
+
usedDefault = true;
|
|
633
|
+
res = defaultValue;
|
|
634
|
+
}
|
|
635
|
+
if (!this.isValidLookup(res)) {
|
|
636
|
+
usedKey = true;
|
|
637
|
+
res = key;
|
|
638
|
+
}
|
|
639
|
+
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
|
640
|
+
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? void 0 : res;
|
|
641
|
+
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
|
642
|
+
if (usedKey || usedDefault || updateMissing) {
|
|
643
|
+
this.logger.log(updateMissing ? "updateKey" : "missingKey", lng, namespace, key, updateMissing ? defaultValue : res);
|
|
644
|
+
if (keySeparator) {
|
|
645
|
+
const fk = this.resolve(key, {
|
|
646
|
+
...opt,
|
|
647
|
+
keySeparator: false
|
|
648
|
+
});
|
|
649
|
+
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.");
|
|
650
|
+
}
|
|
651
|
+
let lngs = [];
|
|
652
|
+
const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
|
|
653
|
+
if (this.options.saveMissingTo === "fallback" && fallbackLngs && fallbackLngs[0]) {
|
|
654
|
+
for (let i = 0; i < fallbackLngs.length; i++) {
|
|
655
|
+
lngs.push(fallbackLngs[i]);
|
|
656
|
+
}
|
|
657
|
+
} else if (this.options.saveMissingTo === "all") {
|
|
658
|
+
lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
|
|
659
|
+
} else {
|
|
660
|
+
lngs.push(opt.lng || this.language);
|
|
661
|
+
}
|
|
662
|
+
const send = (l, k, specificDefaultValue) => {
|
|
663
|
+
const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
|
|
664
|
+
if (this.options.missingKeyHandler) {
|
|
665
|
+
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
666
|
+
} else if (this.backendConnector?.saveMissing) {
|
|
667
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
668
|
+
}
|
|
669
|
+
this.emit("missingKey", l, namespace, k, res);
|
|
670
|
+
};
|
|
671
|
+
if (this.options.saveMissing) {
|
|
672
|
+
if (this.options.saveMissingPlurals && needsPluralHandling) {
|
|
673
|
+
lngs.forEach((language) => {
|
|
674
|
+
const suffixes = this.pluralResolver.getSuffixes(language, opt);
|
|
675
|
+
if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
|
|
676
|
+
suffixes.push(`${this.options.pluralSeparator}zero`);
|
|
677
|
+
}
|
|
678
|
+
suffixes.forEach((suffix) => {
|
|
679
|
+
send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
} else {
|
|
683
|
+
send(lngs, key, defaultValue);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
res = this.extendTranslation(res, keys, opt, resolved, lastKey);
|
|
688
|
+
if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
|
|
689
|
+
res = `${namespace}${nsSeparator}${key}`;
|
|
690
|
+
}
|
|
691
|
+
if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
|
|
692
|
+
res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : void 0, opt);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (returnDetails) {
|
|
696
|
+
resolved.res = res;
|
|
697
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
698
|
+
return resolved;
|
|
699
|
+
}
|
|
700
|
+
return res;
|
|
701
|
+
}
|
|
702
|
+
extendTranslation(res, key, opt, resolved, lastKey) {
|
|
703
|
+
if (this.i18nFormat?.parse) {
|
|
704
|
+
res = this.i18nFormat.parse(res, {
|
|
705
|
+
...this.options.interpolation.defaultVariables,
|
|
706
|
+
...opt
|
|
707
|
+
}, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
|
|
708
|
+
resolved
|
|
709
|
+
});
|
|
710
|
+
} else if (!opt.skipInterpolation) {
|
|
711
|
+
if (opt.interpolation) this.interpolator.init({
|
|
712
|
+
...opt,
|
|
713
|
+
...{
|
|
714
|
+
interpolation: {
|
|
715
|
+
...this.options.interpolation,
|
|
716
|
+
...opt.interpolation
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
});
|
|
720
|
+
const skipOnVariables = isString(res) && (opt?.interpolation?.skipOnVariables !== void 0 ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
|
|
721
|
+
let nestBef;
|
|
722
|
+
if (skipOnVariables) {
|
|
723
|
+
const nb = res.match(this.interpolator.nestingRegexp);
|
|
724
|
+
nestBef = nb && nb.length;
|
|
725
|
+
}
|
|
726
|
+
let data = opt.replace && !isString(opt.replace) ? opt.replace : opt;
|
|
727
|
+
if (this.options.interpolation.defaultVariables) data = {
|
|
728
|
+
...this.options.interpolation.defaultVariables,
|
|
729
|
+
...data
|
|
730
|
+
};
|
|
731
|
+
res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
|
|
732
|
+
if (skipOnVariables) {
|
|
733
|
+
const na = res.match(this.interpolator.nestingRegexp);
|
|
734
|
+
const nestAft = na && na.length;
|
|
735
|
+
if (nestBef < nestAft) opt.nest = false;
|
|
736
|
+
}
|
|
737
|
+
if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
|
|
738
|
+
if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
|
|
739
|
+
if (lastKey?.[0] === args[0] && !opt.context) {
|
|
740
|
+
this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
|
|
741
|
+
return null;
|
|
742
|
+
}
|
|
743
|
+
return this.translate(...args, key);
|
|
744
|
+
}, opt);
|
|
745
|
+
if (opt.interpolation) this.interpolator.reset();
|
|
746
|
+
}
|
|
747
|
+
const postProcess = opt.postProcess || this.options.postProcess;
|
|
748
|
+
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
749
|
+
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
750
|
+
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
751
|
+
i18nResolved: {
|
|
752
|
+
...resolved,
|
|
753
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
754
|
+
},
|
|
755
|
+
...opt
|
|
756
|
+
} : opt, this);
|
|
757
|
+
}
|
|
758
|
+
return res;
|
|
759
|
+
}
|
|
760
|
+
resolve(keys, opt = {}) {
|
|
761
|
+
let found;
|
|
762
|
+
let usedKey;
|
|
763
|
+
let exactUsedKey;
|
|
764
|
+
let usedLng;
|
|
765
|
+
let usedNS;
|
|
766
|
+
if (isString(keys)) keys = [keys];
|
|
767
|
+
keys.forEach((k) => {
|
|
768
|
+
if (this.isValidLookup(found)) return;
|
|
769
|
+
const extracted = this.extractFromKey(k, opt);
|
|
770
|
+
const key = extracted.key;
|
|
771
|
+
usedKey = key;
|
|
772
|
+
let namespaces = extracted.namespaces;
|
|
773
|
+
if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
|
|
774
|
+
const needsPluralHandling = opt.count !== void 0 && !isString(opt.count);
|
|
775
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
|
776
|
+
const needsContextHandling = opt.context !== void 0 && (isString(opt.context) || typeof opt.context === "number") && opt.context !== "";
|
|
777
|
+
const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
|
|
778
|
+
namespaces.forEach((ns) => {
|
|
779
|
+
if (this.isValidLookup(found)) return;
|
|
780
|
+
usedNS = ns;
|
|
781
|
+
if (!checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils?.hasLoadedNamespace && !this.utils?.hasLoadedNamespace(usedNS)) {
|
|
782
|
+
checkedLoadedFor[`${codes[0]}-${ns}`] = true;
|
|
783
|
+
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!!!");
|
|
784
|
+
}
|
|
785
|
+
codes.forEach((code) => {
|
|
786
|
+
if (this.isValidLookup(found)) return;
|
|
787
|
+
usedLng = code;
|
|
788
|
+
const finalKeys = [key];
|
|
789
|
+
if (this.i18nFormat?.addLookupKeys) {
|
|
790
|
+
this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
|
|
791
|
+
} else {
|
|
792
|
+
let pluralSuffix;
|
|
793
|
+
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
|
|
794
|
+
const zeroSuffix = `${this.options.pluralSeparator}zero`;
|
|
795
|
+
const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
|
|
796
|
+
if (needsPluralHandling) {
|
|
797
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
|
798
|
+
finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
|
799
|
+
}
|
|
800
|
+
finalKeys.push(key + pluralSuffix);
|
|
801
|
+
if (needsZeroSuffixLookup) {
|
|
802
|
+
finalKeys.push(key + zeroSuffix);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
if (needsContextHandling) {
|
|
806
|
+
const contextKey = `${key}${this.options.contextSeparator || "_"}${opt.context}`;
|
|
807
|
+
finalKeys.push(contextKey);
|
|
808
|
+
if (needsPluralHandling) {
|
|
809
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
|
810
|
+
finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
|
811
|
+
}
|
|
812
|
+
finalKeys.push(contextKey + pluralSuffix);
|
|
813
|
+
if (needsZeroSuffixLookup) {
|
|
814
|
+
finalKeys.push(contextKey + zeroSuffix);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
let possibleKey;
|
|
820
|
+
while (possibleKey = finalKeys.pop()) {
|
|
821
|
+
if (!this.isValidLookup(found)) {
|
|
822
|
+
exactUsedKey = possibleKey;
|
|
823
|
+
found = this.getResource(code, ns, possibleKey, opt);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
return {
|
|
830
|
+
res: found,
|
|
831
|
+
usedKey,
|
|
832
|
+
exactUsedKey,
|
|
833
|
+
usedLng,
|
|
834
|
+
usedNS
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
isValidLookup(res) {
|
|
838
|
+
return res !== void 0 && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === "");
|
|
839
|
+
}
|
|
840
|
+
getResource(code, ns, key, options = {}) {
|
|
841
|
+
if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
|
|
842
|
+
return this.resourceStore.getResource(code, ns, key, options);
|
|
843
|
+
}
|
|
844
|
+
getUsedParamsDetails(options = {}) {
|
|
845
|
+
const optionsKeys = ["defaultValue", "ordinal", "context", "replace", "lng", "lngs", "fallbackLng", "ns", "keySeparator", "nsSeparator", "returnObjects", "returnDetails", "joinArrays", "postProcess", "interpolation"];
|
|
846
|
+
const useOptionsReplaceForData = options.replace && !isString(options.replace);
|
|
847
|
+
let data = useOptionsReplaceForData ? options.replace : options;
|
|
848
|
+
if (useOptionsReplaceForData && typeof options.count !== "undefined") {
|
|
849
|
+
data.count = options.count;
|
|
850
|
+
}
|
|
851
|
+
if (this.options.interpolation.defaultVariables) {
|
|
852
|
+
data = {
|
|
853
|
+
...this.options.interpolation.defaultVariables,
|
|
854
|
+
...data
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
if (!useOptionsReplaceForData) {
|
|
858
|
+
data = {
|
|
859
|
+
...data
|
|
860
|
+
};
|
|
861
|
+
for (const key of optionsKeys) {
|
|
862
|
+
delete data[key];
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
return data;
|
|
866
|
+
}
|
|
867
|
+
static hasDefaultValue(options) {
|
|
868
|
+
const prefix = "defaultValue";
|
|
869
|
+
for (const option in options) {
|
|
870
|
+
if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && void 0 !== options[option]) {
|
|
871
|
+
return true;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return false;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
class LanguageUtil {
|
|
878
|
+
constructor(options) {
|
|
879
|
+
this.options = options;
|
|
880
|
+
this.supportedLngs = this.options.supportedLngs || false;
|
|
881
|
+
this.logger = baseLogger.create("languageUtils");
|
|
882
|
+
}
|
|
883
|
+
getScriptPartFromCode(code) {
|
|
884
|
+
code = getCleanedCode(code);
|
|
885
|
+
if (!code || code.indexOf("-") < 0) return null;
|
|
886
|
+
const p = code.split("-");
|
|
887
|
+
if (p.length === 2) return null;
|
|
888
|
+
p.pop();
|
|
889
|
+
if (p[p.length - 1].toLowerCase() === "x") return null;
|
|
890
|
+
return this.formatLanguageCode(p.join("-"));
|
|
891
|
+
}
|
|
892
|
+
getLanguagePartFromCode(code) {
|
|
893
|
+
code = getCleanedCode(code);
|
|
894
|
+
if (!code || code.indexOf("-") < 0) return code;
|
|
895
|
+
const p = code.split("-");
|
|
896
|
+
return this.formatLanguageCode(p[0]);
|
|
897
|
+
}
|
|
898
|
+
formatLanguageCode(code) {
|
|
899
|
+
if (isString(code) && code.indexOf("-") > -1) {
|
|
900
|
+
let formattedCode;
|
|
901
|
+
try {
|
|
902
|
+
formattedCode = Intl.getCanonicalLocales(code)[0];
|
|
903
|
+
} catch (e) {
|
|
904
|
+
}
|
|
905
|
+
if (formattedCode && this.options.lowerCaseLng) {
|
|
906
|
+
formattedCode = formattedCode.toLowerCase();
|
|
907
|
+
}
|
|
908
|
+
if (formattedCode) return formattedCode;
|
|
909
|
+
if (this.options.lowerCaseLng) {
|
|
910
|
+
return code.toLowerCase();
|
|
911
|
+
}
|
|
912
|
+
return code;
|
|
913
|
+
}
|
|
914
|
+
return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
|
915
|
+
}
|
|
916
|
+
isSupportedCode(code) {
|
|
917
|
+
if (this.options.load === "languageOnly" || this.options.nonExplicitSupportedLngs) {
|
|
918
|
+
code = this.getLanguagePartFromCode(code);
|
|
919
|
+
}
|
|
920
|
+
return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
|
|
921
|
+
}
|
|
922
|
+
getBestMatchFromCodes(codes) {
|
|
923
|
+
if (!codes) return null;
|
|
924
|
+
let found;
|
|
925
|
+
codes.forEach((code) => {
|
|
926
|
+
if (found) return;
|
|
927
|
+
const cleanedLng = this.formatLanguageCode(code);
|
|
928
|
+
if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
|
|
929
|
+
});
|
|
930
|
+
if (!found && this.options.supportedLngs) {
|
|
931
|
+
codes.forEach((code) => {
|
|
932
|
+
if (found) return;
|
|
933
|
+
const lngScOnly = this.getScriptPartFromCode(code);
|
|
934
|
+
if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
|
|
935
|
+
const lngOnly = this.getLanguagePartFromCode(code);
|
|
936
|
+
if (this.isSupportedCode(lngOnly)) return found = lngOnly;
|
|
937
|
+
found = this.options.supportedLngs.find((supportedLng) => {
|
|
938
|
+
if (supportedLng === lngOnly) return supportedLng;
|
|
939
|
+
if (supportedLng.indexOf("-") < 0 && lngOnly.indexOf("-") < 0) return;
|
|
940
|
+
if (supportedLng.indexOf("-") > 0 && lngOnly.indexOf("-") < 0 && supportedLng.substring(0, supportedLng.indexOf("-")) === lngOnly) return supportedLng;
|
|
941
|
+
if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
|
|
942
|
+
});
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
|
|
946
|
+
return found;
|
|
947
|
+
}
|
|
948
|
+
getFallbackCodes(fallbacks, code) {
|
|
949
|
+
if (!fallbacks) return [];
|
|
950
|
+
if (typeof fallbacks === "function") fallbacks = fallbacks(code);
|
|
951
|
+
if (isString(fallbacks)) fallbacks = [fallbacks];
|
|
952
|
+
if (Array.isArray(fallbacks)) return fallbacks;
|
|
953
|
+
if (!code) return fallbacks.default || [];
|
|
954
|
+
let found = fallbacks[code];
|
|
955
|
+
if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
|
|
956
|
+
if (!found) found = fallbacks[this.formatLanguageCode(code)];
|
|
957
|
+
if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
|
|
958
|
+
if (!found) found = fallbacks.default;
|
|
959
|
+
return found || [];
|
|
960
|
+
}
|
|
961
|
+
toResolveHierarchy(code, fallbackCode) {
|
|
962
|
+
const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
|
|
963
|
+
const codes = [];
|
|
964
|
+
const addCode = (c) => {
|
|
965
|
+
if (!c) return;
|
|
966
|
+
if (this.isSupportedCode(c)) {
|
|
967
|
+
codes.push(c);
|
|
968
|
+
} else {
|
|
969
|
+
this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
if (isString(code) && (code.indexOf("-") > -1 || code.indexOf("_") > -1)) {
|
|
973
|
+
if (this.options.load !== "languageOnly") addCode(this.formatLanguageCode(code));
|
|
974
|
+
if (this.options.load !== "languageOnly" && this.options.load !== "currentOnly") addCode(this.getScriptPartFromCode(code));
|
|
975
|
+
if (this.options.load !== "currentOnly") addCode(this.getLanguagePartFromCode(code));
|
|
976
|
+
} else if (isString(code)) {
|
|
977
|
+
addCode(this.formatLanguageCode(code));
|
|
978
|
+
}
|
|
979
|
+
fallbackCodes.forEach((fc) => {
|
|
980
|
+
if (codes.indexOf(fc) < 0) addCode(this.formatLanguageCode(fc));
|
|
981
|
+
});
|
|
982
|
+
return codes;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
const suffixesOrder = {
|
|
986
|
+
zero: 0,
|
|
987
|
+
one: 1,
|
|
988
|
+
two: 2,
|
|
989
|
+
few: 3,
|
|
990
|
+
many: 4,
|
|
991
|
+
other: 5
|
|
992
|
+
};
|
|
993
|
+
const dummyRule = {
|
|
994
|
+
select: (count) => count === 1 ? "one" : "other",
|
|
995
|
+
resolvedOptions: () => ({
|
|
996
|
+
pluralCategories: ["one", "other"]
|
|
997
|
+
})
|
|
998
|
+
};
|
|
999
|
+
class PluralResolver {
|
|
1000
|
+
constructor(languageUtils, options = {}) {
|
|
1001
|
+
this.languageUtils = languageUtils;
|
|
1002
|
+
this.options = options;
|
|
1003
|
+
this.logger = baseLogger.create("pluralResolver");
|
|
1004
|
+
this.pluralRulesCache = {};
|
|
1005
|
+
}
|
|
1006
|
+
addRule(lng, obj) {
|
|
1007
|
+
this.rules[lng] = obj;
|
|
1008
|
+
}
|
|
1009
|
+
clearCache() {
|
|
1010
|
+
this.pluralRulesCache = {};
|
|
1011
|
+
}
|
|
1012
|
+
getRule(code, options = {}) {
|
|
1013
|
+
const cleanedCode = getCleanedCode(code === "dev" ? "en" : code);
|
|
1014
|
+
const type = options.ordinal ? "ordinal" : "cardinal";
|
|
1015
|
+
const cacheKey = JSON.stringify({
|
|
1016
|
+
cleanedCode,
|
|
1017
|
+
type
|
|
1018
|
+
});
|
|
1019
|
+
if (cacheKey in this.pluralRulesCache) {
|
|
1020
|
+
return this.pluralRulesCache[cacheKey];
|
|
1021
|
+
}
|
|
1022
|
+
let rule;
|
|
1023
|
+
try {
|
|
1024
|
+
rule = new Intl.PluralRules(cleanedCode, {
|
|
1025
|
+
type
|
|
1026
|
+
});
|
|
1027
|
+
} catch (err) {
|
|
1028
|
+
if (!Intl) {
|
|
1029
|
+
this.logger.error("No Intl support, please use an Intl polyfill!");
|
|
1030
|
+
return dummyRule;
|
|
1031
|
+
}
|
|
1032
|
+
if (!code.match(/-|_/)) return dummyRule;
|
|
1033
|
+
const lngPart = this.languageUtils.getLanguagePartFromCode(code);
|
|
1034
|
+
rule = this.getRule(lngPart, options);
|
|
1035
|
+
}
|
|
1036
|
+
this.pluralRulesCache[cacheKey] = rule;
|
|
1037
|
+
return rule;
|
|
1038
|
+
}
|
|
1039
|
+
needsPlural(code, options = {}) {
|
|
1040
|
+
let rule = this.getRule(code, options);
|
|
1041
|
+
if (!rule) rule = this.getRule("dev", options);
|
|
1042
|
+
return rule?.resolvedOptions().pluralCategories.length > 1;
|
|
1043
|
+
}
|
|
1044
|
+
getPluralFormsOfKey(code, key, options = {}) {
|
|
1045
|
+
return this.getSuffixes(code, options).map((suffix) => `${key}${suffix}`);
|
|
1046
|
+
}
|
|
1047
|
+
getSuffixes(code, options = {}) {
|
|
1048
|
+
let rule = this.getRule(code, options);
|
|
1049
|
+
if (!rule) rule = this.getRule("dev", options);
|
|
1050
|
+
if (!rule) return [];
|
|
1051
|
+
return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map((pluralCategory) => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ""}${pluralCategory}`);
|
|
1052
|
+
}
|
|
1053
|
+
getSuffix(code, count, options = {}) {
|
|
1054
|
+
const rule = this.getRule(code, options);
|
|
1055
|
+
if (rule) {
|
|
1056
|
+
return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ""}${rule.select(count)}`;
|
|
1057
|
+
}
|
|
1058
|
+
this.logger.warn(`no plural rule found for: ${code}`);
|
|
1059
|
+
return this.getSuffix("dev", count, options);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
const deepFindWithDefaults = (data, defaultData, key, keySeparator = ".", ignoreJSONStructure = true) => {
|
|
1063
|
+
let path = getPathWithDefaults(data, defaultData, key);
|
|
1064
|
+
if (!path && ignoreJSONStructure && isString(key)) {
|
|
1065
|
+
path = deepFind(data, key, keySeparator);
|
|
1066
|
+
if (path === void 0) path = deepFind(defaultData, key, keySeparator);
|
|
1067
|
+
}
|
|
1068
|
+
return path;
|
|
1069
|
+
};
|
|
1070
|
+
const regexSafe = (val) => val.replace(/\$/g, "$$$$");
|
|
1071
|
+
class Interpolator {
|
|
1072
|
+
constructor(options = {}) {
|
|
1073
|
+
this.logger = baseLogger.create("interpolator");
|
|
1074
|
+
this.options = options;
|
|
1075
|
+
this.format = options?.interpolation?.format || ((value) => value);
|
|
1076
|
+
this.init(options);
|
|
1077
|
+
}
|
|
1078
|
+
init(options = {}) {
|
|
1079
|
+
if (!options.interpolation) options.interpolation = {
|
|
1080
|
+
escapeValue: true
|
|
1081
|
+
};
|
|
1082
|
+
const {
|
|
1083
|
+
escape: escape$1,
|
|
1084
|
+
escapeValue,
|
|
1085
|
+
useRawValueToEscape,
|
|
1086
|
+
prefix,
|
|
1087
|
+
prefixEscaped,
|
|
1088
|
+
suffix,
|
|
1089
|
+
suffixEscaped,
|
|
1090
|
+
formatSeparator,
|
|
1091
|
+
unescapeSuffix,
|
|
1092
|
+
unescapePrefix,
|
|
1093
|
+
nestingPrefix,
|
|
1094
|
+
nestingPrefixEscaped,
|
|
1095
|
+
nestingSuffix,
|
|
1096
|
+
nestingSuffixEscaped,
|
|
1097
|
+
nestingOptionsSeparator,
|
|
1098
|
+
maxReplaces,
|
|
1099
|
+
alwaysFormat
|
|
1100
|
+
} = options.interpolation;
|
|
1101
|
+
this.escape = escape$1 !== void 0 ? escape$1 : escape;
|
|
1102
|
+
this.escapeValue = escapeValue !== void 0 ? escapeValue : true;
|
|
1103
|
+
this.useRawValueToEscape = useRawValueToEscape !== void 0 ? useRawValueToEscape : false;
|
|
1104
|
+
this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || "{{";
|
|
1105
|
+
this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || "}}";
|
|
1106
|
+
this.formatSeparator = formatSeparator || ",";
|
|
1107
|
+
this.unescapePrefix = unescapeSuffix ? "" : unescapePrefix || "-";
|
|
1108
|
+
this.unescapeSuffix = this.unescapePrefix ? "" : unescapeSuffix || "";
|
|
1109
|
+
this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape("$t(");
|
|
1110
|
+
this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(")");
|
|
1111
|
+
this.nestingOptionsSeparator = nestingOptionsSeparator || ",";
|
|
1112
|
+
this.maxReplaces = maxReplaces || 1e3;
|
|
1113
|
+
this.alwaysFormat = alwaysFormat !== void 0 ? alwaysFormat : false;
|
|
1114
|
+
this.resetRegExp();
|
|
1115
|
+
}
|
|
1116
|
+
reset() {
|
|
1117
|
+
if (this.options) this.init(this.options);
|
|
1118
|
+
}
|
|
1119
|
+
resetRegExp() {
|
|
1120
|
+
const getOrResetRegExp = (existingRegExp, pattern) => {
|
|
1121
|
+
if (existingRegExp?.source === pattern) {
|
|
1122
|
+
existingRegExp.lastIndex = 0;
|
|
1123
|
+
return existingRegExp;
|
|
1124
|
+
}
|
|
1125
|
+
return new RegExp(pattern, "g");
|
|
1126
|
+
};
|
|
1127
|
+
this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
|
|
1128
|
+
this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
|
|
1129
|
+
this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}((?:[^()"']+|"[^"]*"|'[^']*'|\\((?:[^()]|"[^"]*"|'[^']*')*\\))*?)${this.nestingSuffix}`);
|
|
1130
|
+
}
|
|
1131
|
+
interpolate(str, data, lng, options) {
|
|
1132
|
+
let match;
|
|
1133
|
+
let value;
|
|
1134
|
+
let replaces;
|
|
1135
|
+
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
|
1136
|
+
const handleFormat = (key) => {
|
|
1137
|
+
if (key.indexOf(this.formatSeparator) < 0) {
|
|
1138
|
+
const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
|
|
1139
|
+
return this.alwaysFormat ? this.format(path, void 0, lng, {
|
|
1140
|
+
...options,
|
|
1141
|
+
...data,
|
|
1142
|
+
interpolationkey: key
|
|
1143
|
+
}) : path;
|
|
1144
|
+
}
|
|
1145
|
+
const p = key.split(this.formatSeparator);
|
|
1146
|
+
const k = p.shift().trim();
|
|
1147
|
+
const f = p.join(this.formatSeparator).trim();
|
|
1148
|
+
return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
|
|
1149
|
+
...options,
|
|
1150
|
+
...data,
|
|
1151
|
+
interpolationkey: k
|
|
1152
|
+
});
|
|
1153
|
+
};
|
|
1154
|
+
this.resetRegExp();
|
|
1155
|
+
const missingInterpolationHandler = options?.missingInterpolationHandler || this.options.missingInterpolationHandler;
|
|
1156
|
+
const skipOnVariables = options?.interpolation?.skipOnVariables !== void 0 ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
|
|
1157
|
+
const todos = [{
|
|
1158
|
+
regex: this.regexpUnescape,
|
|
1159
|
+
safeValue: (val) => regexSafe(val)
|
|
1160
|
+
}, {
|
|
1161
|
+
regex: this.regexp,
|
|
1162
|
+
safeValue: (val) => this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)
|
|
1163
|
+
}];
|
|
1164
|
+
todos.forEach((todo) => {
|
|
1165
|
+
replaces = 0;
|
|
1166
|
+
while (match = todo.regex.exec(str)) {
|
|
1167
|
+
const matchedVar = match[1].trim();
|
|
1168
|
+
value = handleFormat(matchedVar);
|
|
1169
|
+
if (value === void 0) {
|
|
1170
|
+
if (typeof missingInterpolationHandler === "function") {
|
|
1171
|
+
const temp = missingInterpolationHandler(str, match, options);
|
|
1172
|
+
value = isString(temp) ? temp : "";
|
|
1173
|
+
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
|
1174
|
+
value = "";
|
|
1175
|
+
} else if (skipOnVariables) {
|
|
1176
|
+
value = match[0];
|
|
1177
|
+
continue;
|
|
1178
|
+
} else {
|
|
1179
|
+
this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
|
|
1180
|
+
value = "";
|
|
1181
|
+
}
|
|
1182
|
+
} else if (!isString(value) && !this.useRawValueToEscape) {
|
|
1183
|
+
value = makeString(value);
|
|
1184
|
+
}
|
|
1185
|
+
const safeValue = todo.safeValue(value);
|
|
1186
|
+
str = str.replace(match[0], safeValue);
|
|
1187
|
+
if (skipOnVariables) {
|
|
1188
|
+
todo.regex.lastIndex += value.length;
|
|
1189
|
+
todo.regex.lastIndex -= match[0].length;
|
|
1190
|
+
} else {
|
|
1191
|
+
todo.regex.lastIndex = 0;
|
|
1192
|
+
}
|
|
1193
|
+
replaces++;
|
|
1194
|
+
if (replaces >= this.maxReplaces) {
|
|
1195
|
+
break;
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
});
|
|
1199
|
+
return str;
|
|
1200
|
+
}
|
|
1201
|
+
nest(str, fc, options = {}) {
|
|
1202
|
+
let match;
|
|
1203
|
+
let value;
|
|
1204
|
+
let clonedOptions;
|
|
1205
|
+
const handleHasOptions = (key, inheritedOptions) => {
|
|
1206
|
+
const sep = this.nestingOptionsSeparator;
|
|
1207
|
+
if (key.indexOf(sep) < 0) return key;
|
|
1208
|
+
const c = key.split(new RegExp(`${sep}[ ]*{`));
|
|
1209
|
+
let optionsString = `{${c[1]}`;
|
|
1210
|
+
key = c[0];
|
|
1211
|
+
optionsString = this.interpolate(optionsString, clonedOptions);
|
|
1212
|
+
const matchedSingleQuotes = optionsString.match(/'/g);
|
|
1213
|
+
const matchedDoubleQuotes = optionsString.match(/"/g);
|
|
1214
|
+
if ((matchedSingleQuotes?.length ?? 0) % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
|
|
1215
|
+
optionsString = optionsString.replace(/'/g, '"');
|
|
1216
|
+
}
|
|
1217
|
+
try {
|
|
1218
|
+
clonedOptions = JSON.parse(optionsString);
|
|
1219
|
+
if (inheritedOptions) clonedOptions = {
|
|
1220
|
+
...inheritedOptions,
|
|
1221
|
+
...clonedOptions
|
|
1222
|
+
};
|
|
1223
|
+
} catch (e) {
|
|
1224
|
+
this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
|
|
1225
|
+
return `${key}${sep}${optionsString}`;
|
|
1226
|
+
}
|
|
1227
|
+
if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
|
|
1228
|
+
return key;
|
|
1229
|
+
};
|
|
1230
|
+
while (match = this.nestingRegexp.exec(str)) {
|
|
1231
|
+
let formatters = [];
|
|
1232
|
+
clonedOptions = {
|
|
1233
|
+
...options
|
|
1234
|
+
};
|
|
1235
|
+
clonedOptions = clonedOptions.replace && !isString(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
|
|
1236
|
+
clonedOptions.applyPostProcessor = false;
|
|
1237
|
+
delete clonedOptions.defaultValue;
|
|
1238
|
+
const keyEndIndex = /{.*}/.test(match[1]) ? match[1].lastIndexOf("}") + 1 : match[1].indexOf(this.formatSeparator);
|
|
1239
|
+
if (keyEndIndex !== -1) {
|
|
1240
|
+
formatters = match[1].slice(keyEndIndex).split(this.formatSeparator).map((elem) => elem.trim()).filter(Boolean);
|
|
1241
|
+
match[1] = match[1].slice(0, keyEndIndex);
|
|
1242
|
+
}
|
|
1243
|
+
value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
|
|
1244
|
+
if (value && match[0] === str && !isString(value)) return value;
|
|
1245
|
+
if (!isString(value)) value = makeString(value);
|
|
1246
|
+
if (!value) {
|
|
1247
|
+
this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
|
|
1248
|
+
value = "";
|
|
1249
|
+
}
|
|
1250
|
+
if (formatters.length) {
|
|
1251
|
+
value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
|
|
1252
|
+
...options,
|
|
1253
|
+
interpolationkey: match[1].trim()
|
|
1254
|
+
}), value.trim());
|
|
1255
|
+
}
|
|
1256
|
+
str = str.replace(match[0], value);
|
|
1257
|
+
this.regexp.lastIndex = 0;
|
|
1258
|
+
}
|
|
1259
|
+
return str;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
const parseFormatStr = (formatStr) => {
|
|
1263
|
+
let formatName = formatStr.toLowerCase().trim();
|
|
1264
|
+
const formatOptions = {};
|
|
1265
|
+
if (formatStr.indexOf("(") > -1) {
|
|
1266
|
+
const p = formatStr.split("(");
|
|
1267
|
+
formatName = p[0].toLowerCase().trim();
|
|
1268
|
+
const optStr = p[1].substring(0, p[1].length - 1);
|
|
1269
|
+
if (formatName === "currency" && optStr.indexOf(":") < 0) {
|
|
1270
|
+
if (!formatOptions.currency) formatOptions.currency = optStr.trim();
|
|
1271
|
+
} else if (formatName === "relativetime" && optStr.indexOf(":") < 0) {
|
|
1272
|
+
if (!formatOptions.range) formatOptions.range = optStr.trim();
|
|
1273
|
+
} else {
|
|
1274
|
+
const opts = optStr.split(";");
|
|
1275
|
+
opts.forEach((opt) => {
|
|
1276
|
+
if (opt) {
|
|
1277
|
+
const [key, ...rest] = opt.split(":");
|
|
1278
|
+
const val = rest.join(":").trim().replace(/^'+|'+$/g, "");
|
|
1279
|
+
const trimmedKey = key.trim();
|
|
1280
|
+
if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
|
|
1281
|
+
if (val === "false") formatOptions[trimmedKey] = false;
|
|
1282
|
+
if (val === "true") formatOptions[trimmedKey] = true;
|
|
1283
|
+
if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
|
|
1284
|
+
}
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
return {
|
|
1289
|
+
formatName,
|
|
1290
|
+
formatOptions
|
|
1291
|
+
};
|
|
1292
|
+
};
|
|
1293
|
+
const createCachedFormatter = (fn) => {
|
|
1294
|
+
const cache = {};
|
|
1295
|
+
return (v, l, o) => {
|
|
1296
|
+
let optForCache = o;
|
|
1297
|
+
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
|
1298
|
+
optForCache = {
|
|
1299
|
+
...optForCache,
|
|
1300
|
+
[o.interpolationkey]: void 0
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
const key = l + JSON.stringify(optForCache);
|
|
1304
|
+
let frm = cache[key];
|
|
1305
|
+
if (!frm) {
|
|
1306
|
+
frm = fn(getCleanedCode(l), o);
|
|
1307
|
+
cache[key] = frm;
|
|
1308
|
+
}
|
|
1309
|
+
return frm(v);
|
|
1310
|
+
};
|
|
1311
|
+
};
|
|
1312
|
+
const createNonCachedFormatter = (fn) => (v, l, o) => fn(getCleanedCode(l), o)(v);
|
|
1313
|
+
class Formatter {
|
|
1314
|
+
constructor(options = {}) {
|
|
1315
|
+
this.logger = baseLogger.create("formatter");
|
|
1316
|
+
this.options = options;
|
|
1317
|
+
this.init(options);
|
|
1318
|
+
}
|
|
1319
|
+
init(services, options = {
|
|
1320
|
+
interpolation: {}
|
|
1321
|
+
}) {
|
|
1322
|
+
this.formatSeparator = options.interpolation.formatSeparator || ",";
|
|
1323
|
+
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1324
|
+
this.formats = {
|
|
1325
|
+
number: cf((lng, opt) => {
|
|
1326
|
+
const formatter = new Intl.NumberFormat(lng, {
|
|
1327
|
+
...opt
|
|
1328
|
+
});
|
|
1329
|
+
return (val) => formatter.format(val);
|
|
1330
|
+
}),
|
|
1331
|
+
currency: cf((lng, opt) => {
|
|
1332
|
+
const formatter = new Intl.NumberFormat(lng, {
|
|
1333
|
+
...opt,
|
|
1334
|
+
style: "currency"
|
|
1335
|
+
});
|
|
1336
|
+
return (val) => formatter.format(val);
|
|
1337
|
+
}),
|
|
1338
|
+
datetime: cf((lng, opt) => {
|
|
1339
|
+
const formatter = new Intl.DateTimeFormat(lng, {
|
|
1340
|
+
...opt
|
|
1341
|
+
});
|
|
1342
|
+
return (val) => formatter.format(val);
|
|
1343
|
+
}),
|
|
1344
|
+
relativetime: cf((lng, opt) => {
|
|
1345
|
+
const formatter = new Intl.RelativeTimeFormat(lng, {
|
|
1346
|
+
...opt
|
|
1347
|
+
});
|
|
1348
|
+
return (val) => formatter.format(val, opt.range || "day");
|
|
1349
|
+
}),
|
|
1350
|
+
list: cf((lng, opt) => {
|
|
1351
|
+
const formatter = new Intl.ListFormat(lng, {
|
|
1352
|
+
...opt
|
|
1353
|
+
});
|
|
1354
|
+
return (val) => formatter.format(val);
|
|
1355
|
+
})
|
|
1356
|
+
};
|
|
1357
|
+
}
|
|
1358
|
+
add(name, fc) {
|
|
1359
|
+
this.formats[name.toLowerCase().trim()] = fc;
|
|
1360
|
+
}
|
|
1361
|
+
addCached(name, fc) {
|
|
1362
|
+
this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
|
|
1363
|
+
}
|
|
1364
|
+
format(value, format, lng, options = {}) {
|
|
1365
|
+
const formats = format.split(this.formatSeparator);
|
|
1366
|
+
if (formats.length > 1 && formats[0].indexOf("(") > 1 && formats[0].indexOf(")") < 0 && formats.find((f) => f.indexOf(")") > -1)) {
|
|
1367
|
+
const lastIndex = formats.findIndex((f) => f.indexOf(")") > -1);
|
|
1368
|
+
formats[0] = [formats[0], ...formats.splice(1, lastIndex)].join(this.formatSeparator);
|
|
1369
|
+
}
|
|
1370
|
+
const result = formats.reduce((mem, f) => {
|
|
1371
|
+
const {
|
|
1372
|
+
formatName,
|
|
1373
|
+
formatOptions
|
|
1374
|
+
} = parseFormatStr(f);
|
|
1375
|
+
if (this.formats[formatName]) {
|
|
1376
|
+
let formatted = mem;
|
|
1377
|
+
try {
|
|
1378
|
+
const valOptions = options?.formatParams?.[options.interpolationkey] || {};
|
|
1379
|
+
const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
|
|
1380
|
+
formatted = this.formats[formatName](mem, l, {
|
|
1381
|
+
...formatOptions,
|
|
1382
|
+
...options,
|
|
1383
|
+
...valOptions
|
|
1384
|
+
});
|
|
1385
|
+
} catch (error) {
|
|
1386
|
+
this.logger.warn(error);
|
|
1387
|
+
}
|
|
1388
|
+
return formatted;
|
|
1389
|
+
} else {
|
|
1390
|
+
this.logger.warn(`there was no format function for ${formatName}`);
|
|
1391
|
+
}
|
|
1392
|
+
return mem;
|
|
1393
|
+
}, value);
|
|
1394
|
+
return result;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
const removePending = (q, name) => {
|
|
1398
|
+
if (q.pending[name] !== void 0) {
|
|
1399
|
+
delete q.pending[name];
|
|
1400
|
+
q.pendingCount--;
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
class Connector extends EventEmitter {
|
|
1404
|
+
constructor(backend, store, services, options = {}) {
|
|
1405
|
+
super();
|
|
1406
|
+
this.backend = backend;
|
|
1407
|
+
this.store = store;
|
|
1408
|
+
this.services = services;
|
|
1409
|
+
this.languageUtils = services.languageUtils;
|
|
1410
|
+
this.options = options;
|
|
1411
|
+
this.logger = baseLogger.create("backendConnector");
|
|
1412
|
+
this.waitingReads = [];
|
|
1413
|
+
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1414
|
+
this.readingCalls = 0;
|
|
1415
|
+
this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
|
|
1416
|
+
this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
|
|
1417
|
+
this.state = {};
|
|
1418
|
+
this.queue = [];
|
|
1419
|
+
this.backend?.init?.(services, options.backend, options);
|
|
1420
|
+
}
|
|
1421
|
+
queueLoad(languages, namespaces, options, callback) {
|
|
1422
|
+
const toLoad = {};
|
|
1423
|
+
const pending = {};
|
|
1424
|
+
const toLoadLanguages = {};
|
|
1425
|
+
const toLoadNamespaces = {};
|
|
1426
|
+
languages.forEach((lng) => {
|
|
1427
|
+
let hasAllNamespaces = true;
|
|
1428
|
+
namespaces.forEach((ns) => {
|
|
1429
|
+
const name = `${lng}|${ns}`;
|
|
1430
|
+
if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
|
|
1431
|
+
this.state[name] = 2;
|
|
1432
|
+
} else if (this.state[name] < 0) ;
|
|
1433
|
+
else if (this.state[name] === 1) {
|
|
1434
|
+
if (pending[name] === void 0) pending[name] = true;
|
|
1435
|
+
} else {
|
|
1436
|
+
this.state[name] = 1;
|
|
1437
|
+
hasAllNamespaces = false;
|
|
1438
|
+
if (pending[name] === void 0) pending[name] = true;
|
|
1439
|
+
if (toLoad[name] === void 0) toLoad[name] = true;
|
|
1440
|
+
if (toLoadNamespaces[ns] === void 0) toLoadNamespaces[ns] = true;
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
if (!hasAllNamespaces) toLoadLanguages[lng] = true;
|
|
1444
|
+
});
|
|
1445
|
+
if (Object.keys(toLoad).length || Object.keys(pending).length) {
|
|
1446
|
+
this.queue.push({
|
|
1447
|
+
pending,
|
|
1448
|
+
pendingCount: Object.keys(pending).length,
|
|
1449
|
+
loaded: {},
|
|
1450
|
+
errors: [],
|
|
1451
|
+
callback
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
return {
|
|
1455
|
+
toLoad: Object.keys(toLoad),
|
|
1456
|
+
pending: Object.keys(pending),
|
|
1457
|
+
toLoadLanguages: Object.keys(toLoadLanguages),
|
|
1458
|
+
toLoadNamespaces: Object.keys(toLoadNamespaces)
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
loaded(name, err, data) {
|
|
1462
|
+
const s = name.split("|");
|
|
1463
|
+
const lng = s[0];
|
|
1464
|
+
const ns = s[1];
|
|
1465
|
+
if (err) this.emit("failedLoading", lng, ns, err);
|
|
1466
|
+
if (!err && data) {
|
|
1467
|
+
this.store.addResourceBundle(lng, ns, data, void 0, void 0, {
|
|
1468
|
+
skipCopy: true
|
|
1469
|
+
});
|
|
1470
|
+
}
|
|
1471
|
+
this.state[name] = err ? -1 : 2;
|
|
1472
|
+
if (err && data) this.state[name] = 0;
|
|
1473
|
+
const loaded = {};
|
|
1474
|
+
this.queue.forEach((q) => {
|
|
1475
|
+
pushPath(q.loaded, [lng], ns);
|
|
1476
|
+
removePending(q, name);
|
|
1477
|
+
if (err) q.errors.push(err);
|
|
1478
|
+
if (q.pendingCount === 0 && !q.done) {
|
|
1479
|
+
Object.keys(q.loaded).forEach((l) => {
|
|
1480
|
+
if (!loaded[l]) loaded[l] = {};
|
|
1481
|
+
const loadedKeys = q.loaded[l];
|
|
1482
|
+
if (loadedKeys.length) {
|
|
1483
|
+
loadedKeys.forEach((n) => {
|
|
1484
|
+
if (loaded[l][n] === void 0) loaded[l][n] = true;
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
});
|
|
1488
|
+
q.done = true;
|
|
1489
|
+
if (q.errors.length) {
|
|
1490
|
+
q.callback(q.errors);
|
|
1491
|
+
} else {
|
|
1492
|
+
q.callback();
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
});
|
|
1496
|
+
this.emit("loaded", loaded);
|
|
1497
|
+
this.queue = this.queue.filter((q) => !q.done);
|
|
1498
|
+
}
|
|
1499
|
+
read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
|
|
1500
|
+
if (!lng.length) return callback(null, {});
|
|
1501
|
+
if (this.readingCalls >= this.maxParallelReads) {
|
|
1502
|
+
this.waitingReads.push({
|
|
1503
|
+
lng,
|
|
1504
|
+
ns,
|
|
1505
|
+
fcName,
|
|
1506
|
+
tried,
|
|
1507
|
+
wait,
|
|
1508
|
+
callback
|
|
1509
|
+
});
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
this.readingCalls++;
|
|
1513
|
+
const resolver = (err, data) => {
|
|
1514
|
+
this.readingCalls--;
|
|
1515
|
+
if (this.waitingReads.length > 0) {
|
|
1516
|
+
const next = this.waitingReads.shift();
|
|
1517
|
+
this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
|
|
1518
|
+
}
|
|
1519
|
+
if (err && data && tried < this.maxRetries) {
|
|
1520
|
+
setTimeout(() => {
|
|
1521
|
+
this.read.call(this, lng, ns, fcName, tried + 1, wait * 2, callback);
|
|
1522
|
+
}, wait);
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
callback(err, data);
|
|
1526
|
+
};
|
|
1527
|
+
const fc = this.backend[fcName].bind(this.backend);
|
|
1528
|
+
if (fc.length === 2) {
|
|
1529
|
+
try {
|
|
1530
|
+
const r = fc(lng, ns);
|
|
1531
|
+
if (r && typeof r.then === "function") {
|
|
1532
|
+
r.then((data) => resolver(null, data)).catch(resolver);
|
|
1533
|
+
} else {
|
|
1534
|
+
resolver(null, r);
|
|
1535
|
+
}
|
|
1536
|
+
} catch (err) {
|
|
1537
|
+
resolver(err);
|
|
1538
|
+
}
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
return fc(lng, ns, resolver);
|
|
1542
|
+
}
|
|
1543
|
+
prepareLoading(languages, namespaces, options = {}, callback) {
|
|
1544
|
+
if (!this.backend) {
|
|
1545
|
+
this.logger.warn("No backend was added via i18next.use. Will not load resources.");
|
|
1546
|
+
return callback && callback();
|
|
1547
|
+
}
|
|
1548
|
+
if (isString(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
|
|
1549
|
+
if (isString(namespaces)) namespaces = [namespaces];
|
|
1550
|
+
const toLoad = this.queueLoad(languages, namespaces, options, callback);
|
|
1551
|
+
if (!toLoad.toLoad.length) {
|
|
1552
|
+
if (!toLoad.pending.length) callback();
|
|
1553
|
+
return null;
|
|
1554
|
+
}
|
|
1555
|
+
toLoad.toLoad.forEach((name) => {
|
|
1556
|
+
this.loadOne(name);
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
load(languages, namespaces, callback) {
|
|
1560
|
+
this.prepareLoading(languages, namespaces, {}, callback);
|
|
1561
|
+
}
|
|
1562
|
+
reload(languages, namespaces, callback) {
|
|
1563
|
+
this.prepareLoading(languages, namespaces, {
|
|
1564
|
+
reload: true
|
|
1565
|
+
}, callback);
|
|
1566
|
+
}
|
|
1567
|
+
loadOne(name, prefix = "") {
|
|
1568
|
+
const s = name.split("|");
|
|
1569
|
+
const lng = s[0];
|
|
1570
|
+
const ns = s[1];
|
|
1571
|
+
this.read(lng, ns, "read", void 0, void 0, (err, data) => {
|
|
1572
|
+
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1573
|
+
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
1574
|
+
this.loaded(name, err, data);
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {
|
|
1578
|
+
}) {
|
|
1579
|
+
if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
|
|
1580
|
+
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!!!");
|
|
1581
|
+
return;
|
|
1582
|
+
}
|
|
1583
|
+
if (key === void 0 || key === null || key === "") return;
|
|
1584
|
+
if (this.backend?.create) {
|
|
1585
|
+
const opts = {
|
|
1586
|
+
...options,
|
|
1587
|
+
isUpdate
|
|
1588
|
+
};
|
|
1589
|
+
const fc = this.backend.create.bind(this.backend);
|
|
1590
|
+
if (fc.length < 6) {
|
|
1591
|
+
try {
|
|
1592
|
+
let r;
|
|
1593
|
+
if (fc.length === 5) {
|
|
1594
|
+
r = fc(languages, namespace, key, fallbackValue, opts);
|
|
1595
|
+
} else {
|
|
1596
|
+
r = fc(languages, namespace, key, fallbackValue);
|
|
1597
|
+
}
|
|
1598
|
+
if (r && typeof r.then === "function") {
|
|
1599
|
+
r.then((data) => clb(null, data)).catch(clb);
|
|
1600
|
+
} else {
|
|
1601
|
+
clb(null, r);
|
|
1602
|
+
}
|
|
1603
|
+
} catch (err) {
|
|
1604
|
+
clb(err);
|
|
1605
|
+
}
|
|
1606
|
+
} else {
|
|
1607
|
+
fc(languages, namespace, key, fallbackValue, clb, opts);
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
if (!languages || !languages[0]) return;
|
|
1611
|
+
this.store.addResource(languages[0], namespace, key, fallbackValue);
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
const get = () => ({
|
|
1615
|
+
debug: false,
|
|
1616
|
+
initAsync: true,
|
|
1617
|
+
ns: ["translation"],
|
|
1618
|
+
defaultNS: ["translation"],
|
|
1619
|
+
fallbackLng: ["dev"],
|
|
1620
|
+
fallbackNS: false,
|
|
1621
|
+
supportedLngs: false,
|
|
1622
|
+
nonExplicitSupportedLngs: false,
|
|
1623
|
+
load: "all",
|
|
1624
|
+
preload: false,
|
|
1625
|
+
simplifyPluralSuffix: true,
|
|
1626
|
+
keySeparator: ".",
|
|
1627
|
+
nsSeparator: ":",
|
|
1628
|
+
pluralSeparator: "_",
|
|
1629
|
+
contextSeparator: "_",
|
|
1630
|
+
partialBundledLanguages: false,
|
|
1631
|
+
saveMissing: false,
|
|
1632
|
+
updateMissing: false,
|
|
1633
|
+
saveMissingTo: "fallback",
|
|
1634
|
+
saveMissingPlurals: true,
|
|
1635
|
+
missingKeyHandler: false,
|
|
1636
|
+
missingInterpolationHandler: false,
|
|
1637
|
+
postProcess: false,
|
|
1638
|
+
postProcessPassResolved: false,
|
|
1639
|
+
returnNull: false,
|
|
1640
|
+
returnEmptyString: true,
|
|
1641
|
+
returnObjects: false,
|
|
1642
|
+
joinArrays: false,
|
|
1643
|
+
returnedObjectHandler: false,
|
|
1644
|
+
parseMissingKeyHandler: false,
|
|
1645
|
+
appendNamespaceToMissingKey: false,
|
|
1646
|
+
appendNamespaceToCIMode: false,
|
|
1647
|
+
overloadTranslationOptionHandler: (args) => {
|
|
1648
|
+
let ret = {};
|
|
1649
|
+
if (typeof args[1] === "object") ret = args[1];
|
|
1650
|
+
if (isString(args[1])) ret.defaultValue = args[1];
|
|
1651
|
+
if (isString(args[2])) ret.tDescription = args[2];
|
|
1652
|
+
if (typeof args[2] === "object" || typeof args[3] === "object") {
|
|
1653
|
+
const options = args[3] || args[2];
|
|
1654
|
+
Object.keys(options).forEach((key) => {
|
|
1655
|
+
ret[key] = options[key];
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
return ret;
|
|
1659
|
+
},
|
|
1660
|
+
interpolation: {
|
|
1661
|
+
escapeValue: true,
|
|
1662
|
+
format: (value) => value,
|
|
1663
|
+
prefix: "{{",
|
|
1664
|
+
suffix: "}}",
|
|
1665
|
+
formatSeparator: ",",
|
|
1666
|
+
unescapePrefix: "-",
|
|
1667
|
+
nestingPrefix: "$t(",
|
|
1668
|
+
nestingSuffix: ")",
|
|
1669
|
+
nestingOptionsSeparator: ",",
|
|
1670
|
+
maxReplaces: 1e3,
|
|
1671
|
+
skipOnVariables: true
|
|
1672
|
+
},
|
|
1673
|
+
cacheInBuiltFormats: true
|
|
1674
|
+
});
|
|
1675
|
+
const transformOptions = (options) => {
|
|
1676
|
+
if (isString(options.ns)) options.ns = [options.ns];
|
|
1677
|
+
if (isString(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
|
|
1678
|
+
if (isString(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
|
|
1679
|
+
if (options.supportedLngs?.indexOf?.("cimode") < 0) {
|
|
1680
|
+
options.supportedLngs = options.supportedLngs.concat(["cimode"]);
|
|
1681
|
+
}
|
|
1682
|
+
if (typeof options.initImmediate === "boolean") options.initAsync = options.initImmediate;
|
|
1683
|
+
return options;
|
|
1684
|
+
};
|
|
1685
|
+
const noop = () => {
|
|
1686
|
+
};
|
|
1687
|
+
const bindMemberFunctions = (inst) => {
|
|
1688
|
+
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1689
|
+
mems.forEach((mem) => {
|
|
1690
|
+
if (typeof inst[mem] === "function") {
|
|
1691
|
+
inst[mem] = inst[mem].bind(inst);
|
|
1692
|
+
}
|
|
1693
|
+
});
|
|
1694
|
+
};
|
|
1695
|
+
class I18n extends EventEmitter {
|
|
1696
|
+
constructor(options = {}, callback) {
|
|
1697
|
+
super();
|
|
1698
|
+
this.options = transformOptions(options);
|
|
1699
|
+
this.services = {};
|
|
1700
|
+
this.logger = baseLogger;
|
|
1701
|
+
this.modules = {
|
|
1702
|
+
external: []
|
|
1703
|
+
};
|
|
1704
|
+
bindMemberFunctions(this);
|
|
1705
|
+
if (callback && !this.isInitialized && !options.isClone) {
|
|
1706
|
+
if (!this.options.initAsync) {
|
|
1707
|
+
this.init(options, callback);
|
|
1708
|
+
return this;
|
|
1709
|
+
}
|
|
1710
|
+
setTimeout(() => {
|
|
1711
|
+
this.init(options, callback);
|
|
1712
|
+
}, 0);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
init(options = {}, callback) {
|
|
1716
|
+
this.isInitializing = true;
|
|
1717
|
+
if (typeof options === "function") {
|
|
1718
|
+
callback = options;
|
|
1719
|
+
options = {};
|
|
1720
|
+
}
|
|
1721
|
+
if (options.defaultNS == null && options.ns) {
|
|
1722
|
+
if (isString(options.ns)) {
|
|
1723
|
+
options.defaultNS = options.ns;
|
|
1724
|
+
} else if (options.ns.indexOf("translation") < 0) {
|
|
1725
|
+
options.defaultNS = options.ns[0];
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
const defOpts = get();
|
|
1729
|
+
this.options = {
|
|
1730
|
+
...defOpts,
|
|
1731
|
+
...this.options,
|
|
1732
|
+
...transformOptions(options)
|
|
1733
|
+
};
|
|
1734
|
+
this.options.interpolation = {
|
|
1735
|
+
...defOpts.interpolation,
|
|
1736
|
+
...this.options.interpolation
|
|
1737
|
+
};
|
|
1738
|
+
if (options.keySeparator !== void 0) {
|
|
1739
|
+
this.options.userDefinedKeySeparator = options.keySeparator;
|
|
1740
|
+
}
|
|
1741
|
+
if (options.nsSeparator !== void 0) {
|
|
1742
|
+
this.options.userDefinedNsSeparator = options.nsSeparator;
|
|
1743
|
+
}
|
|
1744
|
+
if (typeof this.options.overloadTranslationOptionHandler !== "function") {
|
|
1745
|
+
this.options.overloadTranslationOptionHandler = defOpts.overloadTranslationOptionHandler;
|
|
1746
|
+
}
|
|
1747
|
+
const createClassOnDemand = (ClassOrObject) => {
|
|
1748
|
+
if (!ClassOrObject) return null;
|
|
1749
|
+
if (typeof ClassOrObject === "function") return new ClassOrObject();
|
|
1750
|
+
return ClassOrObject;
|
|
1751
|
+
};
|
|
1752
|
+
if (!this.options.isClone) {
|
|
1753
|
+
if (this.modules.logger) {
|
|
1754
|
+
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1755
|
+
} else {
|
|
1756
|
+
baseLogger.init(null, this.options);
|
|
1757
|
+
}
|
|
1758
|
+
let formatter;
|
|
1759
|
+
if (this.modules.formatter) {
|
|
1760
|
+
formatter = this.modules.formatter;
|
|
1761
|
+
} else {
|
|
1762
|
+
formatter = Formatter;
|
|
1763
|
+
}
|
|
1764
|
+
const lu = new LanguageUtil(this.options);
|
|
1765
|
+
this.store = new ResourceStore(this.options.resources, this.options);
|
|
1766
|
+
const s = this.services;
|
|
1767
|
+
s.logger = baseLogger;
|
|
1768
|
+
s.resourceStore = this.store;
|
|
1769
|
+
s.languageUtils = lu;
|
|
1770
|
+
s.pluralResolver = new PluralResolver(lu, {
|
|
1771
|
+
prepend: this.options.pluralSeparator,
|
|
1772
|
+
simplifyPluralSuffix: this.options.simplifyPluralSuffix
|
|
1773
|
+
});
|
|
1774
|
+
const usingLegacyFormatFunction = this.options.interpolation.format && this.options.interpolation.format !== defOpts.interpolation.format;
|
|
1775
|
+
if (usingLegacyFormatFunction) {
|
|
1776
|
+
this.logger.deprecate(`init: you are still using the legacy format function, please use the new approach: https://www.i18next.com/translation-function/formatting`);
|
|
1777
|
+
}
|
|
1778
|
+
if (formatter && (!this.options.interpolation.format || this.options.interpolation.format === defOpts.interpolation.format)) {
|
|
1779
|
+
s.formatter = createClassOnDemand(formatter);
|
|
1780
|
+
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1781
|
+
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1782
|
+
}
|
|
1783
|
+
s.interpolator = new Interpolator(this.options);
|
|
1784
|
+
s.utils = {
|
|
1785
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
|
1786
|
+
};
|
|
1787
|
+
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1788
|
+
s.backendConnector.on("*", (event, ...args) => {
|
|
1789
|
+
this.emit(event, ...args);
|
|
1790
|
+
});
|
|
1791
|
+
if (this.modules.languageDetector) {
|
|
1792
|
+
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
|
1793
|
+
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
|
1794
|
+
}
|
|
1795
|
+
if (this.modules.i18nFormat) {
|
|
1796
|
+
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
|
1797
|
+
if (s.i18nFormat.init) s.i18nFormat.init(this);
|
|
1798
|
+
}
|
|
1799
|
+
this.translator = new Translator(this.services, this.options);
|
|
1800
|
+
this.translator.on("*", (event, ...args) => {
|
|
1801
|
+
this.emit(event, ...args);
|
|
1802
|
+
});
|
|
1803
|
+
this.modules.external.forEach((m) => {
|
|
1804
|
+
if (m.init) m.init(this);
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
this.format = this.options.interpolation.format;
|
|
1808
|
+
if (!callback) callback = noop;
|
|
1809
|
+
if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
|
|
1810
|
+
const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
|
1811
|
+
if (codes.length > 0 && codes[0] !== "dev") this.options.lng = codes[0];
|
|
1812
|
+
}
|
|
1813
|
+
if (!this.services.languageDetector && !this.options.lng) {
|
|
1814
|
+
this.logger.warn("init: no languageDetector is used and no lng is defined");
|
|
1815
|
+
}
|
|
1816
|
+
const storeApi = ["getResource", "hasResourceBundle", "getResourceBundle", "getDataByLanguage"];
|
|
1817
|
+
storeApi.forEach((fcName) => {
|
|
1818
|
+
this[fcName] = (...args) => this.store[fcName](...args);
|
|
1819
|
+
});
|
|
1820
|
+
const storeApiChained = ["addResource", "addResources", "addResourceBundle", "removeResourceBundle"];
|
|
1821
|
+
storeApiChained.forEach((fcName) => {
|
|
1822
|
+
this[fcName] = (...args) => {
|
|
1823
|
+
this.store[fcName](...args);
|
|
1824
|
+
return this;
|
|
1825
|
+
};
|
|
1826
|
+
});
|
|
1827
|
+
const deferred = defer();
|
|
1828
|
+
const load = () => {
|
|
1829
|
+
const finish = (err, t) => {
|
|
1830
|
+
this.isInitializing = false;
|
|
1831
|
+
if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn("init: i18next is already initialized. You should call init just once!");
|
|
1832
|
+
this.isInitialized = true;
|
|
1833
|
+
if (!this.options.isClone) this.logger.log("initialized", this.options);
|
|
1834
|
+
this.emit("initialized", this.options);
|
|
1835
|
+
deferred.resolve(t);
|
|
1836
|
+
callback(err, t);
|
|
1837
|
+
};
|
|
1838
|
+
if (this.languages && !this.isInitialized) return finish(null, this.t.bind(this));
|
|
1839
|
+
this.changeLanguage(this.options.lng, finish);
|
|
1840
|
+
};
|
|
1841
|
+
if (this.options.resources || !this.options.initAsync) {
|
|
1842
|
+
load();
|
|
1843
|
+
} else {
|
|
1844
|
+
setTimeout(load, 0);
|
|
1845
|
+
}
|
|
1846
|
+
return deferred;
|
|
1847
|
+
}
|
|
1848
|
+
loadResources(language, callback = noop) {
|
|
1849
|
+
let usedCallback = callback;
|
|
1850
|
+
const usedLng = isString(language) ? language : this.language;
|
|
1851
|
+
if (typeof language === "function") usedCallback = language;
|
|
1852
|
+
if (!this.options.resources || this.options.partialBundledLanguages) {
|
|
1853
|
+
if (usedLng?.toLowerCase() === "cimode" && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
|
|
1854
|
+
const toLoad = [];
|
|
1855
|
+
const append = (lng) => {
|
|
1856
|
+
if (!lng) return;
|
|
1857
|
+
if (lng === "cimode") return;
|
|
1858
|
+
const lngs = this.services.languageUtils.toResolveHierarchy(lng);
|
|
1859
|
+
lngs.forEach((l) => {
|
|
1860
|
+
if (l === "cimode") return;
|
|
1861
|
+
if (toLoad.indexOf(l) < 0) toLoad.push(l);
|
|
1862
|
+
});
|
|
1863
|
+
};
|
|
1864
|
+
if (!usedLng) {
|
|
1865
|
+
const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
|
1866
|
+
fallbacks.forEach((l) => append(l));
|
|
1867
|
+
} else {
|
|
1868
|
+
append(usedLng);
|
|
1869
|
+
}
|
|
1870
|
+
this.options.preload?.forEach?.((l) => append(l));
|
|
1871
|
+
this.services.backendConnector.load(toLoad, this.options.ns, (e) => {
|
|
1872
|
+
if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
|
|
1873
|
+
usedCallback(e);
|
|
1874
|
+
});
|
|
1875
|
+
} else {
|
|
1876
|
+
usedCallback(null);
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
reloadResources(lngs, ns, callback) {
|
|
1880
|
+
const deferred = defer();
|
|
1881
|
+
if (typeof lngs === "function") {
|
|
1882
|
+
callback = lngs;
|
|
1883
|
+
lngs = void 0;
|
|
1884
|
+
}
|
|
1885
|
+
if (typeof ns === "function") {
|
|
1886
|
+
callback = ns;
|
|
1887
|
+
ns = void 0;
|
|
1888
|
+
}
|
|
1889
|
+
if (!lngs) lngs = this.languages;
|
|
1890
|
+
if (!ns) ns = this.options.ns;
|
|
1891
|
+
if (!callback) callback = noop;
|
|
1892
|
+
this.services.backendConnector.reload(lngs, ns, (err) => {
|
|
1893
|
+
deferred.resolve();
|
|
1894
|
+
callback(err);
|
|
1895
|
+
});
|
|
1896
|
+
return deferred;
|
|
1897
|
+
}
|
|
1898
|
+
use(module2) {
|
|
1899
|
+
if (!module2) throw new Error("You are passing an undefined module! Please check the object you are passing to i18next.use()");
|
|
1900
|
+
if (!module2.type) throw new Error("You are passing a wrong module! Please check the object you are passing to i18next.use()");
|
|
1901
|
+
if (module2.type === "backend") {
|
|
1902
|
+
this.modules.backend = module2;
|
|
1903
|
+
}
|
|
1904
|
+
if (module2.type === "logger" || module2.log && module2.warn && module2.error) {
|
|
1905
|
+
this.modules.logger = module2;
|
|
1906
|
+
}
|
|
1907
|
+
if (module2.type === "languageDetector") {
|
|
1908
|
+
this.modules.languageDetector = module2;
|
|
1909
|
+
}
|
|
1910
|
+
if (module2.type === "i18nFormat") {
|
|
1911
|
+
this.modules.i18nFormat = module2;
|
|
1912
|
+
}
|
|
1913
|
+
if (module2.type === "postProcessor") {
|
|
1914
|
+
postProcessor.addPostProcessor(module2);
|
|
1915
|
+
}
|
|
1916
|
+
if (module2.type === "formatter") {
|
|
1917
|
+
this.modules.formatter = module2;
|
|
1918
|
+
}
|
|
1919
|
+
if (module2.type === "3rdParty") {
|
|
1920
|
+
this.modules.external.push(module2);
|
|
1921
|
+
}
|
|
1922
|
+
return this;
|
|
1923
|
+
}
|
|
1924
|
+
setResolvedLanguage(l) {
|
|
1925
|
+
if (!l || !this.languages) return;
|
|
1926
|
+
if (["cimode", "dev"].indexOf(l) > -1) return;
|
|
1927
|
+
for (let li = 0; li < this.languages.length; li++) {
|
|
1928
|
+
const lngInLngs = this.languages[li];
|
|
1929
|
+
if (["cimode", "dev"].indexOf(lngInLngs) > -1) continue;
|
|
1930
|
+
if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
|
|
1931
|
+
this.resolvedLanguage = lngInLngs;
|
|
1932
|
+
break;
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
if (!this.resolvedLanguage && this.languages.indexOf(l) < 0 && this.store.hasLanguageSomeTranslations(l)) {
|
|
1936
|
+
this.resolvedLanguage = l;
|
|
1937
|
+
this.languages.unshift(l);
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
changeLanguage(lng, callback) {
|
|
1941
|
+
this.isLanguageChangingTo = lng;
|
|
1942
|
+
const deferred = defer();
|
|
1943
|
+
this.emit("languageChanging", lng);
|
|
1944
|
+
const setLngProps = (l) => {
|
|
1945
|
+
this.language = l;
|
|
1946
|
+
this.languages = this.services.languageUtils.toResolveHierarchy(l);
|
|
1947
|
+
this.resolvedLanguage = void 0;
|
|
1948
|
+
this.setResolvedLanguage(l);
|
|
1949
|
+
};
|
|
1950
|
+
const done = (err, l) => {
|
|
1951
|
+
if (l) {
|
|
1952
|
+
if (this.isLanguageChangingTo === lng) {
|
|
1953
|
+
setLngProps(l);
|
|
1954
|
+
this.translator.changeLanguage(l);
|
|
1955
|
+
this.isLanguageChangingTo = void 0;
|
|
1956
|
+
this.emit("languageChanged", l);
|
|
1957
|
+
this.logger.log("languageChanged", l);
|
|
1958
|
+
}
|
|
1959
|
+
} else {
|
|
1960
|
+
this.isLanguageChangingTo = void 0;
|
|
1961
|
+
}
|
|
1962
|
+
deferred.resolve((...args) => this.t(...args));
|
|
1963
|
+
if (callback) callback(err, (...args) => this.t(...args));
|
|
1964
|
+
};
|
|
1965
|
+
const setLng = (lngs) => {
|
|
1966
|
+
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
1967
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
1968
|
+
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
1969
|
+
if (l) {
|
|
1970
|
+
if (!this.language) {
|
|
1971
|
+
setLngProps(l);
|
|
1972
|
+
}
|
|
1973
|
+
if (!this.translator.language) this.translator.changeLanguage(l);
|
|
1974
|
+
this.services.languageDetector?.cacheUserLanguage?.(l);
|
|
1975
|
+
}
|
|
1976
|
+
this.loadResources(l, (err) => {
|
|
1977
|
+
done(err, l);
|
|
1978
|
+
});
|
|
1979
|
+
};
|
|
1980
|
+
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
|
1981
|
+
setLng(this.services.languageDetector.detect());
|
|
1982
|
+
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
|
1983
|
+
if (this.services.languageDetector.detect.length === 0) {
|
|
1984
|
+
this.services.languageDetector.detect().then(setLng);
|
|
1985
|
+
} else {
|
|
1986
|
+
this.services.languageDetector.detect(setLng);
|
|
1987
|
+
}
|
|
1988
|
+
} else {
|
|
1989
|
+
setLng(lng);
|
|
1990
|
+
}
|
|
1991
|
+
return deferred;
|
|
1992
|
+
}
|
|
1993
|
+
getFixedT(lng, ns, keyPrefix) {
|
|
1994
|
+
const fixedT = (key, opts, ...rest) => {
|
|
1995
|
+
let o;
|
|
1996
|
+
if (typeof opts !== "object") {
|
|
1997
|
+
o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
|
|
1998
|
+
} else {
|
|
1999
|
+
o = {
|
|
2000
|
+
...opts
|
|
2001
|
+
};
|
|
2002
|
+
}
|
|
2003
|
+
o.lng = o.lng || fixedT.lng;
|
|
2004
|
+
o.lngs = o.lngs || fixedT.lngs;
|
|
2005
|
+
o.ns = o.ns || fixedT.ns;
|
|
2006
|
+
if (o.keyPrefix !== "") o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
|
|
2007
|
+
const keySeparator = this.options.keySeparator || ".";
|
|
2008
|
+
let resultKey;
|
|
2009
|
+
if (o.keyPrefix && Array.isArray(key)) {
|
|
2010
|
+
resultKey = key.map((k) => {
|
|
2011
|
+
if (typeof k === "function") k = keysFromSelector(k, {
|
|
2012
|
+
...this.options,
|
|
2013
|
+
...opts
|
|
2014
|
+
});
|
|
2015
|
+
return `${o.keyPrefix}${keySeparator}${k}`;
|
|
2016
|
+
});
|
|
2017
|
+
} else {
|
|
2018
|
+
if (typeof key === "function") key = keysFromSelector(key, {
|
|
2019
|
+
...this.options,
|
|
2020
|
+
...opts
|
|
2021
|
+
});
|
|
2022
|
+
resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
|
|
2023
|
+
}
|
|
2024
|
+
return this.t(resultKey, o);
|
|
2025
|
+
};
|
|
2026
|
+
if (isString(lng)) {
|
|
2027
|
+
fixedT.lng = lng;
|
|
2028
|
+
} else {
|
|
2029
|
+
fixedT.lngs = lng;
|
|
2030
|
+
}
|
|
2031
|
+
fixedT.ns = ns;
|
|
2032
|
+
fixedT.keyPrefix = keyPrefix;
|
|
2033
|
+
return fixedT;
|
|
2034
|
+
}
|
|
2035
|
+
t(...args) {
|
|
2036
|
+
return this.translator?.translate(...args);
|
|
2037
|
+
}
|
|
2038
|
+
exists(...args) {
|
|
2039
|
+
return this.translator?.exists(...args);
|
|
2040
|
+
}
|
|
2041
|
+
setDefaultNamespace(ns) {
|
|
2042
|
+
this.options.defaultNS = ns;
|
|
2043
|
+
}
|
|
2044
|
+
hasLoadedNamespace(ns, options = {}) {
|
|
2045
|
+
if (!this.isInitialized) {
|
|
2046
|
+
this.logger.warn("hasLoadedNamespace: i18next was not initialized", this.languages);
|
|
2047
|
+
return false;
|
|
2048
|
+
}
|
|
2049
|
+
if (!this.languages || !this.languages.length) {
|
|
2050
|
+
this.logger.warn("hasLoadedNamespace: i18n.languages were undefined or empty", this.languages);
|
|
2051
|
+
return false;
|
|
2052
|
+
}
|
|
2053
|
+
const lng = options.lng || this.resolvedLanguage || this.languages[0];
|
|
2054
|
+
const fallbackLng = this.options ? this.options.fallbackLng : false;
|
|
2055
|
+
const lastLng = this.languages[this.languages.length - 1];
|
|
2056
|
+
if (lng.toLowerCase() === "cimode") return true;
|
|
2057
|
+
const loadNotPending = (l, n) => {
|
|
2058
|
+
const loadState = this.services.backendConnector.state[`${l}|${n}`];
|
|
2059
|
+
return loadState === -1 || loadState === 0 || loadState === 2;
|
|
2060
|
+
};
|
|
2061
|
+
if (options.precheck) {
|
|
2062
|
+
const preResult = options.precheck(this, loadNotPending);
|
|
2063
|
+
if (preResult !== void 0) return preResult;
|
|
2064
|
+
}
|
|
2065
|
+
if (this.hasResourceBundle(lng, ns)) return true;
|
|
2066
|
+
if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
|
|
2067
|
+
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
|
|
2068
|
+
return false;
|
|
2069
|
+
}
|
|
2070
|
+
loadNamespaces(ns, callback) {
|
|
2071
|
+
const deferred = defer();
|
|
2072
|
+
if (!this.options.ns) {
|
|
2073
|
+
if (callback) callback();
|
|
2074
|
+
return Promise.resolve();
|
|
2075
|
+
}
|
|
2076
|
+
if (isString(ns)) ns = [ns];
|
|
2077
|
+
ns.forEach((n) => {
|
|
2078
|
+
if (this.options.ns.indexOf(n) < 0) this.options.ns.push(n);
|
|
2079
|
+
});
|
|
2080
|
+
this.loadResources((err) => {
|
|
2081
|
+
deferred.resolve();
|
|
2082
|
+
if (callback) callback(err);
|
|
2083
|
+
});
|
|
2084
|
+
return deferred;
|
|
2085
|
+
}
|
|
2086
|
+
loadLanguages(lngs, callback) {
|
|
2087
|
+
const deferred = defer();
|
|
2088
|
+
if (isString(lngs)) lngs = [lngs];
|
|
2089
|
+
const preloaded = this.options.preload || [];
|
|
2090
|
+
const newLngs = lngs.filter((lng) => preloaded.indexOf(lng) < 0 && this.services.languageUtils.isSupportedCode(lng));
|
|
2091
|
+
if (!newLngs.length) {
|
|
2092
|
+
if (callback) callback();
|
|
2093
|
+
return Promise.resolve();
|
|
2094
|
+
}
|
|
2095
|
+
this.options.preload = preloaded.concat(newLngs);
|
|
2096
|
+
this.loadResources((err) => {
|
|
2097
|
+
deferred.resolve();
|
|
2098
|
+
if (callback) callback(err);
|
|
2099
|
+
});
|
|
2100
|
+
return deferred;
|
|
2101
|
+
}
|
|
2102
|
+
dir(lng) {
|
|
2103
|
+
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
|
2104
|
+
if (!lng) return "rtl";
|
|
2105
|
+
try {
|
|
2106
|
+
const l = new Intl.Locale(lng);
|
|
2107
|
+
if (l && l.getTextInfo) {
|
|
2108
|
+
const ti = l.getTextInfo();
|
|
2109
|
+
if (ti && ti.direction) return ti.direction;
|
|
2110
|
+
}
|
|
2111
|
+
} catch (e) {
|
|
2112
|
+
}
|
|
2113
|
+
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"];
|
|
2114
|
+
const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
|
|
2115
|
+
if (lng.toLowerCase().indexOf("-latn") > 1) return "ltr";
|
|
2116
|
+
return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf("-arab") > 1 ? "rtl" : "ltr";
|
|
2117
|
+
}
|
|
2118
|
+
static createInstance(options = {}, callback) {
|
|
2119
|
+
const instance2 = new I18n(options, callback);
|
|
2120
|
+
instance2.createInstance = I18n.createInstance;
|
|
2121
|
+
return instance2;
|
|
2122
|
+
}
|
|
2123
|
+
cloneInstance(options = {}, callback = noop) {
|
|
2124
|
+
const forkResourceStore = options.forkResourceStore;
|
|
2125
|
+
if (forkResourceStore) delete options.forkResourceStore;
|
|
2126
|
+
const mergedOptions = {
|
|
2127
|
+
...this.options,
|
|
2128
|
+
...options,
|
|
2129
|
+
...{
|
|
2130
|
+
isClone: true
|
|
2131
|
+
}
|
|
2132
|
+
};
|
|
2133
|
+
const clone = new I18n(mergedOptions);
|
|
2134
|
+
if (options.debug !== void 0 || options.prefix !== void 0) {
|
|
2135
|
+
clone.logger = clone.logger.clone(options);
|
|
2136
|
+
}
|
|
2137
|
+
const membersToCopy = ["store", "services", "language"];
|
|
2138
|
+
membersToCopy.forEach((m) => {
|
|
2139
|
+
clone[m] = this[m];
|
|
2140
|
+
});
|
|
2141
|
+
clone.services = {
|
|
2142
|
+
...this.services
|
|
2143
|
+
};
|
|
2144
|
+
clone.services.utils = {
|
|
2145
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2146
|
+
};
|
|
2147
|
+
if (forkResourceStore) {
|
|
2148
|
+
const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
|
|
2149
|
+
prev[l] = {
|
|
2150
|
+
...this.store.data[l]
|
|
2151
|
+
};
|
|
2152
|
+
prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
|
|
2153
|
+
acc[n] = {
|
|
2154
|
+
...prev[l][n]
|
|
2155
|
+
};
|
|
2156
|
+
return acc;
|
|
2157
|
+
}, prev[l]);
|
|
2158
|
+
return prev;
|
|
2159
|
+
}, {});
|
|
2160
|
+
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2161
|
+
clone.services.resourceStore = clone.store;
|
|
2162
|
+
}
|
|
2163
|
+
if (options.interpolation) clone.services.interpolator = new Interpolator(mergedOptions);
|
|
2164
|
+
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2165
|
+
clone.translator.on("*", (event, ...args) => {
|
|
2166
|
+
clone.emit(event, ...args);
|
|
2167
|
+
});
|
|
2168
|
+
clone.init(mergedOptions, callback);
|
|
2169
|
+
clone.translator.options = mergedOptions;
|
|
2170
|
+
clone.translator.backendConnector.services.utils = {
|
|
2171
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2172
|
+
};
|
|
2173
|
+
return clone;
|
|
2174
|
+
}
|
|
2175
|
+
toJSON() {
|
|
2176
|
+
return {
|
|
2177
|
+
options: this.options,
|
|
2178
|
+
store: this.store,
|
|
2179
|
+
language: this.language,
|
|
2180
|
+
languages: this.languages,
|
|
2181
|
+
resolvedLanguage: this.resolvedLanguage
|
|
2182
|
+
};
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
const instance = I18n.createInstance();
|
|
2186
|
+
const createInstance = instance.createInstance;
|
|
2187
|
+
instance.dir;
|
|
2188
|
+
instance.init;
|
|
2189
|
+
instance.loadResources;
|
|
2190
|
+
instance.reloadResources;
|
|
2191
|
+
instance.use;
|
|
2192
|
+
instance.changeLanguage;
|
|
2193
|
+
instance.getFixedT;
|
|
2194
|
+
instance.t;
|
|
2195
|
+
instance.exists;
|
|
2196
|
+
instance.setDefaultNamespace;
|
|
2197
|
+
instance.hasLoadedNamespace;
|
|
2198
|
+
instance.loadNamespaces;
|
|
2199
|
+
instance.loadLanguages;
|
|
2200
|
+
const components = { "back-to-homepage": "Terug naar de homepage", "back-to-homepage-landmark": "$t(components.back-to-homepage)", "back-to-main-site": "Terug naar {{siteName}}", "back-to-main-site-landmark": "$t(components.back-to-main-site)", "search": "Zoeken", "search-label": "Voer een zoekterm in", "search-help": "Zoek in componentnamen en beschrijvingen. Druk op Enter om te zoeken.", "search-button": "Zoek", "current-filter": "Huidige filter:", "remove-filter": " - Klik om filter te verwijderen", "filter-by": "Filter op {{type}}", "search-results_all": 'Alle $t(components.items, {"count": 99 })', "search-results_filtered": "Gefilterde resultaten", "search-results-amount_all": '{{amount}} $t(components.items, {"count": {{amount}} }) in onze database', "search-results-amount_filtered": '{{amount}} $t(components.items, {"count": {{amount}} }) gevonden', "search-results-amount-no-items": 'Geen $t(components.items, {"count": 0 }) gevonden', "status-active": "Active", "status-active_deprecated": "Active, wordt Deprecated per {{date}}", "status-deprecated": "Deprecated", "status-deprecated_deprecated": "Deprecated per {{date}}", "status-deprecated_sunset": "Deprecated, in Sunset per {{date}}", "status-sunset": "Sunset, wordt Retired per {{date}}", "status-retired": "Retired per {{date}}", "version": "Versie {{version}}", "view-details": "Bekijk details", "items_one": "item", "items_other": "items", "no-items-found": 'Geen $t(components.items, {"count": 99 }) gevonden.', "pages": "Pagina’s", "prev": "Vorige", "next": "Volgende", "pagination-landmark": "Paginering", "pagination-out-of-bounds_one": "Pagina {{page}} bestaat niet. Er is in totaal {{count}} pagina.", "pagination-out-of-bounds_other": "Pagina {{page}} bestaat niet. Er zijn in totaal {{count}} pagina’s.", "results-range_zero": "Geen resultaat", "results-range_one": "Resultaat {{range.0}}", "results-range_other": "Resultaten {{range.0}} tot en met {{range.1}}", "page-and-results-range": "Pagina {{label}}: $t(components.results-range)", "created-at": "Aangemaakt op", "updated-at": "Laatst bijgewerkt op", "last-crawled-at": "Laatst gecrawld op", "last-activity-at": "Laatste activiteit op", "adr-score": "ADR Score", "open-in": "Open in", "download-as": "Download als", "read-documentation-at": "Lees de documentatie op", "copy-to-clipboard": "Kopieer naar klembord", "goto-name-on-site": "Ga naar {{name}} op {{site}}", "no-items-present": "Geen {{items}} gedefinieerd", "item-not-found-error": '$t(components.items, {"count": 1 }) niet gevonden. Controleer of het ID correct is en probeer het opnieuw. Als het probleem zich blijft voordoen, neem dan contact op met de beheerder van het register.', "fuzz-error": "These aren't the droids you're looking for" };
|
|
2201
|
+
const layouts = { "meta-description": "Welkom bij het API-Register. Hier vind je informatie over de API's die beschikbaar zijn voor gebruik binnen de overheid." };
|
|
2202
|
+
const pages = { "aside-landmark": "Zijbalk", "meta-landmark": "Metaformatie", "details-of-item": 'Details van $t(components.items, {"count": 1 })', "overview": "Overzicht", "description": "Beschrijving", "tools": "Aan de slag", "documentation": "Documentatie", "servers-and-auth": "Servers en authenticatie", "authentication": "Authenticatie", "tbd-metadata": "Metadata", "servers": "Servers", "links": "Links", "repository-url": "Repository URL", "public-code-url": "PublicCode URL" };
|
|
2203
|
+
const resources = {
|
|
2204
|
+
components,
|
|
2205
|
+
layouts,
|
|
2206
|
+
pages
|
|
2207
|
+
};
|
|
2208
|
+
const i18nInstance = createInstance({
|
|
2209
|
+
lng: "nl",
|
|
2210
|
+
fallbackLng: "nl",
|
|
2211
|
+
// debug: true,
|
|
2212
|
+
interpolation: {
|
|
2213
|
+
escapeValue: false
|
|
2214
|
+
// not needed for react as it escapes by default
|
|
2215
|
+
},
|
|
2216
|
+
resources: {
|
|
2217
|
+
nl: {
|
|
2218
|
+
translation: resources
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
});
|
|
2222
|
+
i18nInstance.init();
|
|
2223
|
+
module.exports = i18nInstance;
|