Skip to content

Commit 10856a1

Browse files
author
yihao03
committed
Refactor translate function to improve XML error handling and update output path
1 parent 098de27 commit 10856a1

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

i18n/controllers/translate.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ async function translate(language: string, filePath: string) {
108108
}
109109
console.log(`Done translating all segments.`);
110110
const output_path = fileURLToPath(
111-
import.meta.resolve("../../xml/translations" + filePath)
111+
import.meta.resolve("../../xml_cn" + filePath)
112112
);
113113

114114
// Ensure directory exists
@@ -140,7 +140,7 @@ async function translate(language: string, filePath: string) {
140140

141141
clean.on("text", text => {
142142
if (currDepth >= 1) {
143-
translated += text;
143+
translated += escapeXML(text);
144144
}
145145
});
146146

@@ -166,6 +166,22 @@ async function translate(language: string, filePath: string) {
166166
translated += `<!-- ${comment} -->`;
167167
});
168168

169+
clean.on("error", error => {
170+
console.log(
171+
"error encountered when validating XML: " +
172+
error +
173+
"\nvalidating section: " +
174+
chunk.substring(0, 100) + "..."
175+
);
176+
177+
// Attempt to recover using the internal parser
178+
try {
179+
clean._parser.resume();
180+
} catch (e) {
181+
console.log("Failed to resume parser:", e);
182+
}
183+
});
184+
169185
let translated = "";
170186

171187
try {
@@ -174,7 +190,7 @@ async function translate(language: string, filePath: string) {
174190
content: `Translate this content to ${language}.
175191
IMPORTANT: You MUST search the uploaded reference file for any technical terms and use EXACTLY the translations specified there.
176192
If a term exists in the reference file, use that translation without deviation.
177-
Do not modify XML tags, content of XML tags and structure. Do not say anything else. Only translate the content and return the xml as is.
193+
Do not modify XML tags, attributes of XML tags and structure. Do not say anything else.
178194
Content to translate:
179195
${chunk}`
180196
});
@@ -225,4 +241,4 @@ function formatAttributes(attrs) {
225241

226242
function escapeXML(str: string): string {
227243
return str.replace(/&(?!(?:amp;|lt;|gt;|apos;|quot;))/g, "&amp;");
228-
}
244+
}

0 commit comments

Comments
 (0)