mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
refactor: update to recent changes in translator
This commit is contained in:
129
src/app/translation/common-string-pipes.ts
Normal file
129
src/app/translation/common-string-pipes.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (C) 2020-2021 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
import {DecimalPipe} from '@angular/common';
|
||||
import {Injectable, OnDestroy, Pipe, PipeTransform} from '@angular/core';
|
||||
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
|
||||
import {Subscription} from 'rxjs';
|
||||
|
||||
// tslint:disable: completed-docs
|
||||
|
||||
@Injectable()
|
||||
@Pipe({
|
||||
name: 'join',
|
||||
pure: false, // required to update the value when the promise is resolved
|
||||
})
|
||||
export class ArrayJoinPipe implements PipeTransform {
|
||||
value = '';
|
||||
|
||||
|
||||
transform(anArray: [], separator: string): unknown {
|
||||
if (typeof separator !== 'string' || separator.length <= 0) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
if (!Array.isArray(anArray)){
|
||||
throw new SyntaxError(`Wrong parameter in ArrayJoinPipe. Expected a valid Array, received: ${anArray}`);
|
||||
}
|
||||
|
||||
this.value = anArray.join(separator);
|
||||
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
@Pipe({
|
||||
name: 'split',
|
||||
pure: false, // required to update the value when the promise is resolved
|
||||
})
|
||||
export class StringSplitPipe implements PipeTransform {
|
||||
value = Array<unknown>();
|
||||
|
||||
transform(aString: string, splitter: string): unknown {
|
||||
if (typeof splitter !== 'string' || splitter.length <= 0) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
if (typeof aString !== 'string'){
|
||||
throw new SyntaxError(`Wrong parameter in StringSplitPipe. Expected a valid String, received: ${aString}`);
|
||||
}
|
||||
|
||||
this.value = aString.split(splitter);
|
||||
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Injectable()
|
||||
@Pipe({
|
||||
name: 'numberLocalized',
|
||||
pure: false, // required to update the value when the promise is resolved
|
||||
})
|
||||
export class NumberLocalizedPipe implements PipeTransform, OnDestroy {
|
||||
decialPipe: DecimalPipe;
|
||||
locale: string;
|
||||
onLangChange: Subscription;
|
||||
value: unknown;
|
||||
|
||||
constructor(private readonly translate: TranslateService) {
|
||||
this.decialPipe = new DecimalPipe('de-DE');
|
||||
this.locale = translate.currentLang;
|
||||
}
|
||||
|
||||
private _dispose(): void {
|
||||
if (this.onLangChange?.closed) {
|
||||
this.onLangChange?.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this._dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value The number to be formatted.
|
||||
* @param digitsInfo Decimal representation options, specified by a string
|
||||
* in the following format: {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
|
||||
* - `minIntegerDigits`: The minimum number of integer digits before the decimal point.
|
||||
* Default is `1`.
|
||||
* - `minFractionDigits`: The minimum number of digits after the decimal point.
|
||||
* Default is `0`.
|
||||
* - `maxFractionDigits`: The maximum number of digits after the decimal point.
|
||||
* Default is `3`.
|
||||
*/
|
||||
transform(value: unknown, digitsInfo?: string | undefined): unknown {
|
||||
|
||||
this.updateValue(value, digitsInfo);
|
||||
this._dispose();
|
||||
if (typeof this.onLangChange === 'undefined' || this.onLangChange.closed ) {
|
||||
this.onLangChange = this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
|
||||
this.locale = event.lang;
|
||||
this.updateValue(value, digitsInfo);
|
||||
});
|
||||
}
|
||||
|
||||
return this.value;
|
||||
}
|
||||
|
||||
updateValue(value: unknown, digitsInfo?: string | undefined): void {
|
||||
// this.value = this.locale;
|
||||
this.value = this.decialPipe.transform(value, digitsInfo,this.locale);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user