2019-01-18 18:03:45 +01:00
|
|
|
import { registerLocaleData } from '@angular/common';
|
2018-08-30 13:09:36 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Observable, Subject } from 'rxjs';
|
2019-01-18 18:03:45 +01:00
|
|
|
import { CultureInfo } from '../../model/culture-info';
|
|
|
|
import { LoggingService } from '../logging/logging-service';
|
|
|
|
import { TypeUtils } from '../utilities/type-utils.service';
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
const availableCultures: CultureInfo[] = require('../../../../assets/localization/available-cultures.json');
|
2018-08-30 13:09:36 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CultureService {
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
private cultureValues = new Map<string, CultureInfo>(); // cultures by name
|
|
|
|
private cultureChangeSubject = new Subject<CultureInfo>();
|
|
|
|
private currentCulture: CultureInfo;
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
constructor(
|
2019-01-18 18:03:45 +01:00
|
|
|
private typeUtils: TypeUtils,
|
|
|
|
private logger: LoggingService
|
2018-10-05 17:00:54 +02:00
|
|
|
) {
|
|
|
|
if (availableCultures) {
|
|
|
|
this.cultureValues = new Map<string, CultureInfo>();
|
|
|
|
availableCultures.forEach(culture => {
|
|
|
|
this.cultureValues.set(culture.name, culture);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
getCultureValues(): CultureInfo[] {
|
|
|
|
const values: CultureInfo[] = [];
|
|
|
|
this.cultureValues.forEach((value) => values.push(value));
|
|
|
|
return values;
|
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
getCultureValue(culture: string): CultureInfo | undefined {
|
|
|
|
return this.cultureValues.get(culture);
|
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
cultureSelected(culture: string | CultureInfo) {
|
|
|
|
let newCultureName: string;
|
2019-01-18 18:03:45 +01:00
|
|
|
if (this.typeUtils.isString(culture)) {
|
2018-10-05 17:00:54 +02:00
|
|
|
if (this.currentCulture && this.currentCulture.name === culture) { return; }
|
|
|
|
newCultureName = culture;
|
|
|
|
} else {
|
|
|
|
if (this.currentCulture && this.currentCulture.name === culture.name) { return; }
|
|
|
|
newCultureName = culture.name;
|
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
const newCulture = this.cultureValues.get(newCultureName);
|
|
|
|
if (!newCulture) {
|
2019-01-18 18:03:45 +01:00
|
|
|
console.error(`unsupported culture given: ${newCultureName}`); //TODO: throw error?
|
2018-10-05 17:00:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentCulture = newCulture;
|
|
|
|
this.cultureChangeSubject.next(newCulture);
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
// Set angular locale based on user selection.
|
2019-01-18 18:03:45 +01:00
|
|
|
// This is a very hacky way to map cultures with angular cultures, since there is no mapping. We first try to
|
|
|
|
// use the culture with the specialization (ex en-US), and if not exists we import the base culture (first part).
|
2018-10-05 17:00:54 +02:00
|
|
|
let locale = newCulture.name;
|
|
|
|
import(`@angular/common/locales/${locale}.js`).catch(reason => {
|
2019-01-18 18:03:45 +01:00
|
|
|
this.logger.error('Could not load locale: ' + locale);
|
2018-10-05 17:00:54 +02:00
|
|
|
}).then(selectedLocale => {
|
2019-01-18 18:03:45 +01:00
|
|
|
if (selectedLocale) {
|
|
|
|
registerLocaleData(selectedLocale.default);
|
|
|
|
} else {
|
|
|
|
locale = newCulture.name.split('-')[0];
|
|
|
|
import(`@angular/common/locales/${locale}.js`).catch(reason => {
|
|
|
|
this.logger.error('Could not load locale: ' + locale);
|
|
|
|
}).then(selectedDefaultLocale => {
|
|
|
|
registerLocaleData(selectedDefaultLocale.default);
|
|
|
|
});
|
|
|
|
}
|
2018-10-05 17:00:54 +02:00
|
|
|
});
|
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
getCultureChangeObservable(): Observable<CultureInfo> {
|
|
|
|
return this.cultureChangeSubject.asObservable();
|
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
getCurrentCulture(): CultureInfo {
|
|
|
|
return this.currentCulture;
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
}
|