@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
package/README.md CHANGED
@@ -28,7 +28,7 @@ i18n for Astro + React islands — automatic tree-shaking, parallel loading, any
28
28
  ## Installation
29
29
 
30
30
  ```bash
31
- npm install @astroscope/i18n @astroscope/boot
31
+ npm install @astroscope/i18n @astroscope/boot @astroscope/excludes
32
32
  ```
33
33
 
34
34
  ## Usage
@@ -51,6 +51,8 @@ export default defineConfig({
51
51
 
52
52
  ### 2. Configure i18n in your boot file
53
53
 
54
+ **VERY IMPORTANT:** `i18n.configure` must be awaited during boot before handling any requests!
55
+
54
56
  ```ts
55
57
  // src/boot.ts
56
58
  import { i18n, type RawTranslations } from '@astroscope/i18n';
@@ -95,6 +97,24 @@ export const onRequest = sequence(
95
97
  );
96
98
  ```
97
99
 
100
+ By default, `RECOMMENDED_EXCLUDES` (static assets like `/_astro/`) are excluded from locale context setup. To customize:
101
+
102
+ ```ts
103
+ import { sequence } from 'astro:middleware';
104
+ import { createI18nChunkMiddleware, createI18nMiddleware, i18n } from '@astroscope/i18n';
105
+ import { RECOMMENDED_EXCLUDES } from '@astroscope/excludes';
106
+
107
+ export const onRequest = sequence(
108
+ createI18nChunkMiddleware(),
109
+ createI18nMiddleware({
110
+ locale: (ctx) =>
111
+ ctx.cookies.get('locale')?.value ??
112
+ i18n.getConfig().defaultLocale,
113
+ exclude: [...RECOMMENDED_EXCLUDES, { exact: '/health' }],
114
+ }),
115
+ );
116
+ ```
117
+
98
118
  ### 4. Add `<I18nScript />` to your layout
99
119
 
100
120
  Inject translations into the page for hydrated components:
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { AstroIntegration, MiddlewareHandler, APIContext } from 'astro';
2
2
  export { F as FallbackBehavior, I as I18nConfig, i as i18n } from './i18n-BGfrSNM8.js';
3
+ import { ExcludePattern } from '@astroscope/excludes';
3
4
  export { R as RawTranslations, T as TranslationMeta } from './types-Bzp6KxsI.js';
4
5
  export { E as ExtractedKey, a as ExtractionManifest } from './types-8uKCwFda.js';
5
6
 
@@ -17,6 +18,28 @@ declare function i18nIntegration(_options?: I18nOptions): AstroIntegration;
17
18
 
18
19
  type I18nMiddlewareOptions = {
19
20
  locale: (ctx: APIContext) => string;
21
+ /**
22
+ * Patterns to exclude from locale context setup.
23
+ * Defaults to RECOMMENDED_EXCLUDES if not provided.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { RECOMMENDED_EXCLUDES } from '@astroscope/excludes';
28
+ *
29
+ * // extend defaults
30
+ * createI18nMiddleware({
31
+ * locale: (ctx) => ctx.locals.session?.locale ?? 'en',
32
+ * exclude: [...RECOMMENDED_EXCLUDES, { exact: '/health' }],
33
+ * })
34
+ *
35
+ * // disable excludes entirely
36
+ * createI18nMiddleware({
37
+ * locale: (ctx) => ctx.locals.session?.locale ?? 'en',
38
+ * exclude: [],
39
+ * })
40
+ * ```
41
+ */
42
+ exclude?: ExcludePattern[] | ((context: APIContext) => boolean) | undefined;
20
43
  };
21
44
  /**
22
45
  * Create the i18n chunk middleware that serves translation chunks at `/_i18n/` endpoints.
package/dist/index.js CHANGED
@@ -55,6 +55,7 @@ ${attributes}
55
55
  }
56
56
 
57
57
  // src/server/middleware.ts
58
+ import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
58
59
  var I18N_ENDPOINT_PREFIX = "/_i18n/";
59
60
  function createI18nChunkMiddleware() {
60
61
  return (ctx, next) => {
@@ -111,6 +112,9 @@ function createI18nChunkMiddleware() {
111
112
  }
112
113
  function createI18nMiddleware(options) {
113
114
  return (ctx, next) => {
115
+ if (shouldExclude(ctx, options.exclude ?? RECOMMENDED_EXCLUDES)) {
116
+ return next();
117
+ }
114
118
  const locale = options.locale(ctx);
115
119
  const context = {
116
120
  locale,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astroscope/i18n",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "i18n for Astro + React islands — automatic tree-shaking, parallel loading, any translation source",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -86,20 +86,21 @@
86
86
  "lint:fix": "eslint 'src/**/*.ts' --fix"
87
87
  },
88
88
  "dependencies": {
89
- "@babel/core": "^7.26.0",
90
- "@babel/preset-typescript": "^7.26.0",
89
+ "@babel/core": "^7.28.6",
90
+ "@babel/preset-typescript": "^7.28.5",
91
91
  "@messageformat/core": "^3.4.0",
92
- "magic-string": "^0.30.17"
92
+ "magic-string": "^0.30.21"
93
93
  },
94
94
  "devDependencies": {
95
- "@babel/types": "^7.26.0",
95
+ "@babel/types": "^7.28.6",
96
96
  "@types/babel__core": "^7.20.5",
97
- "astro": "^5.1.0",
97
+ "astro": "^5.16.9",
98
98
  "tsup": "^8.5.1",
99
99
  "typescript": "^5.9.3",
100
- "vite": "^6.0.0"
100
+ "vite": "^6.4.1"
101
101
  },
102
102
  "peerDependencies": {
103
+ "@astroscope/excludes": "^0.1.2",
103
104
  "astro": "^5.0.0"
104
105
  }
105
106
  }
