-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(datepicker): Create a new injection token to avoid overriding LOCALE_ID #6708
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import {TestBed, async, inject} from '@angular/core/testing'; | ||
import {LOCALE_ID} from '@angular/core'; | ||
import {NativeDateAdapter, NativeDateModule, DateAdapter} from './index'; | ||
import {NativeDateAdapter, NativeDateModule, DateAdapter, MAT_DATE_LOCALE} from './index'; | ||
import {Platform} from '../platform/index'; | ||
import {DEC, FEB, JAN, MAR} from '../testing/month-constants'; | ||
import {LOCALE_ID} from '@angular/core'; | ||
|
||
const SUPPORTS_INTL = typeof Intl != 'undefined'; | ||
|
||
|
@@ -334,6 +334,30 @@ describe('NativeDateAdapter', () => { | |
}); | ||
|
||
|
||
describe('NativeDateAdapter with MAT_DATE_LOCALE override', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we also have tests for |
||
let adapter: NativeDateAdapter; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [NativeDateModule], | ||
providers: [{provide: MAT_DATE_LOCALE, useValue: 'da-DK'}] | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => { | ||
adapter = d; | ||
})); | ||
|
||
it('should take the default locale id from the MAT_DATE_LOCALE injection token', () => { | ||
const expectedValue = SUPPORTS_INTL ? | ||
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] : | ||
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||
|
||
expect(adapter.getDayOfWeekNames('long')).toEqual(expectedValue); | ||
}); | ||
|
||
}); | ||
|
||
describe('NativeDateAdapter with LOCALE_ID override', () => { | ||
let adapter: NativeDateAdapter; | ||
|
||
|
@@ -348,7 +372,7 @@ describe('NativeDateAdapter with LOCALE_ID override', () => { | |
adapter = d; | ||
})); | ||
|
||
it('should take the default locale id from the LOCALE_ID injection token', () => { | ||
it('should cascade locale id from the LOCALE_ID injection token to MAT_DATE_LOCALE', () => { | ||
const expectedValue = SUPPORTS_INTL ? | ||
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] : | ||
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,10 @@ | |
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Inject, Injectable, Optional, LOCALE_ID} from '@angular/core'; | ||
import {Inject, Injectable, Optional, InjectionToken} from '@angular/core'; | ||
import {DateAdapter} from './date-adapter'; | ||
import {extendObject} from '../util/object-extend'; | ||
|
||
|
||
// TODO(mmalerba): Remove when we no longer support safari 9. | ||
/** Whether the browser supports the Intl API. */ | ||
const SUPPORTS_INTL_API = typeof Intl != 'undefined'; | ||
|
@@ -48,13 +47,15 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] { | |
return valuesArray; | ||
} | ||
|
||
/** InjectionToken for datepicker that can be used to override default locale code. */ | ||
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we move this to |
||
|
||
/** Adapts the native JS Date for use with cdk-based components that work with dates. */ | ||
@Injectable() | ||
export class NativeDateAdapter extends DateAdapter<Date> { | ||
constructor(@Optional() @Inject(LOCALE_ID) localeId: any) { | ||
constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: string) { | ||
super(); | ||
super.setLocale(localeId); | ||
super.setLocale(matDateLocale); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,13 +116,13 @@ three pieces via injection: | |
3. The message strings used in the datepicker's UI. | ||
|
||
#### Setting the locale code | ||
By default the datepicker will use the locale code from the `LOCALE_ID` injection token from | ||
`@angular/core`. If you want to override it, you can provide a new value for the token: | ||
|
||
By default `MAT_DATE_LOCALE` injection token will use the existing `LOCALE_ID` locale code from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: By default |
||
`@angular/core`. If you want to override it, you can provide a new value for the `MAT_DATE_LOCALE` | ||
token: | ||
```ts | ||
@NgModule({ | ||
providers: [ | ||
{provide: LOCALE_ID, useValue: 'en-GB'}, | ||
{provide: MAT_DATE_LOCALE, useValue: 'en-GB'}, | ||
], | ||
}) | ||
export class MyApp {} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should go in the
MdDatepickerModule
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jelbourn can you think of a good way to avoid putting this in
NativeDateModule
,MomentDateModule
, etc? I was thinking maybe we could put it inMdCommonModule
and have this module import that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it doesn't really belong in
MdCommonModule
since it is only used in a few components. You could create a separateMdCommonDateModule
, but that also seems like overkill. Easier thing would probably be just to make a common provider, e.g.Adding that is about the same as importing another module.