1 |
89c98d12
|
Václav Jirák
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {TranslateService} from '@ngx-translate/core';
|
3 |
|
|
import {registerLocaleData} from '@angular/common';
|
4 |
18dbad83
|
Václav Jirák
|
import localeEn from '@angular/common/locales/en';
|
5 |
|
|
import localeCs from '@angular/common/locales/cs';
|
6 |
89c98d12
|
Václav Jirák
|
import {Subject} from 'rxjs';
|
7 |
230804d9
|
Václav Jirák
|
import {Languages} from '../enums/common.enum';
|
8 |
18dbad83
|
Václav Jirák
|
|
9 |
|
|
@Injectable({
|
10 |
|
|
providedIn: 'root'
|
11 |
|
|
})
|
12 |
|
|
export class LocalizationService {
|
13 |
230804d9
|
Václav Jirák
|
readonly defaultLocale = 'cs';
|
14 |
18dbad83
|
Václav Jirák
|
|
15 |
230804d9
|
Václav Jirák
|
currentLocaleSubject: Subject<string>;
|
16 |
89c98d12
|
Václav Jirák
|
|
17 |
230804d9
|
Václav Jirák
|
private currentLocale = this.defaultLocale;
|
18 |
18dbad83
|
Václav Jirák
|
|
19 |
|
|
constructor(private translate: TranslateService) {
|
20 |
230804d9
|
Václav Jirák
|
this.currentLocaleSubject = new Subject<string>();
|
21 |
18dbad83
|
Václav Jirák
|
|
22 |
|
|
registerLocaleData(localeEn);
|
23 |
|
|
registerLocaleData(localeCs);
|
24 |
|
|
|
25 |
230804d9
|
Václav Jirák
|
translate.setDefaultLang(this.defaultLocale);
|
26 |
18dbad83
|
Václav Jirák
|
}
|
27 |
|
|
|
28 |
230804d9
|
Václav Jirák
|
switchLocale(locale: string): string {
|
29 |
|
|
this.translate.use(locale);
|
30 |
|
|
this.currentLocaleSubject.next(locale);
|
31 |
|
|
this.currentLocale = locale;
|
32 |
|
|
|
33 |
|
|
return this.getCurrentLanguage();
|
34 |
89c98d12
|
Václav Jirák
|
}
|
35 |
|
|
|
36 |
|
|
getCurrentLanguage(): Languages {
|
37 |
230804d9
|
Václav Jirák
|
switch (this.currentLocale) {
|
38 |
89c98d12
|
Václav Jirák
|
case 'cs':
|
39 |
|
|
return Languages.CZECH;
|
40 |
|
|
case 'en':
|
41 |
|
|
default:
|
42 |
|
|
return Languages.ENGLISH;
|
43 |
|
|
}
|
44 |
18dbad83
|
Václav Jirák
|
}
|
45 |
f4bbdb70
|
Václav Jirák
|
|
46 |
|
|
getCurrentLocale(): string {
|
47 |
230804d9
|
Václav Jirák
|
return this.currentLocale;
|
48 |
f4bbdb70
|
Václav Jirák
|
}
|
49 |
18dbad83
|
Václav Jirák
|
}
|