/* * Copyright (C) 2020-2021 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import {Injectable, OnDestroy} from '@angular/core'; import {LangChangeEvent, TranslateService} from '@ngx-translate/core'; import {SCLanguage, SCThings, SCThingTranslator, SCThingType, SCTranslations} from '@openstapps/core'; import {Subscription} from 'rxjs'; import {isDefined, ThingTranslateParser} from './thing-translate.parser'; // export const DEFAULT_LANGUAGE = new InjectionToken('DEFAULT_LANGUAGE'); // tslint:disable: member-ordering prefer-function-over-method newline-per-chained-call completed-docs @Injectable({ providedIn: 'root', }) export class ThingTranslateService implements OnDestroy { onLangChange: Subscription; translator: SCThingTranslator; /** * * @param translateService Instance of Angular TranslateService * @param parser An instance of the parser currently used * @param language TODO */ constructor(private readonly translateService: TranslateService, public parser: ThingTranslateParser){ this.translator = new SCThingTranslator('de' ?? this.translateService.currentLang as keyof SCTranslations); /** set the default language from configuration */ this.onLangChange = this.translateService.onLangChange.subscribe((event: LangChangeEvent) => { this.translator.language = event.lang as keyof SCTranslations; }); } /** * Returns the parsed result of the translations */ // tslint:disable-next-line: no-any getParsedResult(target: object, key: string): any { return this.parser.getValueFromKeyPath(target, key); } /** * Gets the translated value of a key (or an array of keys) * @returns the translated key, or an object of translated keys */ public get(thing: SCThings, keyPath: string | string[]): string | number | boolean | object { if (!isDefined(keyPath) || keyPath.length === 0) { throw new Error(`Parameter "keyPath" required`); } if (keyPath instanceof Array) { return this.getParsedResult(this.translator.translate(thing), keyPath.join('.')); } return this.getParsedResult(this.translator.translate(thing), keyPath); } /** * Gets the translated value of a key (or an array of keys) * @returns the translated key, or an object of translated keys */ public getPropertyName(type: SCThingType, keyPath: string | string[]): string { const translatedPropertyNames = this.translator.translatedPropertyNames(type); if (!isDefined(translatedPropertyNames)) { throw new Error(`Parameter "type" is an invalid SCThingType`); } if (!isDefined(keyPath) || keyPath.length === 0) { throw new Error(`Parameter "keyPath" required`); } if (keyPath instanceof Array) { return this.getParsedResult(translatedPropertyNames!, keyPath.join('.')); } return this.getParsedResult(translatedPropertyNames!, keyPath); } // tslint:disable-next-line: completed-docs ngOnDestroy() { if (!this.onLangChange.closed) { this.onLangChange.unsubscribe(); } } }