glotto 2.9.0 → 3.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 (151) hide show
  1. package/README.md +150 -54
  2. package/esm/cli.js +155 -43
  3. package/esm/deno.d.ts +5 -1
  4. package/esm/deno.js +18 -7
  5. package/esm/src/config.d.ts +4 -0
  6. package/esm/src/config.d.ts.map +1 -0
  7. package/esm/src/config.js +95 -0
  8. package/esm/src/contants.d.ts +6 -2
  9. package/esm/src/contants.d.ts.map +1 -1
  10. package/esm/src/contants.js +34 -15
  11. package/esm/src/diff.d.ts +4 -0
  12. package/esm/src/diff.d.ts.map +1 -0
  13. package/esm/src/diff.js +53 -0
  14. package/esm/src/file.d.ts +5 -9
  15. package/esm/src/file.d.ts.map +1 -1
  16. package/esm/src/file.js +14 -103
  17. package/esm/src/providers/anthropic.d.ts +6 -11
  18. package/esm/src/providers/anthropic.d.ts.map +1 -1
  19. package/esm/src/providers/anthropic.js +21 -107
  20. package/esm/src/providers/gemini.d.ts +6 -11
  21. package/esm/src/providers/gemini.d.ts.map +1 -1
  22. package/esm/src/providers/gemini.js +20 -113
  23. package/esm/src/providers/openai.d.ts +6 -11
  24. package/esm/src/providers/openai.d.ts.map +1 -1
  25. package/esm/src/providers/openai.js +17 -108
  26. package/esm/src/translator.d.ts +15 -0
  27. package/esm/src/translator.d.ts.map +1 -0
  28. package/esm/src/translator.js +284 -0
  29. package/esm/src/types.d.ts +58 -13
  30. package/esm/src/types.d.ts.map +1 -1
  31. package/esm/src/utilites.d.ts +3 -10
  32. package/esm/src/utilites.d.ts.map +1 -1
  33. package/esm/src/utilites.js +41 -131
  34. package/package.json +20 -6
  35. package/schema/glotto.schema.json +87 -0
  36. package/script/cli.js +153 -41
  37. package/script/deno.d.ts +5 -1
  38. package/script/deno.js +18 -7
  39. package/script/src/config.d.ts +4 -0
  40. package/script/src/config.d.ts.map +1 -0
  41. package/script/src/config.js +132 -0
  42. package/script/src/contants.d.ts +6 -2
  43. package/script/src/contants.d.ts.map +1 -1
  44. package/script/src/contants.js +35 -16
  45. package/script/src/diff.d.ts +4 -0
  46. package/script/src/diff.d.ts.map +1 -0
  47. package/script/src/diff.js +57 -0
  48. package/script/src/file.d.ts +5 -9
  49. package/script/src/file.d.ts.map +1 -1
  50. package/script/src/file.js +19 -113
  51. package/script/src/providers/anthropic.d.ts +6 -11
  52. package/script/src/providers/anthropic.d.ts.map +1 -1
  53. package/script/src/providers/anthropic.js +20 -106
  54. package/script/src/providers/gemini.d.ts +6 -11
  55. package/script/src/providers/gemini.d.ts.map +1 -1
  56. package/script/src/providers/gemini.js +19 -112
  57. package/script/src/providers/openai.d.ts +6 -11
  58. package/script/src/providers/openai.d.ts.map +1 -1
  59. package/script/src/providers/openai.js +16 -107
  60. package/script/src/translator.d.ts +15 -0
  61. package/script/src/translator.d.ts.map +1 -0
  62. package/script/src/translator.js +294 -0
  63. package/script/src/types.d.ts +58 -13
  64. package/script/src/types.d.ts.map +1 -1
  65. package/script/src/utilites.d.ts +3 -10
  66. package/script/src/utilites.d.ts.map +1 -1
  67. package/script/src/utilites.js +44 -138
  68. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  69. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  70. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
  71. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts +0 -35
  72. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts.map +0 -1
  73. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common32.js +0 -192
  74. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts +0 -35
  75. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts.map +0 -1
  76. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common64.js +0 -113
  77. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  78. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  79. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
  80. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  81. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  82. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
  83. package/esm/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts +0 -2
  84. package/esm/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts.map +0 -1
  85. package/esm/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.js +0 -26
  86. package/esm/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts +0 -61
  87. package/esm/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts.map +0 -1
  88. package/esm/deps/jsr.io/@std/encoding/1.0.10/ascii85.js +0 -152
  89. package/esm/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts +0 -40
  90. package/esm/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts.map +0 -1
  91. package/esm/deps/jsr.io/@std/encoding/1.0.10/base32.js +0 -87
  92. package/esm/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts +0 -40
  93. package/esm/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts.map +0 -1
  94. package/esm/deps/jsr.io/@std/encoding/1.0.10/base58.js +0 -131
  95. package/esm/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts +0 -40
  96. package/esm/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts.map +0 -1
  97. package/esm/deps/jsr.io/@std/encoding/1.0.10/base64.js +0 -82
  98. package/esm/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts +0 -40
  99. package/esm/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts.map +0 -1
  100. package/esm/deps/jsr.io/@std/encoding/1.0.10/base64url.js +0 -72
  101. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  102. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  103. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
  104. package/esm/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts +0 -98
  105. package/esm/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts.map +0 -1
  106. package/esm/deps/jsr.io/@std/encoding/1.0.10/mod.js +0 -99
  107. package/esm/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts +0 -120
  108. package/esm/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts.map +0 -1
  109. package/esm/deps/jsr.io/@std/encoding/1.0.10/varint.js +0 -205
  110. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  111. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  112. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -57
  113. package/script/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts +0 -35
  114. package/script/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts.map +0 -1
  115. package/script/deps/jsr.io/@std/encoding/1.0.10/_common32.js +0 -198
  116. package/script/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts +0 -35
  117. package/script/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts.map +0 -1
  118. package/script/deps/jsr.io/@std/encoding/1.0.10/_common64.js +0 -119
  119. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  120. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  121. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -16
  122. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  123. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  124. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -3
  125. package/script/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts +0 -2
  126. package/script/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts.map +0 -1
  127. package/script/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.js +0 -29
  128. package/script/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts +0 -61
  129. package/script/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts.map +0 -1
  130. package/script/deps/jsr.io/@std/encoding/1.0.10/ascii85.js +0 -156
  131. package/script/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts +0 -40
  132. package/script/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts.map +0 -1
  133. package/script/deps/jsr.io/@std/encoding/1.0.10/base32.js +0 -91
  134. package/script/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts +0 -40
  135. package/script/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts.map +0 -1
  136. package/script/deps/jsr.io/@std/encoding/1.0.10/base58.js +0 -135
  137. package/script/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts +0 -40
  138. package/script/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts.map +0 -1
  139. package/script/deps/jsr.io/@std/encoding/1.0.10/base64.js +0 -86
  140. package/script/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts +0 -40
  141. package/script/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts.map +0 -1
  142. package/script/deps/jsr.io/@std/encoding/1.0.10/base64url.js +0 -76
  143. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  144. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  145. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -91
  146. package/script/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts +0 -98
  147. package/script/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts.map +0 -1
  148. package/script/deps/jsr.io/@std/encoding/1.0.10/mod.js +0 -115
  149. package/script/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts +0 -120
  150. package/script/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts.map +0 -1
  151. package/script/deps/jsr.io/@std/encoding/1.0.10/varint.js +0 -211
