All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.app.testing.translate-testing.module.ts Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
import { TranslateModule, TranslateLoader, TranslatePipe, TranslateService } from '@ngx-translate/core';
import { NgModule, PipeTransform, Pipe, Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';

const TRANSLATIONS_EN = require('src/assets/i18n/en.json');

export class FakeLoader implements TranslateLoader {
    getTranslation(lang: string): Observable {
        return of(TRANSLATIONS_EN);
    }
}

@Pipe({
    name: 'translate'
})
export class TranslatePipeMock implements PipeTransform {
    public name = 'translate';

    public transform(query: string, ...args: any[]): any {
        return fromKey(query);
    }
}

@Injectable()
export class TranslateServiceStub {
    public get(key: string): Observable {
        return of(fromKey(key));
    }
}

function fromKey(key: string): string {
    let r = TRANSLATIONS_EN;
    key.split('.').forEach((s) => { if (r) r = r[s]; });
    if (r) { return r; } else { return key; }
}

@NgModule({
    declarations: [
        TranslatePipeMock
    ],
    providers: [
        { provide: TranslateService, useClass: TranslateServiceStub },
        { provide: TranslatePipe, useClass: TranslatePipeMock },
    ],
    imports: [
        TranslateModule.forRoot({
            loader: { provide: TranslateLoader, useClass: FakeLoader },
        })
    ],
    exports: [
        TranslatePipeMock,
        TranslateModule
    ]
})
export class TranslateTestingModule {

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy