improved date formatting and translation handling

This commit is contained in:
Chneemann 2024-06-14 20:45:40 +02:00
parent 2ec5036348
commit 27a148afcf
4 changed files with 78 additions and 29 deletions

11
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,11 @@
{
"cSpell.words": [
"friday",
"monday",
"saturday",
"sunday",
"thursday",
"tuesday",
"wednesday"
]
}

View file

@ -14,6 +14,7 @@ import { ChatMsgBoxComponent } from '../chat-msg-box/chat-msg-box.component';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { ChannelService } from '../../../service/channel.service'; import { ChannelService } from '../../../service/channel.service';
import { InfoComponent } from '../info/info.component'; import { InfoComponent } from '../info/info.component';
import { TranslateService } from '@ngx-translate/core';
@Component({ @Component({
selector: 'app-chat-content', selector: 'app-chat-content',
@ -44,6 +45,7 @@ export class ChatContentComponent implements AfterViewInit {
private chatService: ChatService, private chatService: ChatService,
private userService: UserService, private userService: UserService,
public channelService: ChannelService, public channelService: ChannelService,
private translateService: TranslateService,
private renderer: Renderer2 private renderer: Renderer2
) {} ) {}
@ -118,40 +120,24 @@ export class ChatContentComponent implements AfterViewInit {
* @param {number} timestamp - The timestamp to convert. * @param {number} timestamp - The timestamp to convert.
* @returns {string} The formatted date string. * @returns {string} The formatted date string.
*/ */
convertTimestampDate(timestamp: number) { convertTimestampDate(timestamp: number): string {
const currentDate = new Date(); const currentDate = new Date();
const inputDate = new Date(timestamp * 1000); const inputDate = new Date(timestamp * 1000);
const days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
const months = [
'Jan.',
'Feb.',
'Mar.',
'Apr.',
'May.',
'Jun.',
'Jul.',
'Aug.',
'Sep.',
'Oct.',
'Nov.',
'Dec.',
];
const dayNumber = inputDate.getDate(); const dayNumber = inputDate.getDate();
const day = days[inputDate.getDay()];
const month = months[inputDate.getMonth()]; const day = this.translateService.instant(
`weekdays.${inputDate
.toLocaleDateString('en-US', { weekday: 'long' })
.toLowerCase()}`
);
const month = this.translateService.instant(
`months.${inputDate
.toLocaleDateString('en-US', { month: 'short' })
.toLowerCase()}`
);
if (inputDate.toDateString() === currentDate.toDateString()) { if (inputDate.toDateString() === currentDate.toDateString()) {
return `Today`; return this.translateService.instant('weekdays.today');
} else { } else {
return `${day}, ${dayNumber} ${month}`; return `${day}, ${dayNumber} ${month}`;
} }

View file

@ -217,6 +217,32 @@
"message": "Direktnachrichten" "message": "Direktnachrichten"
}, },
"weekdays": {
"monday": "Montag",
"tuesday": "Dienstag",
"wednesday": "Mittwoch",
"thursday": "Donnerstag",
"friday": "Freitag",
"saturday": "Samstag",
"sunday": "Sonntag",
"today": "Heute"
},
"months": {
"jan": "Jan.",
"feb": "Feb.",
"mar": "Mär.",
"apr": "Apr.",
"may": "Mai",
"jun": "Jun.",
"jul": "Jul.",
"aug": "Aug.",
"sep": "Sep.",
"oct": "Okt.",
"nov": "Nov.",
"dec": "Dez."
},
"imprint": { "imprint": {
"headline": "Imprint", "headline": "Imprint",
"tmg": "Angaben gemäß §5 TMG", "tmg": "Angaben gemäß §5 TMG",

View file

@ -217,6 +217,32 @@
"message": "Direct Messages" "message": "Direct Messages"
}, },
"weekdays": {
"monday": "Monday",
"tuesday": "Tuesday",
"wednesday": "Wednesday",
"thursday": "Thursday",
"friday": "Friday",
"saturday": "Saturday",
"sunday": "Sunday",
"today": "Today"
},
"months": {
"jan": "Jan.",
"feb": "Feb.",
"mar": "Mar.",
"apr": "Apr.",
"may": "May",
"jun": "Jun.",
"jul": "Jul.",
"aug": "Aug.",
"sep": "Sep.",
"oct": "Oct.",
"nov": "Nov.",
"dec": "Dec."
},
"imprint": { "imprint": {
"headline": "Imprint", "headline": "Imprint",
"tmg": "Information according to §5 TMG", "tmg": "Information according to §5 TMG",