I'm using NgPickDatetime in my Angular app (https://danielykpan.github.io/date-time-picker/) and I have a problem when i try to translate labels and messages. I follow the instructions in the documentation and It works fine, but the problem is when I change the site language, the labels still in the previous language.
My code is:
datetime-locale.module.ts:
import { NgModule } from '@angular/core';
import { OwlDateTimeModule, OwlNativeDateTimeModule, OwlDateTimeIntl} from 'ng-pick-datetime';
import { DefaultIntl } from './datepicker-locale.component';
@NgModule({
imports: [OwlDateTimeModule, OwlNativeDateTimeModule],
providers: [
{provide: OwlDateTimeIntl, useClass: DefaultIntl},
],
})
export class DateTimeLocaleModule {
}
datepicker-locale.component.ts:
import { OwlDateTimeIntl } from 'ng-pick-datetime';
export class DefaultIntl extends OwlDateTimeIntl {
public cancelBtnLabel = 'Annuleren';
public setBtnLabel = 'Opslaan';
private currentLang;
constructor() {
super();
this.getLang();
}
public getLang() {
this.currentLang = JSON.parse(localStorage.getItem("language"));
switch (this.currentLang.id) {
case 'en':
this.cancelBtnLabel = 'Cancel';
this.setBtnLabel = 'Set';
break;
case 'nl':
this.cancelBtnLabel = 'Annuleren';
this.setBtnLabel = 'Opslaan';
break;
default:
this.cancelBtnLabel = 'Annuleren';
this.setBtnLabel = 'Opslaan';
break;
}
}
};
So i'm trying to add ngx-translate to component and reload getLang() function when the user change language, but it's doesn't work, because I get the following error:
Uncaught Error: Can't resolve all parameters for DefaultIntl: (?).
The full code of the component with translate service is:
import { OwlDateTimeIntl } from 'ng-pick-datetime';
import { TranslateService } from '@ngx-translate/core';
export class DefaultIntl extends OwlDateTimeIntl {
public cancelBtnLabel = 'Annuleren';
public setBtnLabel = 'Opslaan';
private currentLang;
constructor(private translate: TranslateService) {
super();
this.getLang();
this.translate.onLangChange.subscribe(lang => {
this.getLang();
});
}
public getLang() {
this.currentLang = JSON.parse(localStorage.getItem("language"));
switch (this.currentLang.id) {
case 'en':
this.cancelBtnLabel = 'Cancel';
this.setBtnLabel = 'Set';
break;
case 'nl':
this.cancelBtnLabel = 'Annuleren';
this.setBtnLabel = 'Opslaan';
break;
default:
this.cancelBtnLabel = 'Annuleren';
this.setBtnLabel = 'Opslaan';
break;
}
}
};
I need to use translate service, but I can't do in the component. Is there any solution of this problem ?
Thanks!!!