Skip to content

Commit 2635cad

Browse files
jwshinjwshinjelbourn
authored andcommitted
fix(datepicker): Create a new injection token to avoid overriding LOCALE_ID (#6708)
1 parent efe483a commit 2635cad

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

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

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

9+
import {InjectionToken, LOCALE_ID} from '@angular/core';
10+
11+
/** InjectionToken for datepicker that can be used to override default locale code. */
12+
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE');
13+
14+
/** Provider for MAT_DATE_LOCALE injection token. */
15+
export const MAT_DATE_LOCALE_PROVIDER = {provide: MAT_DATE_LOCALE, useExisting: LOCALE_ID};
16+
917
/** Adapts type `D` to be usable as a date by cdk-based components that work with dates. */
1018
export abstract class DateAdapter<D> {
1119
/** The locale to use for all dates. */

src/lib/core/datetime/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {DateAdapter} from './date-adapter';
10+
import {DateAdapter, MAT_DATE_LOCALE_PROVIDER} from './date-adapter';
1111
import {NativeDateAdapter} from './native-date-adapter';
1212
import {MD_DATE_FORMATS} from './date-formats';
1313
import {MD_NATIVE_DATE_FORMATS} from './native-date-formats';
1414

15-
1615
export * from './date-adapter';
1716
export * from './date-formats';
1817
export * from './native-date-adapter';
1918
export * from './native-date-formats';
2019

2120

2221
@NgModule({
23-
providers: [{provide: DateAdapter, useClass: NativeDateAdapter}],
22+
providers: [
23+
{provide: DateAdapter, useClass: NativeDateAdapter},
24+
MAT_DATE_LOCALE_PROVIDER
25+
],
2426
})
2527
export class NativeDateModule {}
2628

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {TestBed, async, inject} from '@angular/core/testing';
2-
import {LOCALE_ID} from '@angular/core';
3-
import {NativeDateAdapter, NativeDateModule, DateAdapter} from './index';
2+
import {NativeDateAdapter, NativeDateModule, DateAdapter, MAT_DATE_LOCALE} from './index';
43
import {Platform} from '../platform/index';
54
import {DEC, FEB, JAN, MAR} from '../testing/month-constants';
5+
import {LOCALE_ID} from '@angular/core';
66

77
const SUPPORTS_INTL = typeof Intl != 'undefined';
88

@@ -334,6 +334,30 @@ describe('NativeDateAdapter', () => {
334334
});
335335

336336

337+
describe('NativeDateAdapter with MAT_DATE_LOCALE override', () => {
338+
let adapter: NativeDateAdapter;
339+
340+
beforeEach(async(() => {
341+
TestBed.configureTestingModule({
342+
imports: [NativeDateModule],
343+
providers: [{provide: MAT_DATE_LOCALE, useValue: 'da-DK'}]
344+
}).compileComponents();
345+
}));
346+
347+
beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => {
348+
adapter = d;
349+
}));
350+
351+
it('should take the default locale id from the MAT_DATE_LOCALE injection token', () => {
352+
const expectedValue = SUPPORTS_INTL ?
353+
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
354+
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
355+
356+
expect(adapter.getDayOfWeekNames('long')).toEqual(expectedValue);
357+
});
358+
359+
});
360+
337361
describe('NativeDateAdapter with LOCALE_ID override', () => {
338362
let adapter: NativeDateAdapter;
339363

@@ -348,7 +372,7 @@ describe('NativeDateAdapter with LOCALE_ID override', () => {
348372
adapter = d;
349373
}));
350374

351-
it('should take the default locale id from the LOCALE_ID injection token', () => {
375+
it('should cascade locale id from the LOCALE_ID injection token to MAT_DATE_LOCALE', () => {
352376
const expectedValue = SUPPORTS_INTL ?
353377
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
354378
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

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

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

9-
import {Inject, Injectable, Optional, LOCALE_ID} from '@angular/core';
10-
import {DateAdapter} from './date-adapter';
9+
import {Inject, Injectable, Optional} from '@angular/core';
10+
import {DateAdapter, MAT_DATE_LOCALE} from './date-adapter';
1111
import {extendObject} from '../util/object-extend';
1212

13-
1413
// TODO(mmalerba): Remove when we no longer support safari 9.
1514
/** Whether the browser supports the Intl API. */
1615
const SUPPORTS_INTL_API = typeof Intl != 'undefined';
@@ -48,13 +47,12 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
4847
return valuesArray;
4948
}
5049

51-
5250
/** Adapts the native JS Date for use with cdk-based components that work with dates. */
5351
@Injectable()
5452
export class NativeDateAdapter extends DateAdapter<Date> {
55-
constructor(@Optional() @Inject(LOCALE_ID) localeId: any) {
53+
constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: string) {
5654
super();
57-
super.setLocale(localeId);
55+
super.setLocale(matDateLocale);
5856
}
5957

6058
/**

src/lib/datepicker/datepicker.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ three pieces via injection:
116116
3. The message strings used in the datepicker's UI.
117117

118118
#### Setting the locale code
119-
By default the datepicker will use the locale code from the `LOCALE_ID` injection token from
120-
`@angular/core`. If you want to override it, you can provide a new value for the token:
119+
By default, the `MAT_DATE_LOCALE` injection token will use the existing `LOCALE_ID` locale code
120+
from `@angular/core`. If you want to override it, you can provide a new value for the
121+
`MAT_DATE_LOCALE` token:
121122

122123
```ts
123124
@NgModule({
124125
providers: [
125-
{provide: LOCALE_ID, useValue: 'en-GB'},
126+
{provide: MAT_DATE_LOCALE, useValue: 'en-GB'},
126127
],
127128
})
128129
export class MyApp {}

0 commit comments

Comments
 (0)