I realize this is a common issue and have been scouring this repo's support tickets and Stack Overflow looking for why I can't get this working. None of the many suggestions I've tried have worked. I'm confused because my setup is simple and in no way (that I can see) different from the example code setup shared in the ngx-translate repo.
I'm using Angular 5.1.0 along with Angular CLI 1.7. Webpack and ngx-translate versions are as follows:
"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1",
"webpack": "~3.8.1"
http://localhost:4200/src/assets/i18n/en.json resolves to the file correctly in the browser, so the file is in the correct location for the default configuration outlined in the ngx-translate README.
Here are the relevant files:
app.component.ts
import { ActivatedRoute } from '@angular/router';
import { Component, ElementRef, HostListener, OnInit, ViewChild, AfterViewInit, ErrorHandler } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
/*
* Main app component that houses all views.
*/
@Component({
selector: 'app-comp',
templateUrl: './app.component.html'
})
export class AppComponent {
store: any;
constructor(private route: ActivatedRoute, private translate: TranslateService) {
// translate.addLangs(['en', 'fr']);
translate.setDefaultLang('en');
}
}
app.module.ts
import { MaterialModule } from './material.module';
import { BrowserModule, Title } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpModule } from '@angular/http';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RootComponent } from './root.component';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from '../db/in-memory-data.service';
import { AppInterceptor } from './_core/app.interceptor';
import { FooterComponent } from './_core/footer/footer.component';
import { HeaderComponent } from './_core/header/header.component';
import { DataService } from './_core/data.service';
import { DataResolver } from './_core/data.resolver';
import { environment } from '../environments/environment';
import { AppGuard } from './app.guard';
import { MessageService } from './message.service';
import { GatewayService } from './shared/gateway.service';
import { WindowRefService } from './window-ref.service';
/*
* Main module for the app that boostraps everything.
*/
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
HttpModule,
FormsModule,
AppRoutingModule,
BrowserAnimationsModule,
InMemoryWebApiModule.forRoot(InMemoryDataService, { dataEncapsulation: false }) // Remove to use real HTTP calls
],
declarations: [
RootComponent,
HeaderComponent,
AppComponent,
FooterComponent
],
providers: [
AppGuard,
DataService,
DataResolver,
MessageService,
GatewayService,
WindowRefService,
{
provide: HTTP_INTERCEPTORS,
useClass: AppInterceptor,
multi: true,
}
],
bootstrap: [RootComponent]
})
export class AppModule { }
// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
I've tried all the common solutions, including using the CopyWebpackPlugin to try to resolve the webpack bundling, adding the i18n directory to the assets list in angular-cli.json, and explicitly passing my path to TranslateHttpLoader like so:
return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
None of these approaches have worked.
Is there something very basic I'm missing in my setup? I feel like I've just been looking at it too long at this point. :/
http://localhost:4200/i18n/en.json
or not? – Anshuman JaiswalInterceptor
, yourAppInterceptor
prevent http call forassets/i18n/
json file. you should bypass thathttp
call. – hrdkisback