@@ -1,121 +0,0 @@
1
- // src/extraction/babel-plugin.ts
2
- function getStringValue(t, node) {
3
- if (t.isStringLiteral(node)) {
4
- return node.value;
5
- }
6
- if (t.isTemplateLiteral(node) && node.quasis.length === 1 && node.expressions.length === 0) {
7
- return node.quasis[0]?.value.cooked ?? null;
8
- }
9
- return null;
10
- }
11
- function extractMeta(t, node) {
12
- const stringValue = getStringValue(t, node);
13
- if (stringValue !== null) {
14
- return { fallback: stringValue };
15
- }
16
- if (t.isObjectExpression(node)) {
17
- return extractMetaFromObject(t, node);
18
- }
19
- return { fallback: "" };
20
- }
21
- function extractMetaFromObject(t, obj) {
22
- const meta = { fallback: "" };
23
- for (const prop of obj.properties) {
24
- if (!t.isObjectProperty(prop)) continue;
25
- if (!t.isIdentifier(prop.key)) continue;
26
- const name = prop.key.name;
27
- if (name === "fallback") {
28
- const value = getStringValue(t, prop.value);
29
- if (value !== null) {
30
- meta.fallback = value;
31
- }
32
- } else if (name === "description") {
33
- const value = getStringValue(t, prop.value);
34
- if (value !== null) {
35
- meta.description = value;
36
- }
37
- } else if (name === "variables" && t.isObjectExpression(prop.value)) {
38
- meta.variables = extractVariables(t, prop.value);
39
- }
40
- }
41
- return meta;
42
- }
43
- function extractVariables(t, obj) {
44
- const result = {};
45
- for (const prop of obj.properties) {
46
- if (!t.isObjectProperty(prop)) continue;
47
- if (!t.isIdentifier(prop.key)) continue;
48
- const varName = prop.key.name;
49
- if (t.isObjectExpression(prop.value)) {
50
- const varDef = {};
51
- for (const varProp of prop.value.properties) {
52
- if (!t.isObjectProperty(varProp)) continue;
53
- if (!t.isIdentifier(varProp.key)) continue;
54
- const varPropName = varProp.key.name;
55
- const value = getStringValue(t, varProp.value);
56
- if (value !== null) {
57
- if (varPropName === "fallback") {
58
- varDef.fallback = value;
59
- } else if (varPropName === "description") {
60
- varDef.description = value;
61
- }
62
- }
63
- }
64
- result[varName] = varDef;
65
- }
66
- }
67
- return Object.keys(result).length > 0 ? result : void 0;
68
- }
69
- function isMetaArgument(t, node) {
70
- if (t.isStringLiteral(node) || t.isTemplateLiteral(node)) {
71
- return true;
72
- }
73
- if (t.isObjectExpression(node)) {
74
- for (const prop of node.properties) {
75
- if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "fallback") {
76
- return true;
77
- }
78
- }
79
- }
80
- return false;
81
- }
82
- function i18nExtractPlugin({ types: t }) {
83
- return {
84
- name: "@astroscope/i18n/extract",
85
- visitor: {
86
- CallExpression(path, state) {
87
- if (!path.get("callee").isIdentifier({ name: "t" })) return;
88
- const args = path.node.arguments;
89
- if (args.length < 1) return;
90
- const keyArg = args[0];
91
- if (!t.isExpression(keyArg)) return;
92
- const key = getStringValue(t, keyArg);
93
- if (key === null) return;
94
- let meta = { fallback: "" };
95
- if (args.length >= 2 && t.isExpression(args[1]) && isMetaArgument(t, args[1])) {
96
- meta = extractMeta(t, args[1]);
97
- }
98
- state.opts.onKeyExtracted({
99
- key,
100
- meta,
101
- file: state.filename ?? "",
102
- line: path.node.loc?.start.line ?? 0
103
- });
104
- if (state.opts.stripFallbacks && args.length >= 2) {
105
- const secondArg = args[1];
106
- if (!t.isExpression(secondArg)) return;
107
- if (isMetaArgument(t, secondArg)) {
108
- if (args.length === 2) {
109
- path.node.arguments = [args[0]];
110
- } else if (args.length >= 3) {
111
- path.node.arguments = [args[0], args[2]];
112
- }
113
- }
114
- }
115
- }
116
- }
117
- };
118
- }
119
- export {
120
- i18nExtractPlugin as default
121
- };
@@ -1,159 +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
- if (!config.locales?.length) {
57
- throw new Error("i18n.configure(): locales array is required and must not be empty");
58
- }
59
- for (const locale of config.locales) {
60
- if (!locale) {
61
- throw new Error("i18n.configure(): locale cannot be empty");
62
- }
63
- if (locale !== locale.trim()) {
64
- throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
65
- }
66
- }
67
- if (new Set(config.locales).size !== config.locales.length) {
68
- throw new Error("i18n.configure(): locales array contains duplicates");
69
- }
70
- if (config.defaultLocale !== void 0) {
71
- if (!config.defaultLocale) {
72
- throw new Error("i18n.configure(): defaultLocale cannot be empty");
73
- }
74
- if (config.defaultLocale !== config.defaultLocale.trim()) {
75
- throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
76
- }
77
- if (!config.locales.includes(config.defaultLocale)) {
78
- throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
79
- }
80
- }
81
- this.config = config;
82
- }
83
- isConfigured() {
84
- return this.config !== null;
85
- }
86
- getConfig() {
87
- if (!this.config) {
88
- throw new Error("i18n not configured. Call i18n.configure() first.");
89
- }
90
- return this.config;
91
- }
92
- getDefaultLocale() {
93
- const config = this.getConfig();
94
- return config.defaultLocale ?? config.locales[0];
95
- }
96
- getFallbackBehavior() {
97
- return this.getConfig().fallback ?? "fallback";
98
- }
99
- setTranslations(locale, raw) {
100
- this.rawCache.set(locale, raw);
101
- this.compiledCache.set(locale, compileTranslations(locale, raw));
102
- this.hashCache.delete(locale);
103
- const chunks = this.getChunkManifest();
104
- if (chunks) {
105
- this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
106
- }
107
- }
108
- getTranslations(locale) {
109
- return this.rawCache.get(locale) ?? {};
110
- }
111
- getCompiledTranslations(locale) {
112
- return this.compiledCache.get(locale) ?? {};
113
- }
114
- getChunkManifest() {
115
- return this.getExtractionManifest()?.chunks ?? {};
116
- }
117
- /**
118
- * Get extracted translation keys with metadata.
119
- * Use this to populate your CMS with keys extracted from the codebase.
120
- */
121
- getExtractedKeys() {
122
- return this.getExtractionManifest()?.keys ?? null;
123
- }
124
- /**
125
- * Get the full extraction manifest (keys + chunks).
126
- * Returns null in dev mode (manifest doesn't exist).
127
- */
128
- getExtractionManifest() {
129
- if (this.extractionManifest === void 0) {
130
- this.extractionManifest = loadExtractionManifest() ?? null;
131
- }
132
- return this.extractionManifest;
133
- }
134
- getHashes(locale) {
135
- return this.hashCache.get(locale) ?? {};
136
- }
137
- /**
138
- * Clear translations for specified locale(s).
139
- * @param locales - Locale(s) to clear, or all if not specified
140
- */
141
- clear(locales) {
142
- const targets = this.resolveLocales(locales);
143
- for (const locale of targets) {
144
- this.rawCache.delete(locale);
145
- this.compiledCache.delete(locale);
146
- this.hashCache.delete(locale);
147
- }
148
- }
149
- resolveLocales(locales) {
150
- if (!locales) return this.config?.locales ?? [];
151
- if (typeof locales === "string") return [locales];
152
- return locales;
153
- }
154
- };
155
- var i18n = new I18nSingleton();
156
-
157
- export {
158
- i18n
159
- };
@@ -1,144 +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
- // manifest getter (set by init module, provides live data in dev mode)
38
- manifestGetter = null;
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.getManifest();
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
- if (!this.manifestGetter) {
102
- throw new Error("i18n manifest not initialized. Make sure to import '@astroscope/i18n/init' in your boot file.");
103
- }
104
- return this.manifestGetter();
105
- }
106
- /**
107
- * Set the manifest getter function (called by init module).
108
- * In dev mode, this returns live data from the vite plugin.
109
- * In production, this returns the static manifest loaded from JSON.
110
- * @internal
111
- */
112
- setManifestGetter(getter) {
113
- this.manifestGetter = getter;
114
- }
115
- getHashes(locale) {
116
- return this.hashCache.get(locale) ?? {};
117
- }
118
- /**
119
- * Clear translations for specified locale(s).
120
- * @param locales - Locale(s) to clear, or all if not specified
121
- */
122
- clear(locales) {
123
- const targets = this.resolveLocales(locales);
124
- for (const locale of targets) {
125
- this.rawCache.delete(locale);
126
- this.compiledCache.delete(locale);
127
- this.hashCache.delete(locale);
128
- }
129
- }
130
- resolveLocales(locales) {
131
- if (!locales) {
132
- return this.config?.locales ?? [];
133
- }
134
- if (typeof locales === "string") {
135
- return [locales];
136
- }
137
- return locales;
138
- }
139
- };
140
- var i18n = new I18nSingleton();
141
-
142
- export {
143
- i18n
144
- };