mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 01:22:54 +00:00
refactor: replace TSLint with ESLint
This commit is contained in:
committed by
Jovan Krunić
parent
67fb4a43c9
commit
d696215d08
@@ -15,58 +15,81 @@
|
||||
|
||||
import {Injectable, OnDestroy} from '@angular/core';
|
||||
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
|
||||
import {SCLanguage, SCLanguageCode, SCThings, SCThingTranslator, SCThingType, SCTranslations} from '@openstapps/core';
|
||||
import {
|
||||
SCLanguage,
|
||||
SCLanguageCode,
|
||||
SCThings,
|
||||
SCThingTranslator,
|
||||
SCThingType,
|
||||
SCTranslations,
|
||||
} from '@openstapps/core';
|
||||
import moment from 'moment';
|
||||
import {Subscription} from 'rxjs';
|
||||
import {isDefined, ThingTranslateParser} from './thing-translate.parser';
|
||||
|
||||
// export const DEFAULT_LANGUAGE = new InjectionToken<string>('DEFAULT_LANGUAGE');
|
||||
|
||||
// tslint:disable: member-ordering prefer-function-over-method newline-per-chained-call completed-docs
|
||||
/* eslint-disable @typescript-eslint/member-ordering, class-methods-use-this, newline-per-chained-call, */
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ThingTranslateService implements OnDestroy {
|
||||
|
||||
onLangChange: Subscription;
|
||||
|
||||
translator: SCThingTranslator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param translateService Instance of Angular TranslateService
|
||||
* @param parser An instance of the parser currently used
|
||||
* @param language TODO
|
||||
*/
|
||||
constructor(private readonly translateService: TranslateService,
|
||||
public parser: ThingTranslateParser){
|
||||
this.translator = new SCThingTranslator((translateService.currentLang ?? translateService.defaultLang) as SCLanguageCode);
|
||||
constructor(
|
||||
private readonly translateService: TranslateService,
|
||||
public parser: ThingTranslateParser,
|
||||
) {
|
||||
this.translator = new SCThingTranslator(
|
||||
(translateService.currentLang ??
|
||||
translateService.defaultLang) as SCLanguageCode,
|
||||
);
|
||||
/** set the default language from configuration */
|
||||
this.onLangChange = this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
|
||||
this.translator.language = event.lang as keyof SCTranslations<SCLanguage>;
|
||||
moment.locale(event.lang);
|
||||
});
|
||||
|
||||
this.onLangChange = this.translateService.onLangChange.subscribe(
|
||||
(event: LangChangeEvent) => {
|
||||
this.translator.language =
|
||||
event.lang as keyof SCTranslations<SCLanguage>;
|
||||
moment.locale(event.lang);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parsed result of the translations
|
||||
*/
|
||||
// tslint:disable-next-line: no-any
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types
|
||||
getParsedResult(target: object, key: string): any {
|
||||
return this.parser.getValueFromKeyPath(target, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the translated value of a key (or an array of keys)
|
||||
*
|
||||
* @param thing SCThing to get
|
||||
* @param keyPath Path to the key
|
||||
* @returns the translated key, or an object of translated keys
|
||||
*/
|
||||
public get(thing: SCThings, keyPath: string | string[]): string | number | boolean | object {
|
||||
public get(
|
||||
thing: SCThings,
|
||||
keyPath: string | string[],
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
): string | number | boolean | object {
|
||||
if (!isDefined(keyPath) || keyPath.length === 0) {
|
||||
throw new Error(`Parameter "keyPath" required`);
|
||||
}
|
||||
if (keyPath instanceof Array) {
|
||||
return this.getParsedResult(this.translator.translate(thing), keyPath.join('.'));
|
||||
if (Array.isArray(keyPath)) {
|
||||
return this.getParsedResult(
|
||||
this.translator.translate(thing),
|
||||
keyPath.join('.'),
|
||||
);
|
||||
}
|
||||
|
||||
return this.getParsedResult(this.translator.translate(thing), keyPath);
|
||||
@@ -74,24 +97,30 @@ export class ThingTranslateService implements OnDestroy {
|
||||
|
||||
/**
|
||||
* Gets the translated value of a key (or an array of keys)
|
||||
*
|
||||
* @param type Type of the property
|
||||
* @param keyPath Path to the key
|
||||
* @returns the translated key, or an object of translated keys
|
||||
*/
|
||||
public getPropertyName(type: SCThingType, keyPath: string | string[]): string {
|
||||
const translatedPropertyNames = this.translator.translatedPropertyNames(type);
|
||||
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('.'));
|
||||
if (Array.isArray(keyPath)) {
|
||||
return this.getParsedResult(translatedPropertyNames, keyPath.join('.'));
|
||||
}
|
||||
|
||||
return this.getParsedResult(translatedPropertyNames!, keyPath);
|
||||
return this.getParsedResult(translatedPropertyNames, keyPath);
|
||||
}
|
||||
|
||||
// tslint:disable-next-line: completed-docs
|
||||
ngOnDestroy() {
|
||||
if (!this.onLangChange.closed) {
|
||||
this.onLangChange.unsubscribe();
|
||||
|
||||
Reference in New Issue
Block a user