xliff-generator
Version:
A simple module to create xliff files
79 lines (78 loc) • 3.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const invalid_argument_error_1 = require("../errors/invalid-argument.error");
class TranslationContainer {
constructor(original, productName, datatype, sourceLanguageKey, supportedLanguageKeys) {
this.sourceLanguageKey = '';
this.translationUnits = new Map();
this.supportedLanguageKeys = new Set();
if (supportedLanguageKeys.length === 0) {
throw new invalid_argument_error_1.InvalidArgumentError('The size of supported language key is zero');
}
for (let i = 0, len = supportedLanguageKeys.length; i < len; ++i) {
this.supportedLanguageKeys.add(supportedLanguageKeys[i]);
}
if (!this.supportedLanguageKeys.has(sourceLanguageKey)) {
throw new invalid_argument_error_1.InvalidArgumentError(`The source language key '${sourceLanguageKey}' is not a supported language key`);
}
this.sourceLanguageKey = sourceLanguageKey;
this.productName = productName;
this.original = original;
this.datatype = datatype;
}
getSourceLanguageKey() {
return this.sourceLanguageKey;
}
getProductName() {
return this.productName;
}
getOriginal() {
return this.original;
}
getDatatype() {
return this.datatype;
}
getSupportedLanguageKeys() {
return this.supportedLanguageKeys;
}
isLanguageKeySupported(languageKey) {
return this.supportedLanguageKeys.has(languageKey);
}
/**
* Add a translation value
* @param translationId {string}
* @param languageKey {string}
* @param value {string}
*/
addTranslation(translationId, languageKey, value) {
if (!this.isLanguageKeySupported(languageKey)) {
throw new invalid_argument_error_1.InvalidArgumentError(`Language key '${languageKey}' is not supported`);
}
if (!this.translationUnits.has(translationId)) {
this.translationUnits.set(translationId, new Map());
}
const map = this.getTranslationValuesById(translationId);
map.set(languageKey, value);
}
getTranslationIds() {
return Array.from(this.translationUnits.keys());
}
/**
* get the translations of all language keys by translation id.
* @param translationId {string} id to identify the translations
* @returns a map of all tranlations.
* The key of the map is the language key und the corresponding value is the translation value
* @throws InvalidArgumentError will be thrown if the submitted translationId does not exist
*/
getTranslationsById(translationId) {
return this.getTranslationValuesById(translationId);
}
getTranslationValuesById(translationId) {
const map = this.translationUnits.get(translationId);
if (map !== undefined) {
return map;
}
throw new invalid_argument_error_1.InvalidArgumentError(`Translation id '${translationId}' does not exist`);
}
}
exports.TranslationContainer = TranslationContainer;