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