@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-E7ZJS7WX.js
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
compileTranslations
|
|
3
|
-
} from "./chunk-KNDYKTHE.js";
|
|
4
|
-
|
|
5
|
-
// src/extraction/hash.ts
|
|
6
|
-
import { createHash } from "crypto";
|
|
7
|
-
function hashString(str) {
|
|
8
|
-
return createHash("sha256").update(str).digest("hex").substring(0, 8);
|
|
9
|
-
}
|
|
10
|
-
function computeChunkHash(translations, keys) {
|
|
11
|
-
const relevantTranslations = {};
|
|
12
|
-
for (const key of keys.sort()) {
|
|
13
|
-
if (translations[key]) {
|
|
14
|
-
relevantTranslations[key] = translations[key];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return hashString(JSON.stringify(relevantTranslations));
|
|
18
|
-
}
|
|
19
|
-
function computeAllChunkHashes(translations, manifest) {
|
|
20
|
-
const hashes = {};
|
|
21
|
-
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
22
|
-
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
23
|
-
}
|
|
24
|
-
return hashes;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// src/server/i18n.ts
|
|
28
|
-
var I18nSingleton = class {
|
|
29
|
-
// normalized user config with defaults applied
|
|
30
|
-
config = null;
|
|
31
|
-
// locale -> raw translation strings (before ICU compilation)
|
|
32
|
-
rawCache = /* @__PURE__ */ new Map();
|
|
33
|
-
// locale -> compiled ICU MessageFormat functions
|
|
34
|
-
compiledCache = /* @__PURE__ */ new Map();
|
|
35
|
-
// locale -> chunk name -> content hash (for cache busting)
|
|
36
|
-
hashCache = /* @__PURE__ */ new Map();
|
|
37
|
-
// manifest getter (set by init module, provides live data in dev mode)
|
|
38
|
-
manifestGetter = null;
|
|
39
|
-
async configure(config) {
|
|
40
|
-
if (!config.locales?.length) {
|
|
41
|
-
throw new Error("i18n.configure(): locales array is required and must not be empty");
|
|
42
|
-
}
|
|
43
|
-
for (const locale of config.locales) {
|
|
44
|
-
if (!locale) {
|
|
45
|
-
throw new Error("i18n.configure(): locale cannot be empty");
|
|
46
|
-
}
|
|
47
|
-
if (locale !== locale.trim()) {
|
|
48
|
-
throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (new Set(config.locales).size !== config.locales.length) {
|
|
52
|
-
throw new Error("i18n.configure(): locales array contains duplicates");
|
|
53
|
-
}
|
|
54
|
-
if (config.defaultLocale !== void 0) {
|
|
55
|
-
if (!config.defaultLocale) {
|
|
56
|
-
throw new Error("i18n.configure(): defaultLocale cannot be empty");
|
|
57
|
-
}
|
|
58
|
-
if (config.defaultLocale !== config.defaultLocale.trim()) {
|
|
59
|
-
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
|
|
60
|
-
}
|
|
61
|
-
if (!config.locales.includes(config.defaultLocale)) {
|
|
62
|
-
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
this.config = {
|
|
66
|
-
locales: config.locales,
|
|
67
|
-
defaultLocale: config.defaultLocale ?? config.locales[0],
|
|
68
|
-
fallback: config.fallback ?? "fallback"
|
|
69
|
-
};
|
|
70
|
-
if (!this.manifestGetter) {
|
|
71
|
-
const m = await import("virtual:@astroscope/i18n/manifest");
|
|
72
|
-
this.manifestGetter = m.getManifest;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
isConfigured() {
|
|
76
|
-
return this.config !== null;
|
|
77
|
-
}
|
|
78
|
-
getConfig() {
|
|
79
|
-
if (!this.config) {
|
|
80
|
-
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
81
|
-
}
|
|
82
|
-
return this.config;
|
|
83
|
-
}
|
|
84
|
-
setTranslations(locale, raw) {
|
|
85
|
-
this.rawCache.set(locale, raw);
|
|
86
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
87
|
-
this.hashCache.delete(locale);
|
|
88
|
-
const { chunks } = this.getManifest();
|
|
89
|
-
if (Object.keys(chunks).length > 0) {
|
|
90
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
getTranslations(locale) {
|
|
94
|
-
const raw = this.rawCache.get(locale) ?? {};
|
|
95
|
-
const manifest = this.manifestGetter?.() ?? { keys: [], chunks: {} };
|
|
96
|
-
if (manifest.keys.length === 0) {
|
|
97
|
-
return raw;
|
|
98
|
-
}
|
|
99
|
-
const withFallbacks = { ...raw };
|
|
100
|
-
for (const { key, meta } of manifest.keys) {
|
|
101
|
-
if (!(key in withFallbacks) && meta.fallback) {
|
|
102
|
-
withFallbacks[key] = meta.fallback;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return withFallbacks;
|
|
106
|
-
}
|
|
107
|
-
getCompiledTranslations(locale) {
|
|
108
|
-
return compileTranslations(locale, this.getTranslations(locale));
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Get the extraction manifest (keys + chunks).
|
|
112
|
-
* - keys: translation keys extracted from codebase with metadata
|
|
113
|
-
* - chunks: mapping of chunk names to their translation keys
|
|
114
|
-
*/
|
|
115
|
-
getManifest() {
|
|
116
|
-
if (!this.manifestGetter) {
|
|
117
|
-
throw new Error("i18n manifest not initialized. Call i18n.configure() first.");
|
|
118
|
-
}
|
|
119
|
-
return this.manifestGetter();
|
|
120
|
-
}
|
|
121
|
-
getHashes(locale) {
|
|
122
|
-
return this.hashCache.get(locale) ?? {};
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Clear translations for specified locale(s).
|
|
126
|
-
* @param locales - Locale(s) to clear, or all if not specified
|
|
127
|
-
*/
|
|
128
|
-
clear(locales) {
|
|
129
|
-
const targets = this.resolveLocales(locales);
|
|
130
|
-
for (const locale of targets) {
|
|
131
|
-
this.rawCache.delete(locale);
|
|
132
|
-
this.compiledCache.delete(locale);
|
|
133
|
-
this.hashCache.delete(locale);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
resolveLocales(locales) {
|
|
137
|
-
if (!locales) {
|
|
138
|
-
return this.config?.locales ?? [];
|
|
139
|
-
}
|
|
140
|
-
if (typeof locales === "string") {
|
|
141
|
-
return [locales];
|
|
142
|
-
}
|
|
143
|
-
return locales;
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
var i18n = new I18nSingleton();
|
|
147
|
-
|
|
148
|
-
// src/server/context.ts
|
|
149
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
150
|
-
var als = new AsyncLocalStorage();
|
|
151
|
-
function runWithContext(context, fn) {
|
|
152
|
-
return als.run(context, fn);
|
|
153
|
-
}
|
|
154
|
-
function getContext() {
|
|
155
|
-
return als.getStore() ?? null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export {
|
|
159
|
-
i18n,
|
|
160
|
-
runWithContext,
|
|
161
|
-
getContext
|
|
162
|
-
};
|
package/dist/chunk-ESFGCYYH.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildI18nChunkUrl,
|
|
3
|
-
componentUrlToChunkName
|
|
4
|
-
} from "./chunk-FI5KZ5IO.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-FHBVI3TL.js
DELETED
|
@@ -1,372 +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 { manifest } = options;
|
|
35
|
-
const service = getI18nService();
|
|
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
|
-
const translations = await service.getRawTranslations(locale);
|
|
54
|
-
const actualHash = computeChunkHash(translations, keys);
|
|
55
|
-
if (requestedHash !== actualHash) {
|
|
56
|
-
console.warn(`[i18n] hash mismatch for ${chunkId}: expected ${actualHash}, got ${requestedHash}`);
|
|
57
|
-
}
|
|
58
|
-
const chunkTranslations = {};
|
|
59
|
-
for (const key of keys) {
|
|
60
|
-
if (translations[key]) {
|
|
61
|
-
chunkTranslations[key] = translations[key];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const js = `/* @astroscope/i18n chunk: ${chunkId} */
|
|
65
|
-
(function() {
|
|
66
|
-
var i = window.__i18n__;
|
|
67
|
-
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
68
|
-
})();
|
|
69
|
-
`;
|
|
70
|
-
return new Response(js, {
|
|
71
|
-
status: 200,
|
|
72
|
-
headers: {
|
|
73
|
-
"Content-Type": "application/javascript",
|
|
74
|
-
// immutable cache - hash in URL ensures cache busting
|
|
75
|
-
"Cache-Control": "public, max-age=31536000, immutable"
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// src/service.ts
|
|
82
|
-
var instance = null;
|
|
83
|
-
function createI18nService(config) {
|
|
84
|
-
if (instance) {
|
|
85
|
-
throw new Error("i18n service already created. Call createI18nService() only once.");
|
|
86
|
-
}
|
|
87
|
-
instance = new I18nServiceImpl(config);
|
|
88
|
-
return instance;
|
|
89
|
-
}
|
|
90
|
-
function getI18nService() {
|
|
91
|
-
if (!instance) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
"i18n service not initialized. Call createI18nService() before first request (e.g., in your boot file)."
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
return instance;
|
|
97
|
-
}
|
|
98
|
-
function hasI18nService() {
|
|
99
|
-
return instance !== null;
|
|
100
|
-
}
|
|
101
|
-
var I18nServiceImpl = class {
|
|
102
|
-
config;
|
|
103
|
-
// raw translation cache per locale
|
|
104
|
-
rawCache = /* @__PURE__ */ new Map();
|
|
105
|
-
// compiled translation cache per locale
|
|
106
|
-
compiledCache = /* @__PURE__ */ new Map();
|
|
107
|
-
// hash cache per locale (for per-chunk loading)
|
|
108
|
-
hashCache = /* @__PURE__ */ new Map();
|
|
109
|
-
// chunk manifest from build
|
|
110
|
-
chunkManifest;
|
|
111
|
-
// track in-progress fetches to avoid duplicate requests
|
|
112
|
-
fetchInProgress = /* @__PURE__ */ new Map();
|
|
113
|
-
constructor(config) {
|
|
114
|
-
this.config = Object.freeze({ ...config });
|
|
115
|
-
}
|
|
116
|
-
invalidate(locales) {
|
|
117
|
-
const targets = this.resolveLocales(locales);
|
|
118
|
-
for (const locale of targets) {
|
|
119
|
-
this.rawCache.delete(locale);
|
|
120
|
-
this.compiledCache.delete(locale);
|
|
121
|
-
this.hashCache.delete(locale);
|
|
122
|
-
this.fetchInProgress.delete(locale);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
async refresh(locales) {
|
|
126
|
-
const targets = this.resolveLocales(locales);
|
|
127
|
-
const fetches = targets.map(async (locale) => {
|
|
128
|
-
const raw = await this.fetchTranslations(locale);
|
|
129
|
-
return { locale, raw };
|
|
130
|
-
});
|
|
131
|
-
const results = await Promise.all(fetches);
|
|
132
|
-
for (const { locale, raw } of results) {
|
|
133
|
-
this.rawCache.set(locale, raw);
|
|
134
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
135
|
-
if (this.chunkManifest) {
|
|
136
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async prefetch(locales) {
|
|
141
|
-
const targets = this.resolveLocales(locales);
|
|
142
|
-
await Promise.all(
|
|
143
|
-
targets.map(async (locale) => {
|
|
144
|
-
if (this.rawCache.has(locale)) return;
|
|
145
|
-
const raw = await this.fetchTranslations(locale);
|
|
146
|
-
this.rawCache.set(locale, raw);
|
|
147
|
-
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
148
|
-
if (this.chunkManifest) {
|
|
149
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
150
|
-
}
|
|
151
|
-
})
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
async getTranslations(locale) {
|
|
155
|
-
const cached = this.compiledCache.get(locale);
|
|
156
|
-
if (cached) return cached;
|
|
157
|
-
const raw = await this.ensureRawTranslations(locale);
|
|
158
|
-
const compiled = compileTranslations(locale, raw);
|
|
159
|
-
this.compiledCache.set(locale, compiled);
|
|
160
|
-
return compiled;
|
|
161
|
-
}
|
|
162
|
-
async getRawTranslations(locale) {
|
|
163
|
-
return this.ensureRawTranslations(locale);
|
|
164
|
-
}
|
|
165
|
-
getHashes(locale) {
|
|
166
|
-
return this.hashCache.get(locale);
|
|
167
|
-
}
|
|
168
|
-
setChunkManifest(manifest) {
|
|
169
|
-
this.chunkManifest = manifest;
|
|
170
|
-
if (manifest) {
|
|
171
|
-
for (const [locale, raw] of this.rawCache) {
|
|
172
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, manifest));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
getChunkManifest() {
|
|
177
|
-
return this.chunkManifest;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Ensure raw translations are cached, fetching if needed
|
|
181
|
-
*/
|
|
182
|
-
async ensureRawTranslations(locale) {
|
|
183
|
-
const cached = this.rawCache.get(locale);
|
|
184
|
-
if (cached) return cached;
|
|
185
|
-
const raw = await this.fetchTranslations(locale);
|
|
186
|
-
this.rawCache.set(locale, raw);
|
|
187
|
-
if (this.chunkManifest) {
|
|
188
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, this.chunkManifest));
|
|
189
|
-
}
|
|
190
|
-
return raw;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Fetch translations, deduplicating concurrent requests
|
|
194
|
-
*/
|
|
195
|
-
async fetchTranslations(locale) {
|
|
196
|
-
const inProgress = this.fetchInProgress.get(locale);
|
|
197
|
-
if (inProgress) return inProgress;
|
|
198
|
-
const fetchPromise = this.config.fetch(locale, []).finally(() => {
|
|
199
|
-
this.fetchInProgress.delete(locale);
|
|
200
|
-
});
|
|
201
|
-
this.fetchInProgress.set(locale, fetchPromise);
|
|
202
|
-
return fetchPromise;
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Resolve locale argument to array of locales
|
|
206
|
-
*/
|
|
207
|
-
resolveLocales(locales) {
|
|
208
|
-
if (!locales) return this.config.locales;
|
|
209
|
-
if (typeof locales === "string") return [locales];
|
|
210
|
-
return locales;
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
// src/middleware.ts
|
|
215
|
-
var I18N_ENDPOINT_PREFIX = "/_i18n/";
|
|
216
|
-
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
217
|
-
const url = new URL(request.url);
|
|
218
|
-
const localeParam = url.searchParams.get("locale");
|
|
219
|
-
if (localeParam && locales.includes(localeParam)) {
|
|
220
|
-
return localeParam;
|
|
221
|
-
}
|
|
222
|
-
const acceptLanguage = request.headers.get("accept-language");
|
|
223
|
-
if (acceptLanguage) {
|
|
224
|
-
const preferred = acceptLanguage.split(",").map((part) => {
|
|
225
|
-
const [lang, q = "q=1"] = part.trim().split(";");
|
|
226
|
-
return {
|
|
227
|
-
lang: lang.split("-")[0].toLowerCase(),
|
|
228
|
-
q: parseFloat(q.replace("q=", ""))
|
|
229
|
-
};
|
|
230
|
-
}).sort((a, b) => b.q - a.q);
|
|
231
|
-
for (const { lang } of preferred) {
|
|
232
|
-
if (locales.includes(lang)) {
|
|
233
|
-
return lang;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return defaultLocale;
|
|
238
|
-
}
|
|
239
|
-
function generateI18nHashScript(locale, hashes) {
|
|
240
|
-
const state = { locale, hashes, translations: {} };
|
|
241
|
-
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
242
|
-
}
|
|
243
|
-
function generateI18nFullScript(locale, raw) {
|
|
244
|
-
const state = { locale, hashes: {}, translations: raw };
|
|
245
|
-
return `<script>window.__i18n__=${JSON.stringify(state)};</script>`;
|
|
246
|
-
}
|
|
247
|
-
async function injectI18nScript(response, script) {
|
|
248
|
-
const contentType = response.headers.get("content-type");
|
|
249
|
-
if (!contentType?.includes("text/html")) {
|
|
250
|
-
return response;
|
|
251
|
-
}
|
|
252
|
-
const html = await response.text();
|
|
253
|
-
let injectedHtml;
|
|
254
|
-
if (html.includes("</head>")) {
|
|
255
|
-
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
256
|
-
} else if (html.includes("<body")) {
|
|
257
|
-
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
258
|
-
} else {
|
|
259
|
-
injectedHtml = script + html;
|
|
260
|
-
}
|
|
261
|
-
return new Response(injectedHtml, {
|
|
262
|
-
status: response.status,
|
|
263
|
-
statusText: response.statusText,
|
|
264
|
-
headers: response.headers
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
function parseI18nEndpointPath(pathname) {
|
|
268
|
-
if (!pathname.startsWith(I18N_ENDPOINT_PREFIX)) {
|
|
269
|
-
return null;
|
|
270
|
-
}
|
|
271
|
-
const path = pathname.slice(I18N_ENDPOINT_PREFIX.length);
|
|
272
|
-
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
273
|
-
if (!match) {
|
|
274
|
-
return null;
|
|
275
|
-
}
|
|
276
|
-
return { locale: match[1], chunkName: match[2], hash: match[3] };
|
|
277
|
-
}
|
|
278
|
-
function handleI18nEndpoint(chunkName, requestedHash, translations, chunkManifest) {
|
|
279
|
-
const keys = chunkManifest[chunkName];
|
|
280
|
-
if (!keys) {
|
|
281
|
-
return new Response(`/* chunk not found: ${chunkName} */`, {
|
|
282
|
-
status: 404,
|
|
283
|
-
headers: { "Content-Type": "application/javascript" }
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
const actualHash = computeChunkHash(translations, keys);
|
|
287
|
-
if (requestedHash !== actualHash) {
|
|
288
|
-
console.warn(`[i18n] hash mismatch for ${chunkName}: expected ${actualHash}, got ${requestedHash}`);
|
|
289
|
-
}
|
|
290
|
-
const chunkTranslations = {};
|
|
291
|
-
for (const key of keys) {
|
|
292
|
-
if (translations[key]) {
|
|
293
|
-
chunkTranslations[key] = translations[key];
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
const js = `/* @astroscope/i18n chunk: ${chunkName} */
|
|
297
|
-
(function() {
|
|
298
|
-
var i = window.__i18n__;
|
|
299
|
-
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
300
|
-
})();
|
|
301
|
-
`;
|
|
302
|
-
return new Response(js, {
|
|
303
|
-
status: 200,
|
|
304
|
-
headers: {
|
|
305
|
-
"Content-Type": "application/javascript",
|
|
306
|
-
// immutable cache - hash in URL ensures cache busting
|
|
307
|
-
"Cache-Control": "public, max-age=31536000, immutable"
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
function createI18nMiddleware(options) {
|
|
312
|
-
const { locales, defaultLocale, fallback, detectLocale, chunkManifest } = options;
|
|
313
|
-
const service = getI18nService();
|
|
314
|
-
if (chunkManifest) {
|
|
315
|
-
service.setChunkManifest(chunkManifest);
|
|
316
|
-
}
|
|
317
|
-
return async (ctx, next) => {
|
|
318
|
-
const pathname = new URL(ctx.request.url).pathname;
|
|
319
|
-
const serviceChunkManifest = service.getChunkManifest();
|
|
320
|
-
if (serviceChunkManifest) {
|
|
321
|
-
const endpointParams = parseI18nEndpointPath(pathname);
|
|
322
|
-
if (endpointParams) {
|
|
323
|
-
const { locale: urlLocale, chunkName, hash } = endpointParams;
|
|
324
|
-
const endpointRaw = await service.getRawTranslations(urlLocale);
|
|
325
|
-
return handleI18nEndpoint(chunkName, hash, endpointRaw, serviceChunkManifest);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
329
|
-
const translations = await service.getTranslations(locale);
|
|
330
|
-
const raw = await service.getRawTranslations(locale);
|
|
331
|
-
const context = {
|
|
332
|
-
locale,
|
|
333
|
-
translations,
|
|
334
|
-
fallback,
|
|
335
|
-
meta: {}
|
|
336
|
-
};
|
|
337
|
-
setContext(context);
|
|
338
|
-
try {
|
|
339
|
-
const response = await next();
|
|
340
|
-
let script;
|
|
341
|
-
const hasChunkManifest = serviceChunkManifest && Object.keys(serviceChunkManifest).length > 0;
|
|
342
|
-
if (hasChunkManifest) {
|
|
343
|
-
const hashes = service.getHashes(locale) ?? {};
|
|
344
|
-
script = generateI18nHashScript(locale, hashes);
|
|
345
|
-
} else {
|
|
346
|
-
script = generateI18nFullScript(locale, raw);
|
|
347
|
-
}
|
|
348
|
-
const injectedResponse = await injectI18nScript(response, script);
|
|
349
|
-
return injectedResponse;
|
|
350
|
-
} finally {
|
|
351
|
-
clearContext();
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
}
|
|
355
|
-
function clearTranslationCache(locale) {
|
|
356
|
-
getI18nService().invalidate(locale);
|
|
357
|
-
}
|
|
358
|
-
async function getCachedTranslations(locale) {
|
|
359
|
-
return getI18nService().getRawTranslations(locale);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
export {
|
|
363
|
-
createI18nService,
|
|
364
|
-
getI18nService,
|
|
365
|
-
hasI18nService,
|
|
366
|
-
computeChunkHash,
|
|
367
|
-
computeAllChunkHashes,
|
|
368
|
-
createI18nEndpoint,
|
|
369
|
-
createI18nMiddleware,
|
|
370
|
-
clearTranslationCache,
|
|
371
|
-
getCachedTranslations
|
|
372
|
-
};
|
package/dist/chunk-FI5KZ5IO.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// src/i18n-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
|
-
};
|