languages.service.ts 1.51 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';
Vitali Stupin's avatar
Vitali Stupin committed
6
import { LANGUAGES } from './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 17 18
  constructor(
    private translate: TranslateService,
    private title: Title
  ) {
Vitali Stupin's avatar
Vitali Stupin committed
19 20 21
    this.selectedLang = this.getDefaultLang();
    translate.setDefaultLang(LANGUAGES[this.selectedLang]);
    this.updateTitle();
Vitali Stupin's avatar
Vitali Stupin committed
22 23 24 25 26
  }

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

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

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

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

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