localizationSearch
Search for translation items containing specific text in localization files to find and manage multilingual content across projects.
Instructions
搜尋包含特定文字的翻譯項目
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | ||
| searchText | Yes | ||
| language | No | ||
| limit | No |
Implementation Reference
- main.ts:323-343 (registration)Registration of the 'localizationSearch' MCP tool, including description, Zod input schema, and thin async handler that delegates to LocalizationTool.searchEntries and formats the response.server.tool("localizationSearch", "搜尋包含特定文字的翻譯項目", { filePath: z.string(), searchText: z.string(), language: z.string().optional(), limit: z.number().optional() }, async ({ filePath, searchText, language = '', limit = 10 }) => { try { const results = await LocalizationTool.searchEntries(filePath, searchText, language, limit); return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `搜尋失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );
- main.ts:325-330 (schema)Zod input schema defining parameters for the localizationSearch tool: filePath (string), searchText (string), optional language (string), optional limit (number).{ filePath: z.string(), searchText: z.string(), language: z.string().optional(), limit: z.number().optional() },
- tools/localizationTool.ts:300-338 (helper)Core helper function LocalizationTool.searchEntries that loads CSV data from file, performs case-insensitive text search in specified language or all languages (excluding Key), limits results, and returns SearchResult with total and entries.static async searchEntries( filePath: string, searchText: string, language: string = '', limit: number = 10 ): Promise<SearchResult> { try { const records = await this.getCSVData(filePath); const searchTextLower = searchText.toLowerCase(); // 搜尋邏輯 let results: LocalizationEntry[] = []; if (language) { // 僅搜尋指定語言 results = records.filter(entry => entry[language] && entry[language].toLowerCase().includes(searchTextLower) ); } else { // 搜尋所有欄位 results = records.filter(entry => Object.entries(entry).some(([key, value]) => key !== 'Key' && value && value.toLowerCase().includes(searchTextLower) ) ); } // 限制返回數量 const limitedResults = results.slice(0, limit); return { totalResults: results.length, entries: limitedResults }; } catch (error) { console.error(`搜尋文字失敗: ${error instanceof Error ? error.message : '未知錯誤'}`); throw error; } }
- tools/localizationTool.ts:19-22 (schema)Type definition for SearchResult returned by searchEntries.export interface SearchResult { totalResults: number; entries: LocalizationEntry[]; }
- tools/localizationTool.ts:8-14 (schema)Type definition for individual localization entry in CSV.export interface LocalizationEntry { Key: string; 'zh-TW': string; 'zh-CN': string; en: string; [key: string]: string; // 其他可能的語言欄位 }