@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,144 @@
|
|
|
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
|
+
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
|
+
}
|
|
71
|
+
isConfigured() {
|
|
72
|
+
return this.config !== null;
|
|
73
|
+
}
|
|
74
|
+
getConfig() {
|
|
75
|
+
if (!this.config) {
|
|
76
|
+
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
77
|
+
}
|
|
78
|
+
return this.config;
|
|
79
|
+
}
|
|
80
|
+
setTranslations(locale, raw) {
|
|
81
|
+
this.rawCache.set(locale, raw);
|
|
82
|
+
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
83
|
+
this.hashCache.delete(locale);
|
|
84
|
+
const { chunks } = this.getManifest();
|
|
85
|
+
if (Object.keys(chunks).length > 0) {
|
|
86
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
getTranslations(locale) {
|
|
90
|
+
return this.rawCache.get(locale) ?? {};
|
|
91
|
+
}
|
|
92
|
+
getCompiledTranslations(locale) {
|
|
93
|
+
return this.compiledCache.get(locale) ?? {};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the extraction manifest (keys + chunks).
|
|
97
|
+
* - keys: translation keys extracted from codebase with metadata
|
|
98
|
+
* - chunks: mapping of chunk names to their translation keys
|
|
99
|
+
*/
|
|
100
|
+
getManifest() {
|
|
101
|
+
if (!this.manifestGetter) {
|
|
102
|
+
throw new Error("i18n manifest not initialized. Make sure to import '@astroscope/i18n/init' in your boot file.");
|
|
103
|
+
}
|
|
104
|
+
return this.manifestGetter();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Set the manifest getter function (called by init module).
|
|
108
|
+
* In dev mode, this returns live data from the vite plugin.
|
|
109
|
+
* In production, this returns the static manifest loaded from JSON.
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
setManifestGetter(getter) {
|
|
113
|
+
this.manifestGetter = getter;
|
|
114
|
+
}
|
|
115
|
+
getHashes(locale) {
|
|
116
|
+
return this.hashCache.get(locale) ?? {};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Clear translations for specified locale(s).
|
|
120
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
121
|
+
*/
|
|
122
|
+
clear(locales) {
|
|
123
|
+
const targets = this.resolveLocales(locales);
|
|
124
|
+
for (const locale of targets) {
|
|
125
|
+
this.rawCache.delete(locale);
|
|
126
|
+
this.compiledCache.delete(locale);
|
|
127
|
+
this.hashCache.delete(locale);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
resolveLocales(locales) {
|
|
131
|
+
if (!locales) {
|
|
132
|
+
return this.config?.locales ?? [];
|
|
133
|
+
}
|
|
134
|
+
if (typeof locales === "string") {
|
|
135
|
+
return [locales];
|
|
136
|
+
}
|
|
137
|
+
return locales;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var i18n = new I18nSingleton();
|
|
141
|
+
|
|
142
|
+
export {
|
|
143
|
+
i18n
|
|
144
|
+
};
|
|
@@ -0,0 +1,176 @@
|
|
|
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
|
+
// normalized user config with defaults applied
|
|
50
|
+
config = null;
|
|
51
|
+
// locale -> raw translation strings (before ICU compilation)
|
|
52
|
+
rawCache = /* @__PURE__ */ new Map();
|
|
53
|
+
// locale -> compiled ICU MessageFormat functions
|
|
54
|
+
compiledCache = /* @__PURE__ */ new Map();
|
|
55
|
+
// build-time manifest with extracted keys and chunk mappings (lazy-loaded, null in dev)
|
|
56
|
+
extractionManifest = void 0;
|
|
57
|
+
// locale -> chunk name -> content hash (for cache busting)
|
|
58
|
+
hashCache = /* @__PURE__ */ new Map();
|
|
59
|
+
configure(config) {
|
|
60
|
+
if (!config.locales?.length) {
|
|
61
|
+
throw new Error("i18n.configure(): locales array is required and must not be empty");
|
|
62
|
+
}
|
|
63
|
+
for (const locale of config.locales) {
|
|
64
|
+
if (!locale) {
|
|
65
|
+
throw new Error("i18n.configure(): locale cannot be empty");
|
|
66
|
+
}
|
|
67
|
+
if (locale !== locale.trim()) {
|
|
68
|
+
throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (new Set(config.locales).size !== config.locales.length) {
|
|
72
|
+
throw new Error("i18n.configure(): locales array contains duplicates");
|
|
73
|
+
}
|
|
74
|
+
if (config.defaultLocale !== void 0) {
|
|
75
|
+
if (!config.defaultLocale) {
|
|
76
|
+
throw new Error("i18n.configure(): defaultLocale cannot be empty");
|
|
77
|
+
}
|
|
78
|
+
if (config.defaultLocale !== config.defaultLocale.trim()) {
|
|
79
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
|
|
80
|
+
}
|
|
81
|
+
if (!config.locales.includes(config.defaultLocale)) {
|
|
82
|
+
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.config = {
|
|
86
|
+
locales: config.locales,
|
|
87
|
+
defaultLocale: config.defaultLocale ?? config.locales[0],
|
|
88
|
+
fallback: config.fallback ?? "fallback"
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
isConfigured() {
|
|
92
|
+
return this.config !== null;
|
|
93
|
+
}
|
|
94
|
+
getConfig() {
|
|
95
|
+
if (!this.config) {
|
|
96
|
+
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
97
|
+
}
|
|
98
|
+
return this.config;
|
|
99
|
+
}
|
|
100
|
+
setTranslations(locale, raw) {
|
|
101
|
+
this.rawCache.set(locale, raw);
|
|
102
|
+
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
103
|
+
this.hashCache.delete(locale);
|
|
104
|
+
const chunks = this.getChunkManifest();
|
|
105
|
+
if (chunks) {
|
|
106
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
getTranslations(locale) {
|
|
110
|
+
return this.rawCache.get(locale) ?? {};
|
|
111
|
+
}
|
|
112
|
+
getCompiledTranslations(locale) {
|
|
113
|
+
return this.compiledCache.get(locale) ?? {};
|
|
114
|
+
}
|
|
115
|
+
getChunkManifest() {
|
|
116
|
+
return this.getExtractionManifest()?.chunks ?? {};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get extracted translation keys with metadata.
|
|
120
|
+
* Use this to populate your translation provider with keys extracted from the codebase.
|
|
121
|
+
*/
|
|
122
|
+
getExtractedKeys() {
|
|
123
|
+
return this.getExtractionManifest()?.keys ?? null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Get the full extraction manifest (keys + chunks).
|
|
127
|
+
* Returns null in dev mode (manifest doesn't exist).
|
|
128
|
+
*/
|
|
129
|
+
getExtractionManifest() {
|
|
130
|
+
if (this.extractionManifest === void 0) {
|
|
131
|
+
this.extractionManifest = loadExtractionManifest() ?? null;
|
|
132
|
+
}
|
|
133
|
+
return this.extractionManifest;
|
|
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.compiledCache.delete(locale);
|
|
147
|
+
this.hashCache.delete(locale);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
resolveLocales(locales) {
|
|
151
|
+
if (!locales) {
|
|
152
|
+
return this.config?.locales ?? [];
|
|
153
|
+
}
|
|
154
|
+
if (typeof locales === "string") {
|
|
155
|
+
return [locales];
|
|
156
|
+
}
|
|
157
|
+
return locales;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var i18n = new I18nSingleton();
|
|
161
|
+
|
|
162
|
+
// src/server/context.ts
|
|
163
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
164
|
+
var als = new AsyncLocalStorage();
|
|
165
|
+
function runWithContext(context, fn) {
|
|
166
|
+
return als.run(context, fn);
|
|
167
|
+
}
|
|
168
|
+
function getContext() {
|
|
169
|
+
return als.getStore() ?? null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export {
|
|
173
|
+
i18n,
|
|
174
|
+
runWithContext,
|
|
175
|
+
getContext
|
|
176
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/shared/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
|
+
return `/_i18n/${locale}/${chunkIdToName(chunkId)}.${hash}.js`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
chunkIdToName,
|
|
14
|
+
componentUrlToChunkName,
|
|
15
|
+
buildI18nChunkUrl
|
|
16
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/client/t.ts
|
|
2
|
+
import MessageFormat from "@messageformat/core";
|
|
3
|
+
var compiledCache = /* @__PURE__ */ new Map();
|
|
4
|
+
var mf = null;
|
|
5
|
+
function getMessageFormat() {
|
|
6
|
+
if (!mf) {
|
|
7
|
+
mf = new MessageFormat(window.__i18n__.locale);
|
|
8
|
+
}
|
|
9
|
+
return mf;
|
|
10
|
+
}
|
|
11
|
+
function compileMessage(message) {
|
|
12
|
+
try {
|
|
13
|
+
return getMessageFormat().compile(message);
|
|
14
|
+
} catch {
|
|
15
|
+
return (values) => {
|
|
16
|
+
if (!values) return message;
|
|
17
|
+
return Object.entries(values).reduce(
|
|
18
|
+
(result, [key, value]) => result.replace(new RegExp(`\\{${key}\\}`, "g"), String(value)),
|
|
19
|
+
message
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
var t = (key, meta, values) => {
|
|
25
|
+
let compiled = compiledCache.get(key);
|
|
26
|
+
if (!compiled) {
|
|
27
|
+
const raw = window.__i18n__.translations[key] ?? meta.example;
|
|
28
|
+
compiled = compileMessage(raw);
|
|
29
|
+
compiledCache.set(key, compiled);
|
|
30
|
+
}
|
|
31
|
+
return compiled(values);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export {
|
|
35
|
+
t
|
|
36
|
+
};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkIdToName
|
|
3
|
+
} from "./chunk-4A5BKHFN.js";
|
|
4
|
+
|
|
5
|
+
// src/extraction/manifest.ts
|
|
6
|
+
import path from "path";
|
|
7
|
+
var I18N_MANIFEST_GLOBAL_KEY = "__astroscope_i18n_manifest__";
|
|
8
|
+
function getGlobalState() {
|
|
9
|
+
const g = globalThis;
|
|
10
|
+
if (!g[I18N_MANIFEST_GLOBAL_KEY]) {
|
|
11
|
+
g[I18N_MANIFEST_GLOBAL_KEY] = {
|
|
12
|
+
extractedKeys: [],
|
|
13
|
+
chunkManifest: {},
|
|
14
|
+
projectRoot: ""
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return g[I18N_MANIFEST_GLOBAL_KEY];
|
|
18
|
+
}
|
|
19
|
+
function getManifest() {
|
|
20
|
+
const state = getGlobalState();
|
|
21
|
+
const keysWithRelativePaths = state.extractedKeys.map((key) => ({
|
|
22
|
+
...key,
|
|
23
|
+
file: state.projectRoot ? path.relative(state.projectRoot, key.file) : key.file
|
|
24
|
+
}));
|
|
25
|
+
return {
|
|
26
|
+
keys: keysWithRelativePaths,
|
|
27
|
+
chunks: state.chunkManifest
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/extraction/vite-plugin.ts
|
|
32
|
+
import fs from "fs";
|
|
33
|
+
import path2 from "path";
|
|
34
|
+
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
35
|
+
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
36
|
+
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
37
|
+
function hasI18nImport(code) {
|
|
38
|
+
return code.includes("@astroscope/i18n/t");
|
|
39
|
+
}
|
|
40
|
+
function getParserPlugins(filename) {
|
|
41
|
+
const plugins = [];
|
|
42
|
+
if (/\.tsx?$/.test(filename)) {
|
|
43
|
+
plugins.push("typescript");
|
|
44
|
+
}
|
|
45
|
+
if (/\.(jsx|tsx)$/.test(filename)) {
|
|
46
|
+
plugins.push("jsx");
|
|
47
|
+
}
|
|
48
|
+
return plugins;
|
|
49
|
+
}
|
|
50
|
+
function i18nVitePlugin(options) {
|
|
51
|
+
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
52
|
+
const extractedKeys = [];
|
|
53
|
+
const fileToKeys = /* @__PURE__ */ new Map();
|
|
54
|
+
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
55
|
+
let chunkManifest = {};
|
|
56
|
+
let projectRoot = "";
|
|
57
|
+
let isBuild = false;
|
|
58
|
+
let isSSR = false;
|
|
59
|
+
return {
|
|
60
|
+
name: "@astroscope/i18n/extract",
|
|
61
|
+
enforce: "pre",
|
|
62
|
+
configResolved(config) {
|
|
63
|
+
projectRoot = config.root;
|
|
64
|
+
getGlobalState().projectRoot = config.root;
|
|
65
|
+
isBuild = config.command === "build";
|
|
66
|
+
isSSR = !!config.build.ssr;
|
|
67
|
+
},
|
|
68
|
+
resolveId(id) {
|
|
69
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
70
|
+
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
71
|
+
}
|
|
72
|
+
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
73
|
+
return `\0${id}`;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
load(id) {
|
|
77
|
+
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
78
|
+
if (isBuild) {
|
|
79
|
+
return `
|
|
80
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
81
|
+
import { fileURLToPath } from 'node:url';
|
|
82
|
+
import { dirname, join } from 'node:path';
|
|
83
|
+
|
|
84
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
85
|
+
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
86
|
+
|
|
87
|
+
// manifest is emitted to server directory (not publicly accessible)
|
|
88
|
+
const searchPaths = [
|
|
89
|
+
join(__dirname, manifestName), // same dir (server/)
|
|
90
|
+
join(__dirname, '..', manifestName), // parent dir (from chunks/)
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
let manifestPath = searchPaths.find(p => existsSync(p));
|
|
94
|
+
if (!manifestPath) {
|
|
95
|
+
console.warn('[i18n] manifest not found, using empty manifest');
|
|
96
|
+
manifestPath = null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const manifestJson = manifestPath
|
|
100
|
+
? JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
101
|
+
: { keys: [], chunks: {} };
|
|
102
|
+
|
|
103
|
+
export const manifest = manifestJson;
|
|
104
|
+
export function getManifest() { return manifestJson; }
|
|
105
|
+
`;
|
|
106
|
+
}
|
|
107
|
+
return `
|
|
108
|
+
import { getManifest as _getManifest } from '@astroscope/i18n/extraction';
|
|
109
|
+
export const manifest = { keys: [], chunks: {} }; // placeholder, use getManifest()
|
|
110
|
+
export function getManifest() { return _getManifest(); }
|
|
111
|
+
`;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
async transform(code, id) {
|
|
115
|
+
if (!include.some((ext) => id.endsWith(ext))) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
if (id.includes("node_modules")) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (!hasI18nImport(code)) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
filesWithI18n.add(id);
|
|
125
|
+
const fileKeys = [];
|
|
126
|
+
const { transformAsync } = await import("@babel/core");
|
|
127
|
+
const { default: i18nExtractPlugin } = await import("./babel-plugin-FUTQRKLT.js");
|
|
128
|
+
const result = await transformAsync(code, {
|
|
129
|
+
filename: id,
|
|
130
|
+
sourceMaps: true,
|
|
131
|
+
babelrc: false,
|
|
132
|
+
configFile: false,
|
|
133
|
+
parserOpts: {
|
|
134
|
+
plugins: getParserPlugins(id)
|
|
135
|
+
},
|
|
136
|
+
plugins: [
|
|
137
|
+
[
|
|
138
|
+
i18nExtractPlugin,
|
|
139
|
+
{
|
|
140
|
+
onKeyExtracted: (key) => fileKeys.push(key),
|
|
141
|
+
stripFallbacks: isBuild
|
|
142
|
+
// only strip in production
|
|
143
|
+
}
|
|
144
|
+
]
|
|
145
|
+
]
|
|
146
|
+
});
|
|
147
|
+
if (fileKeys.length > 0) {
|
|
148
|
+
extractedKeys.push(...fileKeys);
|
|
149
|
+
getGlobalState().extractedKeys = extractedKeys;
|
|
150
|
+
fileToKeys.set(
|
|
151
|
+
id,
|
|
152
|
+
fileKeys.map((k) => k.key)
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
if (isBuild && result?.code) {
|
|
156
|
+
return {
|
|
157
|
+
code: result.code,
|
|
158
|
+
map: result.map
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
},
|
|
163
|
+
generateBundle(_, bundle) {
|
|
164
|
+
chunkManifest = {};
|
|
165
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
166
|
+
if (chunk.type === "chunk" && chunk.moduleIds) {
|
|
167
|
+
const keys = /* @__PURE__ */ new Set();
|
|
168
|
+
let hasI18nModule = false;
|
|
169
|
+
for (const moduleId of chunk.moduleIds) {
|
|
170
|
+
const moduleKeys = fileToKeys.get(moduleId);
|
|
171
|
+
if (moduleKeys) {
|
|
172
|
+
moduleKeys.forEach((k) => keys.add(k));
|
|
173
|
+
}
|
|
174
|
+
if (filesWithI18n.has(moduleId)) {
|
|
175
|
+
hasI18nModule = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (keys.size > 0) {
|
|
179
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
180
|
+
chunkManifest[chunkName] = Array.from(keys);
|
|
181
|
+
}
|
|
182
|
+
if (hasI18nModule && chunk.type === "chunk") {
|
|
183
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
184
|
+
const loaderCode = `
|
|
185
|
+
/* @astroscope/i18n loader */
|
|
186
|
+
if (typeof window !== 'undefined' && window.__i18n__) {
|
|
187
|
+
const __i18n__ = window.__i18n__;
|
|
188
|
+
const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
|
|
189
|
+
if (__i18n_hash__) {
|
|
190
|
+
await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/* end @astroscope/i18n loader */
|
|
194
|
+
`;
|
|
195
|
+
chunk.code = loaderCode + chunk.code;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
getGlobalState().chunkManifest = chunkManifest;
|
|
200
|
+
const chunkCount = Object.keys(chunkManifest).length;
|
|
201
|
+
const keyCount = extractedKeys.length;
|
|
202
|
+
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
203
|
+
},
|
|
204
|
+
writeBundle(outputOptions) {
|
|
205
|
+
if (!isSSR && outputOptions.dir) {
|
|
206
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
207
|
+
...key,
|
|
208
|
+
file: projectRoot ? path2.relative(projectRoot, key.file) : key.file
|
|
209
|
+
}));
|
|
210
|
+
const manifest = {
|
|
211
|
+
keys: keysWithRelativePaths,
|
|
212
|
+
chunks: chunkManifest
|
|
213
|
+
};
|
|
214
|
+
const serverDir = path2.resolve(outputOptions.dir, "..", "server");
|
|
215
|
+
const manifestPath = path2.join(serverDir, MANIFEST_FILE_NAME);
|
|
216
|
+
if (fs.existsSync(serverDir)) {
|
|
217
|
+
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export {
|
|
225
|
+
getManifest,
|
|
226
|
+
i18nVitePlugin
|
|
227
|
+
};
|