refactor: update to recent changes in translator

This commit is contained in:
Rainer Killinger
2021-01-13 16:47:22 +01:00
parent 456560026c
commit 3d4c476549
20 changed files with 706 additions and 330 deletions

View 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);
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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 {ModuleWithProviders, NgModule, Provider} from '@angular/core';
import {ArrayJoinPipe, NumberLocalizedPipe, StringSplitPipe} from './common-string-pipes';
import {ThingTranslateDefaultParser, ThingTranslateParser} from './thing-translate.parser';
import {ThingPropertyNameTranslatePipe, ThingTranslatePipe} from './thing-translate.pipe';
import {ThingTranslateService} from './thing-translate.service';
export interface ThingTranslateModuleConfig {
parser?: Provider;
}
@NgModule({
declarations: [
ThingTranslatePipe,
ThingPropertyNameTranslatePipe,
ArrayJoinPipe,
StringSplitPipe,
NumberLocalizedPipe,
// ThingTranslatorDirective
],
exports: [
ThingTranslatePipe,
ThingPropertyNameTranslatePipe,
ArrayJoinPipe,
StringSplitPipe,
NumberLocalizedPipe,
// ThingTranslatorDirective
],
})
export class ThingTranslateModule {
/**
* Use this method in your other (non root) modules to import the directive/pipe
*/
static forChild(config: ThingTranslateModuleConfig = {}): ModuleWithProviders<ThingTranslateModule> {
return {
ngModule: ThingTranslateModule,
providers: [
config.parser ?? {provide: ThingTranslateParser, useClass: ThingTranslateDefaultParser},
ThingTranslateService,
],
};
}
/**
* Use this method in your root module to provide the TranslatorService
*/
static forRoot(config: ThingTranslateModuleConfig = {}): ModuleWithProviders<ThingTranslateModule> {
return {
ngModule: ThingTranslateModule,
providers: [
config.parser ?? {provide: ThingTranslateParser, useClass: ThingTranslateDefaultParser},
ThingTranslateService,
],
};
}
}

View File

