@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,344 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkIdToName
|
|
3
|
+
} from "./chunk-4A5BKHFN.js";
|
|
4
|
+
|
|
5
|
+
// src/extraction/vite-plugin.ts
|
|
6
|
+
import { transformAsync } from "@babel/core";
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import path from "path";
|
|
9
|
+
|
|
10
|
+
// src/extraction/babel-plugin.ts
|
|
11
|
+
function getStringValue(t, node) {
|
|
12
|
+
if (t.isStringLiteral(node)) {
|
|
13
|
+
return node.value;
|
|
14
|
+
}
|
|
15
|
+
if (t.isTemplateLiteral(node) && node.quasis.length === 1 && node.expressions.length === 0) {
|
|
16
|
+
return node.quasis[0]?.value.cooked ?? null;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
function extractMeta(t, node) {
|
|
21
|
+
const stringValue = getStringValue(t, node);
|
|
22
|
+
if (stringValue !== null) {
|
|
23
|
+
return { fallback: stringValue };
|
|
24
|
+
}
|
|
25
|
+
if (t.isObjectExpression(node)) {
|
|
26
|
+
return extractMetaFromObject(t, node);
|
|
27
|
+
}
|
|
28
|
+
return { fallback: "" };
|
|
29
|
+
}
|
|
30
|
+
function extractMetaFromObject(t, obj) {
|
|
31
|
+
const meta = { fallback: "" };
|
|
32
|
+
for (const prop of obj.properties) {
|
|
33
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
34
|
+
if (!t.isIdentifier(prop.key)) continue;
|
|
35
|
+
const name = prop.key.name;
|
|
36
|
+
if (name === "fallback") {
|
|
37
|
+
const value = getStringValue(t, prop.value);
|
|
38
|
+
if (value !== null) {
|
|
39
|
+
meta.fallback = value;
|
|
40
|
+
}
|
|
41
|
+
} else if (name === "description") {
|
|
42
|
+
const value = getStringValue(t, prop.value);
|
|
43
|
+
if (value !== null) {
|
|
44
|
+
meta.description = value;
|
|
45
|
+
}
|
|
46
|
+
} else if (name === "variables" && t.isObjectExpression(prop.value)) {
|
|
47
|
+
meta.variables = extractVariables(t, prop.value);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return meta;
|
|
51
|
+
}
|
|
52
|
+
function extractVariables(t, obj) {
|
|
53
|
+
const result = {};
|
|
54
|
+
for (const prop of obj.properties) {
|
|
55
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
56
|
+
if (!t.isIdentifier(prop.key)) continue;
|
|
57
|
+
const varName = prop.key.name;
|
|
58
|
+
if (t.isObjectExpression(prop.value)) {
|
|
59
|
+
const varDef = {};
|
|
60
|
+
for (const varProp of prop.value.properties) {
|
|
61
|
+
if (!t.isObjectProperty(varProp)) continue;
|
|
62
|
+
if (!t.isIdentifier(varProp.key)) continue;
|
|
63
|
+
const varPropName = varProp.key.name;
|
|
64
|
+
const value = getStringValue(t, varProp.value);
|
|
65
|
+
if (value !== null) {
|
|
66
|
+
if (varPropName === "fallback") {
|
|
67
|
+
varDef.fallback = value;
|
|
68
|
+
} else if (varPropName === "description") {
|
|
69
|
+
varDef.description = value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
result[varName] = varDef;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
77
|
+
}
|
|
78
|
+
function isMetaArgument(t, node) {
|
|
79
|
+
if (t.isStringLiteral(node) || t.isTemplateLiteral(node)) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (t.isObjectExpression(node)) {
|
|
83
|
+
for (const prop of node.properties) {
|
|
84
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "fallback") {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
function i18nExtractPlugin({ types: t }) {
|
|
92
|
+
return {
|
|
93
|
+
name: "@astroscope/i18n/extract",
|
|
94
|
+
visitor: {
|
|
95
|
+
CallExpression(path2, state) {
|
|
96
|
+
if (!path2.get("callee").isIdentifier({ name: "t" })) return;
|
|
97
|
+
const args = path2.node.arguments;
|
|
98
|
+
if (args.length < 1) return;
|
|
99
|
+
const keyArg = args[0];
|
|
100
|
+
if (!t.isExpression(keyArg)) return;
|
|
101
|
+
const key = getStringValue(t, keyArg);
|
|
102
|
+
if (key === null) return;
|
|
103
|
+
let meta = { fallback: "" };
|
|
104
|
+
if (args.length >= 2 && t.isExpression(args[1]) && isMetaArgument(t, args[1])) {
|
|
105
|
+
meta = extractMeta(t, args[1]);
|
|
106
|
+
}
|
|
107
|
+
state.opts.onKeyExtracted({
|
|
108
|
+
key,
|
|
109
|
+
meta,
|
|
110
|
+
file: state.filename ?? "",
|
|
111
|
+
line: path2.node.loc?.start.line ?? 0
|
|
112
|
+
});
|
|
113
|
+
if (state.opts.stripFallbacks && args.length >= 2) {
|
|
114
|
+
const secondArg = args[1];
|
|
115
|
+
if (!t.isExpression(secondArg)) return;
|
|
116
|
+
if (isMetaArgument(t, secondArg)) {
|
|
117
|
+
if (args.length === 2) {
|
|
118
|
+
path2.node.arguments = [args[0]];
|
|
119
|
+
} else if (args.length >= 3) {
|
|
120
|
+
path2.node.arguments = [args[0], args[2]];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/extraction/vite-plugin.ts
|
|
130
|
+
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
131
|
+
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
132
|
+
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
133
|
+
var I18N_GLOBAL_KEY = "__astroscope_i18n_manifest__";
|
|
134
|
+
function getGlobalState() {
|
|
135
|
+
const g = globalThis;
|
|
136
|
+
if (!g[I18N_GLOBAL_KEY]) {
|
|
137
|
+
g[I18N_GLOBAL_KEY] = {
|
|
138
|
+
extractedKeys: [],
|
|
139
|
+
chunkManifest: {},
|
|
140
|
+
projectRoot: ""
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return g[I18N_GLOBAL_KEY];
|
|
144
|
+
}
|
|
145
|
+
function getManifest() {
|
|
146
|
+
const state = getGlobalState();
|
|
147
|
+
const keysWithRelativePaths = state.extractedKeys.map((key) => ({
|
|
148
|
+
...key,
|
|
149
|
+
file: state.projectRoot ? path.relative(state.projectRoot, key.file) : key.file
|
|
150
|
+
}));
|
|
151
|
+
return {
|
|
152
|
+
keys: keysWithRelativePaths,
|
|
153
|
+
chunks: state.chunkManifest
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function hasI18nImport(code) {
|
|
157
|
+
return /@astroscope\/i18n\/t/.test(code);
|
|
158
|
+
}
|
|
159
|
+
function getParserPlugins(filename) {
|
|
160
|
+
const plugins = [];
|
|
161
|
+
if (/\.tsx?$/.test(filename)) {
|
|
162
|
+
plugins.push("typescript");
|
|
163
|
+
}
|
|
164
|
+
if (/\.(jsx|tsx)$/.test(filename)) {
|
|
165
|
+
plugins.push("jsx");
|
|
166
|
+
}
|
|
167
|
+
return plugins;
|
|
168
|
+
}
|
|
169
|
+
function i18nVitePlugin(options) {
|
|
170
|
+
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
171
|
+
const extractedKeys = [];
|
|
172
|
+
const fileToKeys = /* @__PURE__ */ new Map();
|
|
173
|
+
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
174
|
+
let chunkManifest = {};
|
|
175
|
+
let projectRoot = "";
|
|
176
|
+
let isBuild = false;
|
|
177
|
+
let isSSR = false;
|
|
178
|
+
return {
|
|
179
|
+
name: "@astroscope/i18n/extract",
|
|
180
|
+
enforce: "pre",
|
|
181
|
+
configResolved(config) {
|
|
182
|
+
projectRoot = config.root;
|
|
183
|
+
getGlobalState().projectRoot = config.root;
|
|
184
|
+
isBuild = config.command === "build";
|
|
185
|
+
isSSR = !!config.build.ssr;
|
|
186
|
+
},
|
|
187
|
+
resolveId(id) {
|
|
188
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
189
|
+
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
190
|
+
}
|
|
191
|
+
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
192
|
+
return `\0${id}`;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
load(id) {
|
|
196
|
+
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
197
|
+
if (isBuild) {
|
|
198
|
+
return `
|
|
199
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
200
|
+
import { fileURLToPath } from 'node:url';
|
|
201
|
+
import { dirname, join } from 'node:path';
|
|
202
|
+
|
|
203
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
204
|
+
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
205
|
+
|
|
206
|
+
// manifest is emitted to server directory (not publicly accessible)
|
|
207
|
+
const searchPaths = [
|
|
208
|
+
join(__dirname, manifestName), // same dir (server/)
|
|
209
|
+
join(__dirname, '..', manifestName), // parent dir (from chunks/)
|
|
210
|
+
];
|
|
211
|
+
|
|
212
|
+
let manifestPath = searchPaths.find(p => existsSync(p));
|
|
213
|
+
if (!manifestPath) {
|
|
214
|
+
console.warn('[i18n] manifest not found, using empty manifest');
|
|
215
|
+
manifestPath = null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const manifestJson = manifestPath
|
|
219
|
+
? JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
220
|
+
: { keys: [], chunks: {} };
|
|
221
|
+
|
|
222
|
+
export const manifest = manifestJson;
|
|
223
|
+
export function getManifest() { return manifestJson; }
|
|
224
|
+
`;
|
|
225
|
+
}
|
|
226
|
+
return `
|
|
227
|
+
import { getManifest as _getManifest } from '@astroscope/i18n/extraction';
|
|
228
|
+
export const manifest = { keys: [], chunks: {} }; // placeholder, use getManifest()
|
|
229
|
+
export function getManifest() { return _getManifest(); }
|
|
230
|
+
`;
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
async transform(code, id) {
|
|
234
|
+
if (!include.some((ext) => id.endsWith(ext))) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
if (id.includes("node_modules")) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
if (!hasI18nImport(code)) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
filesWithI18n.add(id);
|
|
244
|
+
const fileKeys = [];
|
|
245
|
+
const result = await transformAsync(code, {
|
|
246
|
+
filename: id,
|
|
247
|
+
sourceMaps: true,
|
|
248
|
+
babelrc: false,
|
|
249
|
+
configFile: false,
|
|
250
|
+
parserOpts: {
|
|
251
|
+
plugins: getParserPlugins(id)
|
|
252
|
+
},
|
|
253
|
+
plugins: [
|
|
254
|
+
[
|
|
255
|
+
i18nExtractPlugin,
|
|
256
|
+
{
|
|
257
|
+
onKeyExtracted: (key) => fileKeys.push(key),
|
|
258
|
+
stripFallbacks: isBuild
|
|
259
|
+
// only strip in production
|
|
260
|
+
}
|
|
261
|
+
]
|
|
262
|
+
]
|
|
263
|
+
});
|
|
264
|
+
if (fileKeys.length > 0) {
|
|
265
|
+
extractedKeys.push(...fileKeys);
|
|
266
|
+
getGlobalState().extractedKeys = extractedKeys;
|
|
267
|
+
fileToKeys.set(
|
|
268
|
+
id,
|
|
269
|
+
fileKeys.map((k) => k.key)
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
if (isBuild && result?.code) {
|
|
273
|
+
return {
|
|
274
|
+
code: result.code,
|
|
275
|
+
map: result.map
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
},
|
|
280
|
+
generateBundle(_, bundle) {
|
|
281
|
+
chunkManifest = {};
|
|
282
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
283
|
+
if (chunk.type === "chunk" && chunk.moduleIds) {
|
|
284
|
+
const keys = /* @__PURE__ */ new Set();
|
|
285
|
+
let hasI18nModule = false;
|
|
286
|
+
for (const moduleId of chunk.moduleIds) {
|
|
287
|
+
const moduleKeys = fileToKeys.get(moduleId);
|
|
288
|
+
if (moduleKeys) {
|
|
289
|
+
moduleKeys.forEach((k) => keys.add(k));
|
|
290
|
+
}
|
|
291
|
+
if (filesWithI18n.has(moduleId)) {
|
|
292
|
+
hasI18nModule = true;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (keys.size > 0) {
|
|
296
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
297
|
+
chunkManifest[chunkName] = Array.from(keys);
|
|
298
|
+
}
|
|
299
|
+
if (hasI18nModule && chunk.type === "chunk") {
|
|
300
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
301
|
+
const loaderCode = `
|
|
302
|
+
/* @astroscope/i18n loader */
|
|
303
|
+
if (typeof window !== 'undefined' && window.__i18n__) {
|
|
304
|
+
const __i18n__ = window.__i18n__;
|
|
305
|
+
const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
|
|
306
|
+
if (__i18n_hash__) {
|
|
307
|
+
await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/* end @astroscope/i18n loader */
|
|
311
|
+
`;
|
|
312
|
+
chunk.code = loaderCode + chunk.code;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
getGlobalState().chunkManifest = chunkManifest;
|
|
317
|
+
const chunkCount = Object.keys(chunkManifest).length;
|
|
318
|
+
const keyCount = extractedKeys.length;
|
|
319
|
+
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
320
|
+
},
|
|
321
|
+
writeBundle(outputOptions) {
|
|
322
|
+
if (!isSSR && outputOptions.dir) {
|
|
323
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
324
|
+
...key,
|
|
325
|
+
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
326
|
+
}));
|
|
327
|
+
const manifest = {
|
|
328
|
+
keys: keysWithRelativePaths,
|
|
329
|
+
chunks: chunkManifest
|
|
330
|
+
};
|
|
331
|
+
const serverDir = path.resolve(outputOptions.dir, "..", "server");
|
|
332
|
+
const manifestPath = path.join(serverDir, MANIFEST_FILE_NAME);
|
|
333
|
+
if (fs.existsSync(serverDir)) {
|
|
334
|
+
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export {
|
|
342
|
+
getManifest,
|
|
343
|
+
i18nVitePlugin
|
|
344
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildI18nChunkUrl,
|
|
3
|
+
chunkIdToName
|
|
4
|
+
} from "./chunk-HHRMR3TM.js";
|
|
5
|
+
|
|
6
|
+
// src/client/import-with-i18n.ts
|
|
7
|
+
async function importx(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
|
+
importx
|
|
23
|
+
};
|
|
@@ -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}.${hash}.js`) : Promise.resolve()
|
|
8
|
+
]);
|
|
9
|
+
return module;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
importWithI18n
|
|
14
|
+
};
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkIdToName
|
|
3
|
+
} from "./chunk-4A5BKHFN.js";
|
|
4
|
+
|
|
5
|
+
// src/extraction/vite-plugin.ts
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
import path from "path";
|
|
8
|
+
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
9
|
+
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
10
|
+
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
11
|
+
var globalExtractedKeys = [];
|
|
12
|
+
var globalChunkManifest = {};
|
|
13
|
+
var globalProjectRoot = "";
|
|
14
|
+
function getManifest() {
|
|
15
|
+
const keysWithRelativePaths = globalExtractedKeys.map((key) => ({
|
|
16
|
+
...key,
|
|
17
|
+
file: globalProjectRoot ? path.relative(globalProjectRoot, key.file) : key.file
|
|
18
|
+
}));
|
|
19
|
+
return {
|
|
20
|
+
keys: keysWithRelativePaths,
|
|
21
|
+
chunks: globalChunkManifest
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function extractBalancedBraces(code, startIndex) {
|
|
25
|
+
if (code[startIndex] !== "{") return null;
|
|
26
|
+
let depth = 0;
|
|
27
|
+
let i = startIndex;
|
|
28
|
+
while (i < code.length) {
|
|
29
|
+
if (code[i] === "{") depth++;
|
|
30
|
+
else if (code[i] === "}") {
|
|
31
|
+
depth--;
|
|
32
|
+
if (depth === 0) return code.slice(startIndex, i + 1);
|
|
33
|
+
}
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
function extractTranslationCalls(code, filename) {
|
|
39
|
+
const keys = [];
|
|
40
|
+
const regex = /\bt\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*/g;
|
|
41
|
+
let match;
|
|
42
|
+
while ((match = regex.exec(code)) !== null) {
|
|
43
|
+
const key = match[1];
|
|
44
|
+
const afterKey = code.slice(match.index + match[0].length);
|
|
45
|
+
const upToMatch = code.slice(0, match.index);
|
|
46
|
+
const lineNumber = (upToMatch.match(/\n/g) || []).length + 1;
|
|
47
|
+
try {
|
|
48
|
+
const stringMatch = afterKey.match(/^['"`]([^'"`]*)['"`]/);
|
|
49
|
+
if (stringMatch) {
|
|
50
|
+
keys.push({
|
|
51
|
+
key,
|
|
52
|
+
meta: { fallback: stringMatch[1] },
|
|
53
|
+
file: filename,
|
|
54
|
+
line: lineNumber
|
|
55
|
+
});
|
|
56
|
+
} else if (afterKey.startsWith("{")) {
|
|
57
|
+
const metaStr = extractBalancedBraces(afterKey, 0);
|
|
58
|
+
if (metaStr) {
|
|
59
|
+
const fallbackMatch = metaStr.match(/fallback\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
60
|
+
const descMatch = metaStr.match(/description\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
61
|
+
const fallback = fallbackMatch ? fallbackMatch[1] : "";
|
|
62
|
+
const description = descMatch ? descMatch[1] : void 0;
|
|
63
|
+
const varsBlockMatch = metaStr.match(/variables\s*:\s*\{/);
|
|
64
|
+
let variables;
|
|
65
|
+
if (varsBlockMatch) {
|
|
66
|
+
const varsStart = metaStr.indexOf("{", varsBlockMatch.index + varsBlockMatch[0].length - 1);
|
|
67
|
+
const varsStr = extractBalancedBraces(metaStr, varsStart);
|
|
68
|
+
if (varsStr) {
|
|
69
|
+
variables = parseVariables(varsStr);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
keys.push({
|
|
73
|
+
key,
|
|
74
|
+
meta: { fallback, description, variables },
|
|
75
|
+
file: filename,
|
|
76
|
+
line: lineNumber
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return keys;
|
|
84
|
+
}
|
|
85
|
+
function parseVariables(str) {
|
|
86
|
+
const result = {};
|
|
87
|
+
const varNameRegex = /(\w+)\s*:\s*\{/g;
|
|
88
|
+
let nameMatch;
|
|
89
|
+
while ((nameMatch = varNameRegex.exec(str)) !== null) {
|
|
90
|
+
const varName = nameMatch[1];
|
|
91
|
+
const varStart = str.indexOf("{", nameMatch.index + nameMatch[0].length - 1);
|
|
92
|
+
const varObj = extractBalancedBraces(str, varStart);
|
|
93
|
+
if (varObj) {
|
|
94
|
+
const fallbackMatch = varObj.match(/fallback\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
95
|
+
const descMatch = varObj.match(/description\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
96
|
+
result[varName] = {
|
|
97
|
+
fallback: fallbackMatch ? fallbackMatch[1] : void 0,
|
|
98
|
+
description: descMatch ? descMatch[1] : void 0
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
103
|
+
}
|
|
104
|
+
function hasI18nImport(code) {
|
|
105
|
+
return /@astroscope\/i18n\/t/.test(code);
|
|
106
|
+
}
|
|
107
|
+
function i18nVitePlugin(options) {
|
|
108
|
+
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
109
|
+
const extractedKeys = [];
|
|
110
|
+
const fileToKeys = /* @__PURE__ */ new Map();
|
|
111
|
+
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
112
|
+
let chunkManifest = {};
|
|
113
|
+
let projectRoot = "";
|
|
114
|
+
let isBuild = false;
|
|
115
|
+
let isSSR = false;
|
|
116
|
+
return {
|
|
117
|
+
name: "@astroscope/i18n/extract",
|
|
118
|
+
enforce: "pre",
|
|
119
|
+
configResolved(config) {
|
|
120
|
+
projectRoot = config.root;
|
|
121
|
+
globalProjectRoot = config.root;
|
|
122
|
+
isBuild = config.command === "build";
|
|
123
|
+
isSSR = !!config.build.ssr;
|
|
124
|
+
},
|
|
125
|
+
resolveId(id) {
|
|
126
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
127
|
+
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
128
|
+
}
|
|
129
|
+
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
130
|
+
return `\0${id}`;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
load(id) {
|
|
134
|
+
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
135
|
+
if (isBuild) {
|
|
136
|
+
return `
|
|
137
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
138
|
+
import { fileURLToPath } from 'node:url';
|
|
139
|
+
import { dirname, join } from 'node:path';
|
|
140
|
+
|
|
141
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
142
|
+
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
143
|
+
|
|
144
|
+
// manifest is emitted to server directory (not publicly accessible)
|
|
145
|
+
const searchPaths = [
|
|
146
|
+
join(__dirname, manifestName), // same dir (server/)
|
|
147
|
+
join(__dirname, '..', manifestName), // parent dir (from chunks/)
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
let manifestPath = searchPaths.find(p => existsSync(p));
|
|
151
|
+
if (!manifestPath) {
|
|
152
|
+
console.warn('[i18n] manifest not found, using empty manifest');
|
|
153
|
+
manifestPath = null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const manifestJson = manifestPath
|
|
157
|
+
? JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
158
|
+
: { keys: [], chunks: {} };
|
|
159
|
+
|
|
160
|
+
export const manifest = manifestJson;
|
|
161
|
+
export function getManifest() { return manifestJson; }
|
|
162
|
+
`;
|
|
163
|
+
}
|
|
164
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
165
|
+
...key,
|
|
166
|
+
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
167
|
+
}));
|
|
168
|
+
const manifest = {
|
|
169
|
+
keys: keysWithRelativePaths,
|
|
170
|
+
chunks: chunkManifest
|
|
171
|
+
};
|
|
172
|
+
return `
|
|
173
|
+
export const manifest = ${JSON.stringify(manifest)};
|
|
174
|
+
export function getManifest() { return ${JSON.stringify(manifest)}; }
|
|
175
|
+
`;
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
transform(code, id) {
|
|
179
|
+
if (!include.some((ext) => id.endsWith(ext))) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
if (id.includes("node_modules")) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
if (hasI18nImport(code)) {
|
|
186
|
+
filesWithI18n.add(id);
|
|
187
|
+
}
|
|
188
|
+
const keys = extractTranslationCalls(code, id);
|
|
189
|
+
if (keys.length > 0) {
|
|
190
|
+
extractedKeys.push(...keys);
|
|
191
|
+
globalExtractedKeys = extractedKeys;
|
|
192
|
+
fileToKeys.set(
|
|
193
|
+
id,
|
|
194
|
+
keys.map((k) => k.key)
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
198
|
+
},
|
|
199
|
+
generateBundle(_, bundle) {
|
|
200
|
+
chunkManifest = {};
|
|
201
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
202
|
+
if (chunk.type === "chunk" && chunk.moduleIds) {
|
|
203
|
+
const keys = /* @__PURE__ */ new Set();
|
|
204
|
+
let hasI18nModule = false;
|
|
205
|
+
for (const moduleId of chunk.moduleIds) {
|
|
206
|
+
const moduleKeys = fileToKeys.get(moduleId);
|
|
207
|
+
if (moduleKeys) {
|
|
208
|
+
moduleKeys.forEach((k) => keys.add(k));
|
|
209
|
+
}
|
|
210
|
+
if (filesWithI18n.has(moduleId)) {
|
|
211
|
+
hasI18nModule = true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (keys.size > 0) {
|
|
215
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
216
|
+
chunkManifest[chunkName] = Array.from(keys);
|
|
217
|
+
}
|
|
218
|
+
if (hasI18nModule && chunk.type === "chunk") {
|
|
219
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
220
|
+
const loaderCode = `
|
|
221
|
+
/* @astroscope/i18n loader */
|
|
222
|
+
if (typeof window !== 'undefined' && window.__i18n__) {
|
|
223
|
+
const __i18n__ = window.__i18n__;
|
|
224
|
+
const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
|
|
225
|
+
if (__i18n_hash__) {
|
|
226
|
+
await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/* end @astroscope/i18n loader */
|
|
230
|
+
`;
|
|
231
|
+
chunk.code = loaderCode + chunk.code;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
globalChunkManifest = chunkManifest;
|
|
236
|
+
const chunkCount = Object.keys(chunkManifest).length;
|
|
237
|
+
const keyCount = extractedKeys.length;
|
|
238
|
+
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
239
|
+
},
|
|
240
|
+
writeBundle(outputOptions) {
|
|
241
|
+
if (!isSSR && outputOptions.dir) {
|
|
242
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
243
|
+
...key,
|
|
244
|
+
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
245
|
+
}));
|
|
246
|
+
const manifest = {
|
|
247
|
+
keys: keysWithRelativePaths,
|
|
248
|
+
chunks: chunkManifest
|
|
249
|
+
};
|
|
250
|
+
const serverDir = path.resolve(outputOptions.dir, "..", "server");
|
|
251
|
+
const manifestPath = path.join(serverDir, MANIFEST_FILE_NAME);
|
|
252
|
+
if (fs.existsSync(serverDir)) {
|
|
253
|
+
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export {
|
|
261
|
+
getManifest,
|
|
262
|
+
i18nVitePlugin
|
|
263
|
+
};
|