All files / app/shared/components/locale locale.service.ts

100% Statements 21/21
100% Branches 2/2
100% Functions 4/4
100% Lines 20/20

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 423x 3x 3x 3x   3x         3x   12x 12x 12x     12x 12x 12x 9x   3x         11x       9x       14x 14x 14x 14x      
import { inject, Injectable, signal } from '@angular/core';
import { DateAdapter } from '@angular/material/core';
import { TranslateService } from '@ngx-translate/core';
import { DEFAULT_LOCALE, LOCALES } from '../../../core/providers/locale/locale.provider';
 
export const LOCALE_KEY = 'LOCALE_ID';
 
@Injectable({
  providedIn: 'root'
})
export class LocaleService {
 
  private _locale = signal<string>(DEFAULT_LOCALE);
  private _adapter = inject(DateAdapter);
  private _service = inject(TranslateService);
 
  constructor() {
    this._service.addLangs(Object.values(LOCALES));
    const savedLocale = localStorage.getItem(LOCALE_KEY);
    if (savedLocale) {
      this.changeLocale(savedLocale);
    } else {
      this.changeLocale(DEFAULT_LOCALE);
    }
  }
 
  public get locale() {
    return this._locale.asReadonly();
  }
 
  public get locales() {
    return this._service.getLangs();
  }
 
  public changeLocale(locale: string) {
    this._locale.set(locale);
    this._service.use(locale);
    this._adapter.setLocale(locale);
    localStorage.setItem(LOCALE_KEY, locale);
  }
}