@babelize/engine 0.0.0-stage → 0.1.2

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 (270) 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/detection/stack.d.ts +42 -0
  11. package/dist/detection/stack.js +277 -0
  12. package/dist/django/catalog.d.ts +92 -0
  13. package/dist/django/catalog.js +677 -0
  14. package/dist/django/extract.d.ts +20 -0
  15. package/dist/django/extract.js +79 -0
  16. package/dist/django/inject-file.d.ts +6 -0
  17. package/dist/django/inject-file.js +52 -0
  18. package/dist/django/inject.d.ts +11 -0
  19. package/dist/django/inject.js +205 -0
  20. package/dist/django/keys.d.ts +21 -0
  21. package/dist/django/keys.js +63 -0
  22. package/dist/django/locale-names.d.ts +13 -0
  23. package/dist/django/locale-names.js +60 -0
  24. package/dist/django/mo-file.d.ts +6 -0
  25. package/dist/django/mo-file.js +60 -0
  26. package/dist/django/plural-forms.d.ts +34 -0
  27. package/dist/django/plural-forms.js +216 -0
  28. package/dist/django/po-file.d.ts +38 -0
  29. package/dist/django/po-file.js +273 -0
  30. package/dist/django/python-extract.d.ts +34 -0
  31. package/dist/django/python-extract.js +619 -0
  32. package/dist/django/python-lexer.d.ts +65 -0
  33. package/dist/django/python-lexer.js +398 -0
  34. package/dist/django/template-extract.d.ts +32 -0
  35. package/dist/django/template-extract.js +680 -0
  36. package/dist/django/types.d.ts +49 -0
  37. package/dist/django/types.js +15 -0
  38. package/dist/index.d.ts +59 -0
  39. package/dist/index.js +140 -0
  40. package/dist/injection/InjectionStrategy.d.ts +62 -0
  41. package/dist/injection/InjectionStrategy.js +2 -0
  42. package/dist/injection/StrategyFactory.d.ts +4 -0
  43. package/dist/injection/StrategyFactory.js +29 -0
  44. package/dist/injection/html/html-injector.d.ts +11 -0
  45. package/dist/injection/html/html-injector.js +173 -0
  46. package/dist/injection/strategies/NextIntlStrategy.d.ts +25 -0
  47. package/dist/injection/strategies/NextIntlStrategy.js +141 -0
  48. package/dist/injection/strategies/ReactI18nextStrategy.d.ts +19 -0
  49. package/dist/injection/strategies/ReactI18nextStrategy.js +116 -0
  50. package/dist/injection/strategies/VanillaStrategy.d.ts +11 -0
  51. package/dist/injection/strategies/VanillaStrategy.js +73 -0
  52. package/dist/injection/strategies/VueI18nStrategy.d.ts +11 -0
  53. package/dist/injection/strategies/VueI18nStrategy.js +84 -0
  54. package/dist/local/formatter.d.ts +26 -0
  55. package/dist/local/formatter.js +105 -0
  56. package/dist/local/git.d.ts +23 -0
  57. package/dist/local/git.js +136 -0
  58. package/dist/local/hosted-provider.d.ts +68 -0
  59. package/dist/local/hosted-provider.js +215 -0
  60. package/dist/local/index.d.ts +23 -0
  61. package/dist/local/index.js +75 -0
  62. package/dist/local/provider.d.ts +56 -0
  63. package/dist/local/provider.js +119 -0
  64. package/dist/local/pseudo-provider.d.ts +18 -0
  65. package/dist/local/pseudo-provider.js +208 -0
  66. package/dist/local/report.d.ts +177 -0
  67. package/dist/local/report.js +186 -0
  68. package/dist/local/translate-core.d.ts +218 -0
  69. package/dist/local/translate-core.js +499 -0
  70. package/dist/local/translate-messages.d.ts +49 -0
  71. package/dist/local/translate-messages.js +89 -0
  72. package/dist/local/workspace-copy.d.ts +91 -0
  73. package/dist/local/workspace-copy.js +355 -0
  74. package/dist/modules/extractor/extract.d.ts +20 -0
  75. package/dist/modules/extractor/extract.js +709 -0
  76. package/dist/modules/extractor/index.d.ts +2 -0
  77. package/dist/modules/extractor/index.js +63 -0
  78. package/dist/modules/finalization/django-finalize.d.ts +28 -0
  79. package/dist/modules/finalization/django-finalize.js +412 -0
  80. package/dist/modules/finalization/finalize.d.ts +38 -0
  81. package/dist/modules/finalization/finalize.js +745 -0
  82. package/dist/modules/finalization/index.d.ts +1 -0
  83. package/dist/modules/finalization/index.js +5 -0
  84. package/dist/modules/finalization/nextjs-finalize.d.ts +40 -0
  85. package/dist/modules/finalization/nextjs-finalize.js +328 -0
  86. package/dist/modules/finalization/static-runtime.d.ts +9 -0
  87. package/dist/modules/finalization/static-runtime.js +261 -0
  88. package/dist/modules/finalization/writes.d.ts +29 -0
  89. package/dist/modules/finalization/writes.js +78 -0
  90. package/dist/modules/format-injection/index.d.ts +19 -0
  91. package/dist/modules/format-injection/index.js +273 -0
  92. package/dist/modules/format-injection/inject-formats.d.ts +31 -0
  93. package/dist/modules/format-injection/inject-formats.js +208 -0
  94. package/dist/modules/glossary/index.d.ts +2 -0
  95. package/dist/modules/glossary/index.js +16 -0
  96. package/dist/modules/glossary/rules.d.ts +81 -0
  97. package/dist/modules/glossary/rules.js +244 -0
  98. package/dist/modules/glossary/types.d.ts +22 -0
  99. package/dist/modules/glossary/types.js +2 -0
  100. package/dist/modules/injection/index.d.ts +1 -0
  101. package/dist/modules/injection/index.js +6 -0
  102. package/dist/modules/injection/inject.d.ts +29 -0
  103. package/dist/modules/injection/inject.js +1050 -0
  104. package/dist/modules/keygen/index.d.ts +1 -0
  105. package/dist/modules/keygen/index.js +11 -0
  106. package/dist/modules/keygen/keygen.d.ts +4 -0
  107. package/dist/modules/keygen/keygen.js +123 -0
  108. package/dist/modules/keygen/locale-data.d.ts +7 -0
  109. package/dist/modules/keygen/locale-data.js +73 -0
  110. package/dist/modules/keygen/public.d.ts +2 -0
  111. package/dist/modules/keygen/public.js +12 -0
  112. package/dist/modules/locale-builder/index.d.ts +1 -0
  113. package/dist/modules/locale-builder/index.js +7 -0
  114. package/dist/modules/locale-builder/locales.d.ts +13 -0
  115. package/dist/modules/locale-builder/locales.js +130 -0
  116. package/dist/modules/lockfile/hasher.d.ts +7 -0
  117. package/dist/modules/lockfile/hasher.js +13 -0
  118. package/dist/modules/lockfile/index.d.ts +3 -0
  119. package/dist/modules/lockfile/index.js +22 -0
  120. package/dist/modules/lockfile/lockfile-manager.d.ts +99 -0
  121. package/dist/modules/lockfile/lockfile-manager.js +497 -0
  122. package/dist/modules/lockfile/schema.d.ts +196 -0
  123. package/dist/modules/lockfile/schema.js +56 -0
  124. package/dist/modules/parser/index.d.ts +2 -0
  125. package/dist/modules/parser/index.js +9 -0
  126. package/dist/modules/summary/index.d.ts +1 -0
  127. package/dist/modules/summary/index.js +7 -0
  128. package/dist/modules/summary/summary.d.ts +9 -0
  129. package/dist/modules/summary/summary.js +195 -0
  130. package/dist/modules/sync-detector/index.d.ts +2 -0
  131. package/dist/modules/sync-detector/index.js +7 -0
  132. package/dist/modules/sync-detector/reporter.d.ts +3 -0
  133. package/dist/modules/sync-detector/reporter.js +16 -0
  134. package/dist/modules/sync-detector/sync-detector.d.ts +39 -0
  135. package/dist/modules/sync-detector/sync-detector.js +374 -0
  136. package/dist/modules/translator/batch-optimizer.d.ts +12 -0
  137. package/dist/modules/translator/batch-optimizer.js +30 -0
  138. package/dist/modules/translator/client/byok.provider.d.ts +64 -0
  139. package/dist/modules/translator/client/byok.provider.js +319 -0
  140. package/dist/modules/translator/client/direct-translator.client.d.ts +12 -0
  141. package/dist/modules/translator/client/direct-translator.client.js +71 -0
  142. package/dist/modules/translator/client/gemini.provider.d.ts +1 -0
  143. package/dist/modules/translator/client/gemini.provider.js +110 -0
  144. package/dist/modules/translator/client/groq.provider.d.ts +1 -0
  145. package/dist/modules/translator/client/groq.provider.js +111 -0
  146. package/dist/modules/translator/client/request.types.d.ts +1 -0
  147. package/dist/modules/translator/client/request.types.js +2 -0
  148. package/dist/modules/translator/client/retry.d.ts +29 -0
  149. package/dist/modules/translator/client/retry.js +94 -0
  150. package/dist/modules/translator/client/test-utils/mock.client.d.ts +31 -0
  151. package/dist/modules/translator/client/test-utils/mock.client.js +59 -0
  152. package/dist/modules/translator/client/translator.interface.d.ts +1 -0
  153. package/dist/modules/translator/client/translator.interface.js +2 -0
  154. package/dist/modules/translator/icu-branches.d.ts +138 -0
  155. package/dist/modules/translator/icu-branches.js +393 -0
  156. package/dist/modules/translator/language-codes/errors.d.ts +10 -0
  157. package/dist/modules/translator/language-codes/errors.js +18 -0
  158. package/dist/modules/translator/language-codes/flores-map.d.ts +1 -0
  159. package/dist/modules/translator/language-codes/flores-map.js +107 -0
  160. package/dist/modules/translator/language-codes/index.d.ts +11 -0
  161. package/dist/modules/translator/language-codes/index.js +24 -0
  162. package/dist/modules/translator/language-codes/language-mapper.d.ts +25 -0
  163. package/dist/modules/translator/language-codes/language-mapper.js +132 -0
  164. package/dist/modules/translator/language-codes/rtl-languages.d.ts +2 -0
  165. package/dist/modules/translator/language-codes/rtl-languages.js +9 -0
  166. package/dist/modules/translator/memory/memory-matcher.d.ts +8 -0
  167. package/dist/modules/translator/memory/memory-matcher.js +218 -0
  168. package/dist/modules/translator/memory/types.d.ts +22 -0
  169. package/dist/modules/translator/memory/types.js +2 -0
  170. package/dist/modules/translator/translate.d.ts +74 -0
  171. package/dist/modules/translator/translate.js +1047 -0
  172. package/dist/modules/vue/migrate.d.ts +13 -0
  173. package/dist/modules/vue/migrate.js +40 -0
  174. package/dist/modules/vue/script-inject.d.ts +16 -0
  175. package/dist/modules/vue/script-inject.js +115 -0
  176. package/dist/modules/vue/sfc.d.ts +11 -0
  177. package/dist/modules/vue/sfc.js +51 -0
  178. package/dist/modules/vue/template-rewrite.d.ts +25 -0
  179. package/dist/modules/vue/template-rewrite.js +76 -0
  180. package/dist/modules/vue/template-scan.d.ts +29 -0
  181. package/dist/modules/vue/template-scan.js +264 -0
  182. package/dist/modules/workspace/index.d.ts +1 -0
  183. package/dist/modules/workspace/index.js +5 -0
  184. package/dist/modules/workspace/validate.d.ts +13 -0
  185. package/dist/modules/workspace/validate.js +333 -0
  186. package/dist/orchestrator.d.ts +13 -0
  187. package/dist/orchestrator.js +727 -0
  188. package/dist/providers/default-translation-provider.d.ts +20 -0
  189. package/dist/providers/default-translation-provider.js +30 -0
  190. package/dist/providers/index.d.ts +2 -0
  191. package/dist/providers/index.js +21 -0
  192. package/dist/providers/types.d.ts +153 -0
  193. package/dist/providers/types.js +2 -0
  194. package/dist/qa/check-project.d.ts +50 -0
  195. package/dist/qa/check-project.js +193 -0
  196. package/dist/qa/gettext-files.d.ts +15 -0
  197. package/dist/qa/gettext-files.js +115 -0
  198. package/dist/qa/locale-files.d.ts +44 -0
  199. package/dist/qa/locale-files.js +159 -0
  200. package/dist/qa/locale-qa.d.ts +61 -0
  201. package/dist/qa/locale-qa.js +342 -0
  202. package/dist/qa/review.d.ts +43 -0
  203. package/dist/qa/review.js +175 -0
  204. package/dist/runtime/config.d.ts +8 -0
  205. package/dist/runtime/config.js +37 -0
  206. package/dist/runtime/index.d.ts +3 -0
  207. package/dist/runtime/index.js +8 -0
  208. package/dist/runtime/types.d.ts +427 -0
  209. package/dist/runtime/types.js +38 -0
  210. package/dist/types.d.ts +134 -0
  211. package/dist/types.js +12 -0
  212. package/dist/utils/ast-helpers.d.ts +57 -0
  213. package/dist/utils/ast-helpers.js +608 -0
  214. package/dist/utils/cache-keys.d.ts +8 -0
  215. package/dist/utils/cache-keys.js +18 -0
  216. package/dist/utils/circuit-breaker.d.ts +43 -0
  217. package/dist/utils/circuit-breaker.js +119 -0
  218. package/dist/utils/concurrent-map.d.ts +4 -0
  219. package/dist/utils/concurrent-map.js +21 -0
  220. package/dist/utils/detect-format.d.ts +5 -0
  221. package/dist/utils/detect-format.js +125 -0
  222. package/dist/utils/extraction-options.d.ts +29 -0
  223. package/dist/utils/extraction-options.js +33 -0
  224. package/dist/utils/file-utils.d.ts +33 -0
  225. package/dist/utils/file-utils.js +267 -0
  226. package/dist/utils/glossary-match.d.ts +9 -0
  227. package/dist/utils/glossary-match.js +25 -0
  228. package/dist/utils/html-extractor.d.ts +8 -0
  229. package/dist/utils/html-extractor.js +317 -0
  230. package/dist/utils/i18next-icu.d.ts +38 -0
  231. package/dist/utils/i18next-icu.js +159 -0
  232. package/dist/utils/icu-message.d.ts +65 -0
  233. package/dist/utils/icu-message.js +346 -0
  234. package/dist/utils/icu-spans.d.ts +10 -0
  235. package/dist/utils/icu-spans.js +126 -0
  236. package/dist/utils/jsx-message.d.ts +93 -0
  237. package/dist/utils/jsx-message.js +628 -0
  238. package/dist/utils/key-usage.d.ts +35 -0
  239. package/dist/utils/key-usage.js +247 -0
  240. package/dist/utils/logger.d.ts +24 -0
  241. package/dist/utils/logger.js +33 -0
  242. package/dist/utils/metadata.d.ts +82 -0
  243. package/dist/utils/metadata.js +261 -0
  244. package/dist/utils/module-constants.d.ts +30 -0
  245. package/dist/utils/module-constants.js +265 -0
  246. package/dist/utils/not-injected.d.ts +6 -0
  247. package/dist/utils/not-injected.js +38 -0
  248. package/dist/utils/placeholder-protection.d.ts +74 -0
  249. package/dist/utils/placeholder-protection.js +181 -0
  250. package/dist/utils/pseudo-markers.d.ts +9 -0
  251. package/dist/utils/pseudo-markers.js +16 -0
  252. package/dist/utils/string-utils.d.ts +52 -0
  253. package/dist/utils/string-utils.js +270 -0
  254. package/dist/utils/translation-quality.d.ts +34 -0
  255. package/dist/utils/translation-quality.js +156 -0
  256. package/dist/utils/translation-sanitizer.d.ts +11 -0
  257. package/dist/utils/translation-sanitizer.js +71 -0
  258. package/dist/utils/translator-caller.d.ts +32 -0
  259. package/dist/utils/translator-caller.js +217 -0
  260. package/dist/utils/translator-load-balancer.d.ts +73 -0
  261. package/dist/utils/translator-load-balancer.js +297 -0
  262. package/dist/utils/ui-copy.d.ts +32 -0
  263. package/dist/utils/ui-copy.js +126 -0
  264. package/dist/utils/untranslated.d.ts +15 -0
  265. package/dist/utils/untranslated.js +34 -0
  266. package/dist/utils/workspace.d.ts +14 -0
  267. package/dist/utils/workspace.js +89 -0
  268. package/package.json +64 -4
  269. package/schemas/translate-report.schema.json +297 -0
  270. 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,138 @@
