Skip to content

Commit fd9890f

Browse files
committed
Added pipes to get localized readable date.
1 parent 496ef25 commit fd9890f

File tree

13 files changed

+145
-56
lines changed

13 files changed

+145
-56
lines changed

src/app/home/components/reading-record-dialog.component.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { MessageService } from '../../services/message.service';
1818
import {
1919
IBookWithPath,
2020
sortBy,
21-
getReadableDate,
2221
IMessage,
2322
} from '../../vendor';
2423

@@ -66,10 +65,6 @@ export class ReadingRecordDialog implements OnInit{
6665
});
6766
}
6867

69-
readableDate = (date: Date) => {
70-
return getReadableDate(date, this.i18n.browserLang);
71-
}
72-
7368
moveTo = (path: string, sectionAnchor?: string) => {
7469
const msg: IBookWithPath = {
7570
book: this.data,

src/app/home/components/reading-record.dialog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ <h2 mat-dialog-title>
1010
style="height: 40px; padding: 0">
1111
<ion-label color="dark" class="ion-float-left" style="width: 360px;">
1212
<ion-text class="ion-float-left" style="width: 350px;">
13-
<b>{{readableDate(record.dateCreated)}}</b> - <i>{{record.dateCreated | date : 'yyyy-MM-dd HH:mm:ss' : 'GMT+8' }}</i>
13+
<b>{{record.dateCreated | readableDate | async}}</b> - <i>{{record.dateCreated | date : 'yyyy-MM-dd HH:mm:ss' : 'GMT+8' }}</i>
1414
</ion-text>
1515
</ion-label>
1616
</mgl-timeline-entry-header>

src/app/home/components/search.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{{'home.search.stars' | translate}}<ion-icon name="star"></ion-icon><i>{{book.stars}}</i>
2727
</ion-label>
2828
<ion-label>
29-
{{'home.search.dateUpdated' | translate}}<i><b>{{readableDate(book.dateUpdated)}}</b> - {{book.dateUpdated | date : 'yyyy-MM-dd HH:mm:ss' : 'GMT+8' }}</i>
29+
{{'home.search.dateUpdated' | translate}}<i><b>{{book.dateUpdated | stringToDate | readableDate | async}}</b> - {{book.dateUpdated | date : 'yyyy-MM-dd HH:mm:ss' : 'GMT+8' }}</i>
3030
</ion-label>
3131
</mat-card-footer>
3232
</mat-card>

src/app/home/components/search.component.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { parseISO } from 'date-fns';
44
import {
55
ICloudBook,
66
IAddBookDialogResData,
7-
getReadableDate,
87
} from '../../vendor';
98

109
import { I18nService } from '../../i18n/i18n.service';
@@ -41,10 +40,5 @@ export class SearchComponent implements OnInit {
4140
dialogRef.afterClosed().subscribe((res: IAddBookDialogResData) => {
4241
if(res) this.book.save(res);
4342
});
44-
45-
}
46-
47-
readableDate = (date: Date) => {
48-
return getReadableDate(parseISO(date.toString()), this.i18n.browserLang);
4943
}
5044
}

src/app/home/home.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { MatPaginatorIntlHans } from '../vendor';
3030

3131
import { HomePageRoutingModule } from './home-routing.module';
3232
import { HomePage } from './home.page';
33+
import { ReadableDatePipe } from './pipes/readable-date.pipe';
34+
import { StringToDatePipe } from './pipes/string-to-date.pipe';
3335

3436
@NgModule({
3537
imports: [
@@ -50,7 +52,9 @@ import { HomePage } from './home.page';
5052
ReadmeDialog,
5153
ReadingRecordDialog,
5254
SnackbarComponent,
53-
SearchComponent
55+
SearchComponent,
56+
ReadableDatePipe,
57+
StringToDatePipe
5458
],
5559
providers: [
5660
BookService,
@@ -59,6 +63,8 @@ import { HomePage } from './home.page';
5963
CateService,
6064
OpMessageService,
6165
PrivateTokensService,
66+
ReadableDatePipe,
67+
StringToDatePipe,
6268
FetchService,
6369
{ provide: MatPaginatorIntl, useClass: MatPaginatorIntlHans}
6470
]

src/app/home/home.page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ <h2>{{'search.histories' | translate}}</h2>
143143
style="width: 95%"
144144
*ngFor="let history of searchHistory">
145145
<mat-card-title>“{{history.keywords}}”</mat-card-title>
146-
<mat-card-subtitle>@{{history.platform}} - <i>{{readableDate(history.date)}}</i></mat-card-subtitle>
146+
<mat-card-subtitle>@{{history.platform}} - <i>{{history.date | readableDate | async}}</i></mat-card-subtitle>
147147
</mat-card>
148148
</div>
149149
<mat-paginator [length]="_searchHistory.length"

src/app/home/home.page.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import {
4040
IFilter,
4141
filterFn,
4242
sortBy,
43-
getReadableDate,
4443
IMessage,
4544
IQueryResult,
4645
TAvatarIds,
@@ -369,8 +368,4 @@ export class HomePage implements OnInit, AfterViewInit {
369368
clearKeywords = () => {
370369
this.keywords = '';
371370
}
372-
373-
readableDate = (date: Date) => {
374-
return getReadableDate(date, this.i18n.browserLang);
375-
}
376371
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { TranslateService } from '@ngx-translate/core';
3+
4+
import {
5+
differenceInYears,
6+
differenceInMonths,
7+
differenceInWeeks,
8+
differenceInDays,
9+
differenceInHours,
10+
differenceInMinutes
11+
} from 'date-fns';
12+
13+
14+
@Pipe({
15+
name: 'readableDate'
16+
})
17+
export class ReadableDatePipe implements PipeTransform {
18+
constructor (private translate: TranslateService) {}
19+
20+
transform(date: Date, ...args: unknown[]): Promise<string> {
21+
const now = new Date();
22+
23+
const years = differenceInYears(now, date);
24+
if(years > 0) {
25+
return this.translate.get(years>1 ? 'readableDate.yearsAgo' : 'readableDate.yearAgo').toPromise()
26+
.then(_ => { return years + ' ' + _; });
27+
}
28+
29+
const months = differenceInMonths(now, date);
30+
if(months > 0) {
31+
return this.translate.get(months> 1 ? 'readableDate.monthsAgo' : 'readableDate.monthAgo').toPromise()
32+
.then(_ => { return months + ' ' + _; });
33+
}
34+
35+
const weeks = differenceInWeeks(now, date);
36+
if(weeks > 0) {
37+
return this.translate.get(weeks>1 ? 'readableDate.weeksAgo' : 'readableDate.weekAgo').toPromise()
38+
.then(_ => { return weeks + ' ' + _; });
39+
}
40+
41+
const days = differenceInDays(now, date);
42+
if(days > 0) {
43+
return this.translate.get(days>1 ? 'readableDate.daysAgo' : 'readableDate.dayAgo').toPromise()
44+
.then(_ => { return days + ' ' + _; });
45+
}
46+
47+
const hours = differenceInHours(now, date);
48+
if(hours > 0) {
49+
return this.translate.get(hours>1 ? 'readableDate.hoursAgo' : 'readableDate.hourAgo').toPromise()
50+
.then(_ => { return hours + ' ' + _; });
51+
}
52+
53+
const minutes = differenceInMinutes(now, date);
54+
return this.translate.get(minutes>1 ? 'readableDate.minutesAgo' : 'readableDate.minuteAgo').toPromise()
55+
.then(_ => { return minutes + ' ' + _; });
56+
}
57+
58+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
import {
4+
parseISO,
5+
} from 'date-fns';
6+
7+
8+
@Pipe({
9+
name: 'stringToDate'
10+
})
11+
export class StringToDatePipe implements PipeTransform {
12+
13+
transform(value: string, ...args: unknown[]): Date {
14+
return parseISO(value);
15+
}
16+
17+
}

src/app/i18n/i18n.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export class I18nService {
1313
) {
1414
translate.addLangs(this.languages);
1515
translate.setDefaultLang('en');
16-
translate.use(this.browserLang.match(/zh|en/) ? this.browserLang : 'en');
17-
//translate.use('en');
16+
//translate.use(this.browserLang.match(/zh|en/) ? this.browserLang : 'en');
17+
translate.use('en');
1818
}
1919

2020
get browserLang () {

0 commit comments

Comments
 (0)