Skip to content

Commit f7dd874

Browse files
committed
refactor(service): made instants functionality more readable
1 parent 84c763b commit f7dd874

File tree

7 files changed

+168
-64
lines changed

7 files changed

+168
-64
lines changed

projects/ngx-translate/src/lib/translate.service.ts

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const makeObservable = <T>(value: T | Observable<T>): Observable<T> => {
5454
return isObservable(value) ? value : of(value);
5555
};
5656

57+
58+
5759
@Injectable()
5860
export class TranslateService implements ITranslateService {
5961
protected loadingTranslations!: Observable<InterpolatableTranslationObject>;
@@ -457,23 +459,26 @@ export class TranslateService implements ITranslateService {
457459
key: string | string[],
458460
interpolateParams?: InterpolationParameters,
459461
): Translation {
462+
460463
if (!isDefinedAndNotNull(key) || key.length === 0) {
461464
return "";
462465
}
463466

464467
const result = this.getParsedResult(key, interpolateParams);
465468

466-
if (isObservable(result)) {
467-
if (Array.isArray(key)) {
468-
return key.reduce((acc: Record<string, string>, currKey: string) => {
469-
acc[currKey] = currKey;
470-
return acc;
471-
}, {});
472-
}
473-
return key;
474-
}
469+
return isObservable(result) ? this.keyToObject(key) : result;
470+
}
475471

476-
return result;
472+
473+
private keyToObject(key: string | string[])
474+
{
475+
if (Array.isArray(key)) {
476+
return key.reduce((acc: Record<string, string>, currKey: string) => {
477+
acc[currKey] = currKey;
478+
return acc;
479+
}, {});
480+
}
481+
return key;
477482
}
478483

479484
/**
@@ -597,3 +602,102 @@ export class TranslateService implements ITranslateService {
597602
}
598603

599604
}
605+
606+
@Injectable()
607+
export class ChildTranslateService extends TranslateService {
608+
609+
protected parent:TranslateService = inject(TranslateService, {skipSelf:true});
610+
611+
public override use(lang: Language): Observable<InterpolatableTranslationObject>
612+
{
613+
return this.parent.use(lang);
614+
}
615+
616+
public override setFallbackLang(lang: Language): Observable<InterpolatableTranslationObject>
617+
{
618+
return this.parent.setFallbackLang(lang);
619+
}
620+
621+
public override getFallbackLang(): Language | null
622+
{
623+
return this.parent.getFallbackLang();
624+
}
625+
626+
627+
public override get onLangChange(): Observable<TranslationChangeEvent> {
628+
return this.parent.onLangChange;
629+
}
630+
631+
public override get onFallbackLangChange(): Observable<TranslationChangeEvent> {
632+
return this.parent.onFallbackLangChange;
633+
}
634+
635+
public override addLangs(languages: Language[]): void
636+
{
637+
return this.parent.addLangs(languages);
638+
}
639+
640+
public override getLangs(): readonly Language[]
641+
{
642+
return this.parent.getLangs();
643+
}
644+
645+
646+
/*
647+
public override get onTranslationChange(): Observable<TranslationChangeEvent> {
648+
649+
// local and parent
650+
}
651+
652+
*/
653+
654+
/*
655+
656+
public abstract reloadLang(lang: Language): Observable<InterpolatableTranslationObject>;
657+
public abstract resetLang(lang: Language): void;
658+
659+
*/
660+
public override instant(
661+
key: string | string[],
662+
interpolateParams?: InterpolationParameters,
663+
): Translation
664+
{
665+
return super.instant(key, interpolateParams) || this.parent.instant(key, interpolateParams);
666+
}
667+
668+
/*
669+
public abstract stream(
670+
key: string | string[],
671+
interpolateParams?: InterpolationParameters,
672+
): Observable<Translation>;
673+
674+
public abstract getStreamOnTranslationChange(
675+
key: string | string[],
676+
interpolateParams?: InterpolationParameters,
677+
): Observable<Translation>;
678+
679+
public abstract get(
680+
key: string | string[],
681+
interpolateParams?: InterpolationParameters,
682+
): Observable<Translation>;
683+
684+
public abstract getParsedResult(
685+
key: string | string[],
686+
interpolateParams?: InterpolationParameters,
687+
): StrictTranslation | Observable<StrictTranslation>;
688+
689+
690+
691+
public abstract set(
692+
key: string,
693+
translation: string | TranslationObject,
694+
lang?: Language,
695+
): void;
696+
697+
public abstract setTranslation(
698+
lang: Language,
699+
translations: TranslationObject,
700+
shouldMerge?: boolean,
701+
): void;
702+
*/
703+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"demo": {
3-
"title": "Extended: Test Anwendung (overwrites to global)"
4-
},
5-
"standalone-component": {
6-
"title": "Extended: Eigenständige Komponente (overwrites to global)",
7-
"extended": "Extended: Nur Lehsbar durch Extended"
8-
}
2+
"demo": {
3+
"title": "Extended: Test Anwendung (overwrites to global)"
4+
},
5+
"standalone-component": {
6+
"title": "Extended: Eigenständige Komponente (overwrites to global)",
7+
"extended": "Extended: Nur Lehsbar durch Extended"
8+
}
99
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"demo": {
3-
"title": "Extended: Test Application (overwrites to global)"
4-
},
5-
"standalone-component": {
6-
"title": "Extended: Standalone Component (overwrites to global)",
7-
"extended": "Extended: Readable only by extended"
8-
}
2+
"demo": {
3+
"title": "Extended: Test Application (overwrites to global)"
4+
},
5+
"standalone-component": {
6+
"title": "Extended: Standalone Component (overwrites to global)",
7+
"extended": "Extended: Readable only by extended"
8+
}
99
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"demo": {
3-
"simple": {
4-
"text-as-attribute": "Isolated: Text als Attribut",
5-
"text-as-content": "Isolated: Text als Inhalt",
6-
"text-as-pipe-with-params": "Isolated: Text als Pipe mit {{value}}"
2+
"demo": {
3+
"simple": {
4+
"text-as-attribute": "Isolated: Text als Attribut",
5+
"text-as-content": "Isolated: Text als Inhalt",
6+
"text-as-pipe-with-params": "Isolated: Text als Pipe mit {{value}}"
7+
},
8+
"title": "Isolated: Test Anwendung"
79
},
8-
"title": "Isolated: Test Anwendung"
9-
},
10-
"standalone-component": {
11-
"title": "Isolated: Eigenständige Komponente"
12-
}
10+
"standalone-component": {
11+
"title": "Isolated: Eigenständige Komponente"
12+
}
1313
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"demo": {
3-
"simple": {
4-
"text-as-attribute": "Isolated: Text as attribute",
5-
"text-as-content": "Isolated: Text as content",
6-
"text-as-pipe-with-params": "Isolated: Text as pipe with {{value}}"
2+
"demo": {
3+
"simple": {
4+
"text-as-attribute": "Isolated: Text as attribute",
5+
"text-as-content": "Isolated: Text as content",
6+
"text-as-pipe-with-params": "Isolated: Text as pipe with {{value}}"
7+
},
8+
"title": "Isolated: Test Application"
79
},
8-
"title": "Isolated: Test Application"
9-
},
10-
"standalone-component": {
11-
"title": "Isolated: Standalone Component"
12-
}
10+
"standalone-component": {
11+
"title": "Isolated: Standalone Component"
12+
}
1313
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"demo": {
3-
"simple": {
4-
"text-as-attribute": "Global: Text als Attribut",
5-
"text-as-content": "Global: Text als Inhalt",
6-
"text-as-pipe-with-params": "Global: Text als Pipe mit {{value}}"
2+
"demo": {
3+
"simple": {
4+
"text-as-attribute": "Global: Text als Attribut",
5+
"text-as-content": "Global: Text als Inhalt",
6+
"text-as-pipe-with-params": "Global: Text als Pipe mit {{value}}"
7+
},
8+
"title": "Global: Test Anwendung"
79
},
8-
"title": "Global: Test Anwendung"
9-
},
10-
"standalone-component": {
11-
"title": "Global: Eigenständige Komponente"
12-
}
10+
"standalone-component": {
11+
"title": "Global: Eigenständige Komponente"
12+
}
1313
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"demo": {
3-
"simple": {
4-
"text-as-attribute": "Global: Text as attribute",
5-
"text-as-content": "Global: Text as content",
6-
"text-as-pipe-with-params": "Global: Text as pipe with {{value}}"
2+
"demo": {
3+
"simple": {
4+
"text-as-attribute": "Global: Text as attribute",
5+
"text-as-content": "Global: Text as content",
6+
"text-as-pipe-with-params": "Global: Text as pipe with {{value}}"
7+
},
8+
"title": "Global: Test Application"
79
},
8-
"title": "Global: Test Application"
9-
},
10-
"standalone-component": {
11-
"title": "Global: Standalone Component"
12-
}
10+
"standalone-component": {
11+
"title": "Global: Standalone Component"
12+
}
1313
}

0 commit comments

Comments
 (0)