@memory-bank/mcp
Version:
Memory-enabled Co-Pilot (MCP) server for managing project documentation and context
151 lines • 5.2 kB
JavaScript
/**
* I18n Service
*
* Application service for handling internationalization and translations.
* Manages current language context and provides translation functionality.
*/
import { Language } from '../../domain/i18n/Language.js';
import { Translation } from '../../domain/i18n/Translation.js';
import { logger } from '../../shared/utils/logger.js';
/**
* Service for handling internationalization and translations
*/
export class I18nService {
repository;
currentLanguage;
/**
* Constructor
*
* @param repository Repository for accessing translations
*/
constructor(repository) {
this.repository = repository;
this.currentLanguage = Language.default();
}
/**
* Gets the current language
*
* @returns Current language
*/
getCurrentLanguage() {
return this.currentLanguage;
}
/**
* Sets the current language
*
* @param languageCode Language code to set as current
* @throws Error if language code is not supported
*/
setCurrentLanguage(languageCode) {
this.currentLanguage = new Language(languageCode);
}
translationCache = new Map();
isTranslationLoaded = false;
/**
* Load all translations into memory for faster access
*
* @returns Promise resolving when all translations are loaded
*/
async loadAllTranslations() {
this.translationCache.clear();
const languages = await this.repository.getSupportedLanguages();
for (const language of languages) {
const translations = await this.repository.getTranslationsForLanguage(language);
if (!this.translationCache.has(language.code)) {
this.translationCache.set(language.code, new Map());
}
const languageMap = this.translationCache.get(language.code);
for (const translation of translations) {
languageMap?.set(translation.key, translation);
}
}
this.isTranslationLoaded = true;
}
/**
* Implementation of the translate method
*/
translate(key, secondParam) {
if (typeof secondParam === 'string') {
const languageCode = secondParam;
if (!this.isTranslationLoaded) {
logger.warn('Translations not loaded. Call loadAllTranslations() first.', { component: 'I18nService' });
return `?${key}`;
}
const languageMap = this.translationCache.get(languageCode);
let translation = languageMap?.get(key);
if (!translation && languageCode !== 'en') {
const englishMap = this.translationCache.get('en');
translation = englishMap?.get(key);
}
if (!translation) {
return `?${key}`;
}
return translation.text;
}
else {
const variables = secondParam;
return (async () => {
let translation = await this.repository.getTranslation(key, this.currentLanguage);
if (!translation && this.currentLanguage.code !== 'en') {
translation = await this.repository.getTranslation(key, Language.default());
}
if (!translation) {
return `?${key}`;
}
if (variables) {
return translation.withReplacedVariables(variables).text;
}
return translation.text;
})();
}
}
/**
* Gets all supported languages
*
* @returns Promise resolving to array of supported languages
*/
async getSupportedLanguages() {
return this.repository.getSupportedLanguages();
}
/**
* Checks if a translation exists for current language
*
* @param key Translation key
* @returns Promise resolving to boolean indicating if translation exists
*/
async hasTranslation(key) {
return this.repository.hasTranslation(key, this.currentLanguage);
}
/**
* Gets all available translation keys
*
* @returns Promise resolving to array of translation keys
*/
async getAllKeys() {
return this.repository.getAllKeys();
}
/**
* Gets all translations for the current language
*
* @returns Promise resolving to array of translations
*/
async getAllTranslationsForCurrentLanguage() {
return this.repository.getTranslationsForLanguage(this.currentLanguage);
}
/**
* Saves a translation
*
* @param key Translation key
* @param text Translated text
* @param languageCode Optional language code (defaults to current language)
* @returns Promise resolving to boolean indicating success
*/
async saveTranslation(key, text, languageCode) {
const language = languageCode
? new Language(languageCode)
: this.currentLanguage;
const translation = new Translation(language, key, text);
return this.repository.saveTranslation(translation);
}
}
//# sourceMappingURL=I18nService.js.map