@@ -14,11 +14,10 @@
*/
import {Injectable} from '@angular/core';
import {isDefined} from '@ngx-translate/core/lib/util';
// tslint:disable: member-ordering prefer-function-over-method completed-docs
export abstract class TranslateParser {
export abstract class ThingTranslateParser {
/**
* Gets a value from an object by composed key
* parser.getValue({ key1: { keyA: 'valueI' }}, 'key1.keyA') ==> 'valueI'
@@ -32,7 +31,7 @@ export abstract class TranslateParser {
}
@Injectable()
export class TranslateDefaultParser extends TranslateParser {
export class ThingTranslateDefaultParser extends ThingTranslateParser {
/**
* TODO
@@ -80,3 +79,8 @@ export class TranslateDefaultParser extends TranslateParser {
return property;
}
}
// tslint:disable-next-line: no-any
export function isDefined(value: any): boolean {
return typeof value !== 'undefined' && value !== null;
}

View File

@@ -15,34 +15,34 @@
import {Injectable, OnDestroy, Pipe, PipeTransform} from '@angular/core';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {isThing, SCThings} from '@openstapps/core';
import {isThing, SCThings, SCThingType} from '@openstapps/core';
import {Subscription} from 'rxjs';
import {ThingTranslatorService} from './thing-translator.service';
import {ThingTranslateService} from './thing-translate.service';
// tslint:disable: member-ordering prefer-function-over-method completed-docs
@Injectable()
@Pipe({
name: 'translate',
name: 'thingTranslate',
pure: false, // required to update the value when the promise is resolved
})
export class ThingTranslatePipe implements PipeTransform, OnDestroy {
value = '';
value: unknown;
lastKey?: string;
lastThing: SCThings;
onLangChange: Subscription;
constructor(private readonly translate: TranslateService,
// private readonly _ref: ChangeDetectorRef,
private readonly thingTranslate: ThingTranslatorService) {
private readonly thingTranslate: ThingTranslateService) {
}
updateValue(key: string, thing: SCThings): void {
this.value = String(this.thingTranslate.get(thing, key));
this.value = this.thingTranslate.get(thing, key);
}
transform(query: string, thing: SCThings): string {
transform(query: string, thing: SCThings): unknown {
if (typeof query !== 'string' || query.length <= 0) {
return query;
}
@@ -51,20 +51,16 @@ export class ThingTranslatePipe implements PipeTransform, OnDestroy {
throw new SyntaxError(`Wrong parameter in ThingTranslatePipe. Expected a valid SCThing, received: ${thing}`);
}
// store the query, in case it changes
this.lastKey = query;
// store the params, in case they change
this.lastKey = query;
this.lastThing = thing;
// set the value
this.updateValue(query, thing);
// if there is a subscription to onLangChange, clean it
this._dispose();
// subscribe to onLangChange event, in case the language changes
if (typeof this.onLangChange === 'undefined' || this.onLangChange.closed ) {
if (this.onLangChange?.closed ?? true) {
this.onLangChange = this.translate.onLangChange.subscribe((_event: LangChangeEvent) => {
if (typeof this.lastKey === 'string') {
this.lastKey = undefined; // we want to make sure it doesn't return the same value until it's been updated
@@ -89,3 +85,68 @@ export class ThingTranslatePipe implements PipeTransform, OnDestroy {
this._dispose();
}
}
@Injectable()
@Pipe({
name: 'propertyNameTranslate',
pure: false, // required to update the value when the promise is resolved
})
export class ThingPropertyNameTranslatePipe implements PipeTransform, OnDestroy {
value: unknown;
lastKey?: string;
lastType: string;
onLangChange: Subscription;
constructor(private readonly translate: TranslateService,
// private readonly _ref: ChangeDetectorRef,
private readonly thingTranslate: ThingTranslateService) {
}
updateValue(key: string, type: string): void {
this.value = this.thingTranslate.getPropertyName(type as SCThingType, key);
}
transform(query: string, thingOrType: SCThings | string): unknown {
if (typeof query !== 'string' || query.length <= 0) {
return query;
}
if (!isThing(thingOrType) && typeof thingOrType !== 'string'){
throw new SyntaxError(`Wrong parameter in ThingTranslatePipe. Expected a valid SCThing or String, received: ${thingOrType}`);
}
// store the params, in case they change
this.lastKey = query;
this.lastType= typeof thingOrType === 'string' ? thingOrType : thingOrType.type;
this.updateValue(query, this.lastType);
// if there is a subscription to onLangChange, clean it
this._dispose();
if (this.onLangChange?.closed ?? true) {
this.onLangChange = this.translate.onLangChange.subscribe((_event: LangChangeEvent) => {
if (typeof this.lastKey === 'string') {
this.lastKey = undefined; // we want to make sure it doesn't return the same value until it's been updated
this.updateValue(query, this.lastType);
}
});
}
return this.value;
}
/**
* Clean any existing subscription to change events
*/
private _dispose(): void {
if (this.onLangChange?.closed) {
this.onLangChange?.unsubscribe();
}
}
ngOnDestroy(): void {
this._dispose();
}
}

View File

@@ -15,10 +15,9 @@
import {Injectable, OnDestroy} from '@angular/core';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {isDefined} from '@ngx-translate/core/lib/util';
import {SCLanguage, SCThings, SCThingTranslator, SCTranslations} from '@openstapps/core';
import {SCLanguage, SCThings, SCThingTranslator, SCThingType, SCTranslations} from '@openstapps/core';
import {Subscription} from 'rxjs';
import {TranslateParser} from './thing-translator.parser';
import {isDefined, ThingTranslateParser} from './thing-translate.parser';
// export const DEFAULT_LANGUAGE = new InjectionToken<string>('DEFAULT_LANGUAGE');
@@ -27,7 +26,7 @@ import {TranslateParser} from './thing-translator.parser';
@Injectable({
providedIn: 'root',
})
export class ThingTranslatorService implements OnDestroy {
export class ThingTranslateService implements OnDestroy {
onLangChange: Subscription;
translator: SCThingTranslator;
@@ -39,10 +38,9 @@ export class ThingTranslatorService implements OnDestroy {
* @param language TODO
*/
constructor(private readonly translateService: TranslateService,
public parser: TranslateParser,
language?: keyof SCTranslations<SCLanguage>) {
public parser: ThingTranslateParser){
this.translator = new SCThingTranslator(language ?? this.translateService.currentLang as keyof SCTranslations<SCLanguage>);
this.translator = new SCThingTranslator('de' ?? this.translateService.currentLang as keyof SCTranslations<SCLanguage>);
/** set the default language from configuration */
this.onLangChange = this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
@@ -63,15 +61,34 @@ export class ThingTranslatorService implements OnDestroy {
* Gets the translated value of a key (or an array of keys)
* @returns the translated key, or an object of translated keys
*/
public get(thing: SCThings, keyPath: string | string[]): string | number | boolean {
public get(thing: SCThings, keyPath: string | string[]): string | number | boolean | object {
if (!isDefined(keyPath) || keyPath.length === 0) {
throw new Error(`Parameter "key" required`);
throw new Error(`Parameter "keyPath" required`);
}
if (keyPath instanceof Array) {
return this.getParsedResult(thing, keyPath.join('.'));
return this.getParsedResult(this.translator.translate(thing), keyPath.join('.'));
}
return this.getParsedResult(thing, keyPath);
return this.getParsedResult(this.translator.translate(thing), keyPath);
}
/**
* Gets the translated value of a key (or an array of keys)
* @returns the translated key, or an object of translated keys
*/
public getPropertyName(type: SCThingType, keyPath: string | string[]): string {
const translatedPropertyNames = this.translator.translatedPropertyNames(type);
if (!isDefined(translatedPropertyNames)) {
throw new Error(`Parameter "type" is an invalid SCThingType`);
}
if (!isDefined(keyPath) || keyPath.length === 0) {
throw new Error(`Parameter "keyPath" required`);
}
if (keyPath instanceof Array) {
return this.getParsedResult(translatedPropertyNames!, keyPath.join('.'));
}
return this.getParsedResult(translatedPropertyNames!, keyPath);
}
// tslint:disable-next-line: completed-docs