All files / models translation-container.ts

100% Statements 33/33
100% Branches 10/10
100% Functions 11/11
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x   1x   20x 20x 20x                     20x 1x   19x 33x   19x 1x     18x 18x 18x 18x       26x       10x       7x       7x       8x       48x                   32x 1x     31x 18x   31x 31x       12x                     14x       45x 45x 44x   1x      
import { InvalidArgumentError } from '../errors/invalid-argument.error';
 
export class TranslationContainer {
    private productName: string;
    private sourceLanguageKey: string = '';
    private translationUnits: Map<string, Map<string, string>> = new Map<string, Map<string, string>>();
    private supportedLanguageKeys: Set<string> = new Set<string>();
    private original: string;
    private datatype: string;
 
    public constructor(
        original: string,
        productName: string,
        datatype: string,
        sourceLanguageKey: string,
        supportedLanguageKeys: string[],
    ) {
        if (supportedLanguageKeys.length === 0) {
            throw new 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 InvalidArgumentError(
                `The source language key '${sourceLanguageKey}' is not a supported language key`);
        }
        this.sourceLanguageKey = sourceLanguageKey;
        this.productName = productName;
        this.original = original;
        this.datatype = datatype;
    }
 
    public getSourceLanguageKey(): string {
        return this.sourceLanguageKey;
    }
 
    public getProductName(): string {
        return this.productName;
    }
 
    public getOriginal(): string {
        return this.original;
    }
 
    public getDatatype(): string {
        return this.datatype;
    }
 
    public getSupportedLanguageKeys(): Set<string> {
        return this.supportedLanguageKeys;
    }
 
    public isLanguageKeySupported(languageKey: string): boolean {
        return this.supportedLanguageKeys.has(languageKey);
    }
 
    /**
     * Add a translation value
     * @param translationId {string}
     * @param languageKey {string}
     * @param value {string}
     */
    public addTranslation(translationId: string, languageKey: string, value: string): void {
        if (!this.isLanguageKeySupported(languageKey)) {
            throw new InvalidArgumentError(`Language key '${languageKey}' is not supported`);
        }
 
        if (!this.translationUnits.has(translationId)) {
            this.translationUnits.set(translationId, new Map<string, string>());
        }
        const map = this.getTranslationValuesById(translationId);
        map.set(languageKey, value);
    }
 
    public getTranslationIds(): string[] {
        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
     */
    public getTranslationsById(translationId: string): Map<string, string> {
        return this.getTranslationValuesById(translationId);
    }
 
    private getTranslationValuesById(translationId: string): Map<string, string> {
        const map = this.translationUnits.get(translationId);
        if (map !== undefined) {
            return map;
        }
        throw new InvalidArgumentError(`Translation id '${translationId}' does not exist`);
    }
}