diff --git a/src/app/modules/data/chips/add-event-popover.html b/src/app/modules/data/chips/add-event-popover.html index f45ba4a3..b215543a 100644 --- a/src/app/modules/data/chips/add-event-popover.html +++ b/src/app/modules/data/chips/add-event-popover.html @@ -30,7 +30,9 @@ {{ frequency.children[0].item.repeatFrequency - ? (frequency.children[0].item.repeatFrequency | amDuration) + ? (frequency.children[0].item.repeatFrequency + | durationLocalized: true + | sentencecase) : ('data.chips.add_events.popover.SINGLE' | translate | titlecase) }} - {{ item.repeatFrequency | amDuration }}, - {{ item.dates[0] | dateFormat: 'weekday:long' }} + + {{ + item.repeatFrequency | durationLocalized: true | sentencecase + }}, + {{ item.dates[0] | dateFormat: 'weekday:long' }} + ({{ item.dates[0] | dateFormat }} - {{ item.dates[item.dates.length - 1] | dateFormat }}) diff --git a/src/app/modules/schedule/page/grid/schedule-card.html b/src/app/modules/schedule/page/grid/schedule-card.html index d14fb5f6..a8baa1e5 100644 --- a/src/app/modules/schedule/page/grid/schedule-card.html +++ b/src/app/modules/schedule/page/grid/schedule-card.html @@ -32,7 +32,11 @@ *ngIf="scheduleEvent.dateSeries.repeatFrequency" > {{ 'schedule.card.forEach' | translate }} - {{ scheduleEvent.dateSeries.repeatFrequency | amDuration }} + {{ + scheduleEvent.dateSeries.repeatFrequency + | durationLocalized: true + | sentencecase + }} {{ 'schedule.card.until' | translate }} {{ scheduleEvent.dateSeries.dates | last | amDateFormat: 'DD. MMM YYYY' diff --git a/src/app/translation/common-string-pipes.ts b/src/app/translation/common-string-pipes.ts index be5522f1..af9b756a 100644 --- a/src/app/translation/common-string-pipes.ts +++ b/src/app/translation/common-string-pipes.ts @@ -179,6 +179,84 @@ export class OpeningHoursPipe implements PipeTransform, OnDestroy { } } +@Injectable() +@Pipe({ + name: 'durationLocalized', + pure: true, +}) +export class DurationLocalizedPipe implements PipeTransform, OnDestroy { + locale: string; + + onLangChange?: Subscription; + + value: string; + + frequencyPrefixes: {[iso6391Code: string]: string} = { + de: 'alle', + en: 'every', + es: 'cada', + pt: 'a cada', + fr: 'tous les', + cn: '每', + ru: 'kаждые', + }; + + constructor(private readonly translate: TranslateService) { + this.locale = translate.currentLang; + } + + private _dispose(): void { + if (this.onLangChange?.closed === false) { + this.onLangChange?.unsubscribe(); + } + } + + ngOnDestroy(): void { + this._dispose(); + } + + /** + * @param value An ISO 8601 duration string + * @param isFrequency Boolean indicating if this duration is to be interpreted as repeat frequency + */ + transform(value: string | unknown, isFrequency = false): string { + this.updateValue(value, isFrequency); + this._dispose(); + if (this.onLangChange?.closed === true) { + this.onLangChange = this.translate.onLangChange.subscribe( + (event: LangChangeEvent) => { + this.locale = event.lang; + this.updateValue(value, isFrequency); + }, + ); + } + + return this.value; + } + + updateValue(value: string | unknown, isFrequency = false): void { + if (typeof value !== 'string') { + logger.warn(`durationLocalized pipe unable to parse input: ${value}`); + + return; + } + + if (isFrequency) { + const fequencyPrefix = Object.keys(this.frequencyPrefixes).filter( + element => this.locale.includes(element), + ); + this.value = [ + fequencyPrefix.length > 0 + ? this.frequencyPrefixes[fequencyPrefix[0]] + : this.frequencyPrefixes.en, + moment.duration(value).humanize(), + ].join(' '); + } else { + this.value = moment.duration(value).humanize(); + } + } +} + @Injectable() @Pipe({ name: 'metersLocalized', @@ -258,7 +336,7 @@ export class MetersLocalizedPipe implements PipeTransform, OnDestroy { @Injectable() @Pipe({ name: 'numberLocalized', - pure: false, + pure: true, }) export class NumberLocalizedPipe implements PipeTransform, OnDestroy { locale: string; @@ -327,7 +405,7 @@ export class NumberLocalizedPipe implements PipeTransform, OnDestroy { @Injectable() @Pipe({ name: 'dateFormat', - pure: false, + pure: true, }) export class DateLocalizedFormatPipe implements PipeTransform, OnDestroy { locale: string; diff --git a/src/app/translation/thing-translate.module.ts b/src/app/translation/thing-translate.module.ts index b97955c8..201dbf84 100644 --- a/src/app/translation/thing-translate.module.ts +++ b/src/app/translation/thing-translate.module.ts @@ -22,6 +22,7 @@ import { SentenceCasePipe, StringSplitPipe, OpeningHoursPipe, + DurationLocalizedPipe, } from './common-string-pipes'; import { ThingTranslateDefaultParser, @@ -40,6 +41,7 @@ export interface ThingTranslateModuleConfig { @NgModule({ declarations: [ ArrayJoinPipe, + DurationLocalizedPipe, NumberLocalizedPipe, MetersLocalizedPipe, StringSplitPipe, @@ -51,6 +53,7 @@ export interface ThingTranslateModuleConfig { ], exports: [ ArrayJoinPipe, + DurationLocalizedPipe, NumberLocalizedPipe, MetersLocalizedPipe, StringSplitPipe,