@i18n-micro/astro 1.3.25 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +8 -64
- package/dist/auto-middleware.d.ts +1 -0
- package/dist/client/core.d.cts +86 -0
- package/dist/client/core.d.ts +71 -8
- package/dist/client/index.d.cts +2 -6
- package/dist/client/index.d.ts +2 -6
- package/dist/client/index.js +1 -6
- package/dist/client/preact.d.ts +12 -23
- package/dist/client/preact.js +13 -37
- package/dist/client/react.d.ts +14 -21
- package/dist/client/react.js +12 -37
- package/dist/client/svelte.d.ts +14 -14
- package/dist/client/svelte.js +17 -47
- package/dist/client/vue.d.ts +15 -15
- package/dist/client/vue.js +37 -2
- package/dist/composer.d.cts +16 -0
- package/dist/composer.d.ts +10 -30
- package/dist/core-CgrgdmGy.cjs +1 -0
- package/dist/core-DTAcn2yF.js +72 -0
- package/dist/env.d.ts +8 -2
- package/dist/index.cjs +11 -3
- package/dist/index.d.cts +10 -13
- package/dist/index.d.ts +10 -13
- package/dist/index.mjs +171 -482
- package/dist/integration.d.cts +26 -0
- package/dist/integration.d.ts +14 -15
- package/dist/middleware-BT3OA5YL.cjs +1 -0
- package/dist/middleware-Ch8E0iV_.js +257 -0
- package/dist/middleware.d.cts +34 -0
- package/dist/middleware.d.ts +17 -8
- package/dist/middleware.js +23 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +22 -0
- package/dist/request.d.ts +69 -0
- package/dist/router/adapter.d.cts +9 -0
- package/dist/router/adapter.d.ts +5 -4
- package/dist/router/types.d.cts +66 -0
- package/dist/router/types.d.ts +3 -2
- package/dist/utils.d.cts +112 -0
- package/dist/utils.d.ts +39 -35
- package/dist/virtual.d.ts +21 -0
- package/package.json +28 -15
- package/src/auto-middleware.ts +26 -0
- package/src/client/core.ts +124 -59
- package/src/client/index.ts +2 -11
- package/src/client/preact.tsx +12 -127
- package/src/client/react.tsx +11 -123
- package/src/client/svelte.ts +6 -69
- package/src/client/vue.ts +22 -77
- package/src/components/i18n-link.astro +2 -59
- package/src/components/i18n-switcher.astro +46 -149
- package/src/components/i18n-t.astro +6 -54
- package/src/composer.ts +25 -115
- package/src/env.d.ts +7 -0
- package/src/index.ts +1 -6
- package/src/integration.ts +113 -52
- package/src/middleware.ts +174 -120
- package/src/node.ts +51 -0
- package/src/request.ts +177 -0
- package/src/router/adapter.ts +47 -149
- package/src/router/types.ts +3 -2
- package/src/utils.ts +149 -323
- package/src/virtual.d.ts +21 -0
- package/dist/components/index.d.cts +0 -0
- package/dist/components/index.d.ts +0 -0
- package/dist/core-DwkpOV-H.cjs +0 -1
- package/dist/core-VSnkGRWR.js +0 -28
- package/dist/load-translations.d.ts +0 -43
- package/dist/routing-context.d.ts +0 -4
- package/dist/vue-BNiEwZr2.cjs +0 -5
- package/dist/vue-CWHjIOiS.js +0 -1724
- package/src/components/index.ts +0 -11
- package/src/load-translations.ts +0 -106
- package/src/routing-context.ts +0 -23
package/dist/index.mjs
CHANGED
|
@@ -1,525 +1,214 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
88
|
-
|
|
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
|
|
91
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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": (
|
|
101
|
-
let { updateConfig:
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
|
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
|
|
133
|
-
return new
|
|
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
|
-
|
|
214
|
-
|
|
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
|
|
344
|
-
return e.locals.locale
|
|
106
|
+
function A(e) {
|
|
107
|
+
return e.locals.locale ?? k(e).getLocale();
|
|
345
108
|
}
|
|
346
|
-
function
|
|
347
|
-
return e.locals.defaultLocale
|
|
109
|
+
function j(e) {
|
|
110
|
+
return e.locals.defaultLocale ?? k(e).getFallbackLocale();
|
|
348
111
|
}
|
|
349
|
-
function
|
|
112
|
+
function M(e) {
|
|
350
113
|
return e.locals.locales || [];
|
|
351
114
|
}
|
|
352
|
-
function
|
|
353
|
-
|
|
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
|
-
|
|
118
|
+
base: n,
|
|
359
119
|
defaultLocale: r,
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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
|
|
413
|
-
let
|
|
414
|
-
return
|
|
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
|
|
417
|
-
|
|
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
|
|
423
|
-
let {
|
|
424
|
-
if (!
|
|
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
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
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
|
|
496
|
-
let
|
|
497
|
-
for (let
|
|
498
|
-
let t = r
|
|
499
|
-
|
|
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
|
-
|
|
502
|
-
a !== void 0 && (i[a] = n);
|
|
185
|
+
return n;
|
|
503
186
|
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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:
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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
|
-
|
|
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 };
|