OpenSlides/client/src/app/shared/date-adapter.ts
Joshua Sangmeister 90dd843d43 added a datetimepicker to the config, including localisation.
also changed from NativeDateAdapter to MomentDateAdapter.
2019-08-27 14:36:04 +02:00

31 lines
1.2 KiB
TypeScript

import { Inject, Injectable, Optional } from '@angular/core';
import { MAT_DATE_LOCALE } from '@angular/material';
import {
MAT_MOMENT_DATE_ADAPTER_OPTIONS,
MatMomentDateAdapterOptions,
MomentDateAdapter
} from '@angular/material-moment-adapter';
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
/**
* A custom DateAdapter for the datetimepicker in the config. Uses MomentDateAdapter for localisation.
* Is needed to subscribe to language changes
*/
@Injectable()
export class OpenSlidesDateAdapter extends MomentDateAdapter {
public constructor(
translate: TranslateService,
@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string,
@Optional() @Inject(MAT_MOMENT_DATE_ADAPTER_OPTIONS) _options?: MatMomentDateAdapterOptions
) {
super(dateLocale, _options);
// subscribe to language changes to change localisation of dates accordingly
// DateAdapter seems not to be a singleton so we do that in this subclass instead of app.component
this.setLocale(translate.currentLang);
translate.onLangChange.subscribe((e: LangChangeEvent) => {
this.setLocale(e.lang);
});
}
}