Skip to content

Commit 0e9d12a

Browse files
authored
Merge pull request #24194 from abpframework/issue-#20992
Add custom i18n adapter for ng-bootstrap datepicker
2 parents 10b833c + cf2065e commit 0e9d12a

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { formatDate } from '@angular/common';
2+
import { inject, Injectable, LOCALE_ID } from '@angular/core';
3+
import { NgbDatepickerI18n, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
4+
import { ConfigStateService } from '@abp/ng.core';
5+
6+
@Injectable()
7+
export class DatepickerI18nAdapter extends NgbDatepickerI18n {
8+
private configState = inject(ConfigStateService, { optional: true });
9+
private defaultLocale = inject(LOCALE_ID);
10+
11+
private get locale(): string {
12+
return this.configState?.getDeep('localization.currentCulture.cultureName') || this.defaultLocale;
13+
}
14+
15+
getWeekdayLabel(weekday: number): string {
16+
const date = new Date(2017, 0, weekday + 1); // Monday = 1
17+
return formatDate(date, 'EEEEE', this.locale);
18+
}
19+
20+
getWeekLabel(): string {
21+
return '';
22+
}
23+
24+
getMonthShortName(month: number): string {
25+
const date = new Date(2017, month - 1, 1);
26+
return formatDate(date, 'MMM', this.locale);
27+
}
28+
29+
getMonthFullName(month: number): string {
30+
const date = new Date(2017, month - 1, 1);
31+
return formatDate(date, 'MMMM', this.locale);
32+
}
33+
34+
getDayAriaLabel(date: NgbDateStruct): string {
35+
const d = new Date(date.year, date.month - 1, date.day);
36+
return formatDate(d, 'fullDate', this.locale);
37+
}
38+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './date-time.adapter';
22
export * from './date.adapter';
3+
export * from './datepicker-i18n.adapter';
34
export * from './time.adapter';
5+
export * from './timepicker-i18n.adapter';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { formatDate } from '@angular/common';
2+
import { inject, Injectable, LOCALE_ID } from '@angular/core';
3+
import { NgbTimepickerI18n } from '@ng-bootstrap/ng-bootstrap';
4+
import { ConfigStateService } from '@abp/ng.core';
5+
6+
@Injectable()
7+
export class TimepickerI18nAdapter extends NgbTimepickerI18n {
8+
private configState = inject(ConfigStateService, { optional: true });
9+
private defaultLocale = inject(LOCALE_ID);
10+
11+
private get locale(): string {
12+
return this.configState?.getDeep('localization.currentCulture.cultureName') || this.defaultLocale;
13+
}
14+
15+
getMorningPeriod(): string {
16+
const date = new Date(2000, 0, 1, 10, 0, 0);
17+
return formatDate(date, 'a', this.locale);
18+
}
19+
20+
getAfternoonPeriod(): string {
21+
const date = new Date(2000, 0, 1, 22, 0, 0);
22+
return formatDate(date, 'a', this.locale);
23+
}
24+
}

npm/ng-packs/packages/theme-shared/src/lib/providers/ng-bootstrap-config.provider.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { inject, provideAppInitializer } from '@angular/core';
2-
import { NgbInputDatepickerConfig, NgbTypeaheadConfig } from '@ng-bootstrap/ng-bootstrap';
2+
import { NgbDatepickerI18n, NgbInputDatepickerConfig, NgbTypeaheadConfig, NgbTimepickerI18n } from '@ng-bootstrap/ng-bootstrap';
3+
import { DatepickerI18nAdapter, TimepickerI18nAdapter } from '../adapters';
34

45
export const NG_BOOTSTRAP_CONFIG_PROVIDERS = [
6+
{
7+
provide: NgbDatepickerI18n,
8+
useClass: DatepickerI18nAdapter,
9+
},
10+
{
11+
provide: NgbTimepickerI18n,
12+
useClass: TimepickerI18nAdapter,
13+
},
514
provideAppInitializer(() => {
615
configureNgBootstrap();
716
}),

0 commit comments

Comments
 (0)