@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,258 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkIdToName
|
|
3
|
+
} from "./chunk-4A5BKHFN.js";
|
|
4
|
+
|
|
5
|
+
// src/extraction/vite-plugin.ts
|
|
6
|
+
import path from "path";
|
|
7
|
+
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
8
|
+
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
9
|
+
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
10
|
+
var globalExtractedKeys = [];
|
|
11
|
+
var globalChunkManifest = {};
|
|
12
|
+
var globalProjectRoot = "";
|
|
13
|
+
function getManifest() {
|
|
14
|
+
const keysWithRelativePaths = globalExtractedKeys.map((key) => ({
|
|
15
|
+
...key,
|
|
16
|
+
file: globalProjectRoot ? path.relative(globalProjectRoot, key.file) : key.file
|
|
17
|
+
}));
|
|
18
|
+
return {
|
|
19
|
+
keys: keysWithRelativePaths,
|
|
20
|
+
chunks: globalChunkManifest
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function extractBalancedBraces(code, startIndex) {
|
|
24
|
+
if (code[startIndex] !== "{") return null;
|
|
25
|
+
let depth = 0;
|
|
26
|
+
let i = startIndex;
|
|
27
|
+
while (i < code.length) {
|
|
28
|
+
if (code[i] === "{") depth++;
|
|
29
|
+
else if (code[i] === "}") {
|
|
30
|
+
depth--;
|
|
31
|
+
if (depth === 0) return code.slice(startIndex, i + 1);
|
|
32
|
+
}
|
|
33
|
+
i++;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function extractTranslationCalls(code, filename) {
|
|
38
|
+
const keys = [];
|
|
39
|
+
const regex = /\bt\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*/g;
|
|
40
|
+
let match;
|
|
41
|
+
while ((match = regex.exec(code)) !== null) {
|
|
42
|
+
const key = match[1];
|
|
43
|
+
const afterKey = code.slice(match.index + match[0].length);
|
|
44
|
+
const upToMatch = code.slice(0, match.index);
|
|
45
|
+
const lineNumber = (upToMatch.match(/\n/g) || []).length + 1;
|
|
46
|
+
try {
|
|
47
|
+
const stringMatch = afterKey.match(/^['"`]([^'"`]*)['"`]/);
|
|
48
|
+
if (stringMatch) {
|
|
49
|
+
keys.push({
|
|
50
|
+
key,
|
|
51
|
+
meta: { fallback: stringMatch[1] },
|
|
52
|
+
file: filename,
|
|
53
|
+
line: lineNumber
|
|
54
|
+
});
|
|
55
|
+
} else if (afterKey.startsWith("{")) {
|
|
56
|
+
const metaStr = extractBalancedBraces(afterKey, 0);
|
|
57
|
+
if (metaStr) {
|
|
58
|
+
const fallbackMatch = metaStr.match(/fallback\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
59
|
+
const descMatch = metaStr.match(/description\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
60
|
+
const fallback = fallbackMatch ? fallbackMatch[1] : "";
|
|
61
|
+
const description = descMatch ? descMatch[1] : void 0;
|
|
62
|
+
const varsBlockMatch = metaStr.match(/variables\s*:\s*\{/);
|
|
63
|
+
let variables;
|
|
64
|
+
if (varsBlockMatch) {
|
|
65
|
+
const varsStart = metaStr.indexOf("{", varsBlockMatch.index + varsBlockMatch[0].length - 1);
|
|
66
|
+
const varsStr = extractBalancedBraces(metaStr, varsStart);
|
|
67
|
+
if (varsStr) {
|
|
68
|
+
variables = parseVariables(varsStr);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
keys.push({
|
|
72
|
+
key,
|
|
73
|
+
meta: { fallback, description, variables },
|
|
74
|
+
file: filename,
|
|
75
|
+
line: lineNumber
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return keys;
|
|
83
|
+
}
|
|
84
|
+
function parseVariables(str) {
|
|
85
|
+
const result = {};
|
|
86
|
+
const varNameRegex = /(\w+)\s*:\s*\{/g;
|
|
87
|
+
let nameMatch;
|
|
88
|
+
while ((nameMatch = varNameRegex.exec(str)) !== null) {
|
|
89
|
+
const varName = nameMatch[1];
|
|
90
|
+
const varStart = str.indexOf("{", nameMatch.index + nameMatch[0].length - 1);
|
|
91
|
+
const varObj = extractBalancedBraces(str, varStart);
|
|
92
|
+
if (varObj) {
|
|
93
|
+
const fallbackMatch = varObj.match(/fallback\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
94
|
+
const descMatch = varObj.match(/description\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
95
|
+
result[varName] = {
|
|
96
|
+
fallback: fallbackMatch ? fallbackMatch[1] : void 0,
|
|
97
|
+
description: descMatch ? descMatch[1] : void 0
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
102
|
+
}
|
|
103
|
+
function hasI18nImport(code) {
|
|
104
|
+
return /@astroscope\/i18n\/t/.test(code);
|
|
105
|
+
}
|
|
106
|
+
function i18nVitePlugin(options) {
|
|
107
|
+
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
108
|
+
const extractedKeys = [];
|
|
109
|
+
const fileToKeys = /* @__PURE__ */ new Map();
|
|
110
|
+
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
111
|
+
let chunkManifest = {};
|
|
112
|
+
let projectRoot = "";
|
|
113
|
+
let isBuild = false;
|
|
114
|
+
return {
|
|
115
|
+
name: "@astroscope/i18n/extract",
|
|
116
|
+
enforce: "pre",
|
|
117
|
+
configResolved(config) {
|
|
118
|
+
projectRoot = config.root;
|
|
119
|
+
globalProjectRoot = config.root;
|
|
120
|
+
isBuild = config.command === "build";
|
|
121
|
+
},
|
|
122
|
+
resolveId(id) {
|
|
123
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
124
|
+
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
125
|
+
}
|
|
126
|
+
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
127
|
+
return `\0${id}`;
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
load(id) {
|
|
131
|
+
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
132
|
+
if (isBuild) {
|
|
133
|
+
return `
|
|
134
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
135
|
+
import { fileURLToPath } from 'node:url';
|
|
136
|
+
import { dirname, join } from 'node:path';
|
|
137
|
+
|
|
138
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
139
|
+
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
140
|
+
|
|
141
|
+
// try multiple locations to find the manifest
|
|
142
|
+
const searchPaths = [
|
|
143
|
+
join(__dirname, manifestName), // same dir
|
|
144
|
+
join(__dirname, '..', manifestName), // parent dir (server/)
|
|
145
|
+
join(__dirname, '..', '..', 'client', manifestName), // sibling client dir from chunks/
|
|
146
|
+
join(__dirname, '..', 'client', manifestName), // sibling client dir from server/
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
let manifestPath = searchPaths.find(p => existsSync(p));
|
|
150
|
+
if (!manifestPath) {
|
|
151
|
+
console.warn('[i18n] manifest not found, using empty manifest');
|
|
152
|
+
manifestPath = null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const manifestJson = manifestPath
|
|
156
|
+
? JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
157
|
+
: { keys: [], chunks: {} };
|
|
158
|
+
|
|
159
|
+
export const manifest = manifestJson;
|
|
160
|
+
export function getManifest() { return manifestJson; }
|
|
161
|
+
`;
|
|
162
|
+
}
|
|
163
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
164
|
+
...key,
|
|
165
|
+
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
166
|
+
}));
|
|
167
|
+
const manifest = {
|
|
168
|
+
keys: keysWithRelativePaths,
|
|
169
|
+
chunks: chunkManifest
|
|
170
|
+
};
|
|
171
|
+
return `
|
|
172
|
+
export const manifest = ${JSON.stringify(manifest)};
|
|
173
|
+
export function getManifest() { return ${JSON.stringify(manifest)}; }
|
|
174
|
+
`;
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
transform(code, id) {
|
|
178
|
+
if (!include.some((ext) => id.endsWith(ext))) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
if (id.includes("node_modules")) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
if (hasI18nImport(code)) {
|
|
185
|
+
filesWithI18n.add(id);
|
|
186
|
+
}
|
|
187
|
+
const keys = extractTranslationCalls(code, id);
|
|
188
|
+
if (keys.length > 0) {
|
|
189
|
+
extractedKeys.push(...keys);
|
|
190
|
+
globalExtractedKeys = extractedKeys;
|
|
191
|
+
fileToKeys.set(
|
|
192
|
+
id,
|
|
193
|
+
keys.map((k) => k.key)
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
},
|
|
198
|
+
generateBundle(_, bundle) {
|
|
199
|
+
chunkManifest = {};
|
|
200
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
201
|
+
if (chunk.type === "chunk" && chunk.moduleIds) {
|
|
202
|
+
const keys = /* @__PURE__ */ new Set();
|
|
203
|
+
let hasI18nModule = false;
|
|
204
|
+
for (const moduleId of chunk.moduleIds) {
|
|
205
|
+
const moduleKeys = fileToKeys.get(moduleId);
|
|
206
|
+
if (moduleKeys) {
|
|
207
|
+
moduleKeys.forEach((k) => keys.add(k));
|
|
208
|
+
}
|
|
209
|
+
if (filesWithI18n.has(moduleId)) {
|
|
210
|
+
hasI18nModule = true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (keys.size > 0) {
|
|
214
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
215
|
+
chunkManifest[chunkName] = Array.from(keys);
|
|
216
|
+
}
|
|
217
|
+
if (hasI18nModule && chunk.type === "chunk") {
|
|
218
|
+
const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
|
|
219
|
+
const loaderCode = `
|
|
220
|
+
/* @astroscope/i18n loader */
|
|
221
|
+
if (typeof window !== 'undefined' && window.__i18n__) {
|
|
222
|
+
const __i18n__ = window.__i18n__;
|
|
223
|
+
const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
|
|
224
|
+
if (__i18n_hash__) {
|
|
225
|
+
await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/* end @astroscope/i18n loader */
|
|
229
|
+
`;
|
|
230
|
+
chunk.code = loaderCode + chunk.code;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
globalChunkManifest = chunkManifest;
|
|
235
|
+
const keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
236
|
+
...key,
|
|
237
|
+
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
238
|
+
}));
|
|
239
|
+
const manifest = {
|
|
240
|
+
keys: keysWithRelativePaths,
|
|
241
|
+
chunks: chunkManifest
|
|
242
|
+
};
|
|
243
|
+
this.emitFile({
|
|
244
|
+
type: "asset",
|
|
245
|
+
fileName: MANIFEST_FILE_NAME,
|
|
246
|
+
source: JSON.stringify(manifest)
|
|
247
|
+
});
|
|
248
|
+
const chunkCount = Object.keys(chunkManifest).length;
|
|
249
|
+
const keyCount = extractedKeys.length;
|
|
250
|
+
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export {
|
|
256
|
+
getManifest,
|
|
257
|
+
i18nVitePlugin
|
|
258
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/client-directives/utils.ts
|
|
2
|
+
async function loadI18nForChunk(el) {
|
|
3
|
+
const componentUrl = el.getAttribute("component-url");
|
|
4
|
+
if (!componentUrl) return;
|
|
5
|
+
const chunkId = componentUrl.replace(/^\//, "").replace(/\.js$/, "");
|
|
6
|
+
const hash = window.__i18n_hashes__?.[chunkId];
|
|
7
|
+
const locale = window.__i18n_locale__ || "en";
|
|
8
|
+
if (hash) {
|
|
9
|
+
await import(`/_i18n/${locale}/${chunkId}.${hash}.js`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
loadI18nForChunk
|
|
15
|
+
};
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkIdToName
|
|
3
|
+
} from "./chunk-4A5BKHFN.js";
|
|
4
|
+
|
|
5
|
+
// src/extraction/vite-plugin.ts
|
|
6
|
+
import path from "path";
|
|
7
|
+
var VIRTUAL_MODULE_ID = "virtual:@astroscope/i18n/manifest";
|
|
8
|
+
var RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
|
|
9
|
+
var MANIFEST_FILE_NAME = "i18n-manifest.json";
|
|
10
|
+
var globalExtractedKeys = [];
|
|
11
|
+
var globalChunkManifest = {};
|
|
12
|
+
var globalProjectRoot = "";
|
|
13
|
+
function getManifest() {
|
|
14
|
+
const keysWithRelativePaths = globalExtractedKeys.map((key) => ({
|
|
15
|
+
...key,
|
|
16
|
+
file: globalProjectRoot ? path.relative(globalProjectRoot, key.file) : key.file
|
|
17
|
+
}));
|
|
18
|
+
return {
|
|
19
|
+
keys: keysWithRelativePaths,
|
|
20
|
+
chunks: globalChunkManifest
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function extractBalancedBraces(code, startIndex) {
|
|
24
|
+
if (code[startIndex] !== "{") return null;
|
|
25
|
+
let depth = 0;
|
|
26
|
+
let i = startIndex;
|
|
27
|
+
while (i < code.length) {
|
|
28
|
+
if (code[i] === "{") depth++;
|
|
29
|
+
else if (code[i] === "}") {
|
|
30
|
+
depth--;
|
|
31
|
+
if (depth === 0) return code.slice(startIndex, i + 1);
|
|
32
|
+
}
|
|
33
|
+
i++;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function extractTranslationCalls(code, filename) {
|
|
38
|
+
const keys = [];
|
|
39
|
+
const regex = /\bt\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*/g;
|
|
40
|
+
let match;
|
|
41
|
+
while ((match = regex.exec(code)) !== null) {
|
|
42
|
+
const key = match[1];
|
|
43
|
+
const afterKey = code.slice(match.index + match[0].length);
|
|
44
|
+
const upToMatch = code.slice(0, match.index);
|
|
45
|
+
const lineNumber = (upToMatch.match(/\n/g) || []).length + 1;
|
|
46
|
+
try {
|
|
47
|
+
const stringMatch = afterKey.match(/^['"`]([^'"`]*)['"`]/);
|
|
48
|
+
if (stringMatch) {
|
|
49
|
+
keys.push({
|
|
50
|
+
key,
|
|
51
|
+
meta: { fallback: stringMatch[1] },
|
|
52
|
+
file: filename,
|
|
53
|
+
line: lineNumber
|
|
54
|
+
});
|
|
55
|
+
} else if (afterKey.startsWith("{")) {
|
|
56
|
+
const metaStr = extractBalancedBraces(afterKey, 0);
|
|
57
|
+
if (metaStr) {
|
|
58
|
+
const fallbackMatch = metaStr.match(/fallback\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
59
|
+
const descMatch = metaStr.match(/description\s*:\s*['"`]([^'"`]+)['"`]/);
|
|
60
|
+
const fallback = fallbackMatch ? fallbackMatch[1] : "";
|
|
61
|
+
const description = descMatch ? descMatch[1] : void 0;
|
|
62
|
+
const varsBlockMatch = metaStr.match(/variables\s*:\s*\{/);
|
|
63
|
+
let variables;
|
|
64
|
+
if (varsBlockMatch) {
|
|
65
|
+
const varsStart = metaStr.indexOf("{", varsBlockMatch.index + varsBlockMatch[0].length - 1);
|
|
66
|
+
const varsStr = extractBalancedBraces(metaStr, varsStart);
|
|
67
|
+
if (varsStr) {
|
|
68
|
+
variables = parseVariables(varsStr);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
keys.push({
|
|
72
|
+
key,
|
|
73
|
+
meta: { fallback, description, variables },
|
|
74
|
+
file: filename,
|
|
75
|
+
line: lineNumber
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return keys;
|
|
83
|
+
}
|
|
84
|
+
function parseVariables(str) {
|
|
85
|
+
const result = {};
|
|
86
|
+
const varNameRegex = /(\w+)\s*:\s*\{/g;
|
|
87
|
+
let nameMatch;
|
|
88
|
+
while ((nameMatch = varNameRegex.exec(str)) !== null) {
|
|
89
|
+
const varName = nameMatch[1];
|
|
90
|
+
const varStart = str.indexOf("{", nameMatch.index + nameMatch[0].length - 1);
|
|
91
|
+
const varObj = extractBalancedBraces(str, varStart);
|
|
92
|
+
if (varObj) {
|
|
93
|
+
const fallbackMatch = varObj.match(/fallback\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
94
|
+
const descMatch = varObj.match(/description\s*:\s*['"`]([^'"`]*)['"`]/);
|
|
95
|
+
result[varName] = {
|
|
96
|
+
fallback: fallbackMatch ? fallbackMatch[1] : void 0,
|
|
97
|
+
description: descMatch ? descMatch[1] : void 0
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
102
|
+
}
|
|
103
|
+
function hasI18nImport(code) {
|
|
104
|
+
return /@astroscope\/i18n\/t/.test(code);
|
|
105
|
+
}
|
|
106
|
+
function i18nVitePlugin(options) {
|
|
107
|
+
const { include = [".ts", ".tsx", ".js", ".jsx", ".astro"], logger } = options;
|
|
108
|
+
const extractedKeys = [];
|
|
109
|
+
const fileToKeys = /* @__PURE__ */ new Map();
|
|
110
|
+
const filesWithI18n = /* @__PURE__ */ new Set();
|
|
111
|
+
let chunkManifest = {};
|
|
112
|
+
let projectRoot = "";
|
|
113
|
+
let isBuild = false;
|
|
114
|
+
return {
|
|
115
|
+
name: "@astroscope/i18n/extract",
|
|
116
|
+
enforce: "pre",
|
|
117
|
+
configResolved(config) {
|
|
118
|
+
projectRoot = config.root;
|
|
119
|
+
globalProjectRoot = config.root;
|
|
120
|
+
isBuild = config.command === "build";
|
|
121
|
+
},
|
|
122
|
+
resolveId(id) {
|
|
123
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
124
|
+
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
125
|
+
}
|
|
126
|
+
if (id.startsWith(`${VIRTUAL_MODULE_ID}?`)) {
|
|
127
|
+
return `\0${id}`;
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
load(id) {
|
|
131
|
+
if (id === RESOLVED_VIRTUAL_MODULE_ID || id.startsWith(`\0${VIRTUAL_MODULE_ID}?`)) {
|
|
132
|
+
if (isBuild) {
|
|
133
|
+
return `
|
|
134
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
135
|
+
import { fileURLToPath } from 'node:url';
|
|
136
|
+
import { dirname, join } from 'node:path';
|
|
137
|
+
|
|
138
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
139
|
+
const manifestName = '${MANIFEST_FILE_NAME}';
|
|
140
|
+
|
|
141
|
+
// try multiple locations to find the manifest
|
|
142
|
+
// prioritize client manifest (has full chunk mapping) over server manifest
|
|
143
|
+
const searchPaths = [
|
|
144
|
+
join(__dirname, '..', '..', 'client', manifestName), // sibling client dir from chunks/
|
|
145
|
+
join(__dirname, '..', 'client', manifestName), // sibling client dir from server/
|
|
146
|
+
join(__dirname, manifestName), // same dir
|
|
147
|
+
join(__dirname, '..', manifestName), // parent dir (server/)
|
|
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 keysWithRelativePaths = extractedKeys.map((key) => ({
|
|
237
|
+
...key,
|
|
238
|
+
file: projectRoot ? path.relative(projectRoot, key.file) : key.file
|
|
239
|
+
}));
|
|
240
|
+
const manifest = {
|
|
241
|
+
keys: keysWithRelativePaths,
|
|
242
|
+
chunks: chunkManifest
|
|
243
|
+
};
|
|
244
|
+
this.emitFile({
|
|
245
|
+
type: "asset",
|
|
246
|
+
fileName: MANIFEST_FILE_NAME,
|
|
247
|
+
source: JSON.stringify(manifest)
|
|
248
|
+
});
|
|
249
|
+
const chunkCount = Object.keys(chunkManifest).length;
|
|
250
|
+
const keyCount = extractedKeys.length;
|
|
251
|
+
logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export {
|
|
257
|
+
getManifest,
|
|
258
|
+
i18nVitePlugin
|
|
259
|
+
};
|