I try to force datepicker in Angular 5 project generated by jHipster to use polish locale, for days of week and months. As I understand regarding to documentation:
https://ng-bootstrap.github.io/#/components/datepicker/examples#i18n
it should work with default settings, which are:
shared-common.module.ts
...
import { registerLocaleData } from '@angular/common';
import locale from '@angular/common/locales/pl';
...
providers: [
...
{
provide: LOCALE_ID,
useValue: 'pl'
},
],
...
export class TestSharedCommonModule {
constructor() {
registerLocaleData(locale);
}
}
usage
<input id="field_testDate" type="text" class="form-control" name="testDate"
ngbDatepicker #testDateDp="ngbDatepicker" [(ngModel)]="tester.testDate"
required/>
Unfortunately it doesn't work, I also tried different approaches, like:
https://angular.io/api/core/LOCALE_ID
or
https://github.com/NativeScript/nativescript-angular/issues/1147 - where I copied the locale file
Have you got any ideas what might be wrong?
UPDATE
I finally solved it like that:
datepicker-i18n.ts
import { Inject, Injectable, LOCALE_ID } from '@angular/core';
import { NgbDatepickerI18n } from '@ng-bootstrap/ng-bootstrap';
const I18N_VALUES = {
en: {
weekdays: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
pl: {
weekdays: ['Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So', 'N'],
months: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'],
}
};
@Injectable()
export class CustomDatepickerI18n extends NgbDatepickerI18n {
constructor(@Inject(LOCALE_ID) private locale: string) {
super();
}
getWeekdayShortName(weekday: number): string {
return I18N_VALUES[this.locale].weekdays[weekday - 1];
}
getMonthShortName(month: number): string {
return I18N_VALUES[this.locale].months[month - 1];
}
getMonthFullName(month: number): string {
return this.getMonthShortName(month);
}
}
shared-common.module.ts
{ provide: NgbDatepickerI18n, useClass: CustomDatepickerI18n }
Will do the job for me, but any improvement suggestions appreciated.