-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathlang-switcher.component.ts
64 lines (56 loc) · 1.92 KB
/
lang-switcher.component.ts
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
// libs
import { Component, Inject } from '@angular/core';
import { Store } from '@ngrx/store';
// app
import { Config, ILang, LogService } from '../../core/index';
import { IAppState } from '../../ngrx/index';
import { ElectronEventService } from '../../electron/index';
import * as multilingual from '../actions/index';
import { MultilingualService, Languages, LanguageViewHelper } from '../services/index';
@Component({
moduleId: module.id,
selector: 'lang-switcher',
templateUrl: 'lang-switcher.component.html',
styleUrls: ['lang-switcher.component.css'],
})
export class LangSwitcherComponent {
public lang: string;
public supportedLanguages: Array<ILang>;
constructor(
private store: Store<IAppState>,
private log: LogService,
@Inject(Languages) private languages,
@Inject(LanguageViewHelper) private viewHelper
) {
store.take(1).subscribe((s: any) => {
// s && s.18n - ensures testing works in all cases (since some tests dont use i18n state)
this.lang = s && s.i18n ? s.i18n.lang : '';
});
if (Config.IS_DESKTOP()) {
// allow electron menu to talk to component
ElectronEventService.on('changeLang').subscribe((e: any) => {
this.changeLang({ target: { value: e.detail.value } });
});
}
}
changeLang(e: any) {
let lang = this.supportedLanguages[0].code; // fallback to default 'en'
if (Config.IS_MOBILE_NATIVE()) {
if (e) {
lang = this.supportedLanguages[e.newIndex].code;
}
} else if (e && e.target) {
lang = e.target.value;
}
this.log.debug(`Language change: ${lang}`);
this.store.dispatch(new multilingual.ChangeAction(lang));
}
ngOnInit() {
this.supportedLanguages = this.languages;
if (Config.IS_MOBILE_NATIVE() && this.viewHelper) {
// {N} 3.0 requires SegmentedBarItem class for items
// when binding to SegmentedBar
this.supportedLanguages = this.viewHelper;
}
}
}