@astroscope/i18n 0.1.0 → 0.2.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/README.md +23 -1
- package/dist/{chunk-FAKBSLNW.js → chunk-7MSGHEST.js} +14 -7
- package/dist/{chunk-JJEDOJBR.js → chunk-EO4FUQXR.js} +49 -18
- package/dist/{chunk-QFAAGCP5.js → chunk-RHWEV4L2.js} +58 -1
- package/dist/client/directives/idle.d.ts +4 -1
- package/dist/client/directives/idle.js +13 -6
- package/dist/client/directives/load.d.ts +4 -1
- package/dist/client/directives/load.js +6 -6
- package/dist/client/directives/media.d.ts +4 -1
- package/dist/client/directives/media.js +16 -6
- package/dist/client/directives/only.d.ts +6 -1
- package/dist/client/directives/only.js +6 -6
- package/dist/client/directives/utils.d.ts +6 -5
- package/dist/client/directives/utils.js +3 -3
- package/dist/client/directives/visible.d.ts +4 -1
- package/dist/client/directives/visible.js +23 -6
- package/dist/client/t.d.ts +2 -2
- package/dist/extraction/index.d.ts +3 -3
- package/dist/extraction/index.js +1 -1
- package/dist/{i18n-BGfrSNM8.d.ts → i18n-mTHTcD8K.d.ts} +9 -2
- package/dist/index.d.ts +26 -3
- package/dist/index.js +6 -2
- package/dist/internal.d.ts +4 -4
- package/dist/internal.js +1 -1
- package/dist/server/t.d.ts +1 -1
- package/dist/server/t.js +1 -1
- package/dist/{types-BaaSiz9n.d.ts → types-BeDFfFIL.d.ts} +1 -1
- package/dist/{types-Bzp6KxsI.d.ts → types-DNRFj1XA.d.ts} +1 -0
- package/dist/{types-DsNK_9sI.d.ts → types-DTUho6gi.d.ts} +7 -1
- package/package.json +8 -7
- package/src/components/I18nScript.astro +2 -23
- 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-8uKCwFda.d.ts +0 -24
- 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-B_-RxW4A.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-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-4UBFISFY.js
DELETED
|
@@ -1,376 +0,0 @@
|
|
|
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 translationCache = /* @__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 = translationCache.get(locale);
|
|
54
|
-
if (!translations) {
|
|
55
|
-
translations = await cms.fetch(locale, []);
|
|
56
|
-
translationCache.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/service.ts
|
|
86
|
-
var instance = null;
|
|
87
|
-
function createI18nService(config) {
|
|
88
|
-
if (instance) {
|
|
89
|
-
throw new Error("i18n service already created. Call createI18nService() only once.");
|
|
90
|
-
}
|
|
91
|
-
instance = new I18nServiceImpl(config);
|
|
92
|
-
return instance;
|
|
93
|
-
}
|
|
94
|
-
function getI18nService() {
|
|
95
|
-
if (!instance) {
|
|
96
|
-
throw new Error(
|
|
97
|
-
"i18n service not initialized. Call createI18nService() before first request (e.g., in your boot file)."
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
return instance;
|
|
101
|
-
}
|
|
102
|
-
function hasI18nService() {
|
|
103
|
-
return instance !== null;
|
|
104
|
-
}
|
|
105
|
-
var I18nServiceImpl = class {
|
|
106
|
-
config;
|
|
107
|
-
// raw translation cache per locale
|
|
108
|
-
rawCache = /* @__PURE__ */ new Map();
|
|
109
|
-
// compiled translation cache per locale
|
|
110
|
-
compiledCache = /* @__PURE__ */ new Map();
|
|
111
|
-
// hash cache per locale (for per-chunk loading)
|
|
112
|
-
hashCache = /* @__PURE__ */ new Map();
|
|
113
|
-
// chunk manifest from build
|
|
114
|
-
chunkManifest;
|
|
115
|
-
// track in-progress fetches to avoid duplicate requests
|
|
116
|
-
fetchInProgress = /* @__PURE__ */ new Map();
|
|
117
|
-
constructor(config) {
|
|
118
|
-
this.config = Object.freeze({ ...config });
|
|
119
|
-
}
|
|
120
|
-
invalidate(locales) {
|
|
121
|
-
const targets = this.resolveLocales(locales);
|
|
122
|
-
for (const locale of targets) {
|
|
123
|
-
this.rawCache.delete(locale);
|
|
124
|
-
this.compiledCache.delete(locale);
|
|
125
|
-
this.hashCache.delete(locale);
|
|
126
|
-
this.fetchInProgress.delete(locale);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
async refresh(locales) {
|
|
130
|
-
const targets = this.resolveLocales(locales);
|
|
131
|
-
const fetches = targets.map(async (locale) => {
|
|
132
|
-
const raw = await this.fetchTranslations(locale);
|
|
133
|
-
return { locale, raw };
|
|
134
|
-
});
|
|
135
|
-
const results = await Promise.all(fetches);
|
|
136
|
-
for (const { locale, raw } of results) {
|
|
137
|
-
this.rawCache.set(locale, raw);
|
|
138
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
139
|
-
if (this.chunkManifest) {
|
|
140
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
async prefetch(locales) {
|
|
145
|
-
const targets = this.resolveLocales(locales);
|
|
146
|
-
await Promise.all(
|
|
147
|
-
targets.map(async (locale) => {
|
|
148
|
-
if (this.rawCache.has(locale)) return;
|
|
149
|
-
const raw = await this.fetchTranslations(locale);
|
|
150
|
-
this.rawCache.set(locale, raw);
|
|
151
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
152
|
-
if (this.chunkManifest) {
|
|
153
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
154
|
-
}
|
|
155
|
-
})
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
async getTranslations(locale) {
|
|
159
|
-
const cached = this.compiledCache.get(locale);
|
|
160
|
-
if (cached) return cached;
|
|
161
|
-
const raw = await this.ensureRawTranslations(locale);
|
|
162
|
-
const compiled = compileTranslations(locale, raw);
|
|
163
|
-
this.compiledCache.set(locale, compiled);
|
|
164
|
-
return compiled;
|
|
165
|
-
}
|
|
166
|
-
async getRawTranslations(locale) {
|
|
167
|
-
return this.ensureRawTranslations(locale);
|
|
168
|
-
}
|
|
169
|
-
getHashes(locale) {
|
|
170
|
-
return this.hashCache.get(locale);
|
|
171
|
-
}
|
|
172
|
-
setChunkManifest(manifest) {
|
|
173
|
-
this.chunkManifest = manifest;
|
|
174
|
-
if (manifest) {
|
|
175
|
-
for (const [locale, raw] of this.rawCache) {
|
|
176
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, manifest));
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
getChunkManifest() {
|
|
181
|
-
return this.chunkManifest;
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Ensure raw translations are cached, fetching if needed
|
|
185
|
-
*/
|
|
186
|
-
async ensureRawTranslations(locale) {
|
|
187
|
-
const cached = this.rawCache.get(locale);
|
|
188
|
-
if (cached) return cached;
|
|
189
|
-
const raw = await this.fetchTranslations(locale);
|
|
190
|
-
this.rawCache.set(locale, raw);
|
|
191
|
-
if (this.chunkManifest) {
|
|
192
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
193
|
-
}
|
|
194
|
-
return raw;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Fetch translations, deduplicating concurrent requests
|
|
198
|
-
*/
|
|
199
|
-
async fetchTranslations(locale) {
|
|
200
|
-
const inProgress = this.fetchInProgress.get(locale);
|
|
201
|
-
if (inProgress) return inProgress;
|
|
202
|
-
const fetchPromise = this.config.fetch(locale, []).finally(() => {
|
|
203
|
-
this.fetchInProgress.delete(locale);
|
|
204
|
-
});
|
|
205
|
-
this.fetchInProgress.set(locale, fetchPromise);
|
|
206
|
-
return fetchPromise;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Resolve locale argument to array of locales
|
|
210
|
-
*/
|
|
211
|
-
resolveLocales(locales) {
|
|
212
|
-
if (!locales) return this.config.locales;
|
|
213
|
-
if (typeof locales === "string") return [locales];
|
|
214
|
-
return locales;
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
// src/middleware.ts
|
|
219
|
-
var I18N_ENDPOINT_PREFIX = "/_i18n/";
|
|
220
|
-
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
221
|
-
const url = new URL(request.url);
|
|
222
|
-
const localeParam = url.searchParams.get("locale");
|
|
223
|
-
if (localeParam && locales.includes(localeParam)) {
|
|
224
|
-
return localeParam;
|
|
225
|
-
}
|
|
226
|
-
const acceptLanguage = request.headers.get("accept-language");
|
|
227
|
-
if (acceptLanguage) {
|
|
228
|
-
const preferred = acceptLanguage.split(",").map((part) => {
|
|
229
|
-
const [lang, q = "q=1"] = part.trim().split(";");
|
|
230
|
-
return {
|
|
231
|
-
lang: lang.split("-")[0].toLowerCase(),
|
|
232
|
-
q: parseFloat(q.replace("q=", ""))
|
|
233
|
-
};
|
|
234
|
-
}).sort((a, b) => b.q - a.q);
|
|
235
|
-
for (const { lang } of preferred) {
|
|
236
|
-
if (locales.includes(lang)) {
|
|
237
|
-
return lang;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return defaultLocale;
|
|
242
|
-
}
|
|
243
|
-
function generateI18nHashScript(locale, hashes) {
|
|
244
|
-
const state = { locale, hashes, translations: {} };
|
|
245
|
-
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
246
|
-
}
|
|
247
|
-
function generateI18nFullScript(locale, raw) {
|
|
248
|
-
const state = { locale, hashes: {}, translations: raw };
|
|
249
|
-
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
250
|
-
}
|
|
251
|
-
async function injectI18nScript(response, script) {
|
|
252
|
-
const contentType = response.headers.get("content-type");
|
|
253
|
-
if (!contentType?.includes("text/html")) {
|
|
254
|
-
return response;
|
|
255
|
-
}
|
|
256
|
-
const html = await response.text();
|
|
257
|
-
let injectedHtml;
|
|
258
|
-
if (html.includes("</head>")) {
|
|
259
|
-
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
260
|
-
} else if (html.includes("<body")) {
|
|
261
|
-
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
262
|
-
} else {
|
|
263
|
-
injectedHtml = script + html;
|
|
264
|
-
}
|
|
265
|
-
return new Response(injectedHtml, {
|
|
266
|
-
status: response.status,
|
|
267
|
-
statusText: response.statusText,
|
|
268
|
-
headers: response.headers
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
function parseI18nEndpointPath(pathname) {
|
|
272
|
-
if (!pathname.startsWith(I18N_ENDPOINT_PREFIX)) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
const path = pathname.slice(I18N_ENDPOINT_PREFIX.length);
|
|
276
|
-
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
277
|
-
if (!match) {
|
|
278
|
-
return null;
|
|
279
|
-
}
|
|
280
|
-
return { locale: match[1], chunkName: match[2], hash: match[3] };
|
|
281
|
-
}
|
|
282
|
-
function handleI18nEndpoint(chunkName, requestedHash, translations, chunkManifest) {
|
|
283
|
-
const keys = chunkManifest[chunkName];
|
|
284
|
-
if (!keys) {
|
|
285
|
-
return new Response(`/* chunk not found: ${chunkName} */`, {
|
|
286
|
-
status: 404,
|
|
287
|
-
headers: { "Content-Type": "application/javascript" }
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
const actualHash = computeChunkHash(translations, keys);
|
|
291
|
-
if (requestedHash !== actualHash) {
|
|
292
|
-
console.warn(`[i18n] hash mismatch for ${chunkName}: expected ${actualHash}, got ${requestedHash}`);
|
|
293
|
-
}
|
|
294
|
-
const chunkTranslations = {};
|
|
295
|
-
for (const key of keys) {
|
|
296
|
-
if (translations[key]) {
|
|
297
|
-
chunkTranslations[key] = translations[key];
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
const js = `/* @astroscope/i18n chunk: ${chunkName} */
|
|
301
|
-
(function() {
|
|
302
|
-
var i = window.__i18n__;
|
|
303
|
-
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
304
|
-
})();
|
|
305
|
-
`;
|
|
306
|
-
return new Response(js, {
|
|
307
|
-
status: 200,
|
|
308
|
-
headers: {
|
|
309
|
-
"Content-Type": "application/javascript",
|
|
310
|
-
// immutable cache - hash in URL ensures cache busting
|
|
311
|
-
"Cache-Control": "public, max-age=31536000, immutable"
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
function createI18nMiddleware(options) {
|
|
316
|
-
const { locales, defaultLocale, fallback, detectLocale, chunkManifest } = options;
|
|
317
|
-
const service = getI18nService();
|
|
318
|
-
if (chunkManifest) {
|
|
319
|
-
service.setChunkManifest(chunkManifest);
|
|
320
|
-
}
|
|
321
|
-
return async (ctx, next) => {
|
|
322
|
-
const pathname = new URL(ctx.request.url).pathname;
|
|
323
|
-
const serviceChunkManifest = service.getChunkManifest();
|
|
324
|
-
if (serviceChunkManifest) {
|
|
325
|
-
const endpointParams = parseI18nEndpointPath(pathname);
|
|
326
|
-
if (endpointParams) {
|
|
327
|
-
const { locale: urlLocale, chunkName, hash } = endpointParams;
|
|
328
|
-
const endpointRaw = await service.getRawTranslations(urlLocale);
|
|
329
|
-
return handleI18nEndpoint(chunkName, hash, endpointRaw, serviceChunkManifest);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
333
|
-
const translations = await service.getTranslations(locale);
|
|
334
|
-
const raw = await service.getRawTranslations(locale);
|
|
335
|
-
const context = {
|
|
336
|
-
locale,
|
|
337
|
-
translations,
|
|
338
|
-
fallback,
|
|
339
|
-
meta: {}
|
|
340
|
-
};
|
|
341
|
-
setContext(context);
|
|
342
|
-
try {
|
|
343
|
-
const response = await next();
|
|
344
|
-
let script;
|
|
345
|
-
const hasChunkManifest = serviceChunkManifest && Object.keys(serviceChunkManifest).length > 0;
|
|
346
|
-
if (hasChunkManifest) {
|
|
347
|
-
const hashes = service.getHashes(locale) ?? {};
|
|
348
|
-
script = generateI18nHashScript(locale, hashes);
|
|
349
|
-
} else {
|
|
350
|
-
script = generateI18nFullScript(locale, raw);
|
|
351
|
-
}
|
|
352
|
-
const injectedResponse = await injectI18nScript(response, script);
|
|
353
|
-
return injectedResponse;
|
|
354
|
-
} finally {
|
|
355
|
-
clearContext();
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
function clearTranslationCache(locale) {
|
|
360
|
-
getI18nService().invalidate(locale);
|
|
361
|
-
}
|
|
362
|
-
async function getCachedTranslations(locale) {
|
|
363
|
-
return getI18nService().getRawTranslations(locale);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
export {
|
|
367
|
-
computeChunkHash,
|
|
368
|
-
computeAllChunkHashes,
|
|
369
|
-
createI18nEndpoint,
|
|
370
|
-
createI18nService,
|
|
371
|
-
getI18nService,
|
|
372
|
-
hasI18nService,
|
|
373
|
-
createI18nMiddleware,
|
|
374
|
-
clearTranslationCache,
|
|
375
|
-
getCachedTranslations
|
|
376
|
-
};
|
package/dist/chunk-5DERF6DV.js
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
chunkIdToName
|
|
3
|
-
} from "./chunk-4A5BKHFN.js";
|
|
4
|
-
|
|
5
|
-
// src/extraction/vite-plugin.ts
|
|
6
|
-
import fs from "fs";
|
|
7
|
-
import path from "path";
|
|
8
|
-
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
9
|
-
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
10
|
-
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
11
|
-
var I18N_GLOBAL_KEY = "__astroscope_i18n_manifest__";
|
|
12
|
-
function getGlobalState() {
|
|
13
|
-
const g = globalThis;
|
|
14
|
-
if (!g[I18N_GLOBAL_KEY]) {
|
|
15
|
-
g[I18N_GLOBAL_KEY] = {
|
|
16
|
-
extractedKeys: [],
|
|
17
|
-
chunkManifest: {},
|
|
18
|
-
projectRoot: ""
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
return g[I18N_GLOBAL_KEY];
|
|
22
|
-
}
|
|
23
|
-
function getManifest() {
|
|
24
|
-
const state = getGlobalState();
|
|
25
|
-
const keysWithRelativePaths = state.extractedKeys.map((key) => ({
|
|
26
|
-
...key,
|
|
27
|
-
file: state.projectRoot ? path.relative(state.projectRoot, key.file) : key.file
|
|
28
|
-
}));
|
|
29
|
-
return {
|
|
30
|
-
keys: keysWithRelativePaths,
|
|
31
|
-
chunks: state.chunkManifest
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function extractBalancedBraces(code, startIndex) {
|
|
35
|
-
if (code[startIndex] !== "{") return null;
|
|
36
|
-
let depth = 0;
|
|
37
|
-
let i = startIndex;
|
|
38
|
-
while (i < code.length) {
|
|
39
|
-
if (code[i] === "{") depth++;
|
|
40
|
-
else if (code[i] === "}") {
|
|
41
|
-
depth--;
|
|
42
|
-
if (depth === 0) return code.slice(startIndex, i + 1);
|
|
43
|
-
}
|
|
44
|
-
i++;
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
function extractTranslationCalls(code, filename) {
|
|
49
|
-
const keys = [];
|
|
50
|
-
const regex = /\bt\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*/g;
|
|
51
|
-
let match;
|
|
52
|
-
while ((match = regex.exec(code)) !== null) {
|
|
53
|
-
const key = match[1];
|
|
54
|
-
const afterKey = code.slice(match.index + match[0].length);
|
|
55
|
-
const upToMatch = code.slice(0, match.index);
|
|
56
|
-
const lineNumber = (upToMatch.match(/\n/g) || []).length + 1;
|
|
57
|
-
try {
|
|
58
|
-
const stringMatch = afterKey.match(/^['"`]([^'"`]*)['"`]/);
|
|
59
|
-
if (stringMatch) {
|
|
60
|
-
keys.push({
|
|
61
|
-
key,
|
|
62
|
-
meta: { fallback: stringMatch[1] },
|
|
63
|
-
file: filename,
|
|
64
|
-
line: lineNumber
|
|
65
|
-
});
|
|
66
|
-
} else if (afterKey.startsWith("{")) {
|
|
67
|
-
const metaStr = extractBalancedBraces(afterKey, 0);
|
|
68
|
-
if (metaStr) {
|
|
69
|
-
const fallbackMatch = metaStr.match(/fallback\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
70
|
-
const descMatch = metaStr.match(/description\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
71
|
-
const fallback = fallbackMatch ? fallbackMatch[1] : "";
|
|
72
|
-
const description = descMatch ? descMatch[1] : void 0;
|
|
73
|
-
const varsBlockMatch = metaStr.match(/variables\s*:\s*\{/);
|
|
74
|
-
let variables;
|
|
75
|
-
if (varsBlockMatch) {
|
|
76
|
-
const varsStart = metaStr.indexOf("{", varsBlockMatch.index + varsBlockMatch[0].length - 1);
|
|
77
|
-
const varsStr = extractBalancedBraces(metaStr, varsStart);
|
|
78
|
-
if (varsStr) {
|
|
79
|
-
variables = parseVariables(varsStr);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
keys.push({
|
|
83
|
-
key,
|
|
84
|
-
meta: { fallback, description, variables },
|
|
85
|
-
file: filename,
|
|
86
|
-
line: lineNumber
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
} catch {
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return keys;
|
|
94
|
-
}
|
|
95
|
-
function parseVariables(str) {
|
|
96
|
-
const result = {};
|
|
97
|
-
const varNameRegex = /(\w+)\s*:\s*\{/g;
|
|
98
|
-
let nameMatch;
|
|
99
|
-
while ((nameMatch = varNameRegex.exec(str)) !== null) {
|
|
100
|
-
const varName = nameMatch[1];
|
|
101
|
-
const varStart = str.indexOf("{", nameMatch.index + nameMatch[0].length - 1);
|
|
102
|
-
const varObj = extractBalancedBraces(str, varStart);
|
|
103
|
-
if (varObj) {
|
|
104
|
-
const fallbackMatch = varObj.match(/fallback\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
105
|
-
const descMatch = varObj.match(/description\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
106
|
-
result[varName] = {
|
|
107
|
-
fallback: fallbackMatch ? fallbackMatch[1] : void 0,
|
|
108
|
-
description: descMatch ? descMatch[1] : void 0
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return Object.keys(result).length > 0 ? result : void 0;
|
|
113
|
-
}
|
|
114
|
-
function hasI18nImport(code) {
|
|
115
|
-
return /@astroscope\/i18n\/t/.test(code);
|
|
116
|
-
}
|
|
117
|
-
function i18nVitePlugin(options) {
|
|
118
|
-
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
119
|
-
const extractedKeys = [];
|
|
120
|
-
const fileToKeys = /* @__PURE__ */ new Map();
|
|
121
|
-
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
122
|
-
let chunkManifest = {};
|
|
123
|
-
let projectRoot = "";
|
|
124
|
-
let isBuild = false;
|
|
125
|
-
let isSSR = false;
|
|
126
|
-
return {
|
|
127
|
-
name: "@astroscope/i18n/extract",
|
|
128
|
-
enforce: "pre",
|
|
129
|
-
configResolved(config) {
|
|
130
|
-
projectRoot = config.root;
|
|
131
|
-
getGlobalState().projectRoot = config.root;
|
|
132
|
-
isBuild = config.command === "build";
|
|
133
|
-
isSSR = !!config.build.ssr;
|
|
134
|
-
},
|
|
135
|
-
resolveId(id) {
|
|
136
|
-
if (id === VIRTUAL_MODULE_ID) {
|
|
137
|
-
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
138
|
-
}
|
|
139
|
-
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
140
|
-
return `\0${id}`;
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
load(id) {
|
|
144
|
-
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
145
|
-
if (isBuild) {
|
|
146
|
-
return `
|
|
147
|
-
import { readFileSync, existsSync } from 'node:fs';
|
|
148
|
-
import { fileURLToPath } from 'node:url';
|
|
149
|
-
import { dirname, join } from 'node:path';
|
|
150
|
-
|
|
151
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
152
|
-
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
153
|
-
|
|
154
|
-
// manifest is emitted to server directory (not publicly accessible)
|
|
155
|
-
const searchPaths = [
|
|
156
|
-
join(__dirname, manifestName), // same dir (server/)
|
|
157
|
-
join(__dirname, '..', manifestName), // parent dir (from chunks/)
|
|
158
|
-
];
|
|
159
|
-
|
|
160
|
-
let manifestPath = searchPaths.find(p => existsSync(p));
|
|
161
|
-
if (!manifestPath) {
|
|
162
|
-
console.warn('[i18n] manifest not found, using empty manifest');
|
|
163
|
-
manifestPath = null;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const manifestJson = manifestPath
|
|
167
|
-
? JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
168
|
-
: { keys: [], chunks: {} };
|
|
169
|
-
|
|
170
|
-
export const manifest = manifestJson;
|
|
171
|
-
export function getManifest() { return manifestJson; }
|
|
172
|
-
`;
|
|
173
|
-
}
|
|
174
|
-
return `
|
|
175
|
-
import { getManifest as _getManifest } from '@astroscope/i18n/extraction';
|
|
176
|
-
export const manifest = { keys: [], chunks: {} }; // placeholder, use getManifest()
|
|
177
|
-
export function getManifest() { return _getManifest(); }
|
|
178
|
-
`;
|
|
179
|
-
}
|
|
180
|
-
},
|
|
181
|
-
transform(code, id) {
|
|
182
|
-
if (!include.some((ext) => id.endsWith(ext))) {
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
if (id.includes("node_modules")) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
if (hasI18nImport(code)) {
|
|
189
|
-
filesWithI18n.add(id);
|
|
190
|
-
}
|
|
191
|
-
const keys = extractTranslationCalls(code, id);
|
|
192
|
-
if (keys.length > 0) {
|
|
193
|
-
extractedKeys.push(...keys);
|
|
194
|
-
getGlobalState().extractedKeys = extractedKeys;
|
|
195
|
-
fileToKeys.set(
|
|
196
|
-
id,
|
|
197
|
-
keys.map((k) => k.key)
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
return null;
|
|
201
|
-
},
|
|
202
|
-
generateBundle(_, bundle) {
|
|
203
|
-
chunkManifest = {};
|
|
204
|
-
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
205
|
-
if (chunk.type === "chunk" && chunk.moduleIds) {
|
|
206
|
-
const keys = /* @__PURE__ */ new Set();
|
|
207
|
-
let hasI18nModule = false;
|
|
208
|
-
for (const moduleId of chunk.moduleIds) {
|
|
209
|
-
const moduleKeys = fileToKeys.get(moduleId);
|
|
210
|
-
if (moduleKeys) {
|
|
211
|
-
moduleKeys.forEach((k) => keys.add(k));
|
|
212
|
-
}
|
|
213
|
-
if (filesWithI18n.has(moduleId)) {
|
|
214
|
-
hasI18nModule = true;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
if (keys.size > 0) {
|
|
218
|
-
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
219
|
-
chunkManifest[chunkName] = Array.from(keys);
|
|
220
|
-
}
|
|
221
|
-
if (hasI18nModule && chunk.type === "chunk") {
|
|
222
|
-
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
223
|
-
const loaderCode = `
|
|
224
|
-
/* @astroscope/i18n loader */
|
|
225
|
-
if (typeof window !== 'undefined' && window.__i18n__) {
|
|
226
|
-
const __i18n__ = window.__i18n__;
|
|
227
|
-
const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
|
|
228
|
-
if (__i18n_hash__) {
|
|
229
|
-
await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
/* end @astroscope/i18n loader */
|
|
233
|
-
`;
|
|
234
|
-
chunk.code = loaderCode + chunk.code;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
getGlobalState().chunkManifest = chunkManifest;
|
|
239
|
-
const chunkCount = Object.keys(chunkManifest).length;
|
|
240
|
-
const keyCount = extractedKeys.length;
|
|
241
|
-
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
242
|
-
},
|
|
243
|
-
writeBundle(outputOptions) {
|
|
244
|
-
if (!isSSR && outputOptions.dir) {
|
|
245
|
-
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
246
|
-
...key,
|
|
247
|
-
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
248
|
-
}));
|
|
249
|
-
const manifest = {
|
|
250
|
-
keys: keysWithRelativePaths,
|
|
251
|
-
chunks: chunkManifest
|
|
252
|
-
};
|
|
253
|
-
const serverDir = path.resolve(outputOptions.dir, "..", "server");
|
|
254
|
-
const manifestPath = path.join(serverDir, MANIFEST_FILE_NAME);
|
|
255
|
-
if (fs.existsSync(serverDir)) {
|
|
256
|
-
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export {
|
|
264
|
-
getManifest,
|
|
265
|
-
i18nVitePlugin
|
|
266
|
-
};
|