Skip to content
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

Simple internalization of TimeAgoPipe #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/time-ago-pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('time-ago-pipe', () => {
clock.restore();
});
describe('output tests', function () {
let pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone);
let pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone, 'en-US');
it('\'a few seconds ago\' tests', () => {
var pastDate = new Date();
for (let i =0; i < 45; i++){
Expand Down
83 changes: 69 additions & 14 deletions time-ago-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Pipe, PipeTransform, NgZone, ChangeDetectorRef, OnDestroy} from "@angular/core";
import {Pipe, PipeTransform, NgZone, ChangeDetectorRef, OnDestroy, Inject, LOCALE_ID} from "@angular/core";
@Pipe({
name:'timeAgo',
pure:false
})
export class TimeAgoPipe implements PipeTransform, OnDestroy {
private timer: number;
constructor(private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone) {}
constructor(private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone, @Inject(LOCALE_ID) private locale: string) {}
transform(value:string) {
this.removeTimer();
let d = new Date(value);
Expand All @@ -26,27 +26,27 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
let months = Math.round(Math.abs(days/30.416));
let years = Math.round(Math.abs(days/365));
if (seconds <= 45) {
return 'a few seconds ago';
return this.formatMessage('a_few_seconds_ago', []);
} else if (seconds <= 90) {
return 'a minute ago';
return this.formatMessage('a_minute_ago', []);
} else if (minutes <= 45) {
return minutes + ' minutes ago';
return this.formatMessage('x_minutes_ago', [minutes]);
} else if (minutes <= 90) {
return 'an hour ago';
return this.formatMessage('an_hour_ago', []);
} else if (hours <= 22) {
return hours + ' hours ago';
return this.formatMessage('x_hours_ago', [hours]);
} else if (hours <= 36) {
return 'a day ago';
return this.formatMessage('a_day_ago', []);
} else if (days <= 25) {
return days + ' days ago';
return this.formatMessage('x_days_ago', [days]);
} else if (days <= 45) {
return 'a month ago';
return this.formatMessage('a_month_ago', []);
} else if (days <= 345) {
return months + ' months ago';
return this.formatMessage('x_months_ago', [months]);
} else if (days <= 545) {
return 'a year ago';
return this.formatMessage('a_year_ago', []);
} else { // (days > 545)
return years + ' years ago';
return this.formatMessage('x_years_ago', [years]);
}
}
ngOnDestroy(): void {
Expand All @@ -72,4 +72,59 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
return 3600;
}
}
}
private formatMessage(message: string, args: any[]): string {
let formatted: string = messages[message][this.locale] || messages[message]['en-US'];
for (let i = 0; i < args.length; i++) {
let regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, args[i]);
}
return formatted;
}
}

const messages = {
'a_few_seconds_ago': {
'en-US': 'a few seconds ago',
'hu-HU': 'pár másodperce'
},
'a_minute_ago': {
'en-US': 'a minute ago',
'hu-HU': 'egy perce'
},
'x_minutes_ago': {
'en-US': '{0} minutes ago',
'hu-HU': '{0} perce'
},
'an_hour_ago': {
'en-US': 'an hour ago',
'hu-HU': 'egy órája'
},
'x_hours_ago': {
'en-US': '{0} hours ago',
'hu-HU': '{0} órája'
},
'a_day_ago': {
'en-US': 'a day ago',
'hu-HU': 'egy napja'
},
'x_days_ago': {
'en-US': '{0} days ago',
'hu-HU': '{0} napja'
},
'a_month_ago': {
'en-US': 'a month ago',
'hu-HU': 'egy hónapja'
},
'x_months_ago': {
'en-US': '{0} months ago',
'hu-HU': '{0} hónapja'
},
'a_year_ago': {
'en-US': 'a year ago',
'hu-HU': 'egy éve'
},
'x_years_ago': {
'en-US': '{0} years ago',
'hu-HU': '{0} éve'
}
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"experimentalDecorators": true,
"declaration": true,
"removeComments": false,
"noImplicitAny": false
"noImplicitAny": false,
"lib": [ "es2015", "dom" ]
},
"angularCompilerOptions": {
"genDir": "compiled"
Expand Down