@@ -0,0 +1,95 @@
1
+ import * as dntShim from "../_dnt.shims.js";
2
+ import { join } from '../deps/jsr.io/@std/path/1.1.4/mod.js';
3
+ import { CONFIG_FILE_NAME } from './contants.js';
4
+ async function readConfigFile(path) {
5
+ let raw;
6
+ try {
7
+ raw = await dntShim.Deno.readTextFile(path);
8
+ }
9
+ catch (error) {
10
+ if (error instanceof dntShim.Deno.errors.NotFound) {
11
+ return null;
12
+ }
13
+ throw error;
14
+ }
15
+ if (raw.trim() === '') {
16
+ return {};
17
+ }
18
+ const parsed = JSON.parse(raw);
19
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
20
+ throw new Error(`Config file must contain a JSON object: ${path}`);
21
+ }
22
+ return parsed;
23
+ }
24
+ export async function loadConfig(explicitPath) {
25
+ if (explicitPath) {
26
+ const config = await readConfigFile(explicitPath);
27
+ if (config === null) {
28
+ throw new Error(`Config file not found: ${explicitPath}`);
29
+ }
30
+ return config;
31
+ }
32
+ const defaultPath = join(dntShim.Deno.cwd(), CONFIG_FILE_NAME);
33
+ const config = await readConfigFile(defaultPath);
34
+ if (config === null) {
35
+ return {};
36
+ }
37
+ return config;
38
+ }
39
+ function joinList(value) {
40
+ if (value === undefined) {
41
+ return undefined;
42
+ }
43
+ if (Array.isArray(value)) {
44
+ return value.join(',');
45
+ }
46
+ return value;
47
+ }
48
+ function rawArgsHas(rawArgs, flag) {
49
+ const long = `--${flag}`;
50
+ const longEq = `--${flag}=`;
51
+ return rawArgs.some((a) => a === long || a.startsWith(longEq));
52
+ }
53
+ export function applyConfig(cli, config, rawArgs) {
54
+ const merged = { ...cli };
55
+ if (merged.key === undefined && config.key !== undefined) {
56
+ merged.key = config.key;
57
+ }
58
+ if (merged.provider === undefined && config.provider !== undefined) {
59
+ merged.provider = config.provider;
60
+ }
61
+ if (merged.model === undefined && config.model !== undefined) {
62
+ merged.model = config.model;
63
+ }
64
+ if (merged.input === undefined && config.input !== undefined) {
65
+ merged.input = config.input;
66
+ }
67
+ if (merged.output === undefined && config.output !== undefined) {
68
+ merged.output = joinList(config.output);
69
+ }
70
+ if (merged.from === undefined && config.from !== undefined) {
71
+ merged.from = config.from;
72
+ }
73
+ if (merged.to === undefined && config.to !== undefined) {
74
+ merged.to = joinList(config.to);
75
+ }
76
+ if (merged.url === undefined && config.url !== undefined) {
77
+ merged.url = config.url;
78
+ }
79
+ if (merged['max-batch-size'] === undefined && config.maxBatchSize !== undefined) {
80
+ merged['max-batch-size'] = String(config.maxBatchSize);
81
+ }
82
+ if (!rawArgsHas(rawArgs, 'no-limit') && config.noLimit !== undefined) {
83
+ merged['no-limit'] = config.noLimit;
84
+ }
85
+ if (!rawArgsHas(rawArgs, 'no-timeout') && config.noTimeout !== undefined) {
86
+ merged['no-timeout'] = config.noTimeout;
87
+ }
88
+ if (!rawArgsHas(rawArgs, 'stats') && config.stats !== undefined) {
89
+ merged.stats = config.stats;
90
+ }
91
+ if (!rawArgsHas(rawArgs, 'incremental') && config.incremental !== undefined) {
92
+ merged.incremental = config.incremental;
93
+ }
94
+ return merged;
95
+ }
@@ -1,9 +1,13 @@
1
1
  import type { Provider } from './types.js';
