@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,347 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkIdToName
|
|
3
|
+
} from "./chunk-4A5BKHFN.js";
|
|
4
|
+
|
|
5
|
+
// src/extraction/manifest.ts
|
|
6
|
+
import path from "path";
|
|
7
|
+
var I18N_GLOBAL_KEY = "__astroscope_i18n_manifest__";
|
|
8
|
+
function getGlobalState() {
|
|
9
|
+
const g = globalThis;
|
|
10
|
+
if (!g[I18N_GLOBAL_KEY]) {
|
|
11
|
+
g[I18N_GLOBAL_KEY] = {
|
|
12
|
+
extractedKeys: [],
|
|
13
|
+
chunkManifest: {},
|
|
14
|
+
projectRoot: ""
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return g[I18N_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
|
+
import { transformAsync } from "@babel/core";
|
|
35
|
+
|
|
36
|
+
// src/extraction/babel-plugin.ts
|
|
37
|
+
function getStringValue(t, node) {
|
|
38
|
+
if (t.isStringLiteral(node)) {
|
|
39
|
+
return node.value;
|
|
40
|
+
}
|
|
41
|
+
if (t.isTemplateLiteral(node) && node.quasis.length === 1 && node.expressions.length === 0) {
|
|
42
|
+
return node.quasis[0]?.value.cooked ?? null;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
function extractMeta(t, node) {
|
|
47
|
+
const stringValue = getStringValue(t, node);
|
|
48
|
+
if (stringValue !== null) {
|
|
49
|
+
return { fallback: stringValue };
|
|
50
|
+
}
|
|
51
|
+
if (t.isObjectExpression(node)) {
|
|
52
|
+
return extractMetaFromObject(t, node);
|
|
53
|
+
}
|
|
54
|
+
return { fallback: "" };
|
|
55
|
+
}
|
|
56
|
+
function extractMetaFromObject(t, obj) {
|
|
57
|
+
const meta = { fallback: "" };
|
|
58
|
+
for (const prop of obj.properties) {
|
|
59
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
60
|
+
if (!t.isIdentifier(prop.key)) continue;
|
|
61
|
+
const name = prop.key.name;
|
|
62
|
+
if (name === "fallback") {
|
|
63
|
+
const value = getStringValue(t, prop.value);
|
|
64
|
+
if (value !== null) {
|
|
65
|
+
meta.fallback = value;
|
|
66
|
+
}
|
|
67
|
+
} else if (name === "description") {
|
|
68
|
+
const value = getStringValue(t, prop.value);
|
|
69
|
+
if (value !== null) {
|
|
70
|
+
meta.description = value;
|
|
71
|
+
}
|
|
72
|
+
} else if (name === "variables" && t.isObjectExpression(prop.value)) {
|
|
73
|
+
meta.variables = extractVariables(t, prop.value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return meta;
|
|
77
|
+
}
|
|
78
|
+
function extractVariables(t, obj) {
|
|
79
|
+
const result = {};
|
|
80
|
+
for (const prop of obj.properties) {
|
|
81
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
82
|
+
if (!t.isIdentifier(prop.key)) continue;
|
|
83
|
+
const varName = prop.key.name;
|
|
84
|
+
if (t.isObjectExpression(prop.value)) {
|
|
85
|
+
const varDef = {};
|
|
86
|
+
for (const varProp of prop.value.properties) {
|
|
87
|
+
if (!t.isObjectProperty(varProp)) continue;
|
|
88
|
+
if (!t.isIdentifier(varProp.key)) continue;
|
|
89
|
+
const varPropName = varProp.key.name;
|
|
90
|
+
const value = getStringValue(t, varProp.value);
|
|
91
|
+
if (value !== null) {
|
|
92
|
+
if (varPropName === "fallback") {
|
|
93
|
+
varDef.fallback = value;
|
|
94
|
+
} else if (varPropName === "description") {
|
|
95
|
+
varDef.description = value;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
result[varName] = varDef;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
103
|
+
}
|
|
104
|
+
function isMetaArgument(t, node) {
|
|
105
|
+
if (t.isStringLiteral(node) || t.isTemplateLiteral(node)) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
if (t.isObjectExpression(node)) {
|
|
109
|
+
for (const prop of node.properties) {
|
|
110
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "fallback") {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
function i18nExtractPlugin({ types: t }) {
|
|
118
|
+
return {
|
|
119
|
+
name: "@astroscope/i18n/extract",
|
|
120
|
+
visitor: {
|
|
121
|
+
CallExpression(path3, state) {
|
|
122
|
+
if (!path3.get("callee").isIdentifier({ name: "t" })) return;
|
|
123
|
+
const args = path3.node.arguments;
|
|
124
|
+
if (args.length < 1) return;
|
|
125
|
+
const keyArg = args[0];
|
|
126
|
+
if (!t.isExpression(keyArg)) return;
|
|
127
|
+
const key = getStringValue(t, keyArg);
|
|
128
|
+
if (key === null) return;
|
|
129
|
+
let meta = { fallback: "" };
|
|
130
|
+
if (args.length >= 2 && t.isExpression(args[1]) && isMetaArgument(t, args[1])) {
|
|
131
|
+
meta = extractMeta(t, args[1]);
|
|
132
|
+
}
|
|
133
|
+
state.opts.onKeyExtracted({
|
|
134
|
+
key,
|
|
135
|
+
meta,
|
|
136
|
+
file: state.filename ?? "",
|
|
137
|
+
line: path3.node.loc?.start.line ?? 0
|
|
138
|
+
});
|
|
139
|
+
if (state.opts.stripFallbacks && args.length >= 2) {
|
|
140
|
+
const secondArg = args[1];
|
|
141
|
+
if (!t.isExpression(secondArg)) return;
|
|
142
|
+
if (isMetaArgument(t, secondArg)) {
|
|
143
|
+
if (args.length === 2) {
|
|
144
|
+
path3.node.arguments = [args[0]];
|
|
145
|
+
} else if (args.length >= 3) {
|
|
146
|
+
path3.node.arguments = [args[0], args[2]];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/extraction/vite-plugin.ts
|
|
156
|
+
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
157
|
+
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
158
|
+
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
159
|
+
function hasI18nImport(code) {
|
|
160
|
+
return /@astroscope\/i18n\/t/.test(code);
|
|
161
|
+
}
|
|
162
|
+
function getParserPlugins(filename) {
|
|
163
|
+
const plugins = [];
|
|
164
|
+
if (/\.tsx?$/.test(filename)) {
|
|
165
|
+
plugins.push("typescript");
|
|
166
|
+
}
|
|
167
|
+
if (/\.(jsx|tsx)$/.test(filename)) {
|
|
168
|
+
plugins.push("jsx");
|
|
169
|
+
}
|
|
170
|
+
return plugins;
|
|
171
|
+
}
|
|
172
|
+
function i18nVitePlugin(options) {
|
|
173
|
+
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
174
|
+
const extractedKeys = [];
|
|
175
|
+
const fileToKeys = /* @__PURE__ */ new Map();
|
|
176
|
+
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
177
|
+
let chunkManifest = {};
|
|
178
|
+
let projectRoot = "";
|
|
179
|
+
let isBuild = false;
|
|
180
|
+
let isSSR = false;
|
|
181
|
+
return {
|
|
182
|
+
name: "@astroscope/i18n/extract",
|
|
183
|
+
enforce: "pre",
|
|
184
|
+
configResolved(config) {
|
|
185
|
+
projectRoot = config.root;
|
|
186
|
+
getGlobalState().projectRoot = config.root;
|
|
187
|
+
isBuild = config.command === "build";
|
|
188
|
+
isSSR = !!config.build.ssr;
|
|
189
|
+
},
|
|
190
|
+
resolveId(id) {
|
|
191
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
192
|
+
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
193
|
+
}
|
|
194
|
+
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
195
|
+
return `\0${id}`;
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
load(id) {
|
|
199
|
+
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
200
|
+
if (isBuild) {
|
|
201
|
+
return `
|
|
202
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
203
|
+
import { fileURLToPath } from 'node:url';
|
|
204
|
+
import { dirname, join } from 'node:path';
|
|
205
|
+
|
|
206
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
207
|
+
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
208
|
+
|
|
209
|
+
// manifest is emitted to server directory (not publicly accessible)
|
|
210
|
+
const searchPaths = [
|
|
211
|
+
join(__dirname, manifestName), // same dir (server/)
|
|
212
|
+
join(__dirname, '..', manifestName), // parent dir (from chunks/)
|
|
213
|
+
];
|
|
214
|
+
|
|
215
|
+
let manifestPath = searchPaths.find(p => existsSync(p));
|
|
216
|
+
if (!manifestPath) {
|
|
217
|
+
console.warn('[i18n] manifest not found, using empty manifest');
|
|
218
|
+
manifestPath = null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const manifestJson = manifestPath
|
|
222
|
+
? JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
223
|
+
: { keys: [], chunks: {} };
|
|
224
|
+
|
|
225
|
+
export const manifest = manifestJson;
|
|
226
|
+
export function getManifest() { return manifestJson; }
|
|
227
|
+
`;
|
|
228
|
+
}
|
|
229
|
+
return `
|
|
230
|
+
import { getManifest as _getManifest } from '@astroscope/i18n/extraction';
|
|
231
|
+
export const manifest = { keys: [], chunks: {} }; // placeholder, use getManifest()
|
|
232
|
+
export function getManifest() { return _getManifest(); }
|
|
233
|
+
`;
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
async transform(code, id) {
|
|
237
|
+
if (!include.some((ext) => id.endsWith(ext))) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
if (id.includes("node_modules")) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
if (!hasI18nImport(code)) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
filesWithI18n.add(id);
|
|
247
|
+
const fileKeys = [];
|
|
248
|
+
const result = await transformAsync(code, {
|
|
249
|
+
filename: id,
|
|
250
|
+
sourceMaps: true,
|
|
251
|
+
babelrc: false,
|
|
252
|
+
configFile: false,
|
|
253
|
+
parserOpts: {
|
|
254
|
+
plugins: getParserPlugins(id)
|
|
255
|
+
},
|
|
256
|
+
plugins: [
|
|
257
|
+
[
|
|
258
|
+
i18nExtractPlugin,
|
|
259
|
+
{
|
|
260
|
+
onKeyExtracted: (key) => fileKeys.push(key),
|
|
261
|
+
stripFallbacks: isBuild
|
|
262
|
+
// only strip in production
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
]
|
|
266
|
+
});
|
|
267
|
+
if (fileKeys.length > 0) {
|
|
268
|
+
extractedKeys.push(...fileKeys);
|
|
269
|
+
getGlobalState().extractedKeys = extractedKeys;
|
|
270
|
+
fileToKeys.set(
|
|
271
|
+
id,
|
|
272
|
+
fileKeys.map((k) => k.key)
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
if (isBuild && result?.code) {
|
|
276
|
+
return {
|
|
277
|
+
code: result.code,
|
|
278
|
+
map: result.map
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
},
|
|
283
|
+
generateBundle(_, bundle) {
|
|
284
|
+
chunkManifest = {};
|
|
285
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
286
|
+
if (chunk.type === "chunk" && chunk.moduleIds) {
|
|
287
|
+
const keys = /* @__PURE__ */ new Set();
|
|
288
|
+
let hasI18nModule = false;
|
|
289
|
+
for (const moduleId of chunk.moduleIds) {
|
|
290
|
+
const moduleKeys = fileToKeys.get(moduleId);
|
|
291
|
+
if (moduleKeys) {
|
|
292
|
+
moduleKeys.forEach((k) => keys.add(k));
|
|
293
|
+
}
|
|
294
|
+
if (filesWithI18n.has(moduleId)) {
|
|
295
|
+
hasI18nModule = true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (keys.size > 0) {
|
|
299
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
300
|
+
chunkManifest[chunkName] = Array.from(keys);
|
|
301
|
+
}
|
|
302
|
+
if (hasI18nModule && chunk.type === "chunk") {
|
|
303
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
304
|
+
const loaderCode = `
|
|
305
|
+
/* @astroscope/i18n loader */
|
|
306
|
+
if (typeof window !== 'undefined' && window.__i18n__) {
|
|
307
|
+
const __i18n__ = window.__i18n__;
|
|
308
|
+
const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
|
|
309
|
+
if (__i18n_hash__) {
|
|
310
|
+
await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/* end @astroscope/i18n loader */
|
|
314
|
+
`;
|
|
315
|
+
chunk.code = loaderCode + chunk.code;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
getGlobalState().chunkManifest = chunkManifest;
|
|
320
|
+
const chunkCount = Object.keys(chunkManifest).length;
|
|
321
|
+
const keyCount = extractedKeys.length;
|
|
322
|
+
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
323
|
+
},
|
|
324
|
+
writeBundle(outputOptions) {
|
|
325
|
+
if (!isSSR && outputOptions.dir) {
|
|
326
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
327
|
+
...key,
|
|
328
|
+
file: projectRoot ? path2.relative(projectRoot, key.file) : key.file
|
|
329
|
+
}));
|
|
330
|
+
const manifest = {
|
|
331
|
+
keys: keysWithRelativePaths,
|
|
332
|
+
chunks: chunkManifest
|
|
333
|
+
};
|
|
334
|
+
const serverDir = path2.resolve(outputOptions.dir, "..", "server");
|
|
335
|
+
const manifestPath = path2.join(serverDir, MANIFEST_FILE_NAME);
|
|
336
|
+
if (fs.existsSync(serverDir)) {
|
|
337
|
+
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export {
|
|
345
|
+
getManifest,
|
|
346
|
+
i18nVitePlugin
|
|
347
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildI18nChunkUrl,
|
|
3
|
+
componentUrlToChunkName
|
|
4
|
+
} from "./chunk-HHRMR3TM.js";
|
|
5
|
+
|
|
6
|
+
// src/client/directives/utils.ts
|
|
7
|
+
async function loadI18nForChunk(el) {
|
|
8
|
+
const componentUrl = el.getAttribute("component-url");
|
|
9
|
+
if (!componentUrl) return;
|
|
10
|
+
const chunkName = componentUrlToChunkName(componentUrl);
|
|
11
|
+
const i18n = window.__i18n__;
|
|
12
|
+
const hash = i18n.hashes[chunkName];
|
|
13
|
+
if (hash) {
|
|
14
|
+
const url = buildI18nChunkUrl(i18n.locale, chunkName, hash);
|
|
15
|
+
await import(
|
|
16
|
+
/* @vite-ignore */
|
|
17
|
+
url
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
loadI18nForChunk
|
|
24
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/import-with-i18n.ts
|
|
2
|
+
async function importWithI18n(importFn, chunkId) {
|
|
3
|
+
const hash = chunkId ? window.__i18n_hashes__?.[chunkId] : void 0;
|
|
4
|
+
const locale = window.__i18n_locale__ || "en";
|
|
5
|
+
const [module] = await Promise.all([
|
|
6
|
+
importFn(),
|
|
7
|
+
hash ? import(`/_i18n/${locale}/${chunkId?.replace(/^_astro\//, "")}.${hash}.js`) : Promise.resolve()
|
|
8
|
+
]);
|
|
9
|
+
return module;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
importWithI18n
|
|
14
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
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
|
+
this.config = config;
|
|
57
|
+
}
|
|
58
|
+
getConfig() {
|
|
59
|
+
return this.config;
|
|
60
|
+
}
|
|
61
|
+
getDefaultLocale() {
|
|
62
|
+
return this.config?.defaultLocale ?? this.config?.locales[0];
|
|
63
|
+
}
|
|
64
|
+
getFallbackBehavior() {
|
|
65
|
+
return this.config?.fallback ?? "fallback";
|
|
66
|
+
}
|
|
67
|
+
setTranslations(locale, raw) {
|
|
68
|
+
this.rawCache.set(locale, raw);
|
|
69
|
+
this.compiledCache.set(locale, compileTranslations(locale, raw));
|
|
70
|
+
this.hashCache.delete(locale);
|
|
71
|
+
const chunks = this.getChunkManifest();
|
|
72
|
+
if (chunks) {
|
|
73
|
+
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
getTranslations(locale) {
|
|
77
|
+
return this.rawCache.get(locale);
|
|
78
|
+
}
|
|
79
|
+
getCompiledTranslations(locale) {
|
|
80
|
+
return this.compiledCache.get(locale);
|
|
81
|
+
}
|
|
82
|
+
getChunkManifest() {
|
|
83
|
+
return this.getExtractionManifest()?.chunks ?? null;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get extracted translation keys with metadata.
|
|
87
|
+
* Use this to populate your CMS with keys extracted from the codebase.
|
|
88
|
+
*/
|
|
89
|
+
getExtractedKeys() {
|
|
90
|
+
return this.getExtractionManifest()?.keys ?? null;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get the full extraction manifest (keys + chunks).
|
|
94
|
+
* Returns null in dev mode (manifest doesn't exist).
|
|
95
|
+
*/
|
|
96
|
+
getExtractionManifest() {
|
|
97
|
+
if (this.extractionManifest === void 0) {
|
|
98
|
+
this.extractionManifest = loadExtractionManifest() ?? null;
|
|
99
|
+
}
|
|
100
|
+
return this.extractionManifest;
|
|
101
|
+
}
|
|
102
|
+
getHashes(locale) {
|
|
103
|
+
return this.hashCache.get(locale);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Clear translations for specified locale(s).
|
|
107
|
+
* @param locales - Locale(s) to clear, or all if not specified
|
|
108
|
+
*/
|
|
109
|
+
clear(locales) {
|
|
110
|
+
const targets = this.resolveLocales(locales);
|
|
111
|
+
for (const locale of targets) {
|
|
112
|
+
this.rawCache.delete(locale);
|
|
113
|
+
this.compiledCache.delete(locale);
|
|
114
|
+
this.hashCache.delete(locale);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
resolveLocales(locales) {
|
|
118
|
+
if (!locales) return this.config?.locales ?? [];
|
|
119
|
+
if (typeof locales === "string") return [locales];
|
|
120
|
+
return locales;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
var i18n = new I18nSingleton();
|
|
124
|
+
|
|
125
|
+
export {
|
|
126
|
+
computeChunkHash,
|
|
127
|
+
i18n
|
|
128
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ClientDirective } from 'astro';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* i18n-aware client:idle directive
|
|
5
|
+
* Loads translations in parallel before delegating to Astro's original idle directive
|
|
6
|
+
*/
|
|
7
|
+
declare const idleDirective: ClientDirective;
|
|
8
|
+
|
|
9
|
+
export { idleDirective as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadI18nForChunk
|
|
3
|
+
} from "../../chunk-FAKBSLNW.js";
|
|
4
|
+
import "../../chunk-4A5BKHFN.js";
|
|
5
|
+
|
|
6
|
+
// src/client/directives/idle.ts
|
|
7
|
+
import originalIdle from "astro/client/idle.js";
|
|
8
|
+
var idleDirective = async (load, options, el) => {
|
|
9
|
+
await loadI18nForChunk(el);
|
|
10
|
+
return originalIdle(load, options, el);
|
|
11
|
+
};
|
|
12
|
+
var idle_default = idleDirective;
|
|
13
|
+
export {
|
|
14
|
+
idle_default as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ClientDirective } from 'astro';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* i18n-aware client:load directive
|
|
5
|
+
* Loads translations in parallel before delegating to Astro's original load directive
|
|
6
|
+
*/
|
|
7
|
+
declare const loadDirective: ClientDirective;
|
|
8
|
+
|
|
9
|
+
export { loadDirective as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadI18nForChunk
|
|
3
|
+
} from "../../chunk-FAKBSLNW.js";
|
|
4
|
+
import "../../chunk-4A5BKHFN.js";
|
|
5
|
+
|
|
6
|
+
// src/client/directives/load.ts
|
|
7
|
+
import originalLoad from "astro/client/load.js";
|
|
8
|
+
var loadDirective = async (load, options, el) => {
|
|
9
|
+
await loadI18nForChunk(el);
|
|
10
|
+
return originalLoad(load, options, el);
|
|
11
|
+
};
|
|
12
|
+
var load_default = loadDirective;
|
|
13
|
+
export {
|
|
14
|
+
load_default as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ClientDirective } from 'astro';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* i18n-aware client:media directive
|
|
5
|
+
* Loads translations in parallel before delegating to Astro's original media directive
|
|
6
|
+
*/
|
|
7
|
+
declare const mediaDirective: ClientDirective;
|
|
8
|
+
|
|
9
|
+
export { mediaDirective as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadI18nForChunk
|
|
3
|
+
} from "../../chunk-FAKBSLNW.js";
|
|
4
|
+
import "../../chunk-4A5BKHFN.js";
|
|
5
|
+
|
|
6
|
+
// src/client/directives/media.ts
|
|
7
|
+
import originalMedia from "astro/client/media.js";
|
|
8
|
+
var mediaDirective = async (load, options, el) => {
|
|
9
|
+
await loadI18nForChunk(el);
|
|
10
|
+
return originalMedia(load, options, el);
|
|
11
|
+
};
|
|
12
|
+
var media_default = mediaDirective;
|
|
13
|
+
export {
|
|
14
|
+
media_default as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ClientDirective } from 'astro';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* i18n-aware client:only directive
|
|
5
|
+
* Loads translations in parallel before delegating to Astro's original only directive
|
|
6
|
+
*/
|
|
7
|
+
declare const onlyDirective: ClientDirective;
|
|
8
|
+
|
|
9
|
+
export { onlyDirective as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadI18nForChunk
|
|
3
|
+
} from "../../chunk-FAKBSLNW.js";
|
|
4
|
+
import "../../chunk-4A5BKHFN.js";
|
|
5
|
+
|
|
6
|
+
// src/client/directives/only.ts
|
|
7
|
+
import originalOnly from "astro/client/only.js";
|
|
8
|
+
var onlyDirective = async (load, options, el) => {
|
|
9
|
+
await loadI18nForChunk(el);
|
|
10
|
+
return originalOnly(load, options, el);
|
|
11
|
+
};
|
|
12
|
+
var only_default = onlyDirective;
|
|
13
|
+
export {
|
|
14
|
+
only_default as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import '../../types-B_-RxW4A.js';
|
|
2
|
+
import '../../types-Bzp6KxsI.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Load translations for a chunk before hydration
|
|
6
|
+
* Extracts chunk name from astro-island's component-url attribute
|
|
7
|
+
*/
|
|
8
|
+
declare function loadI18nForChunk(el: HTMLElement): Promise<void>;
|
|
9
|
+
|
|
10
|
+
export { loadI18nForChunk };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ClientDirective } from 'astro';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* i18n-aware client:visible directive
|
|
5
|
+
* Loads translations in parallel before delegating to Astro's original visible directive
|
|
6
|
+
*/
|
|
7
|
+
declare const visibleDirective: ClientDirective;
|
|
8
|
+
|
|
9
|
+
export { visibleDirective as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadI18nForChunk
|
|
3
|
+
} from "../../chunk-FAKBSLNW.js";
|
|
4
|
+
import "../../chunk-4A5BKHFN.js";
|
|
5
|
+
|
|
6
|
+
// src/client/directives/visible.ts
|
|
7
|
+
import originalVisible from "astro/client/visible.js";
|
|
8
|
+
var visibleDirective = async (load, options, el) => {
|
|
9
|
+
await loadI18nForChunk(el);
|
|
10
|
+
return originalVisible(load, options, el);
|
|
11
|
+
};
|
|
12
|
+
var visible_default = visibleDirective;
|
|
13
|
+
export {
|
|
14
|
+
visible_default as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated Not needed - translations load automatically via chunk loader.
|
|
3
|
+
* Use regular dynamic import() instead.
|
|
4
|
+
*/
|
|
5
|
+
declare function importx<T>(importFn: () => Promise<T>): Promise<T>;
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated Not needed - translations load automatically via chunk loader.
|
|
8
|
+
* Use regular React.lazy() instead.
|
|
9
|
+
*/
|
|
10
|
+
declare function lazyx<T>(importFn: () => Promise<{
|
|
11
|
+
default: T;
|
|
12
|
+
}>): () => Promise<{
|
|
13
|
+
default: T;
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
export { importx, lazyx };
|