+ [attr.data-automation-id]="'date_' + (data.getValue(row, col) | date: 'medium') ">
- {{value}}
+
+ {{ value | adfTimeAgo: currentLocale }}
+
+
+
+ {{ value | date:'medium' }}
+
+
`,
encapsulation: ViewEncapsulation.None,
host: { class: 'adf-date-cell' }
})
-export class DateCellComponent extends DataTableCellComponent {}
+export class DateCellComponent extends DataTableCellComponent {
+
+ currentLocale;
+
+ constructor(userPreferenceService: UserPreferencesService) {
+ super();
+
+ userPreferenceService.select(UserPreferenceValues.Locale).subscribe((locale) => {
+ this.currentLocale = locale;
+ });
+ }
+
+}
diff --git a/lib/core/datatable/data/object-datatable-adapter.ts b/lib/core/datatable/data/object-datatable-adapter.ts
index 7fbd5ab71b..dc737be03f 100644
--- a/lib/core/datatable/data/object-datatable-adapter.ts
+++ b/lib/core/datatable/data/object-datatable-adapter.ts
@@ -15,9 +15,6 @@
* limitations under the License.
*/
-import { DatePipe } from '@angular/common';
-
-import { TimeAgoPipe } from '../../pipes';
import { DataColumn } from './data-column.model';
import { DataRow } from './data-row.model';
import { ObjectDataRow } from './object-datarow.model';
@@ -107,14 +104,6 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
let value = row.getValue(col.key);
- if (col.type === 'date') {
- try {
- return this.formatDate(col, value);
- } catch (err) {
- console.error(`Error parsing date ${value} to format ${col.format}`);
- }
- }
-
if (col.type === 'icon') {
const icon = row.getValue(col.key);
return icon;
@@ -123,21 +112,6 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
return value;
}
- formatDate(col: DataColumn, value: any): string {
- if (col.type === 'date') {
- const format = col.format || 'medium';
- if (format === 'timeAgo') {
- const timeAgoPipe = new TimeAgoPipe();
- return timeAgoPipe.transform(value);
- } else {
- const datePipe = new DatePipe('en-US');
- return datePipe.transform(value, format);
- }
- }
-
- return value;
- }
-
getSorting(): DataSorting {
return this._sorting;
}
diff --git a/lib/core/pipes/time-ago.pipe.spec.ts b/lib/core/pipes/time-ago.pipe.spec.ts
index 408ad750a5..9676488a24 100644
--- a/lib/core/pipes/time-ago.pipe.spec.ts
+++ b/lib/core/pipes/time-ago.pipe.spec.ts
@@ -16,14 +16,15 @@
*/
import { TimeAgoPipe } from './time-ago.pipe';
+import { async } from '@angular/core/testing';
describe('TimeAgoPipe', () => {
let pipe: TimeAgoPipe;
- beforeEach(() => {
+ beforeEach(async(() => {
pipe = new TimeAgoPipe();
- });
+ }));
it('should return time difference for a given date', () => {
let date = new Date();
@@ -39,4 +40,13 @@ describe('TimeAgoPipe', () => {
expect(pipe.transform(null)).toBe('');
expect(pipe.transform(undefined)).toBe('');
});
+
+ describe('When a locale is given', () => {
+
+ it('should return a localised message', async(() => {
+ let date = new Date();
+ const transformedDate = pipe.transform(date, 'de');
+ expect(transformedDate).toBe('vor ein paar Sekunden');
+ }));
+ });
});
diff --git a/lib/core/pipes/time-ago.pipe.ts b/lib/core/pipes/time-ago.pipe.ts
index d412ffc774..866677ff21 100644
--- a/lib/core/pipes/time-ago.pipe.ts
+++ b/lib/core/pipes/time-ago.pipe.ts
@@ -16,7 +16,6 @@
*/
import moment from 'moment-es6';
-
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
@@ -24,11 +23,14 @@ import { Pipe, PipeTransform } from '@angular/core';
})
export class TimeAgoPipe implements PipeTransform {
- transform(value: Date) {
+ defaultLocale = 'en-US';
+
+ transform(value: Date, locale?: string) {
if (value !== null && value !== undefined ) {
+ const actualLocale = locale ? locale : this.defaultLocale;
const then = moment(value);
- const diff = moment().diff(then, 'days');
- return diff > 7 ? then.format('DD/MM/YYYY HH:mm') : then.fromNow();
+ const diff = moment().locale(actualLocale).diff(then, 'days');
+ return diff > 7 ? then.locale(actualLocale).format('DD/MM/YYYY HH:mm') : then.locale(actualLocale).fromNow();
}
return '';
}