-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Dates respect UI language setting
Relates to #568
- Loading branch information
1 parent
7af51d0
commit dd0e73a
Showing
15 changed files
with
142 additions
and
15 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...dmin-ui/src/lib/core/src/shared/components/datetime-picker/datetime-picker.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/admin-ui/src/lib/core/src/shared/pipes/locale-date.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { LanguageCode } from '../../common/generated-types'; | ||
|
||
import { LocaleDatePipe } from './locale-date.pipe'; | ||
|
||
describe('LocaleDatePipe', () => { | ||
const testDate = new Date('2021-01-12T09:12:42'); | ||
it('medium format', () => { | ||
const pipe = new LocaleDatePipe(); | ||
expect(pipe.transform(testDate, 'medium', LanguageCode.en)).toBe('Jan 12, 2021, 9:12:42 AM'); | ||
}); | ||
it('mediumTime format', () => { | ||
const pipe = new LocaleDatePipe(); | ||
expect(pipe.transform(testDate, 'mediumTime', LanguageCode.en)).toBe('9:12:42 AM'); | ||
}); | ||
it('short format', () => { | ||
const pipe = new LocaleDatePipe(); | ||
expect(pipe.transform(testDate, 'short', LanguageCode.en)).toBe('1/12/21, 9:12 AM'); | ||
}); | ||
it('longDate format', () => { | ||
const pipe = new LocaleDatePipe(); | ||
expect(pipe.transform(testDate, 'longDate', LanguageCode.en)).toBe('January 12, 2021'); | ||
}); | ||
|
||
it('medium format German', () => { | ||
const pipe = new LocaleDatePipe(); | ||
expect(pipe.transform(testDate, 'medium', LanguageCode.de)).toBe('12. Jan. 2021, 9:12:42 AM'); | ||
}); | ||
|
||
it('medium format Chinese', () => { | ||
const pipe = new LocaleDatePipe(); | ||
expect(pipe.transform(testDate, 'medium', LanguageCode.zh)).toBe('2021年1月12日 上午9:12:42'); | ||
}); | ||
}); |
92 changes: 92 additions & 0 deletions
92
packages/admin-ui/src/lib/core/src/shared/pipes/locale-date.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { ChangeDetectorRef, OnDestroy, Optional, Pipe, PipeTransform } from '@angular/core'; | ||
import { DataService } from '@vendure/admin-ui/core'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
import { LanguageCode } from '../../common/generated-types'; | ||
|
||
/** | ||
* @description | ||
* A replacement of the Angular DatePipe which makes use of the Intl API | ||
* to format dates according to the selected UI language. | ||
*/ | ||
@Pipe({ | ||
name: 'localeDate', | ||
pure: false, | ||
}) | ||
export class LocaleDatePipe implements PipeTransform, OnDestroy { | ||
private locale: string; | ||
private readonly subscription: Subscription; | ||
|
||
constructor( | ||
@Optional() private dataService?: DataService, | ||
@Optional() changeDetectorRef?: ChangeDetectorRef, | ||
) { | ||
if (this.dataService && changeDetectorRef) { | ||
this.subscription = this.dataService.client | ||
.uiState() | ||
.mapStream(data => data.uiState.language) | ||
.subscribe(languageCode => { | ||
this.locale = languageCode.replace(/_/g, '-'); | ||
changeDetectorRef.markForCheck(); | ||
}); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
if (this.subscription) { | ||
this.subscription.unsubscribe(); | ||
} | ||
} | ||
|
||
transform(value: unknown, ...args: unknown[]): unknown { | ||
const [format, locale] = args; | ||
if (this.locale || typeof locale === 'string') { | ||
const activeLocale = typeof locale === 'string' ? locale : this.locale; | ||
const date = | ||
value instanceof Date ? value : typeof value === 'string' ? new Date(value) : undefined; | ||
if (date) { | ||
const options = this.getOptionsForFormat(typeof format === 'string' ? format : 'medium'); | ||
return new Intl.DateTimeFormat(activeLocale, options).format(date); | ||
} | ||
} | ||
} | ||
|
||
private getOptionsForFormat(dateFormat: string): Intl.DateTimeFormatOptions | undefined { | ||
switch (dateFormat) { | ||
case 'medium': | ||
return { | ||
month: 'short', | ||
year: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: true, | ||
}; | ||
case 'mediumTime': | ||
return { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: true, | ||
}; | ||
case 'longDate': | ||
return { | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
}; | ||
case 'short': | ||
return { | ||
day: 'numeric', | ||
month: 'numeric', | ||
year: '2-digit', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
hour12: true, | ||
}; | ||
default: | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dmin-ui/src/lib/order/src/components/fulfillment-detail/fulfillment-detail.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters