@astroscope/i18n 0.1.0 → 0.2.1

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 (188) hide show
  1. package/README.md +23 -1
  2. package/dist/{chunk-FAKBSLNW.js → chunk-7MSGHEST.js} +14 -7
  3. package/dist/{chunk-JJEDOJBR.js → chunk-EO4FUQXR.js} +49 -18
  4. package/dist/{chunk-QFAAGCP5.js → chunk-RHWEV4L2.js} +58 -1
  5. package/dist/client/directives/idle.d.ts +4 -1
  6. package/dist/client/directives/idle.js +13 -6
  7. package/dist/client/directives/load.d.ts +4 -1
  8. package/dist/client/directives/load.js +6 -6
  9. package/dist/client/directives/media.d.ts +4 -1
  10. package/dist/client/directives/media.js +16 -6
  11. package/dist/client/directives/only.d.ts +6 -1
  12. package/dist/client/directives/only.js +6 -6
  13. package/dist/client/directives/utils.d.ts +6 -5
  14. package/dist/client/directives/utils.js +3 -3
  15. package/dist/client/directives/visible.d.ts +4 -1
  16. package/dist/client/directives/visible.js +23 -6
  17. package/dist/client/t.d.ts +2 -2
  18. package/dist/extraction/index.d.ts +3 -3
  19. package/dist/extraction/index.js +1 -1
  20. package/dist/{i18n-BGfrSNM8.d.ts → i18n-mTHTcD8K.d.ts} +9 -2
  21. package/dist/index.d.ts +26 -3
  22. package/dist/index.js +6 -2
  23. package/dist/internal.d.ts +4 -4
  24. package/dist/internal.js +1 -1
  25. package/dist/server/t.d.ts +1 -1
  26. package/dist/server/t.js +1 -1
  27. package/dist/{types-BaaSiz9n.d.ts → types-BeDFfFIL.d.ts} +1 -1
  28. package/dist/{types-Bzp6KxsI.d.ts → types-DNRFj1XA.d.ts} +1 -0
  29. package/dist/{types-DsNK_9sI.d.ts → types-DTUho6gi.d.ts} +7 -1
  30. package/package.json +8 -7
  31. package/src/components/I18nScript.astro +2 -23
  32. package/dist/babel-plugin-FUTQRKLT.js +0 -121
  33. package/dist/chunk-22ZZFFC2.js +0 -159
  34. package/dist/chunk-2PSXD3JJ.js +0 -144
  35. package/dist/chunk-3VI62RLT.js +0 -176
  36. package/dist/chunk-4DAQPWXI.js +0 -36
  37. package/dist/chunk-4OGX6AF5.js +0 -10
  38. package/dist/chunk-4PHYKXO3.js +0 -227
  39. package/dist/chunk-4UBFISFY.js +0 -376
  40. package/dist/chunk-5DERF6DV.js +0 -266
  41. package/dist/chunk-5KGNHFTC.js +0 -344
  42. package/dist/chunk-5PP4D2VK.js +0 -23
  43. package/dist/chunk-6BW3PN23.js +0 -14
  44. package/dist/chunk-6ESQ4JQI.js +0 -263
  45. package/dist/chunk-A7MPXMOP.js +0 -202
  46. package/dist/chunk-AWZ2SQDW.js +0 -149
  47. package/dist/chunk-AYY2YIBQ.js +0 -52
  48. package/dist/chunk-BMNEJFTD.js +0 -56
  49. package/dist/chunk-BSXQGEUK.js +0 -16
  50. package/dist/chunk-BSYTJFGF.js +0 -21
  51. package/dist/chunk-BVPXEGTT.js +0 -12
  52. package/dist/chunk-C4DJYWF6.js +0 -193
  53. package/dist/chunk-C5EJYE2P.js +0 -245
  54. package/dist/chunk-CLNOQHMS.js +0 -254
  55. package/dist/chunk-E5MCIRSU.js +0 -144
  56. package/dist/chunk-E7ZJS7WX.js +0 -162
  57. package/dist/chunk-ESFGCYYH.js +0 -24
  58. package/dist/chunk-FHBVI3TL.js +0 -372
  59. package/dist/chunk-FI5KZ5IO.js +0 -17
  60. package/dist/chunk-H5R6YMOL.js +0 -233
  61. package/dist/chunk-H62K3FGA.js +0 -236
  62. package/dist/chunk-HDG2R6XT.js +0 -211
  63. package/dist/chunk-HGXU5QAK.js +0 -344
  64. package/dist/chunk-HHRMR3TM.js +0 -17
  65. package/dist/chunk-HS4HVBAD.js +0 -127
  66. package/dist/chunk-IPJWKKHJ.js +0 -324
  67. package/dist/chunk-ITTL4V7J.js +0 -24
  68. package/dist/chunk-IWUBCMOK.js +0 -128
  69. package/dist/chunk-JJBBY2GR.js +0 -14
  70. package/dist/chunk-JTUKXNIJ.js +0 -23
  71. package/dist/chunk-K7YULG4A.js +0 -14
  72. package/dist/chunk-KNONI6DP.js +0 -89
  73. package/dist/chunk-LMTEO22Y.js +0 -17
  74. package/dist/chunk-LRLZEX4D.js +0 -147
  75. package/dist/chunk-MFKPLUAN.js +0 -241
  76. package/dist/chunk-MIWXQOO3.js +0 -316
  77. package/dist/chunk-MJRTAWPR.js +0 -261
  78. package/dist/chunk-MZEPQCXX.js +0 -249
  79. package/dist/chunk-NHHTSFA7.js +0 -237
  80. package/dist/chunk-NTTXM7AG.js +0 -23
  81. package/dist/chunk-NZFAVJXR.js +0 -192
  82. package/dist/chunk-OFNFSDW3.js +0 -13
  83. package/dist/chunk-PG3XDNGF.js +0 -227
  84. package/dist/chunk-PVEVTPNK.js +0 -139
  85. package/dist/chunk-QAO4BXS4.js +0 -244
  86. package/dist/chunk-QCTULQQK.js +0 -34
  87. package/dist/chunk-QKND2KCA.js +0 -163
  88. package/dist/chunk-QZEGY6RJ.js +0 -151
  89. package/dist/chunk-RAE2L6FT.js +0 -193
  90. package/dist/chunk-S3ABJBU3.js +0 -255
  91. package/dist/chunk-SVDVCPCV.js +0 -236
  92. package/dist/chunk-T35ZUWTL.js +0 -153
  93. package/dist/chunk-T6WV5DW6.js +0 -156
  94. package/dist/chunk-TGUXVDA3.js +0 -82
  95. package/dist/chunk-TYMO7K3A.js +0 -173
  96. package/dist/chunk-V4RBFLQL.js +0 -23
  97. package/dist/chunk-V4XWJXXZ.js +0 -23
  98. package/dist/chunk-VQCY3OKK.js +0 -258
  99. package/dist/chunk-WLGOJPDJ.js +0 -15
  100. package/dist/chunk-WUEPDJIL.js +0 -259
  101. package/dist/chunk-X3L7WZLY.js +0 -254
  102. package/dist/chunk-XDD62ZM3.js +0 -255
  103. package/dist/chunk-XFXXEDRD.js +0 -36
  104. package/dist/chunk-XKYOR5UK.js +0 -8
  105. package/dist/chunk-Y26535SI.js +0 -239
  106. package/dist/chunk-Y3PQX5TX.js +0 -152
  107. package/dist/chunk-Y4BE2V6F.js +0 -347
  108. package/dist/chunk-Z4IZYDIA.js +0 -24
  109. package/dist/chunk-Z52G4CYY.js +0 -14
  110. package/dist/chunk-ZGLUYWVM.js +0 -128
  111. package/dist/client/import-with-i18n.d.ts +0 -16
  112. package/dist/client/import-with-i18n.js +0 -8
  113. package/dist/client-directives/idle.d.ts +0 -9
  114. package/dist/client-directives/idle.js +0 -15
  115. package/dist/client-directives/load.d.ts +0 -9
  116. package/dist/client-directives/load.js +0 -15
  117. package/dist/client-directives/media.d.ts +0 -9
  118. package/dist/client-directives/media.js +0 -15
  119. package/dist/client-directives/only.d.ts +0 -9
  120. package/dist/client-directives/only.js +0 -15
  121. package/dist/client-directives/utils.d.ts +0 -9
  122. package/dist/client-directives/utils.js +0 -7
  123. package/dist/client-directives/visible.d.ts +0 -9
  124. package/dist/client-directives/visible.js +0 -15
  125. package/dist/i18n-3cZHoHwP.d.ts +0 -86
  126. package/dist/i18n-BHU81LMA.d.ts +0 -55
  127. package/dist/i18n-BagoeH4_.d.ts +0 -62
  128. package/dist/i18n-BeL84090.d.ts +0 -77
  129. package/dist/i18n-BeRWDfHi.d.ts +0 -55
  130. package/dist/i18n-Bfs2PVcf.d.ts +0 -79
  131. package/dist/i18n-BrL7p3aN.d.ts +0 -99
  132. package/dist/i18n-CNQNCFU0.d.ts +0 -55
  133. package/dist/i18n-CX_uU8V2.d.ts +0 -80
  134. package/dist/i18n-Cr1QZCwe.d.ts +0 -64
  135. package/dist/i18n-CzTRxGeX.d.ts +0 -80
  136. package/dist/i18n-DUOFiC7g.d.ts +0 -77
  137. package/dist/i18n-DcF47BHS.d.ts +0 -77
  138. package/dist/i18n-DifWvtaO.d.ts +0 -60
  139. package/dist/i18n-DumSqX1t.d.ts +0 -55
  140. package/dist/i18n-fWTJw12h.d.ts +0 -80
  141. package/dist/import-with-i18n.d.ts +0 -23
  142. package/dist/import-with-i18n.js +0 -7
  143. package/dist/init.d.ts +0 -2
  144. package/dist/init.js +0 -8
  145. package/dist/middleware-entrypoint-C0Drh9E_.d.ts +0 -29
  146. package/dist/middleware-entrypoint-mEl66IUy.d.ts +0 -29
  147. package/dist/middleware-entrypoint.d.ts +0 -5
  148. package/dist/middleware-entrypoint.js +0 -30
  149. package/dist/server/middleware-entrypoint.d.ts +0 -2
  150. package/dist/server/middleware-entrypoint.js +0 -28
  151. package/dist/t-BY_C9lZ1.d.ts +0 -30
  152. package/dist/t-CG3yDiEa.d.ts +0 -30
  153. package/dist/t-CHsBfrPr.d.ts +0 -30
  154. package/dist/t-DQpXaDhP.d.ts +0 -30
  155. package/dist/t.client.d.ts +0 -11
  156. package/dist/t.client.js +0 -35
  157. package/dist/t.server.d.ts +0 -22
  158. package/dist/t.server.js +0 -12
  159. package/dist/types-8uKCwFda.d.ts +0 -24
  160. package/dist/types-BC6K-pID.d.ts +0 -110
  161. package/dist/types-BUV7BZML.d.ts +0 -55
  162. package/dist/types-BWGB322F.d.ts +0 -46
  163. package/dist/types-B_-RxW4A.d.ts +0 -7
  164. package/dist/types-Bgl2V5xo.d.ts +0 -102
  165. package/dist/types-BwhB6IT4.d.ts +0 -111
  166. package/dist/types-C6rOJFY2.d.ts +0 -24
  167. package/dist/types-C7lD-Woj.d.ts +0 -50
  168. package/dist/types-CLWZeQhD.d.ts +0 -54
  169. package/dist/types-COXt613w.d.ts +0 -19
  170. package/dist/types-CUlkbl9-.d.ts +0 -50
  171. package/dist/types-CVv7GGPj.d.ts +0 -46
  172. package/dist/types-CiIDN2sW.d.ts +0 -64
  173. package/dist/types-Cr86qjYl.d.ts +0 -58
  174. package/dist/types-CxnObiEO.d.ts +0 -7
  175. package/dist/types-D44mFUjA.d.ts +0 -97
  176. package/dist/types-D85m9UTC.d.ts +0 -137
  177. package/dist/types-DWhsmSPP.d.ts +0 -102
  178. package/dist/types-DdprXGJ1.d.ts +0 -46
  179. package/dist/types-DhVJ2ZIo.d.ts +0 -43
  180. package/dist/types-DnbOuM1L.d.ts +0 -7
  181. package/dist/types-DvWBr9Xp.d.ts +0 -17
  182. package/dist/types-DwkIalh6.d.ts +0 -24
  183. package/dist/types-OYRgjtE_.d.ts +0 -110
  184. package/dist/types-SV8S05GX.d.ts +0 -7
  185. package/dist/types-TYn9WnxY.d.ts +0 -46
  186. package/dist/types-aXCBbdhq.d.ts +0 -55
  187. package/dist/types-gn4aNKGx.d.ts +0 -54
  188. package/dist/types-nippsMkm.d.ts +0 -54
@@ -1,139 +0,0 @@
1
- import {
2
- compileTranslations
3
- } from "./chunk-KNDYKTHE.js";
4
-
5
- // src/extraction/hash.ts
6
- import { createHash } from "crypto";
7
- function hashString(str) {
8
- return createHash("sha256").update(str).digest("hex").substring(0, 8);
9
- }
10
- function computeChunkHash(translations, keys) {
11
- const relevantTranslations = {};
12
- for (const key of keys.sort()) {
13
- if (translations[key]) {
14
- relevantTranslations[key] = translations[key];
15
- }
16
- }
17
- return hashString(JSON.stringify(relevantTranslations));
18
- }
19
- function computeAllChunkHashes(translations, manifest) {
20
- const hashes = {};
21
- for (const [chunkId, keys] of Object.entries(manifest)) {
22
- hashes[chunkId] = computeChunkHash(translations, keys);
23
- }
24
- return hashes;
25
- }
26
-
27
- // src/server/i18n.ts
28
- var I18nSingleton = class {
29
- // normalized user config with defaults applied
30
- config = null;
31
- // locale -> raw translation strings (before ICU compilation)
32
- rawCache = /* @__PURE__ */ new Map();
33
- // locale -> compiled ICU MessageFormat functions
34
- compiledCache = /* @__PURE__ */ new Map();
35
- // locale -> chunk name -> content hash (for cache busting)
36
- hashCache = /* @__PURE__ */ new Map();
37
- // extraction manifest (loaded from virtual module at init time)
38
- manifest = { keys: [], chunks: {} };
39
- configure(config) {
40
- if (!config.locales?.length) {
41
- throw new Error("i18n.configure(): locales array is required and must not be empty");
42
- }
43
- for (const locale of config.locales) {
44
- if (!locale) {
45
- throw new Error("i18n.configure(): locale cannot be empty");
46
- }
47
- if (locale !== locale.trim()) {
48
- throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
49
- }
50
- }
51
- if (new Set(config.locales).size !== config.locales.length) {
52
- throw new Error("i18n.configure(): locales array contains duplicates");
53
- }
54
- if (config.defaultLocale !== void 0) {
55
- if (!config.defaultLocale) {
56
- throw new Error("i18n.configure(): defaultLocale cannot be empty");
57
- }
58
- if (config.defaultLocale !== config.defaultLocale.trim()) {
59
- throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
60
- }
61
- if (!config.locales.includes(config.defaultLocale)) {
62
- throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
63
- }
64
- }
65
- this.config = {
66
- locales: config.locales,
67
- defaultLocale: config.defaultLocale ?? config.locales[0],
68
- fallback: config.fallback ?? "fallback"
69
- };
70
- }
71
- isConfigured() {
72
- return this.config !== null;
73
- }
74
- getConfig() {
75
- if (!this.config) {
76
- throw new Error("i18n not configured. Call i18n.configure() first.");
77
- }
78
- return this.config;
79
- }
80
- setTranslations(locale, raw) {
81
- this.rawCache.set(locale, raw);
82
- this.compiledCache.set(locale, compileTranslations(locale, raw));
83
- this.hashCache.delete(locale);
84
- const { chunks } = this.manifest;
85
- if (Object.keys(chunks).length > 0) {
86
- this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
87
- }
88
- }
89
- getTranslations(locale) {
90
- return this.rawCache.get(locale) ?? {};
91
- }
92
- getCompiledTranslations(locale) {
93
- return this.compiledCache.get(locale) ?? {};
94
- }
95
- /**
96
- * Get the extraction manifest (keys + chunks).
97
- * - keys: translation keys extracted from codebase with metadata
98
- * - chunks: mapping of chunk names to their translation keys
99
- */
100
- getManifest() {
101
- return this.manifest;
102
- }
103
- /**
104
- * Set the extraction manifest (called by init module).
105
- * @internal
106
- */
107
- setManifest(manifest) {
108
- this.manifest = manifest;
109
- }
110
- getHashes(locale) {
111
- return this.hashCache.get(locale) ?? {};
112
- }
113
- /**
114
- * Clear translations for specified locale(s).
115
- * @param locales - Locale(s) to clear, or all if not specified
116
- */
117
- clear(locales) {
118
- const targets = this.resolveLocales(locales);
119
- for (const locale of targets) {
120
- this.rawCache.delete(locale);
121
- this.compiledCache.delete(locale);
122
- this.hashCache.delete(locale);
123
- }
124
- }
125
- resolveLocales(locales) {
126
- if (!locales) {
127
- return this.config?.locales ?? [];
128
- }
129
- if (typeof locales === "string") {
130
- return [locales];
131
- }
132
- return locales;
133
- }
134
- };
135
- var i18n = new I18nSingleton();
136
-
137
- export {
138
- i18n
139
- };
@@ -1,244 +0,0 @@
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
- let manifestPath = join(__dirname, '${MANIFEST_FILE_NAME}');
140
- if (!existsSync(manifestPath)) {
141
- manifestPath = join(__dirname, '..', '${MANIFEST_FILE_NAME}');
142
- }
143
- const manifestJson = JSON.parse(readFileSync(manifestPath, 'utf-8'));
144
-
145
- export const manifest = manifestJson;
146
- export function getManifest() { return manifestJson; }
147
- `;
148
- }
149
- const keysWithRelativePaths = extractedKeys.map((key) => ({
150
- ...key,
151
- file: projectRoot ? path.relative(projectRoot, key.file) : key.file
152
- }));
153
- const manifest = {
154
- keys: keysWithRelativePaths,
155
- chunks: chunkManifest
156
- };
157
- return `
158
- export const manifest = ${JSON.stringify(manifest)};
159
- export function getManifest() { return ${JSON.stringify(manifest)}; }
160
- `;
161
- }
162
- },
163
- transform(code, id) {
164
- if (!include.some((ext) => id.endsWith(ext))) {
165
- return null;
166
- }
167
- if (id.includes("node_modules")) {
168
- return null;
169
- }
170
- if (hasI18nImport(code)) {
171
- filesWithI18n.add(id);
172
- }
173
- const keys = extractTranslationCalls(code, id);
174
- if (keys.length > 0) {
175
- extractedKeys.push(...keys);
176
- globalExtractedKeys = extractedKeys;
177
- fileToKeys.set(
178
- id,
179
- keys.map((k) => k.key)
180
- );
181
- }
182
- return null;
183
- },
184
- generateBundle(_, bundle) {
185
- chunkManifest = {};
186
- for (const [fileName, chunk] of Object.entries(bundle)) {
187
- if (chunk.type === "chunk" && chunk.moduleIds) {
188
- const keys = /* @__PURE__ */ new Set();
189
- let hasI18nModule = false;
190
- for (const moduleId of chunk.moduleIds) {
191
- const moduleKeys = fileToKeys.get(moduleId);
192
- if (moduleKeys) {
193
- moduleKeys.forEach((k) => keys.add(k));
194
- }
195
- if (filesWithI18n.has(moduleId)) {
196
- hasI18nModule = true;
197
- }
198
- }
199
- if (keys.size > 0) {
200
- const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
201
- chunkManifest[chunkName] = Array.from(keys);
202
- }
203
- if (hasI18nModule && chunk.type === "chunk") {
204
- const chunkName = chunkIdToName(fileName.replace(/\.js$/, ""));
205
- const loaderCode = `
206
- /* @astroscope/i18n loader */
207
- if (typeof window !== 'undefined' && window.__i18n__) {
208
- const __i18n__ = window.__i18n__;
209
- const __i18n_hash__ = __i18n__.hashes[${JSON.stringify(chunkName)}];
210
- if (__i18n_hash__) {
211
- await import(\`/_i18n/\${__i18n__.locale}/${chunkName}.\${__i18n_hash__}.js\`);
212
- }
213
- }
214
- /* end @astroscope/i18n loader */
215
- `;
216
- chunk.code = loaderCode + chunk.code;
217
- }
218
- }
219
- }
220
- globalChunkManifest = chunkManifest;
221
- const keysWithRelativePaths = extractedKeys.map((key) => ({
222
- ...key,
223
- file: projectRoot ? path.relative(projectRoot, key.file) : key.file
224
- }));
225
- const manifest = {
226
- keys: keysWithRelativePaths,
227
- chunks: chunkManifest
228
- };
229
- this.emitFile({
230
- type: "asset",
231
- fileName: MANIFEST_FILE_NAME,
232
- source: JSON.stringify(manifest)
233
- });
234
- const chunkCount = Object.keys(chunkManifest).length;
235
- const keyCount = extractedKeys.length;
236
- logger?.info(`manifest: ${chunkCount} chunks, ${keyCount} keys`);
237
- }
238
- };
239
- }
240
-
241
- export {
242
- getManifest,
243
- i18nVitePlugin
244
- };
@@ -1,34 +0,0 @@
1
- import {
2
- buildI18nChunkUrl,
3
- chunkIdToName
4
- } from "./chunk-HHRMR3TM.js";
5
-
6
- // src/client/import-with-i18n.ts
7
- import { lazy } from "react";
8
- function loadTranslations(chunkId) {
9
- if (!chunkId) return Promise.resolve();
10
- const i18n = window.__i18n__;
11
- const chunkName = chunkIdToName(chunkId);
12
- const hash = i18n.hashes[chunkName];
13
- if (!hash) return Promise.resolve();
14
- return import(
15
- /* @vite-ignore */
16
- buildI18nChunkUrl(i18n.locale, chunkName, hash)
17
- ).then(() => {
18
- });
19
- }
20
- async function importx(importFn, chunkId) {
21
- const [module] = await Promise.all([importFn(), loadTranslations(chunkId)]);
22
- return module;
23
- }
24
- function lazyx(importFn, chunkId) {
25
- return lazy(async () => {
26
- const [module] = await Promise.all([importFn(), loadTranslations(chunkId)]);
27
- return module;
28
- });
29
- }
30
-
31
- export {
32
- importx,
33
- lazyx
34
- };
@@ -1,163 +0,0 @@
1
- import {
2
- compileMessage,
3
- compileTranslations
4
- } from "./chunk-KNDYKTHE.js";
5
-
6
- // src/extraction/hash.ts
7
- import { createHash } from "crypto";
8
- function hashString(str) {
9
- return createHash("sha256").update(str).digest("hex").substring(0, 8);
10
- }
11
- function computeChunkHash(translations, keys) {
12
- const relevantTranslations = {};
13
- for (const key of keys.sort()) {
14
- if (translations[key]) {
15
- relevantTranslations[key] = translations[key];
16
- }
17
- }
18
- return hashString(JSON.stringify(relevantTranslations));
19
- }
20
- function computeAllChunkHashes(translations, manifest) {
21
- const hashes = {};
22
- for (const [chunkId, keys] of Object.entries(manifest)) {
23
- hashes[chunkId] = computeChunkHash(translations, keys);
24
- }
25
- return hashes;
26
- }
27
-
28
- // src/server/i18n.ts
29
- var I18nSingleton = class {
30
- // normalized user config with defaults applied
31
- config = null;
32
- // locale -> raw translation strings (before ICU compilation)
33
- rawCache = /* @__PURE__ */ new Map();
34
- // locale -> compiled ICU MessageFormat functions
35
- compiledCache = /* @__PURE__ */ new Map();
36
- // locale -> chunk name -> content hash (for cache busting)
37
- hashCache = /* @__PURE__ */ new Map();
38
- // manifest getter (set by init module, provides live data in dev mode)
39
- manifestGetter = null;
40
- async configure(config) {
41
- if (!config.locales?.length) {
42
- throw new Error("i18n.configure(): locales array is required and must not be empty");
43
- }
44
- for (const locale of config.locales) {
45
- if (!locale) {
46
- throw new Error("i18n.configure(): locale cannot be empty");
47
- }
48
- if (locale !== locale.trim()) {
49
- throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
50
- }
51
- }
52
- if (new Set(config.locales).size !== config.locales.length) {
53
- throw new Error("i18n.configure(): locales array contains duplicates");
54
- }
55
- if (config.defaultLocale !== void 0) {
56
- if (!config.defaultLocale) {
57
- throw new Error("i18n.configure(): defaultLocale cannot be empty");
58
- }
59
- if (config.defaultLocale !== config.defaultLocale.trim()) {
60
- throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
61
- }
62
- if (!config.locales.includes(config.defaultLocale)) {
63
- throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
64
- }
65
- }
66
- this.config = {
67
- locales: config.locales,
68
- defaultLocale: config.defaultLocale ?? config.locales[0],
69
- fallback: config.fallback ?? "fallback"
70
- };
71
- if (!this.manifestGetter) {
72
- const m = await import("virtual:@astroscope/i18n/manifest");
73
- this.manifestGetter = m.getManifest;
74
- }
75
- }
76
- isConfigured() {
77
- return this.config !== null;
78
- }
79
- getConfig() {
80
- if (!this.config) {
81
- throw new Error("i18n not configured. Call i18n.configure() first.");
82
- }
83
- return this.config;
84
- }
85
- setTranslations(locale, raw) {
86
- this.rawCache.set(locale, raw);
87
- this.compiledCache.set(locale, compileTranslations(locale, raw));
88
- this.hashCache.delete(locale);
89
- const { chunks } = this.getManifest();
90
- if (Object.keys(chunks).length > 0) {
91
- this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
92
- }
93
- }
94
- getTranslations(locale) {
95
- return this.rawCache.get(locale) ?? {};
96
- }
97
- getCompiledTranslations(locale) {
98
- const compiled = this.compiledCache.get(locale) ?? {};
99
- const manifest = this.manifestGetter?.() ?? { keys: [], chunks: {} };
100
- if (manifest.keys.length === 0) {
101
- return compiled;
102
- }
103
- const withFallbacks = { ...compiled };
104
- for (const { key, meta } of manifest.keys) {
105
- if (!(key in withFallbacks) && meta.fallback) {
106
- withFallbacks[key] = compileMessage(locale, meta.fallback);
107
- }
108
- }
109
- return withFallbacks;
110
- }
111
- /**
112
- * Get the extraction manifest (keys + chunks).
113
- * - keys: translation keys extracted from codebase with metadata
114
- * - chunks: mapping of chunk names to their translation keys
115
- */
116
- getManifest() {
117
- if (!this.manifestGetter) {
118
- throw new Error("i18n manifest not initialized. Call i18n.configure() first.");
119
- }
120
- return this.manifestGetter();
121
- }
122
- getHashes(locale) {
123
- return this.hashCache.get(locale) ?? {};
124
- }
125
- /**
126
- * Clear translations for specified locale(s).
127
- * @param locales - Locale(s) to clear, or all if not specified
128
- */
129
- clear(locales) {
130
- const targets = this.resolveLocales(locales);
131
- for (const locale of targets) {
132
- this.rawCache.delete(locale);
133
- this.compiledCache.delete(locale);
134
- this.hashCache.delete(locale);
135
- }
136
- }
137
- resolveLocales(locales) {
138
- if (!locales) {
139
- return this.config?.locales ?? [];
140
- }
141
- if (typeof locales === "string") {
142
- return [locales];
143
- }
144
- return locales;
145
- }
146
- };
147
- var i18n = new I18nSingleton();
148
-
149
- // src/server/context.ts
150
- import { AsyncLocalStorage } from "async_hooks";
151
- var als = new AsyncLocalStorage();
152
- function runWithContext(context, fn) {
153
- return als.run(context, fn);
154
- }
155
- function getContext() {
156
- return als.getStore() ?? null;
157
- }
158
-
159
- export {
160
- i18n,
161
- runWithContext,
162
- getContext
163
- };