1
+ /**
2
+ * ICU plural/select messages for plain-text machine translation (NLLB).
3
+ *
4
+ * NLLB translates sentences, not ICU syntax. So an ICU message is taken apart
5
+ * into the plain sentences it can render, each sentence is translated on its
6
+ * own, and the translations are put back together as an ICU message for the
7
+ * target language:
8
+ *
9
+ * - Text around a selector is moved into its branches, so every translated
10
+ * unit is a whole sentence ("You have {count, plural, one {# file} other
11
+ * {# files}}." → one {You have # file.} other {You have # files.}).
12
+ * - A plural gets the TARGET language's CLDR categories (Polish one/few/many/
13
+ * other, Japanese only other). Each category is translated from the source
14
+ * branch of the same name, or the source `other`; exact `=n` branches stay.
15
+ * - The count (`#`, `{count}`, or a gettext `%(count)d`) is replaced by a real
16
+ * number of that category (Polish few → 2, many → 5, Arabic many → 11), so
17
+ * the translator inflects the words around it. The same number must come
18
+ * back exactly where it can be turned back into the count; if it doesn't,
19
+ * that branch — and so the message — fails.
20
+ * - `select` branches keep their keys; nested selectors are expanded the same
21
+ * way. Anything that can't be expressed fails with a reason; a message is
22
+ * never put together from a partial set of branches.
23
+ *
24
+ * Infrastructure-free: the caller sends the units to its translator and hands
25
+ * the per-unit results back to `compose`.
26
+ */
27
+ import { type IcuNode } from '../../utils/icu-message';
28
+ import { type FixedTerm } from '../../utils/placeholder-protection';
29
+ import type { KeyFailure, KeyResult } from '../../providers/types';
30
+ /** Leaves (translated sentences) one message may expand to. */
31
+ export declare const MAX_BRANCH_UNITS = 64;
32
+ type Selector = Extract<IcuNode, {
33
+ options: unknown;
34
+ }>;
35
+ type SelectorType = Selector['type'];
36
+ /** A number standing in for a plural's value in one unit. */
37
+ interface NumberSlot {
38
+ /** As written in the text sent to the translator, e.g. "5" or "1.5". */
39
+ text: string;
40
+ /** Occurrences in the sent text; the translation must have exactly as many. */
41
+ count: number;
42
+ /** What each occurrence turns back into (a sentinel). */
43
+ restore: string;
44
+ /** For messages: "#", "{count}", "%(count)d". */
45
+ label: string;
46
+ /**
47
+ * The branch stands for this one number only (`=1`, German/Polish "one",
48
+ * Arabic "zero"/"two"): the translation may spell it out ("eine Datei")
49
+ * instead of showing it, and the branch is still exact.
50
+ */
51
+ mayBeSpelledOut: boolean;
52
+ }
53
+ export interface BranchUnit {
54
+ /** Unique within the plan. */
55
+ id: string;
56
+ /** Which branch combination, e.g. "count=few" ("message" with no selectors). */
57
+ label: string;
58
+ /** Text for the translator: protected spans as [#VAR_n#] tokens, counts as numbers. */
59
+ sendText: string;
60
+ tokens: string[];
61
+ /** What each token is restored to (a fixed glossary term becomes its translation). */
62
+ restore: string[];
63
+ slots: NumberSlot[];
64
+ /** Units with nothing to translate (only a number, tags, placeholders) are not sent. */
65
+ passthrough: boolean;
66
+ /** Formatted arguments of the unit, by sentinel index. */
67
+ args: string[];
68
+ /** Literal text restored verbatim (gettext count placeholders), by sentinel index. */
69
+ literals: string[];
70
+ /** The unit's parent selector is a plural: `#` and quoting follow plural rules. */
71
+ inPlural: boolean;
72
+ /** For a passthrough unit: the text with sentinels, used as is. */
73
+ sentinelText: string;
74
+ /** Spaces were put between a count and a token it touches (see makeUnit). */
75
+ spaced: boolean;
76
+ }
77
+ export type BranchFailure = {
78
+ ok: false;
79
+ reason: KeyFailure['reason'];
80
+ retryable: boolean;
81
+ message: string;
82
+ };
83
+ export type BranchComposeResult = {
84
+ ok: true;
85
+ value: string;
86
+ } | BranchFailure;
87
+ export interface BranchPlan {
88
+ units: BranchUnit[];
89
+ /**
90
+ * Put the message together from one translator result per unit (by unit
91
+ * id). `checkUnit` adds the caller's own per-branch checks (e.g. "came back
92
+ * untranslated"); it returns a problem message or null.
93
+ */
94
+ compose(results: Map<string, KeyResult | undefined>, checkUnit?: (unit: BranchUnit, translated: string) => BranchFailure | null): BranchComposeResult;
95
+ }
96
+ export interface PlanOptions {
97
+ neverTranslateTerms?: string[];
98
+ /** "Always translate as" terms: tokenized, and restored to their translation. */
99
+ fixedTerms?: FixedTerm[];
100
+ /**
101
+ * Literal text standing for the value of the plural it appears in, besides
102
+ * `#` — e.g. "%(count)d" in a gettext plural turned into ICU.
103
+ */
104
+ valuePlaceholder?: string;
105
+ /**
106
+ * Plain (non-ICU) source: wrap it in a plural over each of these arguments
107
+ * for the target language (target-side plurals for `{count} files`).
108
+ */
109
+ pluralizeArgs?: string[];
110
+ }
111
+ /** Plural categories of a locale, CLDR order. */
112
+ export declare function targetCategories(locale: string, type: 'cardinal' | 'ordinal'): string[];
113
+ /** Candidate numbers for a category (or the exact value of an `=n` branch). */
114
+ declare function numbersFor(locale: string, type: SelectorType, key: string): number[];
115
+ /**
116
+ * Plan the branch-by-branch translation of `source` into `targetLocale`, or
117
+ * explain why it can't be done.
118
+ */
119
+ export declare function planIcuBranches(source: string, targetLocale: string, options?: PlanOptions): BranchPlan | {
120
+ error: string;
121
+ };
122
+ /** "١٢" → "12", "1,5" → "1.5". */
123
+ declare function normalizeNumber(s: string): string;
124
+ /** Replace each slot's number in the translation by its sentinel; null when a number is missing or repeated. */
125
+ declare function restoreNumbers(translated: string, slots: NumberSlot[]): {
126
+ text: string;
127
+ } | {
128
+ problem: string;
129
+ };
130
+ /** ICU-escape literal text: apostrophes before syntax, and `{`, `}` (and `#` in a plural) quoted. */
131
+ export declare function escapeIcuText(text: string, inPlural: boolean): string;
132
+ /** Exposed for tests. */
133
+ export declare const __test: {
134
+ normalizeNumber: typeof normalizeNumber;
135
+ restoreNumbers: typeof restoreNumbers;
136
+ numbersFor: typeof numbersFor;
137
+ };
138
+ export {};