From bfdbce54c213ec412d840e6f482e9ec790e24590 Mon Sep 17 00:00:00 2001 From: apapachristou Date: Mon, 9 Nov 2020 17:14:10 +0200 Subject: [PATCH] Adds date pipe to translate date --- .../src/app/core/formatting.module.ts | 4 + .../pipes/date-time-culture-format.pipe.ts | 83 +++++++++++++++++++ .../src/app/ui/dashboard/dashboard.module.ts | 2 + .../ui/dashboard/drafts/drafts.component.html | 2 +- .../recent-edited-activity.component.html | 8 +- ...ent-edited-dataset-activity.component.html | 4 +- .../recent-edited-dmp-activity.component.html | 4 +- .../src/app/ui/dataset/dataset.module.ts | 2 + .../dataset-listing-item.component.html | 4 +- .../overview/dataset-overview.component.html | 2 +- .../dmp-listing-item.component.html | 4 +- .../dmp-listing-item.component.ts | 2 +- .../dmp/overview/dmp-overview.component.html | 2 +- ...xplore-dataset-listing-item.component.html | 2 +- 14 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 dmp-frontend/src/app/core/pipes/date-time-culture-format.pipe.ts diff --git a/dmp-frontend/src/app/core/formatting.module.ts b/dmp-frontend/src/app/core/formatting.module.ts index 91311f536..afa46884f 100644 --- a/dmp-frontend/src/app/core/formatting.module.ts +++ b/dmp-frontend/src/app/core/formatting.module.ts @@ -6,6 +6,7 @@ import { NgForLimitPipe } from './pipes/ng-for-limit.pipe'; import { TimezoneInfoDisplayPipe } from './pipes/timezone-info-display.pipe'; import { EnumUtils } from './services/utilities/enum-utils.service'; import { JsonParserPipe } from './pipes/json-parser.pipe'; +import { DateTimeCultureFormatPipe } from './pipes/date-time-culture-format.pipe'; // // @@ -19,6 +20,7 @@ import { JsonParserPipe } from './pipes/json-parser.pipe'; TimezoneInfoDisplayPipe, DateFormatPipe, DateTimeFormatPipe, + DateTimeCultureFormatPipe, JsonParserPipe ], exports: [ @@ -26,6 +28,7 @@ import { JsonParserPipe } from './pipes/json-parser.pipe'; TimezoneInfoDisplayPipe, DateFormatPipe, DateTimeFormatPipe, + DateTimeCultureFormatPipe, JsonParserPipe ], providers: [ @@ -35,6 +38,7 @@ import { JsonParserPipe } from './pipes/json-parser.pipe'; TimezoneInfoDisplayPipe, DateFormatPipe, DateTimeFormatPipe, + DateTimeCultureFormatPipe, JsonParserPipe ] }) diff --git a/dmp-frontend/src/app/core/pipes/date-time-culture-format.pipe.ts b/dmp-frontend/src/app/core/pipes/date-time-culture-format.pipe.ts new file mode 100644 index 000000000..077d09146 --- /dev/null +++ b/dmp-frontend/src/app/core/pipes/date-time-culture-format.pipe.ts @@ -0,0 +1,83 @@ +import { DatePipe, registerLocaleData } from '@angular/common'; +import { Pipe, PipeTransform } from '@angular/core'; +import { LangChangeEvent, TranslateService } from '@ngx-translate/core'; +import 'moment-timezone'; +import { CultureInfo } from '../model/culture-info'; +import { CultureService } from '../services/culture/culture-service'; +import { LanguageService } from '../services/language/language.service'; +import { LoggingService } from '../services/logging/logging-service'; +import localeEs from '@angular/common/locales/es'; +import localeDe from '@angular/common/locales/de'; +import localeTr from '@angular/common/locales/tr'; +import localeSk from '@angular/common/locales/sk'; + +const availableCultures: CultureInfo[] = require('../../../assets/localization/available-cultures.json'); + +@Pipe({ + name: 'dateTimeCultureFormatter', + pure: false +}) +export class DateTimeCultureFormatPipe implements PipeTransform { + private cultureValues = new Map(); // cultures by name + private cache; + + constructor(private datePipe: DatePipe, + private translate: TranslateService, + private languageService: LanguageService) { + + if (availableCultures) { + this.cultureValues = new Map(); + availableCultures.forEach(culture => { + this.cultureValues.set(culture.name, culture); + }); + } + + this.translate.onLangChange.subscribe(($event: LangChangeEvent) => { + (this).locale = $event.lang; + this.cache = null; + }); + } + + transform(value: any, format?: string, timezone?: string): string | null { + if (!this.cache) { + const cultureName = this.languageService.getCurrentLanguage(); + let locale: string; + switch (cultureName) { + case 'en': { + locale = this.cultureValues.get('en-US').name; + break; + } + case 'gr': { + locale = this.cultureValues.get('el-GR').name; + break; + } + case 'es': { + locale = this.cultureValues.get('es-ES').name; + registerLocaleData(localeEs); + break; + } + case 'de': { + locale = this.cultureValues.get('de-DE').name; + registerLocaleData(localeDe); + break; + } + case 'tr': { + locale = this.cultureValues.get('tr-TR').name; + registerLocaleData(localeTr); + break; + } + case 'sk': { + locale = this.cultureValues.get('sk-SK').name; + registerLocaleData(localeSk); + break; + } + default: { + locale = this.cultureValues.get('en-US').name; + break; + } + } + this.cache = this.datePipe.transform(value, format, timezone, locale); + } + return this.cache; + } +} diff --git a/dmp-frontend/src/app/ui/dashboard/dashboard.module.ts b/dmp-frontend/src/app/ui/dashboard/dashboard.module.ts index 23a6ca9a5..6f4217527 100644 --- a/dmp-frontend/src/app/ui/dashboard/dashboard.module.ts +++ b/dmp-frontend/src/app/ui/dashboard/dashboard.module.ts @@ -18,10 +18,12 @@ import { RecentEditedDatasetActivityComponent } from './recent-edited-dataset-ac import { DatasetCopyDialogModule } from '../dataset/dataset-wizard/dataset-copy-dialogue/dataset-copy-dialogue.module'; import { RecentEditedDmpActivityComponent } from './recent-edited-dmp-activity/recent-edited-dmp-activity.component'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { FormattingModule } from '@app/core/formatting.module'; @NgModule({ imports: [ CommonUiModule, + FormattingModule, DashboardRoutingModule, ExportMethodDialogModule, ConfirmationDialogModule, diff --git a/dmp-frontend/src/app/ui/dashboard/drafts/drafts.component.html b/dmp-frontend/src/app/ui/dashboard/drafts/drafts.component.html index 36b4601db..cabe660dd 100644 --- a/dmp-frontend/src/app/ui/dashboard/drafts/drafts.component.html +++ b/dmp-frontend/src/app/ui/dashboard/drafts/drafts.component.html @@ -24,7 +24,7 @@
{{'DATASET-LISTING.DATASET-DESCRIPTION' | translate}}
-
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{activity.modified | date:"longDate"}}
+
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{activity.modified | dateTimeCultureFormatter: "d MMMM y"}}
{{activity.label}}
diff --git a/dmp-frontend/src/app/ui/dashboard/recent-edited-activity/recent-edited-activity.component.html b/dmp-frontend/src/app/ui/dashboard/recent-edited-activity/recent-edited-activity.component.html index 534be1cb1..e6539d9ea 100644 --- a/dmp-frontend/src/app/ui/dashboard/recent-edited-activity/recent-edited-activity.component.html +++ b/dmp-frontend/src/app/ui/dashboard/recent-edited-activity/recent-edited-activity.component.html @@ -30,8 +30,8 @@
{{ 'DMP-LISTING.DMP' | translate }}
-
{{ 'DMP-LISTING.EDITED' | translate }}: {{ activity.modified | date: "longDate" }}
-
{{ 'DMP-LISTING.PUBLISHED' | translate }}: {{ activity.publishedAt | date: "longDate" }}
+
{{ 'DMP-LISTING.EDITED' | translate }}: {{ activity.modified | dateTimeCultureFormatter: "d MMMM y" }}
+
{{ 'DMP-LISTING.PUBLISHED' | translate }}: {{ activity.publishedAt | dateTimeCultureFormatter: "d MMMM y" }}
{{activity.title}}
@@ -101,8 +101,8 @@
{{'DATASET-LISTING.DATASET-DESCRIPTION' | translate}}
-
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{activity.modified | date:"longDate"}}
-
{{'DATASET-LISTING.STATES.PUBLISHED' | translate}}: {{activity.publishedAt | date:"longDate"}}
+
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{activity.modified | dateTimeCultureFormatter: "d MMMM y"}}
+
{{'DATASET-LISTING.STATES.PUBLISHED' | translate}}: {{activity.publishedAt | dateTimeCultureFormatter: "d MMMM y"}}
{{activity.title}}
{{activity.title}}
diff --git a/dmp-frontend/src/app/ui/dashboard/recent-edited-dataset-activity/recent-edited-dataset-activity.component.html b/dmp-frontend/src/app/ui/dashboard/recent-edited-dataset-activity/recent-edited-dataset-activity.component.html index 0ec44fdc6..7b939d657 100644 --- a/dmp-frontend/src/app/ui/dashboard/recent-edited-dataset-activity/recent-edited-dataset-activity.component.html +++ b/dmp-frontend/src/app/ui/dashboard/recent-edited-dataset-activity/recent-edited-dataset-activity.component.html @@ -25,8 +25,8 @@
{{'DATASET-LISTING.DATASET-DESCRIPTION' | translate}}
-
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{activity.modified | date:"longDate"}}
-
{{'DATASET-LISTING.STATES.PUBLISHED' | translate}}: {{activity.dmpPublishedAt | date:"longDate"}}
+
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{activity.modified | dateTimeCultureFormatter: "d MMMM y"}}
+
{{'DATASET-LISTING.STATES.PUBLISHED' | translate}}: {{activity.dmpPublishedAt | dateTimeCultureFormatter: "d MMMM y"}}
{{activity.label}}
{{activity.label}}
diff --git a/dmp-frontend/src/app/ui/dashboard/recent-edited-dmp-activity/recent-edited-dmp-activity.component.html b/dmp-frontend/src/app/ui/dashboard/recent-edited-dmp-activity/recent-edited-dmp-activity.component.html index 0bcfb4cc8..fbee89771 100644 --- a/dmp-frontend/src/app/ui/dashboard/recent-edited-dmp-activity/recent-edited-dmp-activity.component.html +++ b/dmp-frontend/src/app/ui/dashboard/recent-edited-dmp-activity/recent-edited-dmp-activity.component.html @@ -25,8 +25,8 @@
{{ 'DMP-LISTING.DMP' | translate }}
-
{{ 'DMP-LISTING.EDITED' | translate }}: {{ activity.modifiedTime | date: "longDate" }}
-
{{ 'DMP-LISTING.PUBLISHED' | translate }}: {{ activity.publishedAt | date: "longDate" }}
+
{{ 'DMP-LISTING.EDITED' | translate }}: {{ activity.modifiedTime | dateTimeCultureFormatter: "d MMMM y" }}
+
{{ 'DMP-LISTING.PUBLISHED' | translate }}: {{ activity.publishedAt | dateTimeCultureFormatter: "d MMMM y" }}
{{activity.label}}
diff --git a/dmp-frontend/src/app/ui/dataset/dataset.module.ts b/dmp-frontend/src/app/ui/dataset/dataset.module.ts index 1f99600d4..720e27218 100644 --- a/dmp-frontend/src/app/ui/dataset/dataset.module.ts +++ b/dmp-frontend/src/app/ui/dataset/dataset.module.ts @@ -29,12 +29,14 @@ import { DmpEditorComponent } from '../dmp/editor/dmp-editor.component'; import { DatasetEditorDetailsComponent } from '../dmp/editor/dataset-editor-details/dataset-editor-details.component'; import { DatasetEditorDetailsModule } from '../dmp/editor/dataset-editor-details/dataset-editor-details.module'; import { FormProgressIndicationModule } from '../misc/dataset-description-form/components/form-progress-indication/form-progress-indication.module'; +import { FormattingModule } from '@app/core/formatting.module'; @NgModule({ imports: [ CommonUiModule, CommonFormsModule, UrlListingModule, + FormattingModule, ConfirmationDialogModule, AutoCompleteModule, ExternalSourcesModule, diff --git a/dmp-frontend/src/app/ui/dataset/listing/listing-item/dataset-listing-item.component.html b/dmp-frontend/src/app/ui/dataset/listing/listing-item/dataset-listing-item.component.html index e1d2cd4bd..88a81e196 100644 --- a/dmp-frontend/src/app/ui/dataset/listing/listing-item/dataset-listing-item.component.html +++ b/dmp-frontend/src/app/ui/dataset/listing/listing-item/dataset-listing-item.component.html @@ -2,8 +2,8 @@
{{'DATASET-LISTING.DATASET-DESCRIPTION' | translate}}
-
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{dataset.modified | date:"longDate"}}
-
{{'DATASET-LISTING.STATES.PUBLISHED' | translate}}: {{dataset.dmpPublishedAt | date:"longDate"}}
+
{{'DATASET-LISTING.STATES.EDITED' | translate}}: {{dataset.modified | dateTimeCultureFormatter: "d MMMM y"}}
+
{{'DATASET-LISTING.STATES.PUBLISHED' | translate}}: {{dataset.dmpPublishedAt | dateTimeCultureFormatter: "d MMMM y"}}
{{dataset.label}}
{{dataset.label}}
diff --git a/dmp-frontend/src/app/ui/dataset/overview/dataset-overview.component.html b/dmp-frontend/src/app/ui/dataset/overview/dataset-overview.component.html index 01fccc688..a60513212 100644 --- a/dmp-frontend/src/app/ui/dataset/overview/dataset-overview.component.html +++ b/dmp-frontend/src/app/ui/dataset/overview/dataset-overview.component.html @@ -38,7 +38,7 @@ .
{{'GENERAL.STATUSES.EDIT' | translate}} : - {{dataset.modified | date:'longDate'}} + {{dataset.modified | dateTimeCultureFormatter: "d MMMM y"}}
diff --git a/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.html b/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.html index ab560e044..fc341a3ad 100644 --- a/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.html +++ b/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.html @@ -2,8 +2,8 @@
{{ 'DMP-LISTING.DMP' | translate }}
-
{{ 'DMP-LISTING.EDITED' | translate }}: {{ dmp.modifiedTime | date: "longDate" }}
-
{{ 'DMP-LISTING.PUBLISHED' | translate }}: {{ dmp.publishedAt | date: "longDate" }}
+
{{ 'DMP-LISTING.EDITED' | translate }}: {{ dmp.modifiedTime | dateTimeCultureFormatter: "d MMMM y" }}
+
{{ 'DMP-LISTING.PUBLISHED' | translate }}: {{ dmp.publishedAt | dateTimeCultureFormatter: "d MMMM y" }}
{{dmp.label}}
diff --git a/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.ts b/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.ts index d32fdc78a..619609ba7 100644 --- a/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.ts +++ b/dmp-frontend/src/app/ui/dmp/listing/listing-item/dmp-listing-item.component.ts @@ -6,7 +6,7 @@ import { Router, ActivatedRoute } from '@angular/router'; import { DatasetService } from '../../../../core/services/dataset/dataset.service'; import { AuthService } from '../../../../core/services/auth/auth.service'; import { Principal } from '../../../../core/model/auth/principal'; -import { TranslateService } from '@ngx-translate/core'; +import { LangChangeEvent, TranslateService } from '@ngx-translate/core'; import { DmpStatus } from '../../../../core/common/enum/dmp-status'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; import { DmpService } from '@app/core/services/dmp/dmp.service'; diff --git a/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.html b/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.html index 9b405d02f..1517c084e 100644 --- a/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.html +++ b/dmp-frontend/src/app/ui/dmp/overview/dmp-overview.component.html @@ -41,7 +41,7 @@
{{'GENERAL.STATUSES.EDIT' | translate}} : - {{dmp.modifiedTime | date:"longDate"}} + {{dmp.modifiedTime | dateTimeCultureFormatter: "d MMMM y"}}
diff --git a/dmp-frontend/src/app/ui/explore-dataset/listing-item/explore-dataset-listing-item.component.html b/dmp-frontend/src/app/ui/explore-dataset/listing-item/explore-dataset-listing-item.component.html index 903822e55..b3c332065 100644 --- a/dmp-frontend/src/app/ui/explore-dataset/listing-item/explore-dataset-listing-item.component.html +++ b/dmp-frontend/src/app/ui/explore-dataset/listing-item/explore-dataset-listing-item.component.html @@ -45,7 +45,7 @@
-

{{'DATASET-LISTING.COLUMNS.PUBLISHED' | translate}} {{dataset.dmpPublishedAt | date: "shortDate"}}

+

{{'DATASET-LISTING.COLUMNS.PUBLISHED' | translate}} {{dataset.dmpPublishedAt | dateTimeCultureFormatter: "d MMMM y"}}