Skip to content

Commit

Permalink
Merge branch 'main' into fix/2162-vertical-rhythm
Browse files Browse the repository at this point in the history
  • Loading branch information
imagoiq authored Dec 7, 2023
2 parents 0bb360d + e969d17 commit e9266de
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-starfishes-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-demo': patch
---

Documented how to translate specific labels on the ng-bootstrap datepicker.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h1 class="bold font-curve-large">Datepicker</h1>
<div class="alert alert-info" role="alert">
<p>
If you use internationalization, check out the custom
<a href="#internationalization">datepicker internationalization service</a>
<a href="#internationalization">datepicker internationalization service and directive</a>
below
</p>
</div>
Expand Down Expand Up @@ -186,12 +186,22 @@ <h4 class="h5 bold mt-4">datepicker-simple.component.ts</h4>

<section id="internationalization">
<p class="light font-curve-regular mt-4">
If you are using internationalization, add this service to your application for custom date
translations.
If you are using internationalization, add this service and directive to your application for
custom date translations. Note that the directive need to be imported every time you use the
datepicker. It's advised to wrap the datepicker and the directive inside a module to be able to
share more easily.
</p>
<h4 class="h5 bold mt-4">datepicker.localization.service.ts</h4>
<code
class="block"
[highlight]="localizationService"
[languages]="['typescript', 'javascript']"
></code>

<h4 class="h5 bold mt-4">datepicker.title-localization.directive.ts</h4>
<code
class="block"
[highlight]="localizationDirective"
[languages]="['typescript', 'javascript']"
></code>
</section>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { UntypedFormGroup, UntypedFormBuilder } from '@angular/forms';
import { I18n } from './datepicker-localization.service';

const dpSimpleHtml =
require('!!raw-loader!../datepicker-simple/datepicker-simple.component.html').default;
const dpSimpleHtmlLg =
Expand All @@ -14,6 +15,8 @@ const dpSimpleTs =
const dpValidationTs =
require('!!raw-loader!../datepicker-validation/datepicker-validation-demo.component.ts').default;
const localizationService = require('!!raw-loader!./datepicker-localization.service.ts').default;
const localizationDirective =
require('!!raw-loader!./datepicker-title-localization.directive.ts').default;

