mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-2699] added localisation for time ago pipe (#3298)
* [ADF-2699] added localisation to the time-ago pipe * [ADF-2699] added lang to time ago pipe * [ADF-2699] added localisation for time ago pipe * [ADF-2699] removed fdescribe * [ADF-2699] removed comments * [ADF-2699] removed useless default values
This commit is contained in:
@@ -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');
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
@@ -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 '';
|
||||
}
|
||||
|
Reference in New Issue
Block a user