@i18n-micro/route-strategy 1.1.30 → 2.0.1

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.mjs CHANGED
@@ -1,615 +1,459 @@
1
- import { isInternalPath as e } from "@i18n-micro/utils/app-path";
2
- import t from "node:path";
3
- import { existsSync as n, mkdirSync as r, writeFileSync as i } from "node:fs";
4
- import { isPrefixAndDefaultStrategy as a, isPrefixStrategy as o } from "@i18n-micro/core";
5
- //#region src/utils/common.ts
6
- var s = (e) => e.map((e) => ({ ...e })), c = (e) => !(!e.redirect || e.file), l = (e, t, n, r = "localized-") => n ? `${r}${e}-${t}` : `${r}${e}`, u = (e, t) => e ?? (t ?? "").replace(/[^a-z0-9]/gi, "-").replace(/^-+|-+$/g, ""), d = (e, t, n) => n && e !== t.code, f = (e, t) => (typeof e == "string" ? e : e.code) === t.code;
7
- //#endregion
8
- //#region src/utils/locales.ts
9
- function p(e, t) {
10
- let n = e.reduce((e, t) => {
11
- let n = e.find((e) => e.code === t.code);
12
- return n ? Object.assign(n, t) : e.push(t), e;
13
- }, []).filter((e) => !e.disabled);
14
- return {
15
- locales: n,
16
- defaultLocale: n.find((e) => e.code === t) ?? { code: t }
17
- };
18
- }
19
- //#endregion
20
- //#region src/utils/path.ts
21
- function m(e) {
22
- return e.split("/").map((e) => e.startsWith("[...") && e.endsWith("]") ? `:${e.substring(4, e.length - 1)}(.*)*` : e.startsWith("[") && e.endsWith("]") ? `:${e.substring(1, e.length - 1)}` : e.replace(/:([^()/]+)\(\)/g, ":$1")).join("/");
23
- }
24
- var h = (e) => {
1
+ import { isInternalPath as e, trimTrailingSlashes as t } from "@i18n-micro/utils/app-path";
2
+ import n from "node:path";
3
+ import { existsSync as r, mkdirSync as i, writeFileSync as a } from "node:fs";
4
+ // src/paths.ts
5
+ function o(e) {
25
6
  if (!e) return "";
26
- let n = t.posix.normalize(e).replace(/\/+$/, "");
27
- return n === "." ? "" : n;
28
- }, g = (e) => e.startsWith("/") ? e.slice(1) : e;
29
- function _(...e) {
30
- return t.posix.join(...e);
7
+ let r = t(n.posix.normalize(e));
8
+ return r === "." ? "" : r;
31
9
  }
32
- function v(e) {
33
- if (e !== void 0) return e.startsWith("/") && e.endsWith("/") ? e?.slice(1, -1) : e;
10
+ var s = (e) => e.startsWith("/") ? e.slice(1) : e, c = (e) => o(`/${s(e)}`) || "/";
11
+ function l(e, t) {
12
+ return t.startsWith("/") ? c(t) : c(n.posix.join(e, t));
34
13
  }
35
- function y(e) {
36
- return e;
37
- }
38
- function b(e) {
39
- return !e || !/[\u0080-\uFFFF]/.test(e) ? e : e.split("/").map((e) => !e || e.startsWith(":") || !/[\u0080-\uFFFF]/.test(e) ? e : encodeURI(e)).join("/");
14
+ function u(e) {
15
+ return e.split("/").map((e) => e.replace(/^\[\.\.\.(\w+)\]$/, ":$1(.*)*").replace(/\[\[(\w+)\]\]/g, ":$1?").replace(/\[(\w+)\]/g, ":$1").replace(/:(\w+)\(\)/g, ":$1")).join("/");
40
16
  }
41
- function x(e) {
42
- let t = b(e);
43
- return t === e ? [] : [t];
17
+ var d = /* @__PURE__ */ new Map();
18
+ function f(e) {
19
+ let t = d.get(e);
20
+ return t === void 0 && d.set(e, t = s(o(u(e))) || "/"), t;
44
21
  }
45
- function S(e, n, r) {
46
- let i = v(r?.toString()) || (Array.isArray(e) ? e.join("|") : e), a = y(n);
47
- return h(t.posix.join("/", `:locale(${i})`, a));
22
+ function p(e, t) {
23
+ if (!t.startsWith("/")) return s(o(t));
24
+ let n = c(t), r = c(e);
25
+ if (r === "/") return s(n);
26
+ if (n === r) return "";
27
+ if (n.startsWith(`${r}/`)) return n.slice(r.length + 1);
28
+ let i = n.split("/").filter(Boolean), a = r.split("/").filter(Boolean), l = 0;
29
+ for (; l < i.length && i[l] === a[l];) l++;
30
+ return i.slice(l).join("/");
48
31
  }
49
- function C(e) {
50
- return h(y(e));
32
+ function m(e) {
33
+ let t = c(e).split("/").slice(1);
34
+ if (t[0]?.includes(":")) return null;
35
+ let n = t.findIndex((e) => e.endsWith("*") || e.endsWith("+") || e.endsWith("?"));
36
+ return `/${(n === -1 ? t : [...t.slice(0, n), "**"]).map((e) => e.replace(/^(:\w+)\(.*\)$/, "$1")).join("/")}`;
51
37
  }
52
- //#endregion
53
- //#region src/core/localized-paths.ts
54
- var w = (e, t) => e ?? (t ?? "").replace(/[^a-z0-9]/gi, "-").replace(/^-+|-+$/g, "");
55
- function T(e) {
56
- return g(e) || "/";
38
+ function h(e, t) {
39
+ if (!t.startsWith("/")) return !0;
40
+ let n = c(e), r = c(t);
41
+ return n === "/" || r === n || r.startsWith(`${n}/`);
57
42
  }
58
- function E(e, t, n, r = "") {
59
- let i = {};
60
- return e.forEach((e) => {
61
- let a = w(e.name, e.path), o = h(_(r, e.path ?? "")), s = T(o), c = m(o), l = g(c) || "/", u = t[s] || t[c] || t[l] || t[a];
62
- if (u) typeof u == "object" && (i[s] = u);
63
- else {
64
- let e = n[a];
65
- e && typeof e == "object" && !Array.isArray(e) && (i[s] = e);
66
- }
67
- if (e.children?.length) {
68
- let a = h(_(r, e.path ?? ""));
69
- Object.assign(i, E(e.children, t, n, a));
70
- }
71
- }), i;
43
+ var g = /[\u0080-￿ "<>`]/;
44
+ function _(e) {
45
+ if (!g.test(e)) return;
46
+ let t = e.split("/").map((e) => e.startsWith(":") || !g.test(e) ? e : encodeURI(e)).join("/");
47
+ return t === e ? void 0 : t;
72
48
  }
73
- //#endregion
74
- //#region src/core/localization-disabled.ts
75
- function D(e, t, n) {
76
- let r = m(t), i = T(t);
77
- return e[n] === !1 || e[r] === !1 || e[i] === !1 || e[g(r)] === !1;
49
+ // src/generate.ts
50
+ var v = (e) => Array.from(e.matchAll(/:(\w+)/g), (e) => e[1]).sort().join(","), y = (e) => !e.name && /(?:^|\/)component-stub(?:\.\w+)?$/.test(e.file ?? "");
51
+ function b(e) {
52
+ let t = f(e);
53
+ return t === "/" ? "index" : t.replace(/\/|:|\(.*?\)|[?*+]/g, (e) => e === "/" ? "-" : "");
78
54
  }
79
- //#endregion
80
- //#region src/locale-route-access.ts
81
- function O(e, t, n, r) {
82
- if (!e || Object.keys(e).length === 0) return !0;
83
- let i = m(n), a = g(i) || "/", o = a === "/" ? "/" : a, s = e[i] ?? e[n] ?? e[a] ?? e[o] ?? (a === "/" ? void 0 : e[`/${a}`]);
84
- if (!Array.isArray(s) || s.length === 0) return !0;
85
- let c = t.map((e) => e.code), l = s.filter((e) => c.includes(e));
86
- return l.length === 0 || l.includes(r);
55
+ function x(e, t, n = "", r = !0) {
56
+ return e.map((e) => {
57
+ let i = r ? c(e.path ?? "") : l(n, e.path ?? ""), a = e.children && x(e.children, t, i, !1), o = a ? {
58
+ ...e,
59
+ children: a
60
+ } : e;
61
+ if (e.name || !e.file || y(e) || e.children?.some((e) => !e.path)) return o;
62
+ let s = b(i);
63
+ return t.has(s) ? o : (t.add(s), {
64
+ ...o,
65
+ name: s
66
+ });
67
+ });
87
68
  }
88
- //#endregion
89
- //#region src/core/context.ts
90
- function k(e) {
91
- let t = {};
92
- if (!e) return t;
93
- for (let n in e) {
94
- let r = m(n), i = e[n];
95
- if (typeof i == "object") {
96
- let e = {};
97
- for (let t in i) {
98
- let n = i[t];
99
- n && (e[t] = m(n));
100
- }
101
- t[r] = e;
102
- } else t[r] = i;
69
+ function S(e, t) {
70
+ for (let n of e) {
71
+ let { name: e } = n;
72
+ e !== void 0 && n.meta?.i18n?.name !== e && n.meta?.i18n !== void 0 && t.has(e) && (n.name = C(e, t)), n.name && t.add(n.name), S(n.children ?? [], t);
103
73
  }
104
- return t;
105
74
  }
106
- var A = class {
107
- constructor(e) {
108
- this.locales = e.locales, this.defaultLocale = this.findLocaleByCode(e.locales, e.defaultLocaleCode) ?? { code: e.defaultLocaleCode }, this.strategy = e.strategy, this.globalLocaleRoutes = k(e.globalLocaleRoutes ?? {}), this.filesLocaleRoutes = e.filesLocaleRoutes ?? {}, this.routeLocales = e.routeLocales ?? {}, this.localizedPaths = E(e.pages, this.globalLocaleRoutes, this.filesLocaleRoutes), this.activeLocaleCodes = this.locales.filter((e) => e.code !== this.defaultLocale.code || a(this.strategy) || o(this.strategy)).map((e) => e.code), this.excludePatterns = e.excludePatterns, this.customRegex = e.customRegex, this.noPrefixRedirect = e.noPrefixRedirect ?? !1, this.localizedRouteNamePrefix = e.localizedRouteNamePrefix ?? "localized-", this.fallbackRedirectComponentPath = e.fallbackRedirectComponentPath, this.rawGlobalLocaleRoutes = e.rawGlobalLocaleRoutes;
109
- }
110
- findLocaleByCode(e, t) {
111
- return e.find((e) => e.code === t);
112
- }
113
- getAllowedLocales(e, t) {
114
- let n = this.routeLocales[e] ?? this.routeLocales[t];
115
- return n?.length ? n.filter((e) => this.locales.some((t) => t.code === e)) : this.locales.map((e) => e.code);
116
- }
117
- getCustomPath(e, t) {
118
- let n = T(e);
119
- return this.localizedPaths[n]?.[t];
120
- }
121
- getCustomPathsForPage(e, t) {
122
- let n = T(e), r = this.localizedPaths[n] ?? this.localizedPaths[t];
123
- if (!r) return;
124
- let i = {};
125
- for (let e in r) {
126
- let t = r[e];
127
- i[e] = t.startsWith("/") ? t : `/${t}`;
128
- }
129
- return i;
130
- }
131
- hasLocaleRestrictions(e, t) {
132
- return !!(this.routeLocales[e] ?? this.routeLocales[t]);
133
- }
134
- filterLocaleCodesWithoutCustomPaths(e) {
135
- let t = T(e);
136
- return this.activeLocaleCodes.filter((e) => !this.localizedPaths[t]?.[e]);
137
- }
138
- };
139
- //#endregion
140
- //#region src/core/builder.ts
141
- function j(e, n) {
142
- let r = (n ?? "").trim();
143
- return r.startsWith("/") ? h(r) : h(t.posix.join(e || "", r));
75
+ function C(e, t) {
76
+ let n = 2;
77
+ for (; t.has(`${e}-${n}`);) n++;
78
+ return `${e}-${n}`;
144
79
  }
145
- var M = (e) => e.replace(/^\/?:locale\([^)]*\)/, "") || "/";
146
- function N(e, t) {
147
- if (!t.startsWith("/")) return h(t).replace(/^\/+/, "");
148
- let n = h(`/${t.replace(/^\/+/, "")}`), r = h(`/${M(e).replace(/^\/+/, "")}`);
149
- if (r === "/" || r === "") return n.replace(/^\/+/, "");
150
- if (n === r) return "";
151
- if (n.startsWith(`${r}/`)) return n.slice(r.length + 1);
152
- let i = n.split("/").filter(Boolean), a = r.split("/").filter(Boolean), o = 0;
153
- for (; o < i.length && o < a.length && i[o] === a[o];) o++;
154
- return i.slice(o).join("/");
80
+ var w = (e) => e.flatMap((e) => [...e.name ? [e.name] : [], ...w(e.children ?? [])]);
81
+ function T(e) {
82
+ return e instanceof RegExp ? e.source : e.length > 1 && e[0] === "/" && e.endsWith("/") ? e.slice(1, -1) : e;
155
83
  }
156
- function P(e, t) {
157
- let { path: n, name: r, children: i, meta: a, alias: o } = t, s = {
158
- ...e,
159
- path: n
160
- };
161
- return r !== void 0 && (s.name = r), i !== void 0 && (s.children = i), a !== void 0 && (s.meta = {
162
- ...e.meta,
163
- ...a
164
- }), o === void 0 ? "alias" in t && (s.alias = void 0, s.meta = {
165
- ...s.meta,
166
- alias: void 0
167
- }) : (s.alias = o, s.meta = {
168
- ...s.meta,
169
- alias: o
170
- }), s;
84
+ function E(e, t) {
85
+ let n = RegExp(`^(?:${T(t)})$`);
86
+ return e.filter((e) => !n.test(e));
171
87
  }
172
- //#endregion
173
- //#region src/strategies/abstract.ts
174
- var F = class {
175
- processPage(t, n) {
176
- if (t.path === void 0) throw Error("page.path is required");
177
- if (c(t) || t.path && e(t.path, n.excludePatterns)) return [t];
178
- let r = t.path ?? "", i = u(t.name, t.path);
179
- return D(n.globalLocaleRoutes, r, i) ? [t] : this.generateVariants(t, n);
180
- }
181
- postProcess(e, t) {
182
- return e;
183
- }
184
- localizeChildren(e, t, n, r, i, a) {
185
- return e.flatMap((e) => this.localizeChild(e, t, n, r, i, a));
186
- }
187
- localizeChildrenAllLocales(e, t, n, r, i) {
188
- return e.flatMap((e) => this.localizeChildAllLocales(e, t, n, r, i));
189
- }
190
- localizeChildAllLocales(e, t, n, r, i, a = !0) {
191
- let o = j(n, e.path ?? ""), c = u(e.name, e.path);
192
- if (D(i.globalLocaleRoutes, o, c)) return [e];
193
- let l = j(t, e.path ?? ""), d = i.localizedPaths[T(o)];
194
- if (d && a) {
195
- let a = r.filter((e) => !d[e]), o = r.filter((e) => d[e]);
196
- return [...a.length ? this.localizeChildAllLocales({ ...e }, t, n, a, i, !1) : [], ...o.flatMap((r) => this.localizeChild(e, t, n, r, i, !0))];
88
+ function D(t, n, r = /* @__PURE__ */ new Map()) {
89
+ let { strategy: i, locales: a, defaultLocale: u, rules: d, namePrefix: m, hostDefaults: g = [] } = n, b = n.customRegex === void 0 ? void 0 : T(n.customRegex), C = (e, t) => o(`/:locale(${b ?? e.join("|")})/${s(t)}`), E = /* @__PURE__ */ new WeakMap();
90
+ function D(e, t, n) {
91
+ let r = E.get(e);
92
+ if (!r) {
93
+ let i = n ? c(e.path ?? "") : l(t, e.path ?? "");
94
+ r = {
95
+ original: i,
96
+ key: f(i),
97
+ rule: d.get(i, e.name),
98
+ allowed: d.allowedLocales(i, e.name),
99
+ noMeta: d.disableMeta(i, e.name)
100
+ }, E.set(e, r);
197
101
  }
198
- let f = g(h(e.path ?? "")), p = this.localizeChildrenAllLocales(s(e.children ?? []), l, o, r, i);
199
- return [P(e, {
200
- path: f,
201
- name: `${i.localizedRouteNamePrefix}${e.name ?? ""}`,
202
- children: p
203
- })];
204
- }
205
- localizeChild(e, t, n, r, i, a) {
206
- let o = j(n, e.path ?? ""), c = u(e.name, e.path);
207
- if (D(i.globalLocaleRoutes, o, c)) return [e];
208
- let l = i.getCustomPath(o, r), d = l ? N(t, l) : g(h(e.path ?? "")), f = j(t, d), p = this.localizeChildren(s(e.children ?? []), f, o, r, i, a), m = u(e.name, e.path);
209
- return [P(e, {
210
- path: d,
211
- name: `${i.localizedRouteNamePrefix}${m}-${r}`,
212
- children: p
213
- })];
102
+ return r;
214
103
  }
215
- declaredAliases(e) {
216
- let t = e.alias ?? e.meta?.alias;
217
- return typeof t == "string" ? [t] : Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
104
+ function O(e) {
105
+ let t = E.get(e)?.noMeta;
106
+ return t === void 0 ? e : {
107
+ ...e,
108
+ meta: {
109
+ ...e.meta,
110
+ i18nNoMeta: t
111
+ }
112
+ };
218
113
  }
219
- routeAliases(e, t, n, r) {
220
- let i = new Set(x(t));
221
- for (let t of this.declaredAliases(e)) {
222
- let e = n ? S(n, t, r.customRegex) : h(t);
223
- i.add(e);
224
- for (let t of x(e)) i.add(t);
114
+ let k = /* @__PURE__ */ new Set();
115
+ function A(e, t, n, r, i) {
116
+ k.has(`${e}\0${t}`) || (k.add(`${e}\0${t}`), console.warn(`[i18n] globalLocaleRoutes["${e}"].${t} = "${n}" is outside its parent "${r}": the page is served at "${i}".`));
117
+ }
118
+ function j(e, t, n, i, a, o) {
119
+ let s = e.path ?? "", { original: u, key: d, rule: f, allowed: m } = D(e, n, a);
120
+ if (f === !1) return o ? {
121
+ page: e,
122
+ path: s,
123
+ children: [],
124
+ raw: !0
125
+ } : null;
126
+ if (t && m && !m.includes(t)) return null;
127
+ let g = t && f ? f[t] : void 0, _ = g === void 0 ? s : a ? c(g) : p(i, g), v = a ? c(_) : l(i, _);
128
+ if (g !== void 0 && !a && !h(i, g) && A(d, t, g, i, v), t) {
129
+ let e = r.get(d);
130
+ e ? e[t] = v : r.set(d, { [t]: v });
225
131
  }
226
- return [...i];
227
- }
228
- localizeChildPathsKeepNames(e, t, n, r, i) {
229
- return e.map((e) => {
230
- let a = j(n, e.path ?? "");
231
- if (D(i.globalLocaleRoutes, a, u(e.name, e.path))) return e;
232
- let o = i.getCustomPath(a, r), s = o ? N(t, o) : e.path ?? "", c = e.children?.length ? this.localizeChildPathsKeepNames(e.children, j(t, s), a, r, i) : e.children;
233
- return o || c !== e.children ? P(e, {
234
- path: s,
235
- children: c
236
- }) : e;
237
- });
238
- }
239
- buildRoutePathForLocales(e, t, n, r, i, a, o) {
240
- if (r) {
241
- let t = e.includes(o);
242
- return a || !t ? S(e, n, i) : h(n);
132
+ let y = [];
133
+ for (let n of e.children ?? []) {
134
+ let e = j(n, t, u, v, !1, o);
135
+ e && y.push(e);
243
136
  }
244
- return S(e, t, i);
137
+ return {
138
+ page: e,
139
+ path: _,
140
+ children: y
141
+ };
245
142
  }
246
- }, I = class extends F {
247
- generateVariants(e, t) {
248
- let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [], d = this.routeAliases(e, h(n), null, t);
249
- d.length && d.join("\n") !== this.declaredAliases(e).join("\n") && (e.alias = d, e.meta = {
250
- ...e.meta,
251
- alias: d
252
- }), c.push(e);
253
- let f = /* @__PURE__ */ new Set();
254
- if (a) for (let s of i) {
255
- let i = a[s];
256
- if (!i) continue;
257
- let u = h(i), d = h(n);
258
- if (u === d) continue;
259
- f.add(s);
260
- let p = C(i), m = l(r, s, !0), g = this.localizeChildrenForNoPrefix(o, u, n, s, t);
261
- c.push(P(e, {
262
- path: p,
263
- name: m,
264
- children: g,
265
- alias: [],
266
- meta: { alias: [] }
267
- })), t.noPrefixRedirect && s === t.defaultLocale.code && u !== d && (e.redirect = u);
143
+ let M = (e) => e.raw ? "!" : `${e.path}(${e.children.map(M).join(",")})`;
144
+ function N(e, t, n, r, i, a, o = !0) {
145
+ if (e.raw) return O(e.page);
146
+ let { page: s } = e, l = a ? i ? C(t, e.path) : c(e.path) : e.path, u = {
147
+ ...s,
148
+ path: l
149
+ };
150
+ if (s.name) {
151
+ u.name = r ? `${m}${s.name}${n}` : s.name;
152
+ let e = E.get(s)?.noMeta;
153
+ u.meta = {
154
+ ...s.meta,
155
+ i18n: {
156
+ name: s.name,
157
+ locales: t,
158
+ ...e === void 0 ? {} : { noMeta: e }
159
+ }
160
+ };
268
161
  }
269
- for (let a of i) f.has(a) || this.hasChangedChildPath(o, n, a, t) && c.push(P(e, {
270
- path: C(n),
271
- name: l(r, a, !0),
272
- children: this.localizeChildrenForNoPrefix(o, h(n), n, a, t),
273
- alias: [],
274
- meta: { alias: [] }
275
- }));
276
- return c;
277
- }
278
- hasChangedChildPath(e, t, n, r) {
279
- return e.some((e) => {
280
- let i = j(t, e.path ?? ""), a = r.getCustomPath(i, n);
281
- return a && N(t, a) !== g(h(e.path ?? "")) ? !0 : this.hasChangedChildPath(e.children ?? [], i, n, r);
282
- });
283
- }
284
- localizeChildrenForNoPrefix(e, t, n, r, i) {
285
- return e.map((e) => {
286
- let a = j(n, e.path ?? ""), o = i.getCustomPath(a, r), c = o ? N(t, o) : g(h(e.path ?? "")), d = j(t, c), f = this.localizeChildrenForNoPrefix(s(e.children ?? []), d, a, r, i);
287
- return P(e, {
288
- path: c,
289
- name: l(u(e.name, e.path), r, !0),
290
- children: f
291
- });
292
- });
293
- }
294
- }, L = class extends F {
295
- generateVariants(e, t) {
296
- let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [];
297
- if (a) {
298
- let s = i.filter((e) => !a[e]);
299
- if (s.length) {
300
- let i = S(s, n, t.customRegex), a = this.routeAliases(e, i, s, t);
301
- c.push(P(e, {
302
- path: i,
303
- name: l(r, s[0], !1, t.localizedRouteNamePrefix),
304
- children: this.localizeChildrenAllLocales(o, n, n, s, t),
305
- alias: a.length ? a : void 0,
306
- meta: { alias: a.length ? a : [] }
307
- }));
308
- }
309
- for (let s of i) {
310
- let i = a[s];
311
- if (!i) continue;
312
- let u = S(s, h(i), t.customRegex), d = l(r, s, !!i, t.localizedRouteNamePrefix), f = i ?? n, p = this.localizeChildren(o, f, n, s, t, !0), m = this.routeAliases(e, u, [s], t);
313
- c.push(P(e, {
314
- path: u,
315
- name: d,
316
- children: p,
317
- alias: m.length ? m : void 0,
318
- meta: { alias: m.length ? m : [] }
319
- }));
162
+ if (s.children && (u.children = e.children.map((e) => N(e, t, n, r, i, !1))), a) {
163
+ let e = P(o ? s : { path: s.path }, l, i ? t : null);
164
+ u.alias = e, u.meta = {
165
+ ...u.meta,
166
+ alias: e
167
+ };
168
+ }
169
+ return u;
170
+ }
171
+ function P(e, t, n) {
172
+ let r = e.alias ?? e.meta?.alias, i = (typeof r == "string" ? [r] : Array.isArray(r) ? r : []).filter((e) => typeof e == "string"), a = /* @__PURE__ */ new Set();
173
+ for (let e of [t, ...i.map((e) => n ? C(n, e) : c(e))]) {
174
+ e !== t && a.add(e);
175
+ let n = _(e);
176
+ n && a.add(n);
177
+ }
178
+ return [...a];
179
+ }
180
+ function F(e, t, n, r) {
181
+ let i = M(j(e, null, "", "", !0, L)), a = [], o, s = /* @__PURE__ */ new Map();
182
+ for (let e of t) {
183
+ let t = r.get(e);
184
+ if (!t) continue;
185
+ let c = M(t);
186
+ if (c === i) a.push(e), o ??= t;
187
+ else {
188
+ let t = n ? e : c;
189
+ s.set(t, [...s.get(t) ?? [], e]);
320
190
  }
321
- } else {
322
- let a = S(i, n, t.customRegex), s = l(r, i[0], !1, t.localizedRouteNamePrefix), u = this.localizeChildrenAllLocales(o, n, n, i, t), d = this.routeAliases(e, a, i, t);
323
- c.push(P(e, {
324
- path: a,
325
- name: s,
326
- children: u,
327
- alias: d,
328
- meta: { alias: d }
329
- }));
330
191
  }
331
- return c;
192
+ let c = [...s.values()].map((e) => N(r.get(e[0]), e, `-${e[0]}`, !0, n, !0, n));
193
+ return {
194
+ plain: a,
195
+ records: o && n ? [N(o, a, "", !0, !0, !0), ...c] : c
196
+ };
332
197
  }
333
- postProcess(t, n) {
334
- let r = t.filter((t) => {
335
- if (t.name === "index" && t.path === "/") return !1;
336
- let r = t.path ?? "";
337
- return !!(r && e(r, n.excludePatterns) || D(n.globalLocaleRoutes, r, t.name ?? "") || /^\/:locale/.test(r) || r === "/");
338
- }), i = n.defaultLocale.code;
339
- for (let [e, t] of Object.entries(n.globalLocaleRoutes)) {
340
- if (t === !1 || typeof t != "object") continue;
341
- let n = t[i];
342
- if (!n) continue;
343
- let a = e.startsWith("/") ? e : `/${e}`, o = `/${i}${n.startsWith("/") ? n : `/${n}`}`;
344
- a !== o && r.push({
345
- path: a,
346
- name: `redirect-${e}`,
347
- redirect: o
198
+ function I(e) {
199
+ let t = /* @__PURE__ */ new Map();
200
+ for (let n of [u, ...g]) {
201
+ let r = e.get(n);
202
+ if (!r) continue;
203
+ let i = M(r);
204
+ t.set(i, [...t.get(i) ?? [], n]);
205
+ }
206
+ return [...t.values()];
207
+ }
208
+ let L = i === "prefix", R = [], z = [];
209
+ for (let r of x(t, new Set(w(t)))) {
210
+ let t = r.path ?? "";
211
+ if (r.redirect && !r.file || e(t, n.excludePatterns, n.apiBaseUrl) || D(r, "", !0).rule === !1) {
212
+ R.push(O(r));
213
+ continue;
214
+ }
215
+ if (y(r)) {
216
+ R.push({
217
+ ...r,
218
+ meta: {
219
+ ...r.meta,
220
+ i18nStub: !0
221
+ }
348
222
  });
223
+ continue;
349
224
  }
350
- return n.fallbackRedirectComponentPath && r.push({
351
- path: "/:pathMatch(.*)*",
352
- name: "custom-fallback-route",
353
- file: n.fallbackRedirectComponentPath,
354
- meta: { globalLocaleRoutes: n.rawGlobalLocaleRoutes ?? n.globalLocaleRoutes }
355
- }), r;
356
- }
357
- }, R = class extends F {
358
- generateVariants(e, t) {
359
- let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [], l = t.defaultLocale.code;
360
- if (i.includes(l)) {
361
- let r = a?.[l], i = r ? h(r) : n, s = o.length ? this.localizeChildPathsKeepNames(o, i, n, l, t) : void 0, u = this.routeAliases(e, i, null, t), d = u.length ? {
362
- alias: u,
363
- meta: { alias: u }
364
- } : {}, f = u.join("\n") !== this.declaredAliases(e).join("\n");
365
- c.push(r || s || f ? P(e, {
366
- path: i,
367
- children: s,
368
- ...d
369
- }) : e);
370
- } else c.push(e);
371
- let d = t.locales.filter((e) => i.includes(e.code));
372
- if (d.length > 0) {
373
- if (a) {
374
- for (let r of d) {
375
- let i = a[r.code];
376
- if (i) {
377
- if (r.code === l) {
378
- let a = this.createLocalizedRoute(e, [r.code], o, !0, i, t.customRegex, !0, r.code, n, t);
379
- a && c.push(a);
380
- } else {
381
- let a = this.createLocalizedRoute(e, [r.code], o, !0, i, t.customRegex, !1, r.code, n, t);
382
- a && c.push(a);
383
- }
225
+ let o = D(r, "", !0).allowed ?? a, s = o.includes(u), l = new Map(o.map((e) => [e, j(r, e, "", "", !0, L)]));
226
+ if (i === "no_prefix") {
227
+ let { plain: e, records: t } = F(r, o, !1, l), i = N(j(r, null, "", "", !0, !0), e, "", !1, !1, !0), a = t.find((e) => (e.meta?.i18n)?.locales.includes(u));
228
+ if (n.noPrefixRedirect && a && a.path !== i.path && !a.path.includes(":")) {
229
+ let e = t.flatMap((e) => e.path.includes(":") ? [] : e.meta.i18n.locales.map((t) => [t, e.path]));
230
+ i.meta = {
231
+ ...i.meta,
232
+ i18n: {
233
+ ...i.meta.i18n,
234
+ paths: Object.fromEntries(e)
384
235
  }
236
+ };
237
+ }
238
+ R.push(i), z.push(...t);
239
+ continue;
240
+ }
241
+ if (i !== "prefix") for (let e of I(l)) {
242
+ let n = e[0] === u, a = N(j(r, e[0], "", "", !0, !0), e, n ? "" : `-${e[0]}-default`, !n, !1, !0, n), o = c(t);
243
+ n && i === "prefix_except_default" && a.path !== o && v(a.path) === v(o) && (a.alias = [
244
+ ...a.alias ?? [],
245
+ o,
246
+ ...[_(o) ?? []].flat()
247
+ ], a.meta = {
248
+ ...a.meta,
249
+ alias: a.alias,
250
+ i18n: {
251
+ ...a.meta.i18n,
252
+ original: o
385
253
  }
386
- let r = d.filter((e) => !a[e.code]).map((e) => e.code);
387
- if (r.length) {
388
- let i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, r.length !== 1 || r[0], n, t);
389
- i && c.push(i);
254
+ }), R.push(a);
255
+ }
256
+ let d = F(r, i === "prefix_except_default" && !g.length ? o.filter((e) => e !== u) : o, !0, l).records;
257
+ if (z.push(...d), (i === "prefix" || i === "prefix_and_default") && s && r.name && !t.includes(":")) {
258
+ let e = c(l.get(u).path);
259
+ if (e !== c(t) && !e.includes(":")) {
260
+ if (i === "prefix" && n.redirects !== !1) R.push({
261
+ path: c(t),
262
+ name: `redirect-${r.name}`,
263
+ redirect: `/${u}${e === "/" ? "" : e}`,
264
+ meta: { i18n: {
265
+ name: r.name,
266
+ locales: []
267
+ } }
268
+ });
269
+ else {
270
+ let e = d.find((e) => e.meta.i18n.locales.includes(u));
271
+ e.meta = {
272
+ ...e.meta,
273
+ i18n: {
274
+ ...e.meta.i18n,
275
+ original: c(t)
276
+ }
277
+ };
390
278
  }
391
- } else {
392
- let r = d.map((e) => e.code), i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, !0, n, t);
393
- i && c.push(i);
394
279
  }
395
280
  }
396
- return c;
397
281
  }
398
- postProcess(e, t) {
399
- let n = [], r = [];
400
- for (let i of e) {
401
- let e = i.name ?? "";
402
- typeof e == "string" && e.startsWith(t.localizedRouteNamePrefix) ? r.push(i) : n.push(i);
403
- }
404
- return [...n, ...r];
282
+ let B = [...R, ...z];
283
+ return S(B, /* @__PURE__ */ new Set()), B;
284
+ }
285
+ // ../utils/dist/domains.mjs
286
+ function O(e) {
287
+ let t = e.trim().toLowerCase();
288
+ return t.endsWith(":80") ? t.slice(0, -3) : t.endsWith(":443") ? t.slice(0, -4) : t;
289
+ }
290
+ function k(e) {
291
+ if (!e.baseUrl) return null;
292
+ try {
293
+ return O(new URL(e.baseUrl).host);
294
+ } catch {
295
+ return null;
405
296
  }
406
- createLocalizedRoute(e, t, n, r, i = "", a, o = !1, s = !1, c, d) {
407
- let f = this.buildRoutePathForLocales(t, e.path ?? "", i, r, a, o, d.defaultLocale.code);
408
- if (!f || !(r && i) && f === e.path || t.length === 0) return null;
409
- let p = t[0];
410
- if (!p) return null;
411
- let m = c ?? e.path ?? "", h = l(u(e.name ?? "", m), p, r, d.localizedRouteNamePrefix), g = o || p !== d.defaultLocale.code, _ = t.length === 1 ? this.localizeChildren(n, f, m, p, d, g) : this.localizeChildrenAllLocales(n, f, m, t, d), v = this.routeAliases(e, f, t, d);
412
- return P(e, {
413
- path: f,
414
- name: h,
415
- children: _,
416
- alias: v,
417
- meta: { alias: v }
418
- });
297
+ }
298
+ function A(e) {
299
+ let t = /* @__PURE__ */ new Map();
300
+ for (let n of e) {
301
+ let e = k(n);
302
+ e && t.set(e, [...t.get(e) ?? [], n]);
419
303
  }
420
- }, z = class extends F {
421
- generateVariants(e, t) {
422
- let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [], l = t.defaultLocale.code;
423
- if (i.includes(l)) {
424
- let r = a?.[l], i = r ? h(r) : n, s = r ? { [l]: r } : {}, u = this.createLocalizedChildren(o, n, [l], !1, !1, !1, s, t), d = this.routeAliases(e, i, null, t);
425
- c.push(P(e, {
426
- path: i,
427
- name: e.name,
428
- children: u,
429
- ...d.length ? {
430
- alias: d,
431
- meta: { alias: d }
432
- } : {}
433
- }));
434
- }
435
- let d = t.locales.filter((e) => i.includes(e.code) && e.code !== l);
436
- if (d.length) {
437
- if (a) {
438
- for (let r of d) {
439
- let i = a[r.code];
440
- if (!i) continue;
441
- let s = this.createLocalizedRoute(e, [r.code], o, !0, i, t.customRegex, !1, r.code, n, t);
442
- s && c.push(s);
443
- }
444
- let r = d.filter((e) => !a[e.code]).map((e) => e.code);
445
- if (r.length) {
446
- let i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, r.length !== 1 || r[0], n, t);
447
- i && c.push(i);
448
- }
449
- } else {
450
- let r = d.map((e) => e.code), i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, !0, n, t);
451
- i && c.push(i);
304
+ return t;
305
+ }
306
+ var j = (e, t) => t.filter((t) => {
307
+ let n = k(t);
308
+ return !n || n === e || t.disabled;
309
+ });
310
+ function M(e, t, n) {
311
+ let r = e.filter((e) => !e.disabled), i = r.find((e) => e.baseDefault);
312
+ return i ? i.code : t.some((e) => e.code === n && !e.disabled) ? n : (r[0] ?? t.find((e) => !e.disabled))?.code;
313
+ }
314
+ function N(e, t) {
315
+ let n = /* @__PURE__ */ new Set();
316
+ for (let [r, i] of A(e)) {
317
+ let a = M(i, j(r, e), t);
318
+ i.some((e) => e.code === a) && n.add(a);
319
+ }
320
+ return e.map((e) => n.has(e.code) && !e.baseDefault ? Object.assign({}, e, { baseDefault: !0 }) : e);
321
+ }
322
+ // src/rules.ts
323
+ var P = class {
324
+ constructor(e = {}, t = {}, n = [], r = {}) {
325
+ this.rules = /* @__PURE__ */ new Map(), this.byName = /* @__PURE__ */ new Map(), this.restrictions = /* @__PURE__ */ new Map(), this.restrictionsByName = /* @__PURE__ */ new Map(), this.noMeta = /* @__PURE__ */ new Map(), this.noMetaByName = /* @__PURE__ */ new Map();
326
+ let i = new Set(n.map((e) => e.code)), a = F(this.rules, this.byName), s = F(this.restrictions, this.restrictionsByName), l = F(this.noMeta, this.noMetaByName);
327
+ for (let [e, t] of Object.entries(r)) l(e, t);
328
+ for (let [t, n] of Object.entries(e ?? {})) {
329
+ if (n === !1) {
330
+ a(t, !1);
331
+ continue;
332
+ }
333
+ if (!n || typeof n != "object") continue;
334
+ let e = {};
335
+ for (let [r, i] of Object.entries(n)) {
336
+ if (!i) continue;
337
+ let n = o(u(i)), a = c(n);
338
+ (a === `/${r}` || a.startsWith(`/${r}/`)) && console.warn(`[i18n] globalLocaleRoutes["${t}"].${r} = "${i}" starts with the locale code; the locale prefix is added automatically.`), e[r] = n;
452
339
  }
340
+ a(t, e);
453
341
  }
454
- return c;
455
- }
456
- postProcess(e, t) {
457
- let n = [], r = [];
458
- for (let i of e) {
459
- let e = i.name ?? "";
460
- typeof e == "string" && e.startsWith(t.localizedRouteNamePrefix) ? r.push(i) : n.push(i);
342
+ for (let [e, n] of Object.entries(t)) {
343
+ let t = n.filter((e) => i.has(e));
344
+ t.length && s(e, t);
461
345
  }
462
- let i = [...n, ...r];
463
- return t.fallbackRedirectComponentPath && i.push({
464
- path: "/:pathMatch(.*)*",
465
- name: "custom-fallback-route",
466
- file: t.fallbackRedirectComponentPath,
467
- meta: { globalLocaleRoutes: t.rawGlobalLocaleRoutes ?? t.globalLocaleRoutes }
468
- }), i;
469
346
  }
470
- createLocalizedChildren(e, t, n, r = !0, i = !1, a = !1, o = {}, s) {
471
- return e.flatMap((e) => this.createLocalizedVariants(e, t, n, r, i, a, o, s));
347
+ get(e, t) {
348
+ return this.rules.get(f(e)) ?? (t ? this.byName.get(t) : void 0);
472
349
  }
473
- createLocalizedVariants(e, t, n, r, i, a = !1, o, c) {
474
- let l = h(e.path ?? ""), f = T(j(t, e.path ?? "")), p = c.localizedPaths[f], m = !!p, _ = [];
475
- if (!m) {
476
- let u = g(l), d = {};
477
- for (let e in o) d[e] = j(o[e], u);
478
- let f = this.createLocalizedChildren(s(e.children ?? []), j(t, e.path ?? ""), n, r, i, a, d, c), p = this.buildChildRouteName(e.name, a, c);
479
- return _.push(P(e, {
480
- path: u,
481
- name: p,
482
- children: f
483
- })), _;
484
- }
485
- for (let a of n) {
486
- let n = o?.[a] || t, l = p?.[a], f = l ? N(n, l) : h(e.path ?? ""), m = g(d(a, c.defaultLocale, i) ? S(a, f, c.customRegex) : f), v = j(n, g(f)), y = this.createLocalizedChildren(s(e.children ?? []), j(t, e.path ?? ""), [a], r, i, a, {
487
- ...o,
488
- [a]: v
489
- }, c), b = this.buildLocalizedRouteName(u(e.name, e.path), a, r, !!p, c);
490
- _.push(P(e, {
491
- path: m,
492
- name: b,
493
- children: y
494
- }));
495
- }
496
- return _;
350
+ allowedLocales(e, t) {
351
+ return this.restrictions.get(f(e)) ?? (t ? this.restrictionsByName.get(t) : void 0);
497
352
  }
498
- buildChildRouteName(e, t, n) {
499
- return t === !0 ? `${n.localizedRouteNamePrefix}${e}` : typeof t == "string" ? `${n.localizedRouteNamePrefix}${e}-${t}` : e;
353
+ disableMeta(e, t) {
354
+ return this.noMeta.get(f(e)) ?? (t ? this.noMetaByName.get(t) : void 0);
500
355
  }
501
- createLocalizedRoute(e, t, n, r, i = "", a, o = !1, s = !1, c, d) {
502
- let f = this.buildRoutePathForLocales(t, e.path ?? "", i, r, a, o, d.defaultLocale.code);
503
- if (!f || f === e.path || t.length === 0) return null;
504
- let p = t[0];
505
- if (!p) return null;
506
- let m = c ?? e.path ?? "", h = l(u(e.name ?? "", m), p, r, d.localizedRouteNamePrefix), g = this.routeAliases(e, f, t, d);
507
- return P(e, {
508
- path: f,
509
- name: h,
510
- children: this.createLocalizedChildren(n, m, t, !0, !1, s, { [p]: i }, d),
511
- alias: g,
512
- meta: { alias: g }
513
- });
356
+ customKeysUnder(e, t) {
357
+ let n = [];
358
+ for (let [r, i] of this.rules) (r === e || r.startsWith(`${e}/`)) && i && i[t] && n.push(r);
359
+ return n;
514
360
  }
515
- buildLocalizedRouteName(e, t, n, r = !1, i) {
516
- return n ? r || t && !f(t, i.defaultLocale) ? `${i.localizedRouteNamePrefix}${e}-${t}` : `${i.localizedRouteNamePrefix}${e}` : e;
361
+ customPath(e, t) {
362
+ let n = this.rules.get(f(e));
363
+ return n ? n[t] : void 0;
517
364
  }
518
365
  };
519
- //#endregion
520
- //#region src/strategies/factory.ts
521
- function B(e) {
522
- switch (e) {
523
- case "no_prefix": return new I();
524
- case "prefix": return new L();
525
- case "prefix_and_default": return new R();
526
- case "prefix_except_default": return new z();
527
- default: throw Error(`Unknown route strategy: ${e}`);
528
- }
366
+ function F(e, t) {
367
+ let n = /* @__PURE__ */ new Set();
368
+ return (r, i) => {
369
+ let a = r.startsWith("/");
370
+ a || t.set(r, i);
371
+ let o = f(r);
372
+ a && e.has(o) && !n.has(o) || (e.set(o, i), a ? n.add(o) : n.delete(o));
373
+ };
529
374
  }
530
- //#endregion
531
- //#region src/route-generator.ts
532
- var V = class {
375
+ function I(e, t, n, r) {
376
+ let i = new P({}, e, t).allowedLocales(n, void 0);
377
+ return !i || i.includes(r);
378
+ }
379
+ // src/route-generator.ts
380
+ function L(e, t) {
381
+ let n = /* @__PURE__ */ new Map();
382
+ for (let t of e) n.set(t.code, {
383
+ ...t,
384
+ ...n.get(t.code)
385
+ });
386
+ let r = [...n.values()], i = r.filter((e) => !e.disabled);
387
+ return {
388
+ locales: i,
389
+ disabledLocales: r.filter((e) => e.disabled),
390
+ defaultLocale: i.find((e) => e.code === t) ?? { code: t }
391
+ };
392
+ }
393
+ var R = class {
533
394
  constructor(e) {
534
- this.localizedPaths = {};
535
- let { locales: t, defaultLocaleCode: n, strategy: r, globalLocaleRoutes: i, filesLocaleRoutes: a = {}, routeLocales: o = {}, noPrefixRedirect: s, excludePatterns: c, localizedRouteNamePrefix: l = "localized-", customRegexMatcher: u, fallbackRedirectComponentPath: d } = e, f = p(t, n);
536
- this.locales = f.locales, this.defaultLocale = f.defaultLocale, this.strategy = r, this.noPrefixRedirect = s, this.excludePatterns = c, this.localizedRouteNamePrefix = l, this.customRegex = u, this.fallbackRedirectComponentPath = d, this.activeLocaleCodes = this.computeActiveLocaleCodes();
537
- let h = {};
538
- for (let e in i) {
539
- let t = m(e), n = i[e];
540
- if (typeof n == "object") {
541
- let e = {};
542
- for (let t in n) {
543
- let r = n[t];
544
- r && (e[t] = m(r));
545
- }
546
- h[t] = e;
547
- } else h[t] = n;
548
- }
549
- this.globalLocaleRoutes = h, this.rawGlobalLocaleRoutes = i ?? {}, this.filesLocaleRoutes = a ?? {}, this.routeLocales = o ?? {};
550
- }
551
- computeActiveLocaleCodes() {
552
- return this.locales.filter((e) => e.code !== this.defaultLocale.code || a(this.strategy) || o(this.strategy)).map((e) => e.code);
395
+ this.localizedPaths = /* @__PURE__ */ new Map();
396
+ let t = L(e.locales, e.defaultLocaleCode);
397
+ this.locales = N(t.locales, e.defaultLocaleCode), this.disabledLocales = t.disabledLocales, this.defaultLocale = t.defaultLocale, this.strategy = e.strategy, this.routeLocales = e.routeLocales ?? {}, this.rules = new P(e.globalLocaleRoutes, this.routeLocales, this.locales, e.routeDisableMeta), this.options = e;
398
+ let n = e.strategy === "prefix_except_default" || e.strategy === "prefix_and_default";
399
+ this.hostDefaults = n ? this.locales.filter((e) => e.baseDefault && e.code !== this.defaultLocale.code).map((e) => e.code) : [], this.parentKeys = (e.parentPaths ?? []).map(f);
553
400
  }
554
401
  extendPages(e) {
555
- let t = new A({
556
- locales: this.locales,
557
- defaultLocaleCode: this.defaultLocale.code,
402
+ this.localizedPaths = /* @__PURE__ */ new Map();
403
+ let t = D(e, {
558
404
  strategy: this.strategy,
559
- globalLocaleRoutes: this.globalLocaleRoutes,
560
- filesLocaleRoutes: this.filesLocaleRoutes,
561
- routeLocales: this.routeLocales,
562
- pages: e,
563
- excludePatterns: this.excludePatterns,
564
- customRegex: this.customRegex,
565
- noPrefixRedirect: this.noPrefixRedirect,
566
- localizedRouteNamePrefix: this.localizedRouteNamePrefix,
567
- fallbackRedirectComponentPath: this.fallbackRedirectComponentPath,
568
- rawGlobalLocaleRoutes: this.rawGlobalLocaleRoutes
569
- });
570
- this.localizedPaths = t.localizedPaths;
571
- let n = B(this.strategy), r = [...e], i = [];
572
- for (let e of r) i.push(...n.processPage(e, t));
573
- let a = n.postProcess(i, t);
574
- e.length = 0, e.push(...a);
575
- }
576
- extractLocalizedPaths(e, t = "") {
577
- return E(e, this.globalLocaleRoutes, this.filesLocaleRoutes, t);
578
- }
579
- ensureTranslationFilesExist(e, n, r, i) {
580
- this.locales.forEach((a) => {
581
- let o = t.join(r, n, `${a.code}.json`);
582
- this.ensureFileExists(o), i || e.forEach((e) => {
583
- let i = t.join(r, n, "pages", `${e}/${a.code}.json`);
584
- this.ensureFileExists(i);
585
- });
586
- });
587
- }
588
- ensureFileExists(e) {
589
- try {
590
- let a = t.dirname(e);
591
- n(a) || r(a, { recursive: !0 }), n(e) || i(e, JSON.stringify({}), "utf-8");
592
- } catch {}
405
+ locales: this.locales.map((e) => e.code),
406
+ defaultLocale: this.defaultLocale.code,
407
+ rules: this.rules,
408
+ namePrefix: this.options.localizedRouteNamePrefix ?? "localized-",
409
+ customRegex: this.options.customRegexMatcher,
410
+ excludePatterns: this.options.excludePatterns,
411
+ apiBaseUrl: this.options.apiBaseUrl,
412
+ noPrefixRedirect: this.options.noPrefixRedirect,
413
+ redirects: this.options.redirects,
414
+ hostDefaults: this.hostDefaults
415
+ }, this.localizedPaths);
416
+ e.splice(0, e.length, ...t);
593
417
  }
594
418
  resolveLocalizedPath(e, t) {
595
- let n = m(e), r = g(n) || "/", i, a = this.globalLocaleRoutes[r], o = this.globalLocaleRoutes[n];
596
- a && typeof a == "object" && !Array.isArray(a) ? i = a[t] : o && typeof o == "object" && !Array.isArray(o) && (i = o[t]);
597
- let s = !!i, c = i ? h(i) : h(n) || "", l = !1;
598
- if (this.strategy === "no_prefix" ? l = !1 : this.strategy === "prefix" || this.strategy === "prefix_and_default" ? l = !0 : this.strategy === "prefix_except_default" && (l = t !== this.defaultLocale.code), t === this.defaultLocale.code && !s && (this.strategy === "prefix_except_default" || this.strategy === "no_prefix") && (l = !1), !l) return h(c) || "/";
599
- let u = g(c);
600
- return u ? h(`/${t}/${u}`) : `/${t}`;
419
+ let n = f(e);
420
+ return this.withPrefix(this.localizedPaths.get(n)?.[t] ?? this.rules.customPath(e, t) ?? e, t);
421
+ }
422
+ withPrefix(e, t) {
423
+ if (!(this.strategy === "prefix" || this.strategy === "prefix_and_default" || this.strategy === "prefix_except_default" && t !== this.defaultLocale.code)) return c(e);
424
+ let n = s(c(e));
425
+ return n ? `/${t}/${n}` : `/${t}`;
426
+ }
427
+ localizedKey(e, t) {
428
+ let n = this.rules.customPath(e, t), r = this.parentKeys.filter((t) => e.startsWith(`${t}/`)).sort((e, t) => t.length - e.length)[0];
429
+ if (!r) return c(n ?? e);
430
+ let i = this.localizedKey(r, t);
431
+ return l(i, n === void 0 ? e.slice(r.length + 1) : p(i, n));
432
+ }
433
+ resolveLocalizedRulePaths(e, t) {
434
+ let n = /* @__PURE__ */ new Set([this.resolveLocalizedPath(e, t)]);
435
+ if (e.endsWith("/**")) {
436
+ let r = f(e.slice(0, -3));
437
+ for (let e of this.rules.customKeysUnder(r, t)) {
438
+ let i = m(this.localizedKey(e, t));
439
+ i && n.add(this.withPrefix(e === r ? `${i}/**` : i, t));
440
+ }
441
+ }
442
+ if (this.hostDefaults.includes(t)) for (let e of [...n]) n.add(`/${e.split("/").slice(2).join("/")}`);
443
+ return [...n];
601
444
  }
602
- generateDataRoutes(e, t, n) {
603
- let r = [], i = this.collectPageNames(e);
604
- for (let e of this.locales) if (r.push(`/${t}/index/${e.code}/data.json`), !n) for (let n of i) r.push(`/${t}/${n}/${e.code}/data.json`);
605
- return r;
445
+ ensureTranslationFilesExist(e, t, r, i) {
446
+ for (let a of this.locales) if (z(n.join(r, t, `${a.code}.json`)), !i) for (let i of e) z(n.join(r, t, "pages", `${i}/${a.code}.json`));
606
447
  }
607
- collectPageNames(e) {
608
- let t = /* @__PURE__ */ new Set(), n = (e) => {
609
- for (let r of e) typeof r.name == "string" && r.name.length > 0 && t.add(r.name), r.children && r.children.length > 0 && n(r.children);
610
- };
611
- return n(e), [...t];
448
+ generateDataRoutes(e, t, n) {
449
+ let r = new Set(w(e)), i = [];
450
+ for (let e of this.locales) if (i.push(`/${t}/index/${e.code}/data.json`), !n) for (let n of r) i.push(`/${t}/${n}/${e.code}/data.json`);
451
+ return i;
612
452
  }
613
453
  };
614
- //#endregion
615
- export { V as RouteGenerator, x as buildEncodedPathAliases, S as buildFullPath, C as buildFullPathNoPrefix, l as buildRouteName, s as cloneArray, E as extractLocalizedPaths, e as isInternalPath, O as isLocaleAllowedForUnlocalizedRoute, f as isLocaleDefault, D as isLocalizationDisabledForPage, c as isPageRedirectOnly, h as normalizePath, m as normalizeRouteKey, g as removeLeadingSlash, d as shouldAddLocalePrefix };
454
+ function z(e) {
455
+ try {
456
+ i(n.dirname(e), { recursive: !0 }), r(e) || a(e, "{}", "utf-8");
457
+ } catch {}
458
+ }
459
+ export { R as RouteGenerator, P as Rules, D as generateRoutes, I as isLocaleAllowedForUnlocalizedRoute, T as localeMatcherSource, E as localesOutsideMatcher, b as nameOfPath, o as normalizePath, u as normalizeRouteKey, w as pageNames, f as routeKey };