moya-locale.component.ts
1.57 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
}
}