languages.service.ts 1.64 KB
Newer Older
Vitali Stupin's avatar
Vitali Stupin committed
1
import { Injectable } from '@angular/core';
Vitali Stupin's avatar
Vitali Stupin committed
2
import {TranslateService} from '@ngx-translate/core';
Vitali Stupin's avatar
Vitali Stupin committed
3 4
import { Title } from '@angular/platform-browser';
import { Subscription } from 'rxjs';
Vitali Stupin's avatar
Vitali Stupin committed
5
import { take } from 'rxjs/operators';
6
import { AppConfig } from './app.config';
Vitali Stupin's avatar
Vitali Stupin committed
7 8 9 10 11

@Injectable({
  providedIn: 'root'
})
export class LanguagesService {
Vitali Stupin's avatar
Vitali Stupin committed
12 13 14
  private selectedLang = '';
  private translateSubscription: Subscription;

Vitali Stupin's avatar
Vitali Stupin committed
15 16
  constructor(
    private translate: TranslateService,
17 18
    private title: Title,
    private config: AppConfig
Vitali Stupin's avatar
Vitali Stupin committed
19
  ) {
Vitali Stupin's avatar
Vitali Stupin committed
20
    this.selectedLang = this.getDefaultLang();
21
    translate.setDefaultLang(this.config.getConfig('LANGUAGES')[this.selectedLang]);
Vitali Stupin's avatar
Vitali Stupin committed
22
    this.updateTitle();
Vitali Stupin's avatar
Vitali Stupin committed
23 24 25 26 27
  }

  updateTitle(): void {
    // Not subscribing if subscription already in progress
    if (!this.translateSubscription || this.translateSubscription.closed) {
Vitali Stupin's avatar
Vitali Stupin committed
28
      this.translateSubscription = this.translate.get('index.title').pipe(take(1)).subscribe((res: string) => {
Vitali Stupin's avatar
Vitali Stupin committed
29
        this.title.setTitle(res);
Vitali Stupin's avatar
Vitali Stupin committed
30 31 32 33 34
      });
    }
  }

  getDefaultLang(): string {
Vitali Stupin's avatar
Vitali Stupin committed
35 36
    if (window && window.localStorage && window.localStorage.getItem('lang')) {
      return window.localStorage.getItem('lang');
Vitali Stupin's avatar
Vitali Stupin committed
37
    }
38
    return Object.keys(this.config.getConfig('LANGUAGES'))[0];
Vitali Stupin's avatar
Vitali Stupin committed
39 40 41
  }

  getLangs(): string[] {
42
    return Object.keys(this.config.getConfig('LANGUAGES'));
Vitali Stupin's avatar
Vitali Stupin committed
43 44
  }

Vitali Stupin's avatar
Vitali Stupin committed
45
  getLang(): string {
Vitali Stupin's avatar
Vitali Stupin committed
46
    return this.selectedLang;
Vitali Stupin's avatar
Vitali Stupin committed
47 48
  }

Vitali Stupin's avatar
Vitali Stupin committed
49
  setLang(lang: string): void {
Vitali Stupin's avatar
Vitali Stupin committed
50 51 52
    this.selectedLang = lang;
    if (window && window.localStorage) {
      window.localStorage.setItem('lang', this.selectedLang);
Vitali Stupin's avatar
Vitali Stupin committed
53
    }
54
    this.translate.use(this.config.getConfig('LANGUAGES')[this.selectedLang]);
Vitali Stupin's avatar
Vitali Stupin committed
55
    this.updateTitle();
Vitali Stupin's avatar
Vitali Stupin committed
56 57
  }
}