glotto 2.9.0 → 3.0.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.
- package/README.md +86 -55
- package/esm/cli.js +47 -39
- package/esm/deno.d.ts +0 -1
- package/esm/deno.js +1 -2
- package/esm/src/contants.d.ts +2 -2
- package/esm/src/contants.d.ts.map +1 -1
- package/esm/src/contants.js +22 -12
- package/esm/src/file.d.ts +2 -7
- package/esm/src/file.d.ts.map +1 -1
- package/esm/src/file.js +1 -108
- package/esm/src/providers/anthropic.d.ts +6 -11
- package/esm/src/providers/anthropic.d.ts.map +1 -1
- package/esm/src/providers/anthropic.js +14 -108
- package/esm/src/providers/gemini.d.ts +6 -11
- package/esm/src/providers/gemini.d.ts.map +1 -1
- package/esm/src/providers/gemini.js +13 -114
- package/esm/src/providers/openai.d.ts +6 -11
- package/esm/src/providers/openai.d.ts.map +1 -1
- package/esm/src/providers/openai.js +10 -109
- package/esm/src/translator.d.ts +8 -0
- package/esm/src/translator.d.ts.map +1 -0
- package/esm/src/translator.js +200 -0
- package/esm/src/types.d.ts +28 -11
- package/esm/src/types.d.ts.map +1 -1
- package/esm/src/utilites.d.ts +0 -6
- package/esm/src/utilites.d.ts.map +1 -1
- package/esm/src/utilites.js +18 -132
- package/package.json +1 -1
- package/script/cli.js +45 -37
- package/script/deno.d.ts +0 -1
- package/script/deno.js +1 -2
- package/script/src/contants.d.ts +2 -2
- package/script/src/contants.d.ts.map +1 -1
- package/script/src/contants.js +23 -13
- package/script/src/file.d.ts +2 -7
- package/script/src/file.d.ts.map +1 -1
- package/script/src/file.js +2 -114
- package/script/src/providers/anthropic.d.ts +6 -11
- package/script/src/providers/anthropic.d.ts.map +1 -1
- package/script/src/providers/anthropic.js +13 -107
- package/script/src/providers/gemini.d.ts +6 -11
- package/script/src/providers/gemini.d.ts.map +1 -1
- package/script/src/providers/gemini.js +12 -113
- package/script/src/providers/openai.d.ts +6 -11
- package/script/src/providers/openai.d.ts.map +1 -1
- package/script/src/providers/openai.js +9 -108
- package/script/src/translator.d.ts +8 -0
- package/script/src/translator.d.ts.map +1 -0
- package/script/src/translator.js +209 -0
- package/script/src/types.d.ts +28 -11
- package/script/src/types.d.ts.map +1 -1
- package/script/src/utilites.d.ts +0 -6
- package/script/src/utilites.d.ts.map +1 -1
- package/script/src/utilites.js +19 -136
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts +0 -35
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common32.js +0 -192
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts +0 -35
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common64.js +0 -113
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts +0 -2
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.js +0 -26
- package/esm/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts +0 -61
- package/esm/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/ascii85.js +0 -152
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts +0 -40
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base32.js +0 -87
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts +0 -40
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base58.js +0 -131
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts +0 -40
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base64.js +0 -82
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts +0 -40
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/base64url.js +0 -72
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
- package/esm/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts +0 -98
- package/esm/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/mod.js +0 -99
- package/esm/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts +0 -120
- package/esm/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/varint.js +0 -205
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -57
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts +0 -35
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common32.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common32.js +0 -198
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts +0 -35
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common64.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common64.js +0 -119
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -16
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -3
- package/script/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts +0 -2
- package/script/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_validate_binary_like.js +0 -29
- package/script/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts +0 -61
- package/script/deps/jsr.io/@std/encoding/1.0.10/ascii85.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/ascii85.js +0 -156
- package/script/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts +0 -40
- package/script/deps/jsr.io/@std/encoding/1.0.10/base32.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/base32.js +0 -91
- package/script/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts +0 -40
- package/script/deps/jsr.io/@std/encoding/1.0.10/base58.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/base58.js +0 -135
- package/script/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts +0 -40
- package/script/deps/jsr.io/@std/encoding/1.0.10/base64.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/base64.js +0 -86
- package/script/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts +0 -40
- package/script/deps/jsr.io/@std/encoding/1.0.10/base64url.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/base64url.js +0 -76
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -91
- package/script/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts +0 -98
- package/script/deps/jsr.io/@std/encoding/1.0.10/mod.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/mod.js +0 -115
- package/script/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts +0 -120
- package/script/deps/jsr.io/@std/encoding/1.0.10/varint.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/varint.js +0 -211
|
@@ -4,31 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const openai_1 = __importDefault(require("openai"));
|
|
7
|
-
const utilites_js_1 = require("../utilites.js");
|
|
8
|
-
const logger_js_1 = require("../logger.js");
|
|
9
|
-
const file_js_1 = require("../file.js");
|
|
10
7
|
const contants_js_1 = require("../contants.js");
|
|
11
|
-
const decoder = new TextDecoder();
|
|
12
8
|
class OpenAIModel {
|
|
13
|
-
constructor(key,
|
|
14
|
-
Object.defineProperty(this, "chunks", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
configurable: true,
|
|
17
|
-
writable: true,
|
|
18
|
-
value: void 0
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(this, "from", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true,
|
|
24
|
-
value: void 0
|
|
25
|
-
});
|
|
26
|
-
Object.defineProperty(this, "to", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true,
|
|
30
|
-
value: void 0
|
|
31
|
-
});
|
|
9
|
+
constructor(key, baseUrl, modelName, options = { noLimit: false, noTimeout: false }) {
|
|
32
10
|
Object.defineProperty(this, "client", {
|
|
33
11
|
enumerable: true,
|
|
34
12
|
configurable: true,
|
|
@@ -41,97 +19,20 @@ class OpenAIModel {
|
|
|
41
19
|
writable: true,
|
|
42
20
|
value: void 0
|
|
43
21
|
});
|
|
44
|
-
this.chunks = chunks;
|
|
45
|
-
this.from = from;
|
|
46
|
-
this.to = to;
|
|
47
22
|
this.model = modelName ?? contants_js_1.DEFAULT_MODELS.openai;
|
|
48
23
|
this.client = new openai_1.default({
|
|
49
24
|
apiKey: key,
|
|
50
25
|
baseURL: baseUrl,
|
|
26
|
+
timeout: options.noTimeout ? 2_147_483_647 : undefined,
|
|
51
27
|
});
|
|
52
28
|
}
|
|
53
|
-
async
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
logger_js_1.logger.warn(`${chunkLabel} Retry ${attempt}/${contants_js_1.MAX_RETRIES} after ${backoffMs}ms backoff...`);
|
|
61
|
-
await (0, utilites_js_1.delay)(backoffMs);
|
|
62
|
-
}
|
|
63
|
-
const response = await this.client.responses.create({
|
|
64
|
-
model: this.model,
|
|
65
|
-
input: [
|
|
66
|
-
{
|
|
67
|
-
role: 'user',
|
|
68
|
-
content: [
|
|
69
|
-
{
|
|
70
|
-
type: 'input_text',
|
|
71
|
-
text: `${systemPrompt}\n\n${userPrompt}\n\nSOURCE_JSON:\n${sourceJson}`,
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
});
|
|
77
|
-
const text = response.output_text;
|
|
78
|
-
if (!text) {
|
|
79
|
-
logger_js_1.logger.error(`${chunkLabel} Empty response from OpenAI`);
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
const cleanedText = (0, utilites_js_1.stripJsonMarkdown)(text);
|
|
83
|
-
const tempJsonFileName = `chunk_${chunk.index + 1}.json`;
|
|
84
|
-
await (0, file_js_1.writeTemp)(this.to, tempJsonFileName, cleanedText);
|
|
85
|
-
if (!(0, utilites_js_1.isValidJson)(cleanedText)) {
|
|
86
|
-
if (attempt === contants_js_1.MAX_RETRIES) {
|
|
87
|
-
logger_js_1.logger.error(`${chunkLabel} Failed after ${contants_js_1.MAX_RETRIES + 1} attempts: Invalid JSON response`);
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
logger_js_1.logger.error(`${chunkLabel} Invalid JSON response, saved to ${tempJsonFileName}`);
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
logger_js_1.logger.info(`${chunkLabel} Translated successfully (${(0, utilites_js_1.formatBytes)(chunk.byteSize)}, ${chunk.keyCount} keys)`);
|
|
94
|
-
return cleanedText;
|
|
95
|
-
}
|
|
96
|
-
catch (error) {
|
|
97
|
-
const isLastAttempt = attempt === contants_js_1.MAX_RETRIES;
|
|
98
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
99
|
-
if (isLastAttempt) {
|
|
100
|
-
logger_js_1.logger.error(`${chunkLabel} Failed after ${contants_js_1.MAX_RETRIES + 1} attempts: ${errorMessage}`);
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
logger_js_1.logger.warn(`${chunkLabel} Attempt ${attempt + 1} failed: ${errorMessage}`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
async translate() {
|
|
109
|
-
const { systemPrompt, userPrompt } = (0, utilites_js_1.generatePrompts)(this.from, this.to);
|
|
110
|
-
const totalChunks = this.chunks.length;
|
|
111
|
-
logger_js_1.logger.info(`Starting translation: ${totalChunks} chunk(s) to process`);
|
|
112
|
-
const results = [];
|
|
113
|
-
const failedChunks = [];
|
|
114
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
115
|
-
const chunk = this.chunks[i];
|
|
116
|
-
if (i > 0) {
|
|
117
|
-
logger_js_1.logger.info(`Waiting ${contants_js_1.INTER_CHUNK_DELAY_MS}ms before next chunk (rate limit protection)...`);
|
|
118
|
-
await (0, utilites_js_1.delay)(contants_js_1.INTER_CHUNK_DELAY_MS);
|
|
119
|
-
}
|
|
120
|
-
const result = await this.translateChunk(chunk, systemPrompt, userPrompt);
|
|
121
|
-
if (result) {
|
|
122
|
-
results.push(result);
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
failedChunks.push(chunk.index + 1);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (failedChunks.length > 0) {
|
|
129
|
-
throw new Error(`Translation failed for chunk(s): ${failedChunks.join(', ')}. Check temp files for partial results.`);
|
|
130
|
-
}
|
|
131
|
-
logger_js_1.logger.info(`All ${totalChunks} chunk(s) translated successfully, merging results...`);
|
|
132
|
-
const jsonInputs = results.map((r) => JSON.parse(r));
|
|
133
|
-
const mergedContent = (0, file_js_1.mergeInputs)(jsonInputs);
|
|
134
|
-
return JSON.stringify(mergedContent, null, 2);
|
|
29
|
+
async translate(prompt) {
|
|
30
|
+
const completion = await this.client.chat.completions.create({
|
|
31
|
+
model: this.model,
|
|
32
|
+
messages: [{ role: 'user', content: prompt }],
|
|
33
|
+
temperature: 0.2,
|
|
34
|
+
});
|
|
35
|
+
return completion.choices[0]?.message?.content ?? '';
|
|
135
36
|
}
|
|
136
37
|
}
|
|
137
38
|
exports.default = OpenAIModel;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Batch, JsonValue, Leaf, TextTranslator, TranslateOptions } from './types.js';
|
|
2
|
+
export declare const extractLeaves: (data: JsonValue) => Leaf[];
|
|
3
|
+
export declare const groupIntoBatches: (leaves: Leaf[], maxBytes: number) => Batch[];
|
|
4
|
+
export declare const buildBatchPrompt: (from: string, to: string, leaves: Leaf[]) => string;
|
|
5
|
+
export declare const decodeResponse: (text: string) => Map<number, string>;
|
|
6
|
+
export declare const reconstruct: (allLeaves: Leaf[], translations: Map<number, string>) => JsonValue;
|
|
7
|
+
export declare const runBatches: (batches: Batch[], translator: TextTranslator, from: string, to: string, options: TranslateOptions) => Promise<Map<number, string>>;
|
|
8
|
+
//# sourceMappingURL=translator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"translator.d.ts","sourceRoot":"","sources":["../../src/src/translator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAQ,cAAc,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAQjG,eAAO,MAAM,aAAa,GAAI,MAAM,SAAS,KAAG,IAAI,EAiCnD,CAAC;AAQF,eAAO,MAAM,gBAAgB,GAAI,QAAQ,IAAI,EAAE,EAAE,UAAU,MAAM,KAAG,KAAK,EA0BxE,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,MAAM,MAAM,EAAE,IAAI,MAAM,EAAE,QAAQ,IAAI,EAAE,KAAG,MAkB3E,CAAC;AAQF,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,KAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAS/D,CAAC;AAgBF,eAAO,MAAM,WAAW,GAAI,WAAW,IAAI,EAAE,EAAE,cAAc,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,SAalF,CAAC;AAEF,eAAO,MAAM,UAAU,GACrB,SAAS,KAAK,EAAE,EAChB,YAAY,cAAc,EAC1B,MAAM,MAAM,EACZ,IAAI,MAAM,EACV,SAAS,gBAAgB,KACxB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAgF7B,CAAC"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runBatches = exports.reconstruct = exports.decodeResponse = exports.buildBatchPrompt = exports.groupIntoBatches = exports.extractLeaves = void 0;
|
|
4
|
+
const logger_js_1 = require("./logger.js");
|
|
5
|
+
const utilites_js_1 = require("./utilites.js");
|
|
6
|
+
const contants_js_1 = require("./contants.js");
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
const byteSize = (s) => encoder.encode(s).byteLength;
|
|
9
|
+
const extractLeaves = (data) => {
|
|
10
|
+
const out = [];
|
|
11
|
+
let nextId = 0;
|
|
12
|
+
const walk = (node, path) => {
|
|
13
|
+
if (node === null || typeof node !== 'object') {
|
|
14
|
+
const isString = typeof node === 'string';
|
|
15
|
+
out.push({
|
|
16
|
+
id: ++nextId,
|
|
17
|
+
path,
|
|
18
|
+
value: node,
|
|
19
|
+
translatable: isString && node.trim().length > 0,
|
|
20
|
+
});
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(node)) {
|
|
24
|
+
if (node.length === 0) {
|
|
25
|
+
out.push({ id: ++nextId, path, value: node, translatable: false });
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
node.forEach((item, i) => walk(item, [...path, i]));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const entries = Object.entries(node);
|
|
32
|
+
if (entries.length === 0) {
|
|
33
|
+
out.push({ id: ++nextId, path, value: node, translatable: false });
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
for (const [k, v] of entries)
|
|
37
|
+
walk(v, [...path, k]);
|
|
38
|
+
};
|
|
39
|
+
walk(data, []);
|
|
40
|
+
return out;
|
|
41
|
+
};
|
|
42
|
+
exports.extractLeaves = extractLeaves;
|
|
43
|
+
const TAG_OPEN = (id) => `≪${id}≫`;
|
|
44
|
+
const TAG_CLOSE = (id) => `≪/${id}≫`;
|
|
45
|
+
const TAG_OVERHEAD_CHARS = 8;
|
|
46
|
+
const wrapEntry = (leaf) => `${TAG_OPEN(leaf.id)}${String(leaf.value)}${TAG_CLOSE(leaf.id)}`;
|
|
47
|
+
const groupIntoBatches = (leaves, maxBytes) => {
|
|
48
|
+
const translatable = leaves.filter((l) => l.translatable);
|
|
49
|
+
const batches = [];
|
|
50
|
+
let current = [];
|
|
51
|
+
let currentSize = 0;
|
|
52
|
+
for (const leaf of translatable) {
|
|
53
|
+
const valueBytes = byteSize(String(leaf.value));
|
|
54
|
+
const idChars = String(leaf.id).length;
|
|
55
|
+
const overhead = TAG_OVERHEAD_CHARS + idChars * 2 + 1;
|
|
56
|
+
const itemSize = valueBytes + overhead;
|
|
57
|
+
if (current.length > 0 && currentSize + itemSize > maxBytes) {
|
|
58
|
+
batches.push({ index: batches.length, leaves: current, byteSize: currentSize });
|
|
59
|
+
current = [];
|
|
60
|
+
currentSize = 0;
|
|
61
|
+
}
|
|
62
|
+
current.push(leaf);
|
|
63
|
+
currentSize += itemSize;
|
|
64
|
+
}
|
|
65
|
+
if (current.length > 0) {
|
|
66
|
+
batches.push({ index: batches.length, leaves: current, byteSize: currentSize });
|
|
67
|
+
}
|
|
68
|
+
return batches;
|
|
69
|
+
};
|
|
70
|
+
exports.groupIntoBatches = groupIntoBatches;
|
|
71
|
+
const buildBatchPrompt = (from, to, leaves) => {
|
|
72
|
+
const entries = leaves.map(wrapEntry).join('\n');
|
|
73
|
+
return `Translate each tagged entry below from ${from} to ${to}.
|
|
74
|
+
|
|
75
|
+
Tag format:
|
|
76
|
+
- Every entry is wrapped exactly as ≪N≫value≪/N≫, where N is the entry id (a positive integer).
|
|
77
|
+
- The opening marker is the character ≪ (U+226A), then the id, then ≫ (U+226B). The closing marker is ≪, /, the same id, then ≫.
|
|
78
|
+
- Copy these markers verbatim. Do not put spaces inside them. Do not change them to <<>>, «», <tag>, [n], or any other notation.
|
|
79
|
+
- The id must match the input id of the same entry — never renumber, merge, split, or reorder entries.
|
|
80
|
+
|
|
81
|
+
What to write back:
|
|
82
|
+
- One ≪N≫translated_value≪/N≫ block per input entry, in the same order, separated by single newlines.
|
|
83
|
+
- Translate only the text that sits between ≪N≫ and ≪/N≫.
|
|
84
|
+
- Preserve every variable ({{name}}, {name}, __VAR__, $t(...), %s, %d), HTML tag, markdown token, escape sequence, and special character inside the value.
|
|
85
|
+
- Output nothing else: no headings, no prose, no commentary, no code fences, no language labels.
|
|
86
|
+
|
|
87
|
+
Now translate the following entries:
|
|
88
|
+
${entries}`;
|
|
89
|
+
};
|
|
90
|
+
exports.buildBatchPrompt = buildBatchPrompt;
|
|
91
|
+
const TAG_PATTERNS = [
|
|
92
|
+
/≪\s*(\d+)\s*≫([\s\S]*?)≪\s*\/\s*\1\s*≫/g,
|
|
93
|
+
/<<\s*(\d+)\s*>>([\s\S]*?)<<\s*\/\s*\1\s*>>/g,
|
|
94
|
+
/«\s*(\d+)\s*»([\s\S]*?)«\s*\/\s*\1\s*»/g,
|
|
95
|
+
];
|
|
96
|
+
const decodeResponse = (text) => {
|
|
97
|
+
const map = new Map();
|
|
98
|
+
for (const pattern of TAG_PATTERNS) {
|
|
99
|
+
for (const match of text.matchAll(pattern)) {
|
|
100
|
+
const id = parseInt(match[1], 10);
|
|
101
|
+
if (!map.has(id))
|
|
102
|
+
map.set(id, match[2].trim());
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return map;
|
|
106
|
+
};
|
|
107
|
+
exports.decodeResponse = decodeResponse;
|
|
108
|
+
const setPath = (root, path, value) => {
|
|
109
|
+
let node = root;
|
|
110
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
111
|
+
const key = path[i];
|
|
112
|
+
const nextKey = path[i + 1];
|
|
113
|
+
const container = node;
|
|
114
|
+
if (container[key] === undefined) {
|
|
115
|
+
container[key] = typeof nextKey === 'number' ? [] : {};
|
|
116
|
+
}
|
|
117
|
+
node = container[key];
|
|
118
|
+
}
|
|
119
|
+
node[path[path.length - 1]] = value;
|
|
120
|
+
};
|
|
121
|
+
const reconstruct = (allLeaves, translations) => {
|
|
122
|
+
if (allLeaves.length === 0)
|
|
123
|
+
return {};
|
|
124
|
+
const firstStep = allLeaves[0].path[0];
|
|
125
|
+
if (firstStep === undefined) {
|
|
126
|
+
const only = allLeaves[0];
|
|
127
|
+
return only.translatable ? translations.get(only.id) ?? only.value : only.value;
|
|
128
|
+
}
|
|
129
|
+
const root = typeof firstStep === 'number' ? [] : {};
|
|
130
|
+
for (const leaf of allLeaves) {
|
|
131
|
+
const value = leaf.translatable && translations.has(leaf.id) ? translations.get(leaf.id) : leaf.value;
|
|
132
|
+
setPath(root, leaf.path, value);
|
|
133
|
+
}
|
|
134
|
+
return root;
|
|
135
|
+
};
|
|
136
|
+
exports.reconstruct = reconstruct;
|
|
137
|
+
const runBatches = async (batches, translator, from, to, options) => {
|
|
138
|
+
const translations = new Map();
|
|
139
|
+
const failedIds = [];
|
|
140
|
+
for (let i = 0; i < batches.length; i++) {
|
|
141
|
+
const batch = batches[i];
|
|
142
|
+
const label = `[Batch ${batch.index + 1}/${batches.length}]`;
|
|
143
|
+
if (i > 0 && !options.noLimit) {
|
|
144
|
+
logger_js_1.logger.info(`Waiting ${contants_js_1.INTER_BATCH_DELAY_MS}ms before next batch (rate limit protection)...`);
|
|
145
|
+
await (0, utilites_js_1.delay)(contants_js_1.INTER_BATCH_DELAY_MS);
|
|
146
|
+
}
|
|
147
|
+
let remaining = [...batch.leaves];
|
|
148
|
+
for (let attempt = 0; attempt <= contants_js_1.MAX_RETRIES; attempt++) {
|
|
149
|
+
if (attempt > 0) {
|
|
150
|
+
const backoffMs = contants_js_1.BASE_RETRY_DELAY_MS * Math.pow(2, attempt - 1);
|
|
151
|
+
logger_js_1.logger.warn(`${label} Retry ${attempt}/${contants_js_1.MAX_RETRIES} after ${backoffMs}ms backoff (${remaining.length} entries)...`);
|
|
152
|
+
await (0, utilites_js_1.delay)(backoffMs);
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const prompt = (0, exports.buildBatchPrompt)(from, to, remaining);
|
|
156
|
+
const response = await translator.translate(prompt);
|
|
157
|
+
if (!response || response.trim().length === 0) {
|
|
158
|
+
if (attempt === contants_js_1.MAX_RETRIES) {
|
|
159
|
+
logger_js_1.logger.error(`${label} Failed after ${contants_js_1.MAX_RETRIES + 1} attempts: empty response`);
|
|
160
|
+
for (const leaf of remaining)
|
|
161
|
+
failedIds.push(leaf.id);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
logger_js_1.logger.warn(`${label} Attempt ${attempt + 1} returned empty response`);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const decoded = (0, exports.decodeResponse)(response);
|
|
168
|
+
const stillMissing = [];
|
|
169
|
+
for (const leaf of remaining) {
|
|
170
|
+
const value = decoded.get(leaf.id);
|
|
171
|
+
if (!value || value.trim().length === 0) {
|
|
172
|
+
stillMissing.push(leaf);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
translations.set(leaf.id, value);
|
|
176
|
+
}
|
|
177
|
+
if (stillMissing.length === 0) {
|
|
178
|
+
logger_js_1.logger.info(`${label} Translated successfully (${(0, utilites_js_1.formatBytes)(batch.byteSize)}, ${batch.leaves.length} entries)`);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
if (attempt === contants_js_1.MAX_RETRIES) {
|
|
182
|
+
logger_js_1.logger.error(`${label} Failed after ${contants_js_1.MAX_RETRIES + 1} attempts: ${stillMissing.length} entries missing`);
|
|
183
|
+
for (const leaf of stillMissing)
|
|
184
|
+
failedIds.push(leaf.id);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
logger_js_1.logger.warn(`${label} ${stillMissing.length}/${remaining.length} entries missing, will retry`);
|
|
188
|
+
remaining = stillMissing;
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
192
|
+
if (attempt === contants_js_1.MAX_RETRIES) {
|
|
193
|
+
logger_js_1.logger.error(`${label} Failed after ${contants_js_1.MAX_RETRIES + 1} attempts: ${message}`);
|
|
194
|
+
for (const leaf of remaining)
|
|
195
|
+
failedIds.push(leaf.id);
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
logger_js_1.logger.warn(`${label} Attempt ${attempt + 1} failed: ${message}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (failedIds.length > 0) {
|
|
203
|
+
const preview = failedIds.slice(0, 20).join(', ');
|
|
204
|
+
const more = failedIds.length > 20 ? `, …(+${failedIds.length - 20})` : '';
|
|
205
|
+
throw new Error(`Translation failed for ${failedIds.length} entry/entries (ids: ${preview}${more})`);
|
|
206
|
+
}
|
|
207
|
+
return translations;
|
|
208
|
+
};
|
|
209
|
+
exports.runBatches = runBatches;
|
package/script/src/types.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export type Provider = 'gemini' | 'openai' | 'anthropic';
|
|
2
|
+
export type TranslateOptions = {
|
|
3
|
+
noLimit: boolean;
|
|
4
|
+
noTimeout: boolean;
|
|
5
|
+
};
|
|
2
6
|
export type TranslateArgs = {
|
|
3
7
|
key?: string;
|
|
4
8
|
provider?: string;
|
|
@@ -8,6 +12,9 @@ export type TranslateArgs = {
|
|
|
8
12
|
from?: string;
|
|
9
13
|
to?: string;
|
|
10
14
|
url?: string;
|
|
15
|
+
'no-limit'?: boolean;
|
|
16
|
+
'no-timeout'?: boolean;
|
|
17
|
+
'max-batch-size'?: string;
|
|
11
18
|
};
|
|
12
19
|
export type ValidatedTranslateArgs = {
|
|
13
20
|
key: string;
|
|
@@ -18,19 +25,29 @@ export type ValidatedTranslateArgs = {
|
|
|
18
25
|
from: string;
|
|
19
26
|
to: string;
|
|
20
27
|
url?: string;
|
|
28
|
+
noLimit: boolean;
|
|
29
|
+
noTimeout: boolean;
|
|
30
|
+
maxBatchBytes: number;
|
|
21
31
|
};
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
to: string;
|
|
26
|
-
translate(): Promise<string>;
|
|
32
|
+
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
|
|
33
|
+
export interface JsonObject {
|
|
34
|
+
[key: string]: JsonValue;
|
|
27
35
|
}
|
|
28
|
-
export type
|
|
29
|
-
export type
|
|
30
|
-
export type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
export type JsonArray = JsonValue[];
|
|
37
|
+
export type PathSegment = string | number;
|
|
38
|
+
export type Path = PathSegment[];
|
|
39
|
+
export type Leaf = {
|
|
40
|
+
id: number;
|
|
41
|
+
path: Path;
|
|
42
|
+
value: JsonValue;
|
|
43
|
+
translatable: boolean;
|
|
44
|
+
};
|
|
45
|
+
export type Batch = {
|
|
34
46
|
index: number;
|
|
47
|
+
leaves: Leaf[];
|
|
48
|
+
byteSize: number;
|
|
35
49
|
};
|
|
50
|
+
export interface TextTranslator {
|
|
51
|
+
translate(prompt: string): Promise<string>;
|
|
52
|
+
}
|
|
36
53
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,WAAW,CAAC;AAEzD,MAAM,MAAM,aAAa,GAAG;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,WAAW,CAAC;AAEzD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,QAAQ,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,UAAU,GAAG,SAAS,CAAC;AAClF,MAAM,WAAW,UAAU;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B;AACD,MAAM,MAAM,SAAS,GAAG,SAAS,EAAE,CAAC;AAEpC,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,CAAC;AAC1C,MAAM,MAAM,IAAI,GAAG,WAAW,EAAE,CAAC;AAEjC,MAAM,MAAM,IAAI,GAAG;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,IAAI,CAAC;IACX,KAAK,EAAE,SAAS,CAAC;IACjB,YAAY,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,IAAI,EAAE,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,WAAW,cAAc;IAC7B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC5C"}
|
package/script/src/utilites.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import type { TranslateArgs, ValidatedTranslateArgs } from './types.js';
|
|
2
2
|
import type { Args } from '../deps/jsr.io/@std/cli/1.0.29/mod.js';
|
|
3
3
|
export declare const validateArgs: (args: Args<TranslateArgs>) => ValidatedTranslateArgs;
|
|
4
|
-
export declare const generatePrompts: (from: string, to: string) => {
|
|
5
|
-
systemPrompt: string;
|
|
6
|
-
userPrompt: string;
|
|
7
|
-
};
|
|
8
|
-
export declare const stripJsonMarkdown: (text: string) => string;
|
|
9
|
-
export declare const isValidJson: (value: string) => boolean;
|
|
10
4
|
export declare const formatBytes: (bytes: number) => string;
|
|
11
5
|
export declare const delay: (ms: number) => Promise<void>;
|
|
12
6
|
//# sourceMappingURL=utilites.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utilites.d.ts","sourceRoot":"","sources":["../../src/src/utilites.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,aAAa,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAClF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uCAAuC,CAAC;
|
|
1
|
+
{"version":3,"file":"utilites.d.ts","sourceRoot":"","sources":["../../src/src/utilites.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,aAAa,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAClF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uCAAuC,CAAC;AAGlE,eAAO,MAAM,YAAY,GAAI,MAAM,IAAI,CAAC,aAAa,CAAC,KAAG,sBA+BxD,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,KAAG,MAI3C,CAAC;AAEF,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAE9C,CAAC"}
|
package/script/src/utilites.js
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.delay = exports.formatBytes = exports.
|
|
3
|
+
exports.delay = exports.formatBytes = exports.validateArgs = void 0;
|
|
4
|
+
const contants_js_1 = require("./contants.js");
|
|
4
5
|
const validateArgs = (args) => {
|
|
5
|
-
if (!args.key)
|
|
6
|
+
if (!args.key)
|
|
6
7
|
throw new Error('AI Key is required');
|
|
7
|
-
|
|
8
|
-
if (!args.provider) {
|
|
8
|
+
if (!args.provider)
|
|
9
9
|
throw new Error('Provider parameter is required');
|
|
10
|
-
|
|
11
|
-
if (!args.input) {
|
|
10
|
+
if (!args.input)
|
|
12
11
|
throw new Error('Input parameter is required');
|
|
13
|
-
|
|
14
|
-
if (!args.output) {
|
|
12
|
+
if (!args.output)
|
|
15
13
|
throw new Error('Output parameter is required');
|
|
16
|
-
|
|
17
|
-
if (!args.from) {
|
|
14
|
+
if (!args.from)
|
|
18
15
|
throw new Error('Source language (from) parameter is required');
|
|
19
|
-
|
|
20
|
-
if (!args.to) {
|
|
16
|
+
if (!args.to)
|
|
21
17
|
throw new Error('Target language (to) parameter is required');
|
|
18
|
+
let maxBatchBytes = contants_js_1.DEFAULT_MAX_BATCH_BYTES;
|
|
19
|
+
const rawMaxBatchSize = args['max-batch-size'];
|
|
20
|
+
if (rawMaxBatchSize !== undefined) {
|
|
21
|
+
const parsed = parseInt(rawMaxBatchSize, 10);
|
|
22
|
+
if (isNaN(parsed) || parsed <= 0) {
|
|
23
|
+
throw new Error('--max-batch-size must be a positive integer (value in KB, e.g. --max-batch-size 8)');
|
|
24
|
+
}
|
|
25
|
+
maxBatchBytes = parsed * 1024;
|
|
22
26
|
}
|
|
23
27
|
return {
|
|
24
28
|
key: args.key,
|
|
@@ -29,133 +33,12 @@ const validateArgs = (args) => {
|
|
|
29
33
|
from: args.from,
|
|
30
34
|
to: args.to,
|
|
31
35
|
url: args.url,
|
|
36
|
+
noLimit: args['no-limit'] ?? false,
|
|
37
|
+
noTimeout: args['no-timeout'] ?? false,
|
|
38
|
+
maxBatchBytes,
|
|
32
39
|
};
|
|
33
40
|
};
|
|
34
41
|
exports.validateArgs = validateArgs;
|
|
35
|
-
const generatePrompts = (from, to) => {
|
|
36
|
-
const systemPrompt = `You are a specialized i18next JSON translation expert. Your role is to translate content from ${from} to ${to} with these strict requirements:
|
|
37
|
-
|
|
38
|
-
1. COMPLETE TRANSLATION:
|
|
39
|
-
- Translate ALL text values comprehensively
|
|
40
|
-
- Double-check to ensure no text is left untranslated
|
|
41
|
-
- Pay special attention to arrays and nested objects to ensure everything is translated
|
|
42
|
-
- If unsure about any translation, provide the most accurate and natural translation possible
|
|
43
|
-
|
|
44
|
-
2. TRANSLATION QUALITY:
|
|
45
|
-
- Use natural, context-appropriate language
|
|
46
|
-
- Maintain consistent terminology throughout the translation
|
|
47
|
-
- Use formal language unless the source is clearly casual
|
|
48
|
-
- Preserve the exact meaning and tone of the original text
|
|
49
|
-
- For UI elements, use standard localized terms common in ${to} applications
|
|
50
|
-
|
|
51
|
-
3. STRUCTURAL PRESERVATION:
|
|
52
|
-
- Keep all JSON structure and keys exactly as they are
|
|
53
|
-
- Maintain all variables and interpolation patterns ({{name}}, __VARIABLE__, $t(), etc.)
|
|
54
|
-
- Preserve all HTML tags and markdown formatting
|
|
55
|
-
- Keep all whitespace, nesting, and formatting intact
|
|
56
|
-
|
|
57
|
-
4. VALIDATION:
|
|
58
|
-
- Return only valid JSON
|
|
59
|
-
- Verify that all text is translated
|
|
60
|
-
- Ensure no source language text remains
|
|
61
|
-
- Confirm all arrays and nested objects are fully translated`;
|
|
62
|
-
const userPrompt = `Please translate this i18next JSON file with these specific requirements:
|
|
63
|
-
|
|
64
|
-
1. THOROUGH TRANSLATION:
|
|
65
|
-
- Translate every single text value from ${from} to ${to}
|
|
66
|
-
- Pay special attention to arrays and nested objects
|
|
67
|
-
- Verify no text is left in ${from}
|
|
68
|
-
- Double-check all translations for completeness
|
|
69
|
-
|
|
70
|
-
2. PRESERVE STRUCTURE:
|
|
71
|
-
- Keep all keys unchanged (e.g. "button.submit")
|
|
72
|
-
- Maintain all variables: {{name}}, __VAR__, $t()
|
|
73
|
-
- Preserve HTML tags and markdown
|
|
74
|
-
- Keep all special characters
|
|
75
|
-
- Maintain exact JSON structure
|
|
76
|
-
|
|
77
|
-
3. QUALITY REQUIREMENTS:
|
|
78
|
-
- Use natural ${to} language
|
|
79
|
-
- Maintain consistent terminology
|
|
80
|
-
- Use formal language unless source is casual
|
|
81
|
-
- Ensure translations match the context
|
|
82
|
-
- Use standard ${to} UI terminology for interface elements
|
|
83
|
-
|
|
84
|
-
4. OUTPUT:
|
|
85
|
-
- Return only the translated JSON
|
|
86
|
-
- No explanations or comments
|
|
87
|
-
- No additional text
|
|
88
|
-
- No formatting changes
|
|
89
|
-
- Must be valid JSON`;
|
|
90
|
-
return { systemPrompt, userPrompt };
|
|
91
|
-
};
|
|
92
|
-
exports.generatePrompts = generatePrompts;
|
|
93
|
-
const stripJsonMarkdown = (text) => {
|
|
94
|
-
const trimmed = text.trim();
|
|
95
|
-
// Extract from code fence if present (handles surrounding text too)
|
|
96
|
-
const fenceMatch = trimmed.match(/```(?:\w+)?\s*\n?([\s\S]*?)\n?```/);
|
|
97
|
-
if (fenceMatch) {
|
|
98
|
-
const candidate = fenceMatch[1].trim();
|
|
99
|
-
if (candidate.startsWith('{') || candidate.startsWith('[')) {
|
|
100
|
-
return candidate;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// Find outermost JSON structure by tracking bracket depth,
|
|
104
|
-
// correctly ignoring brackets inside strings and escape sequences
|
|
105
|
-
const firstBrace = trimmed.indexOf('{');
|
|
106
|
-
const firstBracket = trimmed.indexOf('[');
|
|
107
|
-
if (firstBrace === -1 && firstBracket === -1)
|
|
108
|
-
return trimmed;
|
|
109
|
-
let startIndex;
|
|
110
|
-
let openChar;
|
|
111
|
-
let closeChar;
|
|
112
|
-
if (firstBrace === -1 || (firstBracket !== -1 && firstBracket < firstBrace)) {
|
|
113
|
-
startIndex = firstBracket;
|
|
114
|
-
openChar = '[';
|
|
115
|
-
closeChar = ']';
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
startIndex = firstBrace;
|
|
119
|
-
openChar = '{';
|
|
120
|
-
closeChar = '}';
|
|
121
|
-
}
|
|
122
|
-
let depth = 0;
|
|
123
|
-
let inString = false;
|
|
124
|
-
let escaped = false;
|
|
125
|
-
for (let i = startIndex; i < trimmed.length; i++) {
|
|
126
|
-
const char = trimmed[i];
|
|
127
|
-
if (escaped) {
|
|
128
|
-
escaped = false;
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
if (char === '\\' && inString) {
|
|
132
|
-
escaped = true;
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
if (char === '"') {
|
|
136
|
-
inString = !inString;
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
if (inString)
|
|
140
|
-
continue;
|
|
141
|
-
if (char === openChar)
|
|
142
|
-
depth++;
|
|
143
|
-
else if (char === closeChar && --depth === 0)
|
|
144
|
-
return trimmed.slice(startIndex, i + 1);
|
|
145
|
-
}
|
|
146
|
-
return trimmed;
|
|
147
|
-
};
|
|
148
|
-
exports.stripJsonMarkdown = stripJsonMarkdown;
|
|
149
|
-
const isValidJson = (value) => {
|
|
150
|
-
try {
|
|
151
|
-
JSON.parse(value);
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
catch {
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
exports.isValidJson = isValidJson;
|
|
159
42
|
const formatBytes = (bytes) => {
|
|
160
43
|
if (bytes < 1024)
|
|
161
44
|
return `${bytes} B`;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Uint8Array_ } from "./_types.js";
|
|
2
|
-
export type { Uint8Array_ };
|
|
3
|
-
export declare const alphabet: Uint8Array<ArrayBufferLike>;
|
|
4
|
-
export declare const rAlphabet: Uint8Array<ArrayBuffer>;
|
|
5
|
-
/**
|
|
6
|
-
* Calculate the output size needed to encode a given input size for
|
|
7
|
-
* {@linkcode encodeIntoHex}.
|
|
8
|
-
*
|
|
9
|
-
* @param originalSize The size of the input buffer.
|
|
10
|
-
* @returns The size of the output buffer.
|
|
11
|
-
*
|
|
12
|
-
* @example Basic Usage
|
|
13
|
-
* ```ts
|
|
14
|
-
* import { assertEquals } from "@std/assert";
|
|
15
|
-
* import { calcSizeHex } from "@std/encoding/unstable-hex";
|
|
16
|
-
*
|
|
17
|
-
* assertEquals(calcSizeHex(1), 2);
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export declare function calcSizeHex(originalSize: number): number;
|
|
21
|
-
export declare function encode(buffer: Uint8Array_, i: number, o: number, alphabet: Uint8Array): number;
|
|
22
|
-
export declare function decode(buffer: Uint8Array_, i: number, o: number, alphabet: Uint8Array): number;
|
|
23
|
-
//# sourceMappingURL=_common16.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"_common16.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/encoding/1.0.10/_common16.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,WAAW,EAAE,CAAC;AAE5B,eAAO,MAAM,QAAQ,6BAA+C,CAAC;AACrE,eAAO,MAAM,SAAS,yBAA+B,CAAC;AAMtD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,MAAM,CACpB,MAAM,EAAE,WAAW,EACnB,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,QAAQ,EAAE,UAAU,GACnB,MAAM,CAOR;AAED,wBAAgB,MAAM,CACpB,MAAM,EAAE,WAAW,EACnB,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,QAAQ,EAAE,UAAU,GACnB,MAAM,CAeR"}
|