@babelize/engine 0.0.0-stage → 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.
Files changed (246) hide show
  1. package/LICENSE +202 -0
  2. package/dist/BabelizeEngine.d.ts +12 -0
  3. package/dist/BabelizeEngine.js +47 -0
  4. package/dist/analysis/analyze.d.ts +106 -0
  5. package/dist/analysis/analyze.js +323 -0
  6. package/dist/config/env.d.ts +9 -0
  7. package/dist/config/env.js +16 -0
  8. package/dist/config.d.ts +74 -0
  9. package/dist/config.js +47 -0
  10. package/dist/index.d.ts +54 -0
  11. package/dist/index.js +126 -0
  12. package/dist/injection/InjectionStrategy.d.ts +62 -0
  13. package/dist/injection/InjectionStrategy.js +2 -0
  14. package/dist/injection/StrategyFactory.d.ts +4 -0
  15. package/dist/injection/StrategyFactory.js +29 -0
  16. package/dist/injection/django/django-template-injector.d.ts +29 -0
  17. package/dist/injection/django/django-template-injector.js +216 -0
  18. package/dist/injection/html/html-injector.d.ts +11 -0
  19. package/dist/injection/html/html-injector.js +173 -0
  20. package/dist/injection/strategies/NextIntlStrategy.d.ts +25 -0
  21. package/dist/injection/strategies/NextIntlStrategy.js +141 -0
  22. package/dist/injection/strategies/ReactI18nextStrategy.d.ts +19 -0
  23. package/dist/injection/strategies/ReactI18nextStrategy.js +116 -0
  24. package/dist/injection/strategies/VanillaStrategy.d.ts +11 -0
  25. package/dist/injection/strategies/VanillaStrategy.js +73 -0
  26. package/dist/injection/strategies/VueI18nStrategy.d.ts +11 -0
  27. package/dist/injection/strategies/VueI18nStrategy.js +84 -0
  28. package/dist/local/formatter.d.ts +26 -0
  29. package/dist/local/formatter.js +105 -0
  30. package/dist/local/git.d.ts +23 -0
  31. package/dist/local/git.js +136 -0
  32. package/dist/local/hosted-provider.d.ts +68 -0
  33. package/dist/local/hosted-provider.js +215 -0
  34. package/dist/local/index.d.ts +23 -0
  35. package/dist/local/index.js +75 -0
  36. package/dist/local/provider.d.ts +56 -0
  37. package/dist/local/provider.js +119 -0
  38. package/dist/local/pseudo-provider.d.ts +18 -0
  39. package/dist/local/pseudo-provider.js +207 -0
  40. package/dist/local/report.d.ts +177 -0
  41. package/dist/local/report.js +186 -0
  42. package/dist/local/translate-core.d.ts +215 -0
  43. package/dist/local/translate-core.js +461 -0
  44. package/dist/local/translate-messages.d.ts +49 -0
  45. package/dist/local/translate-messages.js +91 -0
  46. package/dist/local/workspace-copy.d.ts +91 -0
  47. package/dist/local/workspace-copy.js +355 -0
  48. package/dist/modules/extractor/extract.d.ts +20 -0
  49. package/dist/modules/extractor/extract.js +701 -0
  50. package/dist/modules/extractor/index.d.ts +2 -0
  51. package/dist/modules/extractor/index.js +63 -0
  52. package/dist/modules/finalization/django-finalize.d.ts +13 -0
  53. package/dist/modules/finalization/django-finalize.js +275 -0
  54. package/dist/modules/finalization/finalize.d.ts +36 -0
  55. package/dist/modules/finalization/finalize.js +717 -0
  56. package/dist/modules/finalization/index.d.ts +1 -0
  57. package/dist/modules/finalization/index.js +5 -0
  58. package/dist/modules/finalization/nextjs-finalize.d.ts +37 -0
  59. package/dist/modules/finalization/nextjs-finalize.js +329 -0
  60. package/dist/modules/finalization/static-runtime.d.ts +9 -0
  61. package/dist/modules/finalization/static-runtime.js +261 -0
  62. package/dist/modules/format-injection/index.d.ts +19 -0
  63. package/dist/modules/format-injection/index.js +272 -0
  64. package/dist/modules/format-injection/inject-formats.d.ts +31 -0
  65. package/dist/modules/format-injection/inject-formats.js +208 -0
  66. package/dist/modules/glossary/glossary.d.ts +9 -0
  67. package/dist/modules/glossary/glossary.js +27 -0
  68. package/dist/modules/glossary/index.d.ts +3 -0
  69. package/dist/modules/glossary/index.js +8 -0
  70. package/dist/modules/glossary/masker.d.ts +23 -0
  71. package/dist/modules/glossary/masker.js +56 -0
  72. package/dist/modules/glossary/types.d.ts +22 -0
  73. package/dist/modules/glossary/types.js +2 -0
  74. package/dist/modules/injection/index.d.ts +1 -0
  75. package/dist/modules/injection/index.js +6 -0
  76. package/dist/modules/injection/inject.d.ts +29 -0
  77. package/dist/modules/injection/inject.js +1066 -0
  78. package/dist/modules/keygen/index.d.ts +1 -0
  79. package/dist/modules/keygen/index.js +11 -0
  80. package/dist/modules/keygen/keygen.d.ts +4 -0
  81. package/dist/modules/keygen/keygen.js +116 -0
  82. package/dist/modules/keygen/locale-data.d.ts +7 -0
  83. package/dist/modules/keygen/locale-data.js +73 -0
  84. package/dist/modules/keygen/public.d.ts +2 -0
  85. package/dist/modules/keygen/public.js +12 -0
  86. package/dist/modules/locale-builder/index.d.ts +1 -0
  87. package/dist/modules/locale-builder/index.js +7 -0
  88. package/dist/modules/locale-builder/locales.d.ts +13 -0
  89. package/dist/modules/locale-builder/locales.js +107 -0
  90. package/dist/modules/locale-builder/po.d.ts +33 -0
  91. package/dist/modules/locale-builder/po.js +131 -0
  92. package/dist/modules/lockfile/hasher.d.ts +7 -0
  93. package/dist/modules/lockfile/hasher.js +13 -0
  94. package/dist/modules/lockfile/index.d.ts +3 -0
  95. package/dist/modules/lockfile/index.js +21 -0
  96. package/dist/modules/lockfile/lockfile-manager.d.ts +90 -0
  97. package/dist/modules/lockfile/lockfile-manager.js +482 -0
  98. package/dist/modules/lockfile/schema.d.ts +154 -0
  99. package/dist/modules/lockfile/schema.js +45 -0
  100. package/dist/modules/parser/index.d.ts +2 -0
  101. package/dist/modules/parser/index.js +9 -0
  102. package/dist/modules/summary/index.d.ts +1 -0
  103. package/dist/modules/summary/index.js +7 -0
  104. package/dist/modules/summary/summary.d.ts +9 -0
  105. package/dist/modules/summary/summary.js +195 -0
  106. package/dist/modules/sync-detector/index.d.ts +2 -0
  107. package/dist/modules/sync-detector/index.js +7 -0
  108. package/dist/modules/sync-detector/reporter.d.ts +3 -0
  109. package/dist/modules/sync-detector/reporter.js +16 -0
  110. package/dist/modules/sync-detector/sync-detector.d.ts +39 -0
  111. package/dist/modules/sync-detector/sync-detector.js +374 -0
  112. package/dist/modules/translator/batch-optimizer.d.ts +12 -0
  113. package/dist/modules/translator/batch-optimizer.js +30 -0
  114. package/dist/modules/translator/client/byok.provider.d.ts +64 -0
  115. package/dist/modules/translator/client/byok.provider.js +319 -0
  116. package/dist/modules/translator/client/direct-translator.client.d.ts +12 -0
  117. package/dist/modules/translator/client/direct-translator.client.js +71 -0
  118. package/dist/modules/translator/client/gemini.provider.d.ts +1 -0
  119. package/dist/modules/translator/client/gemini.provider.js +110 -0
  120. package/dist/modules/translator/client/groq.provider.d.ts +1 -0
  121. package/dist/modules/translator/client/groq.provider.js +111 -0
  122. package/dist/modules/translator/client/request.types.d.ts +1 -0
  123. package/dist/modules/translator/client/request.types.js +2 -0
  124. package/dist/modules/translator/client/retry.d.ts +29 -0
  125. package/dist/modules/translator/client/retry.js +94 -0
  126. package/dist/modules/translator/client/test-utils/mock.client.d.ts +31 -0
  127. package/dist/modules/translator/client/test-utils/mock.client.js +59 -0
  128. package/dist/modules/translator/client/translator.interface.d.ts +1 -0
  129. package/dist/modules/translator/client/translator.interface.js +2 -0
  130. package/dist/modules/translator/language-codes/errors.d.ts +10 -0
  131. package/dist/modules/translator/language-codes/errors.js +18 -0
  132. package/dist/modules/translator/language-codes/flores-map.d.ts +1 -0
  133. package/dist/modules/translator/language-codes/flores-map.js +107 -0
  134. package/dist/modules/translator/language-codes/index.d.ts +11 -0
  135. package/dist/modules/translator/language-codes/index.js +24 -0
  136. package/dist/modules/translator/language-codes/language-mapper.d.ts +25 -0
  137. package/dist/modules/translator/language-codes/language-mapper.js +132 -0
  138. package/dist/modules/translator/language-codes/rtl-languages.d.ts +2 -0
  139. package/dist/modules/translator/language-codes/rtl-languages.js +9 -0
  140. package/dist/modules/translator/memory/memory-matcher.d.ts +8 -0
  141. package/dist/modules/translator/memory/memory-matcher.js +218 -0
  142. package/dist/modules/translator/memory/types.d.ts +22 -0
  143. package/dist/modules/translator/memory/types.js +2 -0
  144. package/dist/modules/translator/translate.d.ts +38 -0
  145. package/dist/modules/translator/translate.js +932 -0
  146. package/dist/modules/translator/translator.d.ts +5 -0
  147. package/dist/modules/translator/translator.js +46 -0
  148. package/dist/modules/vue/migrate.d.ts +13 -0
  149. package/dist/modules/vue/migrate.js +40 -0
  150. package/dist/modules/vue/script-inject.d.ts +16 -0
  151. package/dist/modules/vue/script-inject.js +115 -0
  152. package/dist/modules/vue/sfc.d.ts +11 -0
  153. package/dist/modules/vue/sfc.js +51 -0
  154. package/dist/modules/vue/template-rewrite.d.ts +25 -0
  155. package/dist/modules/vue/template-rewrite.js +76 -0
  156. package/dist/modules/vue/template-scan.d.ts +29 -0
  157. package/dist/modules/vue/template-scan.js +264 -0
  158. package/dist/modules/workspace/index.d.ts +1 -0
  159. package/dist/modules/workspace/index.js +5 -0
  160. package/dist/modules/workspace/validate.d.ts +13 -0
  161. package/dist/modules/workspace/validate.js +298 -0
  162. package/dist/orchestrator.d.ts +8 -0
  163. package/dist/orchestrator.js +616 -0
  164. package/dist/providers/default-translation-provider.d.ts +20 -0
  165. package/dist/providers/default-translation-provider.js +30 -0
  166. package/dist/providers/index.d.ts +2 -0
  167. package/dist/providers/index.js +21 -0
  168. package/dist/providers/types.d.ts +145 -0
  169. package/dist/providers/types.js +2 -0
  170. package/dist/qa/check-project.d.ts +49 -0
  171. package/dist/qa/check-project.js +150 -0
  172. package/dist/qa/locale-files.d.ts +44 -0
  173. package/dist/qa/locale-files.js +159 -0
  174. package/dist/qa/locale-qa.d.ts +61 -0
  175. package/dist/qa/locale-qa.js +325 -0
  176. package/dist/qa/review.d.ts +43 -0
  177. package/dist/qa/review.js +175 -0
  178. package/dist/runtime/config.d.ts +8 -0
  179. package/dist/runtime/config.js +36 -0
  180. package/dist/runtime/index.d.ts +3 -0
  181. package/dist/runtime/index.js +8 -0
  182. package/dist/runtime/types.d.ts +363 -0
  183. package/dist/runtime/types.js +38 -0
  184. package/dist/types.d.ts +134 -0
  185. package/dist/types.js +12 -0
  186. package/dist/utils/ast-helpers.d.ts +57 -0
  187. package/dist/utils/ast-helpers.js +608 -0
  188. package/dist/utils/cache-keys.d.ts +8 -0
  189. package/dist/utils/cache-keys.js +18 -0
  190. package/dist/utils/circuit-breaker.d.ts +43 -0
  191. package/dist/utils/circuit-breaker.js +119 -0
  192. package/dist/utils/concurrent-map.d.ts +4 -0
  193. package/dist/utils/concurrent-map.js +21 -0
  194. package/dist/utils/detect-format.d.ts +5 -0
  195. package/dist/utils/detect-format.js +125 -0
  196. package/dist/utils/dtl-mask.d.ts +25 -0
  197. package/dist/utils/dtl-mask.js +58 -0
  198. package/dist/utils/extraction-options.d.ts +23 -0
  199. package/dist/utils/extraction-options.js +23 -0
  200. package/dist/utils/file-utils.d.ts +33 -0
  201. package/dist/utils/file-utils.js +262 -0
  202. package/dist/utils/glossary-match.d.ts +9 -0
  203. package/dist/utils/glossary-match.js +25 -0
  204. package/dist/utils/html-extractor.d.ts +8 -0
  205. package/dist/utils/html-extractor.js +317 -0
  206. package/dist/utils/i18next-icu.d.ts +38 -0
  207. package/dist/utils/i18next-icu.js +159 -0
  208. package/dist/utils/icu-message.d.ts +65 -0
  209. package/dist/utils/icu-message.js +346 -0
  210. package/dist/utils/icu-spans.d.ts +10 -0
  211. package/dist/utils/icu-spans.js +126 -0
  212. package/dist/utils/jsx-message.d.ts +93 -0
  213. package/dist/utils/jsx-message.js +621 -0
  214. package/dist/utils/key-usage.d.ts +35 -0
  215. package/dist/utils/key-usage.js +247 -0
  216. package/dist/utils/logger.d.ts +24 -0
  217. package/dist/utils/logger.js +33 -0
  218. package/dist/utils/metadata.d.ts +80 -0
  219. package/dist/utils/metadata.js +255 -0
  220. package/dist/utils/module-constants.d.ts +30 -0
  221. package/dist/utils/module-constants.js +265 -0
  222. package/dist/utils/not-injected.d.ts +6 -0
  223. package/dist/utils/not-injected.js +36 -0
  224. package/dist/utils/placeholder-protection.d.ts +57 -0
  225. package/dist/utils/placeholder-protection.js +136 -0
  226. package/dist/utils/pseudo-markers.d.ts +9 -0
  227. package/dist/utils/pseudo-markers.js +16 -0
  228. package/dist/utils/py-extractor.d.ts +14 -0
  229. package/dist/utils/py-extractor.js +285 -0
  230. package/dist/utils/string-utils.d.ts +52 -0
  231. package/dist/utils/string-utils.js +270 -0
  232. package/dist/utils/translation-quality.d.ts +34 -0
  233. package/dist/utils/translation-quality.js +156 -0
  234. package/dist/utils/translation-sanitizer.d.ts +10 -0
  235. package/dist/utils/translation-sanitizer.js +57 -0
  236. package/dist/utils/translator-caller.d.ts +32 -0
  237. package/dist/utils/translator-caller.js +217 -0
  238. package/dist/utils/translator-load-balancer.d.ts +73 -0
  239. package/dist/utils/translator-load-balancer.js +293 -0
  240. package/dist/utils/ui-copy.d.ts +32 -0
  241. package/dist/utils/ui-copy.js +126 -0
  242. package/dist/utils/workspace.d.ts +14 -0
  243. package/dist/utils/workspace.js +89 -0
  244. package/package.json +64 -4
  245. package/schemas/translate-report.schema.json +297 -0
  246. package/README.md +0 -3
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withRetry503 = exports.ProviderHttpError = void 0;
4
+ exports.parseRetryAfter = parseRetryAfter;
5
+ exports.isRetryable = isRetryable;
6
+ exports.withRetry = withRetry;
7
+ function abortError() {
8
+ const err = new Error('Translation request aborted');
9
+ err.name = 'AbortError';
10
+ return err;
11
+ }
12
+ function sleep(ms, signal) {
13
+ return new Promise((resolve, reject) => {
14
+ if (signal?.aborted)
15
+ return reject(abortError());
16
+ const onAbort = () => {
17
+ clearTimeout(timer);
18
+ reject(abortError());
19
+ };
20
+ const timer = setTimeout(() => {
21
+ signal?.removeEventListener('abort', onAbort);
22
+ resolve();
23
+ }, ms);
24
+ signal?.addEventListener('abort', onAbort, { once: true });
25
+ });
26
+ }
27
+ /** An HTTP error from a translation API, carrying what retry decisions need. */
28
+ class ProviderHttpError extends Error {
29
+ constructor(message, status,
30
+ /** Server-requested wait from a Retry-After header, in ms. */
31
+ retryAfterMs) {
32
+ super(message);
33
+ this.status = status;
34
+ this.retryAfterMs = retryAfterMs;
35
+ this.name = 'ProviderHttpError';
36
+ }
37
+ }
38
+ exports.ProviderHttpError = ProviderHttpError;
39
+ /** Parse a Retry-After header (seconds or HTTP date) into milliseconds. */
40
+ function parseRetryAfter(header) {
41
+ if (!header)
42
+ return undefined;
43
+ const seconds = Number(header);
44
+ if (Number.isFinite(seconds) && seconds >= 0)
45
+ return seconds * 1000;
46
+ const date = Date.parse(header);
47
+ if (!Number.isNaN(date))
48
+ return Math.max(0, date - Date.now());
49
+ return undefined;
50
+ }
51
+ /**
52
+ * Retry only what can succeed on a second try: timeouts, network errors,
53
+ * 408/409/425/429 and 5xx. Auth, bad request and unknown model (other 4xx)
54
+ * fail immediately — retrying a bad key just delays the error.
55
+ */
56
+ function isRetryable(err) {
57
+ if (err instanceof ProviderHttpError) {
58
+ return err.status >= 500 || [408, 409, 425, 429].includes(err.status);
59
+ }
60
+ return true;
61
+ }
62
+ async function withRetry(fn, opts) {
63
+ const maxDelay = opts.maxDelayMs ?? 60000;
64
+ let attempt = 0;
65
+ const outer = opts.signal;
66
+ while (true) {
67
+ if (outer?.aborted)
68
+ throw abortError();
69
+ const controller = new AbortController();
70
+ const timer = setTimeout(() => controller.abort(), opts.timeoutMs);
71
+ const onAbort = () => controller.abort();
72
+ outer?.addEventListener('abort', onAbort, { once: true });
73
+ try {
74
+ return await fn(controller.signal);
75
+ }
76
+ catch (err) {
77
+ if (outer?.aborted)
78
+ throw abortError();
79
+ attempt += 1;
80
+ if (attempt > opts.maxRetries || !isRetryable(err))
81
+ throw err;
82
+ const backoff = opts.baseDelayMs * Math.pow(2, attempt - 1);
83
+ const requested = err instanceof ProviderHttpError ? err.retryAfterMs : undefined;
84
+ const delay = Math.min(maxDelay, Math.max(backoff, requested ?? 0));
85
+ await sleep(delay, outer);
86
+ }
87
+ finally {
88
+ clearTimeout(timer);
89
+ outer?.removeEventListener('abort', onAbort);
90
+ }
91
+ }
92
+ }
93
+ /** @deprecated Use withRetry — kept for existing callers. */
94
+ exports.withRetry503 = withRetry;
@@ -0,0 +1,31 @@
1
+ import type { TranslatorClient } from '../translator.interface';
2
+ import type { HFTranslateRequest, HFTranslateResponse } from '../request.types';
3
+ /**
4
+ * Mock translator client for unit tests.
5
+ *
6
+ * - `health()` / `warmup()` → always succeed (configurable)
7
+ * - `translate()` → deterministic: prefixes each value with `[targetLang]`
8
+ * - `calls` array records every invocation for assertion
9
+ *
10
+ * Usage:
11
+ * const mock = new MockTranslatorClient();
12
+ * // … run code under test …
13
+ * expect(mock.calls.length).toBe(2);
14
+ */
15
+ export declare class MockTranslatorClient implements TranslatorClient {
16
+ /** Every translate() call is pushed here for inspection. */
17
+ readonly calls: HFTranslateRequest[];
18
+ /** Configure health() return value. */
19
+ healthResult: boolean;
20
+ /** Configure warmup() return value. */
21
+ warmupResult: boolean;
22
+ /** If set, translate() will throw this error. */
23
+ translateError: Error | null;
24
+ /** Custom translate handler — overrides default deterministic behaviour. */
25
+ translateHandler: ((req: HFTranslateRequest) => HFTranslateResponse) | null;
26
+ health(): Promise<boolean>;
27
+ warmup(): Promise<boolean>;
28
+ translate(req: HFTranslateRequest): Promise<HFTranslateResponse>;
29
+ /** Reset call history and configuration. */
30
+ reset(): void;
31
+ }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MockTranslatorClient = void 0;
4
+ /**
5
+ * Mock translator client for unit tests.
6
+ *
7
+ * - `health()` / `warmup()` → always succeed (configurable)
8
+ * - `translate()` → deterministic: prefixes each value with `[targetLang]`
9
+ * - `calls` array records every invocation for assertion
10
+ *
11
+ * Usage:
12
+ * const mock = new MockTranslatorClient();
13
+ * // … run code under test …
14
+ * expect(mock.calls.length).toBe(2);
15
+ */
16
+ class MockTranslatorClient {
17
+ constructor() {
18
+ /** Every translate() call is pushed here for inspection. */
19
+ this.calls = [];
20
+ /** Configure health() return value. */
21
+ this.healthResult = true;
22
+ /** Configure warmup() return value. */
23
+ this.warmupResult = true;
24
+ /** If set, translate() will throw this error. */
25
+ this.translateError = null;
26
+ /** Custom translate handler — overrides default deterministic behaviour. */
27
+ this.translateHandler = null;
28
+ }
29
+ async health() {
30
+ return this.healthResult;
31
+ }
32
+ async warmup() {
33
+ return this.warmupResult;
34
+ }
35
+ async translate(req) {
36
+ this.calls.push(req);
37
+ if (this.translateError)
38
+ throw this.translateError;
39
+ if (this.translateHandler)
40
+ return this.translateHandler(req);
41
+ // Deterministic: prefix each value with the target language code
42
+ return {
43
+ translations: req.entries.map((e) => ({
44
+ key: e.key,
45
+ value: `[${req.targetLang}] ${e.value}`,
46
+ })),
47
+ model: 'mock-nllb-200',
48
+ };
49
+ }
50
+ /** Reset call history and configuration. */
51
+ reset() {
52
+ this.calls.length = 0;
53
+ this.healthResult = true;
54
+ this.warmupResult = true;
55
+ this.translateError = null;
56
+ this.translateHandler = null;
57
+ }
58
+ }
59
+ exports.MockTranslatorClient = MockTranslatorClient;
@@ -0,0 +1 @@
1
+ export type { TranslatorClient } from '../../../types';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Babelize Error Types
3
+ *
4
+ * Typed error class for structured error handling across the engine.
5
+ */
6
+ export type BabelizeErrorCode = 'UNSUPPORTED_LANGUAGE' | 'TRANSLATION_FAILED' | 'INVALID_INPUT';
7
+ export declare class BabelizeError extends Error {
8
+ readonly code: BabelizeErrorCode;
9
+ constructor(code: BabelizeErrorCode, message: string);
10
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ /**
3
+ * Babelize Error Types
4
+ *
5
+ * Typed error class for structured error handling across the engine.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.BabelizeError = void 0;
9
+ class BabelizeError extends Error {
10
+ constructor(code, message) {
11
+ super(message);
12
+ this.name = 'BabelizeError';
13
+ this.code = code;
14
+ // Maintains proper prototype chain for instanceof checks
15
+ Object.setPrototypeOf(this, BabelizeError.prototype);
16
+ }
17
+ }
18
+ exports.BabelizeError = BabelizeError;
@@ -0,0 +1 @@
1
+ export declare const FLORES_MAP: Record<string, string>;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FLORES_MAP = void 0;
4
+ exports.FLORES_MAP = {
5
+ af: "afr_Latn",
6
+ sq: "als_Latn",
7
+ am: "amh_Ethi",
8
+ ar: "arb_Arab",
9
+ hy: "hye_Armn",
10
+ az: "azj_Latn",
11
+ eu: "eus_Latn",
12
+ be: "bel_Cyrl",
13
+ bn: "ben_Beng",
14
+ bs: "bos_Latn",
15
+ bg: "bul_Cyrl",
16
+ my: "mya_Mymr",
17
+ ca: "cat_Latn",
18
+ ceb: "ceb_Latn",
19
+ hr: "hrv_Latn",
20
+ cs: "ces_Latn",
21
+ da: "dan_Latn",
22
+ nl: "nld_Latn",
23
+ en: "eng_Latn",
24
+ eo: "epo_Latn",
25
+ et: "est_Latn",
26
+ tl: "tgl_Latn",
27
+ fi: "fin_Latn",
28
+ fr: "fra_Latn",
29
+ gl: "glg_Latn",
30
+ ka: "kat_Geor",
31
+ de: "deu_Latn",
32
+ el: "ell_Grek",
33
+ gu: "guj_Gujr",
34
+ ht: "hat_Latn",
35
+ ha: "hau_Latn",
36
+ haw: "haw_Latn",
37
+ he: "heb_Hebr",
38
+ hi: "hin_Deva",
39
+ hmn: "hmn_Latn",
40
+ hu: "hun_Latn",
41
+ is: "isl_Latn",
42
+ ig: "ibo_Latn",
43
+ id: "ind_Latn",
44
+ ga: "gle_Latn",
45
+ it: "ita_Latn",
46
+ ja: "jpn_Jpan",
47
+ jw: "jav_Latn",
48
+ kn: "kan_Knda",
49
+ kk: "kaz_Cyrl",
50
+ km: "khm_Khmr",
51
+ ko: "kor_Hang",
52
+ ku: "kmr_Latn",
53
+ ky: "kir_Cyrl",
54
+ lo: "lao_Laoo",
55
+ la: "lat_Latn",
56
+ lv: "lvs_Latn",
57
+ lt: "lit_Latn",
58
+ lb: "ltz_Latn",
59
+ mk: "mkd_Cyrl",
60
+ mg: "plt_Latn",
61
+ ms: "zsm_Latn",
62
+ ml: "mal_Mlym",
63
+ mt: "mlt_Latn",
64
+ mi: "mri_Latn",
65
+ mr: "mar_Deva",
66
+ mn: "khk_Cyrl",
67
+ ne: "npi_Deva",
68
+ no: "nob_Latn",
69
+ nb: "nob_Latn",
70
+ ny: "nya_Latn",
71
+ ps: "pbt_Arab",
72
+ fa: "pes_Arab",
73
+ pl: "pol_Latn",
74
+ pt: "por_Latn",
75
+ pa: "pan_Guru",
76
+ ro: "ron_Latn",
77
+ ru: "rus_Cyrl",
78
+ sm: "smo_Latn",
79
+ gd: "gla_Latn",
80
+ sr: "srp_Cyrl",
81
+ st: "sot_Latn",
82
+ sn: "sna_Latn",
83
+ sd: "snd_Arab",
84
+ si: "sin_Sinh",
85
+ sk: "slk_Latn",
86
+ sl: "slv_Latn",
87
+ so: "som_Latn",
88
+ es: "spa_Latn",
89
+ su: "sun_Latn",
90
+ sw: "swh_Latn",
91
+ sv: "swe_Latn",
92
+ tg: "tgk_Cyrl",
93
+ ta: "tam_Taml",
94
+ te: "tel_Telu",
95
+ th: "tha_Thai",
96
+ tr: "tur_Latn",
97
+ uk: "ukr_Cyrl",
98
+ ur: "urd_Arab",
99
+ uz: "uzn_Latn",
100
+ vi: "vie_Latn",
101
+ cy: "cym_Latn",
102
+ xh: "xho_Latn",
103
+ yi: "ydd_Hebr",
104
+ yo: "yor_Latn",
105
+ zu: "zul_Latn",
106
+ zh: "zho_Hans",
107
+ };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Language Codes Module — barrel export
3
+ *
4
+ * Single import point for all language code utilities:
5
+ * import { toFlores, isSupported, RTL_LANGUAGES } from '../modules/translator/language-codes';
6
+ */
7
+ export { BabelizeError } from './errors';
8
+ export type { BabelizeErrorCode } from './errors';
9
+ export { FLORES_MAP } from './flores-map';
10
+ export { toFlores, fromFlores, isSupported, isNllbSupported, getSupportedLanguages, storageLangCode, localeFromStorageCode } from './language-mapper';
11
+ export { RTL_LANGUAGES, isRTL } from './rtl-languages';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ /**
3
+ * Language Codes Module — barrel export
4
+ *
5
+ * Single import point for all language code utilities:
6
+ * import { toFlores, isSupported, RTL_LANGUAGES } from '../modules/translator/language-codes';
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.isRTL = exports.RTL_LANGUAGES = exports.localeFromStorageCode = exports.storageLangCode = exports.getSupportedLanguages = exports.isNllbSupported = exports.isSupported = exports.fromFlores = exports.toFlores = exports.FLORES_MAP = exports.BabelizeError = void 0;
10
+ var errors_1 = require("./errors");
11
+ Object.defineProperty(exports, "BabelizeError", { enumerable: true, get: function () { return errors_1.BabelizeError; } });
12
+ var flores_map_1 = require("./flores-map");
13
+ Object.defineProperty(exports, "FLORES_MAP", { enumerable: true, get: function () { return flores_map_1.FLORES_MAP; } });
14
+ var language_mapper_1 = require("./language-mapper");
15
+ Object.defineProperty(exports, "toFlores", { enumerable: true, get: function () { return language_mapper_1.toFlores; } });
16
+ Object.defineProperty(exports, "fromFlores", { enumerable: true, get: function () { return language_mapper_1.fromFlores; } });
17
+ Object.defineProperty(exports, "isSupported", { enumerable: true, get: function () { return language_mapper_1.isSupported; } });
18
+ Object.defineProperty(exports, "isNllbSupported", { enumerable: true, get: function () { return language_mapper_1.isNllbSupported; } });
19
+ Object.defineProperty(exports, "getSupportedLanguages", { enumerable: true, get: function () { return language_mapper_1.getSupportedLanguages; } });
20
+ Object.defineProperty(exports, "storageLangCode", { enumerable: true, get: function () { return language_mapper_1.storageLangCode; } });
21
+ Object.defineProperty(exports, "localeFromStorageCode", { enumerable: true, get: function () { return language_mapper_1.localeFromStorageCode; } });
22
+ var rtl_languages_1 = require("./rtl-languages");
23
+ Object.defineProperty(exports, "RTL_LANGUAGES", { enumerable: true, get: function () { return rtl_languages_1.RTL_LANGUAGES; } });
24
+ Object.defineProperty(exports, "isRTL", { enumerable: true, get: function () { return rtl_languages_1.isRTL; } });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * FLORES-200 code for an ISO locale.
3
+ * - Region variants map to their language (pt-BR → por_Latn); Chinese regions
4
+ * pick the script (zh-TW → zho_Hant, zh-CN → zho_Hans).
5
+ * - Script subtags are honoured: sr-Cyrl → srp_Cyrl, pa-Arab → pnb_Arab. A
6
+ * script FLORES-200 lacks gets a FLORES-style code (sr-Latn → srp_Latn) that
7
+ * only LLM providers can use — see isNllbSupported().
8
+ */
9
+ export declare function toFlores(isoCode: string): string;
10
+ export declare function fromFlores(floresCode: string): string;
11
+ /** True when any translator can handle the locale (LLM providers use the ISO tag). */
12
+ export declare function isSupported(isoCode: string): boolean;
13
+ /** True when the NLLB translator service can translate into the locale. */
14
+ export declare function isNllbSupported(isoCode: string): boolean;
15
+ /**
16
+ * Key for storing translations of `isoCode` (translation cache, translation
17
+ * memory): the FLORES code including the script, plus the region when there
18
+ * is one, so variants never share translations — pt-BR → por_Latn-br,
19
+ * sr-Latn → srp_Latn, sr-Cyrl-RS → srp_Cyrl-rs. A plain language code keeps
20
+ * its FLORES code (pt → por_Latn), as before.
21
+ */
22
+ export declare function storageLangCode(isoCode: string): string;
23
+ /** BCP-47 tag for a storage code (por_Latn-br → pt-BR, srp_Latn → sr-Latn). */
24
+ export declare function localeFromStorageCode(code: string): string;
25
+ export declare function getSupportedLanguages(): string[];
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toFlores = toFlores;
4
+ exports.fromFlores = fromFlores;
5
+ exports.isSupported = isSupported;
6
+ exports.isNllbSupported = isNllbSupported;
7
+ exports.storageLangCode = storageLangCode;
8
+ exports.localeFromStorageCode = localeFromStorageCode;
9
+ exports.getSupportedLanguages = getSupportedLanguages;
10
+ const errors_1 = require("./errors");
11
+ const flores_map_1 = require("./flores-map");
12
+ /**
13
+ * Script variants that have their own FLORES-200 code (NLLB can translate
14
+ * them). Keys are `language-script` in lower case.
15
+ */
16
+ const SCRIPT_FLORES = {
17
+ 'zh-hans': 'zho_Hans',
18
+ 'zh-hant': 'zho_Hant',
19
+ 'pa-arab': 'pnb_Arab',
20
+ 'az-arab': 'azb_Arab',
21
+ };
22
+ /** Chinese regions imply a script. */
23
+ const REGION_SCRIPT = {
24
+ 'zh-tw': 'hant',
25
+ 'zh-hk': 'hant',
26
+ 'zh-mo': 'hant',
27
+ 'zh-cn': 'hans',
28
+ 'zh-sg': 'hans',
29
+ };
30
+ /** language[-Script][-REGION], e.g. pt-BR, sr-Latn, zh-Hant-TW, es-419. */
31
+ const LOCALE_TAG = /^([a-z]{2,3})(?:-([a-z]{4}))?(?:-([a-z]{2}|\d{3}))?$/;
32
+ function normalize(code) {
33
+ return code.trim().toLowerCase().replace(/_/g, '-');
34
+ }
35
+ function own(map, key) {
36
+ // Own keys only: `in` would also accept prototype keys like "constructor".
37
+ return Object.prototype.hasOwnProperty.call(map, key) ? map[key] : undefined;
38
+ }
39
+ function titleCase(script) {
40
+ return script.charAt(0).toUpperCase() + script.slice(1);
41
+ }
42
+ function resolve(isoCode) {
43
+ const n = normalize(isoCode);
44
+ const plain = own(flores_map_1.FLORES_MAP, n);
45
+ if (plain)
46
+ return { flores: plain, nllb: true, plain: true };
47
+ const m = LOCALE_TAG.exec(n);
48
+ if (!m)
49
+ return undefined;
50
+ const [, lang, explicitScript, region] = m;
51
+ const base = own(flores_map_1.FLORES_MAP, lang);
52
+ if (!base)
53
+ return undefined;
54
+ const script = explicitScript ?? (region ? own(REGION_SCRIPT, `${lang}-${region}`) : undefined);
55
+ if (!script)
56
+ return { flores: base, nllb: true, plain: false, region };
57
+ const variant = own(SCRIPT_FLORES, `${lang}-${script}`);
58
+ if (variant)
59
+ return { flores: variant, nllb: true, plain: false, region };
60
+ const [baseLang, baseScript] = base.split('_');
61
+ if (script === baseScript.toLowerCase())
62
+ return { flores: base, nllb: true, plain: false, region };
63
+ // A script FLORES-200 doesn't have for this language (sr-Latn, uz-Cyrl,
64
+ // az-Cyrl): LLM providers translate it from the ISO locale; NLLB can't.
65
+ return { flores: `${baseLang}_${titleCase(script)}`, nllb: false, plain: false, region };
66
+ }
67
+ /**
68
+ * FLORES-200 code for an ISO locale.
69
+ * - Region variants map to their language (pt-BR → por_Latn); Chinese regions
70
+ * pick the script (zh-TW → zho_Hant, zh-CN → zho_Hans).
71
+ * - Script subtags are honoured: sr-Cyrl → srp_Cyrl, pa-Arab → pnb_Arab. A
72
+ * script FLORES-200 lacks gets a FLORES-style code (sr-Latn → srp_Latn) that
73
+ * only LLM providers can use — see isNllbSupported().
74
+ */
75
+ function toFlores(isoCode) {
76
+ const r = resolve(isoCode);
77
+ if (!r) {
78
+ throw new errors_1.BabelizeError('UNSUPPORTED_LANGUAGE', `Unsupported language code: "${isoCode}". Use getSupportedLanguages() to see available codes.`);
79
+ }
80
+ return r.flores;
81
+ }
82
+ function fromFlores(floresCode) {
83
+ const entry = Object.entries(flores_map_1.FLORES_MAP).find(([, code]) => code === floresCode);
84
+ if (entry)
85
+ return entry[0];
86
+ const variant = Object.entries(SCRIPT_FLORES).find(([, code]) => code === floresCode);
87
+ if (variant) {
88
+ const [lang, script] = variant[0].split('-');
89
+ return `${lang}-${titleCase(script)}`;
90
+ }
91
+ const [langPart, script] = floresCode.split('_');
92
+ const base = Object.entries(flores_map_1.FLORES_MAP).find(([, code]) => code.split('_')[0] === langPart);
93
+ if (base && script)
94
+ return `${base[0]}-${script}`;
95
+ throw new errors_1.BabelizeError('UNSUPPORTED_LANGUAGE', `Flores code '${floresCode}' is not supported.`);
96
+ }
97
+ /** True when any translator can handle the locale (LLM providers use the ISO tag). */
98
+ function isSupported(isoCode) {
99
+ return resolve(isoCode) !== undefined;
100
+ }
101
+ /** True when the NLLB translator service can translate into the locale. */
102
+ function isNllbSupported(isoCode) {
103
+ return resolve(isoCode)?.nllb === true;
104
+ }
105
+ /**
106
+ * Key for storing translations of `isoCode` (translation cache, translation
107
+ * memory): the FLORES code including the script, plus the region when there
108
+ * is one, so variants never share translations — pt-BR → por_Latn-br,
109
+ * sr-Latn → srp_Latn, sr-Cyrl-RS → srp_Cyrl-rs. A plain language code keeps
110
+ * its FLORES code (pt → por_Latn), as before.
111
+ */
112
+ function storageLangCode(isoCode) {
113
+ const r = resolve(isoCode);
114
+ if (!r)
115
+ return toFlores(isoCode); // throws the usual error
116
+ if (r.plain || !r.region)
117
+ return r.flores;
118
+ return `${r.flores}-${r.region}`;
119
+ }
120
+ /** BCP-47 tag for a storage code (por_Latn-br → pt-BR, srp_Latn → sr-Latn). */
121
+ function localeFromStorageCode(code) {
122
+ const [flores, region] = code.split('-');
123
+ if (!region)
124
+ return fromFlores(flores);
125
+ // Chinese regions imply the script: zho_Hant-tw → zh-TW, zho_Hans-cn → zh-CN.
126
+ if (flores === 'zho_Hant' || flores === 'zho_Hans')
127
+ return `zh-${region.toUpperCase()}`;
128
+ return `${fromFlores(flores)}-${/^\d+$/.test(region) ? region : region.toUpperCase()}`;
129
+ }
130
+ function getSupportedLanguages() {
131
+ return Object.keys(flores_map_1.FLORES_MAP);
132
+ }
@@ -0,0 +1,2 @@
1
+ export declare const RTL_LANGUAGES: string[];
2
+ export declare function isRTL(locale: string): boolean;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RTL_LANGUAGES = void 0;
4
+ exports.isRTL = isRTL;
5
+ exports.RTL_LANGUAGES = ['ar', 'he', 'fa', 'ur', 'ps'];
6
+ function isRTL(locale) {
7
+ const base = locale.split('-')[0];
8
+ return exports.RTL_LANGUAGES.includes(base);
9
+ }
@@ -0,0 +1,8 @@
1
+ import type { MemoryEntry, MemoryLookupResult } from './types';
2
+ /**
3
+ * Match from memory with exact and fuzzy matching.
4
+ * Exact matches are a map lookup; fuzzy matches (≥90% similar) are only
5
+ * computed when there is no exact match, and only against candidates whose
6
+ * length could possibly reach the threshold.
7
+ */
8
+ export declare function matchFromMemory(entries: MemoryEntry[], sourceText: string, targetLocale: string): MemoryLookupResult;