Skip to main content
Glama

localizationExportJson

Export translations for a specific language to JSON format from localization files. Specify file path and language to generate structured translation data.

Instructions

將特定語言的翻譯導出為JSON格式

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes
languageYes

Implementation Reference

  • The core handler function that reads the CSV localization file, extracts translations for the specified language, and returns them as a formatted JSON object.
    static async exportLanguageAsJson(filePath: string, language: string): Promise<string> {
      try {
        const records = await this.getCSVData(filePath);
    
        // 創建Key-Value的映射
        const langData: Record<string, string> = {};
    
        for (const entry of records) {
          if (entry.Key && entry[language]) {
            langData[entry.Key] = entry[language];
          }
        }
    
        return JSON.stringify(langData, null, 2);
      } catch (error) {
        console.error(`匯出語言資料失敗: ${error instanceof Error ? error.message : '未知錯誤'}`);
        throw error;
      }
    }
  • main.ts:416-430 (registration)
    Registers the 'localizationExportJson' tool with the MCP server, defines the input schema, and provides a wrapper handler that calls the core implementation and formats the MCP response.
    server.tool("localizationExportJson",
        "將特定語言的翻譯導出為JSON格式",
        { filePath: z.string(), language: z.string() },
        async ({ filePath, language }) => {
            try {
                const result = await LocalizationTool.exportLanguageAsJson(filePath, language);
                return {
                    content: [{ type: "text", text: result }]
                };
            } catch (error) {
                return {
                    content: [{ type: "text", text: `匯出失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }]
                };
            }
        }
  • Zod schema defining the input parameters: filePath (string) and language (string).
    { filePath: z.string(), language: z.string() },
  • TypeScript interface defining the structure of localization entries in the CSV file, used by the tool's parsing logic.
    export interface LocalizationEntry {
      Key: string;
      'zh-TW': string;
      'zh-CN': string;
      en: string;
      [key: string]: string; // 其他可能的語言欄位
    }
  • Helper method that loads and parses the CSV file into LocalizationEntry array, with caching for efficiency. Used by the handler.
    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