@feugene/fint-i18n 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,403 +0,0 @@
1
- import { isRef as e, reactive as t, ref as n, watch as r } from "vue";
2
- //#region src/core/compiler.ts
3
- function i(e) {
4
- if (!e.includes("{")) return () => e;
5
- let t = [], n = 0, r = /\{(\w+)\}/g, i = r.exec(e);
6
- for (; i;) i.index > n && t.push(e.slice(n, i.index)), t.push({
7
- key: i[1],
8
- fallback: i[0]
9
- }), n = i.index + i[0].length, i = r.exec(e);
10
- return n < e.length && t.push(e.slice(n)), (n) => {
11
- if (!n) return e;
12
- let r = "";
13
- for (let e of t) if (typeof e == "string") r += e;
14
- else {
15
- let t = n[e.key];
16
- r += t === void 0 ? e.fallback : String(t);
17
- }
18
- return r;
19
- };
20
- }
21
- //#endregion
22
- //#region src/core/hooks.ts
23
- var a = class {
24
- hooks = /* @__PURE__ */ new Map();
25
- on(e, t) {
26
- let n = this.hooks.get(e);
27
- return n ? n.push(t) : this.hooks.set(e, [t]), () => this.off(e, t);
28
- }
29
- off(e, t) {
30
- let n = this.hooks.get(e);
31
- if (n) {
32
- let r = n.indexOf(t);
33
- r !== -1 && (n.splice(r, 1), n.length === 0 && this.hooks.delete(e));
34
- }
35
- }
36
- emit(e, t) {
37
- let n = this.hooks.get(e);
38
- if (!n || n.length === 0) return Promise.resolve(t);
39
- let r = t, i = n.slice();
40
- return (async () => {
41
- for (let e of i) {
42
- let t = await e(r);
43
- t !== void 0 && (r = t);
44
- }
45
- return r;
46
- })();
47
- }
48
- emitSync(e, t) {
49
- let n = this.hooks.get(e);
50
- if (!n || n.length === 0) return t;
51
- let r = t, i = n.slice();
52
- for (let t of i) {
53
- let n = t(r);
54
- if (n instanceof Promise) {
55
- console.warn(`[fint-i18n] Sync hook "${e}" received a promise from handler. This will be ignored in sync execution.`);
56
- continue;
57
- }
58
- n !== void 0 && (r = n);
59
- }
60
- return r;
61
- }
62
- }, o = ".**", s = ".*";
63
- function c(e) {
64
- return e.endsWith(s) || e.endsWith(o);
65
- }
66
- var l = class {
67
- loaders;
68
- knownBlockNames;
69
- constructor(e) {
70
- this.loaders = this.normalize(e), this.knownBlockNames = this.collectKnownBlockNames(this.loaders);
71
- }
72
- getKnownBlockNames() {
73
- return this.knownBlockNames;
74
- }
75
- expandPattern(e) {
76
- let t = e.endsWith(o), n = !t && e.endsWith(s);
77
- if (!t && !n) return [];
78
- let r = e.slice(0, -(t ? 3 : 2));
79
- if (!r) return [];
80
- let i = `${r}.`, a = i.length, c = [];
81
- for (let e = 0; e < this.knownBlockNames.length; e++) {
82
- let n = this.knownBlockNames[e];
83
- if (n.length <= a) continue;
84
- let r = !0;
85
- for (let e = 0; e < a; e++) if (n.charCodeAt(e) !== i.charCodeAt(e)) {
86
- r = !1;
87
- break;
88
- }
89
- if (r) {
90
- if (!t) {
91
- let e = !1;
92
- for (let t = a; t < n.length; t++) if (n.charCodeAt(t) === 46) {
93
- e = !0;
94
- break;
95
- }
96
- if (e) continue;
97
- }
98
- c.push(n);
99
- }
100
- }
101
- return c;
102
- }
103
- resolve(e, t) {
104
- let n = this.loaders[e];
105
- if (!n) return null;
106
- if (n[t]) return {
107
- resolvedBlockName: t,
108
- loaders: n[t]
109
- };
110
- if (!t.includes(".")) return null;
111
- let r = t.split(".");
112
- for (let e = r.length - 1; e >= 1; e--) {
113
- let t = r.slice(0, e).join("."), i = n[t];
114
- if (i) return {
115
- resolvedBlockName: t,
116
- loaders: i
117
- };
118
- }
119
- return null;
120
- }
121
- normalize(e) {
122
- let t = Object.create(null);
123
- if (!e) return t;
124
- let n = Array.isArray(e) ? e : [e];
125
- for (let e of n) this.mergeCollection(t, e);
126
- return t;
127
- }
128
- mergeCollection(e, t) {
129
- for (let n in t) {
130
- e[n] || (e[n] = Object.create(null));
131
- let r = t[n];
132
- for (let t in r) {
133
- let i = this.normalizeEntry(r[t]);
134
- if (!e[n][t]) {
135
- e[n][t] = i;
136
- continue;
137
- }
138
- e[n][t] = [...e[n][t], ...i];
139
- }
140
- }
141
- }
142
- normalizeEntry(e) {
143
- return Array.isArray(e) ? e : [e];
144
- }
145
- collectKnownBlockNames(e) {
146
- let t = /* @__PURE__ */ new Set();
147
- for (let n in e) {
148
- let r = e[n];
149
- for (let e in r) t.add(e);
150
- }
151
- return Array.from(t);
152
- }
153
- };
154
- //#endregion
155
- //#region src/core/message-utils.ts
156
- function u(e) {
157
- return !!e && typeof e == "object" && !Array.isArray(e);
158
- }
159
- function d(e, t) {
160
- for (let n in t) u(t[n]) ? (u(e[n]) || (e[n] = {}), d(e[n], t[n])) : e[n] = t[n];
161
- }
162
- function f(e, n) {
163
- if (u(e) && u(n)) {
164
- let r = t({});
165
- return d(r, e), d(r, n), r;
166
- }
167
- return n;
168
- }
169
- function p(e, t) {
170
- let n = e, r = 0;
171
- for (; n && typeof n == "object";) {
172
- let e = t.indexOf(".", r), i = e === -1 ? t.slice(r) : t.slice(r, e);
173
- if (n = n[i], n === void 0 || e === -1) return n;
174
- r = e + 1;
175
- }
176
- }
177
- //#endregion
178
- //#region src/core/translate-params.ts
179
- function m(t) {
180
- if (!t) return;
181
- let n;
182
- for (let r in t) {
183
- let i = t[r];
184
- e(i) && (n ||= { ...t }, n[r] = i.value);
185
- }
186
- return n || t;
187
- }
188
- //#endregion
189
- //#region src/core/instance.ts
190
- var h = class {
191
- locale;
192
- fallbackLocale;
193
- messages = t({});
194
- compiledMessages = Object.create(null);
195
- loaderRegistry;
196
- loadingBlocks = /* @__PURE__ */ new Map();
197
- loadedBlocks = /* @__PURE__ */ new Map();
198
- blockUsageCounters = /* @__PURE__ */ new Map();
199
- patternExpansionCache = /* @__PURE__ */ new Map();
200
- pendingUsedBlockLoads = /* @__PURE__ */ new Map();
201
- skipNextUsedBlockLoadLocale = null;
202
- hooks = new a();
203
- constructor(e) {
204
- this.locale = n(e.locale), this.fallbackLocale = e.fallbackLocale || "", this.loaderRegistry = new l(e.loaders), e.plugins && e.plugins.forEach((e) => e.install(this)), r(this.locale, (e, t) => {
205
- if (e !== t) {
206
- if (this.skipNextUsedBlockLoadLocale === e) {
207
- this.skipNextUsedBlockLoadLocale = null;
208
- return;
209
- }
210
- this.loadUsedBlocks(e);
211
- }
212
- }), this.hooks.emitSync("afterInit", void 0);
213
- }
214
- t = (e, t, n) => {
215
- let r = this.locale.value, i = m(t), a = this.hooks.emitSync("onTranslate", {
216
- key: e,
217
- params: i,
218
- result: this.resolve(r, e, i)
219
- }).result ?? e;
220
- if (a === e) {
221
- let t = n?.fallbackLocale || this.fallbackLocale;
222
- if (t && t !== r) {
223
- let n = this.resolve(t, e, i);
224
- if (n !== void 0) return n;
225
- }
226
- this.hooks.emit("onMissingKey", {
227
- key: e,
228
- locale: r
229
- }).catch((e) => {
230
- console.error("[fint-i18n] Error in onMissingKey hook:", e);
231
- });
232
- }
233
- return a;
234
- };
235
- resolve = (e, t, n) => {
236
- let r = this.compiledMessages[e]?.[t];
237
- if (r) return r(n);
238
- let a = this.messages[e];
239
- if (!a) return;
240
- let o = p(a, t);
241
- if (typeof o == "string") {
242
- let r = i(o);
243
- return this.setCompiled(e, t, r), r(n);
244
- }
245
- if (typeof o == "function") {
246
- let r = o;
247
- return this.setCompiled(e, t, r), r(n);
248
- }
249
- if (o != null && typeof o != "object") return String(o);
250
- };
251
- setCompiled = (e, t, n) => {
252
- this.compiledMessages[e] || (this.compiledMessages[e] = Object.create(null)), this.compiledMessages[e][t] = n;
253
- };
254
- expandPattern = (e) => {
255
- let t = this.patternExpansionCache.get(e);
256
- if (t) return t;
257
- let n = this.loaderRegistry.expandPattern(e);
258
- return this.patternExpansionCache.set(e, n), n;
259
- };
260
- loadBlock = async (e, t) => {
261
- let n = t || this.locale.value;
262
- if (c(e)) {
263
- let t = this.expandPattern(e);
264
- if (t.length === 0) {
265
- console.warn(`[fint-i18n] Pattern "${e}" did not match any registered block (locale "${n}")`);
266
- return;
267
- }
268
- await Promise.all(t.map((e) => this.loadBlock(e, n)));
269
- return;
270
- }
271
- let r = `${n}:${e}`;
272
- if (this.isBlockLoaded(e, n)) return;
273
- if (this.loadingBlocks.has(r)) return this.loadingBlocks.get(r);
274
- await this.hooks.emit("beforeLoadBlock", e);
275
- let i = (async () => {
276
- try {
277
- let t = this.loaderRegistry.resolve(n, e);
278
- if (!t) {
279
- console.warn(`[fint-i18n] No loader for block "${e}" in locale "${n}"`);
280
- return;
281
- }
282
- let r;
283
- for (let e of t.loaders) {
284
- let i = await e(), a = i.default || i;
285
- this.mergeMessages(n, t.resolvedBlockName, a), r = r === void 0 ? a : f(r, a);
286
- }
287
- this.markBlockLoaded(t.resolvedBlockName, n), await this.hooks.emit("afterLoadBlock", {
288
- block: t.resolvedBlockName,
289
- locale: n,
290
- messages: r
291
- });
292
- } finally {
293
- this.loadingBlocks.delete(r);
294
- }
295
- })();
296
- return this.loadingBlocks.set(r, i), i;
297
- };
298
- mergeMessages = (e, n, r) => {
299
- this.messages[e] || (this.messages[e] = t({}));
300
- let i = n.split("."), a = i[0];
301
- if (i.length === 1) u(r) ? ((!this.messages[e][a] || typeof this.messages[e][a] != "object") && (this.messages[e][a] = t({})), d(this.messages[e][a], r)) : this.messages[e][a] = r;
302
- else {
303
- (!this.messages[e][a] || typeof this.messages[e][a] != "object") && (this.messages[e][a] = t({}));
304
- let n = this.messages[e][a];
305
- for (let e = 1; e < i.length; e++) {
306
- let r = i[e];
307
- (!n[r] || typeof n[r] != "object") && (n[r] = t({})), n = n[r];
308
- }
309
- d(n, r);
310
- }
311
- this.precompileBlock(e, n, r);
312
- };
313
- precompileBlock = (e, t, n) => {
314
- if (typeof n == "string") {
315
- let r = i(n);
316
- this.setCompiled(e, t, r);
317
- return;
318
- }
319
- if (typeof n == "function") {
320
- this.setCompiled(e, t, n);
321
- return;
322
- }
323
- if (u(n)) for (let r in n) {
324
- let a = n[r], o = `${t}.${r}`;
325
- if (typeof a == "string") {
326
- let t = i(a);
327
- this.setCompiled(e, o, t);
328
- } else typeof a == "function" ? this.setCompiled(e, o, a) : u(a) && this.precompileBlock(e, o, a);
329
- }
330
- };
331
- isBlockLoaded = (e, t) => {
332
- let n = t || this.locale.value, r = this.loadedBlocks.get(n);
333
- if (!r) return !1;
334
- if (r.has(e)) return !0;
335
- let i = e.split("."), a = "";
336
- for (let e = 0; e < i.length - 1; e++) if (a = a ? `${a}.${i[e]}` : i[e], r.has(a)) return !0;
337
- return !1;
338
- };
339
- markBlockLoaded = (e, t) => {
340
- this.loadedBlocks.has(t) || this.loadedBlocks.set(t, /* @__PURE__ */ new Set()), this.loadedBlocks.get(t).add(e);
341
- };
342
- loadUsedBlocks = async (e) => {
343
- let t = this.pendingUsedBlockLoads.get(e);
344
- if (t) {
345
- await t;
346
- return;
347
- }
348
- let n = (async () => {
349
- let t = [];
350
- for (let [n, r] of this.blockUsageCounters.entries()) r > 0 && t.push(this.loadBlock(n, e));
351
- await Promise.all(t);
352
- })();
353
- this.pendingUsedBlockLoads.set(e, n);
354
- try {
355
- await n;
356
- } finally {
357
- this.pendingUsedBlockLoads.delete(e);
358
- }
359
- };
360
- setLocale = async (e) => {
361
- let t = this.locale.value;
362
- t !== e && (await this.loadUsedBlocks(e), this.skipNextUsedBlockLoadLocale = e, this.locale.value = e, this.hooks.emitSync("onLocaleChange", {
363
- locale: e,
364
- previous: t
365
- }));
366
- };
367
- registerUsage = (e) => {
368
- if (c(e)) {
369
- let t = this.expandPattern(e);
370
- if (t.length === 0) {
371
- console.warn(`[fint-i18n] Pattern "${e}" did not match any registered block`);
372
- return;
373
- }
374
- for (let e = 0; e < t.length; e++) this.incrementUsage(t[e]);
375
- return;
376
- }
377
- this.incrementUsage(e);
378
- };
379
- registerBlocks = (e) => {
380
- for (let t = 0; t < e.length; t++) this.registerUsage(e[t]);
381
- };
382
- unregisterUsage = (e) => {
383
- if (c(e)) {
384
- let t = this.expandPattern(e);
385
- for (let e = 0; e < t.length; e++) this.decrementUsage(t[e]);
386
- return;
387
- }
388
- this.decrementUsage(e);
389
- };
390
- incrementUsage = (e) => {
391
- let t = this.blockUsageCounters.get(e) || 0;
392
- this.blockUsageCounters.set(e, t + 1);
393
- };
394
- decrementUsage = (e) => {
395
- let t = this.blockUsageCounters.get(e) || 0;
396
- t <= 1 ? this.blockUsageCounters.delete(e) : this.blockUsageCounters.set(e, t - 1);
397
- };
398
- };
399
- function g(e) {
400
- return new h(e);
401
- }
402
- //#endregion
403
- export { i, g as n, a as r, h as t };