@astroscope/i18n 0.1.0 → 0.2.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.
Files changed (161) hide show
  1. package/README.md +21 -1
  2. package/dist/index.d.ts +23 -0
  3. package/dist/index.js +4 -0
  4. package/package.json +8 -7
  5. package/dist/babel-plugin-FUTQRKLT.js +0 -121
  6. package/dist/chunk-22ZZFFC2.js +0 -159
  7. package/dist/chunk-2PSXD3JJ.js +0 -144
  8. package/dist/chunk-3VI62RLT.js +0 -176
  9. package/dist/chunk-4DAQPWXI.js +0 -36
  10. package/dist/chunk-4OGX6AF5.js +0 -10
  11. package/dist/chunk-4PHYKXO3.js +0 -227
  12. package/dist/chunk-4UBFISFY.js +0 -376
  13. package/dist/chunk-5DERF6DV.js +0 -266
  14. package/dist/chunk-5KGNHFTC.js +0 -344
  15. package/dist/chunk-5PP4D2VK.js +0 -23
  16. package/dist/chunk-6BW3PN23.js +0 -14
  17. package/dist/chunk-6ESQ4JQI.js +0 -263
  18. package/dist/chunk-A7MPXMOP.js +0 -202
  19. package/dist/chunk-AWZ2SQDW.js +0 -149
  20. package/dist/chunk-AYY2YIBQ.js +0 -52
  21. package/dist/chunk-BMNEJFTD.js +0 -56
  22. package/dist/chunk-BSXQGEUK.js +0 -16
  23. package/dist/chunk-BSYTJFGF.js +0 -21
  24. package/dist/chunk-BVPXEGTT.js +0 -12
  25. package/dist/chunk-C4DJYWF6.js +0 -193
  26. package/dist/chunk-C5EJYE2P.js +0 -245
  27. package/dist/chunk-CLNOQHMS.js +0 -254
  28. package/dist/chunk-E5MCIRSU.js +0 -144
  29. package/dist/chunk-E7ZJS7WX.js +0 -162
  30. package/dist/chunk-ESFGCYYH.js +0 -24
  31. package/dist/chunk-FHBVI3TL.js +0 -372
  32. package/dist/chunk-FI5KZ5IO.js +0 -17
  33. package/dist/chunk-H5R6YMOL.js +0 -233
  34. package/dist/chunk-H62K3FGA.js +0 -236
  35. package/dist/chunk-HDG2R6XT.js +0 -211
  36. package/dist/chunk-HGXU5QAK.js +0 -344
  37. package/dist/chunk-HHRMR3TM.js +0 -17
  38. package/dist/chunk-HS4HVBAD.js +0 -127
  39. package/dist/chunk-IPJWKKHJ.js +0 -324
  40. package/dist/chunk-ITTL4V7J.js +0 -24
  41. package/dist/chunk-IWUBCMOK.js +0 -128
  42. package/dist/chunk-JJBBY2GR.js +0 -14
  43. package/dist/chunk-JTUKXNIJ.js +0 -23
  44. package/dist/chunk-K7YULG4A.js +0 -14
  45. package/dist/chunk-KNONI6DP.js +0 -89
  46. package/dist/chunk-LMTEO22Y.js +0 -17
  47. package/dist/chunk-LRLZEX4D.js +0 -147
  48. package/dist/chunk-MFKPLUAN.js +0 -241
  49. package/dist/chunk-MIWXQOO3.js +0 -316
  50. package/dist/chunk-MJRTAWPR.js +0 -261
  51. package/dist/chunk-MZEPQCXX.js +0 -249
  52. package/dist/chunk-NHHTSFA7.js +0 -237
  53. package/dist/chunk-NTTXM7AG.js +0 -23
  54. package/dist/chunk-NZFAVJXR.js +0 -192
  55. package/dist/chunk-OFNFSDW3.js +0 -13
  56. package/dist/chunk-PG3XDNGF.js +0 -227
  57. package/dist/chunk-PVEVTPNK.js +0 -139
  58. package/dist/chunk-QAO4BXS4.js +0 -244
  59. package/dist/chunk-QCTULQQK.js +0 -34
  60. package/dist/chunk-QKND2KCA.js +0 -163
  61. package/dist/chunk-QZEGY6RJ.js +0 -151
  62. package/dist/chunk-RAE2L6FT.js +0 -193
  63. package/dist/chunk-S3ABJBU3.js +0 -255
  64. package/dist/chunk-SVDVCPCV.js +0 -236
  65. package/dist/chunk-T35ZUWTL.js +0 -153
  66. package/dist/chunk-T6WV5DW6.js +0 -156
  67. package/dist/chunk-TGUXVDA3.js +0 -82
  68. package/dist/chunk-TYMO7K3A.js +0 -173
  69. package/dist/chunk-V4RBFLQL.js +0 -23
  70. package/dist/chunk-V4XWJXXZ.js +0 -23
  71. package/dist/chunk-VQCY3OKK.js +0 -258
  72. package/dist/chunk-WLGOJPDJ.js +0 -15
  73. package/dist/chunk-WUEPDJIL.js +0 -259
  74. package/dist/chunk-X3L7WZLY.js +0 -254
  75. package/dist/chunk-XDD62ZM3.js +0 -255
  76. package/dist/chunk-XFXXEDRD.js +0 -36
  77. package/dist/chunk-XKYOR5UK.js +0 -8
  78. package/dist/chunk-Y26535SI.js +0 -239
  79. package/dist/chunk-Y3PQX5TX.js +0 -152
  80. package/dist/chunk-Y4BE2V6F.js +0 -347
  81. package/dist/chunk-Z4IZYDIA.js +0 -24
  82. package/dist/chunk-Z52G4CYY.js +0 -14
  83. package/dist/chunk-ZGLUYWVM.js +0 -128
  84. package/dist/client/import-with-i18n.d.ts +0 -16
  85. package/dist/client/import-with-i18n.js +0 -8
  86. package/dist/client-directives/idle.d.ts +0 -9
  87. package/dist/client-directives/idle.js +0 -15
  88. package/dist/client-directives/load.d.ts +0 -9
  89. package/dist/client-directives/load.js +0 -15
  90. package/dist/client-directives/media.d.ts +0 -9
  91. package/dist/client-directives/media.js +0 -15
  92. package/dist/client-directives/only.d.ts +0 -9
  93. package/dist/client-directives/only.js +0 -15
  94. package/dist/client-directives/utils.d.ts +0 -9
  95. package/dist/client-directives/utils.js +0 -7
  96. package/dist/client-directives/visible.d.ts +0 -9
  97. package/dist/client-directives/visible.js +0 -15
  98. package/dist/i18n-3cZHoHwP.d.ts +0 -86
  99. package/dist/i18n-BHU81LMA.d.ts +0 -55
  100. package/dist/i18n-BagoeH4_.d.ts +0 -62
  101. package/dist/i18n-BeL84090.d.ts +0 -77
  102. package/dist/i18n-BeRWDfHi.d.ts +0 -55
  103. package/dist/i18n-Bfs2PVcf.d.ts +0 -79
  104. package/dist/i18n-BrL7p3aN.d.ts +0 -99
  105. package/dist/i18n-CNQNCFU0.d.ts +0 -55
  106. package/dist/i18n-CX_uU8V2.d.ts +0 -80
  107. package/dist/i18n-Cr1QZCwe.d.ts +0 -64
  108. package/dist/i18n-CzTRxGeX.d.ts +0 -80
  109. package/dist/i18n-DUOFiC7g.d.ts +0 -77
  110. package/dist/i18n-DcF47BHS.d.ts +0 -77
  111. package/dist/i18n-DifWvtaO.d.ts +0 -60
  112. package/dist/i18n-DumSqX1t.d.ts +0 -55
  113. package/dist/i18n-fWTJw12h.d.ts +0 -80
  114. package/dist/import-with-i18n.d.ts +0 -23
  115. package/dist/import-with-i18n.js +0 -7
  116. package/dist/init.d.ts +0 -2
  117. package/dist/init.js +0 -8
  118. package/dist/middleware-entrypoint-C0Drh9E_.d.ts +0 -29
  119. package/dist/middleware-entrypoint-mEl66IUy.d.ts +0 -29
  120. package/dist/middleware-entrypoint.d.ts +0 -5
  121. package/dist/middleware-entrypoint.js +0 -30
  122. package/dist/server/middleware-entrypoint.d.ts +0 -2
  123. package/dist/server/middleware-entrypoint.js +0 -28
  124. package/dist/t-BY_C9lZ1.d.ts +0 -30
  125. package/dist/t-CG3yDiEa.d.ts +0 -30
  126. package/dist/t-CHsBfrPr.d.ts +0 -30
  127. package/dist/t-DQpXaDhP.d.ts +0 -30
  128. package/dist/t.client.d.ts +0 -11
  129. package/dist/t.client.js +0 -35
  130. package/dist/t.server.d.ts +0 -22
  131. package/dist/t.server.js +0 -12
  132. package/dist/types-BC6K-pID.d.ts +0 -110
  133. package/dist/types-BUV7BZML.d.ts +0 -55
  134. package/dist/types-BWGB322F.d.ts +0 -46
  135. package/dist/types-BaaSiz9n.d.ts +0 -7
  136. package/dist/types-Bgl2V5xo.d.ts +0 -102
  137. package/dist/types-BwhB6IT4.d.ts +0 -111
  138. package/dist/types-C6rOJFY2.d.ts +0 -24
  139. package/dist/types-C7lD-Woj.d.ts +0 -50
  140. package/dist/types-CLWZeQhD.d.ts +0 -54
  141. package/dist/types-COXt613w.d.ts +0 -19
  142. package/dist/types-CUlkbl9-.d.ts +0 -50
  143. package/dist/types-CVv7GGPj.d.ts +0 -46
  144. package/dist/types-CiIDN2sW.d.ts +0 -64
  145. package/dist/types-Cr86qjYl.d.ts +0 -58
  146. package/dist/types-CxnObiEO.d.ts +0 -7
  147. package/dist/types-D44mFUjA.d.ts +0 -97
  148. package/dist/types-D85m9UTC.d.ts +0 -137
  149. package/dist/types-DWhsmSPP.d.ts +0 -102
  150. package/dist/types-DdprXGJ1.d.ts +0 -46
  151. package/dist/types-DhVJ2ZIo.d.ts +0 -43
  152. package/dist/types-DnbOuM1L.d.ts +0 -7
  153. package/dist/types-DsNK_9sI.d.ts +0 -24
  154. package/dist/types-DvWBr9Xp.d.ts +0 -17
  155. package/dist/types-DwkIalh6.d.ts +0 -24
  156. package/dist/types-OYRgjtE_.d.ts +0 -110
  157. package/dist/types-SV8S05GX.d.ts +0 -7
  158. package/dist/types-TYn9WnxY.d.ts +0 -46
  159. package/dist/types-aXCBbdhq.d.ts +0 -55
  160. package/dist/types-gn4aNKGx.d.ts +0 -54
  161. package/dist/types-nippsMkm.d.ts +0 -54
@@ -1,344 +0,0 @@
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
- import { transformAsync } from "@babel/core";
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
- };
@@ -1,17 +0,0 @@
1
- // src/shared/url.ts
2
- function chunkIdToName(chunkId) {
3
- return chunkId.replace(/^_astro\//, "");
4
- }
5
- function componentUrlToChunkName(componentUrl) {
6
- return componentUrl.replace(/^\/_astro\//, "").replace(/\.js$/, "");
7
- }
8
- function buildI18nChunkUrl(locale, chunkId, hash) {
9
- const chunkName = chunkIdToName(chunkId);
10
- return `/_i18n/${locale}/${chunkName}.${hash}.js`;
11
- }
12
-
13
- export {
14
- chunkIdToName,
15
- componentUrlToChunkName,
16
- buildI18nChunkUrl
17
- };
@@ -1,127 +0,0 @@
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 ?? {};
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
- i18n
127
- };