From 3cf4bbefe8e1fded35b0be0804894ce7850d54c1 Mon Sep 17 00:00:00 2001 From: Lukas Haase <lukas.haase@mni.thm.de> Date: Wed, 15 Apr 2020 13:37:04 +0200 Subject: [PATCH] de/en time format questionwall --- .../questionwall/QuestionWallComment.ts | 44 ++++++++++++++----- .../question-wall/question-wall.component.ts | 6 ++- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/app/components/shared/questionwall/QuestionWallComment.ts b/src/app/components/shared/questionwall/QuestionWallComment.ts index 8880c938b..2ba8c70b6 100644 --- a/src/app/components/shared/questionwall/QuestionWallComment.ts +++ b/src/app/components/shared/questionwall/QuestionWallComment.ts @@ -2,9 +2,33 @@ import { Comment } from '../../../models/comment'; export class QuestionWallComment { + public static readonly TIME_FORMAT_DE: string[][] = + [ + ['vor % Jahr', 'vor % Jahren'], + ['vor % Monat', 'vor % Monaten'], + ['vor % Tag', 'vor % Tagen'], + ['vor % Stunde', 'vor % Stunden'], + ['vor % Minute', 'vor % Minuten'], + ['vor % Sekunde', 'vor % Sekunden'] + ]; + public static readonly TIME_FORMAT_EN: string[][] = [ + ['% year ago', '% years ago'], + ['% month ago', '% months ago'], + ['% day ago', '% days ago'], + ['% hour ago', '% hours ago'], + ['% minute ago', '% minutes ago'], + ['% second ago', '% seconds ago'], + ]; + + public static currentTimeFormat: string[][] = QuestionWallComment.TIME_FORMAT_EN; + public date: Date; public timeAgo: string; + public static updateTimeFormat(lang: string) { + this.currentTimeFormat = this['TIME_FORMAT_' + lang.toUpperCase()]; + } + constructor( public comment: Comment, public old: boolean @@ -23,21 +47,21 @@ export class QuestionWallComment { const days = Math.floor(hours / 24); const months = Math.floor(days / 30); const years = Math.floor(months / 12); - if (this.setTimeAgo(years, 'year')) {return; } - if (this.setTimeAgo(months, 'month')) {return; } - if (this.setTimeAgo(days, 'day')) {return; } - if (this.setTimeAgo(hours, 'hour')) {return; } - if (this.setTimeAgo(minutes, 'minute')) {return; } - if (this.setTimeAgo(seconds, 'second')) {return; } - this.timeAgo = '1 second ago'; + if (this.setTime(years, QuestionWallComment.currentTimeFormat[0])) {return; } + if (this.setTime(months, QuestionWallComment.currentTimeFormat[1])) {return; } + if (this.setTime(days, QuestionWallComment.currentTimeFormat[2])) {return; } + if (this.setTime(hours, QuestionWallComment.currentTimeFormat[3])) {return; } + if (this.setTime(minutes, QuestionWallComment.currentTimeFormat[4])) {return; } + if (this.setTime(seconds, QuestionWallComment.currentTimeFormat[5])) {return; } + this.timeAgo = QuestionWallComment.currentTimeFormat[5][0].replace('%', '1'); } - private setTimeAgo(time: number, name: string): boolean { + private setTime(time: number, format: string[]): boolean { if (time > 0) { if (time === 1) { - this.timeAgo = time + ' ' + name + ' ago'; + this.timeAgo = format[0].replace('%', time + ''); } else { - this.timeAgo = time + ' ' + name + 's ago'; + this.timeAgo = format[1].replace('%', time + ''); } return true; } else { diff --git a/src/app/components/shared/questionwall/question-wall/question-wall.component.ts b/src/app/components/shared/questionwall/question-wall/question-wall.component.ts index 4aef124e1..14df46d2f 100644 --- a/src/app/components/shared/questionwall/question-wall/question-wall.component.ts +++ b/src/app/components/shared/questionwall/question-wall/question-wall.component.ts @@ -54,11 +54,15 @@ export class QuestionWallComponent implements OnInit, AfterViewInit, OnDestroy { this.timeUpdateInterval = setInterval(() => { this.comments.forEach(e => e.updateTimeAgo()); }, 15000); - this.langService.langEmitter.subscribe(lang => this.translateService.use(lang)); + this.langService.langEmitter.subscribe(lang => { + this.translateService.use(lang); + QuestionWallComment.updateTimeFormat(lang); + }); } ngOnInit(): void { // StyleDebug.border('c'); + QuestionWallComment.updateTimeFormat(localStorage.getItem('currentLang')); this.translateService.use(localStorage.getItem('currentLang')); this.commentService.getAckComments(this.roomId).subscribe(e => { e.sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()); -- GitLab