Skip to content

Commit

Permalink
feat: add locale i18n property, use Intl.DateTimeFormat for aria-labe…
Browse files Browse the repository at this point in the history
…ls (#8433)
  • Loading branch information
vursen authored Jan 3, 2025
1 parent c014f16 commit e77c513
Show file tree
Hide file tree
Showing 9 changed files with 621 additions and 194 deletions.
11 changes: 11 additions & 0 deletions packages/date-picker/src/vaadin-date-picker-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface DatePickerDate {
}

export interface DatePickerI18n {
/**
* A locale in BCP 47 format, e.g. "en-US". If not specified
* defaults to the browser's locale. Used for screen reader
* announcements.
*/
locale?: string | undefined;
/**
* An array with the full names of months starting
* with January.
Expand Down Expand Up @@ -151,6 +157,11 @@ export declare class DatePickerMixinClass {
*
* ```
* {
* // A locale in BCP 47 format, e.g. "en-US". If not specified
* // defaults to the browser's locale. Used for screen reader
* // announcements.
* locale: 'en-US',
*
* // An array with the full names of months starting
* // with January.
* monthNames: [
Expand Down
5 changes: 5 additions & 0 deletions packages/date-picker/src/vaadin-date-picker-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ export const DatePickerMixin = (subclass) =>
*
* ```
* {
* // A locale in BCP 47 format, e.g. "en-US". If not specified
* // defaults to the browser's locale. Used for screen reader
* // announcements.
* locale: 'en-US',
*
* // An array with the full names of months starting
* // with January.
* monthNames: [
Expand Down
2 changes: 1 addition & 1 deletion packages/date-picker/src/vaadin-lit-month-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MonthCalendar extends MonthCalendarMixin(ThemableMixin(PolylitMixin(LitEle
this.maxDate,
this.isDateDisabled,
)}"
aria-label="${this.__computeDayAriaLabel(date)}"
aria-label="${this.__computeDayAriaLabel(date, this.i18n)}"
>${this._getDate(date)}</td
>
`;
Expand Down
16 changes: 2 additions & 14 deletions packages/date-picker/src/vaadin-month-calendar-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,8 @@ export const MonthCalendarMixin = (superClass) =>
}

/** @protected */
__computeDayAriaLabel(date) {
if (!date) {
return '';
}

let ariaLabel = `${this._getDate(date)} ${this.i18n.monthNames[date.getMonth()]} ${date.getFullYear()}, ${
this.i18n.weekdays[date.getDay()]
}`;

if (this._isToday(date)) {
ariaLabel += `, ${this.i18n.today}`;
}

return ariaLabel;
__computeDayAriaLabel(date, i18n) {
return date ? Intl.DateTimeFormat(i18n.locale, { dateStyle: 'full' }).format(date) : '';
}

/** @private */
Expand Down
2 changes: 1 addition & 1 deletion packages/date-picker/src/vaadin-month-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MonthCalendar extends MonthCalendarMixin(ThemableMixin(PolymerElement)) {
disabled$="[[__isDayDisabled(item, minDate, maxDate, isDateDisabled)]]"
aria-selected$="[[__computeDayAriaSelected(item, selectedDate)]]"
aria-disabled$="[[__computeDayAriaDisabled(item, minDate, maxDate, isDateDisabled)]]"
aria-label$="[[__computeDayAriaLabel(item)]]"
aria-label$="[[__computeDayAriaLabel(item, i18n)]]"
>[[_getDate(item)]]</td
>
</template>
Expand Down
Loading

0 comments on commit e77c513

Please sign in to comment.