ngx-translate - No provider for InjectionToken DocumentToken

北战南征 提交于 2019-12-04 19:49:06

For angular version < 4.3 requires installing this version http-loader@0.1.0 of http-loader

1) npm install @ngx-translate/http-loader@0.1.0 --save

2) npm install @ngx-translate/core --save

3) Import HttpModule and Http from @angular/http

4) Import TranslateModule, TranslateLoader, TranslateService from @ngx-translate/core

5) Import TranslateHttpLoader from @ngx-translate/http-loader

6) Export function in app.module.ts with parameter Http

export function HttpLoaderFactory(http: Http) {
  return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}

Here's what solves the issue in the function's parameter because I was using the latest version of http-loader, and I call httpClientModule & HttpClient, and it is not compatible with angular old version.

7) Last but not the least initialize object in constructor calling the service TranslateService

public constructor(public translate: TranslateService){

}

8) Finally you can use this object which you initialize it in constructor in the view(html page) like this:

 {{'HOME.HELLO' | translate }} 

Note: In json file the string (key & value) must be all capitalized.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!