2
2
  export declare const DEFAULT_PROVIDER: Provider;
3
3
  export declare const DEFAULT_MODELS: Record<Provider, string>;
4
- export declare const DEFAULT_MAX_CHUNK_BYTES: 30000;
4
+ export declare const DEFAULT_MAX_BATCH_BYTES: 12000;
5
5
  export declare const MAX_RETRIES: 3;
6
6
  export declare const BASE_RETRY_DELAY_MS: 2000;
7
- export declare const INTER_CHUNK_DELAY_MS: 1500;
7
+ export declare const INTER_BATCH_DELAY_MS: 1500;
8
+ export declare const INTER_LEAF_DELAY_MS: 200;
9
+ export declare const PER_LEAF_FALLBACK_RATIO: 0.5;
10
+ export declare const GITHUB_REPO_URL: "https://github.com/ibodev1/glotto";
11
+ export declare const CONFIG_FILE_NAME: "glotto.config.json";
8
12
  export declare const HELP_TEXT: string;
9
13
  //# sourceMappingURL=contants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"contants.d.ts","sourceRoot":"","sources":["../../src/src/contants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,eAAO,MAAM,gBAAgB,EAAE,QAAmB,CAAC;AAEnD,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAInD,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAG,KAAe,CAAC;AAEvD,eAAO,MAAM,WAAW,EAAG,CAAU,CAAC;AAEtC,eAAO,MAAM,mBAAmB,EAAG,IAAc,CAAC;AAElD,eAAO,MAAM,oBAAoB,EAAG,IAAc,CAAC;AAEnD,eAAO,MAAM,SAAS,QAsBrB,CAAC"}
