@i18n-micro/astro 1.3.25 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +8 -64
  3. package/dist/auto-middleware.d.ts +1 -0
  4. package/dist/client/core.d.cts +86 -0
  5. package/dist/client/core.d.ts +71 -8
  6. package/dist/client/index.d.cts +2 -6
  7. package/dist/client/index.d.ts +2 -6
  8. package/dist/client/index.js +1 -6
  9. package/dist/client/preact.d.ts +12 -23
  10. package/dist/client/preact.js +13 -37
  11. package/dist/client/react.d.ts +14 -21
  12. package/dist/client/react.js +12 -37
  13. package/dist/client/svelte.d.ts +14 -14
  14. package/dist/client/svelte.js +17 -47
  15. package/dist/client/vue.d.ts +15 -15
  16. package/dist/client/vue.js +37 -2
  17. package/dist/composer.d.cts +16 -0
  18. package/dist/composer.d.ts +10 -30
  19. package/dist/core-CgrgdmGy.cjs +1 -0
  20. package/dist/core-DTAcn2yF.js +72 -0
  21. package/dist/env.d.ts +8 -2
  22. package/dist/index.cjs +11 -3
  23. package/dist/index.d.cts +10 -13
  24. package/dist/index.d.ts +10 -13
  25. package/dist/index.mjs +171 -482
  26. package/dist/integration.d.cts +26 -0
  27. package/dist/integration.d.ts +14 -15
  28. package/dist/middleware-BT3OA5YL.cjs +1 -0
  29. package/dist/middleware-Ch8E0iV_.js +257 -0
  30. package/dist/middleware.d.cts +34 -0
  31. package/dist/middleware.d.ts +17 -8
  32. package/dist/middleware.js +23 -0
  33. package/dist/node.d.ts +26 -0
  34. package/dist/node.js +22 -0
  35. package/dist/request.d.ts +69 -0
  36. package/dist/router/adapter.d.cts +9 -0
  37. package/dist/router/adapter.d.ts +5 -4
  38. package/dist/router/types.d.cts +66 -0
  39. package/dist/router/types.d.ts +3 -2
  40. package/dist/utils.d.cts +112 -0
  41. package/dist/utils.d.ts +39 -35
  42. package/dist/virtual.d.ts +21 -0
  43. package/package.json +28 -15
  44. package/src/auto-middleware.ts +26 -0
  45. package/src/client/core.ts +124 -59
  46. package/src/client/index.ts +2 -11
  47. package/src/client/preact.tsx +12 -127
  48. package/src/client/react.tsx +11 -123
  49. package/src/client/svelte.ts +6 -69
  50. package/src/client/vue.ts +22 -77
  51. package/src/components/i18n-link.astro +2 -59
  52. package/src/components/i18n-switcher.astro +46 -149
  53. package/src/components/i18n-t.astro +6 -54
  54. package/src/composer.ts +25 -115
  55. package/src/env.d.ts +7 -0
  56. package/src/index.ts +1 -6
  57. package/src/integration.ts +113 -52
  58. package/src/middleware.ts +174 -120
  59. package/src/node.ts +51 -0
  60. package/src/request.ts +177 -0
  61. package/src/router/adapter.ts +47 -149
  62. package/src/router/types.ts +3 -2
  63. package/src/utils.ts +149 -323
  64. package/src/virtual.d.ts +21 -0
  65. package/dist/components/index.d.cts +0 -0
  66. package/dist/components/index.d.ts +0 -0
  67. package/dist/core-DwkpOV-H.cjs +0 -1
  68. package/dist/core-VSnkGRWR.js +0 -28
  69. package/dist/load-translations.d.ts +0 -43
  70. package/dist/routing-context.d.ts +0 -4
  71. package/dist/vue-BNiEwZr2.cjs +0 -5
  72. package/dist/vue-CWHjIOiS.js +0 -1724
  73. package/src/components/index.ts +0 -11
  74. package/src/load-translations.ts +0 -106
  75. package/src/routing-context.ts +0 -23
