@fluenti/vue 0.3.3 → 0.4.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,420 +1,182 @@
1
- import { Comment as e, Text as t, defineComponent as n, h as r, inject as i, isVNode as a, ref as o, shallowReactive as s } from "vue";
2
- import { createDiagnostics as c, formatDate as l, formatNumber as u, msg as d } from "@fluenti/core";
3
- import { PLURAL_CATEGORIES as f, buildICUMessage as p, buildICUPluralMessage as m, buildICUSelectMessage as h, hashMessage as g, interpolate as _, normalizeSelectForms as v, offsetIndices as y, resolveDescriptorId as b } from "@fluenti/core/internal";
4
- //#region src/use-i18n.ts
5
- function x() {
6
- let e = i(P);
7
- if (!e) throw Error("[fluenti] useI18n() requires createFluenti plugin");
8
- return e;
9
- }
10
- //#endregion
11
- //#region src/components/rich-text.ts
12
- function S(n) {
13
- let r = [], i = "";
14
- function o(n) {
15
- if (n == null || typeof n == "boolean") return;
16
- if (Array.isArray(n)) {
17
- for (let e of n) o(e);
18
- return;
19
- }
20
- if (typeof n == "string" || typeof n == "number") {
21
- i += String(n);
22
- return;
23
- }
24
- if (!a(n) || n.type === e) return;
25
- if (n.type === t) {
26
- i += typeof n.children == "string" ? n.children : "";
27
- return;
28
- }
29
- let s = r.length, c = S(n.children);
30
- r.push(n), r.push(...c.components), i += `<${s}>${y(c.message, s + 1)}</${s}>`;
31
- }
32
- return o(n), {
33
- message: i,
34
- components: r
35
- };
36
- }
37
- function C(e, t) {
38
- let n = /<(\d+)>([\s\S]*?)<\/\1>/g, i = [], a = 0, o;
39
- for (n.lastIndex = 0, o = n.exec(e); o !== null;) {
40
- o.index > a && i.push(e.slice(a, o.index));
41
- let s = t[Number(o[1])], c = C(o[2], t);
42
- s ? i.push(r(s.type, s.props ?? {}, Array.isArray(c) ? c : [c])) : i.push(o[2]), a = n.lastIndex, o = n.exec(e);
43
- }
44
- return a < e.length && i.push(e.slice(a)), i.length <= 1 ? i[0] ?? "" : i;
45
- }
46
- function w(e, t) {
47
- let n = {}, r = [];
48
- for (let i of e) {
49
- let e = t[i];
50
- if (e === void 0) continue;
51
- let a = S(e);
52
- n[i] = y(a.message, r.length), r.push(...a.components);
53
- }
54
- for (let [i, a] of Object.entries(t)) {
55
- if (e.includes(i) || a === void 0) continue;
56
- let t = S(a);
57
- n[i] = y(t.message, r.length), r.push(...t.components);
58
- }
59
- return {
60
- messages: n,
61
- components: r
62
- };
63
- }
64
- var T = n({
65
- name: "Trans",
66
- props: {
67
- id: String,
68
- context: String,
69
- comment: String,
70
- tag: {
71
- type: String,
72
- default: void 0
73
- }
74
- },
75
- setup(e, { slots: t }) {
76
- let { t: n } = x();
77
- return () => {
78
- let i = t.default?.();
79
- if (!i) return null;
80
- let { message: a, components: o } = S(i), s = n({
81
- ...e.id === void 0 ? {} : { id: e.id },
82
- message: a,
83
- ...e.context === void 0 ? {} : { context: e.context },
84
- ...e.comment === void 0 ? {} : { comment: e.comment }
85
- }), c = o.length > 0 ? C(s, o) : s;
86
- return Array.isArray(c) ? c.length === 1 ? c[0] : e.tag ? r(e.tag, null, c) : c : c;
87
- };
88
- }
89
- }), E = n({
90
- name: "Plural",
91
- props: {
92
- value: {
93
- type: Number,
94
- required: !0
95
- },
96
- id: String,
97
- context: String,
98
- comment: String,
99
- zero: String,
100
- one: String,
101
- two: String,
102
- few: String,
103
- many: String,
104
- other: {
105
- type: String,
106
- required: !0
107
- },
108
- offset: Number,
109
- tag: {
110
- type: String,
111
- default: void 0
112
- }
113
- },
114
- setup(e, { slots: t }) {
115
- let { t: n } = x();
116
- return () => {
117
- let i = {
118
- zero: e.zero,
119
- one: e.one,
120
- two: e.two,
121
- few: e.few,
122
- many: e.many,
123
- other: e.other
124
- };
125
- for (let e of f) {
126
- let n = t[e];
127
- n && (i[e] = n({ count: "#" }));
128
- }
129
- let { messages: a, components: o } = w(f, i), s = m({
130
- ...a.zero !== void 0 && { zero: a.zero },
131
- ...a.one !== void 0 && { one: a.one },
132
- ...a.two !== void 0 && { two: a.two },
133
- ...a.few !== void 0 && { few: a.few },
134
- ...a.many !== void 0 && { many: a.many },
135
- other: a.other ?? ""
136
- }, e.offset), c = n({
137
- id: e.id ?? (e.context === void 0 ? s : g(s, e.context)),
138
- message: s,
139
- ...e.context === void 0 ? {} : { context: e.context },
140
- ...e.comment === void 0 ? {} : { comment: e.comment }
141
- }, { count: e.value }), l = o.length > 0 ? C(c, o) : c;
142
- return e.tag ? r(e.tag, void 0, l ?? void 0) : l ?? null;
143
- };
144
- }
145
- }), D = n({
146
- name: "Select",
147
- inheritAttrs: !1,
148
- props: {
149
- value: {
150
- type: String,
151
- required: !0
152
- },
153
- id: String,
154
- context: String,
155
- comment: String,
156
- other: {
157
- type: String,
158
- required: !0
159
- },
160
- options: {
161
- type: Object,
162
- default: void 0
163
- },
164
- tag: {
165
- type: String,
166
- default: void 0
167
- }
168
- },
169
- setup(e, { attrs: t, slots: n }) {
170
- let { t: i } = x();
171
- return () => {
172
- let a = {};
173
- if (e.options !== void 0) {
174
- for (let [t, n] of Object.entries(e.options)) a[t] = n;
175
- a.other = e.other;
176
- } else {
177
- for (let [e, n] of Object.entries(t)) typeof n == "string" && (a[e] = n);
178
- a.other = e.other;
179
- }
180
- for (let [e, t] of Object.entries(n)) e === "default" || !t || (a[e] = t({ value: "{value}" }));
181
- let o = [...Object.keys(a).filter((e) => e !== "other"), "other"], { messages: s, components: c } = w(o, a), l = v(Object.fromEntries([...o].map((e) => [e, s[e] ?? ""]))), u = h(l.forms), d = i({
182
- id: e.id ?? (e.context === void 0 ? u : g(u, e.context)),
183
- message: u,
184
- ...e.context === void 0 ? {} : { context: e.context },
185
- ...e.comment === void 0 ? {} : { comment: e.comment }
186
- }, { value: l.valueMap[e.value] ?? "other" }), f = c.length > 0 ? C(d, c) : d;
187
- return e.tag ? r(e.tag, void 0, f ?? void 0) : f ?? null;
188
- };
189
- }
190
- }), O = n({
191
- name: "DateTime",
192
- props: {
193
- value: {
194
- type: [Date, Number],
195
- required: !0
196
- },
197
- style: {
198
- type: String,
199
- default: void 0
200
- },
201
- tag: {
202
- type: String,
203
- default: "span"
204
- }
205
- },
206
- setup(e) {
207
- let { d: t } = x();
208
- return () => r(e.tag, t(e.value, e.style));
209
- }
210
- }), k = n({
211
- name: "NumberFormat",
212
- props: {
213
- value: {
214
- type: Number,
215
- required: !0
216
- },
217
- style: {
218
- type: String,
219
- default: void 0
220
- },
221
- tag: {
222
- type: String,
223
- default: "span"
224
- }
225
- },
226
- setup(e) {
227
- let { n: t } = x();
228
- return () => r(e.tag, t(e.value, e.style));
229
- }
230
- });
231
- //#endregion
1
+ import { n as e, t } from "./use-i18n-DrhdlExQ.js";
2
+ import { ref as n, shallowReactive as r } from "vue";
3
+ import { createFluentiCore as i, msg as a } from "@fluenti/core";
232
4
  //#region src/plugin.ts
