@i18n-micro/astro 1.3.12 → 1.3.18

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,578 +1,525 @@
1
- import { BaseI18n as U } from "@i18n-micro/core";
2
- import { FormatService as be, defaultPlural as ye, interpolate as we } from "@i18n-micro/core";
3
- import { AsyncLocalStorage as q } from "node:async_hooks";
4
- import { existsSync as _, readdirSync as z, statSync as E, readFileSync as H } from "node:fs";
5
- import { resolve as x, join as V, relative as G, sep as J } from "node:path";
6
- import { mergeRouteTranslationsWithRoot as Z, storeLoadedTranslationFile as K } from "@i18n-micro/utils/parse-path";
7
- import { detectLocaleFromAcceptLanguage as Q } from "@i18n-micro/utils/accept-language";
8
- class v extends U {
9
- constructor(e) {
10
- const r = e._storage || {
11
- translations: /* @__PURE__ */ new Map()
12
- };
13
- if (super({
14
- storage: r,
15
- plural: e.plural,
16
- missingWarn: e.missingWarn,
17
- missingHandler: e.missingHandler,
18
- numberFormats: e.numberFormats,
19
- datetimeFormats: e.datetimeFormats
20
- }), this.initialMessages = {}, this.storage = r, this._locale = e.locale, this._fallbackLocale = e.fallbackLocale || e.locale, this._currentRoute = "index", e.messages) {
21
- this.initialMessages = { ...e.messages };
22
- for (const [l, c] of Object.entries(e.messages))
23
- this.helper.loadTranslations(l, c);
24
- }
25
- }
26
- cloneStorage(e) {
27
- const r = /* @__PURE__ */ new Map();
28
- for (const [l, c] of e.translations)
29
- r.set(l, { ...c });
30
- return { translations: r };
31
- }
32
- clone(e) {
33
- const r = this.cloneStorage(this.storage);
34
- return new v({
35
- locale: e || this._locale,
36
- fallbackLocale: this._fallbackLocale,
37
- plural: this.pluralFunc,
38
- missingWarn: this.missingWarn,
39
- missingHandler: this.missingHandler,
40
- numberFormats: this.formatter.getNumberFormats(),
41
- datetimeFormats: this.formatter.getDateTimeFormats(),
42
- _storage: r
43
- });
44
- }
45
- get locale() {
46
- return this._locale;
47
- }
48
- set locale(e) {
49
- this._locale = e;
50
- }
51
- get fallbackLocale() {
52
- return this._fallbackLocale;
53
- }
54
- set fallbackLocale(e) {
55
- this._fallbackLocale = e;
56
- }
57
- setRoute(e) {
58
- this._currentRoute = e;
59
- }
60
- getLocale() {
61
- return this._locale;
62
- }
63
- getFallbackLocale() {
64
- return this._fallbackLocale;
65
- }
66
- getRoute() {
67
- return this._currentRoute;
68
- }
69
- getRouteTranslations(e, r) {
70
- const l = `${e}:${r}`;
71
- return this.storage.translations.get(l) ?? null;
72
- }
73
- addTranslations(e, r, l = !0) {
74
- super.loadTranslationsCore(e, r, l);
75
- }
76
- addRouteTranslations(e, r, l, c = !0) {
77
- super.loadRouteTranslationsCore(e, r, l, c);
78
- }
79
- mergeTranslations(e, r, l) {
80
- this.helper.mergeTranslation(e, r, l, !0);
81
- }
82
- clearCache() {
83
- const e = { ...this.initialMessages };
84
- if (super.clearCache(), Object.keys(e).length > 0)
85
- for (const [r, l] of Object.entries(e))
86
- this.helper.loadTranslations(r, l);
87
- }
1
+ import { BaseI18n as e, FormatService as t, defaultPlural as n, interpolate as r } from "@i18n-micro/core";
2
+ import { AsyncLocalStorage as i } from "node:async_hooks";
3
+ import { existsSync as a, readFileSync as o, readdirSync as s, statSync as c } from "node:fs";
4
+ import { join as l, relative as u, resolve as d, sep as f } from "node:path";
5
+ import { mergeRouteTranslationsWithRoot as p, storeLoadedTranslationFile as m } from "@i18n-micro/utils/parse-path";
6
+ import { detectLocaleFromAcceptLanguage as h } from "@i18n-micro/utils/accept-language";
7
+ //#region src/composer.ts
8
+ var g = class t extends e {
9
+ constructor(e) {
10
+ let t = e._storage || { translations: /* @__PURE__ */ new Map() };
11
+ if (super({
12
+ storage: t,
13
+ plural: e.plural,
14
+ missingWarn: e.missingWarn,
15
+ missingHandler: e.missingHandler,
16
+ numberFormats: e.numberFormats,
17
+ datetimeFormats: e.datetimeFormats
18
+ }), this.initialMessages = {}, this.storage = t, this._locale = e.locale, this._fallbackLocale = e.fallbackLocale || e.locale, this._currentRoute = "index", e.messages) {
19
+ this.initialMessages = { ...e.messages };
20
+ for (let [t, n] of Object.entries(e.messages)) this.helper.loadTranslations(t, n);
21
+ }
22
+ }
23
+ cloneStorage(e) {
24
+ let t = /* @__PURE__ */ new Map();
25
+ for (let [n, r] of e.translations) t.set(n, { ...r });
26
+ return { translations: t };
27
+ }
28
+ clone(e) {
29
+ let n = this.cloneStorage(this.storage);
30
+ return new t({
31
+ locale: e || this._locale,
32
+ fallbackLocale: this._fallbackLocale,
33
+ plural: this.pluralFunc,
34
+ missingWarn: this.missingWarn,
35
+ missingHandler: this.missingHandler,
36
+ numberFormats: this.formatter.getNumberFormats(),
37
+ datetimeFormats: this.formatter.getDateTimeFormats(),
38
+ _storage: n
39
+ });
40
+ }
41
+ get locale() {
42
+ return this._locale;
43
+ }
44
+ set locale(e) {
45
+ this._locale = e;
46
+ }
47
+ get fallbackLocale() {
48
+ return this._fallbackLocale;
49
+ }
50
+ set fallbackLocale(e) {
51
+ this._fallbackLocale = e;
52
+ }
53
+ setRoute(e) {
54
+ this._currentRoute = e;
55
+ }
56
+ getLocale() {
57
+ return this._locale;
58
+ }
59
+ getFallbackLocale() {
60
+ return this._fallbackLocale;
61
+ }
62
+ getRoute() {
63
+ return this._currentRoute;
64
+ }
65
+ getRouteTranslations(e, t) {
66
+ let n = `${e}:${t}`;
67
+ return this.storage.translations.get(n) ?? null;
68
+ }
69
+ addTranslations(e, t, n = !0) {
70
+ super.loadTranslationsCore(e, t, n);
71
+ }
72
+ addRouteTranslations(e, t, n, r = !0) {
73
+ super.loadRouteTranslationsCore(e, t, n, r);
74
+ }
75
+ mergeTranslations(e, t, n) {
76
+ this.helper.mergeTranslation(e, t, n, !0);
77
+ }
78
+ clearCache() {
79
+ let e = { ...this.initialMessages };
80
+ if (super.clearCache(), Object.keys(e).length > 0) for (let [t, n] of Object.entries(e)) this.helper.loadTranslations(t, n);
81
+ }
82
+ }, _ = null, v = new i();
83
+ function y() {
84
+ let e = v.getStore();
85
+ return e === void 0 ? _ : e;
88
86
  }
89
- let B = null;
90
- const O = new q();
91
- function I() {
92
- const t = O.getStore();
93
- return t !== void 0 ? t : B;
87
+ function b(e) {
88
+ _ = e;
94
89
  }
95
- function X(t) {
96
- B = t;
90
+ function x(e, t) {
91
+ return v.run(e, t);
97
92
  }
98
- function Y(t, e) {
99
- return O.run(t, e);
93
+ //#endregion
94
+ //#region src/integration.ts
95
+ function S(e) {
96
+ let { locale: t, fallbackLocale: n, translationDir: r, routingStrategy: i } = e;
97
+ return b(i || null), {
98
+ name: "@i18n-micro/astro",
99
+ hooks: {
100
+ "astro:config:setup": (i) => {
101
+ let { updateConfig: a } = i, o = "virtual:i18n-micro/config", s = `\0${o}`, c = {
102
+ defaultLocale: t,
103
+ fallbackLocale: n || t,
104
+ locales: e.locales || [],
105
+ localeCodes: (e.locales || []).map((e) => e.code),
106
+ translationDir: r || null,
107
+ autoDetect: e.autoDetect ?? !0,
108
+ redirectToDefault: e.redirectToDefault ?? !1,
109
+ localeCookie: e.localeCookie === null ? null : e.localeCookie || "i18n-locale",
110
+ missingWarn: e.missingWarn ?? !1
111
+ };
112
+ a({ vite: { plugins: [{
113
+ name: "vite-plugin-i18n-micro-config",
114
+ resolveId(e) {
115
+ if (e === o) return s;
116
+ },
117
+ load(e) {
118
+ if (e === s) return `export const config = ${JSON.stringify(c)}`;
119
+ }
120
+ }] } });
121
+ },
122
+ "astro:config:done": (e) => {
123
+ let { injectTypes: t } = e;
124
+ t({
125
+ filename: "i18n-micro-env.d.ts",
126
+ content: "\n /// <reference types=\"@i18n-micro/astro/env\" />\n\n declare module 'virtual:i18n-micro/config' {\n export const config: {\n defaultLocale: string;\n fallbackLocale: string;\n locales: import('@i18n-micro/types').Locale[];\n localeCodes: string[];\n translationDir: string | null;\n autoDetect: boolean;\n redirectToDefault: boolean;\n localeCookie: string | null;\n missingWarn: boolean | null;\n }\n }\n "
127
+ });
128
+ }
129
+ }
130
+ };
100
131
  }
101
- function ce(t) {
102
- const { locale: e, fallbackLocale: r, translationDir: l, routingStrategy: c } = t;
103
- return X(c || null), {
104
- name: "@i18n-micro/astro",
105
- hooks: {
106
- // 1. Vite setup (virtual module) happens here
107
- "astro:config:setup": (u) => {
108
- const { updateConfig: i } = u, o = "virtual:i18n-micro/config", s = `\0${o}`, a = {
109
- defaultLocale: e,
110
- fallbackLocale: r || e,
111
- locales: t.locales || [],
112
- localeCodes: (t.locales || []).map((n) => n.code),
113
- translationDir: l || null,
114
- autoDetect: t.autoDetect ?? !0,
115
- redirectToDefault: t.redirectToDefault ?? !1,
116
- localeCookie: t.localeCookie === null ? null : t.localeCookie || "i18n-locale",
117
- missingWarn: t.missingWarn ?? !1
118
- };
119
- i({
120
- vite: {
121
- plugins: [
122
- {
123
- name: "vite-plugin-i18n-micro-config",
124
- resolveId(n) {
125
- if (n === o)
126
- return s;
127
- },
128
- load(n) {
129
- if (n === s)
130
- return `export const config = ${JSON.stringify(a)}`;
131
- }
132
- }
133
- ]
134
- }
135
- });
136
- },
137
- // 2. Type injection happens here (per Astro documentation)
138
- "astro:config:done": (u) => {
139
- const { injectTypes: i } = u;
140
- i({
141
- filename: "i18n-micro-env.d.ts",
142
- content: `
143
- /// <reference types="@i18n-micro/astro/env" />
144
-
145
- declare module 'virtual:i18n-micro/config' {
146
- export const config: {
147
- defaultLocale: string;
148
- fallbackLocale: string;
149
- locales: import('@i18n-micro/types').Locale[];
150
- localeCodes: string[];
151
- translationDir: string | null;
152
- autoDetect: boolean;
153
- redirectToDefault: boolean;
154
- localeCookie: string | null;
155
- missingWarn: boolean | null;
156
- }
157
- }
158
- `
159
- });
160
- }
161
- }
162
- };
132
+ function C(e) {
133
+ return new g(e);
163
134
  }
164
- function ue(t) {
165
- return new v(t);
135
+ //#endregion
136
+ //#region src/load-translations.ts
137
+ function w(e, t) {
138
+ if (a(e)) for (let n of s(e)) {
139
+ let r = l(e, n);
140
+ if (c(r).isDirectory()) {
141
+ w(r, t);
142
+ continue;
143
+ }
144
+ n.endsWith(".json") && t(r);
145
+ }
166
146
  }
167
- function N(t, e) {
168
- if (_(t))
169
- for (const r of z(t)) {
170
- const l = V(t, r);
171
- if (E(l).isDirectory()) {
172
- N(l, e);
173
- continue;
174
- }
175
- r.endsWith(".json") && e(l);
176
- }
147
+ function T(e) {
148
+ let { translationDir: t, rootDir: n = process.cwd(), disablePageLocales: r = !1 } = e, i = d(n, t);
149
+ if (!a(i)) return console.warn(`[i18n] Translation directory not found: ${i}`), {
150
+ root: {},
151
+ routes: {}
152
+ };
153
+ let s = {
154
+ root: {},
155
+ routes: {}
156
+ };
157
+ return w(i, (e) => {
158
+ let t = u(i, e).split(f).join("/");
159
+ try {
160
+ let n = o(e, "utf-8"), i = JSON.parse(n);
161
+ m(s, t, i, r);
162
+ } catch (t) {
163
+ console.error(`[i18n] Failed to load translation file: ${e}`, t);
164
+ }
165
+ }), s;
177
166
  }
178
- function ee(t) {
179
- const { translationDir: e, rootDir: r = process.cwd(), disablePageLocales: l = !1 } = t, c = x(r, e);
180
- if (!_(c))
181
- return console.warn(`[i18n] Translation directory not found: ${c}`), { root: {}, routes: {} };
182
- const u = { root: {}, routes: {} };
183
- return N(c, (i) => {
184
- const o = G(c, i).split(J).join("/");
185
- try {
186
- const s = H(i, "utf-8"), a = JSON.parse(s);
187
- K(u, o, a, l);
188
- } catch (s) {
189
- console.error(`[i18n] Failed to load translation file: ${i}`, s);
190
- }
191
- }), u;
167
+ function E(e, t) {
168
+ let { root: n, routes: r } = T(t);
169
+ for (let [t, r] of Object.entries(n)) e.addTranslations(t, r, !1);
170
+ for (let [t, i] of Object.entries(r)) for (let [r, a] of Object.entries(i)) e.addRouteTranslations(r, t, p(n[r], a), !1);
192
171
  }
193
- function fe(t, e) {
194
- const { root: r, routes: l } = ee(e);
195
- for (const [c, u] of Object.entries(r))
196
- t.addTranslations(c, u, !1);
197
- for (const [c, u] of Object.entries(l))
198
- for (const [i, o] of Object.entries(u))
199
- t.addRouteTranslations(i, c, Z(r[i], o), !1);
172
+ //#endregion
173
+ //#region src/middleware.ts
174
+ function D(e) {
175
+ let { i18n: t, defaultLocale: n, locales: r, localeObjects: i, autoDetect: a = !0, redirectToDefault: o = !1, routingStrategy: s } = e, c = s || y();
176
+ return async (e, a) => {
177
+ if (e.locals.locale && e.locals.i18n) return a();
178
+ let o = e.url, s = o.pathname;
179
+ if (!c) {
180
+ let c = t.clone(n), l = s === "/" || s === "" ? "index" : s.split("/").filter(Boolean).join("-");
181
+ return c.setRoute(l), e.locals.i18n = c, e.locals.locale = n, e.locals.defaultLocale = n, e.locals.locales = i || r.map((e) => ({ code: e })), e.locals.currentUrl = o, a();
182
+ }
183
+ let l = {
184
+ ...c,
185
+ getCurrentPath: () => s,
186
+ getRoute: () => ({
187
+ fullPath: o.pathname + o.search,
188
+ query: Object.fromEntries(o.searchParams)
189
+ })
190
+ }, u = s.split("/").filter(Boolean)[0], d = u !== void 0 && r.includes(u), f;
191
+ f = d && u ? u : l.getLocaleFromPath ? l.getLocaleFromPath(s, n, r) : n;
192
+ let p = t.clone(f), m = l.getRouteName ? l.getRouteName(s, r) : "index";
193
+ return p.setRoute(m), e.locals.i18n = p, e.locals.locale = f, e.locals.defaultLocale = n, e.locals.locales = i || r.map((e) => ({ code: e })), e.locals.currentUrl = o, e.locals.routingStrategy = l, x(l, () => a());
194
+ };
200
195
  }
201
- function ge(t) {
202
- const {
203
- i18n: e,
204
- // This is the global singleton with cache
205
- defaultLocale: r,
206
- locales: l,
207
- localeObjects: c,
208
- autoDetect: u = !0,
209
- redirectToDefault: i = !1,
210
- routingStrategy: o
211
- } = t, s = o || I();
212
- return async (a, n) => {
213
- if (a.locals.locale && a.locals.i18n)
214
- return n();
215
- const f = a.url, h = f.pathname;
216
- if (!s) {
217
- const w = e.clone(r), g = h === "/" || h === "" ? "index" : h.split("/").filter(Boolean).join("-");
218
- return w.setRoute(g), a.locals.i18n = w, a.locals.locale = r, a.locals.defaultLocale = r, a.locals.locales = c || l.map((L) => ({ code: L })), a.locals.currentUrl = f, n();
219
- }
220
- const d = {
221
- ...s,
222
- getCurrentPath: () => h,
223
- getRoute: () => ({
224
- fullPath: f.pathname + f.search,
225
- query: Object.fromEntries(f.searchParams)
226
- })
227
- }, m = h.split("/").filter(Boolean)[0], b = m !== void 0 && l.includes(m);
228
- let S;
229
- b && m ? S = m : d.getLocaleFromPath ? S = d.getLocaleFromPath(h, r, l) : S = r;
230
- const y = e.clone(S), R = d.getRouteName ? d.getRouteName(h, l) : "index";
231
- return y.setRoute(R), a.locals.i18n = y, a.locals.locale = S, a.locals.defaultLocale = r, a.locals.locales = c || l.map((w) => ({ code: w })), a.locals.currentUrl = f, a.locals.routingStrategy = d, Y(d, () => n());
232
- };
196
+ function O(e, t, n, r, i, a = "i18n-locale", o) {
197
+ let s = o ?? y(), c = r;
198
+ if (s?.getLocaleFromPath) c = s.getLocaleFromPath(e, r, i);
199
+ else {
200
+ let t = e.split("/").filter(Boolean)[0];
201
+ t && i.includes(t) && (c = t);
202
+ }
203
+ if (a !== null && c === r && t.get(a)) {
204
+ let e = t.get(a)?.value;
205
+ e && i.includes(e) && (c = e);
206
+ }
207
+ if (c === r) try {
208
+ let e = h(n.get("accept-language") ?? void 0, i);
209
+ e && (c = e);
210
+ } catch {}
211
+ return c;
233
212
  }
234
- function he(t, e, r, l, c, u = "i18n-locale", i) {
235
- const o = i ?? I();
236
- let s = l;
237
- if (o?.getLocaleFromPath)
238
- s = o.getLocaleFromPath(t, l, c);
239
- else {
240
- const n = t.split("/").filter(Boolean)[0];
241
- n && c.includes(n) && (s = n);
242
- }
243
- if (u !== null && s === l && e.get(u)) {
244
- const a = e.get(u)?.value;
245
- a && c.includes(a) && (s = a);
246
- }
247
- if (s === l)
248
- try {
249
- const a = Q(r.get("accept-language") ?? void 0, c);
250
- a && (s = a);
251
- } catch {
252
- }
253
- return s;
213
+ //#endregion
214
+ //#region src/router/adapter.ts
215
+ function k(e, t, n) {
216
+ let r = e.map((e) => e.code), i = (e, t = []) => {
217
+ let n = e.replace(/^\//, "").replace(/\/$/, "");
218
+ if (!n) return "index";
219
+ let r = n.split("/").filter(Boolean), i = r[0];
220
+ return i && t.includes(i) && r.shift(), r.length === 0 ? "index" : r.join("-");
221
+ }, a = (e, t = "en", n = []) => {
222
+ let r = e.split("/").filter(Boolean)[0];
223
+ return r && n.includes(r) ? r : t;
224
+ }, o = (e, t, n = [], r) => {
225
+ let i = e.split("/").filter(Boolean), a = i[0];
226
+ return a && n.includes(a) && i.shift(), (t !== r || r === void 0) && i.unshift(t), `/${i.join("/")}`;
227
+ }, s = (e, t, n = [], r) => {
228
+ let i = (e.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), a = i[0];
229
+ return a && n.includes(a) && i.shift(), (t !== r || r === void 0) && i.unshift(t), `/${i.join("/")}`;
230
+ };
231
+ return {
232
+ getCurrentPath: () => n ? n().pathname : typeof window < "u" ? window.location.pathname : "/",
233
+ getRouteName: i,
234
+ getLocaleFromPath: a,
235
+ switchLocalePath: o,
236
+ localizePath: s,
237
+ removeLocaleFromPath: (e, t = []) => {
238
+ let n = e.split("/").filter(Boolean), r = n[0];
239
+ return r && t.includes(r) && n.shift(), `/${n.join("/")}`;
240
+ },
241
+ resolvePath: (e, n) => {
242
+ let i = typeof e == "string" ? e : e.path || "/";
243
+ return s(i, n, r, t);
244
+ },
245
+ getRoute: () => {
246
+ if (n) {
247
+ let e = n();
248
+ return {
249
+ fullPath: e.pathname + e.search,
250
+ query: Object.fromEntries(e.searchParams)
251
+ };
252
+ }
253
+ if (typeof window < "u") {
254
+ let e = new URL(window.location.href);
255
+ return {
256
+ fullPath: e.pathname + e.search,
257
+ query: Object.fromEntries(e.searchParams)
258
+ };
259
+ }
260
+ return {
261
+ fullPath: "/",
262
+ query: {}
263
+ };
264
+ },
265
+ push: (e) => {
266
+ typeof window < "u" && (window.location.href = e.path);
267
+ },
268
+ replace: (e) => {
269
+ typeof window < "u" && window.location.replace(e.path);
270
+ }
271
+ };
254
272
  }
255
- function de(t, e, r) {
256
- const l = t.map((n) => n.code), c = (n, f = []) => {
257
- const h = n.replace(/^\//, "").replace(/\/$/, "");
258
- if (!h)
259
- return "index";
260
- const d = h.split("/").filter(Boolean), p = d[0];
261
- return p && f.includes(p) && d.shift(), d.length === 0 ? "index" : d.join("-");
262
- }, u = (n, f = "en", h = []) => {
263
- const p = n.split("/").filter(Boolean)[0];
264
- return p && h.includes(p) ? p : f;
265
- }, i = (n, f, h = [], d) => {
266
- const p = n.split("/").filter(Boolean), m = p[0];
267
- return m && h.includes(m) && p.shift(), (f !== d || d === void 0) && p.unshift(f), `/${p.join("/")}`;
268
- }, o = (n, f, h = [], d) => {
269
- const m = (n.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), b = m[0];
270
- return b && h.includes(b) && m.shift(), (f !== d || d === void 0) && m.unshift(f), `/${m.join("/")}`;
271
- };
272
- return {
273
- getCurrentPath: () => r ? r().pathname : typeof window < "u" ? window.location.pathname : "/",
274
- getRouteName: c,
275
- getLocaleFromPath: u,
276
- switchLocalePath: i,
277
- localizePath: o,
278
- removeLocaleFromPath: (n, f = []) => {
279
- const h = n.split("/").filter(Boolean), d = h[0];
280
- return d && f.includes(d) && h.shift(), `/${h.join("/")}`;
281
- },
282
- resolvePath: (n, f) => {
283
- const h = typeof n == "string" ? n : n.path || "/";
284
- return o(h, f, l, e);
285
- },
286
- getRoute: () => {
287
- if (r) {
288
- const n = r();
289
- return {
290
- fullPath: n.pathname + n.search,
291
- query: Object.fromEntries(n.searchParams)
292
- };
293
- }
294
- if (typeof window < "u") {
295
- const n = new URL(window.location.href);
296
- return {
297
- fullPath: n.pathname + n.search,
298
- query: Object.fromEntries(n.searchParams)
299
- };
300
- }
301
- return {
302
- fullPath: "/",
303
- query: {}
304
- };
305
- },
306
- // Optional: client-side navigation for islands
307
- push: (n) => {
308
- typeof window < "u" && (window.location.href = n.path);
309
- },
310
- replace: (n) => {
311
- typeof window < "u" && window.location.replace(n.path);
312
- }
313
- };
273
+ //#endregion
274
+ //#region ../utils/dist/resolve-hreflang.mjs
275
+ function A(e) {
276
+ return e.iso?.trim() || String(e.code);
314
277
  }
315
- function T(t) {
316
- return t.iso?.trim() || String(t.code);
278
+ function j(e) {
279
+ let t = e.match(/^([A-Za-z]{2,3})(?:[-_](.+))?$/);
280
+ return t ? {
281
+ language: t[1],
282
+ region: t[2]
283
+ } : { language: e };
317
284
  }
318
- function D(t) {
319
- const e = t.match(/^([A-Za-z]{2,3})(?:[-_](.+))?$/);
320
- return e ? { language: e[1], region: e[2] } : { language: t };
285
+ function M(e, t = {}) {
286
+ let { hreflangBaseLanguage: n = !1 } = t;
287
+ if (!n) return e.map((e) => {
288
+ let t = String(e.code);
289
+ return {
290
+ hreflang: A(e),
291
+ localeCode: t
292
+ };
293
+ });
294
+ let r = /* @__PURE__ */ new Map();
295
+ for (let t of e) {
296
+ let e = String(t.code), n = A(t), { language: i, region: a } = j(n);
297
+ i && a && !r.has(i) && r.set(i, e), r.set(n, e);
298
+ }
299
+ let i = /* @__PURE__ */ new Set(), a = [];
300
+ for (let t of e) {
301
+ let e = String(t.code), n = A(t), { language: o, region: s } = j(n);
302
+ o && s && r.get(o) === e && !i.has(o) && (i.add(o), a.push({
303
+ hreflang: o,
304
+ localeCode: e
305
+ })), i.has(n) || (i.add(n), a.push({
306
+ hreflang: n,
307
+ localeCode: e
308
+ }));
309
+ }
310
+ return a;
321
311
  }
322
- function te(t, e = {}) {
323
- const { hreflangBaseLanguage: r = !1 } = e;
324
- if (!r)
325
- return t.map((i) => {
326
- const o = String(i.code);
327
- return { hreflang: T(i), localeCode: o };
328
- });
329
- const l = /* @__PURE__ */ new Map();
330
- for (const i of t) {
331
- const o = String(i.code), s = T(i), { language: a, region: n } = D(s);
332
- a && n && !l.has(a) && l.set(a, o), l.set(s, o);
333
- }
334
- const c = /* @__PURE__ */ new Set(), u = [];
335
- for (const i of t) {
336
- const o = String(i.code), s = T(i), { language: a, region: n } = D(s);
337
- a && n && l.get(a) === o && !c.has(a) && (c.add(a), u.push({ hreflang: a, localeCode: o })), c.has(s) || (c.add(s), u.push({ hreflang: s, localeCode: o }));
338
- }
339
- return u;
312
+ //#endregion
313
+ //#region ../utils/dist/resolve-og-locale.mjs
314
+ function N(e) {
315
+ let t = e.og?.trim();
316
+ if (t) return t;
317
+ let n = e.iso?.trim();
318
+ if (!n) return null;
319
+ let r = n.indexOf("_");
320
+ if (r > 0) {
321
+ let e = n.slice(0, r), t = n.slice(r + 1);
322
+ if (e.length >= 2 && t.length === 2) return `${e.toLowerCase()}_${t.toUpperCase()}`;
323
+ }
324
+ let i = n.indexOf("-");
325
+ if (i > 0) {
326
+ let e = n.slice(0, i), t = n.slice(i + 1);
327
+ if (e.length >= 2 && t.length === 2 && /^[A-Za-z]{2}$/.test(t)) return `${e.toLowerCase()}_${t.toUpperCase()}`;
328
+ }
329
+ return null;
340
330
  }
341
- function $(t) {
342
- const e = t.og?.trim();
343
- if (e) return e;
344
- const r = t.iso?.trim();
345
- if (!r) return null;
346
- const l = r.indexOf("_");
347
- if (l > 0) {
348
- const u = r.slice(0, l), i = r.slice(l + 1);
349
- if (u.length >= 2 && i.length === 2)
350
- return `${u.toLowerCase()}_${i.toUpperCase()}`;
351
- }
352
- const c = r.indexOf("-");
353
- if (c > 0) {
354
- const u = r.slice(0, c), i = r.slice(c + 1);
355
- if (u.length >= 2 && i.length === 2 && /^[A-Za-z]{2}$/.test(i))
356
- return `${u.toLowerCase()}_${i.toUpperCase()}`;
357
- }
358
- return null;
331
+ function P(e, t = {}) {
332
+ if (N(e) !== null || t.missingWarn === !1 || process.env.NODE_ENV === "production") return;
333
+ let n = t.tag ?? "og:locale", r = String(e.code ?? "unknown"), i = e.iso ? `, iso: '${e.iso}'` : "";
334
+ console.warn(`[i18n] Cannot derive ${n} for locale '${r}'${i}. Set locale.og (e.g. 'en_US') or use iso with a 2-letter region (e.g. 'en-US').`);
359
335
  }
360
- function C(t, e = {}) {
361
- if ($(t) !== null || e.missingWarn === !1 || process.env.NODE_ENV === "production") return;
362
- const r = e.tag ?? "og:locale", l = String(t.code ?? "unknown"), c = t.iso ? `, iso: '${t.iso}'` : "";
363
- console.warn(
364
- `[i18n] Cannot derive ${r} for locale '${l}'${c}. Set locale.og (e.g. 'en_US') or use iso with a 2-letter region (e.g. 'en-US').`
365
- );
336
+ //#endregion
337
+ //#region src/utils.ts
338
+ function F(e) {
339
+ let t = e.locals.i18n;
340
+ if (!t) throw Error("i18n instance not found. Make sure i18n middleware is configured.");
341
+ return t;
366
342
  }
367
- function M(t) {
368
- const e = t.locals.i18n;
369
- if (!e)
370
- throw new Error("i18n instance not found. Make sure i18n middleware is configured.");
371
- return e;
343
+ function I(e) {
344
+ return e.locals.locale || "en";
372
345
  }
373
- function j(t) {
374
- return t.locals.locale || "en";
346
+ function L(e) {
347
+ return e.locals.defaultLocale || "en";
375
348
  }
376
- function k(t) {
377
- return t.locals.defaultLocale || "en";
349
+ function R(e) {
350
+ return e.locals.locales || [];
378
351
  }
379
- function A(t) {
380
- return t.locals.locales || [];
352
+ function z(e) {
353
+ return e.locals.routingStrategy || null;
381
354
  }
382
- function W(t) {
383
- return t.locals.routingStrategy || null;
355
+ function B(e) {
356
+ let t = F(e), n = I(e), r = L(e), i = R(e), a = i.map((e) => e.code), o = z(e);
357
+ return {
358
+ locale: n,
359
+ defaultLocale: r,
360
+ locales: i,
361
+ t: (e, n, r, i) => t.t(e, n, r, i),
362
+ ts: (e, n, r, i) => t.ts(e, n, r, i),
363
+ tc: (e, n, r) => t.tc(e, n, r),
364
+ tn: t.tn.bind(t),
365
+ td: t.td.bind(t),
366
+ tdr: t.tdr.bind(t),
367
+ has: (e, n) => t.has(e, n),
368
+ getRoute: () => t.getRoute(),
369
+ getRouteName: (t) => {
370
+ let n = t || e.url.pathname;
371
+ if (o?.getRouteName) return o.getRouteName(n, a);
372
+ let r = n.replace(/^\//, "").replace(/\/$/, "");
373
+ if (!r) return "index";
374
+ let i = r.split("/").filter(Boolean), s = i[0];
375
+ return s && a.includes(s) && i.shift(), i.length === 0 ? "index" : i.join("-");
376
+ },
377
+ getLocaleFromPath: (t) => {
378
+ let n = t || e.url.pathname;
379
+ if (o?.getLocaleFromPath) return o.getLocaleFromPath(n, r, a);
380
+ let i = n.split("/").filter(Boolean)[0];
381
+ return i && a.includes(i) ? i : r;
382
+ },
383
+ switchLocalePath: (t) => {
384
+ if (o?.switchLocalePath) return o.switchLocalePath(e.url.pathname, t, a, r);
385
+ let n = e.url.pathname.split("/").filter(Boolean), i = n[0];
386
+ return i && a.includes(i) && n.shift(), t !== r && n.unshift(t), `/${n.join("/")}`;
387
+ },
388
+ localizePath: (e, t) => {
389
+ if (o?.localizePath) return o.localizePath(e, t || n, a, r);
390
+ let i = (e.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), s = i[0];
391
+ return s && a.includes(s) && i.shift(), t && t !== r && i.unshift(t), `/${i.join("/")}`;
392
+ },
393
+ getI18n: () => t,
394
+ getBasePath: (t) => {
395
+ let n = (t || e.url).pathname.split("/").filter(Boolean), r = n[0];
396
+ return r && a.includes(r) && n.shift(), n.length > 0 ? `/${n.join("/")}` : "/";
397
+ },
398
+ addTranslations: (e, n, r = !0) => {
399
+ t.addTranslations(e, n, r);
400
+ },
401
+ addRouteTranslations: (e, n, r, i = !0) => {
402
+ t.addRouteTranslations(e, n, r, i);
403
+ },
404
+ mergeTranslations: (e, n, r) => {
405
+ t.mergeTranslations(e, n, r);
406
+ },
407
+ clearCache: () => {
408
+ t.clearCache();
409
+ }
410
+ };
384
411
  }
385
- function me(t) {
386
- const e = M(t), r = j(t), l = k(t), c = A(t), u = c.map((o) => o.code), i = W(t);
387
- return {
388
- // Current locale
389
- locale: r,
390
- defaultLocale: l,
391
- locales: c,
392
- // Translation methods
393
- t: (o, s, a, n) => e.t(o, s, a, n),
394
- ts: (o, s, a, n) => e.ts(o, s, a, n),
395
- tc: (o, s, a) => e.tc(o, s, a),
396
- tn: e.tn.bind(e),
397
- td: e.td.bind(e),
398
- tdr: e.tdr.bind(e),
399
- has: (o, s) => e.has(o, s),
400
- // Route management
401
- getRoute: () => e.getRoute(),
402
- getRouteName: (o) => {
403
- const s = o || t.url.pathname;
404
- if (i?.getRouteName)
405
- return i.getRouteName(s, u);
406
- const a = s.replace(/^\//, "").replace(/\/$/, "");
407
- if (!a) return "index";
408
- const n = a.split("/").filter(Boolean), f = n[0];
409
- return f && u.includes(f) && n.shift(), n.length === 0 ? "index" : n.join("-");
410
- },
411
- getLocaleFromPath: (o) => {
412
- const s = o || t.url.pathname;
413
- if (i?.getLocaleFromPath)
414
- return i.getLocaleFromPath(s, l, u);
415
- const n = s.split("/").filter(Boolean)[0];
416
- return n && u.includes(n) ? n : l;
417
- },
418
- // Path utilities
419
- switchLocalePath: (o) => {
420
- if (i?.switchLocalePath)
421
- return i.switchLocalePath(t.url.pathname, o, u, l);
422
- const s = t.url.pathname.split("/").filter(Boolean), a = s[0];
423
- return a && u.includes(a) && s.shift(), o !== l && s.unshift(o), `/${s.join("/")}`;
424
- },
425
- localizePath: (o, s) => {
426
- if (i?.localizePath)
427
- return i.localizePath(o, s || r, u, l);
428
- const n = (o.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), f = n[0];
429
- return f && u.includes(f) && n.shift(), s && s !== l && n.unshift(s), `/${n.join("/")}`;
430
- },
431
- // Get i18n instance
432
- getI18n: () => e,
433
- // Get base path without locale (for rewrite)
434
- getBasePath: (o) => {
435
- const n = (o || t.url).pathname.split("/").filter(Boolean), f = n[0];
436
- return f && u.includes(f) && n.shift(), n.length > 0 ? `/${n.join("/")}` : "/";
437
- },
438
- // Translation management
439
- addTranslations: (o, s, a = !0) => {
440
- e.addTranslations(o, s, a);
441
- },
442
- addRouteTranslations: (o, s, a, n = !0) => {
443
- e.addRouteTranslations(o, s, a, n);
444
- },
445
- mergeTranslations: (o, s, a) => {
446
- e.mergeTranslations(o, s, a);
447
- },
448
- clearCache: () => {
449
- e.clearCache();
450
- }
451
- };
412
+ function V(e) {
413
+ let t = e.locals.currentUrl ?? e.url;
414
+ return B(e).getBasePath(t);
452
415
  }
453
- function pe(t, e = {}) {
454
- const { baseUrl: r = "/", addDirAttribute: l = !0, addSeoAttributes: c = !0, hreflangBaseLanguage: u = !1 } = e, i = j(t), o = k(t), a = A(t).filter((g) => !g.disabled), n = a.filter((g) => g.seo !== !1), f = a.find((g) => g.code === i);
455
- if (!f)
456
- return { htmlAttrs: {}, link: [], meta: [] };
457
- const h = f.iso || i, d = $(f), p = f.dir || "auto", m = {
458
- htmlAttrs: {
459
- lang: h,
460
- ...l ? { dir: p } : {}
461
- },
462
- link: [],
463
- meta: []
464
- };
465
- if (!c)
466
- return m;
467
- const b = `${r}${t.url.pathname}`;
468
- m.link.push({
469
- rel: "canonical",
470
- href: b
471
- });
472
- const S = W(t), y = a.map((g) => g.code), R = /* @__PURE__ */ new Map();
473
- for (const g of n) {
474
- let L = t.url.pathname;
475
- if (S?.switchLocalePath)
476
- L = S.switchLocalePath(t.url.pathname, g.code, y, o);
477
- else {
478
- const P = t.url.pathname.split("/").filter(Boolean), F = P[0];
479
- F && y.includes(F) && P.shift(), g.code !== o && P.unshift(g.code), L = `/${P.join("/")}`;
480
- }
481
- R.set(String(g.code), `${r}${L}`);
482
- }
483
- for (const { hreflang: g, localeCode: L } of te(n, { hreflangBaseLanguage: u })) {
484
- const P = R.get(L);
485
- P && m.link.push({
486
- rel: "alternate",
487
- href: P,
488
- hreflang: g
489
- });
490
- }
491
- if (a.find((g) => g.code === o)?.seo !== !1) {
492
- let g = t.url.pathname;
493
- if (S?.switchLocalePath)
494
- g = S.switchLocalePath(t.url.pathname, o, y, o);
495
- else {
496
- const L = t.url.pathname.split("/").filter(Boolean), P = L[0];
497
- P && y.includes(P) && L.shift(), g = `/${L.join("/")}`;
498
- }
499
- m.link.push({
500
- rel: "alternate",
501
- href: `${r}${g}`,
502
- hreflang: "x-default"
503
- });
504
- }
505
- d ? m.meta.push({
506
- property: "og:locale",
507
- content: d
508
- }) : C(f, { tag: "og:locale" }), m.meta.push({
509
- property: "og:url",
510
- content: b
511
- });
512
- for (const g of n) {
513
- if (g.code === i) continue;
514
- const L = $(g);
515
- if (!L) {
516
- C(g, { tag: "og:locale:alternate" });
517
- continue;
518
- }
519
- m.meta.push({
520
- property: "og:locale:alternate",
521
- content: L
522
- });
523
- }
524
- return m;
416
+ function H(e) {
417
+ let { locale: t } = e.params, n = R(e).map((e) => e.code);
418
+ if (!t || !n.includes(t)) return new Response(null, { status: 404 });
419
+ let r = e.locals.i18n;
420
+ return r && (r.locale = t, e.locals.locale = t), V(e);
525
421
  }
526
- function ne(t, e, r) {
527
- const l = e.split(".");
528
- let c = t;
529
- for (let i = 0; i < l.length - 1; i++) {
530
- const o = l[i];
531
- c[o] || (c[o] = {}), c = c[o];
532
- }
533
- const u = l[l.length - 1];
534
- u !== void 0 && (c[u] = r);
422
+ function U(e, t = {}) {
423
+ let { baseUrl: n = "/", addDirAttribute: r = !0, addSeoAttributes: i = !0, hreflangBaseLanguage: a = !1 } = t, o = I(e), s = L(e), c = R(e).filter((e) => !e.disabled), l = c.filter((e) => e.seo !== !1), u = c.find((e) => e.code === o);
424
+ if (!u) return {
425
+ htmlAttrs: {},
426
+ link: [],
427
+ meta: []
428
+ };
429
+ let d = u.iso || o, f = N(u), p = u.dir || "auto", m = {
430
+ htmlAttrs: {
431
+ lang: d,
432
+ ...r ? { dir: p } : {}
433
+ },
434
+ link: [],
435
+ meta: []
436
+ };
437
+ if (!i) return m;
438
+ let h = `${n}${e.url.pathname}`;
439
+ m.link.push({
440
+ rel: "canonical",
441
+ href: h
442
+ });
443
+ let g = z(e), _ = c.map((e) => e.code), v = /* @__PURE__ */ new Map();
444
+ for (let t of l) {
445
+ let r = e.url.pathname;
446
+ if (g?.switchLocalePath) r = g.switchLocalePath(e.url.pathname, t.code, _, s);
447
+ else {
448
+ let n = e.url.pathname.split("/").filter(Boolean), i = n[0];
449
+ i && _.includes(i) && n.shift(), t.code !== s && n.unshift(t.code), r = `/${n.join("/")}`;
450
+ }
451
+ v.set(String(t.code), `${n}${r}`);
452
+ }
453
+ for (let { hreflang: e, localeCode: t } of M(l, { hreflangBaseLanguage: a })) {
454
+ let n = v.get(t);
455
+ n && m.link.push({
456
+ rel: "alternate",
457
+ href: n,
458
+ hreflang: e
459
+ });
460
+ }
461
+ if (c.find((e) => e.code === s)?.seo !== !1) {
462
+ let t = e.url.pathname;
463
+ if (g?.switchLocalePath) t = g.switchLocalePath(e.url.pathname, s, _, s);
464
+ else {
465
+ let n = e.url.pathname.split("/").filter(Boolean), r = n[0];
466
+ r && _.includes(r) && n.shift(), t = `/${n.join("/")}`;
467
+ }
468
+ m.link.push({
469
+ rel: "alternate",
470
+ href: `${n}${t}`,
471
+ hreflang: "x-default"
472
+ });
473
+ }
474
+ f ? m.meta.push({
475
+ property: "og:locale",
476
+ content: f
477
+ }) : P(u, { tag: "og:locale" }), m.meta.push({
478
+ property: "og:url",
479
+ content: h
480
+ });
481
+ for (let e of l) {
482
+ if (e.code === o) continue;
483
+ let t = N(e);
484
+ if (!t) {
485
+ P(e, { tag: "og:locale:alternate" });
486
+ continue;
487
+ }
488
+ m.meta.push({
489
+ property: "og:locale:alternate",
490
+ content: t
491
+ });
492
+ }
493
+ return m;
535
494
  }
536
- function Le(t, e) {
537
- const r = M(t), l = j(t), c = k(t), u = r.getRoute(), i = {};
538
- if (e && e.length > 0) {
539
- const o = {};
540
- for (const s of e) {
541
- const a = r.t(s, void 0, void 0, u);
542
- a != null && a !== s && ne(o, s, a);
543
- }
544
- Object.keys(o).length > 0 && (i[u] = o);
545
- } else {
546
- const o = r.getRouteTranslations(l, u);
547
- o && (i[u] = o);
548
- }
549
- return {
550
- locale: l,
551
- fallbackLocale: c,
552
- currentRoute: u,
553
- translations: i
554
- };
495
+ function W(e, t, n) {
496
+ let r = t.split("."), i = e;
497
+ for (let e = 0; e < r.length - 1; e++) {
498
+ let t = r[e];
499
+ i[t] || (i[t] = {}), i = i[t];
500
+ }
501
+ let a = r[r.length - 1];
502
+ a !== void 0 && (i[a] = n);
555
503
  }
556
- export {
557
- v as AstroI18n,
558
- be as FormatService,
559
- de as createAstroRouterAdapter,
560
- ue as createI18n,
561
- ge as createI18nMiddleware,
562
- ye as defaultPlural,
563
- he as detectLocale,
564
- k as getDefaultLocale,
565
- I as getGlobalRoutingStrategy,
566
- M as getI18n,
567
- Le as getI18nProps,
568
- j as getLocale,
569
- A as getLocales,
570
- ce as i18nIntegration,
571
- we as interpolate,
572
- ee as loadTranslationsFromDir,
573
- fe as loadTranslationsIntoI18n,
574
- Y as runWithRoutingStrategy,
575
- X as setGlobalRoutingStrategy,
576
- me as useI18n,
577
- pe as useLocaleHead
578
- };
504
+ function G(e, t) {
505
+ let n = F(e), r = I(e), i = L(e), a = n.getRoute(), o = {};
506
+ if (t && t.length > 0) {
507
+ let e = {};
508
+ for (let r of t) {
509
+ let t = n.t(r, void 0, void 0, a);
510
+ t != null && t !== r && W(e, r, t);
511
+ }
512
+ Object.keys(e).length > 0 && (o[a] = e);
513
+ } else {
514
+ let e = n.getRouteTranslations(r, a);
515
+ e && (o[a] = e);
516
+ }
517
+ return {
518
+ locale: r,
519
+ fallbackLocale: i,
520
+ currentRoute: a,
521
+ translations: o
522
+ };
523
+ }
524
+ //#endregion
525
+ export { g as AstroI18n, t as FormatService, k as createAstroRouterAdapter, C as createI18n, D as createI18nMiddleware, n as defaultPlural, O as detectLocale, L as getDefaultLocale, y as getGlobalRoutingStrategy, F as getI18n, G as getI18nProps, I as getLocale, V as getLocaleRewritePath, R as getLocales, S as i18nIntegration, r as interpolate, T as loadTranslationsFromDir, E as loadTranslationsIntoI18n, H as prepareLocaleRewrite, x as runWithRoutingStrategy, b as setGlobalRoutingStrategy, B as useI18n, U as useLocaleHead };