copyI18n
Extract and translate text from project files to manage multilingual content using external translation APIs.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| rootPath | Yes |
Implementation Reference
- src/index.ts:56-98 (handler)The main handler function for the 'copyI18n' tool. It downloads i18n files for Chinese and optionally English, saves them temporarily, copies to src/lang/source, and cleans up.async ({ rootPath }) => { let langArr = [{ name: "ZhCn", value: 1, filePath: "ZhCn.js" }]; // 目前只有企业版需要英文文件 if (port === "5500") { langArr.push({ name: "EnUs", value: 2, filePath: "EnUs.js" }); } // 获取工作区根路径 // const rootPath = process.cwd(); let result = ""; try { for (let lang of langArr) { const langTargetPath = path.join( rootPath, "src/lang/source", lang.filePath ); const tempFilePath = path.join(rootPath, lang.filePath); // 创建一个写入流 const writer = fs.createWriteStream(tempFilePath); const res = await downFile(lang.value); res.data.pipe(writer); await new Promise<void>((resolve, reject) => { writer.on("finish", () => resolve()); writer.on("error", (err) => reject(err)); }); fs.copyFileSync(tempFilePath, langTargetPath); fs.unlinkSync(tempFilePath); console.log(`${lang.name}文件复制成功`); result = `${tempFilePath}文件复制成功`; } } catch (error) { console.log("文件复制失败", error); result = `文件复制失败${error},${rootPath},`; } return { content: [ { type: "text", text: result, }, ], }; }
- src/index.ts:53-55 (schema)Input schema for the copyI18n tool, requiring a 'rootPath' string parameter.{ rootPath: z.string(), },
- src/index.ts:51-99 (registration)Registration of the 'copyI18n' tool using server.tool(), including schema and inline handler.server.tool( "copyI18n", { rootPath: z.string(), }, async ({ rootPath }) => { let langArr = [{ name: "ZhCn", value: 1, filePath: "ZhCn.js" }]; // 目前只有企业版需要英文文件 if (port === "5500") { langArr.push({ name: "EnUs", value: 2, filePath: "EnUs.js" }); } // 获取工作区根路径 // const rootPath = process.cwd(); let result = ""; try { for (let lang of langArr) { const langTargetPath = path.join( rootPath, "src/lang/source", lang.filePath ); const tempFilePath = path.join(rootPath, lang.filePath); // 创建一个写入流 const writer = fs.createWriteStream(tempFilePath); const res = await downFile(lang.value); res.data.pipe(writer); await new Promise<void>((resolve, reject) => { writer.on("finish", () => resolve()); writer.on("error", (err) => reject(err)); }); fs.copyFileSync(tempFilePath, langTargetPath); fs.unlinkSync(tempFilePath); console.log(`${lang.name}文件复制成功`); result = `${tempFilePath}文件复制成功`; } } catch (error) { console.log("文件复制失败", error); result = `文件复制失败${error},${rootPath},`; } return { content: [ { type: "text", text: result, }, ], }; } );
- src/api.ts:36-50 (helper)Helper function 'downFile' used by copyI18n to download the i18n JSON file as a stream for a given language ID.async function downFile(language: number) { const streamConfig = { baseURL: `${url}`, headers: { Cookie: token, }, responseType: "stream" as const }; const res = await http.get( `/MultiLanguageResource/DownWebJson?language=${language}`, null, streamConfig ); return res; }