ngx-translate not working in ng-bootstrap modal

2.3k views Asked by At

The ngx-translate's translate pipe does not work inside the ng-bootstrap's modal.

<div class="modal-header">
  <h3 class="modal-title">
    {{ 'MODAL.TITLE' | translate }}
  </h3>
</div>

<div class="modal-body">
</div>

<div class="modal-footer">
  <button class="btn btn-primary" type="button" (click)="activeModal.close('Close click')">
    <span class="fa fa-remove"></span> {{ 'BUTTON.CLOSE' | translate }}
  </button>
</div>

Sample screen

1

There are 1 answers

5
Maxime On

On the ngx-translate Readme, it's written :

NB: if you're still on Angular <4.3, please use Http from @angular/http with [email protected].

So what are the differences?

In Angular >= 4.3.x, instead of providing Http to the TranslateModule, you now need to provide HttpClient:

app.module.ts :

import {HttpClient, HttpClientModule} from "@angular/common/http"; //<-- before import {HttpModule, Http} from "@angular/http";
...
export function HttpLoaderFactory(httpClient: HttpClient) { //<-- before Http
    return new TranslateHttpLoader(httpClient, "i18n/", ".json");
}

@NgModule({
    imports: [
        ...
        HttpClientModule, //<-- before HttpModule
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient] //<-- before Http
          }
        })
    ],
    ...
})

I forked and fixed the Angular Demo (4.3.6) : https://plnkr.co/edit/1VZdWdQvat3lxaXYVZs3?p=preview