Skip to content

Commit 23da84c

Browse files
committed
ran prettier
1 parent 4c118b7 commit 23da84c

File tree

2 files changed

+116
-123
lines changed

2 files changed

+116
-123
lines changed

i18n/controllers/parsers.ts

Lines changed: 105 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export async function cleanParser(
7676
return translatedChunk;
7777
}
7878

79-
export async function splitParser(filePath: PathLike, logError: Function): Promise<[boolean, string][]> {
79+
export async function splitParser(
80+
filePath: PathLike,
81+
logError: Function
82+
): Promise<[boolean, string][]> {
8083
// Create a SAX parser in strict mode to split source into chunks.
8184
const parser = createParser();
8285

@@ -137,7 +140,7 @@ export async function splitParser(filePath: PathLike, logError: Function): Promi
137140
segments.length > 0 &&
138141
segments[segments.length - 1][0] &&
139142
segments[segments.length - 1][1].length + currentSegment.length <
140-
Number(MAXLEN)
143+
Number(MAXLEN)
141144
) {
142145
segments[segments.length - 1][1] += currentSegment;
143146
} else {
@@ -186,121 +189,123 @@ export async function splitParser(filePath: PathLike, logError: Function): Promi
186189
return segments;
187190
}
188191

189-
export async function recurSplitParser(ori: string, filePath: PathLike, logError: Function): Promise<string[]> {
190-
let subTranslated: string[] = [];
191-
// continue splitting the chunk
192-
// Create a SAX parser in strict mode to split source into chunks.
193-
await new Promise<void>((resolve, reject) => {
194-
const subParser = createParser();
192+
export async function recurSplitParser(
193+
ori: string,
194+
filePath: PathLike,
195+
logError: Function
196+
): Promise<string[]> {
197+
let subTranslated: string[] = [];
198+
// continue splitting the chunk
199+
// Create a SAX parser in strict mode to split source into chunks.
200+
await new Promise<void>((resolve, reject) => {
201+
const subParser = createParser();
195202

196-
let subCurrentDepth = 0;
197-
let subCurrentSegment = "";
198-
const subSegments: [boolean, string][] = [];
199-
let subIsRecording = false;
203+
let subCurrentDepth = 0;
204+
let subCurrentSegment = "";
205+
const subSegments: [boolean, string][] = [];
206+
let subIsRecording = false;
200207

201-
subParser.on("opentag", node => {
202-
if (node.name === "WRAPPER") return;
208+
subParser.on("opentag", node => {
209+
if (node.name === "WRAPPER") return;
203210

204-
subCurrentDepth++;
211+
subCurrentDepth++;
205212

206-
if (subCurrentDepth === 2) subIsRecording = true;
213+
if (subCurrentDepth === 2) subIsRecording = true;
207214

208-
if (subIsRecording) {
209-
subCurrentSegment += `<${node.name}${formatAttributes(node.attributes)}>`;
210-
} else {
211-
subSegments.push([
212-
false,
213-
`<${node.name}${formatAttributes(node.attributes)}>`
214-
]);
215-
}
216-
});
215+
if (subIsRecording) {
216+
subCurrentSegment += `<${node.name}${formatAttributes(node.attributes)}>`;
217+
} else {
218+
subSegments.push([
219+
false,
220+
`<${node.name}${formatAttributes(node.attributes)}>`
221+
]);
222+
}
223+
});
217224

218-
subParser.on("text", text => {
219-
text = strongEscapeXML(text);
220-
if (subIsRecording) {
221-
subCurrentSegment += text;
222-
} else if (
223-
subSegments.length > 0 &&
224-
subSegments[subSegments.length - 1][0]
225-
) {
226-
subSegments[subSegments.length - 1][1] += text;
227-
} else if (
228-
text.trim() === "" ||
229-
text.trim() === "," ||
230-
text.trim() === "."
231-
) {
232-
subSegments.push([false, text]);
233-
} else {
234-
subSegments.push([true, text]);
235-
}
236-
});
225+
subParser.on("text", text => {
226+
text = strongEscapeXML(text);
227+
if (subIsRecording) {
228+
subCurrentSegment += text;
229+
} else if (
230+
subSegments.length > 0 &&
231+
subSegments[subSegments.length - 1][0]
232+
) {
233+
subSegments[subSegments.length - 1][1] += text;
234+
} else if (
235+
text.trim() === "" ||
236+
text.trim() === "," ||
237+
text.trim() === "."
238+
) {
239+
subSegments.push([false, text]);
240+
} else {
241+
subSegments.push([true, text]);
242+
}
243+
});
237244

238-
subParser.on("cdata", cdata => {
239-
if (subIsRecording) {
240-
subCurrentSegment += `<![CDATA[${cdata}]]>`;
241-
}
242-
});
245+
subParser.on("cdata", cdata => {
246+
if (subIsRecording) {
247+
subCurrentSegment += `<![CDATA[${cdata}]]>`;
248+
}
249+
});
243250

244-
subParser.on("closetag", tagName => {
245-
if (tagName === "WRAPPER") {
246-
return;
247-
}
251+
subParser.on("closetag", tagName => {
252+
if (tagName === "WRAPPER") {
253+
return;
254+
}
248255

249-
subCurrentSegment += `</${tagName}>`;
256+
subCurrentSegment += `</${tagName}>`;
250257

251-
if (subCurrentDepth === 2) {
252-
// We are closing a segment element.
253-
if (ignoredTags.includes(tagName)) {
254-
subSegments.push([false, subCurrentSegment]);
255-
} else if (
256-
subSegments.length > 0 &&
257-
subSegments[subSegments.length - 1][0] &&
258-
subSegments[subSegments.length - 1][1].length +
258+
if (subCurrentDepth === 2) {
259+
// We are closing a segment element.
260+
if (ignoredTags.includes(tagName)) {
261+
subSegments.push([false, subCurrentSegment]);
262+
} else if (
263+
subSegments.length > 0 &&
264+
subSegments[subSegments.length - 1][0] &&
265+
subSegments[subSegments.length - 1][1].length +
259266
subCurrentSegment.length <
260267
Number(MAXLEN)
261-
) {
262-
subSegments[subSegments.length - 1][1] += subCurrentSegment;
263-
} else {
264-
subSegments.push([true, subCurrentSegment]);
265-
}
266-
subCurrentSegment = "";
267-
subIsRecording = false;
268+
) {
269+
subSegments[subSegments.length - 1][1] += subCurrentSegment;
270+
} else {
271+
subSegments.push([true, subCurrentSegment]);
268272
}
273+
subCurrentSegment = "";
274+
subIsRecording = false;
275+
}
269276

270-
if (subCurrentDepth === 1) {
271-
subSegments.push([false, `</${tagName}>`]);
272-
subCurrentSegment = "";
273-
}
277+
if (subCurrentDepth === 1) {
278+
subSegments.push([false, `</${tagName}>`]);
279+
subCurrentSegment = "";
280+
}
274281

275-
subCurrentDepth--;
276-
});
282+
subCurrentDepth--;
283+
});
277284

278-
subParser.on("comment", comment => {
279-
if (subIsRecording) {
280-
subCurrentSegment += `<!-- ${comment} -->`;
281-
} else {
282-
subSegments.push([false, `<!-- ${comment} -->`]);
283-
}
284-
});
285-
286-
subParser.on("end", async () =>
287-
resolve()
288-
);
289-
290-
subParser.on("error", err => {
291-
logError(`Error in subParser for ${filePath}:`, err, filePath);
292-
// Try to recover and continue
293-
try {
294-
subParser._parser.error = null;
295-
subParser._parser.resume();
296-
} catch (resumeErr) {
297-
logError(`Could not recover from parser error:`, resumeErr, filePath);
298-
reject(err);
299-
}
300-
});
285+
subParser.on("comment", comment => {
286+
if (subIsRecording) {
287+
subCurrentSegment += `<!-- ${comment} -->`;
288+
} else {
289+
subSegments.push([false, `<!-- ${comment} -->`]);
290+
}
291+
});
292+
293+
subParser.on("end", async () => resolve());
301294

302-
Readable.from("<WRAPPER>" + ori + "</WRAPPER>").pipe(subParser);
295+
subParser.on("error", err => {
296+
logError(`Error in subParser for ${filePath}:`, err, filePath);
297+
// Try to recover and continue
298+
try {
299+
subParser._parser.error = null;
300+
subParser._parser.resume();
301+
} catch (resumeErr) {
302+
logError(`Could not recover from parser error:`, resumeErr, filePath);
303+
reject(err);
304+
}
303305
});
304306

305-
return subTranslated;
307+
Readable.from("<WRAPPER>" + ori + "</WRAPPER>").pipe(subParser);
308+
});
309+
310+
return subTranslated;
306311
}

i18n/controllers/recurTranslate.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ async function translate(langCode: string, filePath: string): Promise<void> {
8383
const output_path = filePath.replace(
8484
path.sep + "en" + path.sep,
8585
path.sep +
86-
".." +
87-
path.sep +
88-
"i18n" +
89-
path.sep +
90-
"translation_output" +
91-
path.sep +
92-
langCode +
93-
path.sep
86+
".." +
87+
path.sep +
88+
"i18n" +
89+
path.sep +
90+
"translation_output" +
91+
path.sep +
92+
langCode +
93+
path.sep
9494
);
9595

9696
const translated: string = await recursivelyTranslate(
@@ -141,11 +141,7 @@ async function recursivelyTranslate(
141141
subTranslated.push(segment[1]);
142142
}
143143
} catch (error) {
144-
logError(
145-
`Error translating segment in ${filePath}:`,
146-
error,
147-
filePath
148-
);
144+
logError(`Error translating segment in ${filePath}:`, error, filePath);
149145
// Add error comment and continue with next segment
150146
subTranslated.push(
151147
segment[1] + `<!-- Error translating this segment -->`
@@ -176,15 +172,9 @@ async function recursivelyTranslate(
176172
translated.push(segment[1]);
177173
}
178174
} catch (error) {
179-
logError(
180-
`Error translating segment in ${filePath}:`,
181-
error,
182-
filePath
183-
);
175+
logError(`Error translating segment in ${filePath}:`, error, filePath);
184176
// Add error comment and continue with next segment
185-
translated.push(
186-
segment[1] + `<!-- Error translating this section -->`
187-
);
177+
translated.push(segment[1] + `<!-- Error translating this section -->`);
188178
}
189179
}
190180

@@ -201,8 +191,6 @@ async function recursivelyTranslate(
201191
return chunk;
202192
}
203193

204-
let translatedChunk = "";
205-
206194
try {
207195
await ai.beta.threads.messages.create(thread.id, {
208196
role: "user",

0 commit comments

Comments
 (0)