From cc7205df077b2e533945e0852bc1f4f75c85f35b Mon Sep 17 00:00:00 2001 From: Andreas Loew Date: Mon, 4 Nov 2024 11:42:13 +0100 Subject: [PATCH] Using @Injectable() decorator --- projects/http-loader/src/lib/http-loader.ts | 11 +++++++++-- .../src/lib/missing-translation-handler.spec.ts | 5 ++++- .../ngx-translate/src/lib/translate.compiler.spec.ts | 2 ++ .../ngx-translate/src/lib/translate.loader.spec.ts | 2 ++ .../src/lib/translate.pipe-modules.spec.ts | 1 + .../src/lib/translate.pipe-standalone.spec.ts | 3 +++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/projects/http-loader/src/lib/http-loader.ts b/projects/http-loader/src/lib/http-loader.ts index 0bb3c53..fd9bc53 100644 --- a/projects/http-loader/src/lib/http-loader.ts +++ b/projects/http-loader/src/lib/http-loader.ts @@ -1,9 +1,16 @@ -import {HttpClient} from "@angular/common/http"; import {TranslateLoader, TranslationObject} from "@ngx-translate/core"; +import {HttpClient} from "@angular/common/http"; +import {Inject, Injectable} from "@angular/core"; import {Observable} from 'rxjs'; +@Injectable() export class TranslateHttpLoader implements TranslateLoader { - constructor(private http: HttpClient, public prefix = "/assets/i18n/", public suffix = ".json") {} + constructor( + private http: HttpClient, + @Inject(String) public prefix:string = "/assets/i18n/", + @Inject(String) public suffix:string = ".json" + ) + {} /** * Gets the translations from the server diff --git a/projects/ngx-translate/src/lib/missing-translation-handler.spec.ts b/projects/ngx-translate/src/lib/missing-translation-handler.spec.ts index 6e64bf8..c8d8c42 100644 --- a/projects/ngx-translate/src/lib/missing-translation-handler.spec.ts +++ b/projects/ngx-translate/src/lib/missing-translation-handler.spec.ts @@ -7,11 +7,12 @@ import { TranslateService, Translation, TranslationObject } from "../public-api"; -import {Type} from "@angular/core"; +import {Injectable, Type} from "@angular/core"; let translations: TranslationObject = {"TEST": "This is a test"}; const fakeTranslation: TranslationObject = {"NOT_USED": "not used"}; +@Injectable() class FakeLoader implements TranslateLoader { getTranslation(lang: string): Observable { if (lang === 'fake') { @@ -26,6 +27,7 @@ describe('MissingTranslationHandler', () => { let translate: TranslateService; let missingTranslationHandler: MissingTranslationHandler; + @Injectable() class Missing implements MissingTranslationHandler { handle(params: MissingTranslationHandlerParams) { void params; @@ -33,6 +35,7 @@ describe('MissingTranslationHandler', () => { } } + @Injectable() class MissingObs implements MissingTranslationHandler { handle(params: MissingTranslationHandlerParams): Observable { return of(`handled: ${params.key}`); diff --git a/projects/ngx-translate/src/lib/translate.compiler.spec.ts b/projects/ngx-translate/src/lib/translate.compiler.spec.ts index 91bbb19..b6e258f 100644 --- a/projects/ngx-translate/src/lib/translate.compiler.spec.ts +++ b/projects/ngx-translate/src/lib/translate.compiler.spec.ts @@ -10,9 +10,11 @@ import { Translation, provideTranslateService } from "../public-api"; +import {Injectable} from "@angular/core"; const translations: TranslationObject = {LOAD: 'This is a test'}; +@Injectable() class FakeLoader implements TranslateLoader { getTranslation(lang: string): Observable { void lang; diff --git a/projects/ngx-translate/src/lib/translate.loader.spec.ts b/projects/ngx-translate/src/lib/translate.loader.spec.ts index 3716f4a..2bd4f54 100644 --- a/projects/ngx-translate/src/lib/translate.loader.spec.ts +++ b/projects/ngx-translate/src/lib/translate.loader.spec.ts @@ -8,9 +8,11 @@ import { Translation, provideTranslateService } from "../public-api"; +import {Injectable} from "@angular/core"; const translations: TranslationObject = {"TEST": "This is a test"}; +@Injectable() class FakeLoader implements TranslateLoader { getTranslation(): Observable { return of(translations); diff --git a/projects/ngx-translate/src/lib/translate.pipe-modules.spec.ts b/projects/ngx-translate/src/lib/translate.pipe-modules.spec.ts index 652d79d..c213df6 100644 --- a/projects/ngx-translate/src/lib/translate.pipe-modules.spec.ts +++ b/projects/ngx-translate/src/lib/translate.pipe-modules.spec.ts @@ -49,6 +49,7 @@ class AppComponent { let translations: TranslationObject = {"TEST": "This is a test"}; +@Injectable() class FakeLoader implements TranslateLoader { getTranslation(lang: string): Observable { void lang; diff --git a/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts b/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts index 9f82fa9..e4591d6 100644 --- a/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts +++ b/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts @@ -58,6 +58,7 @@ class AppComponent { let translations: TranslationObject = {"TEST": "This is a test"}; +@Injectable() class FakeLoader implements TranslateLoader { getTranslation(lang: string): Observable { void lang; @@ -65,12 +66,14 @@ class FakeLoader implements TranslateLoader { } } +@Injectable() class DelayedFrenchLoader implements TranslateLoader { getTranslation(lang: string): Observable { return lang === 'fr' ? timer(10).pipe(map(() => translations)) : of(translations); } } +@Injectable() class MissingObs implements MissingTranslationHandler { handle(params: MissingTranslationHandlerParams): Translation|Observable { return timer(1).pipe(map(() => `handled: ${params.key}`));