@astroscope/i18n 0.1.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.
- package/README.md +262 -0
- package/dist/babel-plugin-FUTQRKLT.js +121 -0
- package/dist/babel-plugin-K2K4ZPM5.js +118 -0
- package/dist/chunk-22ZZFFC2.js +159 -0
- package/dist/chunk-2PSXD3JJ.js +144 -0
- package/dist/chunk-3VI62RLT.js +176 -0
- package/dist/chunk-4A5BKHFN.js +16 -0
- package/dist/chunk-4DAQPWXI.js +36 -0
- package/dist/chunk-4OGX6AF5.js +10 -0
- package/dist/chunk-4PHYKXO3.js +227 -0
- package/dist/chunk-4UBFISFY.js +376 -0
- package/dist/chunk-5DERF6DV.js +266 -0
- package/dist/chunk-5KGNHFTC.js +344 -0
- package/dist/chunk-5PP4D2VK.js +23 -0
- package/dist/chunk-6BW3PN23.js +14 -0
- package/dist/chunk-6ESQ4JQI.js +263 -0
- package/dist/chunk-A7MPXMOP.js +202 -0
- package/dist/chunk-AWZ2SQDW.js +149 -0
- package/dist/chunk-AYY2YIBQ.js +52 -0
- package/dist/chunk-BMNEJFTD.js +56 -0
- package/dist/chunk-BSXQGEUK.js +16 -0
- package/dist/chunk-BSYTJFGF.js +21 -0
- package/dist/chunk-BVPXEGTT.js +12 -0
- package/dist/chunk-C4DJYWF6.js +193 -0
- package/dist/chunk-C5EJYE2P.js +245 -0
- package/dist/chunk-CLNOQHMS.js +254 -0
- package/dist/chunk-E5MCIRSU.js +144 -0
- package/dist/chunk-E7ZJS7WX.js +162 -0
- package/dist/chunk-EMM6LRO2.js +8 -0
- package/dist/chunk-ESFGCYYH.js +24 -0
- package/dist/chunk-FAKBSLNW.js +24 -0
- package/dist/chunk-FHBVI3TL.js +372 -0
- package/dist/chunk-FI5KZ5IO.js +17 -0
- package/dist/chunk-H5R6YMOL.js +233 -0
- package/dist/chunk-H62K3FGA.js +236 -0
- package/dist/chunk-HDG2R6XT.js +211 -0
- package/dist/chunk-HGXU5QAK.js +344 -0
- package/dist/chunk-HHRMR3TM.js +17 -0
- package/dist/chunk-HS4HVBAD.js +127 -0
- package/dist/chunk-IPJWKKHJ.js +324 -0
- package/dist/chunk-ITTL4V7J.js +24 -0
- package/dist/chunk-IWUBCMOK.js +128 -0
- package/dist/chunk-JJBBY2GR.js +14 -0
- package/dist/chunk-JJEDOJBR.js +193 -0
- package/dist/chunk-JTUKXNIJ.js +23 -0
- package/dist/chunk-K7YULG4A.js +14 -0
- package/dist/chunk-KNDYKTHE.js +31 -0
- package/dist/chunk-KNONI6DP.js +89 -0
- package/dist/chunk-LMTEO22Y.js +17 -0
- package/dist/chunk-LRLZEX4D.js +147 -0
- package/dist/chunk-MFKPLUAN.js +241 -0
- package/dist/chunk-MIWXQOO3.js +316 -0
- package/dist/chunk-MJRTAWPR.js +261 -0
- package/dist/chunk-MZEPQCXX.js +249 -0
- package/dist/chunk-NHHTSFA7.js +237 -0
- package/dist/chunk-NTTXM7AG.js +23 -0
- package/dist/chunk-NZFAVJXR.js +192 -0
- package/dist/chunk-OFNFSDW3.js +13 -0
- package/dist/chunk-PG3XDNGF.js +227 -0
- package/dist/chunk-PVEVTPNK.js +139 -0
- package/dist/chunk-QAO4BXS4.js +244 -0
- package/dist/chunk-QCTULQQK.js +34 -0
- package/dist/chunk-QFAAGCP5.js +177 -0
- package/dist/chunk-QKND2KCA.js +163 -0
- package/dist/chunk-QZEGY6RJ.js +151 -0
- package/dist/chunk-RAE2L6FT.js +193 -0
- package/dist/chunk-S3ABJBU3.js +255 -0
- package/dist/chunk-SVDVCPCV.js +236 -0
- package/dist/chunk-T35ZUWTL.js +153 -0
- package/dist/chunk-T6WV5DW6.js +156 -0
- package/dist/chunk-TGUXVDA3.js +82 -0
- package/dist/chunk-TYMO7K3A.js +173 -0
- package/dist/chunk-V4RBFLQL.js +23 -0
- package/dist/chunk-V4XWJXXZ.js +23 -0
- package/dist/chunk-VQCY3OKK.js +258 -0
- package/dist/chunk-WLGOJPDJ.js +15 -0
- package/dist/chunk-WUEPDJIL.js +259 -0
- package/dist/chunk-X3L7WZLY.js +254 -0
- package/dist/chunk-XDD62ZM3.js +255 -0
- package/dist/chunk-XFXXEDRD.js +36 -0
- package/dist/chunk-XKYOR5UK.js +8 -0
- package/dist/chunk-Y26535SI.js +239 -0
- package/dist/chunk-Y3PQX5TX.js +152 -0
- package/dist/chunk-Y4BE2V6F.js +347 -0
- package/dist/chunk-Z4IZYDIA.js +24 -0
- package/dist/chunk-Z52G4CYY.js +14 -0
- package/dist/chunk-ZGLUYWVM.js +128 -0
- package/dist/client/directives/idle.d.ts +9 -0
- package/dist/client/directives/idle.js +15 -0
- package/dist/client/directives/load.d.ts +9 -0
- package/dist/client/directives/load.js +15 -0
- package/dist/client/directives/media.d.ts +9 -0
- package/dist/client/directives/media.js +15 -0
- package/dist/client/directives/only.d.ts +9 -0
- package/dist/client/directives/only.js +15 -0
- package/dist/client/directives/utils.d.ts +10 -0
- package/dist/client/directives/utils.js +7 -0
- package/dist/client/directives/visible.d.ts +9 -0
- package/dist/client/directives/visible.js +15 -0
- package/dist/client/import-with-i18n.d.ts +16 -0
- package/dist/client/import-with-i18n.js +8 -0
- package/dist/client/t.d.ts +20 -0
- package/dist/client/t.js +37 -0
- package/dist/client-directives/idle.d.ts +9 -0
- package/dist/client-directives/idle.js +15 -0
- package/dist/client-directives/load.d.ts +9 -0
- package/dist/client-directives/load.js +15 -0
- package/dist/client-directives/media.d.ts +9 -0
- package/dist/client-directives/media.js +15 -0
- package/dist/client-directives/only.d.ts +9 -0
- package/dist/client-directives/only.js +15 -0
- package/dist/client-directives/utils.d.ts +9 -0
- package/dist/client-directives/utils.js +7 -0
- package/dist/client-directives/visible.d.ts +9 -0
- package/dist/client-directives/visible.js +15 -0
- package/dist/extraction/index.d.ts +34 -0
- package/dist/extraction/index.js +9 -0
- package/dist/i18n-3cZHoHwP.d.ts +86 -0
- package/dist/i18n-BGfrSNM8.d.ts +56 -0
- package/dist/i18n-BHU81LMA.d.ts +55 -0
- package/dist/i18n-BagoeH4_.d.ts +62 -0
- package/dist/i18n-BeL84090.d.ts +77 -0
- package/dist/i18n-BeRWDfHi.d.ts +55 -0
- package/dist/i18n-Bfs2PVcf.d.ts +79 -0
- package/dist/i18n-BrL7p3aN.d.ts +99 -0
- package/dist/i18n-CNQNCFU0.d.ts +55 -0
- package/dist/i18n-CX_uU8V2.d.ts +80 -0
- package/dist/i18n-Cr1QZCwe.d.ts +64 -0
- package/dist/i18n-CzTRxGeX.d.ts +80 -0
- package/dist/i18n-DUOFiC7g.d.ts +77 -0
- package/dist/i18n-DcF47BHS.d.ts +77 -0
- package/dist/i18n-DifWvtaO.d.ts +60 -0
- package/dist/i18n-DumSqX1t.d.ts +55 -0
- package/dist/i18n-fWTJw12h.d.ts +80 -0
- package/dist/import-with-i18n.d.ts +23 -0
- package/dist/import-with-i18n.js +7 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +151 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.js +8 -0
- package/dist/internal.d.ts +11 -0
- package/dist/internal.js +9 -0
- package/dist/middleware-entrypoint-C0Drh9E_.d.ts +29 -0
- package/dist/middleware-entrypoint-mEl66IUy.d.ts +29 -0
- package/dist/middleware-entrypoint.d.ts +5 -0
- package/dist/middleware-entrypoint.js +30 -0
- package/dist/server/middleware-entrypoint.d.ts +2 -0
- package/dist/server/middleware-entrypoint.js +28 -0
- package/dist/server/t.d.ts +17 -0
- package/dist/server/t.js +59 -0
- package/dist/t-BY_C9lZ1.d.ts +30 -0
- package/dist/t-CG3yDiEa.d.ts +30 -0
- package/dist/t-CHsBfrPr.d.ts +30 -0
- package/dist/t-DQpXaDhP.d.ts +30 -0
- package/dist/t.client.d.ts +11 -0
- package/dist/t.client.js +35 -0
- package/dist/t.server.d.ts +22 -0
- package/dist/t.server.js +12 -0
- package/dist/types-8uKCwFda.d.ts +24 -0
- package/dist/types-BC6K-pID.d.ts +110 -0
- package/dist/types-BUV7BZML.d.ts +55 -0
- package/dist/types-BWGB322F.d.ts +46 -0
- package/dist/types-B_-RxW4A.d.ts +7 -0
- package/dist/types-BaaSiz9n.d.ts +7 -0
- package/dist/types-Bgl2V5xo.d.ts +102 -0
- package/dist/types-BwhB6IT4.d.ts +111 -0
- package/dist/types-Bzp6KxsI.d.ts +50 -0
- package/dist/types-C6rOJFY2.d.ts +24 -0
- package/dist/types-C7lD-Woj.d.ts +50 -0
- package/dist/types-CLWZeQhD.d.ts +54 -0
- package/dist/types-COXt613w.d.ts +19 -0
- package/dist/types-CUlkbl9-.d.ts +50 -0
- package/dist/types-CVv7GGPj.d.ts +46 -0
- package/dist/types-CiIDN2sW.d.ts +64 -0
- package/dist/types-Cr86qjYl.d.ts +58 -0
- package/dist/types-CxnObiEO.d.ts +7 -0
- package/dist/types-D44mFUjA.d.ts +97 -0
- package/dist/types-D85m9UTC.d.ts +137 -0
- package/dist/types-DWhsmSPP.d.ts +102 -0
- package/dist/types-DdprXGJ1.d.ts +46 -0
- package/dist/types-DhVJ2ZIo.d.ts +43 -0
- package/dist/types-DnbOuM1L.d.ts +7 -0
- package/dist/types-DsNK_9sI.d.ts +24 -0
- package/dist/types-DvWBr9Xp.d.ts +17 -0
- package/dist/types-DwkIalh6.d.ts +24 -0
- package/dist/types-OYRgjtE_.d.ts +110 -0
- package/dist/types-SV8S05GX.d.ts +7 -0
- package/dist/types-TYn9WnxY.d.ts +46 -0
- package/dist/types-aXCBbdhq.d.ts +55 -0
- package/dist/types-gn4aNKGx.d.ts +54 -0
- package/dist/types-nippsMkm.d.ts +54 -0
- package/package.json +105 -0
- package/src/components/I18nScript.astro +37 -0
- package/src/components/index.ts +2 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clearContext,
|
|
3
|
+
compileTranslations,
|
|
4
|
+
setContext
|
|
5
|
+
} from "./chunk-KNONI6DP.js";
|
|
6
|
+
|
|
7
|
+
// src/i18n-endpoint.ts
|
|
8
|
+
function hashString(str) {
|
|
9
|
+
let hash = 0;
|
|
10
|
+
for (let i = 0; i < str.length; i++) {
|
|
11
|
+
const char = str.charCodeAt(i);
|
|
12
|
+
hash = (hash << 5) - hash + char;
|
|
13
|
+
hash = hash & hash;
|
|
14
|
+
}
|
|
15
|
+
return Math.abs(hash).toString(36).substring(0, 8);
|
|
16
|
+
}
|
|
17
|
+
function computeChunkHash(translations, keys) {
|
|
18
|
+
const relevantTranslations = {};
|
|
19
|
+
for (const key of keys.sort()) {
|
|
20
|
+
if (translations[key]) {
|
|
21
|
+
relevantTranslations[key] = translations[key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return hashString(JSON.stringify(relevantTranslations));
|
|
25
|
+
}
|
|
26
|
+
function computeAllChunkHashes(translations, manifest) {
|
|
27
|
+
const hashes = {};
|
|
28
|
+
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
29
|
+
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
30
|
+
}
|
|
31
|
+
return hashes;
|
|
32
|
+
}
|
|
33
|
+
function createI18nEndpoint(options) {
|
|
34
|
+
const { cms, manifest } = options;
|
|
35
|
+
const translationCache2 = /* @__PURE__ */ new Map();
|
|
36
|
+
return async ({ params }) => {
|
|
37
|
+
const { path } = params;
|
|
38
|
+
if (!path) {
|
|
39
|
+
return new Response("Not found", { status: 404 });
|
|
40
|
+
}
|
|
41
|
+
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
42
|
+
if (!match) {
|
|
43
|
+
return new Response("Invalid path", { status: 400 });
|
|
44
|
+
}
|
|
45
|
+
const [, locale, chunkId, requestedHash] = match;
|
|
46
|
+
const keys = manifest[chunkId];
|
|
47
|
+
if (!keys) {
|
|
48
|
+
return new Response(`/* chunk not found: ${chunkId} */`, {
|
|
49
|
+
status: 404,
|
|
50
|
+
headers: { "Content-Type": "application/javascript" }
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
let translations = translationCache2.get(locale);
|
|
54
|
+
if (!translations) {
|
|
55
|
+
translations = await cms.fetch(locale, []);
|
|
56
|
+
translationCache2.set(locale, translations);
|
|
57
|
+
}
|
|
58
|
+
const actualHash = computeChunkHash(translations, keys);
|
|
59
|
+
if (requestedHash !== actualHash) {
|
|
60
|
+
console.warn(`[i18n] hash mismatch for ${chunkId}: expected ${actualHash}, got ${requestedHash}`);
|
|
61
|
+
}
|
|
62
|
+
const chunkTranslations = {};
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
if (translations[key]) {
|
|
65
|
+
chunkTranslations[key] = translations[key];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const js = `/* @astroscope/i18n chunk: ${chunkId} */
|
|
69
|
+
(function() {
|
|
70
|
+
var i = window.__i18n__;
|
|
71
|
+
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
72
|
+
})();
|
|
73
|
+
`;
|
|
74
|
+
return new Response(js, {
|
|
75
|
+
status: 200,
|
|
76
|
+
headers: {
|
|
77
|
+
"Content-Type": "application/javascript",
|
|
78
|
+
// immutable cache - hash in URL ensures cache busting
|
|
79
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/middleware.ts
|
|
86
|
+
var I18N_ENDPOINT_PREFIX = "/_i18n/";
|
|
87
|
+
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
88
|
+
const url = new URL(request.url);
|
|
89
|
+
const localeParam = url.searchParams.get("locale");
|
|
90
|
+
if (localeParam && locales.includes(localeParam)) {
|
|
91
|
+
return localeParam;
|
|
92
|
+
}
|
|
93
|
+
const acceptLanguage = request.headers.get("accept-language");
|
|
94
|
+
if (acceptLanguage) {
|
|
95
|
+
const preferred = acceptLanguage.split(",").map((part) => {
|
|
96
|
+
const [lang, q = "q=1"] = part.trim().split(";");
|
|
97
|
+
return {
|
|
98
|
+
lang: lang.split("-")[0].toLowerCase(),
|
|
99
|
+
q: parseFloat(q.replace("q=", ""))
|
|
100
|
+
};
|
|
101
|
+
}).sort((a, b) => b.q - a.q);
|
|
102
|
+
for (const { lang } of preferred) {
|
|
103
|
+
if (locales.includes(lang)) {
|
|
104
|
+
return lang;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return defaultLocale;
|
|
109
|
+
}
|
|
110
|
+
var translationCache = /* @__PURE__ */ new Map();
|
|
111
|
+
var hashCache = /* @__PURE__ */ new Map();
|
|
112
|
+
function generateI18nHashScript(locale, hashes) {
|
|
113
|
+
const state = { locale, hashes, translations: {} };
|
|
114
|
+
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
115
|
+
}
|
|
116
|
+
function generateI18nFullScript(locale, raw) {
|
|
117
|
+
const state = { locale, hashes: {}, translations: raw };
|
|
118
|
+
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
119
|
+
}
|
|
120
|
+
async function injectI18nScript(response, script) {
|
|
121
|
+
const contentType = response.headers.get("content-type");
|
|
122
|
+
if (!contentType?.includes("text/html")) {
|
|
123
|
+
return response;
|
|
124
|
+
}
|
|
125
|
+
const html = await response.text();
|
|
126
|
+
let injectedHtml;
|
|
127
|
+
if (html.includes("</head>")) {
|
|
128
|
+
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
129
|
+
} else if (html.includes("<body")) {
|
|
130
|
+
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
131
|
+
} else {
|
|
132
|
+
injectedHtml = script + html;
|
|
133
|
+
}
|
|
134
|
+
return new Response(injectedHtml, {
|
|
135
|
+
status: response.status,
|
|
136
|
+
statusText: response.statusText,
|
|
137
|
+
headers: response.headers
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function parseI18nEndpointPath(pathname) {
|
|
141
|
+
if (!pathname.startsWith(I18N_ENDPOINT_PREFIX)) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const path = pathname.slice(I18N_ENDPOINT_PREFIX.length);
|
|
145
|
+
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
146
|
+
if (!match) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
return { locale: match[1], chunkName: match[2], hash: match[3] };
|
|
150
|
+
}
|
|
151
|
+
function handleI18nEndpoint(chunkName, requestedHash, translations, chunkManifest) {
|
|
152
|
+
const keys = chunkManifest[chunkName];
|
|
153
|
+
if (!keys) {
|
|
154
|
+
return new Response(`/* chunk not found: ${chunkName} */`, {
|
|
155
|
+
status: 404,
|
|
156
|
+
headers: { "Content-Type": "application/javascript" }
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
const actualHash = computeChunkHash(translations, keys);
|
|
160
|
+
if (requestedHash !== actualHash) {
|
|
161
|
+
console.warn(`[i18n] hash mismatch for ${chunkName}: expected ${actualHash}, got ${requestedHash}`);
|
|
162
|
+
}
|
|
163
|
+
const chunkTranslations = {};
|
|
164
|
+
for (const key of keys) {
|
|
165
|
+
if (translations[key]) {
|
|
166
|
+
chunkTranslations[key] = translations[key];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const js = `/* @astroscope/i18n chunk: ${chunkName} */
|
|
170
|
+
(function() {
|
|
171
|
+
var i = window.__i18n__;
|
|
172
|
+
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
173
|
+
})();
|
|
174
|
+
`;
|
|
175
|
+
return new Response(js, {
|
|
176
|
+
status: 200,
|
|
177
|
+
headers: {
|
|
178
|
+
"Content-Type": "application/javascript",
|
|
179
|
+
// immutable cache - hash in URL ensures cache busting
|
|
180
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function createI18nMiddleware(options) {
|
|
185
|
+
const { locales, defaultLocale, fallback, cms, detectLocale, chunkManifest } = options;
|
|
186
|
+
return async (ctx, next) => {
|
|
187
|
+
const pathname = new URL(ctx.request.url).pathname;
|
|
188
|
+
if (chunkManifest) {
|
|
189
|
+
const endpointParams = parseI18nEndpointPath(pathname);
|
|
190
|
+
if (endpointParams) {
|
|
191
|
+
const { locale: urlLocale, chunkName, hash } = endpointParams;
|
|
192
|
+
let endpointRaw = translationCache.get(urlLocale);
|
|
193
|
+
if (!endpointRaw) {
|
|
194
|
+
endpointRaw = await cms.fetch(urlLocale, []);
|
|
195
|
+
translationCache.set(urlLocale, endpointRaw);
|
|
196
|
+
}
|
|
197
|
+
return handleI18nEndpoint(chunkName, hash, endpointRaw, chunkManifest);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
201
|
+
let raw = translationCache.get(locale);
|
|
202
|
+
if (!raw) {
|
|
203
|
+
raw = await cms.fetch(locale, []);
|
|
204
|
+
translationCache.set(locale, raw);
|
|
205
|
+
if (chunkManifest) {
|
|
206
|
+
const hashes = computeAllChunkHashes(raw, chunkManifest);
|
|
207
|
+
hashCache.set(locale, hashes);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const translations = compileTranslations(locale, raw);
|
|
211
|
+
const context = {
|
|
212
|
+
locale,
|
|
213
|
+
translations,
|
|
214
|
+
fallback,
|
|
215
|
+
meta: {}
|
|
216
|
+
};
|
|
217
|
+
setContext(context);
|
|
218
|
+
try {
|
|
219
|
+
const response = await next();
|
|
220
|
+
let script;
|
|
221
|
+
if (chunkManifest) {
|
|
222
|
+
const hashes = hashCache.get(locale) || {};
|
|
223
|
+
script = generateI18nHashScript(locale, hashes);
|
|
224
|
+
} else {
|
|
225
|
+
script = generateI18nFullScript(locale, raw);
|
|
226
|
+
}
|
|
227
|
+
const injectedResponse = await injectI18nScript(response, script);
|
|
228
|
+
return injectedResponse;
|
|
229
|
+
} finally {
|
|
230
|
+
clearContext();
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function getCachedTranslations(locale) {
|
|
235
|
+
return translationCache.get(locale);
|
|
236
|
+
}
|
|
237
|
+
function clearTranslationCache(locale) {
|
|
238
|
+
if (locale) {
|
|
239
|
+
translationCache.delete(locale);
|
|
240
|
+
hashCache.delete(locale);
|
|
241
|
+
} else {
|
|
242
|
+
translationCache.clear();
|
|
243
|
+
hashCache.clear();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export {
|
|
248
|
+
computeChunkHash,
|
|
249
|
+
computeAllChunkHashes,
|
|
250
|
+
createI18nEndpoint,
|
|
251
|
+
createI18nMiddleware,
|
|
252
|
+
getCachedTranslations,
|
|
253
|
+
clearTranslationCache
|
|
254
|
+
};
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clearContext,
|
|
3
|
+
compileTranslations,
|
|
4
|
+
setContext
|
|
5
|
+
} from "./chunk-KNONI6DP.js";
|
|
6
|
+
|
|
7
|
+
// src/i18n-endpoint.ts
|
|
8
|
+
function hashString(str) {
|
|
9
|
+
let hash = 0;
|
|
10
|
+
for (let i = 0; i < str.length; i++) {
|
|
11
|
+
const char = str.charCodeAt(i);
|
|
12
|
+
hash = (hash << 5) - hash + char;
|
|
13
|
+
hash = hash & hash;
|
|
14
|
+
}
|
|
15
|
+
return Math.abs(hash).toString(36).substring(0, 8);
|
|
16
|
+
}
|
|
17
|
+
function computeChunkHash(translations, keys) {
|
|
18
|
+
const relevantTranslations = {};
|
|
19
|
+
for (const key of keys.sort()) {
|
|
20
|
+
if (translations[key]) {
|
|
21
|
+
relevantTranslations[key] = translations[key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return hashString(JSON.stringify(relevantTranslations));
|
|
25
|
+
}
|
|
26
|
+
function computeAllChunkHashes(translations, manifest) {
|
|
27
|
+
const hashes = {};
|
|
28
|
+
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
29
|
+
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
30
|
+
}
|
|
31
|
+
return hashes;
|
|
32
|
+
}
|
|
33
|
+
function createI18nEndpoint(options) {
|
|
34
|
+
const { cms, manifest } = options;
|
|
35
|
+
const translationCache2 = /* @__PURE__ */ new Map();
|
|
36
|
+
return async ({ params }) => {
|
|
37
|
+
const { path } = params;
|
|
38
|
+
if (!path) {
|
|
39
|
+
return new Response("Not found", { status: 404 });
|
|
40
|
+
}
|
|
41
|
+
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
42
|
+
if (!match) {
|
|
43
|
+
return new Response("Invalid path", { status: 400 });
|
|
44
|
+
}
|
|
45
|
+
const [, locale, chunkId, requestedHash] = match;
|
|
46
|
+
const keys = manifest[chunkId];
|
|
47
|
+
if (!keys) {
|
|
48
|
+
return new Response(`/* chunk not found: ${chunkId} */`, {
|
|
49
|
+
status: 404,
|
|
50
|
+
headers: { "Content-Type": "application/javascript" }
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
let translations = translationCache2.get(locale);
|
|
54
|
+
if (!translations) {
|
|
55
|
+
translations = await cms.fetch(locale, []);
|
|
56
|
+
translationCache2.set(locale, translations);
|
|
57
|
+
}
|
|
58
|
+
const actualHash = computeChunkHash(translations, keys);
|
|
59
|
+
if (requestedHash !== actualHash) {
|
|
60
|
+
console.warn(`[i18n] hash mismatch for ${chunkId}: expected ${actualHash}, got ${requestedHash}`);
|
|
61
|
+
}
|
|
62
|
+
const chunkTranslations = {};
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
if (translations[key]) {
|
|
65
|
+
chunkTranslations[key] = translations[key];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const js = `/* @astroscope/i18n chunk: ${chunkId} */
|
|
69
|
+
(function() {
|
|
70
|
+
var i = window.__i18n__;
|
|
71
|
+
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
72
|
+
})();
|
|
73
|
+
`;
|
|
74
|
+
return new Response(js, {
|
|
75
|
+
status: 200,
|
|
76
|
+
headers: {
|
|
77
|
+
"Content-Type": "application/javascript",
|
|
78
|
+
// immutable cache - hash in URL ensures cache busting
|
|
79
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/middleware.ts
|
|
86
|
+
var I18N_ENDPOINT_PREFIX = "/_i18n/";
|
|
87
|
+
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
88
|
+
const url = new URL(request.url);
|
|
89
|
+
const localeParam = url.searchParams.get("locale");
|
|
90
|
+
if (localeParam && locales.includes(localeParam)) {
|
|
91
|
+
return localeParam;
|
|
92
|
+
}
|
|
93
|
+
const acceptLanguage = request.headers.get("accept-language");
|
|
94
|
+
if (acceptLanguage) {
|
|
95
|
+
const preferred = acceptLanguage.split(",").map((part) => {
|
|
96
|
+
const [lang, q = "q=1"] = part.trim().split(";");
|
|
97
|
+
return {
|
|
98
|
+
lang: lang.split("-")[0].toLowerCase(),
|
|
99
|
+
q: parseFloat(q.replace("q=", ""))
|
|
100
|
+
};
|
|
101
|
+
}).sort((a, b) => b.q - a.q);
|
|
102
|
+
for (const { lang } of preferred) {
|
|
103
|
+
if (locales.includes(lang)) {
|
|
104
|
+
return lang;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return defaultLocale;
|
|
109
|
+
}
|
|
110
|
+
var translationCache = /* @__PURE__ */ new Map();
|
|
111
|
+
var hashCache = /* @__PURE__ */ new Map();
|
|
112
|
+
function generateI18nHashScript(locale, hashes) {
|
|
113
|
+
const state = { locale, hashes, translations: {} };
|
|
114
|
+
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
115
|
+
}
|
|
116
|
+
function generateI18nFullScript(locale, raw) {
|
|
117
|
+
const state = { locale, hashes: {}, translations: raw };
|
|
118
|
+
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
119
|
+
}
|
|
120
|
+
async function injectI18nScript(response, script) {
|
|
121
|
+
const contentType = response.headers.get("content-type");
|
|
122
|
+
if (!contentType?.includes("text/html")) {
|
|
123
|
+
return response;
|
|
124
|
+
}
|
|
125
|
+
const html = await response.text();
|
|
126
|
+
let injectedHtml;
|
|
127
|
+
if (html.includes("</head>")) {
|
|
128
|
+
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
129
|
+
} else if (html.includes("<body")) {
|
|
130
|
+
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
131
|
+
} else {
|
|
132
|
+
injectedHtml = script + html;
|
|
133
|
+
}
|
|
134
|
+
return new Response(injectedHtml, {
|
|
135
|
+
status: response.status,
|
|
136
|
+
statusText: response.statusText,
|
|
137
|
+
headers: response.headers
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function parseI18nEndpointPath(pathname) {
|
|
141
|
+
if (!pathname.startsWith(I18N_ENDPOINT_PREFIX)) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const path = pathname.slice(I18N_ENDPOINT_PREFIX.length);
|
|
145
|
+
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
146
|
+
if (!match) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
return { locale: match[1], chunkName: match[2], hash: match[3] };
|
|
150
|
+
}
|
|
151
|
+
function handleI18nEndpoint(chunkName, requestedHash, translations, chunkManifest) {
|
|
152
|
+
const keys = chunkManifest[chunkName];
|
|
153
|
+
if (!keys) {
|
|
154
|
+
return new Response(`/* chunk not found: ${chunkName} */`, {
|
|
155
|
+
status: 404,
|
|
156
|
+
headers: { "Content-Type": "application/javascript" }
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
const actualHash = computeChunkHash(translations, keys);
|
|
160
|
+
if (requestedHash !== actualHash) {
|
|
161
|
+
console.warn(`[i18n] hash mismatch for ${chunkName}: expected ${actualHash}, got ${requestedHash}`);
|
|
162
|
+
}
|
|
163
|
+
const chunkTranslations = {};
|
|
164
|
+
for (const key of keys) {
|
|
165
|
+
if (translations[key]) {
|
|
166
|
+
chunkTranslations[key] = translations[key];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const js = `/* @astroscope/i18n chunk: ${chunkName} */
|
|
170
|
+
(function() {
|
|
171
|
+
var i = window.__i18n__;
|
|
172
|
+
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
173
|
+
})();
|
|
174
|
+
`;
|
|
175
|
+
return new Response(js, {
|
|
176
|
+
status: 200,
|
|
177
|
+
headers: {
|
|
178
|
+
"Content-Type": "application/javascript",
|
|
179
|
+
// immutable cache - hash in URL ensures cache busting
|
|
180
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function createI18nMiddleware(options) {
|
|
185
|
+
const { locales, defaultLocale, fallback, cms, detectLocale, chunkManifest } = options;
|
|
186
|
+
return async (ctx, next) => {
|
|
187
|
+
const pathname = new URL(ctx.request.url).pathname;
|
|
188
|
+
if (chunkManifest) {
|
|
189
|
+
const endpointParams = parseI18nEndpointPath(pathname);
|
|
190
|
+
if (endpointParams) {
|
|
191
|
+
const { locale: urlLocale, chunkName, hash } = endpointParams;
|
|
192
|
+
let endpointRaw = translationCache.get(urlLocale);
|
|
193
|
+
if (!endpointRaw) {
|
|
194
|
+
endpointRaw = await cms.fetch(urlLocale, []);
|
|
195
|
+
translationCache.set(urlLocale, endpointRaw);
|
|
196
|
+
}
|
|
197
|
+
return handleI18nEndpoint(chunkName, hash, endpointRaw, chunkManifest);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
201
|
+
let raw = translationCache.get(locale);
|
|
202
|
+
if (!raw) {
|
|
203
|
+
raw = await cms.fetch(locale, []);
|
|
204
|
+
translationCache.set(locale, raw);
|
|
205
|
+
if (chunkManifest) {
|
|
206
|
+
const hashes = computeAllChunkHashes(raw, chunkManifest);
|
|
207
|
+
hashCache.set(locale, hashes);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const translations = compileTranslations(locale, raw);
|
|
211
|
+
const context = {
|
|
212
|
+
locale,
|
|
213
|
+
translations,
|
|
214
|
+
fallback,
|
|
215
|
+
meta: {}
|
|
216
|
+
};
|
|
217
|
+
setContext(context);
|
|
218
|
+
try {
|
|
219
|
+
const response = await next();
|
|
220
|
+
let script;
|
|
221
|
+
const hasChunkManifest = chunkManifest && Object.keys(chunkManifest).length > 0;
|
|
222
|
+
if (hasChunkManifest) {
|
|
223
|
+
const hashes = hashCache.get(locale) || {};
|
|
224
|
+
script = generateI18nHashScript(locale, hashes);
|
|
225
|
+
} else {
|
|
226
|
+
script = generateI18nFullScript(locale, raw);
|
|
227
|
+
}
|
|
228
|
+
const injectedResponse = await injectI18nScript(response, script);
|
|
229
|
+
return injectedResponse;
|
|
230
|
+
} finally {
|
|
231
|
+
clearContext();
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function getCachedTranslations(locale) {
|
|
236
|
+
return translationCache.get(locale);
|
|
237
|
+
}
|
|
238
|
+
function clearTranslationCache(locale) {
|
|
239
|
+
if (locale) {
|
|
240
|
+
translationCache.delete(locale);
|
|
241
|
+
hashCache.delete(locale);
|
|
242
|
+
} else {
|
|
243
|
+
translationCache.clear();
|
|
244
|
+
hashCache.clear();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export {
|
|
249
|
+
computeChunkHash,
|
|
250
|
+
computeAllChunkHashes,
|
|
251
|
+
createI18nEndpoint,
|
|
252
|
+
createI18nMiddleware,
|
|
253
|
+
getCachedTranslations,
|
|
254
|
+
clearTranslationCache
|
|
255
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildI18nChunkUrl,
|
|
3
|
+
chunkIdToName
|
|
4
|
+
} from "./chunk-4A5BKHFN.js";
|
|
5
|
+
|
|
6
|
+
// src/client/import-with-i18n.ts
|
|
7
|
+
import { lazy } from "react";
|
|
8
|
+
function loadTranslations(chunkId) {
|
|
9
|
+
if (!chunkId) return Promise.resolve();
|
|
10
|
+
const i18n = window.__i18n__;
|
|
11
|
+
const chunkName = chunkIdToName(chunkId);
|
|
12
|
+
const hash = i18n.hashes[chunkName];
|
|
13
|
+
if (!hash) return Promise.resolve();
|
|
14
|
+
return import(
|
|
15
|
+
/* @vite-ignore */
|
|
16
|
+
buildI18nChunkUrl(i18n.locale, chunkName, hash)
|
|
17
|
+
).then(() => {
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async function importx(importFn, chunkId) {
|
|
21
|
+
const [module] = await Promise.all([importFn(), loadTranslations(chunkId)]);
|
|
22
|
+
return module;
|
|
23
|
+
}
|
|
24
|
+
function lazyx(importFn, chunkId) {
|
|
25
|
+
return lazy(async () => {
|
|
26
|
+
const translationPromise = loadTranslations(chunkId);
|
|
27
|
+
const modulePromise = importFn();
|
|
28
|
+
const [module] = await Promise.all([modulePromise, translationPromise]);
|
|
29
|
+
return module;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
importx,
|
|
35
|
+
lazyx
|
|
36
|
+
};
|