All files / src/lib/services NgxTranslateService.ts

61.11% Statements 11/18
0% Branches 0/4
50% Functions 3/6
56.25% Lines 9/16

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 4411x 11x           11x 11x 11x         11x 29x     26x                                           20x      
import { inject, Injectable } from '@angular/core';
import {
  InterpolatableTranslationObject,
  InterpolationParameters,
  TranslateService,
  Translation,
} from '@ngx-translate/core';
import { DecafTranslateService } from '@decaf-ts/ui-decorators';
import { firstValueFrom, Observable } from 'rxjs';
import { Primitives } from '@decaf-ts/decorator-validation';
 
@Injectable({
  providedIn: 'root',
})
export class NgxTranslateService extends DecafTranslateService implements DecafTranslateService {
  private translateService = inject(TranslateService);
 
  override instant(key: string, interpolateParams?: InterpolationParameters): Translation {
    return this.translateService.instant(key, interpolateParams);
  }
 
  override translate(key: string, params?: InterpolationParameters): Translation {
    return this.instant(key, params);
  }
 
  async get(key: string | string[], params?: InterpolationParameters | string): Promise<string> {
    Iif (key) {
      Iif (typeof params === Primitives.STRING) {
        params = { '0': params };
      }
      return await firstValueFrom(this.translateService.get(key, (params || {}) as object));
    }
    return key;
  }
 
  use(lang: string): void {
    this.translateService.use(lang);
  }
 
  setFallbackLang(lang: string): Observable<InterpolatableTranslationObject> {
    return this.translateService.setFallbackLang(lang);
  }
}