ngx-translate / core

The internationalization (i18n) library for Angular
MIT License
4.5k stars 571 forks source link

Ngx-translate and Angular interceptor? #1312

Open Triplecorpse opened 3 years ago

Triplecorpse commented 3 years ago

Hi. I am not sure if this issue was risen. At least, I googled it and found no solution. My app causes Circular dependency when I inject translateService into http interceptor. I googled it and found it's because of HttpClient dependency. The only working solution was to inject Injector and load TranslateService with it. Nevertheless, the error still occurred but the app worked as supposed. Is it any solution for that?

saghishiri commented 2 years ago

+1

yassinumer commented 2 years ago

+1

yassinumer commented 2 years ago

I found a workaround here : https://stackoverflow.com/questions/67152273/angular-circular-dependency-when-inject-translateservice-to-interceptor

hozikm commented 1 year ago

You can break the dependency circle by changing HttpLoaderFactory dependency from HttpClient into HttpBackend and by creating new HttpClient.

export function HttpLoaderFactory(httpHandler: HttpBackend) {
  return new TranslateHttpLoader(new HttpClient(httpHandler));
}

// ... TranslateModule.forRoot ...
loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpBackend] // ←
},
NIBRAS-N commented 1 month ago

You can break the dependency circle by changing HttpLoaderFactory dependency from HttpClient into HttpBackend and by creating new HttpClient.

export function HttpLoaderFactory(httpHandler: HttpBackend) {
  return new TranslateHttpLoader(new HttpClient(httpHandler));
}

// ... TranslateModule.forRoot ...
loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpBackend] // ←
},

This saved my life..Thanks.