@Component({
selector: 'app-datepicker-demo-page',
Expand All @@ -27,6 +30,7 @@ export class NgbDatepickerDemoPageComponent implements OnInit {
dpSimpleTs = dpSimpleTs;
dpValidationTs = dpValidationTs;
localizationService = localizationService;
localizationDirective = localizationDirective;

form: UntypedFormGroup;
sizes = ['sm', 'rg', 'md', 'lg'];
Expand All @@ -44,15 +48,19 @@ export class NgbDatepickerDemoPageComponent implements OnInit {
get sizeRangeSimple() {
return this.form.get('sizeRangeSimple');
}

get dpNavigation() {
return this.form.get('dpNavigation');
}

get sizeRange() {
return this.form.get('sizeRange');
}

get dpValidationSize() {
return this.sizes[this.form.get('sizeRange').value];
}

get dpSimpleSize() {
return this.sizes[this.form.get('sizeRangeSimple').value];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { NgbDatepickerI18n, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';

const I18N_VALUES = {
export const I18N_VALUES = {
fr: {
weekdays: ['Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa', 'Di'],
months: [
Expand Down Expand Up @@ -32,6 +32,10 @@ const I18N_VALUES = {
'Nov.',
'Déc.',
],
selectMonth: 'Sélectionner le mois',
selectYear: "Sélectionner l'année",
previousMonth: 'Mois précédent',
nextMonth: 'Mois suivant',
},
it: {
weekdays: ['Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa', 'Do'],
Expand Down Expand Up @@ -63,6 +67,10 @@ const I18N_VALUES = {
'Nov.',
'Dic.',
],
selectMonth: 'Selezionare il mese',
selectYear: "Selezionare l'anno",
previousMonth: 'Mese precedente',
nextMonth: 'Il prossimo mese',
},
en: {
weekdays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
Expand Down Expand Up @@ -94,6 +102,10 @@ const I18N_VALUES = {
'Nov.',
'Dec.',
],
selectMonth: 'Select Month',
selectYear: 'Select Year',
previousMonth: 'Previous Month',
nextMonth: 'Next Month',
},

de: {
Expand Down Expand Up @@ -126,6 +138,10 @@ const I18N_VALUES = {
'Nov.',
'Dez.',
],
selectMonth: 'Monat wählen',
selectYear: 'Jahr wählen',
previousMonth: 'Vorheriger Monat',
nextMonth: 'Nächsten Monat',
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DatepickerModule } from '../datepicker.module';
import { DatepickerSimpleLgComponent } from '../datepicker-simple/datepicker-simple-lg.component';
import { By } from '@angular/platform-browser';
import { I18N_VALUES } from './datepicker-localization.service';

describe('DatepickerTitleLocalizationDirective', () => {
let fixture: ComponentFixture<DatepickerSimpleLgComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [DatepickerModule],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(DatepickerSimpleLgComponent);
fixture.detectChanges();
});

it('should localize previous month title and aria attribute', () => {
fixture.debugElement
.query(By.css('.ngb-dp-open'))
.nativeElement.dispatchEvent(new Event('click'));
expect(
fixture.debugElement.query(By.css(`[aria-label="${I18N_VALUES.de.previousMonth}"]`)),
).toBeTruthy();
expect(
fixture.debugElement.query(By.css(`[title="${I18N_VALUES.de.previousMonth}"]`)),
).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Directive } from '@angular/core';
import { NgbInputDatepicker } from '@ng-bootstrap/ng-bootstrap';
import { I18n, I18N_VALUES } from './datepicker-localization.service';

@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
selector: '[ngbDatepicker]',
})
export class DatepickerTitleLocalizationDirective {
constructor(private _i18n: I18n, private datepicker: NgbInputDatepicker) {
const previousToggle = this.datepicker.toggle;
this.datepicker.toggle = () => {
previousToggle.bind(this.datepicker)();
if (this.datepicker.isOpen()) {
this.swapTitles();
}
};
}

swapTitles(): void {
// @ts-ignore
const el = this.datepicker._cRef.location.nativeElement as HTMLElement;
const localization = I18N_VALUES[this._i18n.language];

const selectMonth = el.querySelector('select[title="Select month"]');
selectMonth?.setAttribute('title', localization.selectMonth);
selectMonth?.setAttribute('aria-label', localization.selectMonth);

const selectYear = el.querySelector('select[title="Select year"]');
selectYear?.setAttribute('title', localization.selectYear);
selectYear?.setAttribute('aria-label', localization.selectYear);

const previousMonth = el.querySelector('button[title="Previous month"]');
previousMonth.setAttribute('title', localization.previousMonth);
previousMonth.setAttribute('aria-label', localization.previousMonth);

const nextMonth = el.querySelector('button[title="Next month"]');
nextMonth.setAttribute('title', localization.nextMonth);
nextMonth.setAttribute('aria-label', localization.nextMonth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DatepickerSimpleComponent } from './datepicker-simple/datepicker-simple
import { DatepickerValidationLgComponent } from './datepicker-validation/datepicker-validation-lg.component';
import { DatepickerValidationComponent } from './datepicker-validation/datepicker-validation.component';
import { DatepickerValidationDemoComponent } from './datepicker-validation/datepicker-validation-demo.component';
import { DatepickerTitleLocalizationDirective } from './datepicker-demo-page/datepicker-title-localization.directive';

@NgModule({
imports: [
Expand All @@ -29,6 +30,7 @@ import { DatepickerValidationDemoComponent } from './datepicker-validation/datep
DatepickerSimpleDemoComponent,
DatepickerSimpleComponent,
DatepickerSimpleLgComponent,
DatepickerTitleLocalizationDirective,
DatepickerValidationDemoComponent,
DatepickerValidationComponent,
DatepickerValidationLgComponent,
Expand Down

0 comments on commit e9266de

Please sign in to comment.