package/dist/index.mjs CHANGED
@@ -1,525 +1,214 @@
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;
1
+ import { a as e, i as t, n, o as r, r as i, t as a } from "./middleware-Ch8E0iV_.js";
2
+ import { FormatService as o, defaultPlural as s, getByPath as c, interpolate as l, isProduction as u, localeIsoMap as d, setTranslationAtKey as f } from "@i18n-micro/core";
3
+ import { getEnabledLocaleCodes as p, getEnabledLocales as m } from "@i18n-micro/utils/active-locales";
4
+ import { forwardSlashes as h, withoutAppBaseURL as g } from "@i18n-micro/utils/app-path";
5
+ import { buildLocaleHead as _, joinBase as v } from "@i18n-micro/utils/locale-head";
6
+ // src/integration.ts
7
+ var y = [
8
+ "messages",
9
+ "plural",
10
+ "missingHandler",
11
+ "routingStrategy"
12
+ ], b = "virtual:i18n-micro/config", x = "virtual:i18n-micro/messages", S = (e) => `\0${e}`, C = "@i18n-micro/astro/node";
13
+ function w(e, t) {
14
+ let n = e.locales || [];
15
+ return {
16
+ defaultLocale: e.locale,
17
+ fallbackLocale: e.fallbackLocale || e.locale,
18
+ locales: n,
19
+ localeCodes: p(n),
20
+ translationDir: e.translationDir || null,
21
+ autoDetect: e.autoDetect ?? !0,
22
+ redirectToDefault: e.redirectToDefault ?? !1,
23
+ localeCookie: e.localeCookie === null ? null : e.localeCookie || "i18n-locale",
24
+ missingWarn: e.missingWarn ?? !1,
25
+ base: t === "/" ? "" : t
26
+ };
86
27
  }
