@memory-bank/mcp
Version:
Memory-enabled Co-Pilot (MCP) server for managing project documentation and context
89 lines • 2.52 kB
JavaScript
/**
* Translation class represents a localized text in a specific language
*/
export class Translation {
_language;
_key;
_text;
/**
* Constructor with validation
*
* @param language Language instance
* @param key Translation key
* @param text Translated text
* @throws Error if key or text is empty
*/
constructor(language, key, text) {
if (!key || key.trim() === '') {
throw new Error('Translation key cannot be empty');
}
if (!text || text.trim() === '') {
throw new Error('Translation text cannot be empty');
}
this._language = language;
this._key = key.trim();
this._text = text;
}
/**
* Gets the language
*/
get language() {
return this._language;
}
/**
* Gets the translation key
*/
get key() {
return this._key;
}
/**
* Gets the translated text
*/
get text() {
return this._text;
}
/**
* Creates a Translation instance, validating inputs
*
* @param language Language instance
* @param key Translation key
* @param text Translated text
* @returns Translation instance
* @throws Error if key or text is empty
*/
static create(language, key, text) {
return new Translation(language, key, text);
}
/**
* Creates a new Translation with variables replaced in the text
*
* @param variables Object with variable names and their values
* @returns New Translation instance with replaced variables
*/
withReplacedVariables(variables) {
if (!variables || Object.keys(variables).length === 0) {
return this;
}
let newText = this._text;
Object.entries(variables).forEach(([name, value]) => {
const pattern = new RegExp(`\\{\\{${name}\\}\\}`, 'g');
newText = newText.replace(pattern, value);
});
if (newText === this._text) {
return this;
}
return new Translation(this._language, this._key, newText);
}
/**
* Compare equality with another Translation object
*
* @param other Another Translation object to compare
* @returns true if equal, false otherwise
*/
equals(other) {
return (this._language.equals(other.language) &&
this._key === other.key &&
this._text === other.text);
}
}
//# sourceMappingURL=Translation.js.map