Skip to content

Commit 3329c54

Browse files
committed
reorganised file structure and yarn prepare json to translate all languages in xml
1 parent d48e425 commit 3329c54

File tree

147 files changed

+10546
-312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+10546
-312
lines changed

i18n/controllers/recurTranslate.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const ai = new OpenAI({
1919
baseURL: process.env.AI_BASEURL
2020
});
2121

22-
const MAXLEN = 3000;
22+
const MAXLEN = Number(process.env.MAX_LEN) || 3000;
2323

2424
const createParser = () =>
2525
(sax as any).createStream(true, { trim: false }, { strictEntities: true });
@@ -35,7 +35,7 @@ async function translate(language: string, filePath: string): Promise<void> {
3535
const translated: string = await recursivelyTranslate(language, input_dir);
3636

3737
const output_path = fileURLToPath(
38-
import.meta.resolve("../../xml_cn" + filePath)
38+
import.meta.resolve("../../xml/cn" + filePath)
3939
);
4040

4141
// Ensure directory exists
@@ -69,7 +69,7 @@ async function recursivelyTranslate(
6969
await new Promise<void>((resolve, reject) => {
7070
const subParser = createParser();
7171

72-
let subCurrentDepth = 0;
72+
let subCurrentDepth = 1;
7373
let subCurrentSegment = "";
7474
const subSegments: [boolean, string][] = [];
7575
let subIsRecording = false;
@@ -78,7 +78,7 @@ async function recursivelyTranslate(
7878
if (node.name === "WRAPPER") {
7979
return;
8080
}
81-
81+
8282
subCurrentDepth++;
8383

8484
// If we're at depth 2, this is the start of a new segment.
@@ -160,7 +160,6 @@ async function recursivelyTranslate(
160160
// We are closing the root element.
161161
subSegments.push([false, `</${tagName}>`]);
162162
}
163-
164163
subCurrentDepth--;
165164
});
166165

@@ -194,7 +193,7 @@ async function recursivelyTranslate(
194193
// Create a SAX parser in strict mode to split source into chunks.
195194
const parser = createParser();
196195

197-
// const assistant = await createAssistant(language, ai);
196+
// const assistant = await createAssistant(language, ai as any);
198197
const assistant_id = "asst_BLVYfog5DpWrbu3fW3o2oD4r";
199198
const thread = await ai.beta.threads.create();
200199
let translated: String[] = [];
@@ -316,7 +315,7 @@ async function recursivelyTranslate(
316315
return chunk;
317316
}
318317

319-
console.log("Translating chunk of length: " + chunk.length);
318+
// console.log("Translating chunk of length: " + chunk.length);
320319
if (chunk.length < 100) {
321320
console.log("\nchunk: " + chunk)
322321
}
@@ -333,13 +332,15 @@ async function recursivelyTranslate(
333332
Content to translate:
334333
<TRANSLATE> ${chunk} </TRANSLATE>`
335334
});
335+
336336
const run = await ai.beta.threads.runs.createAndPoll(thread.id, {
337337
assistant_id: assistant_id
338338
});
339339

340340
const messages = await ai.beta.threads.messages.list(thread.id, {
341341
run_id: run.id
342342
});
343+
343344
const message = messages.data.pop()!;
344345
const messageContent = message.content[0];
345346

@@ -352,6 +353,7 @@ async function recursivelyTranslate(
352353
const text = messageContent.text;
353354

354355
const safeText = escapeXML(text.value);
356+
console.log(safeText);
355357
const textStream = Readable.from("<WRAPPER>" + safeText + "</WRAPPER>");
356358

357359
await new Promise<void>((resolve, reject) => {
@@ -416,7 +418,7 @@ async function recursivelyTranslate(
416418
console.log(`Error occured while translating ${path}:\n ` + err);
417419
return (
418420
translatedChunk +
419-
`<!-- Error occured while translating this section-->\n<!-- Error: ${err.length < 50 ? err : err.subString(0, 50) + "..."}-->`
421+
`<!-- Error occured while translating this section-->\n<!-- Error: ${err}-->`
420422
);
421423
}
422424
}

i18n/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export default async function fancyName(path: string) {
1212
try {
1313
await Promise.all([
1414
// fancyName("2"),
15-
fancyName("1.1"),
15+
// fancyName("1.1"),
1616
fancyName("1.1.2"),
17-
fancyName("1.1.3"),
18-
fancyName("1.1.4"),
19-
fancyName("1.1.5"),
17+
// fancyName("1.1.3"),
18+
// fancyName("1.1.4"),
19+
// fancyName("1.1.5"),
2020
// fancyName("1.1.6"),
2121
// fancyName("1.1.7"),
2222
// fancyName("1.1.8"),

i18n/initializers/initialize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export default async function createAssistant(language: string, ai: OpenAI) {
2121
].map(path => fs.createReadStream(path));
2222

2323
// Create a vector store including our two files.
24-
const vectorStore = await ai.beta.vectorStores.create({
24+
const vectorStore = await ai.vectorStores.create({
2525
name: "Translation instructions"
2626
});
2727

28-
await ai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, {
28+
await ai.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, {
2929
files: fileStreams
3030
});
3131

javascript/index.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { writeRewritedSearchData } from "./searchRewrite";
4242
import { setupSnippetsJson } from "./processingFunctions/processSnippetJson";
4343
import { createTocJson } from "./generateTocJson";
4444
import { setupReferencesJson } from "./processingFunctions/processReferenceJson";
45+
import { SourceTextModule } from "vm";
4546

4647
export let parseType;
4748
let version;
@@ -58,6 +59,11 @@ const ensureDirectoryExists = (path, cb) => {
5859
});
5960
};
6061

62+
const getDirectories = async source =>
63+
(await readdir(source, { withFileTypes: true }))
64+
.filter(dirent => dirent.isDirectory())
65+
.map(dirent => dirent.name);
66+
6167
async function translateXml(filepath, filename, option) {
6268
const fullFilepath = path.join(inputDir, filepath, filename);
6369
const fileToRead = await open(fullFilepath, "r");
@@ -200,8 +206,14 @@ async function recursiveXmlToHtmlInOrder(option) {
200206
}
201207
}
202208

203-
async function recursiveTranslateXml(filepath, option) {
209+
async function recursiveTranslateXml(filepath, option, lang = "en") {
204210
let files;
211+
212+
if (lang != null) {
213+
filepath = path.join(filepath, lang);
214+
console.log(filepath);
215+
}
216+
205217
const fullPath = path.join(inputDir, filepath);
206218
files = await readdir(fullPath);
207219
const promises = [];
@@ -222,7 +234,9 @@ async function recursiveTranslateXml(filepath, option) {
222234
promises.push(translateXml(filepath, file, option));
223235
}
224236
} else if (fs.lstatSync(path.join(fullPath, file)).isDirectory()) {
225-
promises.push(recursiveTranslateXml(path.join(filepath, file), option));
237+
promises.push(
238+
recursiveTranslateXml(path.join(filepath, file), option, null)
239+
);
226240
}
227241
});
228242
await Promise.all(promises);
@@ -349,23 +363,28 @@ async function main() {
349363
console.log("setup snippets done\n");
350364
recursiveTranslateXml("", "parseXml");
351365
} else if (parseType == "json") {
352-
outputDir = path.join(__dirname, "../json");
366+
const languages = await getDirectories(path.join(__dirname, "../xml"));
367+
console.dir(languages)
353368

354-
createMain();
355369

356-
console.log("\ngenerate table of content\n");
357-
await recursiveTranslateXml("", "generateTOC");
358-
allFilepath = sortTOC(allFilepath);
359-
createTocJson(outputDir);
370+
languages.forEach(async lang => {
371+
outputDir = path.join(__dirname, "../json", lang);
372+
createMain();
360373

361-
console.log("setup snippets and references\n");
362-
await recursiveXmlToHtmlInOrder("setupSnippet");
363-
console.log("setup snippets and references done\n");
374+
console.log("\ngenerate table of content\n");
375+
await recursiveTranslateXml("", "generateTOC", lang);
376+
allFilepath = sortTOC(allFilepath);
377+
createTocJson(outputDir);
378+
379+
console.log("setup snippets and references\n");
380+
await recursiveXmlToHtmlInOrder("setupSnippet");
381+
console.log("setup snippets and references done\n");
364382

365-
await recursiveXmlToHtmlInOrder("parseXml");
366-
writeRewritedSearchData();
367-
// this is meant to be temp; also, will remove the original "generateSearchData" after the updation at the frontend is completed.
368-
//testIndexSearch();
383+
await recursiveXmlToHtmlInOrder("parseXml");
384+
writeRewritedSearchData();
385+
// this is meant to be temp; also, will remove the original "generateSearchData" after the updation at the frontend is completed.
386+
//testIndexSearch();
387+
});
369388
}
370389
}
371390

0 commit comments

Comments
 (0)