@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,156 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compileTranslations
|
|
3
|
+
} from "./chunk-KNDYKTHE.js";
|
|
4
|
+
|
|
5
|
+
// src/server/i18n.ts
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
|
|
8
|
+
// src/extraction/hash.ts
|
|
9
|
+
import { createHash } from "crypto";
|
|
10
|
+
function hashString(str) {
|
|
11
|
+
return createHash("sha256").update(str).digest("hex").substring(0, 8);
|
|
12
|
+
}
|
|
13
|
+
function computeChunkHash(translations, keys) {
|
|
14
|
+
const relevantTranslations = {};
|
|
15
|
+
for (const key of keys.sort()) {
|
|
16
|
+
if (translations[key]) {
|
|
17
|
+
relevantTranslations[key] = translations[key];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return hashString(JSON.stringify(relevantTranslations));
|
|
21
|
+
}
|
|
22
|
+
function computeAllChunkHashes(translations, manifest) {
|
|
23
|
+
const hashes = {};
|
|
24
|
+
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
25
|
+
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
26
|
+
}
|
|
27
|
+
return hashes;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/server/i18n.ts
|
|
31
|
+
function loadExtractionManifest() {
|
|
32
|
+
const candidates = [
|
|
33
|
+
"./i18n-manifest.json",
|
|
34
|
+
// same directory
|
|
35
|
+
"../i18n-manifest.json"
|
|
36
|
+
// parent (when code is in chunks/)
|
|
37
|
+
];
|
|
38
|
+
for (const candidate of candidates) {
|
|
39
|
+
try {
|
|
40
|
+
const manifestPath = new URL(candidate, import.meta.url);
|
|
41
|
+
const content = fs.readFileSync(manifestPath, "utf-8");
|
|
42
|
+
return JSON.parse(content);
|
|
43
|
+
} catch {
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
var I18nSingleton = class {
|
|
49
|
+
config = null;
|
|
50
|
+
rawCache = /* @__PURE__ */ new Map();
|
|
51
|
+
compiledCache = /* @__PURE__ */ new Map();
|
|
52
|
+
extractionManifest = void 0;
|
|
53
|
+
// undefined = not loaded yet
|
|
54
|
+
hashCache = /* @__PURE__ */ new Map();
|
|
55
|
+
configure(config) {
|
|
56
|
+
if (!config.locales?.length) {
|
|
57
|
+
throw new Error("i18n.configure(): locales array is required and must not be empty");
|
|
58
|
+
}
|
|
59
|
+
for (const locale of config.locales) {
|
|
60
|
+
if (!locale) {
|
|
61
|
+
throw new Error("i18n.configure(): locale cannot be empty");
|
|
62
|
+
}
|
|
63
|
+
if (locale !== locale.trim()) {
|
|
64
|
+
throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (new Set(config.locales).size !== config.locales.length) {
|
|
68
|
+
throw new Error("i18n.configure(): locales array contains duplicates");
|
|
69
|
+
}
|
|
70
|
+
if (config.defaultLocale !== void 0) {
|
|
71
|
+
if (!config.defaultLocale) {
|
|
72
|
+
throw new Error("i18n.configure(): defaultLocale cannot be empty");
|
|
73
|
+
}
|
|
74
|
+
if (config.defaultLocale !== config.defaultLocale.trim()) {
|
|
75
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
|
|
76
|
+
}
|
|
77
|
+
if (!config.locales.includes(config.defaultLocale)) {
|
|
78
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
this.config = {
|
|
82
|
+
locales: config.locales,
|
|
83
|
+
defaultLocale: config.defaultLocale ?? config.locales[0],
|
|
84
|
+
fallback: config.fallback ?? "fallback"
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
isConfigured() {
|
|
88
|
+
return this.config !== null;
|
|
89
|
+
}
|
|
90
|
+
getConfig() {
|
|
91
|
+
if (!this.config) {
|
|
92
|
+
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
93
|
+
}
|
|
94
|
+
return this.config;
|
|
95
|
+
}
|
|
96
|
+
setTranslations(locale, raw) {
|
|
97
|
+
this.rawCache.set(locale, raw);
|
|
98
|
+
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
99
|
+
this.hashCache.delete(locale);
|
|
100
|
+
const chunks = this.getChunkManifest();
|
|
101
|
+
if (chunks) {
|
|
102
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
getTranslations(locale) {
|
|
106
|
+
return this.rawCache.get(locale) ?? {};
|
|
107
|
+
}
|
|
108
|
+
getCompiledTranslations(locale) {
|
|
109
|
+
return this.compiledCache.get(locale) ?? {};
|
|
110
|
+
}
|
|
111
|
+
getChunkManifest() {
|
|
112
|
+
return this.getExtractionManifest()?.chunks ?? {};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get extracted translation keys with metadata.
|
|
116
|
+
* Use this to populate your CMS with keys extracted from the codebase.
|
|
117
|
+
*/
|
|
118
|
+
getExtractedKeys() {
|
|
119
|
+
return this.getExtractionManifest()?.keys ?? null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get the full extraction manifest (keys + chunks).
|
|
123
|
+
* Returns null in dev mode (manifest doesn't exist).
|
|
124
|
+
*/
|
|
125
|
+
getExtractionManifest() {
|
|
126
|
+
if (this.extractionManifest === void 0) {
|
|
127
|
+
this.extractionManifest = loadExtractionManifest() ?? null;
|
|
128
|
+
}
|
|
129
|
+
return this.extractionManifest;
|
|
130
|
+
}
|
|
131
|
+
getHashes(locale) {
|
|
132
|
+
return this.hashCache.get(locale) ?? {};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Clear translations for specified locale(s).
|
|
136
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
137
|
+
*/
|
|
138
|
+
clear(locales) {
|
|
139
|
+
const targets = this.resolveLocales(locales);
|
|
140
|
+
for (const locale of targets) {
|
|
141
|
+
this.rawCache.delete(locale);
|
|
142
|
+
this.compiledCache.delete(locale);
|
|
143
|
+
this.hashCache.delete(locale);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
resolveLocales(locales) {
|
|
147
|
+
if (!locales) return this.config?.locales ?? [];
|
|
148
|
+
if (typeof locales === "string") return [locales];
|
|
149
|
+
return locales;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var i18n = new I18nSingleton();
|
|
153
|
+
|
|
154
|
+
export {
|
|
155
|
+
i18n
|
|
156
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clearContext,
|
|
3
|
+
compileTranslations,
|
|
4
|
+
setContext
|
|
5
|
+
} from "./chunk-KNONI6DP.js";
|
|
6
|
+
|
|
7
|
+
// src/middleware.ts
|
|
8
|
+
function defaultDetectLocale(request, locales, defaultLocale) {
|
|
9
|
+
const acceptLanguage = request.headers.get("accept-language");
|
|
10
|
+
if (!acceptLanguage) {
|
|
11
|
+
return defaultLocale;
|
|
12
|
+
}
|
|
13
|
+
const preferred = acceptLanguage.split(",").map((part) => {
|
|
14
|
+
const [lang, q = "q=1"] = part.trim().split(";");
|
|
15
|
+
return {
|
|
16
|
+
lang: lang.split("-")[0].toLowerCase(),
|
|
17
|
+
q: parseFloat(q.replace("q=", ""))
|
|
18
|
+
};
|
|
19
|
+
}).sort((a, b) => b.q - a.q);
|
|
20
|
+
for (const { lang } of preferred) {
|
|
21
|
+
if (locales.includes(lang)) {
|
|
22
|
+
return lang;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return defaultLocale;
|
|
26
|
+
}
|
|
27
|
+
var translationCache = /* @__PURE__ */ new Map();
|
|
28
|
+
function generateI18nScript(locale, raw, fallback) {
|
|
29
|
+
const fallbackValue = typeof fallback === "function" ? "example" : fallback;
|
|
30
|
+
return `<script>window.__i18n_locale__=${JSON.stringify(locale)};window.__i18n_fallback__=${JSON.stringify(fallbackValue)};window.__i18n__=${JSON.stringify(raw)};</script>`;
|
|
31
|
+
}
|
|
32
|
+
async function injectI18nScript(response, script) {
|
|
33
|
+
const contentType = response.headers.get("content-type");
|
|
34
|
+
if (!contentType?.includes("text/html")) {
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
const html = await response.text();
|
|
38
|
+
let injectedHtml;
|
|
39
|
+
if (html.includes("</head>")) {
|
|
40
|
+
injectedHtml = html.replace("</head>", `${script}</head>`);
|
|
41
|
+
} else if (html.includes("<body")) {
|
|
42
|
+
injectedHtml = html.replace(/<body([^>]*)>/, `<body$1>${script}`);
|
|
43
|
+
} else {
|
|
44
|
+
injectedHtml = script + html;
|
|
45
|
+
}
|
|
46
|
+
return new Response(injectedHtml, {
|
|
47
|
+
status: response.status,
|
|
48
|
+
statusText: response.statusText,
|
|
49
|
+
headers: response.headers
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function createI18nMiddleware(options) {
|
|
53
|
+
const { locales, defaultLocale, fallback, cms, detectLocale } = options;
|
|
54
|
+
return async (ctx, next) => {
|
|
55
|
+
const locale = detectLocale ? detectLocale(ctx.request) : ctx.currentLocale ?? defaultDetectLocale(ctx.request, locales, defaultLocale);
|
|
56
|
+
let raw = translationCache.get(locale);
|
|
57
|
+
if (!raw) {
|
|
58
|
+
raw = await cms.fetch(locale, []);
|
|
59
|
+
translationCache.set(locale, raw);
|
|
60
|
+
}
|
|
61
|
+
const translations = compileTranslations(locale, raw);
|
|
62
|
+
const context = {
|
|
63
|
+
locale,
|
|
64
|
+
translations,
|
|
65
|
+
fallback,
|
|
66
|
+
meta: {}
|
|
67
|
+
};
|
|
68
|
+
setContext(context);
|
|
69
|
+
try {
|
|
70
|
+
const response = await next();
|
|
71
|
+
const script = generateI18nScript(locale, raw, fallback);
|
|
72
|
+
const injectedResponse = await injectI18nScript(response, script);
|
|
73
|
+
return injectedResponse;
|
|
74
|
+
} finally {
|
|
75
|
+
clearContext();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export {
|
|
81
|
+
createI18nMiddleware
|
|
82
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
// extraction manifest getter (lazy-loads from virtual module)
|
|
38
|
+
manifestGetter = null;
|
|
39
|
+
// cached manifest (populated after first async fetch)
|
|
40
|
+
manifestCache = null;
|
|
41
|
+
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
|
+
}
|
|
73
|
+
isConfigured() {
|
|
74
|
+
return this.config !== null;
|
|
75
|
+
}
|
|
76
|
+
getConfig() {
|
|
77
|
+
if (!this.config) {
|
|
78
|
+
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
79
|
+
}
|
|
80
|
+
return this.config;
|
|
81
|
+
}
|
|
82
|
+
setTranslations(locale, raw) {
|
|
83
|
+
this.rawCache.set(locale, raw);
|
|
84
|
+
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
85
|
+
this.hashCache.delete(locale);
|
|
86
|
+
const chunks = this.getChunkManifest();
|
|
87
|
+
if (Object.keys(chunks).length > 0) {
|
|
88
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
getTranslations(locale) {
|
|
92
|
+
return this.rawCache.get(locale) ?? {};
|
|
93
|
+
}
|
|
94
|
+
getCompiledTranslations(locale) {
|
|
95
|
+
return this.compiledCache.get(locale) ?? {};
|
|
96
|
+
}
|
|
97
|
+
getChunkManifest() {
|
|
98
|
+
return this.getExtractionManifest().chunks;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get extracted translation keys with metadata (sync, uses cached manifest).
|
|
102
|
+
* Use getExtractedKeysAsync() for fresh data in dev mode.
|
|
103
|
+
*/
|
|
104
|
+
getExtractedKeys() {
|
|
105
|
+
return this.getExtractionManifest().keys;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Async fetch of extracted translation keys with metadata.
|
|
109
|
+
* In dev mode, this re-imports the virtual module to get freshly extracted keys.
|
|
110
|
+
* Use this to populate your translation provider with keys extracted from the codebase.
|
|
111
|
+
*/
|
|
112
|
+
async getExtractedKeysAsync() {
|
|
113
|
+
const manifest = await this.getExtractionManifestAsync();
|
|
114
|
+
return manifest.keys;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the full extraction manifest (keys + chunks).
|
|
118
|
+
* Returns cached manifest or empty default if not yet fetched.
|
|
119
|
+
* Use getExtractionManifestAsync() for fresh data in dev mode.
|
|
120
|
+
*/
|
|
121
|
+
getExtractionManifest() {
|
|
122
|
+
return this.manifestCache ?? { keys: [], chunks: {} };
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Async fetch of extraction manifest.
|
|
126
|
+
* In dev mode, this re-imports the virtual module to get fresh extracted keys.
|
|
127
|
+
* In build mode, returns the cached manifest.
|
|
128
|
+
*/
|
|
129
|
+
async getExtractionManifestAsync() {
|
|
130
|
+
if (!this.manifestGetter) {
|
|
131
|
+
return this.manifestCache ?? { keys: [], chunks: {} };
|
|
132
|
+
}
|
|
133
|
+
const manifest = await this.manifestGetter();
|
|
134
|
+
this.manifestCache = manifest;
|
|
135
|
+
return manifest;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Set the manifest getter function (called by init module).
|
|
139
|
+
* @internal
|
|
140
|
+
*/
|
|
141
|
+
setManifestGetter(getter) {
|
|
142
|
+
this.manifestGetter = getter;
|
|
143
|
+
}
|
|
144
|
+
getHashes(locale) {
|
|
145
|
+
return this.hashCache.get(locale) ?? {};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Clear translations for specified locale(s).
|
|
149
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
150
|
+
*/
|
|
151
|
+
clear(locales) {
|
|
152
|
+
const targets = this.resolveLocales(locales);
|
|
153
|
+
for (const locale of targets) {
|
|
154
|
+
this.rawCache.delete(locale);
|
|
155
|
+
this.compiledCache.delete(locale);
|
|
156
|
+
this.hashCache.delete(locale);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
resolveLocales(locales) {
|
|
160
|
+
if (!locales) {
|
|
161
|
+
return this.config?.locales ?? [];
|
|
162
|
+
}
|
|
163
|
+
if (typeof locales === "string") {
|
|
164
|
+
return [locales];
|
|
165
|
+
}
|
|
166
|
+
return locales;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var i18n = new I18nSingleton();
|
|
170
|
+
|
|
171
|
+
export {
|
|
172
|
+
i18n
|
|
173
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildI18nChunkUrl,
|
|
3
|
+
chunkIdToName
|
|
4
|
+
} from "./chunk-LMTEO22Y.js";
|
|
5
|
+
|
|
6
|
+
// src/client/import-with-i18n.ts
|
|
7
|
+
async function importWithI18n(importFn, chunkId) {
|
|
8
|
+
const i18n = window.__i18n__;
|
|
9
|
+
const chunkName = chunkId ? chunkIdToName(chunkId) : void 0;
|
|
10
|
+
const hash = chunkName ? i18n.hashes[chunkName] : void 0;
|
|
11
|
+
const [module] = await Promise.all([
|
|
12
|
+
importFn(),
|
|
13
|
+
hash && chunkName ? import(
|
|
14
|
+
/* @vite-ignore */
|
|
15
|
+
buildI18nChunkUrl(i18n.locale, chunkName, hash)
|
|
16
|
+
) : Promise.resolve()
|
|
17
|
+
]);
|
|
18
|
+
return module;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
importWithI18n
|
|
23
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildI18nChunkUrl,
|
|
3
|
+
chunkIdToName
|
|
4
|
+
} from "./chunk-FI5KZ5IO.js";
|
|
5
|
+
|
|
6
|
+
// src/import-with-i18n.ts
|
|
7
|
+
async function importWithI18n(importFn, chunkId) {
|
|
8
|
+
const i18n = window.__i18n__;
|
|
9
|
+
const chunkName = chunkId ? chunkIdToName(chunkId) : void 0;
|
|
10
|
+
const hash = chunkName ? i18n?.hashes[chunkName] : void 0;
|
|
11
|
+
const [module] = await Promise.all([
|
|
12
|
+
importFn(),
|
|
13
|
+
hash && chunkName && i18n ? import(
|
|
14
|
+
/* @vite-ignore */
|
|
15
|
+
buildI18nChunkUrl(i18n.locale, chunkName, hash)
|
|
16
|
+
) : Promise.resolve()
|
|
17
|
+
]);
|
|
18
|
+
return module;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
importWithI18n
|
|
23
|
+
};
|