233
- function A(e) {
5
+ function o(e) {
234
6
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
235
7
  }
236
- var j = Symbol.for("fluenti.runtime.vue.v1");
237
- function M() {
238
- let e = globalThis[j];
8
+ var s = Symbol.for("fluenti.runtime.vue.v1");
9
+ function c() {
10
+ let e = globalThis[s];
239
11
  return typeof e == "object" && e ? e : null;
240
12
  }
241
- function N(e) {
13
+ function l(e) {
242
14
  return typeof e == "object" && e && "default" in e ? e.default : e;
243
15
  }
244
- var P = Symbol("fluenti");
245
- function F(e, t, n) {
246
- return typeof e == "function" ? e(t) : _(e, t, n);
247
- }
248
- function I(e) {
16
+ function u(e) {
249
17
  let t = Object.keys(e).filter((e) => e !== "plural");
250
18
  return t.length > 0 ? t[0] : void 0;
251
19
  }
252
- function L(e) {
253
- let t = e.lazyLocaleLoading ?? e.splitting ?? !1, n = e.diagnostics ? c(e.diagnostics) : void 0, r = o(e.locale), i = s({ ...e.messages }), a = o(!1), d = new Set([e.locale]), f = o(new Set(d));
254
- function m(e, t) {
255
- let n = i[e];
20
+ function d(t) {
21
+ let a = t.lazyLocaleLoading ?? t.splitting ?? !1, s = {
22
+ locale: t.locale,
23
+ messages: t.messages ?? {}
24
+ };
25
+ t.fallbackLocale !== void 0 && (s.fallbackLocale = t.fallbackLocale), t.fallbackChain !== void 0 && (s.fallbackChain = t.fallbackChain), t.dateFormats !== void 0 && (s.dateFormats = t.dateFormats), t.numberFormats !== void 0 && (s.numberFormats = t.numberFormats), t.missing !== void 0 && (s.missing = t.missing), t.diagnostics !== void 0 && (s.diagnostics = t.diagnostics), t.interpolate !== void 0 && (s.interpolate = t.interpolate);
26
+ let d = i(s), f = n(t.locale), p = r({ ...t.messages }), m = n(!1), h = new Set([t.locale]), g = n(new Set(h));
27
+ function _(e, t) {
28
+ let n = p[e];
256
29
  if (n) return n[t];
257
30
  }
258
- function h(t, ...i) {
259
- if (Array.isArray(t) && "raw" in t) return h(p(t, i), Object.fromEntries(i.map((e, t) => [`arg${t}`, e])));
260
- let a = t, o = i[0], s, c;
261
- typeof a == "object" && a ? (s = b(a) ?? "", c = a.message) : s = a;
262
- let l = r.value, u = [l];
263
- if (e.fallbackLocale && !u.includes(e.fallbackLocale) && u.push(e.fallbackLocale), e.fallbackChain?.[l]) for (let t of e.fallbackChain[l]) u.includes(t) || u.push(t);
264
- else if (e.fallbackChain?.["*"]) for (let t of e.fallbackChain["*"]) u.includes(t) || u.push(t);
265
- for (let e of u) {
266
- let t = m(e, s);
267
- if (t !== void 0) return e !== l && n?.fallbackUsed(l, e, s), F(t, o, e);
268
- }
269
- if (n?.missingKey(l, s), e.missing) {
270
- let t = e.missing(l, s);
271
- if (t !== void 0) return t;
272
- }
273
- return c ? _(c, o, l) : s.includes("{") ? _(s, o, l) : s;
31
+ function v() {
32
+ d.locale !== f.value && (d.locale = f.value);
33
+ }
34
+ function y(e, ...t) {
35
+ return p[f.value], v(), Array.isArray(e) && "raw" in e ? d.t(e, ...t) : d.t(e, t[0]);
274
36
  }
275
- let g = 0;
276
- async function v(n) {
277
- if (!t || !e.chunkLoader) {
278
- r.value = n;
37
+ let b = 0;
38
+ async function x(e) {
39
+ if (!a || !t.chunkLoader) {
40
+ d.locale = e, f.value = e;
279
41
  return;
280
42
  }
281
- let o = M();
282
- if (d.has(n)) {
283
- o?.__switchLocale && await o.__switchLocale(n), r.value = n;
43
+ let n = c();
44
+ if (h.has(e)) {
45
+ n?.__switchLocale && await n.__switchLocale(e), d.locale = e, f.value = e;
284
46
  return;
285
47
  }
286
- let s = ++g;
287
- a.value = !0;
48
+ let r = ++b;
49
+ m.value = !0;
288
50
  try {
289
- let t = N(await e.chunkLoader(n));
290
- if (s !== g) return;
291
- i[n] = {
292
- ...i[n],
293
- ...t
294
- }, d.add(n), f.value = new Set(d), o?.__switchLocale && await o.__switchLocale(n), r.value = n;
51
+ let i = l(await t.chunkLoader(e));
52
+ if (r !== b || (p[e] = {
53
+ ...p[e],
54
+ ...i
55
+ }, d.loadMessages(e, i), h.add(e), g.value = new Set(h), n?.__switchLocale && await n.__switchLocale(e), r !== b)) return;
56
+ d.locale = e, f.value = e;
295
57
  } finally {
296
- s === g && (a.value = !1);
58
+ r === b && (m.value = !1);
297
59
  }
298
60
  }
299
- function y(e, t) {
300
- i[e] = {
301
- ...i[e],
61
+ function S(e, t) {
62
+ p[e] = {
63
+ ...p[e],
302
64
  ...t
303
- }, d.add(e), f.value = new Set(d);
304
- }
305
- let x = /* @__PURE__ */ new Set();
306
- function S(n) {
307
- if (!t || d.has(n) || !e.chunkLoader || x.has(n)) return;
308
- x.add(n);
309
- let r = M();
310
- e.chunkLoader(n).then(async (e) => {
311
- let t = N(e);
312
- i[n] = {
313
- ...i[n],
314
- ...t
315
- }, d.add(n), f.value = new Set(d), r?.__preloadLocale && await r.__preloadLocale(n);
316
- }).catch((e) => {
317
- console.warn("[fluenti] preload failed:", n, e);
65
+ }, d.loadMessages(e, t), h.add(e), g.value = new Set(h);
66
+ }
67
+ let C = /* @__PURE__ */ new Set();
68
+ function w(e) {
69
+ if (!a || h.has(e) || !t.chunkLoader || C.has(e)) return;
70
+ C.add(e);
71
+ let n = c();
72
+ t.chunkLoader(e).then(async (t) => {
73
+ let r = l(t);
74
+ p[e] = {
75
+ ...p[e],
76
+ ...r
77
+ }, d.loadMessages(e, r), h.add(e), g.value = new Set(h), n?.__preloadLocale && await n.__preloadLocale(e);
78
+ }).catch((t) => {
79
+ console.warn("[fluenti] preload failed:", e, t);
318
80
  }).finally(() => {
319
- x.delete(n);
81
+ C.delete(e);
320
82
  });
321
83
  }
322
- function C() {
323
- return Object.keys(i);
84
+ function T() {
85
+ return v(), d.getLocales();
324
86
  }
325
- function w(t, n) {
326
- let i = r.value;
327
- return l(t, i, n, e.dateFormats);
87
+ function E(e, t) {
88
+ return f.value, v(), d.d(e, t);
328
89
  }
329
- function j(t, n) {
330
- let i = r.value;
331
- return u(t, i, n, e.numberFormats);
90
+ function D(e, t) {
91
+ return f.value, v(), d.n(e, t);
332
92
  }
333
- function L(e, t) {
334
- return F(e, t, r.value);
93
+ function O(e, t) {
94
+ return f.value, v(), d.format(e, t);
335
95
  }
336
- function R(e, t, n) {
337
- let r = A(n ? h(e, n) : h(e));
96
+ function k(e, t, n) {
97
+ let r = o(n ? y(e, n) : y(e));
338
98
  function i(e) {
339
99
  if (e.rawAttrs != null && e.rawAttrs !== "") {
340
100
  let t = [], n = /([\w:@.!-]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'))?/g, r;
341
101
  for (; (r = n.exec(e.rawAttrs)) !== null;) {
342
- let e = A(r[1]), n = r[2] ?? r[3];
343
- t.push(n === void 0 ? e : `${e}="${A(n)}"`);
102
+ let e = o(r[1]), n = r[2] ?? r[3];
103
+ t.push(n === void 0 ? e : `${e}="${o(n)}"`);
344
104
  }
345
105
  return t.join(" ");
346
106
  }
347
- return e.attrs ? Object.entries(e.attrs).map(([e, t]) => t ? `${A(e)}="${A(t)}"` : A(e)).join(" ") : "";
107
+ return e.attrs ? Object.entries(e.attrs).map(([e, t]) => t ? `${o(e)}="${o(t)}"` : o(e)).join(" ") : "";
348
108
  }
349
109
  let a = r.replace(/&lt;(\d+)\/&gt;/g, (e, n) => {
350
110
  let r = t[Number(n)];
351
111
  if (!r) return "";
352
- let a = A(r.tag), o = i(r);
353
- return `<${a}${o ? " " + o : ""} />`;
112
+ let a = o(r.tag), s = i(r);
113
+ return `<${a}${s ? " " + s : ""} />`;
354
114
  });
355
115
  return a = a.replace(/&lt;(\d+)&gt;([\s\S]*?)&lt;\/\1&gt;/g, (e, n, r) => {
356
116
  let a = t[Number(n)];
357
117
  if (!a) return r;
358
- let o = A(a.tag), s = i(a);
359
- return `<${o}${s ? " " + s : ""}>${r}</${o}>`;
118
+ let s = o(a.tag), c = i(a);
119
+ return `<${s}${c ? " " + c : ""}>${r}</${s}>`;
360
120
  }), a;
361
121
  }
362
- function z(e, t) {
363
- return m(t ?? r.value, e) !== void 0;
364
- }
365
- function B(e, t) {
366
- return m(t ?? r.value, e);
367
- }
368
- let V = {
369
- t: h,
370
- locale: r,
371
- setLocale: v,
372
- loadMessages: y,
373
- getLocales: C,
374
- d: w,
375
- n: j,
376
- format: L,
377
- isLoading: a,
378
- loadedLocales: f,
379
- preloadLocale: S,
380
- te: z,
381
- tm: B
122
+ function A(e, t) {
123
+ return _(t ?? f.value, e) !== void 0;
124
+ }
125
+ function j(e, t) {
126
+ return _(t ?? f.value, e);
127
+ }
128
+ let M = {
129
+ t: y,
130
+ locale: f,
131
+ setLocale: x,
132
+ loadMessages: S,
133
+ getLocales: T,
134
+ d: E,
135
+ n: D,
136
+ format: O,
137
+ isLoading: m,
138
+ loadedLocales: g,
139
+ preloadLocale: w,
140
+ te: A,
141
+ tm: j
382
142
  };
383
143
  return {
384
- install(t) {
385
- t.provide(P, V);
386
- let n = e.componentPrefix ?? "";
387
- t.component(`${n}Trans`, T), t.component(`${n}Plural`, E), t.component(`${n}Select`, D), t.component(`${n}DateTime`, O), t.component(`${n}NumberFormat`, k), e.injectGlobalProperties !== !1 && (t.config.globalProperties.$t = h, t.config.globalProperties.$d = w, t.config.globalProperties.$n = j, t.config.globalProperties.$vtRich = R);
144
+ install(n) {
145
+ if (n.provide(e, M), t.components) {
146
+ let e = t.componentPrefix ?? "", r = t.components;
147
+ r.Trans && n.component(`${e}Trans`, r.Trans), r.Plural && n.component(`${e}Plural`, r.Plural), r.Select && n.component(`${e}Select`, r.Select), r.DateTime && n.component(`${e}DateTime`, r.DateTime), r.NumberFormat && n.component(`${e}NumberFormat`, r.NumberFormat);
148
+ }
149
+ t.injectGlobalProperties !== !1 && (n.config.globalProperties.$t = y, n.config.globalProperties.$d = E, n.config.globalProperties.$n = D, n.config.globalProperties.$vtRich = k);
388
150
  let r = /* @__PURE__ */ new WeakMap();
389
- t.directive("t", {
151
+ n.directive("t", {
390
152
  mounted(e, t) {
391
- let n = I(t.modifiers);
153
+ let n = u(t.modifiers);
392
154
  if (n) {
393
155
  let t = e.getAttribute(n) ?? "";
394
- r.set(e, t), e.setAttribute(n, h(t));
156
+ r.set(e, t), e.setAttribute(n, y(t));
395
157
  } else {
396
158
  let n = t.arg ?? e.textContent ?? "";
397
- r.set(e, n.trim()), e.textContent = h(n.trim(), t.value == null ? void 0 : { ...t.value });
159
+ r.set(e, n.trim()), e.textContent = y(n.trim(), t.value == null ? void 0 : { ...t.value });
398
160
  }
399
161
  },
400
162
  updated(e, t) {
401
- let n = I(t.modifiers);
163
+ let n = u(t.modifiers);
402
164
  if (n) {
403
165
  let t = r.get(e) ?? e.getAttribute(n) ?? "";
404
- e.setAttribute(n, h(t));
405
- } else e.textContent = h((t.arg ?? r.get(e) ?? "").trim(), t.value == null ? void 0 : { ...t.value });
166
+ e.setAttribute(n, y(t));
167
+ } else e.textContent = y((t.arg ?? r.get(e) ?? "").trim(), t.value == null ? void 0 : { ...t.value });
406
168
  }
407
169
  });
408
170
  },
409
- global: V
171
+ global: M
410
172
  };
411
173
  }
412
174
  //#endregion
413
175
  //#region src/compile-time-t.ts
414
- var R = ((...e) => {
176
+ var f = ((...e) => {
415
177
  throw Error("[fluenti] `t` imported from '@fluenti/vue' is a compile-time API. Use it only with the Fluenti build transform inside <script setup> or setup(). For runtime lookups, use useI18n().t(...).");
416
178
  });
417
179
  //#endregion
418
- export { O as DateTime, P as FLUENTI_KEY, k as NumberFormat, E as Plural, D as Select, T as Trans, L as createFluenti, d as msg, R as t, x as useI18n };
180
+ export { e as FLUENTI_KEY, d as createFluenti, a as msg, f as t, t as useI18n };
419
181
 
420
182
  //# sourceMappingURL=index.js.map