1
+ {"version":3,"file":"contants.d.ts","sourceRoot":"","sources":["../../src/src/contants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,eAAO,MAAM,gBAAgB,EAAE,QAAmB,CAAC;AAEnD,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAInD,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAG,KAAe,CAAC;AAEvD,eAAO,MAAM,WAAW,EAAG,CAAU,CAAC;AAEtC,eAAO,MAAM,mBAAmB,EAAG,IAAc,CAAC;AAElD,eAAO,MAAM,oBAAoB,EAAG,IAAc,CAAC;AAEnD,eAAO,MAAM,mBAAmB,EAAG,GAAY,CAAC;AAEhD,eAAO,MAAM,uBAAuB,EAAG,GAAY,CAAC;AAEpD,eAAO,MAAM,eAAe,EAAG,mCAA4C,CAAC;AAE5E,eAAO,MAAM,gBAAgB,EAAG,oBAA6B,CAAC;AAE9D,eAAO,MAAM,SAAS,QAqCrB,CAAC"}
@@ -1,33 +1,52 @@
1
- export const DEFAULT_PROVIDER = 'gemini';
1
+ export const DEFAULT_PROVIDER = 'openai';
2
2
  export const DEFAULT_MODELS = {
3
3
  gemini: 'gemini-2.5-flash',
4
4
  openai: 'gpt-4.1-mini',
5
5
  anthropic: 'claude-3-5-haiku-latest',
6
6
  };
7
- export const DEFAULT_MAX_CHUNK_BYTES = 30_000;
7
+ export const DEFAULT_MAX_BATCH_BYTES = 12_000;
8
8
  export const MAX_RETRIES = 3;
9
9
  export const BASE_RETRY_DELAY_MS = 2_000;
10
- export const INTER_CHUNK_DELAY_MS = 1_500;
10
+ export const INTER_BATCH_DELAY_MS = 1_500;
11
+ export const INTER_LEAF_DELAY_MS = 200;
12
+ export const PER_LEAF_FALLBACK_RATIO = 0.5;
13
+ export const GITHUB_REPO_URL = 'https://github.com/ibodev1/glotto';
14
+ export const CONFIG_FILE_NAME = 'glotto.config.json';
11
15
  export const HELP_TEXT = `
12
16
  Glotto AI Translator
13
17
  -------------------
14
18
  A tool for translating i18n JSON files using AI services.
15
19
 
20
+ Glotto walks the input JSON, extracts every string leaf with its path, sends them to the
21
+ chosen provider as plain-text batches (using ≪id≫value≪/id≫ tagged entries), and
22
+ reconstructs the JSON from the responses. JSON structure, keys, variables and HTML tags
23
+ are preserved by the tool itself — the model only sees and produces text.
24
+
16
25
  Options:
17
- --key API key for the AI service (required)
18
- -p, --provider AI translation provider to use (default: ${DEFAULT_PROVIDER})
19
- -m, --model Model name for the selected provider (optional)
20
- -i, --input Path to source JSON file (required)
21
- -o, --output Path to target JSON file (required)
22
- -f, --from Source language (required)
23
- -t, --to Target language (required)
24
- --url Custom base URL for OpenAI/Anthropic (optional)
25
- -h, --help Display this help message
26
- -v, --version Display version
26
+ --key API key for the AI service (required)
27
+ -p, --provider AI translation provider to use (default: ${DEFAULT_PROVIDER})
28
+ -m, --model Model name for the selected provider (optional)
29
+ -i, --input Path to source JSON file (required)
30
+ -o, --output Target JSON file path. Comma-separated for multi-target (required)
31
+ -f, --from Source language (required)
32
+ -t, --to Target language. Comma-separated for multi-target (required)
33
+ --url Custom base URL for OpenAI/Anthropic (optional)
34
+ --config Path to a glotto.config.json (default: ./glotto.config.json if present)
35
+ --stats Print AI usage stats (input/output tokens, calls) at the end
36
+ --incremental Translate only missing/empty keys when the target file already exists
37
+ --no-limit Disable rate limit delay between batches
38
+ --no-timeout Disable request timeout (wait indefinitely for AI response)
39
+ --max-batch-size Maximum source size per batch, in KB (default: ${DEFAULT_MAX_BATCH_BYTES / 1024} KB)
40
+ -h, --help Display this help message
41
+ -v, --version Display version
27
42
 
28
43
  Examples:
29
44
  glotto --key {{key}} --input=en.json --output=tr.json --from=english --to=turkish
30
45
  glotto --key {{key}} -i en.json -o tr.json -f english -t turkish -p gemini
31
- glotto --key {{key}} -i en.json -o tr.json -f english -t turkish -p openai
32
- glotto --key {{key}} -i en.json -o tr.json -f english -t turkish -p anthropic
46
+ glotto --key {{key}} -i en.json -f english -t "turkish,french" -o "tr.json,fr.json"
47
+ glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --incremental
48
+ glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --stats
49
+ glotto --key {{key}} --config ./glotto.config.json
50
+ glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --no-limit --no-timeout
51
+ glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --max-batch-size 8
33
52
  `;
@@ -0,0 +1,4 @@
1
+ import type { JsonValue, Leaf } from './types.js';
2
+ export declare function findMissingLeaves(sourceLeaves: Leaf[], existingTarget: JsonValue): Leaf[];
3
+ export declare function mergeTranslations(existingTarget: JsonValue, sourceLeaves: Leaf[], translations: Map<number, string>): JsonValue;
4
+ //# sourceMappingURL=diff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../src/src/diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAQ,MAAM,YAAY,CAAC;AAyBxD,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,SAAS,GAAG,IAAI,EAAE,CAYzF;AASD,wBAAgB,iBAAiB,CAC/B,cAAc,EAAE,SAAS,EACzB,YAAY,EAAE,IAAI,EAAE,EACpB,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,SAAS,CAYX"}
@@ -0,0 +1,53 @@
1
+ import { setPath } from './translator.js';
2
+ function getPath(root, path) {
3
+ let node = root;
4
+ for (const key of path) {
5
+ if (node === null || typeof node !== 'object') {
6
+ return undefined;
7
+ }
8
+ const container = node;
9
+ if (!(key in container)) {
10
+ return undefined;
11
+ }
12
+ node = container[key];
13
+ }
14
+ return node;
15
+ }
16
+ function isPresentTranslation(value) {
17
+ if (typeof value !== 'string') {
18
+ return false;
19
+ }
20
+ return value.trim().length > 0;
21
+ }
22
+ export function findMissingLeaves(sourceLeaves, existingTarget) {
23
+ const missing = [];
24
+ for (const leaf of sourceLeaves) {
25
+ if (!leaf.translatable) {
26
+ continue;
27
+ }
28
+ const existingValue = getPath(existingTarget, leaf.path);
29
+ if (!isPresentTranslation(existingValue)) {
30
+ missing.push(leaf);
31
+ }
32
+ }
33
+ return missing;
34
+ }
35
+ function deepClone(value) {
36
+ if (value === null || typeof value !== 'object') {
37
+ return value;
38
+ }
39
+ return JSON.parse(JSON.stringify(value));
40
+ }
41
+ export function mergeTranslations(existingTarget, sourceLeaves, translations) {
42
+ const clone = deepClone(existingTarget);
43
+ for (const leaf of sourceLeaves) {
44
+ if (!translations.has(leaf.id)) {
45
+ continue;
46
+ }
47
+ if (leaf.path.length === 0) {
48
+ return translations.get(leaf.id);
49
+ }
50
+ setPath(clone, leaf.path, translations.get(leaf.id));
51
+ }
52
+ return clone;
53
+ }
package/esm/src/file.d.ts CHANGED
@@ -1,10 +1,6 @@
1
- import type { ChunkInfo, JsonObject } from './types.js';
2
- export declare const resolvePath: (...paths: string[]) => string;
3
- export declare const getImportJson: <T = JsonObject>(input: string) => Promise<T>;
4
- export declare const writeOutput: (outputPath: string, content: string) => Promise<void>;
5
- export declare const existsFile: (path: string) => Promise<boolean>;
6
- export declare const ensureDirectoryExists: (directory: string) => Promise<void>;
7
- export declare const splitJson: (data: JsonObject, maxChunkBytes?: number) => ChunkInfo[];
8
- export declare const mergeInputs: (inputs: JsonObject[]) => JsonObject;
9
- export declare const writeTemp: (targetLanguage: string, tempJsonFileName: string, content: string) => Promise<void>;
1
+ import type { JsonValue } from './types.js';
2
+ export declare function resolvePath(...paths: string[]): string;
3
+ export declare function getImportJson<T = JsonValue>(input: string): Promise<T>;
4
+ export declare function writeOutput(outputPath: string, content: string): Promise<void>;
5
+ export declare function readJsonIfExists<T = JsonValue>(path: string): Promise<T | null>;
10
6
  //# sourceMappingURL=file.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/src/file.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGxD,eAAO,MAAM,WAAW,GAAI,GAAG,OAAO,MAAM,EAAE,WAE7C,CAAC;AAEF,eAAO,MAAM,aAAa,GAAU,CAAC,GAAG,UAAU,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,CAAC,CAS5E,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,YAAY,MAAM,EAAE,SAAS,MAAM,kBAEpE,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,MAAM,MAAM,qBAW5C,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,IAAI,CAK3E,CAAC;AAeF,eAAO,MAAM,SAAS,GAAI,MAAM,UAAU,EAAE,gBAAe,MAAe,KAAG,SAAS,EA4DrF,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,QAAQ,UAAU,EAAE,KAAG,UAElD,CAAC;AAkBF,eAAO,MAAM,SAAS,GAAU,gBAAgB,MAAM,EAAE,kBAAkB,MAAM,EAAE,SAAS,MAAM,kBAKhG,CAAC"}
1
+ {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/src/file.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,wBAAgB,WAAW,CAAC,GAAG,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAEtD;AAED,wBAAsB,aAAa,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAO5E;AAED,wBAAsB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEpF;AAED,wBAAsB,gBAAgB,CAAC,CAAC,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAcrF"}
package/esm/src/file.js CHANGED
@@ -1,121 +1,32 @@
1
1
  import * as dntShim from "../_dnt.shims.js";
2
2
  import { join } from '../deps/jsr.io/@std/path/1.1.4/mod.js';
3
- import { logger } from './logger.js';
4
- export const resolvePath = (...paths) => {
3
+ export function resolvePath(...paths) {
5
4
  return join(dntShim.Deno.cwd(), ...paths);
6
- };
7
- export const getImportJson = async (input) => {
5
+ }
6
+ export async function getImportJson(input) {
8
7
  const filePath = resolvePath(input);
9
8
  const fileContent = await dntShim.Deno.readTextFile(filePath);
10
9
  if (typeof fileContent !== 'string' || fileContent.trim() === '') {
11
10
  throw new Error('No Content!');
12
11
  }
13
12
  return JSON.parse(fileContent);
14
- };
15
- export const writeOutput = async (outputPath, content) => {
13
+ }
14
+ export async function writeOutput(outputPath, content) {
16
15
  await dntShim.Deno.writeTextFile(outputPath, content, { create: true });
17
- };
18
- export const existsFile = async (path) => {
16
+ }
17
+ export async function readJsonIfExists(path) {
18
+ let raw;
19
19
  try {
20
- await dntShim.Deno.stat(path);
21
- return true;
20
+ raw = await dntShim.Deno.readTextFile(path);
22
21
  }
23
22
  catch (error) {
24
23
  if (error instanceof dntShim.Deno.errors.NotFound) {
25
- return false;
24
+ return null;
26
25
  }
27
26
  throw error;
28
27
  }
29
- };
30
- export const ensureDirectoryExists = async (directory) => {
31
- const directoryExists = await existsFile(directory);
32
- if (!directoryExists) {
33
- await dntShim.Deno.mkdir(directory, { recursive: true });
28
+ if (raw.trim() === '') {
29
+ return null;
34
30
  }
35
- };
36
- const getByteSize = (str) => {
37
- return new TextEncoder().encode(str).byteLength;
38
- };
39
- const buildChunkWithSize = (data, keys) => {
40
- const chunk = Object.create(null);
41
- for (const key of keys) {
42
- chunk[key] = data[key];
43
- }
44
- const json = JSON.stringify(chunk);
45
- return { json, size: getByteSize(json) };
46
- };
47
- export const splitJson = (data, maxChunkBytes = 30_000) => {
48
- const encoder = new TextEncoder();
49
- const dataKeys = Object.keys(data);
50
- const totalKeys = dataKeys.length;
51
- const fullJson = JSON.stringify(data);
52
- const totalBytes = getByteSize(fullJson);
53
- if (totalBytes <= maxChunkBytes) {
54
- return [
55
- {
56
- data: encoder.encode(fullJson),
57
- keyCount: totalKeys,
58
- byteSize: totalBytes,
59
- index: 0,
60
- },
61
- ];
62
- }
63
- const chunks = [];
64
- let currentKeys = [];
65
- let currentSize = 2;
66
- for (let i = 0; i < totalKeys; i++) {
67
- const key = dataKeys[i];
68
- const keyValueJson = JSON.stringify({ [key]: data[key] });
69
- const entrySize = getByteSize(keyValueJson) - 2;
70
- const separatorSize = currentKeys.length > 0 ? 1 : 0;
71
- const projectedSize = currentSize + entrySize + separatorSize;
72
- if (projectedSize > maxChunkBytes && currentKeys.length > 0) {
73
- const { json, size } = buildChunkWithSize(data, currentKeys);
74
- chunks.push({
75
- data: encoder.encode(json),
76
- keyCount: currentKeys.length,
77
- byteSize: size,
78
- index: chunks.length,
79
- });
80
- currentKeys = [key];
81
- currentSize = 2 + entrySize;
82
- }
83
- else {
84
- currentKeys.push(key);
85
- currentSize = projectedSize;
86
- }
87
- }
88
- if (currentKeys.length > 0) {
89
- const { json, size } = buildChunkWithSize(data, currentKeys);
90
- chunks.push({
91
- data: encoder.encode(json),
92
- keyCount: currentKeys.length,
93
- byteSize: size,
94
- index: chunks.length,
95
- });
96
- }
97
- return chunks;
98
- };
99
- export const mergeInputs = (inputs) => {
100
- return Object.assign({}, ...inputs);
101
- };
102
- const getOsTempDir = () => {
103
- if (dntShim.Deno.build.os === 'windows') {
104
- return dntShim.Deno.env.get('TEMP') ?? dntShim.Deno.env.get('TMP') ?? 'C:\\Windows\\Temp';
105
- }
106
- return dntShim.Deno.env.get('TMPDIR') ?? '/tmp';
107
- };
108
- const resolveTempDir = async (targetLanguage) => {
109
- const tempDir = getOsTempDir();
110
- const glottoTempDir = join(tempDir, 'glotto');
111
- await ensureDirectoryExists(glottoTempDir);
112
- const languageTempDir = join(glottoTempDir, targetLanguage);
113
- await ensureDirectoryExists(languageTempDir);
114
- return languageTempDir;
115
- };
116
- export const writeTemp = async (targetLanguage, tempJsonFileName, content) => {
117
- const tempDir = await resolveTempDir(targetLanguage);
118
- const tempJsonFilePath = join(tempDir, tempJsonFileName);
119
- await dntShim.Deno.writeTextFile(tempJsonFilePath, content, { create: true });
120
- logger.info(`Temp saved: ${tempJsonFilePath}`);
121
- };
31
+ return JSON.parse(raw);
32
+ }
@@ -1,14 +1,9 @@
1
- import Anthropic from '@anthropic-ai/sdk';
2
- import type { ChunkInfo, Translatable } from '../types.js';
3
- declare class AnthropicModel implements Translatable {
4
- chunks: ChunkInfo[];
5
- from: string;
6
- to: string;
7
- client: Anthropic;
8
- model: string;
9
- constructor(key: string, chunks: ChunkInfo[], from: string, to: string, baseUrl?: string, modelName?: string);
10
- private translateChunk;
11
- translate(): Promise<string>;
1
+ import type { TextTranslator, TranslateOptions, TranslateResult } from '../types.js';
2
+ declare class AnthropicModel implements TextTranslator {
3
+ private client;
4
+ private model;
5
+ constructor(key: string, baseUrl?: string, modelName?: string, options?: TranslateOptions);
6
+ translate(prompt: string): Promise<TranslateResult>;
12
7
  }
13
8
  export default AnthropicModel;
14
9
  //# sourceMappingURL=anthropic.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAE,SAAS,EAAc,YAAY,EAAE,MAAM,aAAa,CAAC;AAQvE,cAAM,cAAe,YAAW,YAAY;IAC1C,MAAM,EAAE,SAAS,EAAE,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,SAAS,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;gBAEF,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;YAW9F,cAAc;IAkEtB,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC;CAqCnC;AAED,eAAe,cAAc,CAAC"}
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,aAAa,CAAC;AAGrG,cAAM,cAAe,YAAW,cAAc;IAC5C,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,KAAK,CAAS;gBAGpB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,GAAE,gBAAuD;IAU5D,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAoB1D;AAED,eAAe,cAAc,CAAC"}
@@ -1,29 +1,7 @@
1
1
  import Anthropic from '@anthropic-ai/sdk';
2
- import { delay, formatBytes, generatePrompts, isValidJson, stripJsonMarkdown } from '../utilites.js';
3
- import { logger } from '../logger.js';
4
- import { mergeInputs, writeTemp } from '../file.js';
5
- import { BASE_RETRY_DELAY_MS, DEFAULT_MODELS, INTER_CHUNK_DELAY_MS, MAX_RETRIES } from '../contants.js';
6
- const decoder = new TextDecoder();
2
+ import { DEFAULT_MODELS } from '../contants.js';
7
3
  class AnthropicModel {
8
- constructor(key, chunks, from, to, baseUrl, modelName) {
9
- Object.defineProperty(this, "chunks", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- Object.defineProperty(this, "from", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- Object.defineProperty(this, "to", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
4
+ constructor(key, baseUrl, modelName, options = { noLimit: false, noTimeout: false }) {
27
5
  Object.defineProperty(this, "client", {
28
6
  enumerable: true,
29
7
  configurable: true,
@@ -36,96 +14,32 @@ class AnthropicModel {
36
14
  writable: true,
37
15
  value: void 0
38
16
  });
39
- this.chunks = chunks;
40
- this.from = from;
41
- this.to = to;
42
17
  this.model = modelName ?? DEFAULT_MODELS.anthropic;
43
18
  this.client = new Anthropic({
44
19
  apiKey: key,
45
20
  baseURL: baseUrl,
21
+ timeout: options.noTimeout ? 0 : undefined,
46
22
  });
47
23
  }
48
- async translateChunk(chunk, systemPrompt, userPrompt) {
49
- const chunkLabel = `[Chunk ${chunk.index + 1}/${this.chunks.length}]`;
50
- const sourceJson = decoder.decode(chunk.data);
51
- for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
52
- try {
53
- if (attempt > 0) {
54
- const backoffMs = BASE_RETRY_DELAY_MS * Math.pow(2, attempt - 1);
55
- logger.warn(`${chunkLabel} Retry ${attempt}/${MAX_RETRIES} after ${backoffMs}ms backoff...`);
56
- await delay(backoffMs);
57
- }
58
- const message = await this.client.messages.create({
59
- model: this.model,
60
- max_tokens: 4096,
61
- messages: [
62
- {
63
- role: 'user',
64
- content: `${systemPrompt}\n\n${userPrompt}\n\nSOURCE_JSON:\n${sourceJson}`,
65
- },
66
- ],
67
- });
68
- const textParts = message.content
69
- .filter((part) => part.type === 'text')
70
- .map((part) => part.text);
71
- const text = textParts.join('').trim();
72
- if (!text) {
73
- logger.error(`${chunkLabel} Empty response from Anthropic`);
74
- continue;
75
- }
76
- const cleanedText = stripJsonMarkdown(text);
77
- const tempJsonFileName = `chunk_${chunk.index + 1}.json`;
78
- await writeTemp(this.to, tempJsonFileName, cleanedText);
79
- if (!isValidJson(cleanedText)) {
80
- if (attempt === MAX_RETRIES) {
81
- logger.error(`${chunkLabel} Failed after ${MAX_RETRIES + 1} attempts: Invalid JSON response`);
82
- return null;
83
- }
84
- logger.error(`${chunkLabel} Invalid JSON response, saved to ${tempJsonFileName}`);
85
- continue;
86
- }
87
- logger.info(`${chunkLabel} Translated successfully (${formatBytes(chunk.byteSize)}, ${chunk.keyCount} keys)`);
88
- return cleanedText;
89
- }
90
- catch (error) {
91
- const isLastAttempt = attempt === MAX_RETRIES;
92
- const errorMessage = error instanceof Error ? error.message : String(error);
93
- if (isLastAttempt) {
94
- logger.error(`${chunkLabel} Failed after ${MAX_RETRIES + 1} attempts: ${errorMessage}`);
95
- return null;
96
- }
97
- logger.warn(`${chunkLabel} Attempt ${attempt + 1} failed: ${errorMessage}`);
98
- }
99
- }
100
- return null;
101
- }
102
- async translate() {
103
- const { systemPrompt, userPrompt } = generatePrompts(this.from, this.to);
104
- const totalChunks = this.chunks.length;
105
- logger.info(`Starting translation: ${totalChunks} chunk(s) to process`);
106
- const results = [];
107
- const failedChunks = [];
108
- for (let i = 0; i < totalChunks; i++) {
109
- const chunk = this.chunks[i];
110
- if (i > 0) {
111
- logger.info(`Waiting ${INTER_CHUNK_DELAY_MS}ms before next chunk (rate limit protection)...`);
112
- await delay(INTER_CHUNK_DELAY_MS);
113
- }
114
- const result = await this.translateChunk(chunk, systemPrompt, userPrompt);
115
- if (result) {
116
- results.push(result);
117
- }
118
- else {
119
- failedChunks.push(chunk.index + 1);
120
- }
121
- }
122
- if (failedChunks.length > 0) {
123
- throw new Error(`Translation failed for chunk(s): ${failedChunks.join(', ')}. Check temp files for partial results.`);
24
+ async translate(prompt) {
25
+ const message = await this.client.messages.create({
26
+ model: this.model,
27
+ max_tokens: 8192,
28
+ temperature: 0.2,
29
+ messages: [{ role: 'user', content: prompt }],
30
+ });
31
+ const text = message.content
32
+ .filter((part) => part.type === 'text')
33
+ .map((part) => part.text)
34
+ .join('');
35
+ let usage;
36
+ if (message.usage) {
37
+ usage = {
38
+ inputTokens: message.usage.input_tokens ?? 0,
39
+ outputTokens: message.usage.output_tokens ?? 0,
40
+ };
124
41
  }
125
- logger.info(`All ${totalChunks} chunk(s) translated successfully, merging results...`);
126
- const jsonInputs = results.map((r) => JSON.parse(r));
127
- const mergedContent = mergeInputs(jsonInputs);
128
- return JSON.stringify(mergedContent, null, 2);
42
+ return { text, usage };
129
43
  }
130
44
  }
131
45
  export default AnthropicModel;
@@ -1,14 +1,9 @@
1
- import { GoogleGenAI } from '@google/genai';
2
- import type { ChunkInfo, Translatable } from '../types.js';
3
- declare class Gemini implements Translatable {
4
- chunks: ChunkInfo[];
5
- from: string;
6
- to: string;
7
- genAI: GoogleGenAI;
8
- model: string;
9
- constructor(key: string, chunks: ChunkInfo[], from: string, to: string, modelName?: string);
10
- private translateChunk;
11
- translate(): Promise<string>;
1
+ import type { TextTranslator, TranslateOptions, TranslateResult } from '../types.js';
2
+ declare class Gemini implements TextTranslator {
3
+ private genAI;
4
+ private model;
5
+ constructor(key: string, modelName?: string, options?: TranslateOptions);
6
+ translate(prompt: string): Promise<TranslateResult>;
12
7
  }
13
8
  export default Gemini;
14
9
  //# sourceMappingURL=gemini.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"gemini.d.ts","sourceRoot":"","sources":["../../../src/src/providers/gemini.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAa,MAAM,eAAe,CAAC;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAc,YAAY,EAAE,MAAM,aAAa,CAAC;AAMvE,cAAM,MAAO,YAAW,YAAY;IAClC,MAAM,EAAE,SAAS,EAAE,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,WAAW,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;gBAEF,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;YAU5E,cAAc;IAwEtB,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC;CAqCnC;AAED,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"gemini.d.ts","sourceRoot":"","sources":["../../../src/src/providers/gemini.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,aAAa,CAAC;AAGrG,cAAM,MAAO,YAAW,cAAc;IACpC,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,KAAK,CAAS;gBAGpB,GAAG,EAAE,MAAM,EACX,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,GAAE,gBAAuD;IAS5D,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAmB1D;AAED,eAAe,MAAM,CAAC"}