87
- function b(e) {
88
- _ = e;
28
+ function T(e, t) {
29
+ let n = `export const config = ${JSON.stringify(w(e, t.base))}`, r = [], i = async (n) => {
30
+ let { translationDir: i, disablePageLocales: a } = e;
31
+ if (!i) return "export const messages = {\"root\":{},\"routes\":{}}";
32
+ let { readTranslationDir: o } = await import(
33
+ /* @vite-ignore */
34
+ C
35
+ ), { messages: s, files: c } = o({
36
+ translationDir: i,
37
+ rootDir: t.root,
38
+ disablePageLocales: a
39
+ });
40
+ r = c;
41
+ for (let e of c) n(e);
42
+ return `export const messages = ${JSON.stringify(s)}`;
43
+ };
44
+ return {
45
+ name: "vite-plugin-i18n-micro-config",
46
+ resolveId(e) {
47
+ return e === b || e === x ? S(e) : void 0;
48
+ },
49
+ load(e) {
50
+ return e === S(b) ? n : e === S(x) ? i((e) => this.addWatchFile(e)) : void 0;
51
+ },
52
+ configureServer(t) {
53
+ let n = e.translationDir && `/${e.translationDir.replace(/^\.?\/+|\/+$/g, "")}/`;
54
+ n && t.watcher.on("add", (e) => {
55
+ e.endsWith(".json") && h(e).includes(n) && r[0] && t.watcher.emit("change", r[0]);
56
+ });
57
+ }
58
+ };
89
59
  }
90
- function x(e, t) {
91
- return v.run(e, t);
60
+ function E(e, t) {
61
+ let n = p(e.locales);
62
+ return e.middleware !== !1 || t.i18n || !n.includes(e.locale) ? {} : { i18n: {
63
+ locales: n,
64
+ defaultLocale: e.locale,
65
+ routing: "manual"
66
+ } };
92
67
  }
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), {
68
+ function D(e) {
69
+ let t = y.find((t) => e[t] !== void 0);
70
+ if (t) throw Error(`[i18n-micro] i18nIntegration no longer takes "${t}": pass it to createI18n / createI18nMiddleware in src/middleware.ts`);
71
+ return {
98
72
  name: "@i18n-micro/astro",
99
73
  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)}`;
74
+ "astro:config:setup": (t) => {
75
+ let { updateConfig: n, addMiddleware: r, config: i } = t;
76
+ n({
77
+ ...E(e, i),
78
+ vite: {
79
+ ssr: { noExternal: ["@i18n-micro/astro"] },
80
+ plugins: [T(e, i)]
119
81
  }
120
- }] } });
82
+ }), e.middleware !== !1 && r({
83
+ order: "pre",
84
+ entrypoint: "@i18n-micro/astro/middleware"
85
+ });
121
86
  },
122
87
  "astro:config:done": (e) => {
123
88
  let { injectTypes: t } = e;
124
89
  t({
125
90
  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 "
91
+ 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;\n base: string;\n }\n }\n\n declare module 'virtual:i18n-micro/messages' {\n export const messages: {\n root: Record<string, import('@i18n-micro/types').Translations>;\n routes: Record<string, Record<string, import('@i18n-micro/types').Translations>>;\n }\n }\n "
127
92
  });
128
93
  }
129
94
  }
130
95
  };
131
96
  }
132
- function C(e) {
133
- return new g(e);
134
- }
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
- }
146
- }
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;
166
- }
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);
171
- }
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
- };
195
- }
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;
97
+ function O(e) {
98
+ return new r(e);
212
99
  }
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
- };
272
- }
273
- //#endregion
274
- //#region ../utils/dist/resolve-hreflang.mjs
275
- function A(e) {
276
- return e.iso?.trim() || String(e.code);
277
- }
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 };
284
- }
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;
311
- }
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;
330
- }
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').`);
335
- }
336
- //#endregion
337
- //#region src/utils.ts
338
- function F(e) {
100
+ // src/utils.ts
101
+ function k(e) {
339
102
  let t = e.locals.i18n;
340
103
  if (!t) throw Error("i18n instance not found. Make sure i18n middleware is configured.");
341
104
  return t;
342
105
  }
343
- function I(e) {
344
- return e.locals.locale || "en";
106
+ function A(e) {
107
+ return e.locals.locale ?? k(e).getLocale();
345
108
  }
346
- function L(e) {
347
- return e.locals.defaultLocale || "en";
109
+ function j(e) {
110
+ return e.locals.defaultLocale ?? k(e).getFallbackLocale();
348
111
  }
349
- function R(e) {
112
+ function M(e) {
350
113
  return e.locals.locales || [];
351
114
  }
352
- function z(e) {
353
- return e.locals.routingStrategy || null;
354
- }
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);
115
+ function N(e) {
116
+ let { locals: t } = e, n = t.i18nBase ?? "", r = j(e), a = M(e);
357
117
  return {
358
- locale: n,
118
+ base: n,
359
119
  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
- }
120
+ codes: p(a),
121
+ strategy: t.routingStrategy ?? i(a, r),
122
+ path: g((t.currentUrl ?? e.url).pathname, n)
410
123
  };
411
124
  }
412
- function V(e) {
413
- let t = e.locals.currentUrl ?? e.url;
414
- return B(e).getBasePath(t);
125
+ function P(n) {
126
+ let r = k(n), i = A(n), a = j(n), o = M(n), s = N(n), { strategy: c, codes: l, base: u, path: d } = s, f = (e) => g(e, u);
127
+ return {
128
+ locale: i,
129
+ defaultLocale: a,
130
+ locales: o,
131
+ t: r.t.bind(r),
132
+ ts: r.ts.bind(r),
133
+ tc: r.tc.bind(r),
134
+ tn: r.tn.bind(r),
135
+ td: r.td.bind(r),
136
+ tdr: r.tdr.bind(r),
137
+ has: r.has.bind(r),
138
+ getRoute: r.getRoute.bind(r),
139
+ getRouteName: (e) => e === void 0 ? r.getRoute() : c.getRouteName(f(e), l),
140
+ getLocaleFromPath: (e) => c.getLocaleFromPath(e === void 0 ? d : f(e), a, l),
141
+ switchLocalePath: (t) => e(c.switchLocalePath(d, t, l, a), u),
142
+ localizePath: (e, n) => t(e, n || i, s),
143
+ getI18n: () => r,
144
+ getBasePath: (e) => c.removeLocaleFromPath(e ? f(e.pathname) : d, l),
145
+ addTranslations: r.addTranslations.bind(r),
146
+ addRouteTranslations: r.addRouteTranslations.bind(r),
147
+ mergeTranslations: r.mergeTranslations.bind(r),
148
+ clearCache: r.clearCache.bind(r)
149
+ };
415
150
  }
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);
151
+ function F(e) {
152
+ return P(e).getBasePath(e.locals.currentUrl ?? e.url);
421
153
  }
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 {
154
+ function I(e) {
155
+ let { locale: t } = e.params;
156
+ if (!t || !p(M(e)).includes(t)) return new Response(null, { status: 404 });
157
+ let n = e.locals.i18n;
158
+ return n && (n.locale = t, e.locals.locale = t), F(e);
159
+ }
160
+ function L(t, n = {}) {
161
+ let { addDirAttribute: r, addSeoAttributes: i, hreflangBaseLanguage: a } = n, o = n.baseUrl ?? t.site?.origin ?? "", { base: s, defaultLocale: c, codes: l, strategy: u, path: d } = N(t), f = A(t), p = m(M(t)), h = p.find((e) => e.code === f);
162
+ if (!h) return {
425
163
  htmlAttrs: {},
426
164
  link: [],
427
165
  meta: []
428
166
  };
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
167
+ let g = (t) => v(o, e(u.switchLocalePath(d, t, l, c), s)), y = p.find((e) => e.code === c);
168
+ return _({
169
+ locale: h,
170
+ seoLocales: p.filter((e) => e.seo !== !1),
171
+ hrefOf: g,
172
+ canonical: v(o, e(d, s)),
173
+ xDefault: y && y.seo !== !1 ? g(c) : void 0,
174
+ addDirAttribute: r,
175
+ addSeoAttributes: i,
176
+ hreflangBaseLanguage: a
480
177
  });
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;
494
178
  }
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];
179
+ function R(e, t) {
180
+ let n = {};
181
+ for (let r of t) {
182
+ let t = c(e, r);
183
+ t != null && (n = f(n, r, t));
500
184
  }
501
- let a = r[r.length - 1];
502
- a !== void 0 && (i[a] = n);
185
+ return n;
503
186
  }
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);
187
+ var z = 2e4, B = /* @__PURE__ */ new WeakSet();
188
+ function V(e) {
189
+ try {
190
+ return JSON.stringify(e).length;
191
+ } catch {
192
+ return 0;
516
193
  }
194
+ }
195
+ function H(e) {
196
+ if (u() || B.has(e)) return;
197
+ B.add(e);
198
+ let t = V(e);
199
+ t > z && console.warn(`[i18n-micro] getI18nProps() without keys puts ${t} bytes in every island's props: pass the keys the island uses.`);
200
+ }
201
+ function U(e, t) {
202
+ let n = k(e), r = n.getRoute(), i = n.resolveTranslations(r);
203
+ t?.length || H(i);
204
+ let a = n.getLocaleIso();
517
205
  return {
518
- locale: r,
519
- fallbackLocale: i,
520
- currentRoute: a,
521
- translations: o
206
+ locale: A(e),
207
+ pluralLocale: n.pluralLocale(),
208
+ ...Object.keys(a).length > 0 && { localeIso: a },
209
+ fallbackLocale: n.getFallbackLocale(),
210
+ currentRoute: r,
211
+ translations: { [r]: t && t.length > 0 ? R(i, t) : i }
522
212
  };
523
213
  }
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 };
214
+ export { r as AstroI18n, o as FormatService, i as createAstroRouterAdapter, O as createI18n, a as createI18nMiddleware, s as defaultPlural, n as detectLocale, j as getDefaultLocale, k as getI18n, U as getI18nProps, A as getLocale, F as getLocaleRewritePath, M as getLocales, D as i18nIntegration, l as interpolate, d as localeIsoMap, I as prepareLocaleRewrite, P as useI18n, L as useLocaleHead };