I am new to Angular and have been starting to build a new project with the next characteristics. Sorry if the explanation is not clear enough. I'm more than glad to provide more information but don't want to through my complete project to have irrelevant information.
I'm using
- Visual Studio 2015
- ASPNET CORE
- AspNetCore.Angular Services
- ngx-translate
I've followed the examples and instructions provided in [https://github.com/ngx-translate/core][1]
My code is working as expected being able to translate 4 languages and use the pipe directive in multiple components inside a router. This means that my json files are being loaded and are structured properly. I saw many reports in which there was an error on the json files but it is not my case.
However my problem is that i can't add the lines that setup the default language and language to be used in the constructor.
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app',
template: require('./app.component.html'),
styles: [require('./app.component.css')]
})
export class AppComponent {
constructor(private translate: TranslateService) {
translate.addLangs(["en", "fr", "es", "de"]);
//Error shows up when uncommenting any of the next two lines
//translate.setDefaultLang('en');
//translate.use('en');
}
}
Whenever i uncomment these lines i get the error
Exception: Call to Node module failed with error: SyntaxError: Unexpected token in JSON at position 0 at JSON.parse ()
If I comment these lines my application launches without problem and the only detail is that the language has not been setup yet. I can manually select it in a dropdown list and then everything works as expected.
Here is the html file
<div id="MainContainer" class='container-fluid'>
<div class='row'>
<label>
{{ "SELECT" | translate }}<br />
<select #langSelect (change)="translate.use(langSelect.value)">
<option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{ lang }}</option>
</select>
</label>
</div>
</div>
Here is my app module
import { NgModule } from '@angular/core';
import { UniversalModule } from 'angular2-universal'; // this automatically imports BrowserModule, HttpModule, and JsonpModule too.
import { Http } from '@angular/http';
import { FormsModule } from '@angular/forms'; // <-- ngModel lives here
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
// main Routing
import { AppRoutingModule } from './routers/app-routing.module';
// own Services
import { ImgService } from './services/img.service';
// own components
import { AppComponent } from './components/app/app.component';
import { NavMenuComponent } from './components/nav-menu/nav-menu.component';
import { CompleteSystemComponent } from './components/complete-system/complete-system.component';
import { VehicleComponent } from './components/vehicle/vehicle.component';
import { RobotComponent } from './components/robot/robot.component';
import { MapComponent } from './components/map/map.component';
import { StatusBarComponent } from './components/status-bar/status-bar.component';
import { RobotCameraComponent } from './components/robot-camera/robot-camera.component';
import { ImageProcessingComponent } from './components/image-processing/image-processing.component';
import { Station1Component } from './components/station1/station1.component';
import { Station2Component } from './components/station2/station2.component';
//AoT requires an exported function for factories
export function HttpLoaderFactory(http: Http) {
//return new TranslateHttpLoader(http, '/i18n/', '.json');
//return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
return new TranslateHttpLoader(http);
}
@NgModule({
imports: [
UniversalModule, // must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
FormsModule,
AppRoutingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [Http]
}
}),
],
declarations: [
AppComponent,
NavMenuComponent,
RobotComponent,
VehicleComponent,
CompleteSystemComponent,
MapComponent,
StatusBarComponent,
RobotCameraComponent,
ImageProcessingComponent,
Station1Component,
Station2Component
],
providers: [
ImgService
],
bootstrap: [AppComponent]
})
export class AppModule {
}
I would appreciate if some one could give me a hint on what to try. So far these are the things that I've tried to solve the problem
- Move the definition of default language and use to ngOnInit (failed the same way)
- Move the definition of default language and use to a function called with a button (It work without problems)
- Try to move the definition of default language and use to ngAfterViewInit() and ngAfterViewChecked().
I suspect that the problem is that the json files have not been loaded from the wwwroot directory or that the service is not able to find them there when the application is launching. After the application launched it finds them without a problem and everything works as expected.
Thanks again for your comments and let me know if more Information would help debug this issue