2018-08-30 13:09:36 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
import { CultureInfo } from './models/culture-info';
|
|
|
|
import { registerLocaleData } from '@angular/common';
|
2018-08-31 10:14:56 +02:00
|
|
|
import { AuthService } from '../../services/auth/auth.service';
|
2018-08-30 13:09:36 +02:00
|
|
|
|
|
|
|
const availableCultures: CultureInfo[] = require('../../../assets/resources/available-cultures.json');
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CultureService {
|
|
|
|
|
|
|
|
private cultureValues = new Map<string, CultureInfo>(); // cultures by name
|
|
|
|
private cultureChangeSubject = new Subject<CultureInfo>();
|
|
|
|
private currentCulture: CultureInfo;
|
|
|
|
|
|
|
|
constructor(
|
2018-08-31 10:14:56 +02:00
|
|
|
private authService: AuthService
|
2018-08-30 13:09:36 +02:00
|
|
|
) {
|
|
|
|
if (availableCultures) {
|
|
|
|
this.cultureValues = new Map<string, CultureInfo>();
|
|
|
|
availableCultures.forEach(culture => {
|
|
|
|
this.cultureValues.set(culture.name, culture);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCultureValues(): CultureInfo[] {
|
|
|
|
const values: CultureInfo[] = [];
|
|
|
|
this.cultureValues.forEach((value) => values.push(value));
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCultureValue(culture: string): CultureInfo | undefined {
|
|
|
|
return this.cultureValues.get(culture);
|
|
|
|
}
|
|
|
|
|
|
|
|
cultureSelected(culture: string | CultureInfo) {
|
|
|
|
let newCultureName: string;
|
|
|
|
if (typeof culture == 'string') {
|
|
|
|
if (this.currentCulture && this.currentCulture.name === culture) { return; }
|
|
|
|
newCultureName = culture;
|
|
|
|
} else {
|
|
|
|
if (this.currentCulture && this.currentCulture.name === culture.name) { return; }
|
|
|
|
newCultureName = culture.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newCulture = this.cultureValues.get(newCultureName);
|
|
|
|
if (!newCulture) {
|
|
|
|
console.error(`unsupported culture given: ${newCultureName}`); //TODO: throw error?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentCulture = newCulture;
|
|
|
|
this.cultureChangeSubject.next(newCulture);
|
|
|
|
|
|
|
|
// Set angular locale based on user selection.
|
2018-10-02 16:33:58 +02:00
|
|
|
let locale = newCulture.name
|
2018-08-30 13:09:36 +02:00
|
|
|
import(`@angular/common/locales/${locale}.js`).catch(reason => {
|
2018-10-02 16:33:58 +02:00
|
|
|
console.warn('Could not load locale: ' + locale);
|
|
|
|
locale = newCulture.name.split('-')[0];
|
|
|
|
console.debug('Will try ' + locale);
|
|
|
|
return import(`@angular/common/locales/${locale}.js`).catch(reason => {
|
|
|
|
console.error('Could not load locale: ' + locale)
|
|
|
|
});
|
2018-08-30 13:09:36 +02:00
|
|
|
}).then(selectedLocale => {
|
|
|
|
registerLocaleData(selectedLocale.default);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCultureChangeObservable(): Observable<CultureInfo> {
|
|
|
|
return this.cultureChangeSubject.asObservable();
|
|
|
|
}
|
|
|
|
|
2018-08-31 10:14:56 +02:00
|
|
|
getCurrentCulture(): String {
|
|
|
|
return this.authService.current() && this.authService.current().culture ? this.authService.current().culture : this.currentCulture.name;
|
2018-08-30 13:09:36 +02:00
|
|
|
}
|
|
|
|
}
|