0

I am using the Ng2-Translate i am facing a problem where once the application is loaded i cannot able to see the translations from the json file.

Import that i have done:

import { TranslateService } from 'ng2-translate';

Code:

ngOnInit() {
    this.translations.instant('message');
}

But, when visiting the component again i can see that the translations are being loaded. I am loading this trnaslation in ngOnInit(). Can anybody suggest why its not loaded at first time.

App Module :

Code:

   import { TranslateLoader, TranslateModule, TranslateStaticLoader } from 'ng2-translate';


        export function createTranslateLoader(http: Http, config: Configuration) {
            return new TranslateStaticLoader(http, config.i18NUrl, '.json');
        }


@NgModule({
    imports: [  

        SharedModule.forRoot(),
        TranslateModule.forRoot({
            provide: TranslateLoader,
            useFactory: (createTranslateLoader),
            deps: [Http, Configuration],
        }),
        TranslationUtilModule,
        CovalentLayoutModule.forRoot(),
        FlexLayoutModule,
        BootstrapGrowlModule,
    ],
    declarations: [AppComponent],
    providers: [
        TranslateModule,        
        { provide: Configuration, useFactory: Configuration.createConfiguration(), deps: [] },
    ],
    bootstrap: [AppComponent],
})
Rohitesh
  • 1,514
  • 7
  • 28
  • 51
  • Are you using an old version of `ngx-translate`? I'm asking because now it must be imported like this `import { TranslateService } from '@ngx-translate/core';`. – developer033 Jun 21 '17 at 04:30
  • @developer033 i am using "ng2-translate": "5.0.0", which is latest. I am using ng2 translate not ngx-translate . – Rohitesh Jun 21 '17 at 04:31
  • I don't know if you know but `ng2-translate` was renamed to `@ngx-translate` and the latest version is "7.0.0". You can see [**this issue**](https://github.com/ngx-translate/core/issues/335) for more. – developer033 Jun 21 '17 at 04:33
  • @developer033 i was not aware of this thing thanks . But, my library is still working. Can you suggest what need to be done. – Rohitesh Jun 21 '17 at 04:35
  • Can you provide more code of app.module, component of default route ? – Thien Hoang Jun 21 '17 at 04:46
  • @ThienHoang please find the code for app.module in edit. – Rohitesh Jun 21 '17 at 04:51
  • `visiting the component again`, `the application is loaded` is it same component and did application load json file ? – Thien Hoang Jun 21 '17 at 05:00
  • @ThienHoang when i load my application the component where i am doing the translation is rendering the translation key as it is. e.g. if i want to do a translation for key hello.message which is comming from some json file then the output will be same hello.message. But, again when i am visiting the component again then this time value is getting rendered. – Rohitesh Jun 21 '17 at 05:04
  • @Rohitesh can you check network json file loaded or not when call application ? – Thien Hoang Jun 21 '17 at 07:21
  • @ThienHoang yes it getting loaded. – Rohitesh Jun 21 '17 at 07:53

0 Answers0