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