import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Injectable({
  providedIn: 'root',
})

export class HenselTranslateService extends TranslateService {

  public instant(key: string | Array<string>, interpolateParams?: Object): string | any {
    if (!key) return '';
    return super.instant(key, interpolateParams);
  }

  public translateString(stringToTranslate: string) {
    let res = stringToTranslate?.replace(/\${(.*?)}/g, (found: string, translationKey: string) => this.instant(translationKey));
    if (res === stringToTranslate) res = this.instant(res); // source string does not contain placeholder "${}" - try to translate directly
    return res;
  }

}
