Skip to content

Commit c09e8a7

Browse files
crisbetommalerba
authored andcommitted
feat(native-date-adapter): use default locale from LOCALE_ID (#5419)
Uses the `LOCALE_ID` from `@angular/core` to determine the default locale in the `NativeDateAdapter`. BREAKING CHANGE: Until now, the `NativeDateAdapter` was using the browser locale, however the `LOCALE_ID` appears to default to `en-US`. Fixes #5393.
1 parent 6583505 commit c09e8a7

File tree

6 files changed

+64
-33
lines changed

6 files changed

+64
-33
lines changed

src/lib/core/datetime/native-date-adapter.spec.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
import {NativeDateAdapter} from './native-date-adapter';
1+
import {TestBed, async, inject} from '@angular/core/testing';
2+
import {LOCALE_ID} from '@angular/core';
3+
import {NativeDateAdapter, NativeDateModule, DateAdapter} from './index';
24
import {Platform} from '../platform/index';
35
import {DEC, FEB, JAN, MAR} from '../testing/month-constants';
46

57
const SUPPORTS_INTL = typeof Intl != 'undefined';
68

79
describe('NativeDateAdapter', () => {
8-
let adapter;
9-
let platform;
10+
const platform = new Platform();
11+
let adapter: NativeDateAdapter;
1012

11-
beforeEach(() => {
12-
adapter = new NativeDateAdapter();
13-
platform = new Platform();
14-
});
13+
beforeEach(async(() => {
14+
TestBed.configureTestingModule({
15+
imports: [NativeDateModule]
16+
}).compileComponents();
17+
}));
18+
19+
beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => {
20+
adapter = d;
21+
}));
1522

1623
it('should get year', () => {
1724
expect(adapter.getYear(new Date(2017, JAN, 1))).toBe(2017);
@@ -195,9 +202,9 @@ describe('NativeDateAdapter', () => {
195202

196203
it('should format', () => {
197204
if (SUPPORTS_INTL) {
198-
expect(adapter.format(new Date(2017, JAN, 1))).toEqual('1/1/2017');
205+
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('1/1/2017');
199206
} else {
200-
expect(adapter.format(new Date(2017, JAN, 1))).toEqual('Sun Jan 01 2017');
207+
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('Sun Jan 01 2017');
201208
}
202209
});
203210

@@ -222,12 +229,12 @@ describe('NativeDateAdapter', () => {
222229
if (SUPPORTS_INTL) {
223230
// Edge & IE use a different format in Japanese.
224231
if (platform.EDGE || platform.TRIDENT) {
225-
expect(adapter.format(new Date(2017, JAN, 1))).toEqual('2017年1月1日');
232+
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('2017年1月1日');
226233
} else {
227-
expect(adapter.format(new Date(2017, JAN, 1))).toEqual('2017/1/1');
234+
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('2017/1/1');
228235
}
229236
} else {
230-
expect(adapter.format(new Date(2017, JAN, 1))).toEqual('Sun Jan 01 2017');
237+
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('Sun Jan 01 2017');
231238
}
232239
});
233240

@@ -290,3 +297,28 @@ describe('NativeDateAdapter', () => {
290297
.toEqual(new Date(2018, FEB, 1));
291298
});
292299
});
300+
301+
302+
describe('NativeDateAdapter with LOCALE_ID override', () => {
303+
let adapter: NativeDateAdapter;
304+
305+
beforeEach(async(() => {
306+
TestBed.configureTestingModule({
307+
imports: [NativeDateModule],
308+
providers: [{provide: LOCALE_ID, useValue: 'da-DK'}]
309+
}).compileComponents();
310+
}));
311+
312+
beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => {
313+
adapter = d;
314+
}));
315+
316+
it('should take the default locale id from the LOCALE_ID injection token', () => {
317+
const expectedValue = SUPPORTS_INTL ?
318+
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
319+
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
320+
321+
expect(adapter.getDayOfWeekNames('long')).toEqual(expectedValue);
322+
});
323+
324+
});

src/lib/core/datetime/native-date-adapter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {Inject, Injectable, Optional, LOCALE_ID} from '@angular/core';
910
import {DateAdapter} from './date-adapter';
1011

1112

@@ -48,7 +49,13 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
4849

4950

5051
/** Adapts the native JS Date for use with cdk-based components that work with dates. */
52+
@Injectable()
5153
export class NativeDateAdapter extends DateAdapter<Date> {
54+
constructor(@Optional() @Inject(LOCALE_ID) localeId: any) {
55+
super();
56+
super.setLocale(localeId);
57+
}
58+
5259
getYear(date: Date): number {
5360
return date.getFullYear();
5461
}

src/lib/datepicker/datepicker.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ three pieces via injection:
104104
2. The display and parse formats used by the datepicker.
105105
3. The message strings used in the datepicker's UI.
106106

107+
#### Setting the locale code
108+
By default the datepicker will use the locale code from the `LOCALE_ID` injection token from
109+
`@angular/core`. If you want to override it, you can provide a new value for the token:
110+
111+
```ts
112+
@NgModule({
113+
providers: [
114+
{provide: LOCALE_ID, useValue: 'en-GB'},
115+
],
116+
})
117+
export class MyApp {}
118+
```
119+
107120
#### Choosing a date implementation and date format settings
108121
The datepicker was built to be date implementation agnostic. This means that it can be made to work
109122
with a variety of different date implementations. However it also means that developers need to make

src/lib/datepicker/datepicker.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ describe('MdDatepicker', () => {
2424
NoopAnimationsModule,
2525
ReactiveFormsModule,
2626
],
27-
providers: [
28-
{provide: DateAdapter, useFactory: () => {
29-
let adapter = new NativeDateAdapter();
30-
adapter.setLocale('en-US');
31-
return adapter;
32-
}},
33-
],
3427
declarations: [
3528
DatepickerWithFilterAndValidation,
3629
DatepickerWithFormControl,

src/lib/datepicker/month-view.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ describe('MdMonthView', () => {
1212
imports: [
1313
MdNativeDateModule,
1414
],
15-
providers: [
16-
{provide: DateAdapter, useFactory: () => {
17-
let adapter = new NativeDateAdapter();
18-
adapter.setLocale('en-US');
19-
return adapter;
20-
}}
21-
],
2215
declarations: [
2316
MdCalendarBody,
2417
MdMonthView,

src/lib/datepicker/year-view.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ describe('MdYearView', () => {
1212
imports: [
1313
MdNativeDateModule,
1414
],
15-
providers: [
16-
{provide: DateAdapter, useFactory: () => {
17-
let adapter = new NativeDateAdapter();
18-
adapter.setLocale('en-US');
19-
return adapter;
20-
}}
21-
],
2215
declarations: [
2316
MdCalendarBody,
2417
MdYearView,

0 commit comments

Comments
 (0)