moya-locale.component.ts 1.57 KB
import { Component, OnInit } from '@angular/core';
import {DEFAULT_LOCALE, MoyaLocaleService} from "./moya-locale.service";
import {TranslateService} from "@ngx-translate/core";


const ENGLISH = "en";
const FINNISH = "fi";
const SWEDISH = "sv";



@Component({
  selector: 'moya-locale',
  templateUrl: './moya-locale.component.html',
  styleUrls: ['./moya-locale.component.scss']
})
export class MoyaLocaleComponent implements OnInit {

  fiSelected : boolean = true;
  svSelected : boolean = true;
  enSelected : boolean = true;

  constructor(private localeService : MoyaLocaleService, private translate : TranslateService) {
    // fallback language
    translate.setDefaultLang(DEFAULT_LOCALE);
    this.selectLocale(DEFAULT_LOCALE);
  }

  ngOnInit() {
    this.localeService.getUserLocale().subscribe(locale => { this.selectLocale(locale)} );
  }


  selectLocale(locale : string, save :boolean = false):void {

    if(![ENGLISH, FINNISH, SWEDISH].includes(locale)) {
      return;
    }

    this.translate.use(locale);

    this.fiSelected = this.svSelected = this.enSelected = false;

    switch (locale) {
      case FINNISH:
        this.fiSelected = true;
        break;
      case ENGLISH:
        this.enSelected = true;
        break;
      case SWEDISH:
        this.svSelected = true;
        break;
    }

    if(save) {
      this.localeService.setUserLocale(locale).subscribe();
    }

  }


  selectEnglish() {
    this.selectLocale(ENGLISH, true);
  }

  selectFinnish() {
    this.selectLocale(FINNISH, true);
  }

  selectSwedish() {
    this.selectLocale(SWEDISH, true);
  }




}