UNPKG

@memory-bank/mcp

Version:

Memory-enabled Co-Pilot (MCP) server for managing project documentation and context

127 lines 5.07 kB
/** * I18n Provider Implementation * Provides internationalization services for the application */ import path from 'node:path'; import { logger } from '../../shared/utils/logger.js'; // Import logger /** * Implementation of II18nProvider */ export class I18nProvider { fileSystemService; componentLogger = logger.withContext({ component: 'I18nProvider' }); // Add logger instance translations = new Map(); defaultLanguage = 'en'; supportedLanguages = ['en', 'ja', 'zh']; translationsPath; /** * Constructor * * @param fileSystemService Service for file system operations */ constructor(fileSystemService) { this.fileSystemService = fileSystemService; const dirname = __dirname; // This might need adjustment in ESM context this.translationsPath = path.join(dirname, 'translations'); } /** * Implements II18nProvider.translate */ // Changed parameters to object literal type translate(params) { const { key, language: langParam, params: substitutionParams } = params; // Destructure, rename language to langParam to avoid conflict let language = langParam; // Declare language with let for reassignment if (!this.translations.has(language)) { logger.warn(`Translations not loaded for language ${language}, falling back to ${this.defaultLanguage}`, { component: 'I18nProvider', language }); if (!this.translations.has(this.defaultLanguage)) { // Corrected second argument of processPlaceholders to substitutionParams return this.processPlaceholders(key, substitutionParams); } language = this.defaultLanguage; } const translationMap = this.translations.get(language); const translation = translationMap[key]; if (!translation && language !== this.defaultLanguage && this.translations.has(this.defaultLanguage)) { const defaultTranslationMap = this.translations.get(this.defaultLanguage); const defaultTranslation = defaultTranslationMap[key]; if (defaultTranslation) { // Corrected second argument of processPlaceholders to substitutionParams return this.processPlaceholders(defaultTranslation, substitutionParams); } } // Corrected second argument of processPlaceholders to substitutionParams return this.processPlaceholders(translation || key, substitutionParams); } /** * Implements II18nProvider.loadTranslations */ async loadTranslations(language) { try { const filePath = path.join(this.translationsPath, `${language}.json`); const exists = await this.fileSystemService.fileExists(filePath); if (!exists) { logger.warn(`Translation file not found: ${filePath}`, { component: 'I18nProvider', filePath }); return false; } const content = await this.fileSystemService.readFile(filePath); const translationFile = JSON.parse(content); if (translationFile.language !== language) { logger.warn(`Language mismatch in translation file: expected ${language}, got ${translationFile.language}`, { component: 'I18nProvider', filePath, expectedLanguage: language, actualLanguage: translationFile.language }); return false; } this.translations.set(language, translationFile.translations); return true; } catch (error) { this.componentLogger.error(`Failed to load translations for ${language}`, { error }); // Use componentLogger return false; } } /** * Implements II18nProvider.isLanguageSupported */ isLanguageSupported(language) { return this.supportedLanguages.includes(language); } /** * Implements II18nProvider.getSupportedLanguages */ getSupportedLanguages() { return [...this.supportedLanguages]; } /** * Implements II18nProvider.getDefaultLanguage */ getDefaultLanguage() { return this.defaultLanguage; } /** * Process placeholders in the text * Replaces {{PLACEHOLDER}} with corresponding value from params * * @param text Text containing placeholders * @param params Parameters to substitute * @returns Text with placeholders replaced */ processPlaceholders(text, params) { if (!params) { return text; } return text.replace(/\{\{([A-Z_]+)\}\}/g, (match, placeholder) => { return params[placeholder] || match; }); } /** * Loads all supported languages * * @returns Promise resolving when all languages are loaded */ async loadAllTranslations() { for (const language of this.supportedLanguages) { await this.loadTranslations(language); } } } //# sourceMappingURL=I18nProvider.js.map