@astroscope/i18n 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +262 -0
- package/dist/babel-plugin-FUTQRKLT.js +121 -0
- package/dist/babel-plugin-K2K4ZPM5.js +118 -0
- package/dist/chunk-22ZZFFC2.js +159 -0
- package/dist/chunk-2PSXD3JJ.js +144 -0
- package/dist/chunk-3VI62RLT.js +176 -0
- package/dist/chunk-4A5BKHFN.js +16 -0
- package/dist/chunk-4DAQPWXI.js +36 -0
- package/dist/chunk-4OGX6AF5.js +10 -0
- package/dist/chunk-4PHYKXO3.js +227 -0
- package/dist/chunk-4UBFISFY.js +376 -0
- package/dist/chunk-5DERF6DV.js +266 -0
- package/dist/chunk-5KGNHFTC.js +344 -0
- package/dist/chunk-5PP4D2VK.js +23 -0
- package/dist/chunk-6BW3PN23.js +14 -0
- package/dist/chunk-6ESQ4JQI.js +263 -0
- package/dist/chunk-A7MPXMOP.js +202 -0
- package/dist/chunk-AWZ2SQDW.js +149 -0
- package/dist/chunk-AYY2YIBQ.js +52 -0
- package/dist/chunk-BMNEJFTD.js +56 -0
- package/dist/chunk-BSXQGEUK.js +16 -0
- package/dist/chunk-BSYTJFGF.js +21 -0
- package/dist/chunk-BVPXEGTT.js +12 -0
- package/dist/chunk-C4DJYWF6.js +193 -0
- package/dist/chunk-C5EJYE2P.js +245 -0
- package/dist/chunk-CLNOQHMS.js +254 -0
- package/dist/chunk-E5MCIRSU.js +144 -0
- package/dist/chunk-E7ZJS7WX.js +162 -0
- package/dist/chunk-EMM6LRO2.js +8 -0
- package/dist/chunk-ESFGCYYH.js +24 -0
- package/dist/chunk-FAKBSLNW.js +24 -0
- package/dist/chunk-FHBVI3TL.js +372 -0
- package/dist/chunk-FI5KZ5IO.js +17 -0
- package/dist/chunk-H5R6YMOL.js +233 -0
- package/dist/chunk-H62K3FGA.js +236 -0
- package/dist/chunk-HDG2R6XT.js +211 -0
- package/dist/chunk-HGXU5QAK.js +344 -0
- package/dist/chunk-HHRMR3TM.js +17 -0
- package/dist/chunk-HS4HVBAD.js +127 -0
- package/dist/chunk-IPJWKKHJ.js +324 -0
- package/dist/chunk-ITTL4V7J.js +24 -0
- package/dist/chunk-IWUBCMOK.js +128 -0
- package/dist/chunk-JJBBY2GR.js +14 -0
- package/dist/chunk-JJEDOJBR.js +193 -0
- package/dist/chunk-JTUKXNIJ.js +23 -0
- package/dist/chunk-K7YULG4A.js +14 -0
- package/dist/chunk-KNDYKTHE.js +31 -0
- package/dist/chunk-KNONI6DP.js +89 -0
- package/dist/chunk-LMTEO22Y.js +17 -0
- package/dist/chunk-LRLZEX4D.js +147 -0
- package/dist/chunk-MFKPLUAN.js +241 -0
- package/dist/chunk-MIWXQOO3.js +316 -0
- package/dist/chunk-MJRTAWPR.js +261 -0
- package/dist/chunk-MZEPQCXX.js +249 -0
- package/dist/chunk-NHHTSFA7.js +237 -0
- package/dist/chunk-NTTXM7AG.js +23 -0
- package/dist/chunk-NZFAVJXR.js +192 -0
- package/dist/chunk-OFNFSDW3.js +13 -0
- package/dist/chunk-PG3XDNGF.js +227 -0
- package/dist/chunk-PVEVTPNK.js +139 -0
- package/dist/chunk-QAO4BXS4.js +244 -0
- package/dist/chunk-QCTULQQK.js +34 -0
- package/dist/chunk-QFAAGCP5.js +177 -0
- package/dist/chunk-QKND2KCA.js +163 -0
- package/dist/chunk-QZEGY6RJ.js +151 -0
- package/dist/chunk-RAE2L6FT.js +193 -0
- package/dist/chunk-S3ABJBU3.js +255 -0
- package/dist/chunk-SVDVCPCV.js +236 -0
- package/dist/chunk-T35ZUWTL.js +153 -0
- package/dist/chunk-T6WV5DW6.js +156 -0
- package/dist/chunk-TGUXVDA3.js +82 -0
- package/dist/chunk-TYMO7K3A.js +173 -0
- package/dist/chunk-V4RBFLQL.js +23 -0
- package/dist/chunk-V4XWJXXZ.js +23 -0
- package/dist/chunk-VQCY3OKK.js +258 -0
- package/dist/chunk-WLGOJPDJ.js +15 -0
- package/dist/chunk-WUEPDJIL.js +259 -0
- package/dist/chunk-X3L7WZLY.js +254 -0
- package/dist/chunk-XDD62ZM3.js +255 -0
- package/dist/chunk-XFXXEDRD.js +36 -0
- package/dist/chunk-XKYOR5UK.js +8 -0
- package/dist/chunk-Y26535SI.js +239 -0
- package/dist/chunk-Y3PQX5TX.js +152 -0
- package/dist/chunk-Y4BE2V6F.js +347 -0
- package/dist/chunk-Z4IZYDIA.js +24 -0
- package/dist/chunk-Z52G4CYY.js +14 -0
- package/dist/chunk-ZGLUYWVM.js +128 -0
- package/dist/client/directives/idle.d.ts +9 -0
- package/dist/client/directives/idle.js +15 -0
- package/dist/client/directives/load.d.ts +9 -0
- package/dist/client/directives/load.js +15 -0
- package/dist/client/directives/media.d.ts +9 -0
- package/dist/client/directives/media.js +15 -0
- package/dist/client/directives/only.d.ts +9 -0
- package/dist/client/directives/only.js +15 -0
- package/dist/client/directives/utils.d.ts +10 -0
- package/dist/client/directives/utils.js +7 -0
- package/dist/client/directives/visible.d.ts +9 -0
- package/dist/client/directives/visible.js +15 -0
- package/dist/client/import-with-i18n.d.ts +16 -0
- package/dist/client/import-with-i18n.js +8 -0
- package/dist/client/t.d.ts +20 -0
- package/dist/client/t.js +37 -0
- package/dist/client-directives/idle.d.ts +9 -0
- package/dist/client-directives/idle.js +15 -0
- package/dist/client-directives/load.d.ts +9 -0
- package/dist/client-directives/load.js +15 -0
- package/dist/client-directives/media.d.ts +9 -0
- package/dist/client-directives/media.js +15 -0
- package/dist/client-directives/only.d.ts +9 -0
- package/dist/client-directives/only.js +15 -0
- package/dist/client-directives/utils.d.ts +9 -0
- package/dist/client-directives/utils.js +7 -0
- package/dist/client-directives/visible.d.ts +9 -0
- package/dist/client-directives/visible.js +15 -0
- package/dist/extraction/index.d.ts +34 -0
- package/dist/extraction/index.js +9 -0
- package/dist/i18n-3cZHoHwP.d.ts +86 -0
- package/dist/i18n-BGfrSNM8.d.ts +56 -0
- package/dist/i18n-BHU81LMA.d.ts +55 -0
- package/dist/i18n-BagoeH4_.d.ts +62 -0
- package/dist/i18n-BeL84090.d.ts +77 -0
- package/dist/i18n-BeRWDfHi.d.ts +55 -0
- package/dist/i18n-Bfs2PVcf.d.ts +79 -0
- package/dist/i18n-BrL7p3aN.d.ts +99 -0
- package/dist/i18n-CNQNCFU0.d.ts +55 -0
- package/dist/i18n-CX_uU8V2.d.ts +80 -0
- package/dist/i18n-Cr1QZCwe.d.ts +64 -0
- package/dist/i18n-CzTRxGeX.d.ts +80 -0
- package/dist/i18n-DUOFiC7g.d.ts +77 -0
- package/dist/i18n-DcF47BHS.d.ts +77 -0
- package/dist/i18n-DifWvtaO.d.ts +60 -0
- package/dist/i18n-DumSqX1t.d.ts +55 -0
- package/dist/i18n-fWTJw12h.d.ts +80 -0
- package/dist/import-with-i18n.d.ts +23 -0
- package/dist/import-with-i18n.js +7 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +151 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.js +8 -0
- package/dist/internal.d.ts +11 -0
- package/dist/internal.js +9 -0
- package/dist/middleware-entrypoint-C0Drh9E_.d.ts +29 -0
- package/dist/middleware-entrypoint-mEl66IUy.d.ts +29 -0
- package/dist/middleware-entrypoint.d.ts +5 -0
- package/dist/middleware-entrypoint.js +30 -0
- package/dist/server/middleware-entrypoint.d.ts +2 -0
- package/dist/server/middleware-entrypoint.js +28 -0
- package/dist/server/t.d.ts +17 -0
- package/dist/server/t.js +59 -0
- package/dist/t-BY_C9lZ1.d.ts +30 -0
- package/dist/t-CG3yDiEa.d.ts +30 -0
- package/dist/t-CHsBfrPr.d.ts +30 -0
- package/dist/t-DQpXaDhP.d.ts +30 -0
- package/dist/t.client.d.ts +11 -0
- package/dist/t.client.js +35 -0
- package/dist/t.server.d.ts +22 -0
- package/dist/t.server.js +12 -0
- package/dist/types-8uKCwFda.d.ts +24 -0
- package/dist/types-BC6K-pID.d.ts +110 -0
- package/dist/types-BUV7BZML.d.ts +55 -0
- package/dist/types-BWGB322F.d.ts +46 -0
- package/dist/types-B_-RxW4A.d.ts +7 -0
- package/dist/types-BaaSiz9n.d.ts +7 -0
- package/dist/types-Bgl2V5xo.d.ts +102 -0
- package/dist/types-BwhB6IT4.d.ts +111 -0
- package/dist/types-Bzp6KxsI.d.ts +50 -0
- package/dist/types-C6rOJFY2.d.ts +24 -0
- package/dist/types-C7lD-Woj.d.ts +50 -0
- package/dist/types-CLWZeQhD.d.ts +54 -0
- package/dist/types-COXt613w.d.ts +19 -0
- package/dist/types-CUlkbl9-.d.ts +50 -0
- package/dist/types-CVv7GGPj.d.ts +46 -0
- package/dist/types-CiIDN2sW.d.ts +64 -0
- package/dist/types-Cr86qjYl.d.ts +58 -0
- package/dist/types-CxnObiEO.d.ts +7 -0
- package/dist/types-D44mFUjA.d.ts +97 -0
- package/dist/types-D85m9UTC.d.ts +137 -0
- package/dist/types-DWhsmSPP.d.ts +102 -0
- package/dist/types-DdprXGJ1.d.ts +46 -0
- package/dist/types-DhVJ2ZIo.d.ts +43 -0
- package/dist/types-DnbOuM1L.d.ts +7 -0
- package/dist/types-DsNK_9sI.d.ts +24 -0
- package/dist/types-DvWBr9Xp.d.ts +17 -0
- package/dist/types-DwkIalh6.d.ts +24 -0
- package/dist/types-OYRgjtE_.d.ts +110 -0
- package/dist/types-SV8S05GX.d.ts +7 -0
- package/dist/types-TYn9WnxY.d.ts +46 -0
- package/dist/types-aXCBbdhq.d.ts +55 -0
- package/dist/types-gn4aNKGx.d.ts +54 -0
- package/dist/types-nippsMkm.d.ts +54 -0
- package/package.json +105 -0
- package/src/components/I18nScript.astro +37 -0
- package/src/components/index.ts +2 -0
|
@@ -0,0 +1,177 @@
|
|
|
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 -> raw translations merged with manifest fallbacks
|
|
34
|
+
mergedCache = /* @__PURE__ */ new Map();
|
|
35
|
+
// locale -> compiled ICU MessageFormat functions
|
|
36
|
+
compiledCache = /* @__PURE__ */ new Map();
|
|
37
|
+
// locale -> chunk name -> content hash (for cache busting)
|
|
38
|
+
hashCache = /* @__PURE__ */ new Map();
|
|
39
|
+
// manifest getter (set by init module, provides live data in dev mode)
|
|
40
|
+
manifestGetter = null;
|
|
41
|
+
async configure(config) {
|
|
42
|
+
if (!config.locales?.length) {
|
|
43
|
+
throw new Error("i18n.configure(): locales array is required and must not be empty");
|
|
44
|
+
}
|
|
45
|
+
for (const locale of config.locales) {
|
|
46
|
+
if (!locale) {
|
|
47
|
+
throw new Error("i18n.configure(): locale cannot be empty");
|
|
48
|
+
}
|
|
49
|
+
if (locale !== locale.trim()) {
|
|
50
|
+
throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (new Set(config.locales).size !== config.locales.length) {
|
|
54
|
+
throw new Error("i18n.configure(): locales array contains duplicates");
|
|
55
|
+
}
|
|
56
|
+
if (config.defaultLocale !== void 0) {
|
|
57
|
+
if (!config.defaultLocale) {
|
|
58
|
+
throw new Error("i18n.configure(): defaultLocale cannot be empty");
|
|
59
|
+
}
|
|
60
|
+
if (config.defaultLocale !== config.defaultLocale.trim()) {
|
|
61
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
|
|
62
|
+
}
|
|
63
|
+
if (!config.locales.includes(config.defaultLocale)) {
|
|
64
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.config = {
|
|
68
|
+
locales: config.locales,
|
|
69
|
+
defaultLocale: config.defaultLocale ?? config.locales[0],
|
|
70
|
+
fallback: config.fallback ?? "fallback"
|
|
71
|
+
};
|
|
72
|
+
if (!this.manifestGetter) {
|
|
73
|
+
const m = await import("virtual:@astroscope/i18n/manifest");
|
|
74
|
+
this.manifestGetter = m.getManifest;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
isConfigured() {
|
|
78
|
+
return this.config !== null;
|
|
79
|
+
}
|
|
80
|
+
getConfig() {
|
|
81
|
+
if (!this.config) {
|
|
82
|
+
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
83
|
+
}
|
|
84
|
+
return this.config;
|
|
85
|
+
}
|
|
86
|
+
setTranslations(locale, raw) {
|
|
87
|
+
this.rawCache.set(locale, raw);
|
|
88
|
+
this.mergedCache.delete(locale);
|
|
89
|
+
this.compiledCache.delete(locale);
|
|
90
|
+
this.hashCache.delete(locale);
|
|
91
|
+
const { chunks } = this.getManifest();
|
|
92
|
+
if (Object.keys(chunks).length > 0) {
|
|
93
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
getTranslations(locale) {
|
|
97
|
+
const cached = this.mergedCache.get(locale);
|
|
98
|
+
if (cached) {
|
|
99
|
+
return cached;
|
|
100
|
+
}
|
|
101
|
+
const raw = this.rawCache.get(locale) ?? {};
|
|
102
|
+
const manifest = this.manifestGetter?.() ?? { keys: [], chunks: {} };
|
|
103
|
+
if (manifest.keys.length === 0) {
|
|
104
|
+
return raw;
|
|
105
|
+
}
|
|
106
|
+
const withFallbacks = { ...raw };
|
|
107
|
+
for (const { key, meta } of manifest.keys) {
|
|
108
|
+
if (!(key in withFallbacks) && meta.fallback) {
|
|
109
|
+
withFallbacks[key] = meta.fallback;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.mergedCache.set(locale, withFallbacks);
|
|
113
|
+
return withFallbacks;
|
|
114
|
+
}
|
|
115
|
+
getCompiledTranslations(locale) {
|
|
116
|
+
const cached = this.compiledCache.get(locale);
|
|
117
|
+
if (cached) {
|
|
118
|
+
return cached;
|
|
119
|
+
}
|
|
120
|
+
const compiled = compileTranslations(locale, this.getTranslations(locale));
|
|
121
|
+
this.compiledCache.set(locale, compiled);
|
|
122
|
+
return compiled;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Get the extraction manifest (keys + chunks).
|
|
126
|
+
* - keys: translation keys extracted from codebase with metadata
|
|
127
|
+
* - chunks: mapping of chunk names to their translation keys
|
|
128
|
+
*/
|
|
129
|
+
getManifest() {
|
|
130
|
+
if (!this.manifestGetter) {
|
|
131
|
+
throw new Error("i18n manifest not initialized. Call i18n.configure() first.");
|
|
132
|
+
}
|
|
133
|
+
return this.manifestGetter();
|
|
134
|
+
}
|
|
135
|
+
getHashes(locale) {
|
|
136
|
+
return this.hashCache.get(locale) ?? {};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Clear translations for specified locale(s).
|
|
140
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
141
|
+
*/
|
|
142
|
+
clear(locales) {
|
|
143
|
+
const targets = this.resolveLocales(locales);
|
|
144
|
+
for (const locale of targets) {
|
|
145
|
+
this.rawCache.delete(locale);
|
|
146
|
+
this.mergedCache.delete(locale);
|
|
147
|
+
this.compiledCache.delete(locale);
|
|
148
|
+
this.hashCache.delete(locale);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
resolveLocales(locales) {
|
|
152
|
+
if (!locales) {
|
|
153
|
+
return this.config?.locales ?? [];
|
|
154
|
+
}
|
|
155
|
+
if (typeof locales === "string") {
|
|
156
|
+
return [locales];
|
|
157
|
+
}
|
|
158
|
+
return locales;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var i18n = new I18nSingleton();
|
|
162
|
+
|
|
163
|
+
// src/server/context.ts
|
|
164
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
165
|
+
var als = new AsyncLocalStorage();
|
|
166
|
+
function runWithContext(context, fn) {
|
|
167
|
+
return als.run(context, fn);
|
|
168
|
+
}
|
|
169
|
+
function getContext() {
|
|
170
|
+
return als.getStore() ?? null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export {
|
|
174
|
+
i18n,
|
|
175
|
+
runWithContext,
|
|
176
|
+
getContext
|
|
177
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compileMessage,
|
|
3
|
+
compileTranslations
|
|
4
|
+
} from "./chunk-KNDYKTHE.js";
|
|
5
|
+
|
|
6
|
+
// src/extraction/hash.ts
|
|
7
|
+
import { createHash } from "crypto";
|
|
8
|
+
function hashString(str) {
|
|
9
|
+
return createHash("sha256").update(str).digest("hex").substring(0, 8);
|
|
10
|
+
}
|
|
11
|
+
function computeChunkHash(translations, keys) {
|
|
12
|
+
const relevantTranslations = {};
|
|
13
|
+
for (const key of keys.sort()) {
|
|
14
|
+
if (translations[key]) {
|
|
15
|
+
relevantTranslations[key] = translations[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return hashString(JSON.stringify(relevantTranslations));
|
|
19
|
+
}
|
|
20
|
+
function computeAllChunkHashes(translations, manifest) {
|
|
21
|
+
const hashes = {};
|
|
22
|
+
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
23
|
+
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
24
|
+
}
|
|
25
|
+
return hashes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/server/i18n.ts
|
|
29
|
+
var I18nSingleton = class {
|
|
30
|
+
// normalized user config with defaults applied
|
|
31
|
+
config = null;
|
|
32
|
+
// locale -> raw translation strings (before ICU compilation)
|
|
33
|
+
rawCache = /* @__PURE__ */ new Map();
|
|
34
|
+
// locale -> compiled ICU MessageFormat functions
|
|
35
|
+
compiledCache = /* @__PURE__ */ new Map();
|
|
36
|
+
// locale -> chunk name -> content hash (for cache busting)
|
|
37
|
+
hashCache = /* @__PURE__ */ new Map();
|
|
38
|
+
// manifest getter (set by init module, provides live data in dev mode)
|
|
39
|
+
manifestGetter = null;
|
|
40
|
+
async configure(config) {
|
|
41
|
+
if (!config.locales?.length) {
|
|
42
|
+
throw new Error("i18n.configure(): locales array is required and must not be empty");
|
|
43
|
+
}
|
|
44
|
+
for (const locale of config.locales) {
|
|
45
|
+
if (!locale) {
|
|
46
|
+
throw new Error("i18n.configure(): locale cannot be empty");
|
|
47
|
+
}
|
|
48
|
+
if (locale !== locale.trim()) {
|
|
49
|
+
throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (new Set(config.locales).size !== config.locales.length) {
|
|
53
|
+
throw new Error("i18n.configure(): locales array contains duplicates");
|
|
54
|
+
}
|
|
55
|
+
if (config.defaultLocale !== void 0) {
|
|
56
|
+
if (!config.defaultLocale) {
|
|
57
|
+
throw new Error("i18n.configure(): defaultLocale cannot be empty");
|
|
58
|
+
}
|
|
59
|
+
if (config.defaultLocale !== config.defaultLocale.trim()) {
|
|
60
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
|
|
61
|
+
}
|
|
62
|
+
if (!config.locales.includes(config.defaultLocale)) {
|
|
63
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
this.config = {
|
|
67
|
+
locales: config.locales,
|
|
68
|
+
defaultLocale: config.defaultLocale ?? config.locales[0],
|
|
69
|
+
fallback: config.fallback ?? "fallback"
|
|
70
|
+
};
|
|
71
|
+
if (!this.manifestGetter) {
|
|
72
|
+
const m = await import("virtual:@astroscope/i18n/manifest");
|
|
73
|
+
this.manifestGetter = m.getManifest;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
isConfigured() {
|
|
77
|
+
return this.config !== null;
|
|
78
|
+
}
|
|
79
|
+
getConfig() {
|
|
80
|
+
if (!this.config) {
|
|
81
|
+
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
82
|
+
}
|
|
83
|
+
return this.config;
|
|
84
|
+
}
|
|
85
|
+
setTranslations(locale, raw) {
|
|
86
|
+
this.rawCache.set(locale, raw);
|
|
87
|
+
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
88
|
+
this.hashCache.delete(locale);
|
|
89
|
+
const { chunks } = this.getManifest();
|
|
90
|
+
if (Object.keys(chunks).length > 0) {
|
|
91
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
getTranslations(locale) {
|
|
95
|
+
return this.rawCache.get(locale) ?? {};
|
|
96
|
+
}
|
|
97
|
+
getCompiledTranslations(locale) {
|
|
98
|
+
const compiled = this.compiledCache.get(locale) ?? {};
|
|
99
|
+
const manifest = this.manifestGetter?.() ?? { keys: [], chunks: {} };
|
|
100
|
+
if (manifest.keys.length === 0) {
|
|
101
|
+
return compiled;
|
|
102
|
+
}
|
|
103
|
+
const withFallbacks = { ...compiled };
|
|
104
|
+
for (const { key, meta } of manifest.keys) {
|
|
105
|
+
if (!(key in withFallbacks) && meta.fallback) {
|
|
106
|
+
withFallbacks[key] = compileMessage(locale, meta.fallback);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return withFallbacks;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get the extraction manifest (keys + chunks).
|
|
113
|
+
* - keys: translation keys extracted from codebase with metadata
|
|
114
|
+
* - chunks: mapping of chunk names to their translation keys
|
|
115
|
+
*/
|
|
116
|
+
getManifest() {
|
|
117
|
+
if (!this.manifestGetter) {
|
|
118
|
+
throw new Error("i18n manifest not initialized. Call i18n.configure() first.");
|
|
119
|
+
}
|
|
120
|
+
return this.manifestGetter();
|
|
121
|
+
}
|
|
122
|
+
getHashes(locale) {
|
|
123
|
+
return this.hashCache.get(locale) ?? {};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Clear translations for specified locale(s).
|
|
127
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
128
|
+
*/
|
|
129
|
+
clear(locales) {
|
|
130
|
+
const targets = this.resolveLocales(locales);
|
|
131
|
+
for (const locale of targets) {
|
|
132
|
+
this.rawCache.delete(locale);
|
|
133
|
+
this.compiledCache.delete(locale);
|
|
134
|
+
this.hashCache.delete(locale);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
resolveLocales(locales) {
|
|
138
|
+
if (!locales) {
|
|
139
|
+
return this.config?.locales ?? [];
|
|
140
|
+
}
|
|
141
|
+
if (typeof locales === "string") {
|
|
142
|
+
return [locales];
|
|
143
|
+
}
|
|
144
|
+
return locales;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
var i18n = new I18nSingleton();
|
|
148
|
+
|
|
149
|
+
// src/server/context.ts
|
|
150
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
151
|
+
var als = new AsyncLocalStorage();
|
|
152
|
+
function runWithContext(context, fn) {
|
|
153
|
+
return als.run(context, fn);
|
|
154
|
+
}
|
|
155
|
+
function getContext() {
|
|
156
|
+
return als.getStore() ?? null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export {
|
|
160
|
+
i18n,
|
|
161
|
+
runWithContext,
|
|
162
|
+
getContext
|
|
163
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
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
|
+
return this.rawCache.get(locale) ?? {};
|
|
95
|
+
}
|
|
96
|
+
getCompiledTranslations(locale) {
|
|
97
|
+
return this.compiledCache.get(locale) ?? {};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Get the extraction manifest (keys + chunks).
|
|
101
|
+
* - keys: translation keys extracted from codebase with metadata
|
|
102
|
+
* - chunks: mapping of chunk names to their translation keys
|
|
103
|
+
*/
|
|
104
|
+
getManifest() {
|
|
105
|
+
if (!this.manifestGetter) {
|
|
106
|
+
throw new Error("i18n manifest not initialized. Call i18n.configure() first.");
|
|
107
|
+
}
|
|
108
|
+
return this.manifestGetter();
|
|
109
|
+
}
|
|
110
|
+
getHashes(locale) {
|
|
111
|
+
return this.hashCache.get(locale) ?? {};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Clear translations for specified locale(s).
|
|
115
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
116
|
+
*/
|
|
117
|
+
clear(locales) {
|
|
118
|
+
const targets = this.resolveLocales(locales);
|
|
119
|
+
for (const locale of targets) {
|
|
120
|
+
this.rawCache.delete(locale);
|
|
121
|
+
this.compiledCache.delete(locale);
|
|
122
|
+
this.hashCache.delete(locale);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
resolveLocales(locales) {
|
|
126
|
+
if (!locales) {
|
|
127
|
+
return this.config?.locales ?? [];
|
|
128
|
+
}
|
|
129
|
+
if (typeof locales === "string") {
|
|
130
|
+
return [locales];
|
|
131
|
+
}
|
|
132
|
+
return locales;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var i18n = new I18nSingleton();
|
|
136
|
+
|
|
137
|
+
// src/server/context.ts
|
|
138
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
139
|
+
var als = new AsyncLocalStorage();
|
|
140
|
+
function runWithContext(context, fn) {
|
|
141
|
+
return als.run(context, fn);
|
|
142
|
+
}
|
|
143
|
+
function getContext() {
|
|
144
|
+
return als.getStore() ?? null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export {
|
|
148
|
+
i18n,
|
|
149
|
+
runWithContext,
|
|
150
|
+
getContext
|
|
151
|
+
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clearContext,
|
|
3
|
+
compileTranslations,
|
|
4
|
+
setContext
|
|
5
|
+
} from "./chunk-KNONI6DP.js";
|
|
6
|
+
|
|
7
|
+
// src/i18n-endpoint.ts
|
|
8
|
+
function hashString(str) {
|
|
9
|
+
let hash = 0;
|
|
10
|
+
for (let i = 0; i < str.length; i++) {
|
|
11
|
+
const char = str.charCodeAt(i);
|
|
12
|
+
hash = (hash << 5) - hash + char;
|
|
13
|
+
hash = hash & hash;
|
|
14
|
+
}
|
|
15
|
+
return Math.abs(hash).toString(36).substring(0, 8);
|
|
16
|
+
}
|
|
17
|
+
function computeChunkHash(translations, keys) {
|
|
18
|
+
const relevantTranslations = {};
|
|
19
|
+
for (const key of keys.sort()) {
|
|
20
|
+
if (translations[key]) {
|
|
21
|
+
relevantTranslations[key] = translations[key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return hashString(JSON.stringify(relevantTranslations));
|
|
25
|
+
}
|
|
26
|
+
function computeAllChunkHashes(translations, manifest) {
|
|
27
|
+
const hashes = {};
|
|
28
|
+
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
29
|
+
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
30
|
+
}
|
|
31
|
+
return hashes;
|
|
32
|
+
}
|
|
33
|
+
function createI18nEndpoint(options) {
|
|
34
|
+
const { cms, manifest } = options;
|
|
35
|
+
const translationCache2 = /* @__PURE__ */ new Map();
|
|
36
|
+
return async ({ params }) => {
|
|
37
|
+
const { path } = params;
|
|
38
|
+
if (!path) {
|
|
39
|
+
return new Response("Not found", { status: 404 });
|
|
40
|
+
}
|
|
41
|
+
const match = path.match(/^([^/]+)\/(.+)\.([a-z0-9]+)\.js$/);
|
|
42
|
+
if (!match) {
|
|
43
|
+
return new Response("Invalid path", { status: 400 });
|
|
44
|
+
}
|
|
45
|
+
const [, locale, chunkId, requestedHash] = match;
|
|
46
|
+
const chunkFileName = `${chunkId}.js`;
|
|
47
|
+
const keys = manifest[chunkFileName] || manifest[chunkId];
|
|
48
|
+
if (!keys) {
|
|
49
|
+
return new Response(`/* chunk not found: ${chunkId} */`, {
|
|
50
|
+
status: 404,
|
|
51
|
+
headers: { "Content-Type": "application/javascript" }
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
let translations = translationCache2.get(locale);
|
|
55
|
+
if (!translations) {
|
|
56
|
+
translations = await cms.fetch(locale, []);
|
|
57
|
+
translationCache2.set(locale, translations);
|
|
58
|
+
}
|
|
59
|
+
const actualHash = computeChunkHash(translations, keys);
|
|
60
|
+
if (requestedHash !== actualHash) {
|
|
61
|
+
console.warn(`[i18n] hash mismatch for ${chunkId}: expected ${actualHash}, got ${requestedHash}`);
|
|
62
|
+
}
|
|
63
|
+
const chunkTranslations = {};
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
if (translations[key]) {
|
|
66
|
+
chunkTranslations[key] = translations[key];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const js = `/* @astroscope/i18n chunk: ${chunkId} */
|
|
70
|
+
(function() {
|
|
71
|
+
window.__i18n__ = window.__i18n__ || {};
|
|
72
|
+
Object.assign(window.__i18n__, ${JSON.stringify(chunkTranslations)});
|
|
73
|
+
})();
|
|
74
|
+
`;
|
|
75
|
+
return new Response(js, {
|
|
76
|
+
status: 200,
|
|
77
|
+
headers: {
|
|
78
|
+
"Content-Type": "application/javascript",
|
|
79
|
+
// immutable cache - hash in URL ensures cache busting
|
|
80
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/middleware.ts
|
|
87
|
+
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
88
|
+
const acceptLanguage = request.headers.get("accept-language");
|
|
89
|
+
if (!acceptLanguage) {
|
|
90
|
+
return defaultLocale;
|
|
91
|
+
}
|
|
92
|
+
const preferred = acceptLanguage.split(",").map((part) => {
|
|
93
|
+
const [lang, q = "q=1"] = part.trim().split(";");
|
|
94
|
+
return {
|
|
95
|
+
lang: lang.split("-")[0].toLowerCase(),
|
|
96
|
+
q: parseFloat(q.replace("q=", ""))
|
|
97
|
+
};
|
|
98
|
+
}).sort((a, b) => b.q - a.q);
|
|
99
|
+
for (const { lang } of preferred) {
|
|
100
|
+
if (locales.includes(lang)) {
|
|
101
|
+
return lang;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return defaultLocale;
|
|
105
|
+
}
|
|
106
|
+
var translationCache = /* @__PURE__ */ new Map();
|
|
107
|
+
var hashCache = /* @__PURE__ */ new Map();
|
|
108
|
+
function generateI18nHashScript(locale, hashes, fallback) {
|
|
109
|
+
const fallbackValue = typeof fallback === "function" ? "example" : fallback;
|
|
110
|
+
return `<script>window.__i18n_locale__=${JSON.stringify(locale)};window.__i18n_fallback__=${JSON.stringify(fallbackValue)};window.__i18n_hashes__=${JSON.stringify(hashes)};window.__i18n__=window.__i18n__||{};</script>`;
|
|
111
|
+
}
|
|
112
|
+
function generateI18nFullScript(locale, raw, fallback) {
|
|
113
|
+
const fallbackValue = typeof fallback === "function" ? "example" : fallback;
|
|
114
|
+
return `<script>window.__i18n_locale__=${JSON.stringify(locale)};window.__i18n_fallback__=${JSON.stringify(fallbackValue)};window.__i18n__=${JSON.stringify(raw)};</script>`;
|
|
115
|
+
}
|
|
116
|
+
async function injectI18nScript(response, script) {
|
|
117
|
+
const contentType = response.headers.get("content-type");
|
|
118
|
+
if (!contentType?.includes("text/html")) {
|
|
119
|
+
return response;
|
|
120
|
+
}
|
|
121
|
+
const html = await response.text();
|
|
122
|
+
let injectedHtml;
|
|
123
|
+
if (html.includes("</head>")) {
|
|
124
|
+
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
125
|
+
} else if (html.includes("<body")) {
|
|
126
|
+
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
127
|
+
} else {
|
|
128
|
+
injectedHtml = script + html;
|
|
129
|
+
}
|
|
130
|
+
return new Response(injectedHtml, {
|
|
131
|
+
status: response.status,
|
|
132
|
+
statusText: response.statusText,
|
|
133
|
+
headers: response.headers
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function createI18nMiddleware(options) {
|
|
137
|
+
const { locales, defaultLocale, fallback, cms, detectLocale, chunkManifest } = options;
|
|
138
|
+
return async (ctx, next) => {
|
|
139
|
+
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
140
|
+
let raw = translationCache.get(locale);
|
|
141
|
+
if (!raw) {
|
|
142
|
+
raw = await cms.fetch(locale, []);
|
|
143
|
+
translationCache.set(locale, raw);
|
|
144
|
+
if (chunkManifest) {
|
|
145
|
+
const hashes = computeAllChunkHashes(raw, chunkManifest);
|
|
146
|
+
hashCache.set(locale, hashes);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const translations = compileTranslations(locale, raw);
|
|
150
|
+
const context = {
|
|
151
|
+
locale,
|
|
152
|
+
translations,
|
|
153
|
+
fallback,
|
|
154
|
+
meta: {}
|
|
155
|
+
};
|
|
156
|
+
setContext(context);
|
|
157
|
+
try {
|
|
158
|
+
const response = await next();
|
|
159
|
+
let script;
|
|
160
|
+
if (chunkManifest) {
|
|
161
|
+
const hashes = hashCache.get(locale) || {};
|
|
162
|
+
script = generateI18nHashScript(locale, hashes, fallback);
|
|
163
|
+
} else {
|
|
164
|
+
script = generateI18nFullScript(locale, raw, fallback);
|
|
165
|
+
}
|
|
166
|
+
const injectedResponse = await injectI18nScript(response, script);
|
|
167
|
+
return injectedResponse;
|
|
168
|
+
} finally {
|
|
169
|
+
clearContext();
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function getCachedTranslations(locale) {
|
|
174
|
+
return translationCache.get(locale);
|
|
175
|
+
}
|
|
176
|
+
function clearTranslationCache(locale) {
|
|
177
|
+
if (locale) {
|
|
178
|
+
translationCache.delete(locale);
|
|
179
|
+
hashCache.delete(locale);
|
|
180
|
+
} else {
|
|
181
|
+
translationCache.clear();
|
|
182
|
+
hashCache.clear();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export {
|
|
187
|
+
computeChunkHash,
|
|
188
|
+
computeAllChunkHashes,
|
|
189
|
+
createI18nEndpoint,
|
|
190
|
+
createI18nMiddleware,
|
|
191
|
+
getCachedTranslations,
|
|
192
|
+
clearTranslationCache
|
|
193
|
+
};
|