ngx-translate/http-loader

getTranslation not returning body of the Response object

mario-petrovic opened this issue · 7 comments

Hello,

I don't know if this is overlooked, but i see that in commit 65001abd for 1.0.0 release .json() was removed.

I guess that since http-loader is for general use, and that use is loading json files or any other REST call, which will go through http.get() call, it needs .json() to extract the data from the response.

Thank you.

Mario

It's not overlooked, the new @angular/common/http HttpClient returns json by default and the .json() call doesn't exist anymore.

But i am getting Response object back in my component when i log result:

this._translateService.use('en_US').toPromise().then((res: any) => {
      console.log(this._translateService.translations);
});

Are you using the new @angular/common/http HttpClient or not? From the docs:

NB: if you're still on Angular <4.3, please use Http from @angular/http with http-loader@0.1.0.

So if you are using the "old" @angular/http, make sure you are using http-loader@0.1.0.

Yea, i saw that, and i went for the last version with HttpClient. This is my code in the module:

import { HttpClient } from '@angular/common/http';

export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http, 'app/' , '.json');
}

@NgModule({
    imports: [
        TranslateModule.forChild({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [Http],
            },
            isolate: true
        })
    ]
})

Ahhh. I got Http left in deps. I have that custom interceptor with Http, i guess that is what was wrong.

Jep, you have to pass HttpClient in your deps list.

Thanks for your help Sam.

Cheers.

No problem, glad it works :)