translationI18n
Translate text across multiple languages using AI-powered multilingual capabilities. Integrates with external APIs for automatic extraction and resource management.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes |
Implementation Reference
- src/index.ts:18-48 (handler)The handler function that implements the core logic of the 'translationI18n' tool. It extracts text blocks wrapped in ##...## using regex, optionally translates to English if port is 5500, calls geti18n to obtain aliases, replaces the original blocks, and returns the modified content.async ({ input }) => { // 匹配所有 ##...## 的内容 const regex = /##([\s\S]+?)##/g; let matches = input.matchAll(regex); let result = input; for (const match of matches) { const value = match[1]; if (!value) continue; let params: Record<string, any> = { language: 1, autoAlias: true, value: value, }; if (port === "5500") { params.valueEn = await TranslateToEn(value); } // 调用多语言接口 const alias = await geti18n(params); if (alias) { result = result.replace(match[0], alias); } } return { content: [ { type: "text", text: result, }, ], }; }
- src/index.ts:13-49 (registration)The registration of the 'translationI18n' tool using server.tool(), including the input schema and inline handler function.server.tool( "translationI18n", { input: z.string(), }, async ({ input }) => { // 匹配所有 ##...## 的内容 const regex = /##([\s\S]+?)##/g; let matches = input.matchAll(regex); let result = input; for (const match of matches) { const value = match[1]; if (!value) continue; let params: Record<string, any> = { language: 1, autoAlias: true, value: value, }; if (port === "5500") { params.valueEn = await TranslateToEn(value); } // 调用多语言接口 const alias = await geti18n(params); if (alias) { result = result.replace(match[0], alias); } } return { content: [ { type: "text", text: result, }, ], }; } );
- src/index.ts:15-17 (schema)Input schema for the tool, defined as a string using Zod.{ input: z.string(), },
- src/api.ts:13-25 (helper)Helper function that makes a POST request to the i18n API endpoint to add a resource and retrieve the translation alias, used by the tool handler.async function geti18n(params: Record<string, any>) { const res = await http.post( "/MultiLanguageResource/AddResource", params, baseConfig ); if (res.Data) { return res.Data.ResourceAlias; } else { return ""; } }
- src/api.ts:27-34 (helper)Helper function to translate text to English via API, conditionally used in the handler when port === '5500'.async function TranslateToEn(text: string) { const res = await http.get( "/MultiLanguageResource/TranslateToEn", { text }, baseConfig ); return res; }