localizationSearch
Search for specific text within translation items by specifying file path, text, language, and limit. Part of GonMCPtool for managing localization tasks.
Instructions
搜尋包含特定文字的翻譯項目
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | ||
| language | No | ||
| limit | No | ||
| searchText | Yes |
Implementation Reference
- tools/localizationTool.ts:300-338 (handler)Core handler function that performs the actual search logic: loads localization CSV data, filters entries where values in specified language (or all) contain the search text (case-insensitive), limits results, and returns SearchResult.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; } }
- main.ts:323-342 (registration)MCP server tool registration for 'localizationSearch', including description, Zod input schema, and thin async handler that calls the core LocalizationTool.searchEntries and formats 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 : "未知錯誤"}` }] }; } }
- tools/localizationTool.ts:19-22 (schema)TypeScript interface defining the structure of the search results: total count and array of matching LocalizationEntry objects.export interface SearchResult { totalResults: number; entries: LocalizationEntry[]; }
- tools/localizationTool.ts:8-14 (schema)TypeScript interface defining a single localization entry with Key and language fields.export interface LocalizationEntry { Key: string; 'zh-TW': string; 'zh-CN': string; en: string; [key: string]: string; // 其他可能的語言欄位 }
- tools/localizationTool.ts:180-205 (helper)Helper method to load and parse CSV localization data with caching to avoid repeated file reads.private static async getCSVData(filePath: string, force = false): Promise<LocalizationEntry[]> { const now = Date.now(); const cached = this.cache.get(filePath); // 如果緩存有效且不需要強制重新讀取 if (!force && cached && (now - cached.timestamp < this.CACHE_EXPIRY)) { return cached.data; } try { // 讀取並解析CSV檔案 const content = await this.readCSVFileRaw(filePath); const records = this.parseCSVContent(content); // 更新緩存 this.cache.set(filePath, { data: records, timestamp: now }); return records; } catch (error) { console.error(`解析CSV檔案失敗: ${error instanceof Error ? error.message : '未知錯誤'}`); throw error; } }