Skip to content

Commit

Permalink
Using @Injectable() decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeAndWeb committed Nov 4, 2024
1 parent 42ecef8 commit cc7205d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 deletions.
11 changes: 9 additions & 2 deletions projects/http-loader/src/lib/http-loader.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TranslationObject> {
if (lang === 'fake') {
Expand All @@ -26,13 +27,15 @@ describe('MissingTranslationHandler', () => {
let translate: TranslateService;
let missingTranslationHandler: MissingTranslationHandler;

@Injectable()
class Missing implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
void params;
return "handled";
}
}

@Injectable()
class MissingObs implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams): Observable<Translation> {
return of(`handled: ${params.key}`);
Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-translate/src/lib/translate.compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TranslationObject> {
void lang;
Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-translate/src/lib/translate.loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TranslationObject> {
return of(translations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AppComponent {

let translations: TranslationObject = {"TEST": "This is a test"};

@Injectable()
class FakeLoader implements TranslateLoader {
getTranslation(lang: string): Observable<TranslationObject> {
void lang;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ class AppComponent {

let translations: TranslationObject = {"TEST": "This is a test"};

@Injectable()
class FakeLoader implements TranslateLoader {
getTranslation(lang: string): Observable<TranslationObject> {
void lang;
return of(translations);
}
}

@Injectable()
class DelayedFrenchLoader implements TranslateLoader {
getTranslation(lang: string): Observable<TranslationObject> {
return lang === 'fr' ? timer(10).pipe(map(() => translations)) : of(translations);
}
}

@Injectable()
class MissingObs implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams): Translation|Observable<Translation> {
return timer(1).pipe(map(() => `handled: ${params.key}`));
Expand Down

0 comments on commit cc7205d

Please sign in to comment.