@i18n-micro/core 1.0.28 → 1.1.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/base.d.ts +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +200 -236
- package/dist/route-service.d.ts +6 -5
- package/dist/translation.d.ts +16 -17
- package/package.json +3 -3
- package/src/base.ts +4 -4
- package/src/helpers.ts +7 -6
- package/src/index.ts +2 -2
- package/src/route-service.ts +29 -34
- package/src/translation.ts +71 -181
- package/tests/base.test.ts +11 -36
- package/tests/route-service.test.ts +31 -81
package/dist/index.mjs
CHANGED
|
@@ -1,155 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
t = t[n];
|
|
16
|
-
else
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
return typeof t == "object" && t !== null ? N(t) : t ?? null;
|
|
20
|
-
}
|
|
21
|
-
function p(o) {
|
|
22
|
-
return typeof o == "object" && o !== null && "value" in o ? o.value : o;
|
|
23
|
-
}
|
|
24
|
-
function d(o, e, t) {
|
|
25
|
-
const r = p(o);
|
|
26
|
-
r[e] = t;
|
|
27
|
-
}
|
|
28
|
-
function g(o, e) {
|
|
29
|
-
return p(o)[e];
|
|
1
|
+
function d(o, e) {
|
|
2
|
+
if (!o || typeof e != "string") return null;
|
|
3
|
+
if (e in o) {
|
|
4
|
+
const s = o[e];
|
|
5
|
+
return s;
|
|
6
|
+
}
|
|
7
|
+
const t = e.split(".");
|
|
8
|
+
let r = o;
|
|
9
|
+
for (const s of t)
|
|
10
|
+
if (r && typeof r == "object" && s in r)
|
|
11
|
+
r = r[s];
|
|
12
|
+
else
|
|
13
|
+
return null;
|
|
14
|
+
return r ?? null;
|
|
30
15
|
}
|
|
31
|
-
function
|
|
32
|
-
const e = o?.
|
|
16
|
+
function C(o) {
|
|
17
|
+
const e = o?.translations ?? /* @__PURE__ */ new Map();
|
|
33
18
|
return {
|
|
34
|
-
hasCache(
|
|
35
|
-
const
|
|
36
|
-
return (
|
|
19
|
+
hasCache(t, r) {
|
|
20
|
+
const s = `${t}:${r}`;
|
|
21
|
+
return e.has(s) || e.has(t);
|
|
37
22
|
},
|
|
38
|
-
getCache(
|
|
39
|
-
const
|
|
40
|
-
return
|
|
23
|
+
getCache(t, r) {
|
|
24
|
+
const s = `${t}:${r}`;
|
|
25
|
+
return e.get(s);
|
|
41
26
|
},
|
|
42
|
-
setCache(
|
|
43
|
-
const l = `${s}:${i}`;
|
|
44
|
-
d(n, l, a);
|
|
27
|
+
setCache(t, r, s) {
|
|
45
28
|
},
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
const h = process.env.NODE_ENV !== "production";
|
|
53
|
-
!u && h && console.warn(`[i18n] mergeTranslation called for '${c}' which was not pre-loaded. Skipping merge. Use force: true if this is intentional.`);
|
|
29
|
+
hasTranslation(t, r) {
|
|
30
|
+
for (const [s, n] of e)
|
|
31
|
+
if ((s === t || s.startsWith(`${t}:`)) && d(n, r) !== null)
|
|
32
|
+
return !0;
|
|
33
|
+
return !1;
|
|
54
34
|
},
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
!a && !l && console.error(`marge: route ${s} not loaded`), d(e, s, {
|
|
58
|
-
...l ?? {},
|
|
59
|
-
...i
|
|
60
|
-
});
|
|
35
|
+
hasGeneralTranslation(t) {
|
|
36
|
+
return e.has(t);
|
|
61
37
|
},
|
|
62
|
-
|
|
63
|
-
return
|
|
38
|
+
hasPageTranslation(t, r) {
|
|
39
|
+
return e.has(`${t}:${r}`);
|
|
64
40
|
},
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
41
|
+
getTranslation(t, r, s) {
|
|
42
|
+
const n = `${t}:${r}`, i = e.get(n), a = d(i, s);
|
|
43
|
+
if (a !== null) return a;
|
|
44
|
+
const l = e.get(t);
|
|
45
|
+
return d(l, s);
|
|
68
46
|
},
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
if (v(c[s] || null, i) !== null)
|
|
73
|
-
return !0;
|
|
74
|
-
const l = g(e, s);
|
|
75
|
-
return v(l || null, i) !== null;
|
|
47
|
+
loadTranslations(t, r) {
|
|
48
|
+
const s = e.get(t) ?? {};
|
|
49
|
+
e.set(t, { ...s, ...r });
|
|
76
50
|
},
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
for (const m of C)
|
|
84
|
-
if (h = v(m[s] || null, a), h !== null) break;
|
|
85
|
-
if (!h) {
|
|
86
|
-
const m = g(t, l), R = g(e, s);
|
|
87
|
-
h = v(m || null, a) ?? v(R || null, a);
|
|
88
|
-
}
|
|
89
|
-
if (h) {
|
|
90
|
-
const m = c ?? /* @__PURE__ */ new Map();
|
|
91
|
-
m.set(a, h), d(n, l, m);
|
|
92
|
-
}
|
|
93
|
-
return h;
|
|
51
|
+
setTranslations(t, r) {
|
|
52
|
+
e.set(t, r);
|
|
53
|
+
},
|
|
54
|
+
loadPageTranslations(t, r, s) {
|
|
55
|
+
const n = `${t}:${r}`, i = e.get(n);
|
|
56
|
+
!i || Object.keys(i).length === 0 ? e.set(n, s) : e.set(n, { ...i, ...s });
|
|
94
57
|
},
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
58
|
+
mergeTranslation(t, r, s, n = !1) {
|
|
59
|
+
const i = `${t}:${r}`, a = e.get(i) ?? {};
|
|
60
|
+
e.set(i, { ...a, ...s });
|
|
98
61
|
},
|
|
99
|
-
|
|
100
|
-
|
|
62
|
+
mergeGlobalTranslation(t, r, s = !1) {
|
|
63
|
+
const n = e.get(t) ?? {};
|
|
64
|
+
e.set(t, { ...n, ...r });
|
|
101
65
|
},
|
|
102
66
|
clearCache() {
|
|
103
|
-
|
|
104
|
-
Object.keys(s).forEach((c) => {
|
|
105
|
-
d(e, c, {});
|
|
106
|
-
});
|
|
107
|
-
const i = p(t);
|
|
108
|
-
Object.keys(i).forEach((c) => {
|
|
109
|
-
d(t, c, {});
|
|
110
|
-
});
|
|
111
|
-
const a = p(r);
|
|
112
|
-
a.length = 0;
|
|
113
|
-
const l = p(n);
|
|
114
|
-
Object.keys(l).forEach((c) => {
|
|
115
|
-
g(n, c)?.clear();
|
|
116
|
-
});
|
|
67
|
+
e.clear();
|
|
117
68
|
}
|
|
118
69
|
};
|
|
119
70
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
71
|
+
const y = /\{(\w+)\}/g;
|
|
72
|
+
function P(o, e) {
|
|
73
|
+
return e ? o.replace(y, (t, r) => {
|
|
74
|
+
const s = e[r];
|
|
75
|
+
return s !== void 0 ? String(s) : `{${r}}`;
|
|
76
|
+
}) : o;
|
|
125
77
|
}
|
|
126
|
-
function
|
|
78
|
+
function f(o) {
|
|
127
79
|
return o === "prefix" || o === "prefix_and_default";
|
|
128
80
|
}
|
|
129
|
-
function
|
|
81
|
+
function c(o) {
|
|
130
82
|
return o === "no_prefix";
|
|
131
83
|
}
|
|
132
|
-
function
|
|
84
|
+
function b(o) {
|
|
133
85
|
return o === "prefix";
|
|
134
86
|
}
|
|
135
|
-
function
|
|
87
|
+
function R(o) {
|
|
136
88
|
return o === "prefix_except_default";
|
|
137
89
|
}
|
|
138
|
-
function
|
|
90
|
+
function $(o) {
|
|
139
91
|
return o === "prefix_and_default";
|
|
140
92
|
}
|
|
141
|
-
const
|
|
142
|
-
const
|
|
143
|
-
if (!
|
|
93
|
+
const L = (o, e, t, r, s) => {
|
|
94
|
+
const n = s(o, t);
|
|
95
|
+
if (!n)
|
|
144
96
|
return null;
|
|
145
|
-
const i =
|
|
97
|
+
const i = n.toString().split("|");
|
|
146
98
|
if (i.length === 0) return null;
|
|
147
99
|
const a = e < i.length ? i[e] : i[i.length - 1];
|
|
148
100
|
return a ? a.trim().replace("{count}", e.toString()) : null;
|
|
149
101
|
};
|
|
150
|
-
class
|
|
151
|
-
constructor(e, t, r,
|
|
152
|
-
this.i18nConfig = e, this.router = t, this.hashLocaleDefault = r, this.noPrefixDefault =
|
|
102
|
+
class w {
|
|
103
|
+
constructor(e, t, r, s, n, i) {
|
|
104
|
+
this.i18nConfig = e, this.router = t, this.hashLocaleDefault = r, this.noPrefixDefault = s, this.navigateTo = n, this.getDefaultLocale = i;
|
|
153
105
|
}
|
|
154
106
|
/**
|
|
155
107
|
* Extracts locale from URL path by checking the first path segment
|
|
@@ -162,25 +114,41 @@ class z {
|
|
|
162
114
|
const r = e.split("?")[0]?.split("#")[0];
|
|
163
115
|
if (!r || r === "/")
|
|
164
116
|
return null;
|
|
165
|
-
const
|
|
166
|
-
if (
|
|
117
|
+
const s = r.split("/").filter(Boolean);
|
|
118
|
+
if (s.length === 0)
|
|
167
119
|
return null;
|
|
168
|
-
const
|
|
169
|
-
return
|
|
120
|
+
const n = s[0];
|
|
121
|
+
return n && (this.i18nConfig.locales?.map((a) => a.code) || []).includes(n) ? n : null;
|
|
170
122
|
}
|
|
171
123
|
getCurrentLocale(e) {
|
|
172
|
-
if (e = e ?? this.router.currentRoute.value, this.i18nConfig.hashMode
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return this.
|
|
124
|
+
if (e = e ?? this.router.currentRoute.value, this.i18nConfig.hashMode) {
|
|
125
|
+
const n = this.getDefaultLocale?.();
|
|
126
|
+
if (n) return n;
|
|
127
|
+
if (this.hashLocaleDefault) return this.hashLocaleDefault;
|
|
128
|
+
}
|
|
129
|
+
if (c(this.i18nConfig.strategy)) {
|
|
130
|
+
const n = this.getDefaultLocale?.();
|
|
131
|
+
if (n) return n;
|
|
132
|
+
if (this.noPrefixDefault) return this.noPrefixDefault;
|
|
133
|
+
}
|
|
134
|
+
const t = e.path || e.fullPath || "";
|
|
135
|
+
if ($(this.i18nConfig.strategy) && (t === "/" || t === "")) {
|
|
136
|
+
const n = this.getDefaultLocale?.();
|
|
137
|
+
if (n) return n;
|
|
138
|
+
}
|
|
176
139
|
if (e.params?.locale)
|
|
177
140
|
return e.params.locale.toString();
|
|
178
|
-
const
|
|
179
|
-
|
|
141
|
+
const r = this.extractLocaleFromPath(t);
|
|
142
|
+
if (r)
|
|
143
|
+
return r;
|
|
144
|
+
if (R(this.i18nConfig.strategy))
|
|
145
|
+
return (this.i18nConfig.defaultLocale || "en").toString();
|
|
146
|
+
const s = this.getDefaultLocale?.();
|
|
147
|
+
return s || (this.i18nConfig.defaultLocale || "en").toString();
|
|
180
148
|
}
|
|
181
149
|
getCurrentName(e) {
|
|
182
150
|
const t = this.getCurrentLocale(e);
|
|
183
|
-
return this.i18nConfig.locales?.find((
|
|
151
|
+
return this.i18nConfig.locales?.find((s) => s.code === t)?.displayName ?? null;
|
|
184
152
|
}
|
|
185
153
|
getRouteName(e, t) {
|
|
186
154
|
return (e.name ?? "").toString().toString().replace("localized-", "").replace(new RegExp(`-${t}$`), "");
|
|
@@ -189,36 +157,36 @@ class z {
|
|
|
189
157
|
return this.i18nConfig.disablePageLocales ? "general" : this.getRouteName(e, t);
|
|
190
158
|
}
|
|
191
159
|
getFullPathWithBaseUrl(e, t) {
|
|
192
|
-
let
|
|
193
|
-
e?.baseDefault && (
|
|
194
|
-
let
|
|
195
|
-
return
|
|
160
|
+
let s = this.router.resolve(t).fullPath;
|
|
161
|
+
e?.baseDefault && (s = s.replace(new RegExp(`^/${e.code}`), ""));
|
|
162
|
+
let n = e.baseUrl;
|
|
163
|
+
return n || (n = ""), n?.endsWith("/") && (n = n.slice(0, -1)), n + s;
|
|
196
164
|
}
|
|
197
|
-
switchLocaleRoute(e, t, r,
|
|
198
|
-
const
|
|
165
|
+
switchLocaleRoute(e, t, r, s) {
|
|
166
|
+
const n = this.i18nConfig.locales?.find((g) => g.code === t), i = this.getRouteName(r, e);
|
|
199
167
|
if (this.router.hasRoute(`localized-${i}-${t}`)) {
|
|
200
|
-
const
|
|
201
|
-
delete
|
|
202
|
-
const
|
|
168
|
+
const m = { ...s?.[t] ? { ...s[t] } : { ...r.params ?? {} } };
|
|
169
|
+
delete m.locale, c(this.i18nConfig.strategy) || (m.locale = t);
|
|
170
|
+
const p = {
|
|
203
171
|
name: `localized-${i}-${t}`,
|
|
204
|
-
params:
|
|
172
|
+
params: m,
|
|
205
173
|
query: r.query,
|
|
206
174
|
hash: r.hash
|
|
207
175
|
};
|
|
208
|
-
return
|
|
176
|
+
return n?.baseUrl ? this.getFullPathWithBaseUrl(n, p) : p;
|
|
209
177
|
}
|
|
210
178
|
let a = i;
|
|
211
|
-
const
|
|
212
|
-
delete
|
|
213
|
-
const
|
|
179
|
+
const u = { ...s?.[t] ? { ...s[t] } : { ...r.params ?? {} } };
|
|
180
|
+
delete u.locale, c(this.i18nConfig.strategy) || (i === "custom-fallback-route" ? a = i : a = t !== this.i18nConfig.defaultLocale || f(this.i18nConfig.strategy) ? `localized-${i}` : i, c(this.i18nConfig.strategy) || (t !== this.i18nConfig.defaultLocale || f(this.i18nConfig.strategy)) && (u.locale = t));
|
|
181
|
+
const h = {
|
|
214
182
|
name: a,
|
|
215
|
-
params:
|
|
183
|
+
params: u,
|
|
216
184
|
query: r.query,
|
|
217
185
|
hash: r.hash
|
|
218
186
|
};
|
|
219
|
-
return
|
|
220
|
-
|
|
221
|
-
}),
|
|
187
|
+
return c(this.i18nConfig.strategy) && this.i18nConfig.locales?.forEach((g, m) => {
|
|
188
|
+
h.name.endsWith(`-${g.code}`) && (h.name = h.name.slice(0, -g.code - 1));
|
|
189
|
+
}), n?.baseUrl ? this.getFullPathWithBaseUrl(n, h) : h;
|
|
222
190
|
}
|
|
223
191
|
resolveParams(e) {
|
|
224
192
|
const t = typeof e == "object" && "params" in e && typeof e.params == "object" ? { ...e.params } : {};
|
|
@@ -229,110 +197,106 @@ class z {
|
|
|
229
197
|
return t;
|
|
230
198
|
}
|
|
231
199
|
handlePrefixStrategy(e) {
|
|
232
|
-
if (!
|
|
200
|
+
if (!f(this.i18nConfig.strategy))
|
|
233
201
|
return e;
|
|
234
202
|
const t = this.i18nConfig.defaultLocale;
|
|
235
203
|
let r = e;
|
|
236
204
|
typeof e == "string" && (r = this.router.resolve("/" + t + e));
|
|
237
|
-
const
|
|
238
|
-
return
|
|
239
|
-
name: `localized-${
|
|
205
|
+
const s = this.getRouteName(r, t), n = this.resolveParams(r);
|
|
206
|
+
return c(this.i18nConfig.strategy) || (n.locale = t), this.router.hasRoute(`localized-${s}`) ? this.router.resolve({
|
|
207
|
+
name: `localized-${s}`,
|
|
240
208
|
query: r.query,
|
|
241
209
|
hash: r.hash,
|
|
242
|
-
params:
|
|
243
|
-
}) : this.router.hasRoute(`localized-${
|
|
244
|
-
name: `localized-${
|
|
210
|
+
params: n
|
|
211
|
+
}) : this.router.hasRoute(`localized-${s}-${t}`) ? this.router.resolve({
|
|
212
|
+
name: `localized-${s}-${t}`,
|
|
245
213
|
query: r.query,
|
|
246
214
|
hash: r.hash,
|
|
247
|
-
params:
|
|
215
|
+
params: n
|
|
248
216
|
}) : e;
|
|
249
217
|
}
|
|
250
218
|
createLocalizedRoute(e, t, r) {
|
|
251
|
-
const
|
|
252
|
-
if (!
|
|
253
|
-
let
|
|
254
|
-
return (r !== this.i18nConfig.defaultLocale ||
|
|
255
|
-
path:
|
|
256
|
-
query:
|
|
257
|
-
hash:
|
|
219
|
+
const s = this.router.resolve(e), n = this.getRouteName(s, r).replace(new RegExp(`-${this.i18nConfig.defaultLocale}$`), "");
|
|
220
|
+
if (!c(this.i18nConfig.strategy) && (!n || n === "")) {
|
|
221
|
+
let u = this.router.resolve(e).path.replace(new RegExp(`^/${r}/`), "/");
|
|
222
|
+
return (r !== this.i18nConfig.defaultLocale || f(this.i18nConfig.strategy)) && (u = "/" + r + u), this.router.resolve({
|
|
223
|
+
path: u,
|
|
224
|
+
query: s.query,
|
|
225
|
+
hash: s.hash
|
|
258
226
|
});
|
|
259
227
|
}
|
|
260
|
-
if (this.router.hasRoute(`localized-${
|
|
261
|
-
const l = this.resolveParams(
|
|
262
|
-
return
|
|
263
|
-
name: `localized-${
|
|
228
|
+
if (this.router.hasRoute(`localized-${n}-${r}`)) {
|
|
229
|
+
const l = this.resolveParams(s);
|
|
230
|
+
return c(this.i18nConfig.strategy) || (l.locale = r), this.router.resolve({
|
|
231
|
+
name: `localized-${n}-${r}`,
|
|
264
232
|
params: l,
|
|
265
|
-
query:
|
|
266
|
-
hash:
|
|
233
|
+
query: s.query,
|
|
234
|
+
hash: s.hash
|
|
267
235
|
});
|
|
268
236
|
}
|
|
269
|
-
const i = r !== this.i18nConfig.defaultLocale ||
|
|
237
|
+
const i = r !== this.i18nConfig.defaultLocale || f(this.i18nConfig.strategy) ? `localized-${n}` : n;
|
|
270
238
|
if (!this.router.hasRoute(i)) {
|
|
271
239
|
const l = this.resolveParams(e);
|
|
272
|
-
return delete l.locale, this.router.hasRoute(
|
|
273
|
-
name:
|
|
240
|
+
return delete l.locale, this.router.hasRoute(n) ? this.router.resolve({
|
|
241
|
+
name: n,
|
|
274
242
|
params: l,
|
|
275
|
-
query:
|
|
276
|
-
hash:
|
|
243
|
+
query: s.query,
|
|
244
|
+
hash: s.hash
|
|
277
245
|
}) : this.router.resolve("/");
|
|
278
246
|
}
|
|
279
247
|
const a = this.resolveParams(e);
|
|
280
|
-
return delete a.locale,
|
|
248
|
+
return delete a.locale, c(this.i18nConfig.strategy) || (r !== this.i18nConfig.defaultLocale || f(this.i18nConfig.strategy)) && (a.locale = r), this.router.resolve({
|
|
281
249
|
name: i,
|
|
282
250
|
params: a,
|
|
283
|
-
query:
|
|
284
|
-
hash:
|
|
251
|
+
query: s.query,
|
|
252
|
+
hash: s.hash
|
|
285
253
|
});
|
|
286
254
|
}
|
|
287
255
|
getLocalizedRoute(e, t, r) {
|
|
288
|
-
const
|
|
289
|
-
return this.createLocalizedRoute(
|
|
290
|
-
}
|
|
291
|
-
updateCookies(e) {
|
|
292
|
-
const t = this.cookieLocaleName || this.i18nConfig.localeCookie || "user-locale";
|
|
293
|
-
this.i18nConfig.hashMode && (this.setCookie("hash-locale", e), this.hashLocaleDefault = e), f(this.i18nConfig.strategy) && (this.setCookie(t, e), this.noPrefixDefault = e), !this.i18nConfig.hashMode && !f(this.i18nConfig.strategy) && t && (this.setCookie(t, e), this.cookieLocaleDefault = e);
|
|
256
|
+
const s = r || this.getCurrentLocale(t), n = this.handlePrefixStrategy(e);
|
|
257
|
+
return this.createLocalizedRoute(n, t, s);
|
|
294
258
|
}
|
|
295
259
|
getCurrentRoute() {
|
|
296
260
|
return this.router.currentRoute.value;
|
|
297
261
|
}
|
|
298
262
|
resolveRouteWithStrategy(e, t, r) {
|
|
299
|
-
return
|
|
263
|
+
return c(this.i18nConfig.strategy) ? this.router.resolve(e) : t !== this.i18nConfig.defaultLocale || f(this.i18nConfig.strategy) ? this.router.resolve(`/${r}${e}`) : this.router.resolve(e);
|
|
300
264
|
}
|
|
301
265
|
switchLocaleLogic(e, t, r) {
|
|
302
|
-
const
|
|
303
|
-
let
|
|
304
|
-
typeof r == "string" ?
|
|
305
|
-
const i = this.switchLocaleRoute(
|
|
306
|
-
return typeof i == "string" && i.startsWith("http") ? this.navigateTo(i, { redirectCode: 200, external: !0 }) : (
|
|
266
|
+
const s = this.getCurrentLocale();
|
|
267
|
+
let n;
|
|
268
|
+
typeof r == "string" ? n = this.resolveRouteWithStrategy(r, e, s) : n = r ?? this.getCurrentRoute();
|
|
269
|
+
const i = this.switchLocaleRoute(s, e, n, t);
|
|
270
|
+
return typeof i == "string" && i.startsWith("http") ? this.navigateTo(i, { redirectCode: 200, external: !0 }) : (c(this.i18nConfig.strategy) && (i.force = !0), this.router.push(i));
|
|
307
271
|
}
|
|
308
272
|
resolveLocalizedRoute(e, t) {
|
|
309
|
-
const r = this.getCurrentRoute(),
|
|
273
|
+
const r = this.getCurrentRoute(), s = this.getCurrentLocale(), n = t ?? s;
|
|
310
274
|
let i;
|
|
311
275
|
if (typeof e == "string")
|
|
312
276
|
if (e.startsWith("/"))
|
|
313
|
-
i = this.resolveRouteWithStrategy(e,
|
|
277
|
+
i = this.resolveRouteWithStrategy(e, n, s);
|
|
314
278
|
else {
|
|
315
279
|
const a = e;
|
|
316
|
-
this.router.hasRoute(a) ? i = this.router.resolve({ name: a }) : (e = `/${e}`, i = this.resolveRouteWithStrategy(e,
|
|
280
|
+
this.router.hasRoute(a) ? i = this.router.resolve({ name: a }) : (e = `/${e}`, i = this.resolveRouteWithStrategy(e, n, s));
|
|
317
281
|
}
|
|
318
282
|
else
|
|
319
283
|
i = e;
|
|
320
|
-
return this.getLocalizedRoute(i, r,
|
|
284
|
+
return this.getLocalizedRoute(i, r, n);
|
|
321
285
|
}
|
|
322
286
|
}
|
|
323
|
-
class
|
|
287
|
+
class v {
|
|
324
288
|
formatNumber(e, t, r) {
|
|
325
289
|
return new Intl.NumberFormat(t, r).format(e);
|
|
326
290
|
}
|
|
327
291
|
formatDate(e, t, r) {
|
|
328
|
-
const
|
|
329
|
-
return Number.isNaN(
|
|
292
|
+
const s = new Date(e);
|
|
293
|
+
return Number.isNaN(s.getTime()) ? "Invalid Date" : new Intl.DateTimeFormat(t, r).format(s);
|
|
330
294
|
}
|
|
331
295
|
formatRelativeTime(e, t, r) {
|
|
332
|
-
const
|
|
333
|
-
if (Number.isNaN(
|
|
296
|
+
const s = new Date(e);
|
|
297
|
+
if (Number.isNaN(s.getTime()))
|
|
334
298
|
return new Intl.RelativeTimeFormat(t, r).format(0, "second");
|
|
335
|
-
const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() -
|
|
299
|
+
const i = Math.floor(((/* @__PURE__ */ new Date()).getTime() - s.getTime()) / 1e3), a = [
|
|
336
300
|
{ unit: "year", seconds: 31536e3 },
|
|
337
301
|
{ unit: "month", seconds: 2592e3 },
|
|
338
302
|
{ unit: "day", seconds: 86400 },
|
|
@@ -340,62 +304,62 @@ class P {
|
|
|
340
304
|
{ unit: "minute", seconds: 60 },
|
|
341
305
|
{ unit: "second", seconds: 1 }
|
|
342
306
|
];
|
|
343
|
-
for (const { unit: l, seconds:
|
|
344
|
-
const
|
|
345
|
-
if (
|
|
346
|
-
return new Intl.RelativeTimeFormat(t, r).format(-
|
|
307
|
+
for (const { unit: l, seconds: u } of a) {
|
|
308
|
+
const h = Math.floor(i / u);
|
|
309
|
+
if (h >= 1)
|
|
310
|
+
return new Intl.RelativeTimeFormat(t, r).format(-h, l);
|
|
347
311
|
}
|
|
348
312
|
return new Intl.RelativeTimeFormat(t, r).format(0, "second");
|
|
349
313
|
}
|
|
350
314
|
}
|
|
351
|
-
class
|
|
315
|
+
class T {
|
|
352
316
|
constructor(e = {}) {
|
|
353
|
-
this.formatter = new
|
|
317
|
+
this.formatter = new v(), this.helper = C(e.storage), this.formatter = new v(), this.pluralFunc = e.plural || L, this.missingWarn = e.missingWarn ?? !0, this.missingHandler = e.missingHandler, this.getPreviousPageInfo = e.getPreviousPageInfo, this.getCustomMissingHandler = e.getCustomMissingHandler, this.enablePreviousPageFallback = e.enablePreviousPageFallback ?? !1;
|
|
354
318
|
}
|
|
355
319
|
// --- Public methods (implemented in base class) ---
|
|
356
320
|
/**
|
|
357
321
|
* Get translation for a key
|
|
358
322
|
* Based on logic from src/runtime/plugins/01.plugin.ts
|
|
359
323
|
*/
|
|
360
|
-
t(e, t, r,
|
|
324
|
+
t(e, t, r, s) {
|
|
361
325
|
if (!e) return "";
|
|
362
|
-
const
|
|
363
|
-
let a = this.helper.getTranslation(
|
|
326
|
+
const n = this.getLocale(), i = s || this.getRoute();
|
|
327
|
+
let a = this.helper.getTranslation(n, i, e);
|
|
364
328
|
if (!a && this.enablePreviousPageFallback && this.getPreviousPageInfo) {
|
|
365
329
|
const l = this.getPreviousPageInfo();
|
|
366
330
|
if (l) {
|
|
367
|
-
const
|
|
368
|
-
|
|
331
|
+
const u = this.helper.getTranslation(l.locale, l.routeName, e);
|
|
332
|
+
u && (a = u, process.env.NODE_ENV !== "production" && console.log(`Using fallback translation from previous route: ${l.routeName} -> ${e}`));
|
|
369
333
|
}
|
|
370
334
|
}
|
|
371
335
|
if (!a) {
|
|
372
336
|
const l = this.getFallbackLocale();
|
|
373
|
-
|
|
337
|
+
n !== l && (a = this.helper.getTranslation(l, i, e));
|
|
374
338
|
}
|
|
375
339
|
if (!a) {
|
|
376
340
|
const l = this.getCustomMissingHandler?.();
|
|
377
|
-
l ? l(
|
|
341
|
+
l ? l(n, e, i) : this.missingHandler ? this.missingHandler(n, e, i) : this.missingWarn && process.env.NODE_ENV !== "production" && typeof window < "u" && console.warn(`Not found '${e}' key in '${n}' locale messages for route '${i}'.`), a = r === void 0 ? e : r || e;
|
|
378
342
|
}
|
|
379
|
-
return typeof a == "string" && t ?
|
|
343
|
+
return typeof a == "string" && t ? P(a, t) : a;
|
|
380
344
|
}
|
|
381
345
|
/**
|
|
382
346
|
* Get translation as string
|
|
383
347
|
*/
|
|
384
|
-
ts(e, t, r,
|
|
385
|
-
return this.t(e, t, r,
|
|
348
|
+
ts(e, t, r, s) {
|
|
349
|
+
return this.t(e, t, r, s)?.toString() ?? r ?? e;
|
|
386
350
|
}
|
|
387
351
|
/**
|
|
388
352
|
* Plural translation
|
|
389
353
|
*/
|
|
390
354
|
tc(e, t, r) {
|
|
391
|
-
const { count:
|
|
392
|
-
if (
|
|
355
|
+
const { count: s, ...n } = typeof t == "number" ? { count: t } : t;
|
|
356
|
+
if (s === void 0)
|
|
393
357
|
return r ?? e;
|
|
394
|
-
const i = (l,
|
|
358
|
+
const i = (l, u, h) => this.t(l, u, h);
|
|
395
359
|
return this.pluralFunc(
|
|
396
360
|
e,
|
|
397
|
-
Number.parseInt(
|
|
398
|
-
|
|
361
|
+
Number.parseInt(s.toString()),
|
|
362
|
+
n,
|
|
399
363
|
this.getLocale(),
|
|
400
364
|
i
|
|
401
365
|
) ?? r ?? e;
|
|
@@ -423,8 +387,8 @@ class E {
|
|
|
423
387
|
* Based on logic from src/runtime/plugins/01.plugin.ts
|
|
424
388
|
*/
|
|
425
389
|
has(e, t) {
|
|
426
|
-
const r = t || this.getRoute(),
|
|
427
|
-
return !!this.helper.getTranslation(
|
|
390
|
+
const r = t || this.getRoute(), s = this.getLocale();
|
|
391
|
+
return !!this.helper.getTranslation(s, r, e);
|
|
428
392
|
}
|
|
429
393
|
/**
|
|
430
394
|
* Clear cache
|
|
@@ -438,26 +402,26 @@ class E {
|
|
|
438
402
|
* Subclasses can override addTranslations/addRouteTranslations to add reactivity
|
|
439
403
|
*/
|
|
440
404
|
loadTranslationsCore(e, t, r) {
|
|
441
|
-
r ? this.helper.mergeGlobalTranslation(e, t, !0) : this.helper.
|
|
405
|
+
r ? this.helper.mergeGlobalTranslation(e, t, !0) : this.helper.setTranslations(e, t);
|
|
442
406
|
}
|
|
443
407
|
/**
|
|
444
408
|
* Core route translation loading logic (without reactivity)
|
|
445
409
|
* Subclasses can override addRouteTranslations to add reactivity
|
|
446
410
|
*/
|
|
447
|
-
loadRouteTranslationsCore(e, t, r,
|
|
448
|
-
|
|
411
|
+
loadRouteTranslationsCore(e, t, r, s) {
|
|
412
|
+
s ? this.helper.mergeTranslation(e, t, r, !0) : this.helper.loadPageTranslations(e, t, r);
|
|
449
413
|
}
|
|
450
414
|
}
|
|
451
415
|
export {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
416
|
+
T as BaseI18n,
|
|
417
|
+
v as FormatService,
|
|
418
|
+
w as RouteService,
|
|
419
|
+
L as defaultPlural,
|
|
420
|
+
P as interpolate,
|
|
421
|
+
c as isNoPrefixStrategy,
|
|
422
|
+
$ as isPrefixAndDefaultStrategy,
|
|
423
|
+
R as isPrefixExceptDefaultStrategy,
|
|
424
|
+
b as isPrefixStrategy,
|
|
425
|
+
C as useTranslationHelper,
|
|
426
|
+
f as withPrefixStrategy
|
|
463
427
|
};
|