Skip to content

Commit 4125ea4

Browse files
committed
replace MAXLEN with max_chunk_len according to code review
1 parent a53b54d commit 4125ea4

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

i18n/controllers/loggers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import path from "path";
33
import fs from "fs";
44
import { getFileErrors } from "./translator";
55
import { translationSummaryPrefix } from "../config";
6+
import { fileURLToPath } from "url";
7+
import { dirname } from "path";
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = dirname(__filename);
611

712
// Function to save summary log - can be called from signal handlers
813
export async function saveSummaryLog(
@@ -103,7 +108,7 @@ Success rate: ${translateNum > 0 ? ((successCount / translateNum) * 100).toFixed
103108
);
104109
fs.writeFileSync(logPath, summaryLog);
105110
console.log(
106-
`Summary log saved to logs/translation-summary-${timestamp}.log`
111+
`Summary log saved to i18n/logs/${translationSummaryPrefix}-${timestamp}.log`
107112
);
108113
} catch (logError) {
109114
console.error("Failed to save log file:", logError);

i18n/controllers/parsers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { escapeXML, formatAttributes, strongEscapeXML } from "./xmlUtilities";
33
import { Readable } from "stream";
44
import { ignoredTags, max_chunk_len } from "../config";
55
import fs, { PathLike } from "fs";
6-
import { FileLike } from "openai/uploads.mjs";
76

8-
const MAXLEN = max_chunk_len;
97
const createParser = () =>
108
(sax as any).createStream(false, { trim: false }, { strictEntities: true });
119

@@ -140,7 +138,7 @@ export async function splitParser(
140138
segments.length > 0 &&
141139
segments[segments.length - 1][0] &&
142140
segments[segments.length - 1][1].length + currentSegment.length <
143-
Number(MAXLEN)
141+
max_chunk_len
144142
) {
145143
segments[segments.length - 1][1] += currentSegment;
146144
} else {
@@ -264,7 +262,7 @@ export async function recurSplitParser(
264262
subSegments[subSegments.length - 1][0] &&
265263
subSegments[subSegments.length - 1][1].length +
266264
subCurrentSegment.length <
267-
Number(MAXLEN)
265+
max_chunk_len
268266
) {
269267
subSegments[subSegments.length - 1][1] += subCurrentSegment;
270268
} else {

i18n/controllers/translator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import createAssistant from "../initializers/initialize";
55
import dotenv from "dotenv";
66
import sax from "sax";
77
import { cleanParser, recurSplitParser, splitParser } from "./parsers";
8+
import { max_chunk_len } from "../config";
89

910
dotenv.config();
1011

@@ -18,8 +19,6 @@ const ai = new OpenAI({
1819
baseURL: process.env.AI_BASEURL
1920
});
2021

21-
const MAXLEN = Number(process.env.MAX_LEN) || 3000;
22-
2322
// change to true to avoid calling openai api, useful for troubleshooting
2423
// chunking logic
2524
const troubleshoot = false;
@@ -126,7 +125,7 @@ async function recursivelyTranslate(
126125
): Promise<string> {
127126
// Recursive function to split and translate
128127
async function helper(ori: string): Promise<string> {
129-
if (ori.length < MAXLEN) {
128+
if (ori.length < max_chunk_len) {
130129
return await translateChunk(ori); // translate the chunk
131130
}
132131

0 commit comments

Comments
 (0)