Skip to main content
Glama

localizationDelete

Remove translation entries for a specific key from a specified file in the GonMCPtool MCP server for efficient localization management.

Instructions

刪除指定Key的翻譯項目

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes
keyYes

Implementation Reference

  • main.ts:398-413 (registration)
    Registration of the 'localizationDelete' MCP tool, including inline input schema { filePath: z.string(), key: z.string() } and a thin async handler that calls LocalizationTool.deleteEntry(filePath, key) with try-catch error handling.
    server.tool("localizationDelete", "刪除指定Key的翻譯項目", { filePath: z.string(), key: z.string() }, async ({ filePath, key }) => { try { const result = await LocalizationTool.deleteEntry(filePath, key); return { content: [{ type: "text", text: result }] }; } catch (error) { return { content: [{ type: "text", text: `刪除失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );
  • The core handler logic for deleting a specific localization entry by key from the CSV file. Loads cached CSV data, finds and removes the matching entry, updates cache, and writes modified data back to file. Returns success/error message.
    static async deleteEntry(filePath: string, key: string): Promise<string> { try { const records = await this.getCSVData(filePath); // 找到要刪除的項目索引 const index = records.findIndex(entry => entry.Key === key); if (index === -1) { return `錯誤: Key "${key}" 不存在`; } // 刪除項目 records.splice(index, 1); // 寫回檔案並更新緩存 await this.writeCSVData(filePath, records); return `成功刪除Key "${key}"`; } catch (error) { console.error(`刪除翻譯項失敗: ${error instanceof Error ? error.message : '未知錯誤'}`); throw error; } }
  • TypeScript interface defining the structure of a localization entry in the CSV, used throughout the LocalizationTool class.
    export interface LocalizationEntry { Key: string; 'zh-TW': string; 'zh-CN': string; en: string; [key: string]: string; // 其他可能的語言欄位 }
  • Supporting helper method to write modified CSV data back to file with proper column ordering (Key first), quoting, and cache update. Called by deleteEntry.
    private static async writeCSVData(filePath: string, data: LocalizationEntry[]): Promise<void> { try { // 檢查是否有數據 if (data.length === 0) { throw new Error('沒有要寫入的數據'); } // 獲取所有列名 const columnSet = new Set<string>(); columnSet.add('Key'); // 確保Key始終是第一列 // 收集所有可能的列 data.forEach(entry => { Object.keys(entry).forEach(key => columnSet.add(key)); }); // 轉換為陣列並將Key移到第一位 const columns = Array.from(columnSet); if (columns[0] !== 'Key') { const keyIndex = columns.indexOf('Key'); if (keyIndex > 0) { columns.splice(keyIndex, 1); columns.unshift('Key'); } } // 使用csv-stringify生成CSV內容 const output = stringify(data, { header: true, columns, quoted_string: true, quoted_empty: true }); // 寫入檔案 await fs.writeFile(filePath, output, 'utf-8'); // 更新緩存 this.cache.set(filePath, { data: [...data], // 深拷貝防止引用問題 timestamp: Date.now() }); } catch (error) { console.error(`寫入CSV檔案失敗: ${error instanceof Error ? error.message : '未知錯誤'}`); throw error; } }
  • Helper method to load CSV data from file or cache (30s expiry), with robust parsing (standard or manual). Used by deleteEntry to get current data.
    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; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/GonTwVn/GonMCPtool'

If you have feedback or need assistance with the MCP directory API, please join our Discord server