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