Skip to content

Commit 9caca4b

Browse files
committed
add yarn trans test and fixed other bugs
1 parent 63e0d21 commit 9caca4b

File tree

3 files changed

+26
-48
lines changed

3 files changed

+26
-48
lines changed

i18n/controllers/recurTranslate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async function translate(langCode: string, filePath: string): Promise<void> {
8787
throw new Error('Undefined language');
8888
}
8989

90-
if (!troubleshoot) assistant = await createAssistant(langCode, ai as any);
90+
if (!troubleshoot) assistant = await createAssistant(langCode, language, ai as any);
9191

9292
// Generate output path by replacing "/en/" with "/../i18n/translation_output/zh_CN/" in the path
9393
const output_path = filePath.replace(

i18n/index.ts

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -196,30 +196,6 @@ async function findAllXmlFiles(directory: string): Promise<string[]> {
196196
return xmlFiles;
197197
}
198198

199-
// Function to check if a file needs translation
200-
async function needsTranslation(enFilePath: string, lang: string): Promise<boolean> {
201-
// Generate the corresponding cn file path
202-
const cnFilePath = enFilePath.replace(
203-
path.sep + "en" + path.sep,
204-
path.sep + lang + path.sep
205-
);
206-
207-
try {
208-
// Check if CN file exists
209-
const cnStats = await fs.promises.stat(cnFilePath);
210-
if (!cnStats.isFile()) {
211-
return true; // CN path exists but is not a file (unusual case)
212-
}
213-
214-
// Compare modification times
215-
const enStats = await fs.promises.stat(enFilePath);
216-
return enStats.mtime > cnStats.mtime; // Return true if EN file is newer
217-
} catch (error) {
218-
// If we get an error, it's likely because the CN file doesn't exist
219-
return true; // Need to translate since CN file doesn't exist
220-
}
221-
}
222-
223199
(async () => {
224200
await setupCleanupHandlers();
225201

@@ -230,33 +206,35 @@ async function needsTranslation(enFilePath: string, lang: string): Promise<boole
230206
// Get the absolute path to the xml/en directory using proper path resolution
231207
const enDirPath = path.resolve(__dirname, "../xml/en");
232208

233-
// Find all XML files
234-
if (process.argv[2] === "all" || process.argv.length <= 2) {
209+
if (process.argv[2] === "test") {
210+
if (process.argv.length !== 5) {
211+
console.log("syntax: yarn trans test <section> <lang>");
212+
return;
213+
}
214+
try {
215+
console.log('start translating, may take a while ...');
216+
const fullPath = PathGenerator(process.argv[3]);
217+
await translate(process.argv[4], fullPath);
218+
} catch (e) {
219+
console.error('test error: ', e);
220+
}
221+
return;
222+
} if (process.argv[2] === "all" || process.argv.length <= 2) {
223+
// Find all XML files
235224
console.log(`Scanning directory: ${enDirPath}`);
236-
xmlFiles = await findAllXmlFiles(enDirPath);
225+
filestoTranslate = await findAllXmlFiles(enDirPath);
237226
} else {
238-
const [, , ..._xmlFiles] = process.argv;
239-
xmlFiles = _xmlFiles.map(file => path.join(__dirname, "..", file));
227+
const [, , ...xmlFiles] = process.argv;
228+
filesToTranslate = xmlFiles.map(file => path.join(__dirname, "..", file));
240229
}
241230

242-
console.log(`Found ${xmlFiles.length} XML files to check for translation`);
231+
if (filesToTranslate.length === 0) {
232+
console.log(`No files to translate.`);
233+
return;
234+
}
235+
console.log(`${filesToTranslate.length} files need translation`);
243236

244237
for (const lang of languages) {
245-
// Filter files that need translation
246-
filesToTranslate = [];
247-
for (const file of xmlFiles) {
248-
if (await needsTranslation(file, lang)) {
249-
filesToTranslate.push(file as never);
250-
}
251-
}
252-
253-
console.log(`${filesToTranslate.length} files need translation`);
254-
255-
if (filesToTranslate.length === 0) {
256-
console.log(`No files need translation for ${lang}.`);
257-
return;
258-
}
259-
260238
// Process files in batches to avoid overwhelming the system
261239
const batchSize: number = max_trans_num;
262240

i18n/initializers/initialize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from "url";
77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = dirname(__filename);
99

10-
export default async function createAssistant(language: string, ai: OpenAI) {
10+
export default async function createAssistant(langCode: string, language: string, ai: OpenAI) {
1111
const assistant = await ai.beta.assistants.create({
1212
name: "SICP Translator",
1313
instructions: `You are a professional translator with high technical skills in computer science.
@@ -23,7 +23,7 @@ export default async function createAssistant(language: string, ai: OpenAI) {
2323
tools: [{ type: "file_search" }]
2424
});
2525

26-
const fileStreams = [path.join(__dirname, "../ai_files", language, "dictionary.txt")].map(
26+
const fileStreams = [path.join(__dirname, "../ai_files", langCode, "dictionary.txt")].map(
2727
path => fs.createReadStream(path)
2828
);
2929

0 commit comments

Comments
 (0)