@astroscope/i18n 0.1.0 → 0.2.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 +21 -1
- package/dist/index.d.ts +23 -0
- package/dist/index.js +4 -0
- package/package.json +8 -7
- package/dist/babel-plugin-FUTQRKLT.js +0 -121
- package/dist/chunk-22ZZFFC2.js +0 -159
- package/dist/chunk-2PSXD3JJ.js +0 -144
- package/dist/chunk-3VI62RLT.js +0 -176
- package/dist/chunk-4DAQPWXI.js +0 -36
- package/dist/chunk-4OGX6AF5.js +0 -10
- package/dist/chunk-4PHYKXO3.js +0 -227
- package/dist/chunk-4UBFISFY.js +0 -376
- package/dist/chunk-5DERF6DV.js +0 -266
- package/dist/chunk-5KGNHFTC.js +0 -344
- package/dist/chunk-5PP4D2VK.js +0 -23
- package/dist/chunk-6BW3PN23.js +0 -14
- package/dist/chunk-6ESQ4JQI.js +0 -263
- package/dist/chunk-A7MPXMOP.js +0 -202
- package/dist/chunk-AWZ2SQDW.js +0 -149
- package/dist/chunk-AYY2YIBQ.js +0 -52
- package/dist/chunk-BMNEJFTD.js +0 -56
- package/dist/chunk-BSXQGEUK.js +0 -16
- package/dist/chunk-BSYTJFGF.js +0 -21
- package/dist/chunk-BVPXEGTT.js +0 -12
- package/dist/chunk-C4DJYWF6.js +0 -193
- package/dist/chunk-C5EJYE2P.js +0 -245
- package/dist/chunk-CLNOQHMS.js +0 -254
- package/dist/chunk-E5MCIRSU.js +0 -144
- package/dist/chunk-E7ZJS7WX.js +0 -162
- package/dist/chunk-ESFGCYYH.js +0 -24
- package/dist/chunk-FHBVI3TL.js +0 -372
- package/dist/chunk-FI5KZ5IO.js +0 -17
- package/dist/chunk-H5R6YMOL.js +0 -233
- package/dist/chunk-H62K3FGA.js +0 -236
- package/dist/chunk-HDG2R6XT.js +0 -211
- package/dist/chunk-HGXU5QAK.js +0 -344
- package/dist/chunk-HHRMR3TM.js +0 -17
- package/dist/chunk-HS4HVBAD.js +0 -127
- package/dist/chunk-IPJWKKHJ.js +0 -324
- package/dist/chunk-ITTL4V7J.js +0 -24
- package/dist/chunk-IWUBCMOK.js +0 -128
- package/dist/chunk-JJBBY2GR.js +0 -14
- package/dist/chunk-JTUKXNIJ.js +0 -23
- package/dist/chunk-K7YULG4A.js +0 -14
- package/dist/chunk-KNONI6DP.js +0 -89
- package/dist/chunk-LMTEO22Y.js +0 -17
- package/dist/chunk-LRLZEX4D.js +0 -147
- package/dist/chunk-MFKPLUAN.js +0 -241
- package/dist/chunk-MIWXQOO3.js +0 -316
- package/dist/chunk-MJRTAWPR.js +0 -261
- package/dist/chunk-MZEPQCXX.js +0 -249
- package/dist/chunk-NHHTSFA7.js +0 -237
- package/dist/chunk-NTTXM7AG.js +0 -23
- package/dist/chunk-NZFAVJXR.js +0 -192
- package/dist/chunk-OFNFSDW3.js +0 -13
- package/dist/chunk-PG3XDNGF.js +0 -227
- package/dist/chunk-PVEVTPNK.js +0 -139
- package/dist/chunk-QAO4BXS4.js +0 -244
- package/dist/chunk-QCTULQQK.js +0 -34
- package/dist/chunk-QKND2KCA.js +0 -163
- package/dist/chunk-QZEGY6RJ.js +0 -151
- package/dist/chunk-RAE2L6FT.js +0 -193
- package/dist/chunk-S3ABJBU3.js +0 -255
- package/dist/chunk-SVDVCPCV.js +0 -236
- package/dist/chunk-T35ZUWTL.js +0 -153
- package/dist/chunk-T6WV5DW6.js +0 -156
- package/dist/chunk-TGUXVDA3.js +0 -82
- package/dist/chunk-TYMO7K3A.js +0 -173
- package/dist/chunk-V4RBFLQL.js +0 -23
- package/dist/chunk-V4XWJXXZ.js +0 -23
- package/dist/chunk-VQCY3OKK.js +0 -258
- package/dist/chunk-WLGOJPDJ.js +0 -15
- package/dist/chunk-WUEPDJIL.js +0 -259
- package/dist/chunk-X3L7WZLY.js +0 -254
- package/dist/chunk-XDD62ZM3.js +0 -255
- package/dist/chunk-XFXXEDRD.js +0 -36
- package/dist/chunk-XKYOR5UK.js +0 -8
- package/dist/chunk-Y26535SI.js +0 -239
- package/dist/chunk-Y3PQX5TX.js +0 -152
- package/dist/chunk-Y4BE2V6F.js +0 -347
- package/dist/chunk-Z4IZYDIA.js +0 -24
- package/dist/chunk-Z52G4CYY.js +0 -14
- package/dist/chunk-ZGLUYWVM.js +0 -128
- package/dist/client/import-with-i18n.d.ts +0 -16
- package/dist/client/import-with-i18n.js +0 -8
- package/dist/client-directives/idle.d.ts +0 -9
- package/dist/client-directives/idle.js +0 -15
- package/dist/client-directives/load.d.ts +0 -9
- package/dist/client-directives/load.js +0 -15
- package/dist/client-directives/media.d.ts +0 -9
- package/dist/client-directives/media.js +0 -15
- package/dist/client-directives/only.d.ts +0 -9
- package/dist/client-directives/only.js +0 -15
- package/dist/client-directives/utils.d.ts +0 -9
- package/dist/client-directives/utils.js +0 -7
- package/dist/client-directives/visible.d.ts +0 -9
- package/dist/client-directives/visible.js +0 -15
- package/dist/i18n-3cZHoHwP.d.ts +0 -86
- package/dist/i18n-BHU81LMA.d.ts +0 -55
- package/dist/i18n-BagoeH4_.d.ts +0 -62
- package/dist/i18n-BeL84090.d.ts +0 -77
- package/dist/i18n-BeRWDfHi.d.ts +0 -55
- package/dist/i18n-Bfs2PVcf.d.ts +0 -79
- package/dist/i18n-BrL7p3aN.d.ts +0 -99
- package/dist/i18n-CNQNCFU0.d.ts +0 -55
- package/dist/i18n-CX_uU8V2.d.ts +0 -80
- package/dist/i18n-Cr1QZCwe.d.ts +0 -64
- package/dist/i18n-CzTRxGeX.d.ts +0 -80
- package/dist/i18n-DUOFiC7g.d.ts +0 -77
- package/dist/i18n-DcF47BHS.d.ts +0 -77
- package/dist/i18n-DifWvtaO.d.ts +0 -60
- package/dist/i18n-DumSqX1t.d.ts +0 -55
- package/dist/i18n-fWTJw12h.d.ts +0 -80
- package/dist/import-with-i18n.d.ts +0 -23
- package/dist/import-with-i18n.js +0 -7
- package/dist/init.d.ts +0 -2
- package/dist/init.js +0 -8
- package/dist/middleware-entrypoint-C0Drh9E_.d.ts +0 -29
- package/dist/middleware-entrypoint-mEl66IUy.d.ts +0 -29
- package/dist/middleware-entrypoint.d.ts +0 -5
- package/dist/middleware-entrypoint.js +0 -30
- package/dist/server/middleware-entrypoint.d.ts +0 -2
- package/dist/server/middleware-entrypoint.js +0 -28
- package/dist/t-BY_C9lZ1.d.ts +0 -30
- package/dist/t-CG3yDiEa.d.ts +0 -30
- package/dist/t-CHsBfrPr.d.ts +0 -30
- package/dist/t-DQpXaDhP.d.ts +0 -30
- package/dist/t.client.d.ts +0 -11
- package/dist/t.client.js +0 -35
- package/dist/t.server.d.ts +0 -22
- package/dist/t.server.js +0 -12
- package/dist/types-BC6K-pID.d.ts +0 -110
- package/dist/types-BUV7BZML.d.ts +0 -55
- package/dist/types-BWGB322F.d.ts +0 -46
- package/dist/types-BaaSiz9n.d.ts +0 -7
- package/dist/types-Bgl2V5xo.d.ts +0 -102
- package/dist/types-BwhB6IT4.d.ts +0 -111
- package/dist/types-C6rOJFY2.d.ts +0 -24
- package/dist/types-C7lD-Woj.d.ts +0 -50
- package/dist/types-CLWZeQhD.d.ts +0 -54
- package/dist/types-COXt613w.d.ts +0 -19
- package/dist/types-CUlkbl9-.d.ts +0 -50
- package/dist/types-CVv7GGPj.d.ts +0 -46
- package/dist/types-CiIDN2sW.d.ts +0 -64
- package/dist/types-Cr86qjYl.d.ts +0 -58
- package/dist/types-CxnObiEO.d.ts +0 -7
- package/dist/types-D44mFUjA.d.ts +0 -97
- package/dist/types-D85m9UTC.d.ts +0 -137
- package/dist/types-DWhsmSPP.d.ts +0 -102
- package/dist/types-DdprXGJ1.d.ts +0 -46
- package/dist/types-DhVJ2ZIo.d.ts +0 -43
- package/dist/types-DnbOuM1L.d.ts +0 -7
- package/dist/types-DsNK_9sI.d.ts +0 -24
- package/dist/types-DvWBr9Xp.d.ts +0 -17
- package/dist/types-DwkIalh6.d.ts +0 -24
- package/dist/types-OYRgjtE_.d.ts +0 -110
- package/dist/types-SV8S05GX.d.ts +0 -7
- package/dist/types-TYn9WnxY.d.ts +0 -46
- package/dist/types-aXCBbdhq.d.ts +0 -55
- package/dist/types-gn4aNKGx.d.ts +0 -54
- package/dist/types-nippsMkm.d.ts +0 -54
package/dist/chunk-IPJWKKHJ.js
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
clearContext,
|
|
3
|
-
compileTranslations,
|
|
4
|
-
setContext
|
|
5
|
-
} from "./chunk-AYY2YIBQ.js";
|
|
6
|
-
|
|
7
|
-
// src/extraction/hash.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
|
-
|
|
34
|
-
// src/server/service.ts
|
|
35
|
-
var instance = null;
|
|
36
|
-
function createI18nService(config) {
|
|
37
|
-
if (instance) {
|
|
38
|
-
throw new Error("i18n service already created. Call createI18nService() only once.");
|
|
39
|
-
}
|
|
40
|
-
instance = new I18nServiceImpl(config);
|
|
41
|
-
return instance;
|
|
42
|
-
}
|
|
43
|
-
function getI18nService() {
|
|
44
|
-
if (!instance) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
"i18n service not initialized. Call createI18nService() before first request (e.g., in your boot file)."
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return instance;
|
|
50
|
-
}
|
|
51
|
-
function hasI18nService() {
|
|
52
|
-
return instance !== null;
|
|
53
|
-
}
|
|
54
|
-
var I18nServiceImpl = class {
|
|
55
|
-
config;
|
|
56
|
-
// raw translation cache per locale
|
|
57
|
-
rawCache = /* @__PURE__ */ new Map();
|
|
58
|
-
// compiled translation cache per locale
|
|
59
|
-
compiledCache = /* @__PURE__ */ new Map();
|
|
60
|
-
// hash cache per locale (for per-chunk loading)
|
|
61
|
-
hashCache = /* @__PURE__ */ new Map();
|
|
62
|
-
// chunk manifest from build
|
|
63
|
-
chunkManifest;
|
|
64
|
-
// track in-progress fetches to avoid duplicate requests
|
|
65
|
-
fetchInProgress = /* @__PURE__ */ new Map();
|
|
66
|
-
constructor(config) {
|
|
67
|
-
this.config = Object.freeze({ ...config });
|
|
68
|
-
}
|
|
69
|
-
invalidate(locales) {
|
|
70
|
-
const targets = this.resolveLocales(locales);
|
|
71
|
-
for (const locale of targets) {
|
|
72
|
-
this.rawCache.delete(locale);
|
|
73
|
-
this.compiledCache.delete(locale);
|
|
74
|
-
this.hashCache.delete(locale);
|
|
75
|
-
this.fetchInProgress.delete(locale);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async refresh(locales) {
|
|
79
|
-
const targets = this.resolveLocales(locales);
|
|
80
|
-
const fetches = targets.map(async (locale) => {
|
|
81
|
-
const raw = await this.fetchTranslations(locale);
|
|
82
|
-
return { locale, raw };
|
|
83
|
-
});
|
|
84
|
-
const results = await Promise.all(fetches);
|
|
85
|
-
for (const { locale, raw } of results) {
|
|
86
|
-
this.rawCache.set(locale, raw);
|
|
87
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
88
|
-
if (this.chunkManifest) {
|
|
89
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
async prefetch(locales) {
|
|
94
|
-
const targets = this.resolveLocales(locales);
|
|
95
|
-
await Promise.all(
|
|
96
|
-
targets.map(async (locale) => {
|
|
97
|
-
if (this.rawCache.has(locale)) return;
|
|
98
|
-
const raw = await this.fetchTranslations(locale);
|
|
99
|
-
this.rawCache.set(locale, raw);
|
|
100
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
101
|
-
if (this.chunkManifest) {
|
|
102
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
103
|
-
}
|
|
104
|
-
})
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
async getTranslations(locale) {
|
|
108
|
-
const cached = this.compiledCache.get(locale);
|
|
109
|
-
if (cached) return cached;
|
|
110
|
-
const raw = await this.ensureRawTranslations(locale);
|
|
111
|
-
const compiled = compileTranslations(locale, raw);
|
|
112
|
-
this.compiledCache.set(locale, compiled);
|
|
113
|
-
return compiled;
|
|
114
|
-
}
|
|
115
|
-
async getRawTranslations(locale) {
|
|
116
|
-
return this.ensureRawTranslations(locale);
|
|
117
|
-
}
|
|
118
|
-
getHashes(locale) {
|
|
119
|
-
return this.hashCache.get(locale);
|
|
120
|
-
}
|
|
121
|
-
setChunkManifest(manifest) {
|
|
122
|
-
this.chunkManifest = manifest;
|
|
123
|
-
if (manifest) {
|
|
124
|
-
for (const [locale, raw] of this.rawCache) {
|
|
125
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, manifest));
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
getChunkManifest() {
|
|
130
|
-
return this.chunkManifest;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Ensure raw translations are cached, fetching if needed
|
|
134
|
-
*/
|
|
135
|
-
async ensureRawTranslations(locale) {
|
|
136
|
-
const cached = this.rawCache.get(locale);
|
|
137
|
-
if (cached) return cached;
|
|
138
|
-
const raw = await this.fetchTranslations(locale);
|
|
139
|
-
this.rawCache.set(locale, raw);
|
|
140
|
-
if (this.chunkManifest) {
|
|
141
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
142
|
-
}
|
|
143
|
-
return raw;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Fetch translations, deduplicating concurrent requests
|
|
147
|
-
*/
|
|
148
|
-
async fetchTranslations(locale) {
|
|
149
|
-
const inProgress = this.fetchInProgress.get(locale);
|
|
150
|
-
if (inProgress) return inProgress;
|
|
151
|
-
const fetchPromise = this.config.fetch(locale, []).finally(() => {
|
|
152
|
-
this.fetchInProgress.delete(locale);
|
|
153
|
-
});
|
|
154
|
-
this.fetchInProgress.set(locale, fetchPromise);
|
|
155
|
-
return fetchPromise;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Resolve locale argument to array of locales
|
|
159
|
-
*/
|
|
160
|
-
resolveLocales(locales) {
|
|
161
|
-
if (!locales) return this.config.locales;
|
|
162
|
-
if (typeof locales === "string") return [locales];
|
|
163
|
-
return locales;
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
// src/server/middleware.ts
|
|
168
|
-
var I18N_ENDPOINT_PREFIX = "/_i18n/";
|
|
169
|
-
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
170
|
-
const url = new URL(request.url);
|
|
171
|
-
const localeParam = url.searchParams.get("locale");
|
|
172
|
-
if (localeParam && locales.includes(localeParam)) {
|
|
173
|
-
return localeParam;
|
|
174
|
-
}
|
|
175
|
-
const acceptLanguage = request.headers.get("accept-language");
|
|
176
|
-
if (acceptLanguage) {
|
|
177
|
-
const preferred = acceptLanguage.split(",").map((part) => {
|
|
178
|
-
const [lang, q = "q=1"] = part.trim().split(";");
|
|
179
|
-
return {
|
|
180
|
-
lang: lang.split("-")[0].toLowerCase(),
|
|
181
|
-
q: parseFloat(q.replace("q=", ""))
|
|
182
|
-
};
|
|
183
|
-
}).sort((a, b) => b.q - a.q);
|
|
184
|
-
for (const { lang } of preferred) {
|
|
185
|
-
if (locales.includes(lang)) {
|
|
186
|
-
return lang;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return defaultLocale;
|
|
191
|
-
}
|
|
192
|
-
function generateI18nHashScript(locale, hashes) {
|
|
193
|
-
const state = { locale, hashes, translations: {} };
|
|
194
|
-
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
195
|
-
}
|
|
196
|
-
function generateI18nFullScript(locale, raw) {
|
|
197
|
-
const state = { locale, hashes: {}, translations: raw };
|
|
198
|
-
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
199
|
-
}
|
|
200
|
-
async function injectI18nScript(response, script) {
|
|
201
|
-
const contentType = response.headers.get("content-type");
|
|
202
|
-
if (!contentType?.includes("text/html")) {
|
|
203
|
-
return response;
|
|
204
|
-
}
|
|
205
|
-
const html = await response.text();
|
|
206
|
-
let injectedHtml;
|
|
207
|
-
if (html.includes("</head>")) {
|
|
208
|
-
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
209
|
-
} else if (html.includes("<body")) {
|
|
210
|
-
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
211
|
-
} else {
|
|
212
|
-
injectedHtml = script + html;
|
|
213
|
-
}
|
|
214
|
-
return new Response(injectedHtml, {
|
|
215
|
-
status: response.status,
|
|
216
|
-
statusText: response.statusText,
|
|
217
|
-
headers: response.headers
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
function parseI18nEndpointPath(pathname) {
|
|
221
|
-
if (!pathname.startsWith(I18N_ENDPOINT_PREFIX)) {
|
|
222
|
-
return null;
|
|
223
|
-
}
|
|
224
|
-
const path = pathname.slice(I18N_ENDPOINT_PREFIX.length);
|
|
225
|
-
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
226
|
-
if (!match) {
|
|
227
|
-
return null;
|
|
228
|
-
}
|
|
229
|
-
return { locale: match[1], chunkName: match[2], hash: match[3] };
|
|
230
|
-
}
|
|
231
|
-
function handleI18nEndpoint(chunkName, requestedHash, translations, chunkManifest) {
|
|
232
|
-
const keys = chunkManifest[chunkName];
|
|
233
|
-
if (!keys) {
|
|
234
|
-
return new Response(`/* chunk not found: ${chunkName} */`, {
|
|
235
|
-
status: 404,
|
|
236
|
-
headers: { "Content-Type": "application/javascript" }
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
const actualHash = computeChunkHash(translations, keys);
|
|
240
|
-
if (requestedHash !== actualHash) {
|
|
241
|
-
console.warn(`[i18n] hash mismatch for ${chunkName}: expected ${actualHash}, got ${requestedHash}`);
|
|
242
|
-
}
|
|
243
|
-
const chunkTranslations = {};
|
|
244
|
-
for (const key of keys) {
|
|
245
|
-
if (translations[key]) {
|
|
246
|
-
chunkTranslations[key] = translations[key];
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
const js = `/* @astroscope/i18n chunk: ${chunkName} */
|
|
250
|
-
(function() {
|
|
251
|
-
var i = window.__i18n__;
|
|
252
|
-
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
253
|
-
})();
|
|
254
|
-
`;
|
|
255
|
-
return new Response(js, {
|
|
256
|
-
status: 200,
|
|
257
|
-
headers: {
|
|
258
|
-
"Content-Type": "application/javascript",
|
|
259
|
-
// immutable cache - hash in URL ensures cache busting
|
|
260
|
-
"Cache-Control": "public, max-age=31536000, immutable"
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
function createI18nMiddleware(options) {
|
|
265
|
-
const { locales, defaultLocale, fallback, detectLocale, chunkManifest } = options;
|
|
266
|
-
const service = getI18nService();
|
|
267
|
-
if (chunkManifest) {
|
|
268
|
-
service.setChunkManifest(chunkManifest);
|
|
269
|
-
}
|
|
270
|
-
return async (ctx, next) => {
|
|
271
|
-
const pathname = new URL(ctx.request.url).pathname;
|
|
272
|
-
const serviceChunkManifest = service.getChunkManifest();
|
|
273
|
-
if (serviceChunkManifest) {
|
|
274
|
-
const endpointParams = parseI18nEndpointPath(pathname);
|
|
275
|
-
if (endpointParams) {
|
|
276
|
-
const { locale: urlLocale, chunkName, hash } = endpointParams;
|
|
277
|
-
const endpointRaw = await service.getRawTranslations(urlLocale);
|
|
278
|
-
return handleI18nEndpoint(chunkName, hash, endpointRaw, serviceChunkManifest);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
282
|
-
const translations = await service.getTranslations(locale);
|
|
283
|
-
const raw = await service.getRawTranslations(locale);
|
|
284
|
-
const context = {
|
|
285
|
-
locale,
|
|
286
|
-
translations,
|
|
287
|
-
fallback,
|
|
288
|
-
meta: {}
|
|
289
|
-
};
|
|
290
|
-
setContext(context);
|
|
291
|
-
try {
|
|
292
|
-
const response = await next();
|
|
293
|
-
let script;
|
|
294
|
-
const hasChunkManifest = serviceChunkManifest && Object.keys(serviceChunkManifest).length > 0;
|
|
295
|
-
if (hasChunkManifest) {
|
|
296
|
-
const hashes = service.getHashes(locale) ?? {};
|
|
297
|
-
script = generateI18nHashScript(locale, hashes);
|
|
298
|
-
} else {
|
|
299
|
-
script = generateI18nFullScript(locale, raw);
|
|
300
|
-
}
|
|
301
|
-
const injectedResponse = await injectI18nScript(response, script);
|
|
302
|
-
return injectedResponse;
|
|
303
|
-
} finally {
|
|
304
|
-
clearContext();
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
function clearTranslationCache(locale) {
|
|
309
|
-
getI18nService().invalidate(locale);
|
|
310
|
-
}
|
|
311
|
-
async function getCachedTranslations(locale) {
|
|
312
|
-
return getI18nService().getRawTranslations(locale);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
export {
|
|
316
|
-
computeChunkHash,
|
|
317
|
-
computeAllChunkHashes,
|
|
318
|
-
createI18nService,
|
|
319
|
-
getI18nService,
|
|
320
|
-
hasI18nService,
|
|
321
|
-
createI18nMiddleware,
|
|
322
|
-
clearTranslationCache,
|
|
323
|
-
getCachedTranslations
|
|
324
|
-
};
|
package/dist/chunk-ITTL4V7J.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildI18nChunkUrl,
|
|
3
|
-
componentUrlToChunkName
|
|
4
|
-
} from "./chunk-LMTEO22Y.js";
|
|
5
|
-
|
|
6
|
-
// src/client/directives/utils.ts
|
|
7
|
-
async function loadI18nForChunk(el) {
|
|
8
|
-
const componentUrl = el.getAttribute("component-url");
|
|
9
|
-
if (!componentUrl) return;
|
|
10
|
-
const chunkName = componentUrlToChunkName(componentUrl);
|
|
11
|
-
const i18n = window.__i18n__;
|
|
12
|
-
const hash = i18n.hashes[chunkName];
|
|
13
|
-
if (hash) {
|
|
14
|
-
const url = buildI18nChunkUrl(i18n.locale, chunkName, hash);
|
|
15
|
-
await import(
|
|
16
|
-
/* @vite-ignore */
|
|
17
|
-
url
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export {
|
|
23
|
-
loadI18nForChunk
|
|
24
|
-
};
|
package/dist/chunk-IWUBCMOK.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
compileTranslations
|
|
3
|
-
} from "./chunk-KNDYKTHE.js";
|
|
4
|
-
|
|
5
|
-
// src/server/i18n.ts
|
|
6
|
-
import fs from "fs";
|
|
7
|
-
|
|
8
|
-
// src/extraction/hash.ts
|
|
9
|
-
import { createHash } from "crypto";
|
|
10
|
-
function hashString(str) {
|
|
11
|
-
return createHash("sha256").update(str).digest("hex").substring(0, 8);
|
|
12
|
-
}
|
|
13
|
-
function computeChunkHash(translations, keys) {
|
|
14
|
-
const relevantTranslations = {};
|
|
15
|
-
for (const key of keys.sort()) {
|
|
16
|
-
if (translations[key]) {
|
|
17
|
-
relevantTranslations[key] = translations[key];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return hashString(JSON.stringify(relevantTranslations));
|
|
21
|
-
}
|
|
22
|
-
function computeAllChunkHashes(translations, manifest) {
|
|
23
|
-
const hashes = {};
|
|
24
|
-
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
25
|
-
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
26
|
-
}
|
|
27
|
-
return hashes;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// src/server/i18n.ts
|
|
31
|
-
function loadExtractionManifest() {
|
|
32
|
-
const candidates = [
|
|
33
|
-
"./i18n-manifest.json",
|
|
34
|
-
// same directory
|
|
35
|
-
"../i18n-manifest.json"
|
|
36
|
-
// parent (when code is in chunks/)
|
|
37
|
-
];
|
|
38
|
-
for (const candidate of candidates) {
|
|
39
|
-
try {
|
|
40
|
-
const manifestPath = new URL(candidate, import.meta.url);
|
|
41
|
-
const content = fs.readFileSync(manifestPath, "utf-8");
|
|
42
|
-
return JSON.parse(content);
|
|
43
|
-
} catch {
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return void 0;
|
|
47
|
-
}
|
|
48
|
-
var I18nSingleton = class {
|
|
49
|
-
config = null;
|
|
50
|
-
rawCache = /* @__PURE__ */ new Map();
|
|
51
|
-
compiledCache = /* @__PURE__ */ new Map();
|
|
52
|
-
extractionManifest = void 0;
|
|
53
|
-
// undefined = not loaded yet
|
|
54
|
-
hashCache = /* @__PURE__ */ new Map();
|
|
55
|
-
configure(config) {
|
|
56
|
-
this.config = config;
|
|
57
|
-
}
|
|
58
|
-
getConfig() {
|
|
59
|
-
return this.config;
|
|
60
|
-
}
|
|
61
|
-
getDefaultLocale() {
|
|
62
|
-
return this.config?.defaultLocale ?? this.config?.locales[0];
|
|
63
|
-
}
|
|
64
|
-
getFallbackBehavior() {
|
|
65
|
-
return this.config?.fallback ?? "fallback";
|
|
66
|
-
}
|
|
67
|
-
setTranslations(locale, raw) {
|
|
68
|
-
this.rawCache.set(locale, raw);
|
|
69
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
70
|
-
this.hashCache.delete(locale);
|
|
71
|
-
const chunks = this.getChunkManifest();
|
|
72
|
-
if (chunks) {
|
|
73
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
getTranslations(locale) {
|
|
77
|
-
return this.rawCache.get(locale) ?? {};
|
|
78
|
-
}
|
|
79
|
-
getCompiledTranslations(locale) {
|
|
80
|
-
return this.compiledCache.get(locale) ?? {};
|
|
81
|
-
}
|
|
82
|
-
getChunkManifest() {
|
|
83
|
-
return this.getExtractionManifest()?.chunks ?? {};
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Get extracted translation keys with metadata.
|
|
87
|
-
* Use this to populate your CMS with keys extracted from the codebase.
|
|
88
|
-
*/
|
|
89
|
-
getExtractedKeys() {
|
|
90
|
-
return this.getExtractionManifest()?.keys ?? null;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Get the full extraction manifest (keys + chunks).
|
|
94
|
-
* Returns null in dev mode (manifest doesn't exist).
|
|
95
|
-
*/
|
|
96
|
-
getExtractionManifest() {
|
|
97
|
-
if (this.extractionManifest === void 0) {
|
|
98
|
-
this.extractionManifest = loadExtractionManifest() ?? null;
|
|
99
|
-
}
|
|
100
|
-
return this.extractionManifest;
|
|
101
|
-
}
|
|
102
|
-
getHashes(locale) {
|
|
103
|
-
return this.hashCache.get(locale) ?? {};
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Clear translations for specified locale(s).
|
|
107
|
-
* @param locales - Locale(s) to clear, or all if not specified
|
|
108
|
-
*/
|
|
109
|
-
clear(locales) {
|
|
110
|
-
const targets = this.resolveLocales(locales);
|
|
111
|
-
for (const locale of targets) {
|
|
112
|
-
this.rawCache.delete(locale);
|
|
113
|
-
this.compiledCache.delete(locale);
|
|
114
|
-
this.hashCache.delete(locale);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
resolveLocales(locales) {
|
|
118
|
-
if (!locales) return this.config?.locales ?? [];
|
|
119
|
-
if (typeof locales === "string") return [locales];
|
|
120
|
-
return locales;
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
var i18n = new I18nSingleton();
|
|
124
|
-
|
|
125
|
-
export {
|
|
126
|
-
computeChunkHash,
|
|
127
|
-
i18n
|
|
128
|
-
};
|
package/dist/chunk-JJBBY2GR.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// src/server/context.ts
|
|
2
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
3
|
-
var als = new AsyncLocalStorage();
|
|
4
|
-
function runWithContext(context, fn) {
|
|
5
|
-
return als.run(context, fn);
|
|
6
|
-
}
|
|
7
|
-
function getContext() {
|
|
8
|
-
return als.getStore() ?? null;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
runWithContext,
|
|
13
|
-
getContext
|
|
14
|
-
};
|
package/dist/chunk-JTUKXNIJ.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildI18nChunkUrl,
|
|
3
|
-
chunkIdToName
|
|
4
|
-
} from "./chunk-HHRMR3TM.js";
|
|
5
|
-
|
|
6
|
-
// src/client/import-with-i18n.ts
|
|
7
|
-
async function importWithI18n(importFn, chunkId) {
|
|
8
|
-
const i18n = window.__i18n__;
|
|
9
|
-
const chunkName = chunkId ? chunkIdToName(chunkId) : void 0;
|
|
10
|
-
const hash = chunkName ? i18n.hashes[chunkName] : void 0;
|
|
11
|
-
const [module] = await Promise.all([
|
|
12
|
-
importFn(),
|
|
13
|
-
hash && chunkName ? import(
|
|
14
|
-
/* @vite-ignore */
|
|
15
|
-
buildI18nChunkUrl(i18n.locale, chunkName, hash)
|
|
16
|
-
) : Promise.resolve()
|
|
17
|
-
]);
|
|
18
|
-
return module;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
importWithI18n
|
|
23
|
-
};
|
package/dist/chunk-K7YULG4A.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// src/shared/meta.ts
|
|
2
|
-
function isMeta(arg) {
|
|
3
|
-
if (typeof arg === "string") return true;
|
|
4
|
-
if (typeof arg === "object" && arg !== null && "fallback" in arg) return true;
|
|
5
|
-
return false;
|
|
6
|
-
}
|
|
7
|
-
function normalizeMeta(meta) {
|
|
8
|
-
return typeof meta === "string" ? { fallback: meta } : meta;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
isMeta,
|
|
13
|
-
normalizeMeta
|
|
14
|
-
};
|
package/dist/chunk-KNONI6DP.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// src/messageformat.ts
|
|
2
|
-
import MessageFormat from "@messageformat/core";
|
|
3
|
-
var mfCache = /* @__PURE__ */ new Map();
|
|
4
|
-
function getMessageFormat(locale) {
|
|
5
|
-
let mf = mfCache.get(locale);
|
|
6
|
-
if (!mf) {
|
|
7
|
-
mf = new MessageFormat(locale);
|
|
8
|
-
mfCache.set(locale, mf);
|
|
9
|
-
}
|
|
10
|
-
return mf;
|
|
11
|
-
}
|
|
12
|
-
function compileMessage(locale, message) {
|
|
13
|
-
const mf = getMessageFormat(locale);
|
|
14
|
-
try {
|
|
15
|
-
return mf.compile(message);
|
|
16
|
-
} catch {
|
|
17
|
-
return (values) => {
|
|
18
|
-
if (!values) return message;
|
|
19
|
-
return Object.entries(values).reduce(
|
|
20
|
-
(result, [key, value]) => result.replace(new RegExp(`\\{${key}\\}`, "g"), String(value)),
|
|
21
|
-
message
|
|
22
|
-
);
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function compileTranslations(locale, raw) {
|
|
27
|
-
const compiled = {};
|
|
28
|
-
for (const [key, message] of Object.entries(raw)) {
|
|
29
|
-
compiled[key] = compileMessage(locale, message);
|
|
30
|
-
}
|
|
31
|
-
return compiled;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// src/t.server.ts
|
|
35
|
-
var currentContext = null;
|
|
36
|
-
function setContext(context) {
|
|
37
|
-
currentContext = context;
|
|
38
|
-
}
|
|
39
|
-
function getContext() {
|
|
40
|
-
return currentContext;
|
|
41
|
-
}
|
|
42
|
-
function clearContext() {
|
|
43
|
-
currentContext = null;
|
|
44
|
-
}
|
|
45
|
-
function applyFallback(key, meta, fallback) {
|
|
46
|
-
if (typeof fallback === "function") {
|
|
47
|
-
return fallback(key, meta);
|
|
48
|
-
}
|
|
49
|
-
switch (fallback) {
|
|
50
|
-
case "key":
|
|
51
|
-
return key;
|
|
52
|
-
case "example":
|
|
53
|
-
return meta.example;
|
|
54
|
-
case "throw":
|
|
55
|
-
throw new Error(`Missing translation for key: ${key}`);
|
|
56
|
-
default:
|
|
57
|
-
return meta.example;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
var t = (key, meta, values) => {
|
|
61
|
-
const ctx = currentContext;
|
|
62
|
-
if (!ctx) {
|
|
63
|
-
return interpolateExample(meta.example, values);
|
|
64
|
-
}
|
|
65
|
-
ctx.meta[key] = meta;
|
|
66
|
-
const compiled = ctx.translations[key];
|
|
67
|
-
if (!compiled) {
|
|
68
|
-
const fallbackValue = applyFallback(key, meta, ctx.fallback);
|
|
69
|
-
const compiledFallback = compileMessage(ctx.locale, fallbackValue);
|
|
70
|
-
ctx.translations[key] = compiledFallback;
|
|
71
|
-
return compiledFallback(values);
|
|
72
|
-
}
|
|
73
|
-
return compiled(values);
|
|
74
|
-
};
|
|
75
|
-
function interpolateExample(example, values) {
|
|
76
|
-
if (!values) return example;
|
|
77
|
-
return Object.entries(values).reduce(
|
|
78
|
-
(result, [key, value]) => result.replace(new RegExp(`\\{${key}\\}`, "g"), String(value)),
|
|
79
|
-
example
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export {
|
|
84
|
-
compileTranslations,
|
|
85
|
-
setContext,
|
|
86
|
-
getContext,
|
|
87
|
-
clearContext,
|
|
88
|
-
t
|
|
89
|
-
};
|
package/dist/chunk-LMTEO22Y.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// src/extraction/url.ts
|
|
2
|
-
function chunkIdToName(chunkId) {
|
|
3
|
-
return chunkId.replace(/^_astro\//, "");
|
|
4
|
-
}
|
|
5
|
-
function componentUrlToChunkName(componentUrl) {
|
|
6
|
-
return componentUrl.replace(/^\/_astro\//, "").replace(/\.js$/, "");
|
|
7
|
-
}
|
|
8
|
-
function buildI18nChunkUrl(locale, chunkId, hash) {
|
|
9
|
-
const chunkName = chunkIdToName(chunkId);
|
|
10
|
-
return `/_i18n/${locale}/${chunkName}.${hash}.js`;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export {
|
|
14
|
-
chunkIdToName,
|
|
15
|
-
componentUrlToChunkName,
|
|
16
|
-
buildI18nChunkUrl
|
|
17
|
-
};
|